[email protected] | bdd3445 | 2012-04-16 15:58:40 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 135e478 | 2012-09-19 20:36:52 | [diff] [blame] | 5 | #include "chrome/browser/extensions/api/preference/preference_helpers.h" |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 6 | |
[email protected] | bdd3445 | 2012-04-16 15:58:40 | [diff] [blame] | 7 | #include "base/json/json_writer.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 8 | #include "base/prefs/pref_service.h" |
[email protected] | bdd3445 | 2012-04-16 15:58:40 | [diff] [blame] | 9 | #include "base/values.h" |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 10 | #include "chrome/browser/extensions/event_router.h" |
[email protected] | bdd3445 | 2012-04-16 15:58:40 | [diff] [blame] | 11 | #include "chrome/browser/extensions/extension_prefs.h" |
| 12 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 9e5be1f9 | 2012-10-29 19:01:45 | [diff] [blame] | 13 | #include "chrome/browser/extensions/extension_system.h" |
[email protected] | bdd3445 | 2012-04-16 15:58:40 | [diff] [blame] | 14 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 06492ed | 2013-03-24 22:13:14 | [diff] [blame] | 15 | #include "chrome/common/extensions/incognito_handler.h" |
[email protected] | bdd3445 | 2012-04-16 15:58:40 | [diff] [blame] | 16 | |
[email protected] | 135e478 | 2012-09-19 20:36:52 | [diff] [blame] | 17 | namespace extensions { |
| 18 | namespace preference_helpers { |
| 19 | |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | const char kIncognitoPersistent[] = "incognito_persistent"; |
| 23 | const char kIncognitoSessionOnly[] = "incognito_session_only"; |
| 24 | const char kRegular[] = "regular"; |
[email protected] | 7d3a0e3 | 2012-06-14 22:37:29 | [diff] [blame] | 25 | const char kRegularOnly[] = "regular_only"; |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 26 | |
[email protected] | bdd3445 | 2012-04-16 15:58:40 | [diff] [blame] | 27 | const char kLevelOfControlKey[] = "levelOfControl"; |
| 28 | |
| 29 | const char kNotControllable[] = "not_controllable"; |
| 30 | const char kControlledByOtherExtensions[] = "controlled_by_other_extensions"; |
| 31 | const char kControllableByThisExtension[] = "controllable_by_this_extension"; |
| 32 | const char kControlledByThisExtension[] = "controlled_by_this_extension"; |
| 33 | |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 34 | } // namespace |
| 35 | |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 36 | bool StringToScope(const std::string& s, |
| 37 | extensions::ExtensionPrefsScope* scope) { |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 38 | if (s == kRegular) |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 39 | *scope = extensions::kExtensionPrefsScopeRegular; |
[email protected] | 7d3a0e3 | 2012-06-14 22:37:29 | [diff] [blame] | 40 | else if (s == kRegularOnly) |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 41 | *scope = extensions::kExtensionPrefsScopeRegularOnly; |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 42 | else if (s == kIncognitoPersistent) |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 43 | *scope = extensions::kExtensionPrefsScopeIncognitoPersistent; |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 44 | else if (s == kIncognitoSessionOnly) |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 45 | *scope = extensions::kExtensionPrefsScopeIncognitoSessionOnly; |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 46 | else |
| 47 | return false; |
| 48 | return true; |
| 49 | } |
| 50 | |
[email protected] | bdd3445 | 2012-04-16 15:58:40 | [diff] [blame] | 51 | const char* GetLevelOfControl( |
| 52 | Profile* profile, |
| 53 | const std::string& extension_id, |
| 54 | const std::string& browser_pref, |
| 55 | bool incognito) { |
| 56 | PrefService* prefs = incognito ? profile->GetOffTheRecordPrefs() |
| 57 | : profile->GetPrefs(); |
[email protected] | 42c037e | 2012-06-26 22:23:32 | [diff] [blame] | 58 | bool from_incognito = false; |
| 59 | bool* from_incognito_ptr = incognito ? &from_incognito : NULL; |
[email protected] | bdd3445 | 2012-04-16 15:58:40 | [diff] [blame] | 60 | const PrefService::Preference* pref = |
| 61 | prefs->FindPreference(browser_pref.c_str()); |
| 62 | CHECK(pref); |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 63 | extensions::ExtensionPrefs* ep = |
| 64 | profile->GetExtensionService()->extension_prefs(); |
[email protected] | bdd3445 | 2012-04-16 15:58:40 | [diff] [blame] | 65 | |
| 66 | if (!pref->IsExtensionModifiable()) |
| 67 | return kNotControllable; |
| 68 | |
[email protected] | 42c037e | 2012-06-26 22:23:32 | [diff] [blame] | 69 | if (ep->DoesExtensionControlPref(extension_id, |
| 70 | browser_pref, |
| 71 | from_incognito_ptr)) { |
[email protected] | bdd3445 | 2012-04-16 15:58:40 | [diff] [blame] | 72 | return kControlledByThisExtension; |
[email protected] | 42c037e | 2012-06-26 22:23:32 | [diff] [blame] | 73 | } |
[email protected] | bdd3445 | 2012-04-16 15:58:40 | [diff] [blame] | 74 | |
| 75 | if (ep->CanExtensionControlPref(extension_id, browser_pref, incognito)) |
| 76 | return kControllableByThisExtension; |
| 77 | |
| 78 | return kControlledByOtherExtensions; |
| 79 | } |
| 80 | |
| 81 | void DispatchEventToExtensions( |
| 82 | Profile* profile, |
| 83 | const std::string& event_name, |
| 84 | ListValue* args, |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 85 | extensions::APIPermission::ID permission, |
[email protected] | bdd3445 | 2012-04-16 15:58:40 | [diff] [blame] | 86 | bool incognito, |
| 87 | const std::string& browser_pref) { |
[email protected] | 9e5be1f9 | 2012-10-29 19:01:45 | [diff] [blame] | 88 | extensions::EventRouter* router = |
| 89 | extensions::ExtensionSystem::Get(profile)->event_router(); |
[email protected] | bdd3445 | 2012-04-16 15:58:40 | [diff] [blame] | 90 | if (!router || !router->HasEventListener(event_name)) |
| 91 | return; |
| 92 | ExtensionService* extension_service = profile->GetExtensionService(); |
| 93 | const ExtensionSet* extensions = extension_service->extensions(); |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 94 | extensions::ExtensionPrefs* extension_prefs = |
| 95 | extension_service->extension_prefs(); |
[email protected] | bdd3445 | 2012-04-16 15:58:40 | [diff] [blame] | 96 | for (ExtensionSet::const_iterator it = extensions->begin(); |
| 97 | it != extensions->end(); ++it) { |
| 98 | std::string extension_id = (*it)->id(); |
| 99 | // TODO(bauerb): Only iterate over registered event listeners. |
| 100 | if (router->ExtensionHasEventListener(extension_id, event_name) && |
| 101 | (*it)->HasAPIPermission(permission) && |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame^] | 102 | (!incognito || IncognitoInfo::IsSplitMode(it->get()) || |
| 103 | extension_service->CanCrossIncognito(it->get()))) { |
[email protected] | bdd3445 | 2012-04-16 15:58:40 | [diff] [blame] | 104 | // Inject level of control key-value. |
| 105 | DictionaryValue* dict; |
| 106 | bool rv = args->GetDictionary(0, &dict); |
| 107 | DCHECK(rv); |
| 108 | std::string level_of_control = |
| 109 | GetLevelOfControl(profile, extension_id, browser_pref, incognito); |
| 110 | dict->SetString(kLevelOfControlKey, level_of_control); |
[email protected] | 42c037e | 2012-06-26 22:23:32 | [diff] [blame] | 111 | |
| 112 | // If the extension is in incognito split mode, |
| 113 | // a) incognito pref changes are visible only to the incognito tabs |
| 114 | // b) regular pref changes are visible only to the incognito tabs if the |
| 115 | // incognito pref has not alredy been set |
| 116 | Profile* restrict_to_profile = NULL; |
| 117 | bool from_incognito = false; |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame^] | 118 | if (IncognitoInfo::IsSplitMode(it->get())) { |
[email protected] | 42c037e | 2012-06-26 22:23:32 | [diff] [blame] | 119 | if (incognito && extension_service->IsIncognitoEnabled(extension_id)) { |
| 120 | restrict_to_profile = profile->GetOffTheRecordProfile(); |
| 121 | } else if (!incognito && |
| 122 | extension_prefs->DoesExtensionControlPref( |
| 123 | extension_id, |
| 124 | browser_pref, |
| 125 | &from_incognito) && |
| 126 | from_incognito) { |
| 127 | restrict_to_profile = profile; |
| 128 | } |
| 129 | } |
| 130 | |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 131 | scoped_ptr<ListValue> args_copy(args->DeepCopy()); |
[email protected] | 01f7a804 | 2012-12-07 07:48:02 | [diff] [blame] | 132 | scoped_ptr<Event> event(new Event(event_name, args_copy.Pass())); |
| 133 | event->restrict_to_profile = restrict_to_profile; |
| 134 | router->DispatchEventToExtension(extension_id, event.Pass()); |
[email protected] | bdd3445 | 2012-04-16 15:58:40 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
[email protected] | 135e478 | 2012-09-19 20:36:52 | [diff] [blame] | 139 | } // namespace preference_helpers |
| 140 | } // namespace extensions |