| 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 kOnPrefChangeEffectiveFormat[] = | 45 const char kOnPrefChangeEffectiveFormat[] = |
| 42 "preferences.%s.onChangeEffective"; | 46 "preferences.%s.onChangeEffective"; |
| 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 extension_prefs_scope::Scope StringToScope(const std::string& s) { |
| 117 if (s == kRegular) { |
| 118 return extension_prefs_scope::kRegular; |
| 119 } else if (s == kIncognitoPersistent) { |
| 120 return extension_prefs_scope::kIncognitoPersistent; |
| 121 } else { |
| 122 // The extension API checks that no invalid string can be passed. |
| 123 NOTREACHED(); |
| 124 return extension_prefs_scope::kRegular; |
| 125 } |
| 126 } |
| 127 |
| 112 class PrefMapping { | 128 class PrefMapping { |
| 113 public: | 129 public: |
| 114 static PrefMapping* GetInstance() { | 130 static PrefMapping* GetInstance() { |
| 115 return Singleton<PrefMapping>::get(); | 131 return Singleton<PrefMapping>::get(); |
| 116 } | 132 } |
| 117 | 133 |
| 118 bool FindBrowserPrefForExtensionPref(const std::string& extension_pref, | 134 bool FindBrowserPrefForExtensionPref(const std::string& extension_pref, |
| 119 std::string* browser_pref, | 135 std::string* browser_pref, |
| 120 std::string* permission) { | 136 std::string* permission) { |
| 121 std::map<std::string, std::pair<std::string, std::string> >::iterator it = | 137 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 | 355 |
| 340 bool SetPreferenceFunction::RunImpl() { | 356 bool SetPreferenceFunction::RunImpl() { |
| 341 std::string pref_key; | 357 std::string pref_key; |
| 342 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | 358 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 343 DictionaryValue* details = NULL; | 359 DictionaryValue* details = NULL; |
| 344 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | 360 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
| 345 | 361 |
| 346 Value* value = NULL; | 362 Value* value = NULL; |
| 347 EXTENSION_FUNCTION_VALIDATE(details->Get(kValue, &value)); | 363 EXTENSION_FUNCTION_VALIDATE(details->Get(kValue, &value)); |
| 348 | 364 |
| 349 bool incognito = false; | 365 std::string scope = kRegular; |
| 366 if (details->HasKey(kScope)) |
| 367 EXTENSION_FUNCTION_VALIDATE(details->GetString(kScope, &scope)); |
| 350 | 368 |
| 351 // TODO(battre): enable incognito preferences again. | 369 // TODO(battre): add kIncognitoSessionOnly |
| 352 // if (details->HasKey(kIncognito)) | 370 bool incognito = (scope == kIncognitoPersistent); |
| 353 // EXTENSION_FUNCTION_VALIDATE(details->GetBoolean(kIncognito, &incognito)); | |
| 354 | |
| 355 if (incognito && !include_incognito()) { | 371 if (incognito && !include_incognito()) { |
| 356 error_ = kIncognitoErrorMessage; | 372 error_ = kIncognitoErrorMessage; |
| 357 return false; | 373 return false; |
| 358 } | 374 } |
| 359 | 375 |
| 360 std::string browser_pref; | 376 std::string browser_pref; |
| 361 std::string permission; | 377 std::string permission; |
| 362 EXTENSION_FUNCTION_VALIDATE( | 378 EXTENSION_FUNCTION_VALIDATE( |
| 363 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( | 379 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( |
| 364 pref_key, &browser_pref, &permission)); | 380 pref_key, &browser_pref, &permission)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 377 bool bad_message = false; | 393 bool bad_message = false; |
| 378 Value* browserPrefValue = | 394 Value* browserPrefValue = |
| 379 transformer->ExtensionToBrowserPref(value, &error, &bad_message); | 395 transformer->ExtensionToBrowserPref(value, &error, &bad_message); |
| 380 if (!browserPrefValue) { | 396 if (!browserPrefValue) { |
| 381 error_ = error; | 397 error_ = error; |
| 382 bad_message_ = bad_message; | 398 bad_message_ = bad_message; |
| 383 return false; | 399 return false; |
| 384 } | 400 } |
| 385 prefs->SetExtensionControlledPref(extension_id(), | 401 prefs->SetExtensionControlledPref(extension_id(), |
| 386 browser_pref, | 402 browser_pref, |
| 387 incognito, | 403 StringToScope(scope), |
| 388 browserPrefValue); | 404 browserPrefValue); |
| 389 return true; | 405 return true; |
| 390 } | 406 } |
| 391 | 407 |
| 392 ClearPreferenceFunction::~ClearPreferenceFunction() { } | 408 ClearPreferenceFunction::~ClearPreferenceFunction() { } |
| 393 | 409 |
| 394 bool ClearPreferenceFunction::RunImpl() { | 410 bool ClearPreferenceFunction::RunImpl() { |
| 395 std::string pref_key; | 411 std::string pref_key; |
| 396 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | 412 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 397 DictionaryValue* details = NULL; | 413 DictionaryValue* details = NULL; |
| 398 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | 414 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
| 399 | 415 |
| 400 bool incognito = false; | 416 std::string scope = kRegular; |
| 401 | 417 if (details->HasKey(kScope)) |
| 402 // TODO(battre): enable incognito preferences again. | 418 EXTENSION_FUNCTION_VALIDATE(details->GetString(kScope, &scope)); |
| 403 // if (details->HasKey(kIncognito)) | |
| 404 // EXTENSION_FUNCTION_VALIDATE(details->GetBoolean(kIncognito, &incognito)); | |
| 405 | 419 |
| 406 // We don't check incognito permissions here, as an extension should be always | 420 // We don't check incognito permissions here, as an extension should be always |
| 407 // allowed to clear its own settings. | 421 // allowed to clear its own settings. |
| 408 | 422 |
| 409 std::string browser_pref; | 423 std::string browser_pref; |
| 410 std::string permission; | 424 std::string permission; |
| 411 EXTENSION_FUNCTION_VALIDATE( | 425 EXTENSION_FUNCTION_VALIDATE( |
| 412 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( | 426 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( |
| 413 pref_key, &browser_pref, &permission)); | 427 pref_key, &browser_pref, &permission)); |
| 414 if (!GetExtension()->HasApiPermission(permission)) { | 428 if (!GetExtension()->HasApiPermission(permission)) { |
| 415 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); | 429 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); |
| 416 return false; | 430 return false; |
| 417 } | 431 } |
| 418 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | 432 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
| 419 prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, incognito); | 433 prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, |
| 434 StringToScope(scope)); |
| 420 return true; | 435 return true; |
| 421 } | 436 } |
| OLD | NEW |