Chromium Code Reviews| 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 51 | 51 |
| 52 const char kIncognitoSessionOnlyErrorMessage[] = | 52 const char kIncognitoSessionOnlyErrorMessage[] = |
| 53 "You cannot set a preference with scope 'incognito_session_only' when no " | 53 "You cannot set a preference with scope 'incognito_session_only' when no " |
| 54 "incognito window is open."; | 54 "incognito window is open."; |
| 55 | 55 |
| 56 const char kPermissionErrorMessage[] = | 56 const char kPermissionErrorMessage[] = |
| 57 "You do not have permission to access the preference '%s'. " | 57 "You do not have permission to access the preference '%s'. " |
| 58 "Be sure to declare in your manifest what permissions you need."; | 58 "Be sure to declare in your manifest what permissions you need."; |
| 59 | 59 |
| 60 PrefMappingEntry kPrefMapping[] = { | 60 PrefMappingEntry kPrefMapping[] = { |
| 61 { "blockThirdPartyCookies", | 61 { "thirdPartyCookiesAllowed", |
| 62 prefs::kBlockThirdPartyCookies, | 62 prefs::kBlockThirdPartyCookies, |
| 63 Extension::kContentSettingsPermission | 63 Extension::kContentSettingsPermission |
| 64 }, | 64 }, |
| 65 { "enableReferrers", | 65 { "referrersEnabled", |
| 66 prefs::kEnableReferrers, | 66 prefs::kEnableReferrers, |
| 67 Extension::kContentSettingsPermission | 67 Extension::kContentSettingsPermission |
| 68 }, | 68 }, |
| 69 { "enableHyperlinkAuditing", | 69 { "hyperlinkAuditingEnabled", |
| 70 prefs::kEnableHyperlinkAuditing, | 70 prefs::kEnableHyperlinkAuditing, |
| 71 Extension::kContentSettingsPermission | 71 Extension::kContentSettingsPermission |
| 72 }, | 72 }, |
| 73 { "proxy", | 73 { "proxy", |
| 74 prefs::kProxy, | 74 prefs::kProxy, |
| 75 Extension::kProxyPermission | 75 Extension::kProxyPermission |
| 76 }, | 76 }, |
| 77 }; | 77 }; |
| 78 | 78 |
| 79 class IdentityPrefTransformer : public PrefTransformerInterface { | 79 class IdentityPrefTransformer : public PrefTransformerInterface { |
| 80 public: | 80 public: |
| 81 IdentityPrefTransformer() { } | |
| 82 virtual ~IdentityPrefTransformer() { } | |
| 83 | |
| 84 virtual Value* ExtensionToBrowserPref(const Value* extension_pref, | 81 virtual Value* ExtensionToBrowserPref(const Value* extension_pref, |
| 85 std::string* error, | 82 std::string* error, |
| 86 bool* bad_message) { | 83 bool* bad_message) { |
| 87 return extension_pref->DeepCopy(); | 84 return extension_pref->DeepCopy(); |
| 88 } | 85 } |
| 89 | 86 |
| 90 virtual Value* BrowserToExtensionPref(const Value* browser_pref) { | 87 virtual Value* BrowserToExtensionPref(const Value* browser_pref) { |
| 91 return browser_pref->DeepCopy(); | 88 return browser_pref->DeepCopy(); |
| 92 } | 89 } |
| 93 }; | 90 }; |
| 94 | 91 |
| 92 class InvertBooleanTransformer : public PrefTransformerInterface { | |
| 93 public: | |
| 94 virtual Value* ExtensionToBrowserPref(const Value* extension_pref, | |
| 95 std::string* error, | |
| 96 bool* bad_message) { | |
| 97 return InvertBooleanValue(extension_pref); | |
| 98 } | |
| 99 | |
| 100 virtual Value* BrowserToExtensionPref(const Value* browser_pref) { | |
| 101 return InvertBooleanValue(browser_pref); | |
| 102 } | |
| 103 | |
| 104 private: | |
| 105 static Value* InvertBooleanValue(const Value* value) { | |
|
jochen (gone - plz use gerrit)
2011/06/01 08:46:43
why static and private member? either should be en
Bernhard Bauer
2011/06/01 16:48:48
Why not? :-) It's static because it doesn't use ob
| |
| 106 bool bool_value = false; | |
| 107 bool result = value->GetAsBoolean(&bool_value); | |
| 108 DCHECK(result); | |
| 109 return Value::CreateBooleanValue(!bool_value); | |
| 110 } | |
| 111 }; | |
| 112 | |
| 95 // Returns a string constant (defined in the API) indicating the level of | 113 // Returns a string constant (defined in the API) indicating the level of |
| 96 // control this extension has over the specified preference. | 114 // control this extension has over the specified preference. |
| 97 const char* GetLevelOfControl( | 115 const char* GetLevelOfControl( |
| 98 Profile* profile, | 116 Profile* profile, |
| 99 const std::string& extension_id, | 117 const std::string& extension_id, |
| 100 const std::string& browser_pref, | 118 const std::string& browser_pref, |
| 101 bool incognito) { | 119 bool incognito) { |
| 102 PrefService* prefs = incognito ? profile->GetOffTheRecordPrefs() | 120 PrefService* prefs = incognito ? profile->GetOffTheRecordPrefs() |
| 103 : profile->GetPrefs(); | 121 : profile->GetPrefs(); |
| 104 const PrefService::Preference* pref = | 122 const PrefService::Preference* pref = |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 kPrefMapping[i].permission); | 201 kPrefMapping[i].permission); |
| 184 std::string event_name = | 202 std::string event_name = |
| 185 base::StringPrintf(kOnPrefChangeFormat, | 203 base::StringPrintf(kOnPrefChangeFormat, |
| 186 kPrefMapping[i].extension_pref); | 204 kPrefMapping[i].extension_pref); |
| 187 event_mapping_[kPrefMapping[i].browser_pref] = | 205 event_mapping_[kPrefMapping[i].browser_pref] = |
| 188 std::make_pair(event_name, kPrefMapping[i].permission); | 206 std::make_pair(event_name, kPrefMapping[i].permission); |
| 189 } | 207 } |
| 190 DCHECK_EQ(arraysize(kPrefMapping), mapping_.size()); | 208 DCHECK_EQ(arraysize(kPrefMapping), mapping_.size()); |
| 191 DCHECK_EQ(arraysize(kPrefMapping), event_mapping_.size()); | 209 DCHECK_EQ(arraysize(kPrefMapping), event_mapping_.size()); |
| 192 RegisterPrefTransformer(prefs::kProxy, new ProxyPrefTransformer()); | 210 RegisterPrefTransformer(prefs::kProxy, new ProxyPrefTransformer()); |
| 211 RegisterPrefTransformer(prefs::kBlockThirdPartyCookies, | |
| 212 new InvertBooleanTransformer()); | |
| 193 } | 213 } |
| 194 | 214 |
| 195 ~PrefMapping() { | 215 ~PrefMapping() { |
| 196 STLDeleteContainerPairSecondPointers(transformers_.begin(), | 216 STLDeleteContainerPairSecondPointers(transformers_.begin(), |
| 197 transformers_.end()); | 217 transformers_.end()); |
| 198 } | 218 } |
| 199 | 219 |
| 200 void RegisterPrefTransformer(const std::string& browser_pref, | 220 void RegisterPrefTransformer(const std::string& browser_pref, |
| 201 PrefTransformerInterface* transformer) { | 221 PrefTransformerInterface* transformer) { |
| 202 DCHECK_EQ(0u, transformers_.count(browser_pref)) << | 222 DCHECK_EQ(0u, transformers_.count(browser_pref)) << |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 473 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( | 493 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( |
| 474 pref_key, &browser_pref, &permission)); | 494 pref_key, &browser_pref, &permission)); |
| 475 if (!GetExtension()->HasApiPermission(permission)) { | 495 if (!GetExtension()->HasApiPermission(permission)) { |
| 476 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); | 496 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); |
| 477 return false; | 497 return false; |
| 478 } | 498 } |
| 479 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | 499 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
| 480 prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, scope); | 500 prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, scope); |
| 481 return true; | 501 return true; |
| 482 } | 502 } |
| OLD | NEW |