| 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_prefs.h" | 15 #include "chrome/browser/extensions/extension_prefs.h" |
| 16 #include "chrome/browser/extensions/extension_prefs_scope.h" |
| 16 #include "chrome/browser/extensions/extension_proxy_api.h" | 17 #include "chrome/browser/extensions/extension_proxy_api.h" |
| 17 #include "chrome/browser/extensions/extension_service.h" | 18 #include "chrome/browser/extensions/extension_service.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 20 #include "content/common/notification_type.h" | 21 #include "content/common/notification_type.h" |
| 21 #include "content/common/notification_service.h" | 22 #include "content/common/notification_service.h" |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 struct PrefMappingEntry { | 26 struct PrefMappingEntry { |
| 26 const char* extension_pref; | 27 const char* extension_pref; |
| 27 const char* browser_pref; | 28 const char* browser_pref; |
| 28 const char* permission; | 29 const char* permission; |
| 29 }; | 30 }; |
| 30 | 31 |
| 31 const char kNotControllable[] = "NotControllable"; | 32 const char kNotControllable[] = "NotControllable"; |
| 32 const char kControlledByOtherExtensions[] = "ControlledByOtherExtensions"; | 33 const char kControlledByOtherExtensions[] = "ControlledByOtherExtensions"; |
| 33 const char kControllableByThisExtension[] = "ControllableByThisExtension"; | 34 const char kControllableByThisExtension[] = "ControllableByThisExtension"; |
| 34 const char kControlledByThisExtension[] = "ControlledByThisExtension"; | 35 const char kControlledByThisExtension[] = "ControlledByThisExtension"; |
| 35 | 36 |
| 36 const char kIncognito[] = "incognito"; | 37 const char kIncognito[] = "incognito"; |
| 37 const char kIncognitoSpecific[] = "incognitoSpecific"; | 38 const char kIncognitoSpecific[] = "incognitoSpecific"; |
| 39 const char kScope[] = "scope"; |
| 38 const char kLevelOfControl[] = "levelOfControl"; | 40 const char kLevelOfControl[] = "levelOfControl"; |
| 41 const char kRegular[] = "regular"; |
| 42 const char kIncognitoPersistent[] = "incognito_persistent"; |
| 39 const char kValue[] = "value"; | 43 const char kValue[] = "value"; |
| 40 | 44 |
| 41 const char kOnPrefChangeFormat[] = | 45 const char kOnPrefChangeFormat[] = |
| 42 "preferences.%s.onChange"; | 46 "preferences.%s.onChange"; |
| 43 | 47 |
| 44 const char kIncognitoErrorMessage[] = | 48 const char kIncognitoErrorMessage[] = |
| 45 "You do not have permission to access incognito preferences."; | 49 "You do not have permission to access incognito preferences."; |
| 46 | 50 |
| 47 const char kPermissionErrorMessage[] = | 51 const char kPermissionErrorMessage[] = |
| 48 "You do not have permission to access the preference '%s'. " | 52 "You do not have permission to access the preference '%s'. " |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 | 106 |
| 103 if (ep->DoesExtensionControlPref(extension_id, browser_pref, incognito)) | 107 if (ep->DoesExtensionControlPref(extension_id, browser_pref, incognito)) |
| 104 return kControlledByThisExtension; | 108 return kControlledByThisExtension; |
| 105 | 109 |
| 106 if (ep->CanExtensionControlPref(extension_id, browser_pref, incognito)) | 110 if (ep->CanExtensionControlPref(extension_id, browser_pref, incognito)) |
| 107 return kControllableByThisExtension; | 111 return kControllableByThisExtension; |
| 108 | 112 |
| 109 return kControlledByOtherExtensions; | 113 return kControlledByOtherExtensions; |
| 110 } | 114 } |
| 111 | 115 |
| 116 bool StringToScope(const std::string& s, extension_prefs_scope::Scope* scope) { |
| 117 if (s == kRegular) |
| 118 *scope = extension_prefs_scope::kRegular; |
| 119 else if (s == kIncognitoPersistent) |
| 120 *scope = extension_prefs_scope::kIncognitoPersistent; |
| 121 else |
| 122 return false; |
| 123 return true; |
| 124 } |
| 125 |
| 112 class PrefMapping { | 126 class PrefMapping { |
| 113 public: | 127 public: |
| 114 static PrefMapping* GetInstance() { | 128 static PrefMapping* GetInstance() { |
| 115 return Singleton<PrefMapping>::get(); | 129 return Singleton<PrefMapping>::get(); |
| 116 } | 130 } |
| 117 | 131 |
| 118 bool FindBrowserPrefForExtensionPref(const std::string& extension_pref, | 132 bool FindBrowserPrefForExtensionPref(const std::string& extension_pref, |
| 119 std::string* browser_pref, | 133 std::string* browser_pref, |
| 120 std::string* permission) { | 134 std::string* permission) { |
| 121 std::map<std::string, std::pair<std::string, std::string> >::iterator it = | 135 std::map<std::string, std::pair<std::string, std::string> >::iterator it = |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 | 353 |
| 340 bool SetPreferenceFunction::RunImpl() { | 354 bool SetPreferenceFunction::RunImpl() { |
| 341 std::string pref_key; | 355 std::string pref_key; |
| 342 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | 356 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 343 DictionaryValue* details = NULL; | 357 DictionaryValue* details = NULL; |
| 344 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | 358 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
| 345 | 359 |
| 346 Value* value = NULL; | 360 Value* value = NULL; |
| 347 EXTENSION_FUNCTION_VALIDATE(details->Get(kValue, &value)); | 361 EXTENSION_FUNCTION_VALIDATE(details->Get(kValue, &value)); |
| 348 | 362 |
| 349 bool incognito = false; | 363 std::string scope_str = kRegular; |
| 364 if (details->HasKey(kScope)) |
| 365 EXTENSION_FUNCTION_VALIDATE(details->GetString(kScope, &scope_str)); |
| 350 | 366 |
| 351 // TODO(battre): enable incognito preferences again. | 367 extension_prefs_scope::Scope scope; |
| 352 // if (details->HasKey(kIncognito)) | 368 EXTENSION_FUNCTION_VALIDATE(StringToScope(scope_str, &scope)); |
| 353 // EXTENSION_FUNCTION_VALIDATE(details->GetBoolean(kIncognito, &incognito)); | |
| 354 | 369 |
| 355 if (incognito && !include_incognito()) { | 370 // TODO(battre): add kIncognitoSessionOnly |
| 356 error_ = kIncognitoErrorMessage; | 371 bool incognito = (scope == extension_prefs_scope::kIncognitoPersistent); |
| 357 return false; | 372 if (incognito) { |
| 373 // Regular profiles can't access incognito unless include_incognito is true. |
| 374 if (!profile()->IsOffTheRecord() && !include_incognito()) { |
| 375 error_ = kIncognitoErrorMessage; |
| 376 return false; |
| 377 } |
| 378 } else { |
| 379 // Incognito profiles can't access regular mode ever, they only exist in |
| 380 // split mode. |
| 381 if (profile()->IsOffTheRecord()) { |
| 382 error_ = "Can't modify regular settings from an incognito context."; |
| 383 return false; |
| 384 } |
| 358 } | 385 } |
| 359 | 386 |
| 360 std::string browser_pref; | 387 std::string browser_pref; |
| 361 std::string permission; | 388 std::string permission; |
| 362 EXTENSION_FUNCTION_VALIDATE( | 389 EXTENSION_FUNCTION_VALIDATE( |
| 363 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( | 390 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( |
| 364 pref_key, &browser_pref, &permission)); | 391 pref_key, &browser_pref, &permission)); |
| 365 if (!GetExtension()->HasApiPermission(permission)) { | 392 if (!GetExtension()->HasApiPermission(permission)) { |
| 366 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); | 393 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); |
| 367 return false; | 394 return false; |
| 368 } | 395 } |
| 369 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | 396 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
| 370 const PrefService::Preference* pref = | 397 const PrefService::Preference* pref = |
| 371 prefs->pref_service()->FindPreference(browser_pref.c_str()); | 398 prefs->pref_service()->FindPreference(browser_pref.c_str()); |
| 372 CHECK(pref); | 399 CHECK(pref); |
| 373 EXTENSION_FUNCTION_VALIDATE(value->GetType() == pref->GetType()); | 400 EXTENSION_FUNCTION_VALIDATE(value->GetType() == pref->GetType()); |
| 374 PrefTransformerInterface* transformer = | 401 PrefTransformerInterface* transformer = |
| 375 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); | 402 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); |
| 376 std::string error; | 403 std::string error; |
| 377 bool bad_message = false; | 404 bool bad_message = false; |
| 378 Value* browserPrefValue = | 405 Value* browserPrefValue = |
| 379 transformer->ExtensionToBrowserPref(value, &error, &bad_message); | 406 transformer->ExtensionToBrowserPref(value, &error, &bad_message); |
| 380 if (!browserPrefValue) { | 407 if (!browserPrefValue) { |
| 381 error_ = error; | 408 error_ = error; |
| 382 bad_message_ = bad_message; | 409 bad_message_ = bad_message; |
| 383 return false; | 410 return false; |
| 384 } | 411 } |
| 385 prefs->SetExtensionControlledPref(extension_id(), | 412 prefs->SetExtensionControlledPref(extension_id(), |
| 386 browser_pref, | 413 browser_pref, |
| 387 incognito, | 414 scope, |
| 388 browserPrefValue); | 415 browserPrefValue); |
| 389 return true; | 416 return true; |
| 390 } | 417 } |
| 391 | 418 |
| 392 ClearPreferenceFunction::~ClearPreferenceFunction() { } | 419 ClearPreferenceFunction::~ClearPreferenceFunction() { } |
| 393 | 420 |
| 394 bool ClearPreferenceFunction::RunImpl() { | 421 bool ClearPreferenceFunction::RunImpl() { |
| 395 std::string pref_key; | 422 std::string pref_key; |
| 396 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | 423 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 397 DictionaryValue* details = NULL; | 424 DictionaryValue* details = NULL; |
| 398 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | 425 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
| 399 | 426 |
| 400 bool incognito = false; | 427 std::string scope_str = kRegular; |
| 428 if (details->HasKey(kScope)) |
| 429 EXTENSION_FUNCTION_VALIDATE(details->GetString(kScope, &scope_str)); |
| 401 | 430 |
| 402 // TODO(battre): enable incognito preferences again. | 431 extension_prefs_scope::Scope scope; |
| 403 // if (details->HasKey(kIncognito)) | 432 EXTENSION_FUNCTION_VALIDATE(StringToScope(scope_str, &scope)); |
| 404 // EXTENSION_FUNCTION_VALIDATE(details->GetBoolean(kIncognito, &incognito)); | |
| 405 | 433 |
| 406 // We don't check incognito permissions here, as an extension should be always | 434 // TODO(battre): add kIncognitoSessionOnly |
| 407 // allowed to clear its own settings. | 435 bool incognito = (scope == extension_prefs_scope::kIncognitoPersistent); |
| 436 if (incognito) { |
| 437 // We don't check incognito permissions here, as an extension should be |
| 438 // always allowed to clear its own settings. |
| 439 } else { |
| 440 // Incognito profiles can't access regular mode ever, they only exist in |
| 441 // split mode. |
| 442 if (profile()->IsOffTheRecord()) { |
| 443 error_ = "Can't modify regular settings from an incognito context."; |
| 444 return false; |
| 445 } |
| 446 } |
| 408 | 447 |
| 409 std::string browser_pref; | 448 std::string browser_pref; |
| 410 std::string permission; | 449 std::string permission; |
| 411 EXTENSION_FUNCTION_VALIDATE( | 450 EXTENSION_FUNCTION_VALIDATE( |
| 412 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( | 451 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( |
| 413 pref_key, &browser_pref, &permission)); | 452 pref_key, &browser_pref, &permission)); |
| 414 if (!GetExtension()->HasApiPermission(permission)) { | 453 if (!GetExtension()->HasApiPermission(permission)) { |
| 415 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); | 454 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); |
| 416 return false; | 455 return false; |
| 417 } | 456 } |
| 418 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | 457 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
| 419 prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, incognito); | 458 prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, scope); |
| 420 return true; | 459 return true; |
| 421 } | 460 } |
| OLD | NEW |