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 std::string& browser_pref) { |
| 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 // Inject level of control key-value. |
| 87 DictionaryValue* dict; |
| 88 bool rv = args->GetDictionary(0, &dict); |
| 89 DCHECK(rv); |
| 90 std::string level_of_control = |
| 91 GetLevelOfControl(profile, extension_id, browser_pref, incognito); |
| 92 dict->SetString(kLevelOfControlKey, level_of_control); |
| 93 |
| 94 std::string json_args; |
| 95 base::JSONWriter::Write(args, &json_args); |
| 96 router->DispatchEventToExtension( |
| 97 extension_id, event_name, json_args, NULL, GURL()); |
| 98 } |
| 99 } |
| 100 } |
| 101 |
29 } // namespace extension_preference_helpers | 102 } // namespace extension_preference_helpers |
OLD | NEW |