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" |
(...skipping 22 matching lines...) Expand all Loading... | |
33 const char kControlledByOtherExtensions[] = "ControlledByOtherExtensions"; | 33 const char kControlledByOtherExtensions[] = "ControlledByOtherExtensions"; |
34 const char kControllableByThisExtension[] = "ControllableByThisExtension"; | 34 const char kControllableByThisExtension[] = "ControllableByThisExtension"; |
35 const char kControlledByThisExtension[] = "ControlledByThisExtension"; | 35 const char kControlledByThisExtension[] = "ControlledByThisExtension"; |
36 | 36 |
37 const char kIncognito[] = "incognito"; | 37 const char kIncognito[] = "incognito"; |
38 const char kIncognitoSpecific[] = "incognitoSpecific"; | 38 const char kIncognitoSpecific[] = "incognitoSpecific"; |
39 const char kScope[] = "scope"; | 39 const char kScope[] = "scope"; |
40 const char kLevelOfControl[] = "levelOfControl"; | 40 const char kLevelOfControl[] = "levelOfControl"; |
41 const char kRegular[] = "regular"; | 41 const char kRegular[] = "regular"; |
42 const char kIncognitoPersistent[] = "incognito_persistent"; | 42 const char kIncognitoPersistent[] = "incognito_persistent"; |
43 const char kIncognitoSessionOnly[] = "incognito_session_only"; | |
43 const char kValue[] = "value"; | 44 const char kValue[] = "value"; |
44 | 45 |
45 const char kOnPrefChangeFormat[] = | 46 const char kOnPrefChangeFormat[] = |
46 "preferences.%s.onChange"; | 47 "preferences.%s.onChange"; |
47 | 48 |
48 const char kIncognitoErrorMessage[] = | 49 const char kIncognitoErrorMessage[] = |
49 "You do not have permission to access incognito preferences."; | 50 "You do not have permission to access incognito preferences."; |
50 | 51 |
52 const char kIncognitoSessionOnlyErrorMessage[] = | |
53 "You cannot set 'session-only incognito' preferences when no incognito " | |
Matt Perry
2011/05/24 22:07:17
Reword: "You cannot set a preference with scope 'i
battre
2011/05/25 11:51:30
Done.
| |
54 "window exists."; | |
55 | |
51 const char kPermissionErrorMessage[] = | 56 const char kPermissionErrorMessage[] = |
52 "You do not have permission to access the preference '%s'. " | 57 "You do not have permission to access the preference '%s'. " |
53 "Be sure to declare in your manifest what permissions you need."; | 58 "Be sure to declare in your manifest what permissions you need."; |
54 | 59 |
55 PrefMappingEntry kPrefMapping[] = { | 60 PrefMappingEntry kPrefMapping[] = { |
56 { "blockThirdPartyCookies", | 61 { "blockThirdPartyCookies", |
57 prefs::kBlockThirdPartyCookies, | 62 prefs::kBlockThirdPartyCookies, |
58 Extension::kContentSettingsPermission | 63 Extension::kContentSettingsPermission |
59 }, | 64 }, |
60 { "enableReferrers", | 65 { "enableReferrers", |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 return kControllableByThisExtension; | 116 return kControllableByThisExtension; |
112 | 117 |
113 return kControlledByOtherExtensions; | 118 return kControlledByOtherExtensions; |
114 } | 119 } |
115 | 120 |
116 extension_prefs_scope::Scope StringToScope(const std::string& s) { | 121 extension_prefs_scope::Scope StringToScope(const std::string& s) { |
117 if (s == kRegular) { | 122 if (s == kRegular) { |
118 return extension_prefs_scope::kRegular; | 123 return extension_prefs_scope::kRegular; |
119 } else if (s == kIncognitoPersistent) { | 124 } else if (s == kIncognitoPersistent) { |
120 return extension_prefs_scope::kIncognitoPersistent; | 125 return extension_prefs_scope::kIncognitoPersistent; |
126 } else if (s == kIncognitoSessionOnly) { | |
127 return extension_prefs_scope::kIncognitoSessionOnly; | |
121 } else { | 128 } else { |
122 // The extension API checks that no invalid string can be passed. | 129 // The extension API checks that no invalid string can be passed. |
123 NOTREACHED(); | 130 NOTREACHED(); |
124 return extension_prefs_scope::kRegular; | 131 return extension_prefs_scope::kRegular; |
125 } | 132 } |
126 } | 133 } |
127 | 134 |
128 class PrefMapping { | 135 class PrefMapping { |
129 public: | 136 public: |
130 static PrefMapping* GetInstance() { | 137 static PrefMapping* GetInstance() { |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
355 | 362 |
356 bool SetPreferenceFunction::RunImpl() { | 363 bool SetPreferenceFunction::RunImpl() { |
357 std::string pref_key; | 364 std::string pref_key; |
358 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | 365 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
359 DictionaryValue* details = NULL; | 366 DictionaryValue* details = NULL; |
360 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | 367 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
361 | 368 |
362 Value* value = NULL; | 369 Value* value = NULL; |
363 EXTENSION_FUNCTION_VALIDATE(details->Get(kValue, &value)); | 370 EXTENSION_FUNCTION_VALIDATE(details->Get(kValue, &value)); |
364 | 371 |
365 std::string scope = kRegular; | 372 std::string scope_str = kRegular; |
366 if (details->HasKey(kScope)) | 373 if (details->HasKey(kScope)) |
367 EXTENSION_FUNCTION_VALIDATE(details->GetString(kScope, &scope)); | 374 EXTENSION_FUNCTION_VALIDATE(details->GetString(kScope, &scope_str)); |
368 | 375 |
369 // TODO(battre): add kIncognitoSessionOnly | 376 extension_prefs_scope::Scope scope = StringToScope(scope_str); |
370 bool incognito = (scope == kIncognitoPersistent); | 377 |
378 bool incognito = (scope == extension_prefs_scope::kIncognitoPersistent || | |
379 scope == extension_prefs_scope::kIncognitoSessionOnly); | |
371 if (incognito && !include_incognito()) { | 380 if (incognito && !include_incognito()) { |
372 error_ = kIncognitoErrorMessage; | 381 error_ = kIncognitoErrorMessage; |
373 return false; | 382 return false; |
374 } | 383 } |
375 | 384 |
385 if (scope == extension_prefs_scope::kIncognitoSessionOnly && | |
386 !profile_->HasOffTheRecordProfile()) { | |
387 error_ = kIncognitoSessionOnlyErrorMessage; | |
388 return false; | |
389 } | |
390 | |
376 std::string browser_pref; | 391 std::string browser_pref; |
377 std::string permission; | 392 std::string permission; |
378 EXTENSION_FUNCTION_VALIDATE( | 393 EXTENSION_FUNCTION_VALIDATE( |
379 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( | 394 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( |
380 pref_key, &browser_pref, &permission)); | 395 pref_key, &browser_pref, &permission)); |
381 if (!GetExtension()->HasApiPermission(permission)) { | 396 if (!GetExtension()->HasApiPermission(permission)) { |
382 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); | 397 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); |
383 return false; | 398 return false; |
384 } | 399 } |
385 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | 400 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
386 const PrefService::Preference* pref = | 401 const PrefService::Preference* pref = |
387 prefs->pref_service()->FindPreference(browser_pref.c_str()); | 402 prefs->pref_service()->FindPreference(browser_pref.c_str()); |
388 CHECK(pref); | 403 CHECK(pref); |
389 EXTENSION_FUNCTION_VALIDATE(value->GetType() == pref->GetType()); | 404 EXTENSION_FUNCTION_VALIDATE(value->GetType() == pref->GetType()); |
390 PrefTransformerInterface* transformer = | 405 PrefTransformerInterface* transformer = |
391 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); | 406 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); |
392 std::string error; | 407 std::string error; |
393 bool bad_message = false; | 408 bool bad_message = false; |
394 Value* browserPrefValue = | 409 Value* browserPrefValue = |
395 transformer->ExtensionToBrowserPref(value, &error, &bad_message); | 410 transformer->ExtensionToBrowserPref(value, &error, &bad_message); |
396 if (!browserPrefValue) { | 411 if (!browserPrefValue) { |
397 error_ = error; | 412 error_ = error; |
398 bad_message_ = bad_message; | 413 bad_message_ = bad_message; |
399 return false; | 414 return false; |
400 } | 415 } |
401 prefs->SetExtensionControlledPref(extension_id(), | 416 prefs->SetExtensionControlledPref(extension_id(), |
402 browser_pref, | 417 browser_pref, |
403 StringToScope(scope), | 418 scope, |
404 browserPrefValue); | 419 browserPrefValue); |
405 return true; | 420 return true; |
406 } | 421 } |
407 | 422 |
408 ClearPreferenceFunction::~ClearPreferenceFunction() { } | 423 ClearPreferenceFunction::~ClearPreferenceFunction() { } |
409 | 424 |
410 bool ClearPreferenceFunction::RunImpl() { | 425 bool ClearPreferenceFunction::RunImpl() { |
411 std::string pref_key; | 426 std::string pref_key; |
412 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | 427 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
413 DictionaryValue* details = NULL; | 428 DictionaryValue* details = NULL; |
(...skipping 13 matching lines...) Expand all Loading... | |
427 pref_key, &browser_pref, &permission)); | 442 pref_key, &browser_pref, &permission)); |
428 if (!GetExtension()->HasApiPermission(permission)) { | 443 if (!GetExtension()->HasApiPermission(permission)) { |
429 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); | 444 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); |
430 return false; | 445 return false; |
431 } | 446 } |
432 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | 447 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
433 prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, | 448 prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, |
434 StringToScope(scope)); | 449 StringToScope(scope)); |
435 return true; | 450 return true; |
436 } | 451 } |
OLD | NEW |