[email protected] | 607e652 | 2012-01-25 08:14:52 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [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_api.h" |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 6 | |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 7 | #include <map> |
[email protected] | 8e7b2cf4 | 2012-04-18 14:26:58 | [diff] [blame] | 8 | #include <utility> |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 9 | |
[email protected] | c39df1a | 2013-01-08 18:22:07 | [diff] [blame] | 10 | #include "base/lazy_instance.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 11 | #include "base/memory/singleton.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame^] | 12 | #include "base/prefs/pref_service.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 13 | #include "base/stl_util.h" |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 14 | #include "base/stringprintf.h" |
| 15 | #include "base/values.h" |
[email protected] | 135e478 | 2012-09-19 20:36:52 | [diff] [blame] | 16 | #include "chrome/browser/extensions/api/preference/preference_api_constants.h" |
| 17 | #include "chrome/browser/extensions/api/preference/preference_helpers.h" |
[email protected] | c454fe67 | 2012-03-12 21:18:01 | [diff] [blame] | 18 | #include "chrome/browser/extensions/api/proxy/proxy_api.h" |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 19 | #include "chrome/browser/extensions/extension_prefs.h" |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 20 | #include "chrome/browser/extensions/extension_prefs_scope.h" |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 21 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 06bdd2b | 2012-11-30 18:47:13 | [diff] [blame] | 22 | #include "chrome/browser/extensions/extension_system.h" |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 23 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 24 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 25 | #include "chrome/common/extensions/permissions/api_permission.h" |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 26 | #include "chrome/common/pref_names.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 27 | #include "content/public/browser/notification_details.h" |
| 28 | #include "content/public/browser/notification_source.h" |
[email protected] | e9f541a | 2012-11-19 21:52:31 | [diff] [blame] | 29 | #include "extensions/common/error_utils.h" |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 30 | |
[email protected] | 135e478 | 2012-09-19 20:36:52 | [diff] [blame] | 31 | namespace keys = extensions::preference_api_constants; |
| 32 | namespace helpers = extensions::preference_helpers; |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 33 | |
[email protected] | 135e478 | 2012-09-19 20:36:52 | [diff] [blame] | 34 | namespace extensions { |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 35 | |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 36 | namespace { |
| 37 | |
| 38 | struct PrefMappingEntry { |
[email protected] | 80f95f51 | 2011-12-19 00:51:04 | [diff] [blame] | 39 | // Name of the preference referenced by the extension API JSON. |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 40 | const char* extension_pref; |
[email protected] | a4f9892 | 2011-12-06 11:32:23 | [diff] [blame] | 41 | |
| 42 | // Name of the preference in the PrefStores. |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 43 | const char* browser_pref; |
[email protected] | a4f9892 | 2011-12-06 11:32:23 | [diff] [blame] | 44 | |
| 45 | // Permission required to access this preference. |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 46 | // Use APIPermission::kInvalid for |permission| to express that no |
[email protected] | a4f9892 | 2011-12-06 11:32:23 | [diff] [blame] | 47 | // permission is necessary. |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 48 | APIPermission::ID permission; |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 49 | }; |
| 50 | |
[email protected] | 023208cf | 2011-06-02 23:56:41 | [diff] [blame] | 51 | const char kOnPrefChangeFormat[] = "types.ChromeSetting.%s.onChange"; |
[email protected] | ca2bbaf | 2012-10-22 10:36:21 | [diff] [blame] | 52 | const char kConversionErrorMessage[] = |
| 53 | "Internal error: Stored value for preference '*' cannot be converted " |
| 54 | "properly."; |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 55 | |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 56 | PrefMappingEntry kPrefMapping[] = { |
[email protected] | cc3a5c8 | 2012-05-09 16:54:57 | [diff] [blame] | 57 | #if defined(OS_CHROMEOS) |
| 58 | { "protectedContentEnabled", |
| 59 | prefs::kEnableCrosDRM, |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 60 | APIPermission::kPrivacy |
[email protected] | cc3a5c8 | 2012-05-09 16:54:57 | [diff] [blame] | 61 | }, |
| 62 | #endif // defined(OS_CHROMEOS) |
[email protected] | fbc66010 | 2011-08-26 05:41:21 | [diff] [blame] | 63 | { "alternateErrorPagesEnabled", |
| 64 | prefs::kAlternateErrorPagesEnabled, |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 65 | APIPermission::kPrivacy |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 66 | }, |
[email protected] | fbc66010 | 2011-08-26 05:41:21 | [diff] [blame] | 67 | { "autofillEnabled", |
| 68 | prefs::kAutofillEnabled, |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 69 | APIPermission::kPrivacy |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 70 | }, |
[email protected] | c9471ea | 2011-06-02 18:09:07 | [diff] [blame] | 71 | { "hyperlinkAuditingEnabled", |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 72 | prefs::kEnableHyperlinkAuditing, |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 73 | APIPermission::kPrivacy |
[email protected] | fbc66010 | 2011-08-26 05:41:21 | [diff] [blame] | 74 | }, |
| 75 | { "instantEnabled", |
| 76 | prefs::kInstantEnabled, |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 77 | APIPermission::kPrivacy |
[email protected] | fbc66010 | 2011-08-26 05:41:21 | [diff] [blame] | 78 | }, |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 79 | { "managedModeEnabled", |
| 80 | prefs::kInManagedMode, |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 81 | APIPermission::kManagedModePrivate |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 82 | }, |
[email protected] | fbc66010 | 2011-08-26 05:41:21 | [diff] [blame] | 83 | { "networkPredictionEnabled", |
| 84 | prefs::kNetworkPredictionEnabled, |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 85 | APIPermission::kPrivacy |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 86 | }, |
[email protected] | ccfdfbdf | 2011-02-24 12:41:46 | [diff] [blame] | 87 | { "proxy", |
| 88 | prefs::kProxy, |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 89 | APIPermission::kProxy |
[email protected] | ccfdfbdf | 2011-02-24 12:41:46 | [diff] [blame] | 90 | }, |
[email protected] | fbc66010 | 2011-08-26 05:41:21 | [diff] [blame] | 91 | { "referrersEnabled", |
| 92 | prefs::kEnableReferrers, |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 93 | APIPermission::kPrivacy |
[email protected] | fbc66010 | 2011-08-26 05:41:21 | [diff] [blame] | 94 | }, |
[email protected] | 75fc259 | 2012-04-05 13:00:33 | [diff] [blame] | 95 | { "safeBrowsingEnabled", |
| 96 | prefs::kSafeBrowsingEnabled, |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 97 | APIPermission::kPrivacy |
[email protected] | 75fc259 | 2012-04-05 13:00:33 | [diff] [blame] | 98 | }, |
[email protected] | fbc66010 | 2011-08-26 05:41:21 | [diff] [blame] | 99 | { "searchSuggestEnabled", |
| 100 | prefs::kSearchSuggestEnabled, |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 101 | APIPermission::kPrivacy |
[email protected] | fbc66010 | 2011-08-26 05:41:21 | [diff] [blame] | 102 | }, |
[email protected] | 75fc259 | 2012-04-05 13:00:33 | [diff] [blame] | 103 | { "spellingServiceEnabled", |
| 104 | prefs::kSpellCheckUseSpellingService, |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 105 | APIPermission::kPrivacy |
[email protected] | fbc66010 | 2011-08-26 05:41:21 | [diff] [blame] | 106 | }, |
| 107 | { "thirdPartyCookiesAllowed", |
| 108 | prefs::kBlockThirdPartyCookies, |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 109 | APIPermission::kPrivacy |
[email protected] | fbc66010 | 2011-08-26 05:41:21 | [diff] [blame] | 110 | }, |
| 111 | { "translationServiceEnabled", |
| 112 | prefs::kEnableTranslate, |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 113 | APIPermission::kPrivacy |
[email protected] | fbc66010 | 2011-08-26 05:41:21 | [diff] [blame] | 114 | } |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 115 | }; |
| 116 | |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 117 | class IdentityPrefTransformer : public PrefTransformerInterface { |
| 118 | public: |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 119 | virtual Value* ExtensionToBrowserPref(const Value* extension_pref, |
[email protected] | ca943e7 | 2011-05-03 16:58:04 | [diff] [blame] | 120 | std::string* error, |
[email protected] | 49aeab6 | 2013-02-07 02:53:11 | [diff] [blame] | 121 | bool* bad_message) OVERRIDE { |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 122 | return extension_pref->DeepCopy(); |
| 123 | } |
| 124 | |
[email protected] | 49aeab6 | 2013-02-07 02:53:11 | [diff] [blame] | 125 | virtual Value* BrowserToExtensionPref(const Value* browser_pref) OVERRIDE { |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 126 | return browser_pref->DeepCopy(); |
| 127 | } |
| 128 | }; |
| 129 | |
[email protected] | c9471ea | 2011-06-02 18:09:07 | [diff] [blame] | 130 | class InvertBooleanTransformer : public PrefTransformerInterface { |
| 131 | public: |
| 132 | virtual Value* ExtensionToBrowserPref(const Value* extension_pref, |
| 133 | std::string* error, |
[email protected] | 49aeab6 | 2013-02-07 02:53:11 | [diff] [blame] | 134 | bool* bad_message) OVERRIDE { |
[email protected] | c9471ea | 2011-06-02 18:09:07 | [diff] [blame] | 135 | return InvertBooleanValue(extension_pref); |
| 136 | } |
| 137 | |
[email protected] | 49aeab6 | 2013-02-07 02:53:11 | [diff] [blame] | 138 | virtual Value* BrowserToExtensionPref(const Value* browser_pref) OVERRIDE { |
[email protected] | c9471ea | 2011-06-02 18:09:07 | [diff] [blame] | 139 | return InvertBooleanValue(browser_pref); |
| 140 | } |
| 141 | |
| 142 | private: |
| 143 | static Value* InvertBooleanValue(const Value* value) { |
| 144 | bool bool_value = false; |
| 145 | bool result = value->GetAsBoolean(&bool_value); |
| 146 | DCHECK(result); |
| 147 | return Value::CreateBooleanValue(!bool_value); |
| 148 | } |
| 149 | }; |
| 150 | |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 151 | class PrefMapping { |
| 152 | public: |
| 153 | static PrefMapping* GetInstance() { |
| 154 | return Singleton<PrefMapping>::get(); |
| 155 | } |
| 156 | |
| 157 | bool FindBrowserPrefForExtensionPref(const std::string& extension_pref, |
| 158 | std::string* browser_pref, |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 159 | APIPermission::ID* permission) { |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 160 | PrefMap::iterator it = mapping_.find(extension_pref); |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 161 | if (it != mapping_.end()) { |
| 162 | *browser_pref = it->second.first; |
| 163 | *permission = it->second.second; |
| 164 | return true; |
| 165 | } |
| 166 | return false; |
| 167 | } |
| 168 | |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 169 | bool FindEventForBrowserPref(const std::string& browser_pref, |
| 170 | std::string* event_name, |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 171 | APIPermission::ID* permission) { |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 172 | PrefMap::iterator it = event_mapping_.find(browser_pref); |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 173 | if (it != event_mapping_.end()) { |
| 174 | *event_name = it->second.first; |
| 175 | *permission = it->second.second; |
| 176 | return true; |
| 177 | } |
| 178 | return false; |
| 179 | } |
| 180 | |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 181 | PrefTransformerInterface* FindTransformerForBrowserPref( |
| 182 | const std::string& browser_pref) { |
| 183 | std::map<std::string, PrefTransformerInterface*>::iterator it = |
| 184 | transformers_.find(browser_pref); |
| 185 | if (it != transformers_.end()) |
| 186 | return it->second; |
| 187 | else |
| 188 | return identity_transformer_.get(); |
| 189 | } |
| 190 | |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 191 | private: |
| 192 | friend struct DefaultSingletonTraits<PrefMapping>; |
| 193 | |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 194 | PrefMapping() { |
| 195 | identity_transformer_.reset(new IdentityPrefTransformer()); |
| 196 | for (size_t i = 0; i < arraysize(kPrefMapping); ++i) { |
| 197 | mapping_[kPrefMapping[i].extension_pref] = |
| 198 | std::make_pair(kPrefMapping[i].browser_pref, |
| 199 | kPrefMapping[i].permission); |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 200 | std::string event_name = |
| 201 | base::StringPrintf(kOnPrefChangeFormat, |
| 202 | kPrefMapping[i].extension_pref); |
| 203 | event_mapping_[kPrefMapping[i].browser_pref] = |
| 204 | std::make_pair(event_name, kPrefMapping[i].permission); |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 205 | } |
| 206 | DCHECK_EQ(arraysize(kPrefMapping), mapping_.size()); |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 207 | DCHECK_EQ(arraysize(kPrefMapping), event_mapping_.size()); |
[email protected] | da01ca5 | 2012-12-12 22:42:55 | [diff] [blame] | 208 | RegisterPrefTransformer(prefs::kProxy, new ProxyPrefTransformer()); |
[email protected] | c9471ea | 2011-06-02 18:09:07 | [diff] [blame] | 209 | RegisterPrefTransformer(prefs::kBlockThirdPartyCookies, |
| 210 | new InvertBooleanTransformer()); |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | ~PrefMapping() { |
| 214 | STLDeleteContainerPairSecondPointers(transformers_.begin(), |
| 215 | transformers_.end()); |
| 216 | } |
| 217 | |
| 218 | void RegisterPrefTransformer(const std::string& browser_pref, |
| 219 | PrefTransformerInterface* transformer) { |
| 220 | DCHECK_EQ(0u, transformers_.count(browser_pref)) << |
| 221 | "Trying to register pref transformer for " << browser_pref << " twice"; |
| 222 | transformers_[browser_pref] = transformer; |
| 223 | } |
| 224 | |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 225 | typedef std::map<std::string, |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 226 | std::pair<std::string, APIPermission::ID> > |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 227 | PrefMap; |
| 228 | |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 229 | // Mapping from extension pref keys to browser pref keys and permissions. |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 230 | PrefMap mapping_; |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 231 | |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 232 | // Mapping from browser pref keys to extension event names and permissions. |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 233 | PrefMap event_mapping_; |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 234 | |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 235 | // Mapping from browser pref keys to transformers. |
| 236 | std::map<std::string, PrefTransformerInterface*> transformers_; |
| 237 | |
| 238 | scoped_ptr<PrefTransformerInterface> identity_transformer_; |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 239 | |
| 240 | DISALLOW_COPY_AND_ASSIGN(PrefMapping); |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 241 | }; |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 242 | |
| 243 | } // namespace |
| 244 | |
[email protected] | 135e478 | 2012-09-19 20:36:52 | [diff] [blame] | 245 | PreferenceEventRouter::PreferenceEventRouter(Profile* profile) |
| 246 | : profile_(profile) { |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 247 | registrar_.Init(profile_->GetPrefs()); |
| 248 | incognito_registrar_.Init(profile_->GetOffTheRecordPrefs()); |
| 249 | for (size_t i = 0; i < arraysize(kPrefMapping); ++i) { |
[email protected] | 90bb38d | 2012-11-14 18:36:03 | [diff] [blame] | 250 | registrar_.Add(kPrefMapping[i].browser_pref, |
| 251 | base::Bind(&PreferenceEventRouter::OnPrefChanged, |
| 252 | base::Unretained(this), |
| 253 | registrar_.prefs())); |
| 254 | incognito_registrar_.Add(kPrefMapping[i].browser_pref, |
| 255 | base::Bind(&PreferenceEventRouter::OnPrefChanged, |
| 256 | base::Unretained(this), |
| 257 | incognito_registrar_.prefs())); |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
[email protected] | 135e478 | 2012-09-19 20:36:52 | [diff] [blame] | 261 | PreferenceEventRouter::~PreferenceEventRouter() { } |
[email protected] | 7da9a823 | 2011-05-27 21:07:11 | [diff] [blame] | 262 | |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 263 | void PreferenceEventRouter::OnPrefChanged(PrefServiceBase* pref_service, |
[email protected] | 135e478 | 2012-09-19 20:36:52 | [diff] [blame] | 264 | const std::string& browser_pref) { |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 265 | bool incognito = (pref_service != profile_->GetPrefs()); |
| 266 | |
| 267 | std::string event_name; |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 268 | APIPermission::ID permission = APIPermission::kInvalid; |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 269 | bool rv = PrefMapping::GetInstance()->FindEventForBrowserPref( |
| 270 | browser_pref, &event_name, &permission); |
| 271 | DCHECK(rv); |
| 272 | |
| 273 | ListValue args; |
| 274 | DictionaryValue* dict = new DictionaryValue(); |
| 275 | args.Append(dict); |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 276 | const PrefServiceBase::Preference* pref = |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 277 | pref_service->FindPreference(browser_pref.c_str()); |
| 278 | CHECK(pref); |
[email protected] | 06bdd2b | 2012-11-30 18:47:13 | [diff] [blame] | 279 | ExtensionService* extension_service = |
[email protected] | da01ca5 | 2012-12-12 22:42:55 | [diff] [blame] | 280 | ExtensionSystem::Get(profile_)->extension_service(); |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 281 | PrefTransformerInterface* transformer = |
| 282 | PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); |
[email protected] | ca2bbaf | 2012-10-22 10:36:21 | [diff] [blame] | 283 | Value* transformed_value = |
| 284 | transformer->BrowserToExtensionPref(pref->GetValue()); |
| 285 | if (!transformed_value) { |
[email protected] | da01ca5 | 2012-12-12 22:42:55 | [diff] [blame] | 286 | LOG(ERROR) << ErrorUtils::FormatErrorMessage(kConversionErrorMessage, |
| 287 | pref->name()); |
[email protected] | ca2bbaf | 2012-10-22 10:36:21 | [diff] [blame] | 288 | return; |
| 289 | } |
| 290 | |
| 291 | dict->Set(keys::kValue, transformed_value); |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 292 | if (incognito) { |
[email protected] | da01ca5 | 2012-12-12 22:42:55 | [diff] [blame] | 293 | ExtensionPrefs* ep = extension_service->extension_prefs(); |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 294 | dict->SetBoolean(keys::kIncognitoSpecific, |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 295 | ep->HasIncognitoPrefValue(browser_pref)); |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 296 | } |
| 297 | |
[email protected] | bdd3445 | 2012-04-16 15:58:40 | [diff] [blame] | 298 | helpers::DispatchEventToExtensions(profile_, |
| 299 | event_name, |
| 300 | &args, |
| 301 | permission, |
| 302 | incognito, |
| 303 | browser_pref); |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 304 | } |
| 305 | |
[email protected] | da01ca5 | 2012-12-12 22:42:55 | [diff] [blame] | 306 | PreferenceAPI::PreferenceAPI(Profile* profile) : profile_(profile) { |
| 307 | for (size_t i = 0; i < arraysize(kPrefMapping); ++i) { |
| 308 | std::string event_name; |
| 309 | APIPermission::ID permission = APIPermission::kInvalid; |
| 310 | bool rv = PrefMapping::GetInstance()->FindEventForBrowserPref( |
| 311 | kPrefMapping[i].browser_pref, &event_name, &permission); |
| 312 | DCHECK(rv); |
| 313 | ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( |
| 314 | this, event_name); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | PreferenceAPI::~PreferenceAPI() { |
| 319 | } |
| 320 | |
| 321 | void PreferenceAPI::Shutdown() { |
| 322 | ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); |
| 323 | } |
| 324 | |
[email protected] | c39df1a | 2013-01-08 18:22:07 | [diff] [blame] | 325 | static base::LazyInstance<ProfileKeyedAPIFactory<PreferenceAPI> > |
| 326 | g_factory = LAZY_INSTANCE_INITIALIZER; |
| 327 | |
| 328 | // static |
| 329 | ProfileKeyedAPIFactory<PreferenceAPI>* PreferenceAPI::GetFactoryInstance() { |
| 330 | return &g_factory.Get(); |
| 331 | } |
| 332 | |
[email protected] | da01ca5 | 2012-12-12 22:42:55 | [diff] [blame] | 333 | void PreferenceAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 334 | preference_event_router_.reset(new PreferenceEventRouter(profile_)); |
| 335 | ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); |
| 336 | } |
| 337 | |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 338 | PreferenceFunction::~PreferenceFunction() { } |
| 339 | |
| 340 | bool PreferenceFunction::ValidateBrowserPref( |
| 341 | const std::string& extension_pref_key, |
| 342 | std::string* browser_pref_key) { |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 343 | APIPermission::ID permission = APIPermission::kInvalid; |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 344 | EXTENSION_FUNCTION_VALIDATE( |
| 345 | PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( |
| 346 | extension_pref_key, browser_pref_key, &permission)); |
| 347 | if (!GetExtension()->HasAPIPermission(permission)) { |
[email protected] | e9f541a | 2012-11-19 21:52:31 | [diff] [blame] | 348 | error_ = ErrorUtils::FormatErrorMessage( |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 349 | keys::kPermissionErrorMessage, extension_pref_key); |
| 350 | return false; |
| 351 | } |
| 352 | return true; |
| 353 | } |
[email protected] | c433bcb | 2011-02-24 13:10:27 | [diff] [blame] | 354 | |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 355 | GetPreferenceFunction::~GetPreferenceFunction() { } |
| 356 | |
| 357 | bool GetPreferenceFunction::RunImpl() { |
| 358 | std::string pref_key; |
| 359 | EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 360 | DictionaryValue* details = NULL; |
| 361 | EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
| 362 | |
| 363 | bool incognito = false; |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 364 | if (details->HasKey(keys::kIncognitoKey)) |
| 365 | EXTENSION_FUNCTION_VALIDATE(details->GetBoolean(keys::kIncognitoKey, |
| 366 | &incognito)); |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 367 | |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 368 | // Check incognito access. |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 369 | if (incognito && !include_incognito()) { |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 370 | error_ = keys::kIncognitoErrorMessage; |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 371 | return false; |
| 372 | } |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 373 | |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 374 | // Obtain pref. |
| 375 | std::string browser_pref; |
| 376 | if (!ValidateBrowserPref(pref_key, &browser_pref)) |
| 377 | return false; |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 378 | PrefService* prefs = incognito ? profile_->GetOffTheRecordPrefs() |
| 379 | : profile_->GetPrefs(); |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 380 | const PrefService::Preference* pref = |
| 381 | prefs->FindPreference(browser_pref.c_str()); |
| 382 | CHECK(pref); |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 383 | |
| 384 | scoped_ptr<DictionaryValue> result(new DictionaryValue); |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 385 | |
| 386 | // Retrieve level of control. |
| 387 | std::string level_of_control = |
[email protected] | bdd3445 | 2012-04-16 15:58:40 | [diff] [blame] | 388 | helpers::GetLevelOfControl(profile_, extension_id(), browser_pref, |
| 389 | incognito); |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 390 | result->SetString(keys::kLevelOfControl, level_of_control); |
| 391 | |
| 392 | // Retrieve pref value. |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 393 | PrefTransformerInterface* transformer = |
| 394 | PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); |
[email protected] | ca2bbaf | 2012-10-22 10:36:21 | [diff] [blame] | 395 | Value* transformed_value = |
| 396 | transformer->BrowserToExtensionPref(pref->GetValue()); |
| 397 | if (!transformed_value) { |
| 398 | LOG(ERROR) << |
[email protected] | e9f541a | 2012-11-19 21:52:31 | [diff] [blame] | 399 | ErrorUtils::FormatErrorMessage(kConversionErrorMessage, |
[email protected] | ca2bbaf | 2012-10-22 10:36:21 | [diff] [blame] | 400 | pref->name()); |
| 401 | return false; |
| 402 | } |
| 403 | result->Set(keys::kValue, transformed_value); |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 404 | |
| 405 | // Retrieve incognito status. |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 406 | if (incognito) { |
[email protected] | da01ca5 | 2012-12-12 22:42:55 | [diff] [blame] | 407 | ExtensionPrefs* ep = |
| 408 | ExtensionSystem::Get(profile_)->extension_service()->extension_prefs(); |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 409 | result->SetBoolean(keys::kIncognitoSpecific, |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 410 | ep->HasIncognitoPrefValue(browser_pref)); |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 411 | } |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 412 | |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 413 | SetResult(result.release()); |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 414 | return true; |
| 415 | } |
| 416 | |
| 417 | SetPreferenceFunction::~SetPreferenceFunction() { } |
| 418 | |
| 419 | bool SetPreferenceFunction::RunImpl() { |
| 420 | std::string pref_key; |
| 421 | EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 422 | DictionaryValue* details = NULL; |
| 423 | EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
| 424 | |
| 425 | Value* value = NULL; |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 426 | EXTENSION_FUNCTION_VALIDATE(details->Get(keys::kValue, &value)); |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 427 | |
[email protected] | da01ca5 | 2012-12-12 22:42:55 | [diff] [blame] | 428 | ExtensionPrefsScope scope = kExtensionPrefsScopeRegular; |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 429 | if (details->HasKey(keys::kScopeKey)) { |
| 430 | std::string scope_str; |
| 431 | EXTENSION_FUNCTION_VALIDATE( |
| 432 | details->GetString(keys::kScopeKey, &scope_str)); |
[email protected] | 8b001f3 | 2011-05-25 19:03:23 | [diff] [blame] | 433 | |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 434 | EXTENSION_FUNCTION_VALIDATE(helpers::StringToScope(scope_str, &scope)); |
| 435 | } |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 436 | |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 437 | // Check incognito scope. |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 438 | bool incognito = |
[email protected] | da01ca5 | 2012-12-12 22:42:55 | [diff] [blame] | 439 | (scope == kExtensionPrefsScopeIncognitoPersistent || |
| 440 | scope == kExtensionPrefsScopeIncognitoSessionOnly); |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 441 | if (incognito) { |
| 442 | // Regular profiles can't access incognito unless include_incognito is true. |
| 443 | if (!profile()->IsOffTheRecord() && !include_incognito()) { |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 444 | error_ = keys::kIncognitoErrorMessage; |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 445 | return false; |
| 446 | } |
| 447 | } else { |
| 448 | // Incognito profiles can't access regular mode ever, they only exist in |
| 449 | // split mode. |
| 450 | if (profile()->IsOffTheRecord()) { |
| 451 | error_ = "Can't modify regular settings from an incognito context."; |
| 452 | return false; |
| 453 | } |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 454 | } |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 455 | |
[email protected] | da01ca5 | 2012-12-12 22:42:55 | [diff] [blame] | 456 | if (scope == kExtensionPrefsScopeIncognitoSessionOnly && |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 457 | !profile_->HasOffTheRecordProfile()) { |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 458 | error_ = keys::kIncognitoSessionOnlyErrorMessage; |
[email protected] | d20e8ce | 2011-05-25 23:54:25 | [diff] [blame] | 459 | return false; |
| 460 | } |
| 461 | |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 462 | // Obtain pref. |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 463 | std::string browser_pref; |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 464 | if (!ValidateBrowserPref(pref_key, &browser_pref)) |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 465 | return false; |
[email protected] | da01ca5 | 2012-12-12 22:42:55 | [diff] [blame] | 466 | ExtensionPrefs* prefs = |
| 467 | ExtensionSystem::Get(profile_)->extension_service()->extension_prefs(); |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 468 | const PrefService::Preference* pref = |
| 469 | prefs->pref_service()->FindPreference(browser_pref.c_str()); |
| 470 | CHECK(pref); |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 471 | |
| 472 | // Validate new value. |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 473 | EXTENSION_FUNCTION_VALIDATE(value->GetType() == pref->GetType()); |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 474 | PrefTransformerInterface* transformer = |
| 475 | PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); |
| 476 | std::string error; |
[email protected] | ca943e7 | 2011-05-03 16:58:04 | [diff] [blame] | 477 | bool bad_message = false; |
[email protected] | ca2bbaf | 2012-10-22 10:36:21 | [diff] [blame] | 478 | scoped_ptr<Value> browser_pref_value( |
| 479 | transformer->ExtensionToBrowserPref(value, &error, &bad_message)); |
| 480 | if (!browser_pref_value) { |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 481 | error_ = error; |
[email protected] | ca943e7 | 2011-05-03 16:58:04 | [diff] [blame] | 482 | bad_message_ = bad_message; |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 483 | return false; |
| 484 | } |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 485 | |
[email protected] | ca2bbaf | 2012-10-22 10:36:21 | [diff] [blame] | 486 | // Validate also that the stored value can be converted back by the |
| 487 | // transformer. |
| 488 | scoped_ptr<Value> extensionPrefValue( |
| 489 | transformer->BrowserToExtensionPref(browser_pref_value.get())); |
| 490 | if (!extensionPrefValue) { |
[email protected] | e9f541a | 2012-11-19 21:52:31 | [diff] [blame] | 491 | error_ = ErrorUtils::FormatErrorMessage(kConversionErrorMessage, |
[email protected] | ca2bbaf | 2012-10-22 10:36:21 | [diff] [blame] | 492 | pref->name()); |
| 493 | bad_message_ = true; |
| 494 | return false; |
| 495 | } |
| 496 | |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 497 | prefs->SetExtensionControlledPref(extension_id(), |
| 498 | browser_pref, |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 499 | scope, |
[email protected] | ca2bbaf | 2012-10-22 10:36:21 | [diff] [blame] | 500 | browser_pref_value.release()); |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 501 | return true; |
| 502 | } |
[email protected] | c433bcb | 2011-02-24 13:10:27 | [diff] [blame] | 503 | |
| 504 | ClearPreferenceFunction::~ClearPreferenceFunction() { } |
| 505 | |
| 506 | bool ClearPreferenceFunction::RunImpl() { |
| 507 | std::string pref_key; |
| 508 | EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 509 | DictionaryValue* details = NULL; |
| 510 | EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
| 511 | |
[email protected] | da01ca5 | 2012-12-12 22:42:55 | [diff] [blame] | 512 | ExtensionPrefsScope scope = kExtensionPrefsScopeRegular; |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 513 | if (details->HasKey(keys::kScopeKey)) { |
| 514 | std::string scope_str; |
| 515 | EXTENSION_FUNCTION_VALIDATE( |
| 516 | details->GetString(keys::kScopeKey, &scope_str)); |
[email protected] | 8b001f3 | 2011-05-25 19:03:23 | [diff] [blame] | 517 | |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 518 | EXTENSION_FUNCTION_VALIDATE(helpers::StringToScope(scope_str, &scope)); |
| 519 | } |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 520 | |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 521 | // Check incognito scope. |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 522 | bool incognito = |
[email protected] | da01ca5 | 2012-12-12 22:42:55 | [diff] [blame] | 523 | (scope == kExtensionPrefsScopeIncognitoPersistent || |
| 524 | scope == kExtensionPrefsScopeIncognitoSessionOnly); |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 525 | if (incognito) { |
| 526 | // We don't check incognito permissions here, as an extension should be |
| 527 | // always allowed to clear its own settings. |
| 528 | } else { |
| 529 | // Incognito profiles can't access regular mode ever, they only exist in |
| 530 | // split mode. |
| 531 | if (profile()->IsOffTheRecord()) { |
| 532 | error_ = "Can't modify regular settings from an incognito context."; |
| 533 | return false; |
| 534 | } |
| 535 | } |
[email protected] | c433bcb | 2011-02-24 13:10:27 | [diff] [blame] | 536 | |
| 537 | std::string browser_pref; |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 538 | if (!ValidateBrowserPref(pref_key, &browser_pref)) |
[email protected] | c433bcb | 2011-02-24 13:10:27 | [diff] [blame] | 539 | return false; |
[email protected] | dcac1fec | 2012-03-06 21:00:49 | [diff] [blame] | 540 | |
[email protected] | da01ca5 | 2012-12-12 22:42:55 | [diff] [blame] | 541 | ExtensionPrefs* prefs = |
| 542 | ExtensionSystem::Get(profile_)->extension_service()->extension_prefs(); |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 543 | prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, scope); |
[email protected] | c433bcb | 2011-02-24 13:10:27 | [diff] [blame] | 544 | return true; |
| 545 | } |
[email protected] | 135e478 | 2012-09-19 20:36:52 | [diff] [blame] | 546 | |
| 547 | } // namespace extensions |