Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/extension_preference_helpers.h" | 5 #include "chrome/browser/extensions/extension_preference_helpers.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | |
| 8 #include "base/values.h" | |
| 9 #include "chrome/browser/extensions/extension_event_router.h" | |
| 10 #include "chrome/browser/extensions/extension_prefs.h" | |
| 11 #include "chrome/browser/extensions/extension_service.h" | |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 | |
| 7 namespace { | 14 namespace { |
| 8 | 15 |
| 9 const char kIncognitoPersistent[] = "incognito_persistent"; | 16 const char kIncognitoPersistent[] = "incognito_persistent"; |
| 10 const char kIncognitoSessionOnly[] = "incognito_session_only"; | 17 const char kIncognitoSessionOnly[] = "incognito_session_only"; |
| 11 const char kRegular[] = "regular"; | 18 const char kRegular[] = "regular"; |
| 12 | 19 |
| 20 const char kLevelOfControlKey[] = "levelOfControl"; | |
| 21 | |
| 22 const char kNotControllable[] = "not_controllable"; | |
| 23 const char kControlledByOtherExtensions[] = "controlled_by_other_extensions"; | |
| 24 const char kControllableByThisExtension[] = "controllable_by_this_extension"; | |
| 25 const char kControlledByThisExtension[] = "controlled_by_this_extension"; | |
| 26 | |
| 13 } // namespace | 27 } // namespace |
| 14 | 28 |
| 15 namespace extension_preference_helpers { | 29 namespace extension_preference_helpers { |
| 16 | 30 |
| 17 bool StringToScope(const std::string& s, ExtensionPrefsScope* scope) { | 31 bool StringToScope(const std::string& s, ExtensionPrefsScope* scope) { |
| 18 if (s == kRegular) | 32 if (s == kRegular) |
| 19 *scope = kExtensionPrefsScopeRegular; | 33 *scope = kExtensionPrefsScopeRegular; |
| 20 else if (s == kIncognitoPersistent) | 34 else if (s == kIncognitoPersistent) |
| 21 *scope = kExtensionPrefsScopeIncognitoPersistent; | 35 *scope = kExtensionPrefsScopeIncognitoPersistent; |
| 22 else if (s == kIncognitoSessionOnly) | 36 else if (s == kIncognitoSessionOnly) |
| 23 *scope = kExtensionPrefsScopeIncognitoSessionOnly; | 37 *scope = kExtensionPrefsScopeIncognitoSessionOnly; |
| 24 else | 38 else |
| 25 return false; | 39 return false; |
| 26 return true; | 40 return true; |
| 27 } | 41 } |
| 28 | 42 |
| 43 const char* GetLevelOfControl( | |
| 44 Profile* profile, | |
| 45 const std::string& extension_id, | |
| 46 const std::string& browser_pref, | |
| 47 bool incognito) { | |
| 48 PrefService* prefs = incognito ? profile->GetOffTheRecordPrefs() | |
| 49 : profile->GetPrefs(); | |
| 50 const PrefService::Preference* pref = | |
| 51 prefs->FindPreference(browser_pref.c_str()); | |
| 52 CHECK(pref); | |
| 53 ExtensionPrefs* ep = profile->GetExtensionService()->extension_prefs(); | |
| 54 | |
| 55 if (!pref->IsExtensionModifiable()) | |
| 56 return kNotControllable; | |
| 57 | |
| 58 if (ep->DoesExtensionControlPref(extension_id, browser_pref, incognito)) | |
| 59 return kControlledByThisExtension; | |
| 60 | |
| 61 if (ep->CanExtensionControlPref(extension_id, browser_pref, incognito)) | |
| 62 return kControllableByThisExtension; | |
| 63 | |
| 64 return kControlledByOtherExtensions; | |
| 65 } | |
| 66 | |
| 67 void DispatchEventToExtensions( | |
| 68 Profile* profile, | |
| 69 const std::string& event_name, | |
| 70 ListValue* args, | |
| 71 ExtensionAPIPermission::ID permission, | |
| 72 bool incognito, | |
| 73 const char* browser_pref) { | |
|
Bernhard Bauer
2012/04/16 08:00:40
I think I'd prefer passing around std::string's fo
| |
| 74 ExtensionEventRouter* router = profile->GetExtensionEventRouter(); | |
| 75 if (!router || !router->HasEventListener(event_name)) | |
| 76 return; | |
| 77 ExtensionService* extension_service = profile->GetExtensionService(); | |
| 78 const ExtensionSet* extensions = extension_service->extensions(); | |
| 79 for (ExtensionSet::const_iterator it = extensions->begin(); | |
| 80 it != extensions->end(); ++it) { | |
| 81 std::string extension_id = (*it)->id(); | |
| 82 // TODO(bauerb): Only iterate over registered event listeners. | |
| 83 if (router->ExtensionHasEventListener(extension_id, event_name) && | |
| 84 (*it)->HasAPIPermission(permission) && | |
| 85 (!incognito || extension_service->CanCrossIncognito(*it))) { | |
| 86 std::string json_args; | |
| 87 | |
| 88 // Inject level of control key-value. | |
| 89 if (browser_pref) { | |
|
Bernhard Bauer
2012/04/16 08:00:40
If we don't have a caller that passes in NULL, I t
falken
2012/04/16 09:25:31
Done. My idea was to check in case someone wants t
| |
| 90 DictionaryValue* dict; | |
| 91 bool rv = args->GetDictionary(0, &dict); | |
| 92 DCHECK(rv); | |
| 93 | |
| 94 std::string level_of_control = | |
| 95 GetLevelOfControl(profile, extension_id, browser_pref, incognito); | |
| 96 dict->SetString(kLevelOfControlKey, level_of_control); | |
| 97 } | |
| 98 | |
| 99 base::JSONWriter::Write(args, &json_args); | |
| 100 router->DispatchEventToExtension( | |
| 101 extension_id, event_name, json_args, NULL, GURL()); | |
| 102 } | |
| 103 } | |
| 104 } | |
| 105 | |
| 29 } // namespace extension_preference_helpers | 106 } // namespace extension_preference_helpers |
| OLD | NEW |