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