blob: 7b18e784fc821917e01a2813f0a19b4fd0eb2e63 [file] [log] [blame]
[email protected]bdd34452012-04-16 15:58:401// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]fa0624262011-06-09 14:17:382// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]135e4782012-09-19 20:36:525#include "chrome/browser/extensions/api/preference/preference_helpers.h"
[email protected]fa0624262011-06-09 14:17:386
[email protected]bdd34452012-04-16 15:58:407#include "base/json/json_writer.h"
[email protected]3853a4c2013-02-11 17:15:578#include "base/prefs/pref_service.h"
[email protected]bdd34452012-04-16 15:58:409#include "base/values.h"
[email protected]5a38dfd2012-07-23 23:22:1010#include "chrome/browser/extensions/event_router.h"
[email protected]bdd34452012-04-16 15:58:4011#include "chrome/browser/extensions/extension_prefs.h"
12#include "chrome/browser/extensions/extension_service.h"
[email protected]9e5be1f92012-10-29 19:01:4513#include "chrome/browser/extensions/extension_system.h"
[email protected]bdd34452012-04-16 15:58:4014#include "chrome/browser/profiles/profile.h"
[email protected]06492ed2013-03-24 22:13:1415#include "chrome/common/extensions/incognito_handler.h"
[email protected]bdd34452012-04-16 15:58:4016
[email protected]135e4782012-09-19 20:36:5217namespace extensions {
18namespace preference_helpers {
19
[email protected]fa0624262011-06-09 14:17:3820namespace {
21
22const char kIncognitoPersistent[] = "incognito_persistent";
23const char kIncognitoSessionOnly[] = "incognito_session_only";
24const char kRegular[] = "regular";
[email protected]7d3a0e32012-06-14 22:37:2925const char kRegularOnly[] = "regular_only";
[email protected]fa0624262011-06-09 14:17:3826
[email protected]bdd34452012-04-16 15:58:4027const char kLevelOfControlKey[] = "levelOfControl";
28
29const char kNotControllable[] = "not_controllable";
30const char kControlledByOtherExtensions[] = "controlled_by_other_extensions";
31const char kControllableByThisExtension[] = "controllable_by_this_extension";
32const char kControlledByThisExtension[] = "controlled_by_this_extension";
33
[email protected]fa0624262011-06-09 14:17:3834} // namespace
35
[email protected]45759612012-07-10 17:21:2336bool StringToScope(const std::string& s,
37 extensions::ExtensionPrefsScope* scope) {
[email protected]fa0624262011-06-09 14:17:3838 if (s == kRegular)
[email protected]45759612012-07-10 17:21:2339 *scope = extensions::kExtensionPrefsScopeRegular;
[email protected]7d3a0e32012-06-14 22:37:2940 else if (s == kRegularOnly)
[email protected]45759612012-07-10 17:21:2341 *scope = extensions::kExtensionPrefsScopeRegularOnly;
[email protected]fa0624262011-06-09 14:17:3842 else if (s == kIncognitoPersistent)
[email protected]45759612012-07-10 17:21:2343 *scope = extensions::kExtensionPrefsScopeIncognitoPersistent;
[email protected]fa0624262011-06-09 14:17:3844 else if (s == kIncognitoSessionOnly)
[email protected]45759612012-07-10 17:21:2345 *scope = extensions::kExtensionPrefsScopeIncognitoSessionOnly;
[email protected]fa0624262011-06-09 14:17:3846 else
47 return false;
48 return true;
49}
50
[email protected]bdd34452012-04-16 15:58:4051const 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]42c037e2012-06-26 22:23:3258 bool from_incognito = false;
59 bool* from_incognito_ptr = incognito ? &from_incognito : NULL;
[email protected]bdd34452012-04-16 15:58:4060 const PrefService::Preference* pref =
61 prefs->FindPreference(browser_pref.c_str());
62 CHECK(pref);
[email protected]45759612012-07-10 17:21:2363 extensions::ExtensionPrefs* ep =
64 profile->GetExtensionService()->extension_prefs();
[email protected]bdd34452012-04-16 15:58:4065
66 if (!pref->IsExtensionModifiable())
67 return kNotControllable;
68
[email protected]42c037e2012-06-26 22:23:3269 if (ep->DoesExtensionControlPref(extension_id,
70 browser_pref,
71 from_incognito_ptr)) {
[email protected]bdd34452012-04-16 15:58:4072 return kControlledByThisExtension;
[email protected]42c037e2012-06-26 22:23:3273 }
[email protected]bdd34452012-04-16 15:58:4074
75 if (ep->CanExtensionControlPref(extension_id, browser_pref, incognito))
76 return kControllableByThisExtension;
77
78 return kControlledByOtherExtensions;
79}
80
81void DispatchEventToExtensions(
82 Profile* profile,
83 const std::string& event_name,
84 ListValue* args,
[email protected]c2e66e12012-06-27 06:27:0685 extensions::APIPermission::ID permission,
[email protected]bdd34452012-04-16 15:58:4086 bool incognito,
87 const std::string& browser_pref) {
[email protected]9e5be1f92012-10-29 19:01:4588 extensions::EventRouter* router =
89 extensions::ExtensionSystem::Get(profile)->event_router();
[email protected]bdd34452012-04-16 15:58:4090 if (!router || !router->HasEventListener(event_name))
91 return;
92 ExtensionService* extension_service = profile->GetExtensionService();
93 const ExtensionSet* extensions = extension_service->extensions();
[email protected]45759612012-07-10 17:21:2394 extensions::ExtensionPrefs* extension_prefs =
95 extension_service->extension_prefs();
[email protected]bdd34452012-04-16 15:58:4096 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]cadac622013-06-11 16:46:36102 (!incognito || IncognitoInfo::IsSplitMode(it->get()) ||
103 extension_service->CanCrossIncognito(it->get()))) {
[email protected]bdd34452012-04-16 15:58:40104 // 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]42c037e2012-06-26 22:23:32111
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]cadac622013-06-11 16:46:36118 if (IncognitoInfo::IsSplitMode(it->get())) {
[email protected]42c037e2012-06-26 22:23:32119 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]c9bd90f2012-08-07 23:58:15131 scoped_ptr<ListValue> args_copy(args->DeepCopy());
[email protected]01f7a8042012-12-07 07:48:02132 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]bdd34452012-04-16 15:58:40135 }
136 }
137}
138
[email protected]135e4782012-09-19 20:36:52139} // namespace preference_helpers
140} // namespace extensions