[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 1 | // Copyright (c) 2011 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_api.h" |
| 6 | |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 7 | #include <map> |
| 8 | |
| 9 | #include "base/json/json_writer.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 10 | #include "base/memory/singleton.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 11 | #include "base/stl_util.h" |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 12 | #include "base/stringprintf.h" |
| 13 | #include "base/values.h" |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 14 | #include "chrome/browser/extensions/extension_event_router.h" |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 15 | #include "chrome/browser/extensions/extension_preference_api_constants.h" |
| 16 | #include "chrome/browser/extensions/extension_preference_helpers.h" |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 17 | #include "chrome/browser/extensions/extension_prefs.h" |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 18 | #include "chrome/browser/extensions/extension_prefs_scope.h" |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 19 | #include "chrome/browser/extensions/extension_proxy_api.h" |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 20 | #include "chrome/browser/extensions/extension_service.h" |
| 21 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 22 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 23 | #include "chrome/common/extensions/extension_error_utils.h" |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 24 | #include "chrome/common/extensions/extension_permission_set.h" |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 25 | #include "chrome/common/pref_names.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame^] | 26 | #include "content/public/browser/notification_details.h" |
| 27 | #include "content/public/browser/notification_source.h" |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 28 | |
| 29 | namespace { |
| 30 | |
| 31 | struct PrefMappingEntry { |
| 32 | const char* extension_pref; |
| 33 | const char* browser_pref; |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 34 | ExtensionAPIPermission::ID permission; |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 35 | }; |
| 36 | |
[email protected] | d20e8ce | 2011-05-25 23:54:25 | [diff] [blame] | 37 | const char kNotControllable[] = "not_controllable"; |
| 38 | const char kControlledByOtherExtensions[] = "controlled_by_other_extensions"; |
| 39 | const char kControllableByThisExtension[] = "controllable_by_this_extension"; |
| 40 | const char kControlledByThisExtension[] = "controlled_by_this_extension"; |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 41 | |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 42 | const char kIncognitoSpecific[] = "incognitoSpecific"; |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 43 | const char kLevelOfControl[] = "levelOfControl"; |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 44 | const char kValue[] = "value"; |
| 45 | |
[email protected] | 023208cf | 2011-06-02 23:56:41 | [diff] [blame] | 46 | const char kOnPrefChangeFormat[] = "types.ChromeSetting.%s.onChange"; |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 47 | |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 48 | PrefMappingEntry kPrefMapping[] = { |
[email protected] | fbc66010 | 2011-08-26 05:41:21 | [diff] [blame] | 49 | { "alternateErrorPagesEnabled", |
| 50 | prefs::kAlternateErrorPagesEnabled, |
| 51 | ExtensionAPIPermission::kExperimental |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 52 | }, |
[email protected] | fbc66010 | 2011-08-26 05:41:21 | [diff] [blame] | 53 | { "autofillEnabled", |
| 54 | prefs::kAutofillEnabled, |
| 55 | ExtensionAPIPermission::kExperimental |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 56 | }, |
[email protected] | c9471ea | 2011-06-02 18:09:07 | [diff] [blame] | 57 | { "hyperlinkAuditingEnabled", |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 58 | prefs::kEnableHyperlinkAuditing, |
[email protected] | fbc66010 | 2011-08-26 05:41:21 | [diff] [blame] | 59 | ExtensionAPIPermission::kExperimental |
| 60 | }, |
| 61 | { "instantEnabled", |
| 62 | prefs::kInstantEnabled, |
| 63 | ExtensionAPIPermission::kExperimental |
| 64 | }, |
| 65 | // TODO(mkwst): come back to this once the UMA discussion has been resolved. |
| 66 | // { "metricsReportingEnabled", |
| 67 | // prefs::kMetricsReportingEnabled, |
| 68 | // ExtensionAPIPermission::kMetrics |
| 69 | // }, |
| 70 | { "networkPredictionEnabled", |
| 71 | prefs::kNetworkPredictionEnabled, |
| 72 | ExtensionAPIPermission::kExperimental |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 73 | }, |
[email protected] | ccfdfbdf | 2011-02-24 12:41:46 | [diff] [blame] | 74 | { "proxy", |
| 75 | prefs::kProxy, |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 76 | ExtensionAPIPermission::kProxy |
[email protected] | ccfdfbdf | 2011-02-24 12:41:46 | [diff] [blame] | 77 | }, |
[email protected] | fbc66010 | 2011-08-26 05:41:21 | [diff] [blame] | 78 | { "referrersEnabled", |
| 79 | prefs::kEnableReferrers, |
| 80 | ExtensionAPIPermission::kExperimental |
| 81 | }, |
| 82 | { "searchSuggestEnabled", |
| 83 | prefs::kSearchSuggestEnabled, |
| 84 | ExtensionAPIPermission::kExperimental |
| 85 | }, |
| 86 | { "safeBrowsingEnabled", |
| 87 | prefs::kSafeBrowsingEnabled, |
| 88 | ExtensionAPIPermission::kExperimental |
| 89 | }, |
| 90 | { "thirdPartyCookiesAllowed", |
| 91 | prefs::kBlockThirdPartyCookies, |
| 92 | ExtensionAPIPermission::kExperimental |
| 93 | }, |
| 94 | { "translationServiceEnabled", |
| 95 | prefs::kEnableTranslate, |
| 96 | ExtensionAPIPermission::kExperimental |
| 97 | } |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 98 | }; |
| 99 | |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 100 | class IdentityPrefTransformer : public PrefTransformerInterface { |
| 101 | public: |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 102 | virtual Value* ExtensionToBrowserPref(const Value* extension_pref, |
[email protected] | ca943e7 | 2011-05-03 16:58:04 | [diff] [blame] | 103 | std::string* error, |
| 104 | bool* bad_message) { |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 105 | return extension_pref->DeepCopy(); |
| 106 | } |
| 107 | |
| 108 | virtual Value* BrowserToExtensionPref(const Value* browser_pref) { |
| 109 | return browser_pref->DeepCopy(); |
| 110 | } |
| 111 | }; |
| 112 | |
[email protected] | c9471ea | 2011-06-02 18:09:07 | [diff] [blame] | 113 | class InvertBooleanTransformer : public PrefTransformerInterface { |
| 114 | public: |
| 115 | virtual Value* ExtensionToBrowserPref(const Value* extension_pref, |
| 116 | std::string* error, |
| 117 | bool* bad_message) { |
| 118 | return InvertBooleanValue(extension_pref); |
| 119 | } |
| 120 | |
| 121 | virtual Value* BrowserToExtensionPref(const Value* browser_pref) { |
| 122 | return InvertBooleanValue(browser_pref); |
| 123 | } |
| 124 | |
| 125 | private: |
| 126 | static Value* InvertBooleanValue(const Value* value) { |
| 127 | bool bool_value = false; |
| 128 | bool result = value->GetAsBoolean(&bool_value); |
| 129 | DCHECK(result); |
| 130 | return Value::CreateBooleanValue(!bool_value); |
| 131 | } |
| 132 | }; |
| 133 | |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 134 | // Returns a string constant (defined in the API) indicating the level of |
| 135 | // control this extension has over the specified preference. |
| 136 | const char* GetLevelOfControl( |
| 137 | Profile* profile, |
| 138 | const std::string& extension_id, |
| 139 | const std::string& browser_pref, |
| 140 | bool incognito) { |
| 141 | PrefService* prefs = incognito ? profile->GetOffTheRecordPrefs() |
| 142 | : profile->GetPrefs(); |
| 143 | const PrefService::Preference* pref = |
| 144 | prefs->FindPreference(browser_pref.c_str()); |
| 145 | CHECK(pref); |
| 146 | ExtensionPrefs* ep = profile->GetExtensionService()->extension_prefs(); |
| 147 | |
| 148 | if (!pref->IsExtensionModifiable()) |
| 149 | return kNotControllable; |
| 150 | |
| 151 | if (ep->DoesExtensionControlPref(extension_id, browser_pref, incognito)) |
| 152 | return kControlledByThisExtension; |
| 153 | |
| 154 | if (ep->CanExtensionControlPref(extension_id, browser_pref, incognito)) |
| 155 | return kControllableByThisExtension; |
| 156 | |
| 157 | return kControlledByOtherExtensions; |
| 158 | } |
| 159 | |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 160 | class PrefMapping { |
| 161 | public: |
| 162 | static PrefMapping* GetInstance() { |
| 163 | return Singleton<PrefMapping>::get(); |
| 164 | } |
| 165 | |
| 166 | bool FindBrowserPrefForExtensionPref(const std::string& extension_pref, |
| 167 | std::string* browser_pref, |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 168 | ExtensionAPIPermission::ID* permission) { |
| 169 | PrefMap::iterator it = mapping_.find(extension_pref); |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 170 | if (it != mapping_.end()) { |
| 171 | *browser_pref = it->second.first; |
| 172 | *permission = it->second.second; |
| 173 | return true; |
| 174 | } |
| 175 | return false; |
| 176 | } |
| 177 | |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 178 | bool FindEventForBrowserPref(const std::string& browser_pref, |
| 179 | std::string* event_name, |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 180 | ExtensionAPIPermission::ID* permission) { |
| 181 | PrefMap::iterator it = event_mapping_.find(browser_pref); |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 182 | if (it != event_mapping_.end()) { |
| 183 | *event_name = it->second.first; |
| 184 | *permission = it->second.second; |
| 185 | return true; |
| 186 | } |
| 187 | return false; |
| 188 | } |
| 189 | |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 190 | PrefTransformerInterface* FindTransformerForBrowserPref( |
| 191 | const std::string& browser_pref) { |
| 192 | std::map<std::string, PrefTransformerInterface*>::iterator it = |
| 193 | transformers_.find(browser_pref); |
| 194 | if (it != transformers_.end()) |
| 195 | return it->second; |
| 196 | else |
| 197 | return identity_transformer_.get(); |
| 198 | } |
| 199 | |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 200 | private: |
| 201 | friend struct DefaultSingletonTraits<PrefMapping>; |
| 202 | |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 203 | PrefMapping() { |
| 204 | identity_transformer_.reset(new IdentityPrefTransformer()); |
| 205 | for (size_t i = 0; i < arraysize(kPrefMapping); ++i) { |
| 206 | mapping_[kPrefMapping[i].extension_pref] = |
| 207 | std::make_pair(kPrefMapping[i].browser_pref, |
| 208 | kPrefMapping[i].permission); |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 209 | std::string event_name = |
| 210 | base::StringPrintf(kOnPrefChangeFormat, |
| 211 | kPrefMapping[i].extension_pref); |
| 212 | event_mapping_[kPrefMapping[i].browser_pref] = |
| 213 | std::make_pair(event_name, kPrefMapping[i].permission); |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 214 | } |
| 215 | DCHECK_EQ(arraysize(kPrefMapping), mapping_.size()); |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 216 | DCHECK_EQ(arraysize(kPrefMapping), event_mapping_.size()); |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 217 | RegisterPrefTransformer(prefs::kProxy, new ProxyPrefTransformer()); |
[email protected] | c9471ea | 2011-06-02 18:09:07 | [diff] [blame] | 218 | RegisterPrefTransformer(prefs::kBlockThirdPartyCookies, |
| 219 | new InvertBooleanTransformer()); |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | ~PrefMapping() { |
| 223 | STLDeleteContainerPairSecondPointers(transformers_.begin(), |
| 224 | transformers_.end()); |
| 225 | } |
| 226 | |
| 227 | void RegisterPrefTransformer(const std::string& browser_pref, |
| 228 | PrefTransformerInterface* transformer) { |
| 229 | DCHECK_EQ(0u, transformers_.count(browser_pref)) << |
| 230 | "Trying to register pref transformer for " << browser_pref << " twice"; |
| 231 | transformers_[browser_pref] = transformer; |
| 232 | } |
| 233 | |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 234 | typedef std::map<std::string, |
| 235 | std::pair<std::string, ExtensionAPIPermission::ID> > |
| 236 | PrefMap; |
| 237 | |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 238 | // Mapping from extension pref keys to browser pref keys and permissions. |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 239 | PrefMap mapping_; |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 240 | |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 241 | // Mapping from browser pref keys to extension event names and permissions. |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 242 | PrefMap event_mapping_; |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 243 | |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 244 | // Mapping from browser pref keys to transformers. |
| 245 | std::map<std::string, PrefTransformerInterface*> transformers_; |
| 246 | |
| 247 | scoped_ptr<PrefTransformerInterface> identity_transformer_; |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 248 | |
| 249 | DISALLOW_COPY_AND_ASSIGN(PrefMapping); |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 250 | }; |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 251 | |
| 252 | } // namespace |
| 253 | |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 254 | namespace keys = extension_preference_api_constants; |
| 255 | namespace helpers = extension_preference_helpers; |
| 256 | |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 257 | ExtensionPreferenceEventRouter::ExtensionPreferenceEventRouter( |
| 258 | Profile* profile) : profile_(profile) { |
| 259 | registrar_.Init(profile_->GetPrefs()); |
| 260 | incognito_registrar_.Init(profile_->GetOffTheRecordPrefs()); |
| 261 | for (size_t i = 0; i < arraysize(kPrefMapping); ++i) { |
| 262 | registrar_.Add(kPrefMapping[i].browser_pref, this); |
| 263 | incognito_registrar_.Add(kPrefMapping[i].browser_pref, this); |
| 264 | } |
| 265 | } |
| 266 | |
[email protected] | 7da9a823 | 2011-05-27 21:07:11 | [diff] [blame] | 267 | ExtensionPreferenceEventRouter::~ExtensionPreferenceEventRouter() { } |
| 268 | |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 269 | void ExtensionPreferenceEventRouter::Observe( |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 270 | int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 271 | const content::NotificationSource& source, |
| 272 | const content::NotificationDetails& details) { |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 273 | if (type == chrome::NOTIFICATION_PREF_CHANGED) { |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 274 | const std::string* pref_key = |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 275 | content::Details<const std::string>(details).ptr(); |
| 276 | OnPrefChanged(content::Source<PrefService>(source).ptr(), *pref_key); |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 277 | } else { |
| 278 | NOTREACHED(); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | void ExtensionPreferenceEventRouter::OnPrefChanged( |
| 283 | PrefService* pref_service, |
| 284 | const std::string& browser_pref) { |
| 285 | bool incognito = (pref_service != profile_->GetPrefs()); |
| 286 | |
| 287 | std::string event_name; |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 288 | ExtensionAPIPermission::ID permission = ExtensionAPIPermission::kInvalid; |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 289 | bool rv = PrefMapping::GetInstance()->FindEventForBrowserPref( |
| 290 | browser_pref, &event_name, &permission); |
| 291 | DCHECK(rv); |
| 292 | |
| 293 | ListValue args; |
| 294 | DictionaryValue* dict = new DictionaryValue(); |
| 295 | args.Append(dict); |
| 296 | const PrefService::Preference* pref = |
| 297 | pref_service->FindPreference(browser_pref.c_str()); |
| 298 | CHECK(pref); |
| 299 | ExtensionService* extension_service = profile_->GetExtensionService(); |
| 300 | PrefTransformerInterface* transformer = |
| 301 | PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); |
| 302 | dict->Set(kValue, transformer->BrowserToExtensionPref(pref->GetValue())); |
| 303 | if (incognito) { |
| 304 | ExtensionPrefs* ep = extension_service->extension_prefs(); |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 305 | dict->SetBoolean(kIncognitoSpecific, |
| 306 | ep->HasIncognitoPrefValue(browser_pref)); |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | ExtensionEventRouter* router = profile_->GetExtensionEventRouter(); |
| 310 | if (!router || !router->HasEventListener(event_name)) |
| 311 | return; |
| 312 | const ExtensionList* extensions = extension_service->extensions(); |
| 313 | for (ExtensionList::const_iterator it = extensions->begin(); |
| 314 | it != extensions->end(); ++it) { |
| 315 | std::string extension_id = (*it)->id(); |
| 316 | // TODO(bauerb): Only iterate over registered event listeners. |
| 317 | if (router->ExtensionHasEventListener(extension_id, event_name) && |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 318 | (*it)->HasAPIPermission(permission) && |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 319 | (!incognito || extension_service->CanCrossIncognito(*it))) { |
| 320 | std::string level_of_control = |
| 321 | GetLevelOfControl(profile_, extension_id, browser_pref, incognito); |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 322 | dict->SetString(kLevelOfControl, level_of_control); |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 323 | |
| 324 | std::string json_args; |
| 325 | base::JSONWriter::Write(&args, false, &json_args); |
| 326 | |
| 327 | DispatchEvent(extension_id, event_name, json_args); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | void ExtensionPreferenceEventRouter::DispatchEvent( |
| 333 | const std::string& extension_id, |
| 334 | const std::string& event_name, |
| 335 | const std::string& json_args) { |
| 336 | profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
| 337 | extension_id, event_name, json_args, NULL, GURL()); |
| 338 | } |
| 339 | |
[email protected] | c433bcb | 2011-02-24 13:10:27 | [diff] [blame] | 340 | // TODO(battre): Factor out common parts once this is stable. |
| 341 | |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 342 | GetPreferenceFunction::~GetPreferenceFunction() { } |
| 343 | |
| 344 | bool GetPreferenceFunction::RunImpl() { |
| 345 | std::string pref_key; |
| 346 | EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 347 | DictionaryValue* details = NULL; |
| 348 | EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
| 349 | |
| 350 | bool incognito = false; |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 351 | if (details->HasKey(keys::kIncognitoKey)) |
| 352 | EXTENSION_FUNCTION_VALIDATE(details->GetBoolean(keys::kIncognitoKey, |
| 353 | &incognito)); |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 354 | |
| 355 | if (incognito && !include_incognito()) { |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 356 | error_ = keys::kIncognitoErrorMessage; |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 357 | return false; |
| 358 | } |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 359 | |
| 360 | PrefService* prefs = incognito ? profile_->GetOffTheRecordPrefs() |
| 361 | : profile_->GetPrefs(); |
| 362 | std::string browser_pref; |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 363 | ExtensionAPIPermission::ID permission = ExtensionAPIPermission::kInvalid; |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 364 | EXTENSION_FUNCTION_VALIDATE( |
| 365 | PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( |
| 366 | pref_key, &browser_pref, &permission)); |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 367 | if (!GetExtension()->HasAPIPermission(permission)) { |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 368 | error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 369 | keys::kPermissionErrorMessage, pref_key); |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 370 | return false; |
| 371 | } |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 372 | |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 373 | const PrefService::Preference* pref = |
| 374 | prefs->FindPreference(browser_pref.c_str()); |
| 375 | CHECK(pref); |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 376 | std::string level_of_control = |
| 377 | GetLevelOfControl(profile_, extension_id(), browser_pref, incognito); |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 378 | |
| 379 | scoped_ptr<DictionaryValue> result(new DictionaryValue); |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 380 | PrefTransformerInterface* transformer = |
| 381 | PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); |
| 382 | result->Set(kValue, transformer->BrowserToExtensionPref(pref->GetValue())); |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 383 | result->SetString(kLevelOfControl, level_of_control); |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 384 | if (incognito) { |
| 385 | ExtensionPrefs* ep = profile_->GetExtensionService()->extension_prefs(); |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 386 | result->SetBoolean(kIncognitoSpecific, |
| 387 | ep->HasIncognitoPrefValue(browser_pref)); |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 388 | } |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 389 | result_.reset(result.release()); |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 390 | return true; |
| 391 | } |
| 392 | |
| 393 | SetPreferenceFunction::~SetPreferenceFunction() { } |
| 394 | |
| 395 | bool SetPreferenceFunction::RunImpl() { |
| 396 | std::string pref_key; |
| 397 | EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 398 | DictionaryValue* details = NULL; |
| 399 | EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
| 400 | |
| 401 | Value* value = NULL; |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 402 | EXTENSION_FUNCTION_VALIDATE(details->Get(kValue, &value)); |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 403 | |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 404 | ExtensionPrefsScope scope = kExtensionPrefsScopeRegular; |
| 405 | if (details->HasKey(keys::kScopeKey)) { |
| 406 | std::string scope_str; |
| 407 | EXTENSION_FUNCTION_VALIDATE( |
| 408 | details->GetString(keys::kScopeKey, &scope_str)); |
[email protected] | 8b001f3 | 2011-05-25 19:03:23 | [diff] [blame] | 409 | |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 410 | EXTENSION_FUNCTION_VALIDATE(helpers::StringToScope(scope_str, &scope)); |
| 411 | } |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 412 | |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 413 | bool incognito = (scope == kExtensionPrefsScopeIncognitoPersistent || |
| 414 | scope == kExtensionPrefsScopeIncognitoSessionOnly); |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 415 | if (incognito) { |
| 416 | // Regular profiles can't access incognito unless include_incognito is true. |
| 417 | if (!profile()->IsOffTheRecord() && !include_incognito()) { |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 418 | error_ = keys::kIncognitoErrorMessage; |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 419 | return false; |
| 420 | } |
| 421 | } else { |
| 422 | // Incognito profiles can't access regular mode ever, they only exist in |
| 423 | // split mode. |
| 424 | if (profile()->IsOffTheRecord()) { |
| 425 | error_ = "Can't modify regular settings from an incognito context."; |
| 426 | return false; |
| 427 | } |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 428 | } |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 429 | |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 430 | if (scope == kExtensionPrefsScopeIncognitoSessionOnly && |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 431 | !profile_->HasOffTheRecordProfile()) { |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 432 | error_ = keys::kIncognitoSessionOnlyErrorMessage; |
[email protected] | d20e8ce | 2011-05-25 23:54:25 | [diff] [blame] | 433 | return false; |
| 434 | } |
| 435 | |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 436 | std::string browser_pref; |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 437 | ExtensionAPIPermission::ID permission = ExtensionAPIPermission::kInvalid; |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 438 | EXTENSION_FUNCTION_VALIDATE( |
| 439 | PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( |
| 440 | pref_key, &browser_pref, &permission)); |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 441 | if (!GetExtension()->HasAPIPermission(permission)) { |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 442 | error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 443 | keys::kPermissionErrorMessage, pref_key); |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 444 | return false; |
| 445 | } |
| 446 | ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
| 447 | const PrefService::Preference* pref = |
| 448 | prefs->pref_service()->FindPreference(browser_pref.c_str()); |
| 449 | CHECK(pref); |
| 450 | EXTENSION_FUNCTION_VALIDATE(value->GetType() == pref->GetType()); |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 451 | PrefTransformerInterface* transformer = |
| 452 | PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); |
| 453 | std::string error; |
[email protected] | ca943e7 | 2011-05-03 16:58:04 | [diff] [blame] | 454 | bool bad_message = false; |
| 455 | Value* browserPrefValue = |
| 456 | transformer->ExtensionToBrowserPref(value, &error, &bad_message); |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 457 | if (!browserPrefValue) { |
| 458 | error_ = error; |
[email protected] | ca943e7 | 2011-05-03 16:58:04 | [diff] [blame] | 459 | bad_message_ = bad_message; |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 460 | return false; |
| 461 | } |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 462 | prefs->SetExtensionControlledPref(extension_id(), |
| 463 | browser_pref, |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 464 | scope, |
[email protected] | 3e334269 | 2011-03-15 12:03:27 | [diff] [blame] | 465 | browserPrefValue); |
[email protected] | 598bbcc | 2011-02-24 10:03:25 | [diff] [blame] | 466 | return true; |
| 467 | } |
[email protected] | c433bcb | 2011-02-24 13:10:27 | [diff] [blame] | 468 | |
| 469 | ClearPreferenceFunction::~ClearPreferenceFunction() { } |
| 470 | |
| 471 | bool ClearPreferenceFunction::RunImpl() { |
| 472 | std::string pref_key; |
| 473 | EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 474 | DictionaryValue* details = NULL; |
| 475 | EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
| 476 | |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 477 | ExtensionPrefsScope scope = kExtensionPrefsScopeRegular; |
| 478 | if (details->HasKey(keys::kScopeKey)) { |
| 479 | std::string scope_str; |
| 480 | EXTENSION_FUNCTION_VALIDATE( |
| 481 | details->GetString(keys::kScopeKey, &scope_str)); |
[email protected] | 8b001f3 | 2011-05-25 19:03:23 | [diff] [blame] | 482 | |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 483 | EXTENSION_FUNCTION_VALIDATE(helpers::StringToScope(scope_str, &scope)); |
| 484 | } |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 485 | |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 486 | bool incognito = (scope == kExtensionPrefsScopeIncognitoPersistent || |
| 487 | scope == kExtensionPrefsScopeIncognitoSessionOnly); |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 488 | if (incognito) { |
| 489 | // We don't check incognito permissions here, as an extension should be |
| 490 | // always allowed to clear its own settings. |
| 491 | } else { |
| 492 | // Incognito profiles can't access regular mode ever, they only exist in |
| 493 | // split mode. |
| 494 | if (profile()->IsOffTheRecord()) { |
| 495 | error_ = "Can't modify regular settings from an incognito context."; |
| 496 | return false; |
| 497 | } |
| 498 | } |
[email protected] | c433bcb | 2011-02-24 13:10:27 | [diff] [blame] | 499 | |
| 500 | std::string browser_pref; |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 501 | ExtensionAPIPermission::ID permission = ExtensionAPIPermission::kInvalid; |
[email protected] | c433bcb | 2011-02-24 13:10:27 | [diff] [blame] | 502 | EXTENSION_FUNCTION_VALIDATE( |
| 503 | PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( |
| 504 | pref_key, &browser_pref, &permission)); |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 505 | if (!GetExtension()->HasAPIPermission(permission)) { |
[email protected] | fa062426 | 2011-06-09 14:17:38 | [diff] [blame] | 506 | error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 507 | keys::kPermissionErrorMessage, pref_key); |
[email protected] | c433bcb | 2011-02-24 13:10:27 | [diff] [blame] | 508 | return false; |
| 509 | } |
| 510 | ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 511 | prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, scope); |
[email protected] | c433bcb | 2011-02-24 13:10:27 | [diff] [blame] | 512 | return true; |
| 513 | } |