diff options
author | bauerb@chromium.org <bauerb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-15 12:03:27 +0000 |
---|---|---|
committer | bauerb@chromium.org <bauerb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-15 12:03:27 +0000 |
commit | 3e3342690f7c69fa6e5802782c33a12eec000129 (patch) | |
tree | 3d48886843d6ed087cb85acc4a166869e5608867 /chrome | |
parent | e47c53d4c0a7dd696b0193fb79c2925ba6416818 (diff) | |
download | chromium_src-3e3342690f7c69fa6e5802782c33a12eec000129.zip chromium_src-3e3342690f7c69fa6e5802782c33a12eec000129.tar.gz chromium_src-3e3342690f7c69fa6e5802782c33a12eec000129.tar.bz2 |
Use PreferenceTransformerInterface in extension preference APIs.
This allows us to get rid of custom preference API functions for the proxy extension API.
BUG=73994
TEST=ProxyApiTest.*:ExtensionApiTest.*
Review URL: http://codereview.chromium.org/6683002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@78197 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
7 files changed, 104 insertions, 122 deletions
diff --git a/chrome/browser/extensions/extension_function_dispatcher.cc b/chrome/browser/extensions/extension_function_dispatcher.cc index 4fb7af3..1d58b498 100644 --- a/chrome/browser/extensions/extension_function_dispatcher.cc +++ b/chrome/browser/extensions/extension_function_dispatcher.cc @@ -258,10 +258,6 @@ void FactoryRegistry::ResetFunctions() { RegisterFunction<OmniboxSendSuggestionsFunction>(); RegisterFunction<OmniboxSetDefaultSuggestionFunction>(); - // Proxies. - RegisterFunction<SetProxySettingsFunction>(); - RegisterFunction<GetProxySettingsFunction>(); - // Sidebar. RegisterFunction<CollapseSidebarFunction>(); RegisterFunction<ExpandSidebarFunction>(); diff --git a/chrome/browser/extensions/extension_preference_api.cc b/chrome/browser/extensions/extension_preference_api.cc index 088c8cf..73e29ce 100644 --- a/chrome/browser/extensions/extension_preference_api.cc +++ b/chrome/browser/extensions/extension_preference_api.cc @@ -4,10 +4,15 @@ #include "chrome/browser/extensions/extension_preference_api.h" +#include <map> + +#include "base/json/json_writer.h" #include "base/singleton.h" +#include "base/stl_util-inl.h" #include "base/stringprintf.h" #include "base/values.h" #include "chrome/browser/extensions/extension_prefs.h" +#include "chrome/browser/extensions/extension_proxy_api.h" #include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/profiles/profile.h" #include "chrome/common/pref_names.h" @@ -33,7 +38,11 @@ const char kValue[] = "value"; const char kIncognitoErrorMessage[] = "You do not have permission to access incognito preferences."; -PrefMappingEntry pref_mapping[] = { +const char kPermissionErrorMessage[] = + "You do not have permission to access the preference '%s'. " + "Be sure to declare in your manifest what permissions you need."; + +PrefMappingEntry kPrefMapping[] = { { "blockThirdPartyCookies", prefs::kBlockThirdPartyCookies, Extension::kContentSettingsPermission @@ -44,6 +53,21 @@ PrefMappingEntry pref_mapping[] = { }, }; +class IdentityPrefTransformer : public PrefTransformerInterface { + public: + IdentityPrefTransformer() { } + virtual ~IdentityPrefTransformer() { } + + virtual Value* ExtensionToBrowserPref(const Value* extension_pref, + std::string* error) { + return extension_pref->DeepCopy(); + } + + virtual Value* BrowserToExtensionPref(const Value* browser_pref) { + return browser_pref->DeepCopy(); + } +}; + class PrefMapping { public: static PrefMapping* GetInstance() { @@ -63,23 +87,51 @@ class PrefMapping { return false; } + PrefTransformerInterface* FindTransformerForBrowserPref( + const std::string& browser_pref) { + std::map<std::string, PrefTransformerInterface*>::iterator it = + transformers_.find(browser_pref); + if (it != transformers_.end()) + return it->second; + else + return identity_transformer_.get(); + } + private: friend struct DefaultSingletonTraits<PrefMapping>; - std::map<std::string, std::pair<std::string, std::string> > mapping_; - PrefMapping() { - for (size_t i = 0; i < arraysize(pref_mapping); ++i) { - mapping_[pref_mapping[i].extension_pref] = - std::make_pair(pref_mapping[i].browser_pref, - pref_mapping[i].permission); + identity_transformer_.reset(new IdentityPrefTransformer()); + for (size_t i = 0; i < arraysize(kPrefMapping); ++i) { + mapping_[kPrefMapping[i].extension_pref] = + std::make_pair(kPrefMapping[i].browser_pref, + kPrefMapping[i].permission); } + DCHECK_EQ(arraysize(kPrefMapping), mapping_.size()); + RegisterPrefTransformer(prefs::kProxy, new ProxyPrefTransformer()); } -}; -const char kPermissionErrorMessage[] = - "You do not have permission to access the preference '%s'. " - "Be sure to declare in your manifest what permissions you need."; + ~PrefMapping() { + STLDeleteContainerPairSecondPointers(transformers_.begin(), + transformers_.end()); + } + + void RegisterPrefTransformer(const std::string& browser_pref, + PrefTransformerInterface* transformer) { + DCHECK_EQ(0u, transformers_.count(browser_pref)) << + "Trying to register pref transformer for " << browser_pref << " twice"; + transformers_[browser_pref] = transformer; + } + + // Mapping from extension pref keys to browser pref keys. + std::map<std::string, std::pair<std::string, std::string> > mapping_; + + + // Mapping from browser pref keys to transformers. + std::map<std::string, PrefTransformerInterface*> transformers_; + + scoped_ptr<PrefTransformerInterface> identity_transformer_; +}; } // namespace @@ -142,7 +194,9 @@ bool GetPreferenceFunction::RunImpl() { std::string level_of_control = GetLevelOfControl(browser_pref, incognito); scoped_ptr<DictionaryValue> result(new DictionaryValue); - result->Set(kValue, pref->GetValue()->DeepCopy()); + PrefTransformerInterface* transformer = + PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); + result->Set(kValue, transformer->BrowserToExtensionPref(pref->GetValue())); result->Set(kLevelOfControl, Value::CreateStringValue(level_of_control)); if (incognito) { ExtensionPrefs* ep = profile_->GetExtensionService()->extension_prefs(); @@ -188,10 +242,18 @@ bool SetPreferenceFunction::RunImpl() { prefs->pref_service()->FindPreference(browser_pref.c_str()); CHECK(pref); EXTENSION_FUNCTION_VALIDATE(value->GetType() == pref->GetType()); + PrefTransformerInterface* transformer = + PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); + std::string error; + Value* browserPrefValue = transformer->ExtensionToBrowserPref(value, &error); + if (!browserPrefValue) { + error_ = error; + return false; + } prefs->SetExtensionControlledPref(extension_id(), browser_pref, incognito, - value->DeepCopy()); + browserPrefValue); return true; } diff --git a/chrome/browser/extensions/extension_preference_api.h b/chrome/browser/extensions/extension_preference_api.h index 2ec2dba4..d020c11 100644 --- a/chrome/browser/extensions/extension_preference_api.h +++ b/chrome/browser/extensions/extension_preference_api.h @@ -12,8 +12,10 @@ class Value; -class PreferenceTransformerInterface { +class PrefTransformerInterface { public: + virtual ~PrefTransformerInterface() {} + // Converts the representation of a preference as seen by the extension // into a representation that is used in the pref stores of the browser. // Returns the pref store representation in case of success or sets diff --git a/chrome/browser/extensions/extension_proxy_api.cc b/chrome/browser/extensions/extension_proxy_api.cc index 60e1316..13a5049 100644 --- a/chrome/browser/extensions/extension_proxy_api.cc +++ b/chrome/browser/extensions/extension_proxy_api.cc @@ -144,13 +144,13 @@ void ExtensionProxyEventRouter::OnProxyError( } } -ProxyPreferenceTransformer::ProxyPreferenceTransformer() { +ProxyPrefTransformer::ProxyPrefTransformer() { } -ProxyPreferenceTransformer::~ProxyPreferenceTransformer() { +ProxyPrefTransformer::~ProxyPrefTransformer() { } -Value* ProxyPreferenceTransformer::ExtensionToBrowserPref( +Value* ProxyPrefTransformer::ExtensionToBrowserPref( const Value* extension_pref, std::string* error) { CHECK(extension_pref->IsType(Value::TYPE_DICTIONARY)); @@ -261,7 +261,7 @@ Value* ProxyPreferenceTransformer::ExtensionToBrowserPref( return result_proxy_config; } -Value* ProxyPreferenceTransformer::BrowserToExtensionPref( +Value* ProxyPrefTransformer::BrowserToExtensionPref( const Value* browser_pref) { CHECK(browser_pref->IsType(Value::TYPE_DICTIONARY)); ProxyConfigDictionary dict(static_cast<const DictionaryValue*>(browser_pref)); @@ -339,7 +339,7 @@ Value* ProxyPreferenceTransformer::BrowserToExtensionPref( return extension_pref.release(); } -bool ProxyPreferenceTransformer::GetProxyServer( +bool ProxyPrefTransformer::GetProxyServer( const DictionaryValue* dict, net::ProxyServer::Scheme default_scheme, net::ProxyServer* proxy_server, @@ -378,7 +378,7 @@ bool ProxyPreferenceTransformer::GetProxyServer( return true; } -bool ProxyPreferenceTransformer::GetProxyRules(DictionaryValue* proxy_rules, +bool ProxyPrefTransformer::GetProxyRules(DictionaryValue* proxy_rules, std::string* out, std::string* error) { if (!proxy_rules) @@ -440,10 +440,10 @@ bool ProxyPreferenceTransformer::GetProxyRules(DictionaryValue* proxy_rules, return true; } -bool ProxyPreferenceTransformer::JoinUrlList(ListValue* list, - const std::string& joiner, - std::string* out, - std::string* error) { +bool ProxyPrefTransformer::JoinUrlList(ListValue* list, + const std::string& joiner, + std::string* out, + std::string* error) { std::string result; for (size_t i = 0; i < list->GetSize(); ++i) { if (!result.empty()) @@ -465,9 +465,9 @@ bool ProxyPreferenceTransformer::JoinUrlList(ListValue* list, return true; } -bool ProxyPreferenceTransformer::GetBypassList(DictionaryValue* proxy_rules, - std::string* out, - std::string* error) { +bool ProxyPrefTransformer::GetBypassList(DictionaryValue* proxy_rules, + std::string* out, + std::string* error) { if (!proxy_rules) return false; @@ -484,8 +484,8 @@ bool ProxyPreferenceTransformer::GetBypassList(DictionaryValue* proxy_rules, return JoinUrlList(bypass_list, ",", out, error); } -bool ProxyPreferenceTransformer::ParseRules(const std::string& rules, - DictionaryValue* out) const { +bool ProxyPrefTransformer::ParseRules(const std::string& rules, + DictionaryValue* out) const { net::ProxyConfig::ProxyRules config; config.ParseFromString(rules); switch (config.type) { @@ -520,7 +520,7 @@ bool ProxyPreferenceTransformer::ParseRules(const std::string& rules, return true; } -DictionaryValue* ProxyPreferenceTransformer::ConvertToDictionary( +DictionaryValue* ProxyPrefTransformer::ConvertToDictionary( const net::ProxyServer& proxy) const { DictionaryValue* out = new DictionaryValue; switch (proxy.scheme()) { @@ -545,50 +545,3 @@ DictionaryValue* ProxyPreferenceTransformer::ConvertToDictionary( out->SetInteger(kProxyCfgRulePort, proxy.host_port_pair().port()); return out; } - -SetProxySettingsFunction::~SetProxySettingsFunction() { -} - -bool SetProxySettingsFunction::RunImpl() { - DictionaryValue* details = NULL; - EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); - - DictionaryValue* proxy_config = NULL; - EXTENSION_FUNCTION_VALIDATE(details->GetDictionary("value", &proxy_config)); - - Value* result_proxy_config = - transformer.ExtensionToBrowserPref(proxy_config, &error_); - - if (!result_proxy_config) - return false; - - details->Set("value", result_proxy_config); - return SetPreferenceFunction::RunImpl(); -} - -GetProxySettingsFunction::~GetProxySettingsFunction() { -} - -bool GetProxySettingsFunction::RunImpl() { - if (!GetPreferenceFunction::RunImpl()) - return false; - - DCHECK(result_->IsType(Value::TYPE_DICTIONARY)); - - DictionaryValue* result_dict_ = static_cast<DictionaryValue*>(result_.get()); - - // This is how it is stored in the PrefStores: - DictionaryValue* proxy_prefs = NULL; - if (!result_dict_->GetDictionary(kProxyCfgValue, &proxy_prefs)) { - LOG(ERROR) << "Received invalid configuration."; - return false; - } - - Value* result_proxy_config = transformer.BrowserToExtensionPref(proxy_prefs); - - if (!result_proxy_config) - return false; - - result_dict_->Set(kProxyCfgValue, result_proxy_config); - return true; -} diff --git a/chrome/browser/extensions/extension_proxy_api.h b/chrome/browser/extensions/extension_proxy_api.h index 22685142..1aa8a63 100644 --- a/chrome/browser/extensions/extension_proxy_api.h +++ b/chrome/browser/extensions/extension_proxy_api.h @@ -8,7 +8,6 @@ #include <string> #include "base/singleton.h" -#include "chrome/browser/extensions/extension_function.h" #include "chrome/browser/extensions/extension_preference_api.h" #include "chrome/browser/profiles/profile.h" #include "net/proxy/proxy_config.h" @@ -16,12 +15,12 @@ class DictionaryValue; class ExtensionEventRouterForwarder; -class ProxyPreferenceTransformer : public PreferenceTransformerInterface { +class ProxyPrefTransformer : public PrefTransformerInterface { public: - ProxyPreferenceTransformer(); - virtual ~ProxyPreferenceTransformer(); + ProxyPrefTransformer(); + virtual ~ProxyPrefTransformer(); - // Implementation of PreferenceTransformerInterface. + // Implementation of PrefTransformerInterface. virtual Value* ExtensionToBrowserPref(const Value* extension_pref, std::string* error); virtual Value* BrowserToExtensionPref(const Value* browser_pref); @@ -69,7 +68,7 @@ class ProxyPreferenceTransformer : public PreferenceTransformerInterface { bool ParseRules(const std::string& rules, DictionaryValue* out) const; DictionaryValue* ConvertToDictionary(const net::ProxyServer& proxy) const; - DISALLOW_COPY_AND_ASSIGN(ProxyPreferenceTransformer); + DISALLOW_COPY_AND_ASSIGN(ProxyPrefTransformer); }; // This class observes proxy error events and routes them to the appropriate @@ -92,26 +91,4 @@ class ExtensionProxyEventRouter { DISALLOW_COPY_AND_ASSIGN(ExtensionProxyEventRouter); }; -class SetProxySettingsFunction : public SetPreferenceFunction { - public: - virtual ~SetProxySettingsFunction(); - virtual bool RunImpl(); - - DECLARE_EXTENSION_FUNCTION_NAME("experimental.proxy.set") - - private: - ProxyPreferenceTransformer transformer; -}; - -class GetProxySettingsFunction : public GetPreferenceFunction { - public: - virtual ~GetProxySettingsFunction(); - virtual bool RunImpl(); - - DECLARE_EXTENSION_FUNCTION_NAME("experimental.proxy.get") - - private: - ProxyPreferenceTransformer transformer; -}; - #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROXY_API_H_ diff --git a/chrome/common/extensions/api/extension_api.json b/chrome/common/extensions/api/extension_api.json index b3683eb..88c566d 100644 --- a/chrome/common/extensions/api/extension_api.json +++ b/chrome/common/extensions/api/extension_api.json @@ -3779,11 +3779,7 @@ "description": "Proxy settings to be used. The value of this preference is a ProxyConfig object.", "value": [ "proxy", - {"$ref": "ProxyConfig"}, - { - "get": "experimental.proxy.get", - "set": "experimental.proxy.set" - } + {"$ref": "ProxyConfig"} ] } }, diff --git a/chrome/renderer/resources/extension_process_bindings.js b/chrome/renderer/resources/extension_process_bindings.js index 7b4ce83..6565f6f 100644 --- a/chrome/renderer/resources/extension_process_bindings.js +++ b/chrome/renderer/resources/extension_process_bindings.js @@ -340,14 +340,11 @@ var chrome = chrome || {}; var customBindings = {}; function setupPreferences() { - customBindings['Preference'] = - function(prefKey, valueSchema, customHandlers) { - if (customHandlers === undefined) - customHandlers = {}; + customBindings['Preference'] = function(prefKey, valueSchema) { this.get = function(details, callback) { var getSchema = this.parameters.get; chromeHidden.validate([details, callback], getSchema); - return sendRequest(customHandlers.get || 'experimental.preferences.get', + return sendRequest('experimental.preferences.get', [prefKey, details, callback], extendSchema(getSchema)); }; @@ -355,15 +352,14 @@ var chrome = chrome || {}; var setSchema = this.parameters.set.slice(); setSchema[0].properties.value = valueSchema; chromeHidden.validate([details, callback], setSchema); - return sendRequest(customHandlers.set || 'experimental.preferences.set', + return sendRequest('experimental.preferences.set', [prefKey, details, callback], extendSchema(setSchema)); }; this.clear = function(details, callback) { var clearSchema = this.parameters.clear; chromeHidden.validate([details, callback], clearSchema); - return sendRequest(customHandlers.clear || - 'experimental.preferences.clear', + return sendRequest('experimental.preferences.clear', [prefKey, details, callback], extendSchema(clearSchema)); }; |