| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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_api.h" | 5 #include "chrome/browser/extensions/extension_preference_api.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
| 11 #include "base/stl_util-inl.h" | 11 #include "base/stl_util-inl.h" |
| 12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/extensions/extension_event_router.h" | 14 #include "chrome/browser/extensions/extension_event_router.h" |
| 15 #include "chrome/browser/extensions/extension_preference_api_constants.h" |
| 16 #include "chrome/browser/extensions/extension_preference_helpers.h" |
| 15 #include "chrome/browser/extensions/extension_prefs.h" | 17 #include "chrome/browser/extensions/extension_prefs.h" |
| 16 #include "chrome/browser/extensions/extension_prefs_scope.h" | 18 #include "chrome/browser/extensions/extension_prefs_scope.h" |
| 17 #include "chrome/browser/extensions/extension_proxy_api.h" | 19 #include "chrome/browser/extensions/extension_proxy_api.h" |
| 18 #include "chrome/browser/extensions/extension_service.h" | 20 #include "chrome/browser/extensions/extension_service.h" |
| 19 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/common/extensions/extension_error_utils.h" |
| 20 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
| 21 #include "content/common/notification_type.h" | 24 #include "content/common/notification_type.h" |
| 22 #include "content/common/notification_service.h" | 25 #include "content/common/notification_service.h" |
| 23 | 26 |
| 24 namespace { | 27 namespace { |
| 25 | 28 |
| 26 struct PrefMappingEntry { | 29 struct PrefMappingEntry { |
| 27 const char* extension_pref; | 30 const char* extension_pref; |
| 28 const char* browser_pref; | 31 const char* browser_pref; |
| 29 const char* permission; | 32 const char* permission; |
| 30 }; | 33 }; |
| 31 | 34 |
| 32 const char kNotControllable[] = "not_controllable"; | 35 const char kNotControllable[] = "not_controllable"; |
| 33 const char kControlledByOtherExtensions[] = "controlled_by_other_extensions"; | 36 const char kControlledByOtherExtensions[] = "controlled_by_other_extensions"; |
| 34 const char kControllableByThisExtension[] = "controllable_by_this_extension"; | 37 const char kControllableByThisExtension[] = "controllable_by_this_extension"; |
| 35 const char kControlledByThisExtension[] = "controlled_by_this_extension"; | 38 const char kControlledByThisExtension[] = "controlled_by_this_extension"; |
| 36 | 39 |
| 37 const char kIncognito[] = "incognito"; | |
| 38 const char kIncognitoSpecific[] = "incognitoSpecific"; | 40 const char kIncognitoSpecific[] = "incognitoSpecific"; |
| 39 const char kScope[] = "scope"; | |
| 40 const char kLevelOfControl[] = "levelOfControl"; | 41 const char kLevelOfControl[] = "levelOfControl"; |
| 41 const char kRegular[] = "regular"; | |
| 42 const char kIncognitoPersistent[] = "incognito_persistent"; | |
| 43 const char kIncognitoSessionOnly[] = "incognito_session_only"; | |
| 44 const char kValue[] = "value"; | 42 const char kValue[] = "value"; |
| 45 | 43 |
| 46 const char kOnPrefChangeFormat[] = "types.ChromeSetting.%s.onChange"; | 44 const char kOnPrefChangeFormat[] = "types.ChromeSetting.%s.onChange"; |
| 47 | 45 |
| 48 const char kIncognitoErrorMessage[] = | |
| 49 "You do not have permission to access incognito preferences."; | |
| 50 | |
| 51 const char kIncognitoSessionOnlyErrorMessage[] = | |
| 52 "You cannot set a preference with scope 'incognito_session_only' when no " | |
| 53 "incognito window is open."; | |
| 54 | |
| 55 const char kPermissionErrorMessage[] = | |
| 56 "You do not have permission to access the preference '%s'. " | |
| 57 "Be sure to declare in your manifest what permissions you need."; | |
| 58 | |
| 59 PrefMappingEntry kPrefMapping[] = { | 46 PrefMappingEntry kPrefMapping[] = { |
| 60 { "thirdPartyCookiesAllowed", | 47 { "thirdPartyCookiesAllowed", |
| 61 prefs::kBlockThirdPartyCookies, | 48 prefs::kBlockThirdPartyCookies, |
| 62 Extension::kContentSettingsPermission | 49 Extension::kContentSettingsPermission |
| 63 }, | 50 }, |
| 64 { "referrersEnabled", | 51 { "referrersEnabled", |
| 65 prefs::kEnableReferrers, | 52 prefs::kEnableReferrers, |
| 66 Extension::kContentSettingsPermission | 53 Extension::kContentSettingsPermission |
| 67 }, | 54 }, |
| 68 { "hyperlinkAuditingEnabled", | 55 { "hyperlinkAuditingEnabled", |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 | 115 |
| 129 if (ep->DoesExtensionControlPref(extension_id, browser_pref, incognito)) | 116 if (ep->DoesExtensionControlPref(extension_id, browser_pref, incognito)) |
| 130 return kControlledByThisExtension; | 117 return kControlledByThisExtension; |
| 131 | 118 |
| 132 if (ep->CanExtensionControlPref(extension_id, browser_pref, incognito)) | 119 if (ep->CanExtensionControlPref(extension_id, browser_pref, incognito)) |
| 133 return kControllableByThisExtension; | 120 return kControllableByThisExtension; |
| 134 | 121 |
| 135 return kControlledByOtherExtensions; | 122 return kControlledByOtherExtensions; |
| 136 } | 123 } |
| 137 | 124 |
| 138 bool StringToScope(const std::string& s, ExtensionPrefsScope* scope) { | |
| 139 if (s == kRegular) | |
| 140 *scope = kExtensionPrefsScopeRegular; | |
| 141 else if (s == kIncognitoPersistent) | |
| 142 *scope = kExtensionPrefsScopeIncognitoPersistent; | |
| 143 else if (s == kIncognitoSessionOnly) | |
| 144 *scope = kExtensionPrefsScopeIncognitoSessionOnly; | |
| 145 else | |
| 146 return false; | |
| 147 return true; | |
| 148 } | |
| 149 | |
| 150 class PrefMapping { | 125 class PrefMapping { |
| 151 public: | 126 public: |
| 152 static PrefMapping* GetInstance() { | 127 static PrefMapping* GetInstance() { |
| 153 return Singleton<PrefMapping>::get(); | 128 return Singleton<PrefMapping>::get(); |
| 154 } | 129 } |
| 155 | 130 |
| 156 bool FindBrowserPrefForExtensionPref(const std::string& extension_pref, | 131 bool FindBrowserPrefForExtensionPref(const std::string& extension_pref, |
| 157 std::string* browser_pref, | 132 std::string* browser_pref, |
| 158 std::string* permission) { | 133 std::string* permission) { |
| 159 std::map<std::string, std::pair<std::string, std::string> >::iterator it = | 134 std::map<std::string, std::pair<std::string, std::string> >::iterator it = |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 // Mapping from browser pref keys to transformers. | 207 // Mapping from browser pref keys to transformers. |
| 233 std::map<std::string, PrefTransformerInterface*> transformers_; | 208 std::map<std::string, PrefTransformerInterface*> transformers_; |
| 234 | 209 |
| 235 scoped_ptr<PrefTransformerInterface> identity_transformer_; | 210 scoped_ptr<PrefTransformerInterface> identity_transformer_; |
| 236 | 211 |
| 237 DISALLOW_COPY_AND_ASSIGN(PrefMapping); | 212 DISALLOW_COPY_AND_ASSIGN(PrefMapping); |
| 238 }; | 213 }; |
| 239 | 214 |
| 240 } // namespace | 215 } // namespace |
| 241 | 216 |
| 217 namespace keys = extension_preference_api_constants; |
| 218 namespace helpers = extension_preference_helpers; |
| 219 |
| 242 ExtensionPreferenceEventRouter::ExtensionPreferenceEventRouter( | 220 ExtensionPreferenceEventRouter::ExtensionPreferenceEventRouter( |
| 243 Profile* profile) : profile_(profile) { | 221 Profile* profile) : profile_(profile) { |
| 244 registrar_.Init(profile_->GetPrefs()); | 222 registrar_.Init(profile_->GetPrefs()); |
| 245 incognito_registrar_.Init(profile_->GetOffTheRecordPrefs()); | 223 incognito_registrar_.Init(profile_->GetOffTheRecordPrefs()); |
| 246 for (size_t i = 0; i < arraysize(kPrefMapping); ++i) { | 224 for (size_t i = 0; i < arraysize(kPrefMapping); ++i) { |
| 247 registrar_.Add(kPrefMapping[i].browser_pref, this); | 225 registrar_.Add(kPrefMapping[i].browser_pref, this); |
| 248 incognito_registrar_.Add(kPrefMapping[i].browser_pref, this); | 226 incognito_registrar_.Add(kPrefMapping[i].browser_pref, this); |
| 249 } | 227 } |
| 250 } | 228 } |
| 251 | 229 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 280 args.Append(dict); | 258 args.Append(dict); |
| 281 const PrefService::Preference* pref = | 259 const PrefService::Preference* pref = |
| 282 pref_service->FindPreference(browser_pref.c_str()); | 260 pref_service->FindPreference(browser_pref.c_str()); |
| 283 CHECK(pref); | 261 CHECK(pref); |
| 284 ExtensionService* extension_service = profile_->GetExtensionService(); | 262 ExtensionService* extension_service = profile_->GetExtensionService(); |
| 285 PrefTransformerInterface* transformer = | 263 PrefTransformerInterface* transformer = |
| 286 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); | 264 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); |
| 287 dict->Set(kValue, transformer->BrowserToExtensionPref(pref->GetValue())); | 265 dict->Set(kValue, transformer->BrowserToExtensionPref(pref->GetValue())); |
| 288 if (incognito) { | 266 if (incognito) { |
| 289 ExtensionPrefs* ep = extension_service->extension_prefs(); | 267 ExtensionPrefs* ep = extension_service->extension_prefs(); |
| 290 dict->Set( | 268 dict->SetBoolean(kIncognitoSpecific, |
| 291 kIncognitoSpecific, | 269 ep->HasIncognitoPrefValue(browser_pref)); |
| 292 Value::CreateBooleanValue(ep->HasIncognitoPrefValue(browser_pref))); | |
| 293 } | 270 } |
| 294 | 271 |
| 295 ExtensionEventRouter* router = profile_->GetExtensionEventRouter(); | 272 ExtensionEventRouter* router = profile_->GetExtensionEventRouter(); |
| 296 if (!router || !router->HasEventListener(event_name)) | 273 if (!router || !router->HasEventListener(event_name)) |
| 297 return; | 274 return; |
| 298 const ExtensionList* extensions = extension_service->extensions(); | 275 const ExtensionList* extensions = extension_service->extensions(); |
| 299 for (ExtensionList::const_iterator it = extensions->begin(); | 276 for (ExtensionList::const_iterator it = extensions->begin(); |
| 300 it != extensions->end(); ++it) { | 277 it != extensions->end(); ++it) { |
| 301 std::string extension_id = (*it)->id(); | 278 std::string extension_id = (*it)->id(); |
| 302 // TODO(bauerb): Only iterate over registered event listeners. | 279 // TODO(bauerb): Only iterate over registered event listeners. |
| 303 if (router->ExtensionHasEventListener(extension_id, event_name) && | 280 if (router->ExtensionHasEventListener(extension_id, event_name) && |
| 304 (*it)->HasApiPermission(permission) && | 281 (*it)->HasApiPermission(permission) && |
| 305 (!incognito || extension_service->CanCrossIncognito(*it))) { | 282 (!incognito || extension_service->CanCrossIncognito(*it))) { |
| 306 std::string level_of_control = | 283 std::string level_of_control = |
| 307 GetLevelOfControl(profile_, extension_id, browser_pref, incognito); | 284 GetLevelOfControl(profile_, extension_id, browser_pref, incognito); |
| 308 dict->Set(kLevelOfControl, Value::CreateStringValue(level_of_control)); | 285 dict->SetString(kLevelOfControl, level_of_control); |
| 309 | 286 |
| 310 std::string json_args; | 287 std::string json_args; |
| 311 base::JSONWriter::Write(&args, false, &json_args); | 288 base::JSONWriter::Write(&args, false, &json_args); |
| 312 | 289 |
| 313 DispatchEvent(extension_id, event_name, json_args); | 290 DispatchEvent(extension_id, event_name, json_args); |
| 314 } | 291 } |
| 315 } | 292 } |
| 316 } | 293 } |
| 317 | 294 |
| 318 void ExtensionPreferenceEventRouter::DispatchEvent( | 295 void ExtensionPreferenceEventRouter::DispatchEvent( |
| 319 const std::string& extension_id, | 296 const std::string& extension_id, |
| 320 const std::string& event_name, | 297 const std::string& event_name, |
| 321 const std::string& json_args) { | 298 const std::string& json_args) { |
| 322 profile_->GetExtensionEventRouter()->DispatchEventToExtension( | 299 profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
| 323 extension_id, event_name, json_args, NULL, GURL()); | 300 extension_id, event_name, json_args, NULL, GURL()); |
| 324 } | 301 } |
| 325 | 302 |
| 326 // TODO(battre): Factor out common parts once this is stable. | 303 // TODO(battre): Factor out common parts once this is stable. |
| 327 | 304 |
| 328 GetPreferenceFunction::~GetPreferenceFunction() { } | 305 GetPreferenceFunction::~GetPreferenceFunction() { } |
| 329 | 306 |
| 330 bool GetPreferenceFunction::RunImpl() { | 307 bool GetPreferenceFunction::RunImpl() { |
| 331 std::string pref_key; | 308 std::string pref_key; |
| 332 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | 309 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 333 DictionaryValue* details = NULL; | 310 DictionaryValue* details = NULL; |
| 334 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | 311 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
| 335 | 312 |
| 336 bool incognito = false; | 313 bool incognito = false; |
| 337 if (details->HasKey(kIncognito)) | 314 if (details->HasKey(keys::kIncognitoKey)) |
| 338 EXTENSION_FUNCTION_VALIDATE(details->GetBoolean(kIncognito, &incognito)); | 315 EXTENSION_FUNCTION_VALIDATE(details->GetBoolean(keys::kIncognitoKey, |
| 316 &incognito)); |
| 339 | 317 |
| 340 if (incognito && !include_incognito()) { | 318 if (incognito && !include_incognito()) { |
| 341 error_ = kIncognitoErrorMessage; | 319 error_ = keys::kIncognitoErrorMessage; |
| 342 return false; | 320 return false; |
| 343 } | 321 } |
| 344 | 322 |
| 345 PrefService* prefs = incognito ? profile_->GetOffTheRecordPrefs() | 323 PrefService* prefs = incognito ? profile_->GetOffTheRecordPrefs() |
| 346 : profile_->GetPrefs(); | 324 : profile_->GetPrefs(); |
| 347 std::string browser_pref; | 325 std::string browser_pref; |
| 348 std::string permission; | 326 std::string permission; |
| 349 EXTENSION_FUNCTION_VALIDATE( | 327 EXTENSION_FUNCTION_VALIDATE( |
| 350 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( | 328 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( |
| 351 pref_key, &browser_pref, &permission)); | 329 pref_key, &browser_pref, &permission)); |
| 352 if (!GetExtension()->HasApiPermission(permission)) { | 330 if (!GetExtension()->HasApiPermission(permission)) { |
| 353 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); | 331 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 332 keys::kPermissionErrorMessage, pref_key); |
| 354 return false; | 333 return false; |
| 355 } | 334 } |
| 356 | 335 |
| 357 const PrefService::Preference* pref = | 336 const PrefService::Preference* pref = |
| 358 prefs->FindPreference(browser_pref.c_str()); | 337 prefs->FindPreference(browser_pref.c_str()); |
| 359 CHECK(pref); | 338 CHECK(pref); |
| 360 std::string level_of_control = | 339 std::string level_of_control = |
| 361 GetLevelOfControl(profile_, extension_id(), browser_pref, incognito); | 340 GetLevelOfControl(profile_, extension_id(), browser_pref, incognito); |
| 362 | 341 |
| 363 scoped_ptr<DictionaryValue> result(new DictionaryValue); | 342 scoped_ptr<DictionaryValue> result(new DictionaryValue); |
| 364 PrefTransformerInterface* transformer = | 343 PrefTransformerInterface* transformer = |
| 365 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); | 344 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); |
| 366 result->Set(kValue, transformer->BrowserToExtensionPref(pref->GetValue())); | 345 result->Set(kValue, transformer->BrowserToExtensionPref(pref->GetValue())); |
| 367 result->Set(kLevelOfControl, Value::CreateStringValue(level_of_control)); | 346 result->SetString(kLevelOfControl, level_of_control); |
| 368 if (incognito) { | 347 if (incognito) { |
| 369 ExtensionPrefs* ep = profile_->GetExtensionService()->extension_prefs(); | 348 ExtensionPrefs* ep = profile_->GetExtensionService()->extension_prefs(); |
| 370 result->Set( | 349 result->SetBoolean(kIncognitoSpecific, |
| 371 kIncognitoSpecific, | 350 ep->HasIncognitoPrefValue(browser_pref)); |
| 372 Value::CreateBooleanValue(ep->HasIncognitoPrefValue(browser_pref))); | |
| 373 } | 351 } |
| 374 result_.reset(result.release()); | 352 result_.reset(result.release()); |
| 375 return true; | 353 return true; |
| 376 } | 354 } |
| 377 | 355 |
| 378 SetPreferenceFunction::~SetPreferenceFunction() { } | 356 SetPreferenceFunction::~SetPreferenceFunction() { } |
| 379 | 357 |
| 380 bool SetPreferenceFunction::RunImpl() { | 358 bool SetPreferenceFunction::RunImpl() { |
| 381 std::string pref_key; | 359 std::string pref_key; |
| 382 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | 360 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 383 DictionaryValue* details = NULL; | 361 DictionaryValue* details = NULL; |
| 384 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | 362 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
| 385 | 363 |
| 386 Value* value = NULL; | 364 Value* value = NULL; |
| 387 EXTENSION_FUNCTION_VALIDATE(details->Get(kValue, &value)); | 365 EXTENSION_FUNCTION_VALIDATE(details->Get(kValue, &value)); |
| 388 | 366 |
| 389 std::string scope_str = kRegular; | 367 ExtensionPrefsScope scope = kExtensionPrefsScopeRegular; |
| 390 if (details->HasKey(kScope)) | 368 if (details->HasKey(keys::kScopeKey)) { |
| 391 EXTENSION_FUNCTION_VALIDATE(details->GetString(kScope, &scope_str)); | 369 std::string scope_str; |
| 370 EXTENSION_FUNCTION_VALIDATE( |
| 371 details->GetString(keys::kScopeKey, &scope_str)); |
| 392 | 372 |
| 393 ExtensionPrefsScope scope; | 373 EXTENSION_FUNCTION_VALIDATE(helpers::StringToScope(scope_str, &scope)); |
| 394 EXTENSION_FUNCTION_VALIDATE(StringToScope(scope_str, &scope)); | 374 } |
| 395 | 375 |
| 396 bool incognito = (scope == kExtensionPrefsScopeIncognitoPersistent || | 376 bool incognito = (scope == kExtensionPrefsScopeIncognitoPersistent || |
| 397 scope == kExtensionPrefsScopeIncognitoSessionOnly); | 377 scope == kExtensionPrefsScopeIncognitoSessionOnly); |
| 398 if (incognito) { | 378 if (incognito) { |
| 399 // Regular profiles can't access incognito unless include_incognito is true. | 379 // Regular profiles can't access incognito unless include_incognito is true. |
| 400 if (!profile()->IsOffTheRecord() && !include_incognito()) { | 380 if (!profile()->IsOffTheRecord() && !include_incognito()) { |
| 401 error_ = kIncognitoErrorMessage; | 381 error_ = keys::kIncognitoErrorMessage; |
| 402 return false; | 382 return false; |
| 403 } | 383 } |
| 404 } else { | 384 } else { |
| 405 // Incognito profiles can't access regular mode ever, they only exist in | 385 // Incognito profiles can't access regular mode ever, they only exist in |
| 406 // split mode. | 386 // split mode. |
| 407 if (profile()->IsOffTheRecord()) { | 387 if (profile()->IsOffTheRecord()) { |
| 408 error_ = "Can't modify regular settings from an incognito context."; | 388 error_ = "Can't modify regular settings from an incognito context."; |
| 409 return false; | 389 return false; |
| 410 } | 390 } |
| 411 } | 391 } |
| 412 | 392 |
| 413 if (scope == kExtensionPrefsScopeIncognitoSessionOnly && | 393 if (scope == kExtensionPrefsScopeIncognitoSessionOnly && |
| 414 !profile_->HasOffTheRecordProfile()) { | 394 !profile_->HasOffTheRecordProfile()) { |
| 415 error_ = kIncognitoSessionOnlyErrorMessage; | 395 error_ = keys::kIncognitoSessionOnlyErrorMessage; |
| 416 return false; | 396 return false; |
| 417 } | 397 } |
| 418 | 398 |
| 419 if (scope == kExtensionPrefsScopeIncognitoSessionOnly && | |
| 420 !profile_->HasOffTheRecordProfile()) { | |
| 421 error_ = kIncognitoSessionOnlyErrorMessage; | |
| 422 return false; | |
| 423 } | |
| 424 | |
| 425 std::string browser_pref; | 399 std::string browser_pref; |
| 426 std::string permission; | 400 std::string permission; |
| 427 EXTENSION_FUNCTION_VALIDATE( | 401 EXTENSION_FUNCTION_VALIDATE( |
| 428 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( | 402 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( |
| 429 pref_key, &browser_pref, &permission)); | 403 pref_key, &browser_pref, &permission)); |
| 430 if (!GetExtension()->HasApiPermission(permission)) { | 404 if (!GetExtension()->HasApiPermission(permission)) { |
| 431 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); | 405 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 406 keys::kPermissionErrorMessage, pref_key); |
| 432 return false; | 407 return false; |
| 433 } | 408 } |
| 434 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | 409 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
| 435 const PrefService::Preference* pref = | 410 const PrefService::Preference* pref = |
| 436 prefs->pref_service()->FindPreference(browser_pref.c_str()); | 411 prefs->pref_service()->FindPreference(browser_pref.c_str()); |
| 437 CHECK(pref); | 412 CHECK(pref); |
| 438 EXTENSION_FUNCTION_VALIDATE(value->GetType() == pref->GetType()); | 413 EXTENSION_FUNCTION_VALIDATE(value->GetType() == pref->GetType()); |
| 439 PrefTransformerInterface* transformer = | 414 PrefTransformerInterface* transformer = |
| 440 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); | 415 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); |
| 441 std::string error; | 416 std::string error; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 455 } | 430 } |
| 456 | 431 |
| 457 ClearPreferenceFunction::~ClearPreferenceFunction() { } | 432 ClearPreferenceFunction::~ClearPreferenceFunction() { } |
| 458 | 433 |
| 459 bool ClearPreferenceFunction::RunImpl() { | 434 bool ClearPreferenceFunction::RunImpl() { |
| 460 std::string pref_key; | 435 std::string pref_key; |
| 461 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | 436 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 462 DictionaryValue* details = NULL; | 437 DictionaryValue* details = NULL; |
| 463 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | 438 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
| 464 | 439 |
| 465 std::string scope_str = kRegular; | 440 ExtensionPrefsScope scope = kExtensionPrefsScopeRegular; |
| 466 if (details->HasKey(kScope)) | 441 if (details->HasKey(keys::kScopeKey)) { |
| 467 EXTENSION_FUNCTION_VALIDATE(details->GetString(kScope, &scope_str)); | 442 std::string scope_str; |
| 443 EXTENSION_FUNCTION_VALIDATE( |
| 444 details->GetString(keys::kScopeKey, &scope_str)); |
| 468 | 445 |
| 469 ExtensionPrefsScope scope; | 446 EXTENSION_FUNCTION_VALIDATE(helpers::StringToScope(scope_str, &scope)); |
| 470 EXTENSION_FUNCTION_VALIDATE(StringToScope(scope_str, &scope)); | 447 } |
| 471 | 448 |
| 472 bool incognito = (scope == kExtensionPrefsScopeIncognitoPersistent || | 449 bool incognito = (scope == kExtensionPrefsScopeIncognitoPersistent || |
| 473 scope == kExtensionPrefsScopeIncognitoSessionOnly); | 450 scope == kExtensionPrefsScopeIncognitoSessionOnly); |
| 474 if (incognito) { | 451 if (incognito) { |
| 475 // We don't check incognito permissions here, as an extension should be | 452 // We don't check incognito permissions here, as an extension should be |
| 476 // always allowed to clear its own settings. | 453 // always allowed to clear its own settings. |
| 477 } else { | 454 } else { |
| 478 // Incognito profiles can't access regular mode ever, they only exist in | 455 // Incognito profiles can't access regular mode ever, they only exist in |
| 479 // split mode. | 456 // split mode. |
| 480 if (profile()->IsOffTheRecord()) { | 457 if (profile()->IsOffTheRecord()) { |
| 481 error_ = "Can't modify regular settings from an incognito context."; | 458 error_ = "Can't modify regular settings from an incognito context."; |
| 482 return false; | 459 return false; |
| 483 } | 460 } |
| 484 } | 461 } |
| 485 | 462 |
| 486 std::string browser_pref; | 463 std::string browser_pref; |
| 487 std::string permission; | 464 std::string permission; |
| 488 EXTENSION_FUNCTION_VALIDATE( | 465 EXTENSION_FUNCTION_VALIDATE( |
| 489 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( | 466 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( |
| 490 pref_key, &browser_pref, &permission)); | 467 pref_key, &browser_pref, &permission)); |
| 491 if (!GetExtension()->HasApiPermission(permission)) { | 468 if (!GetExtension()->HasApiPermission(permission)) { |
| 492 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); | 469 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 470 keys::kPermissionErrorMessage, pref_key); |
| 493 return false; | 471 return false; |
| 494 } | 472 } |
| 495 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | 473 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
| 496 prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, scope); | 474 prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, scope); |
| 497 return true; | 475 return true; |
| 498 } | 476 } |
| OLD | NEW |