diff options
author | robliao@chromium.org <robliao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-12 03:09:41 +0000 |
---|---|---|
committer | robliao@chromium.org <robliao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-12 03:09:41 +0000 |
commit | d56228dfe042da4946a7ca52e6e3bdada257963e (patch) | |
tree | ff529ca39f9636deecb98cad022a80cf9e83b9de /chrome/browser/extensions/api/preference | |
parent | 93b29064e134da54291c3e07bdf7fe0dd0335169 (diff) | |
download | chromium_src-d56228dfe042da4946a7ca52e6e3bdada257963e.zip chromium_src-d56228dfe042da4946a7ca52e6e3bdada257963e.tar.gz chromium_src-d56228dfe042da4946a7ca52e6e3bdada257963e.tar.bz2 |
Add types.private.ChromeDirectSetting and Connect it to preferencesPrivate.googleGeolocationAccessEnabled
* Adds a component extension only API type
chrome.types.private.ChromeDirectSetting (mirrors ChromeSetting except that it
does not provide levelOfControl info given that this only applies to component
extensions).
* Switches preferencesPrivate.googleGeolocationAccessEnabled to use chrome.types.private.ChromeDirectSetting
* Implements preliminary get, set, and clear functionality (direct access to the preferences)
* Adds stubs for onChange to be done later.
* Removes existing component extension affordances in the existing extension
preferences codepath
BUG=164227
Review URL: https://chromiumcodereview.appspot.com/18341016
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@211300 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/api/preference')
4 files changed, 216 insertions, 11 deletions
diff --git a/chrome/browser/extensions/api/preference/chrome_direct_setting.cc b/chrome/browser/extensions/api/preference/chrome_direct_setting.cc new file mode 100644 index 0000000..161ef9b --- /dev/null +++ b/chrome/browser/extensions/api/preference/chrome_direct_setting.cc @@ -0,0 +1,128 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/extensions/api/preference/chrome_direct_setting.h" + +#include "base/containers/hash_tables.h" +#include "base/prefs/pref_service.h" +#include "base/values.h" +#include "chrome/browser/extensions/api/preference/preference_api_constants.h" +#include "chrome/browser/profiles/profile.h" + +namespace extensions { +namespace chromedirectsetting { + +namespace { + +class PreferenceWhitelist { + public: + PreferenceWhitelist() { + whitelist_.insert("googlegeolocationaccess.enabled"); + } + + ~PreferenceWhitelist() {} + + bool IsPreferenceOnWhitelist(const std::string& pref_key){ + return whitelist_.find(pref_key) != whitelist_.end(); + } + + private: + base::hash_set<std::string> whitelist_; + + DISALLOW_COPY_AND_ASSIGN(PreferenceWhitelist); +}; + +static base::LazyInstance<PreferenceWhitelist> preference_whitelist_ = + LAZY_INSTANCE_INITIALIZER; + +} // namespace + +DirectSettingFunctionBase::DirectSettingFunctionBase() {} + +DirectSettingFunctionBase::~DirectSettingFunctionBase() {} + +PrefService* DirectSettingFunctionBase::GetPrefService() { + return profile()->GetPrefs(); +} + +bool DirectSettingFunctionBase::IsCalledFromComponentExtension() { + return GetExtension()->location() == Manifest::COMPONENT; +} + +bool DirectSettingFunctionBase::IsPreferenceOnWhitelist( + const std::string& pref_key) { + return preference_whitelist_.Get().IsPreferenceOnWhitelist(pref_key); +} + +GetDirectSettingFunction::GetDirectSettingFunction() {} + +bool GetDirectSettingFunction::RunImpl() { + EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); + + std::string pref_key; + EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); + EXTENSION_FUNCTION_VALIDATE(IsPreferenceOnWhitelist(pref_key)); + + const PrefService::Preference* preference = + GetPrefService()->FindPreference(pref_key.c_str()); + EXTENSION_FUNCTION_VALIDATE(preference); + const base::Value* value = preference->GetValue(); + + scoped_ptr<DictionaryValue> result(new DictionaryValue); + result->Set(preference_api_constants::kValue, value->DeepCopy()); + SetResult(result.release()); + + return true; +} + +GetDirectSettingFunction::~GetDirectSettingFunction() {} + +SetDirectSettingFunction::SetDirectSettingFunction() {} + +bool SetDirectSettingFunction::RunImpl() { + EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); + + std::string pref_key; + EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); + EXTENSION_FUNCTION_VALIDATE(IsPreferenceOnWhitelist(pref_key)); + + DictionaryValue* details = NULL; + EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); + + Value* value = NULL; + EXTENSION_FUNCTION_VALIDATE( + details->Get(preference_api_constants::kValue, &value)); + + PrefService* pref_service = GetPrefService(); + const PrefService::Preference* preference = + pref_service->FindPreference(pref_key.c_str()); + EXTENSION_FUNCTION_VALIDATE(preference); + + EXTENSION_FUNCTION_VALIDATE(value->GetType() == preference->GetType()); + + pref_service->Set(pref_key.c_str(), *value); + + return true; +} + +SetDirectSettingFunction::~SetDirectSettingFunction() {} + +ClearDirectSettingFunction::ClearDirectSettingFunction() {} + +bool ClearDirectSettingFunction::RunImpl() { + EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); + + std::string pref_key; + EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); + EXTENSION_FUNCTION_VALIDATE(IsPreferenceOnWhitelist(pref_key)); + GetPrefService()->ClearPref(pref_key.c_str()); + + return true; +} + +ClearDirectSettingFunction::~ClearDirectSettingFunction() {} + +} // namespace chromedirectsetting +} // namespace extensions + diff --git a/chrome/browser/extensions/api/preference/chrome_direct_setting.h b/chrome/browser/extensions/api/preference/chrome_direct_setting.h new file mode 100644 index 0000000..1a8164b --- /dev/null +++ b/chrome/browser/extensions/api/preference/chrome_direct_setting.h @@ -0,0 +1,87 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_EXTENSIONS_API_PREFERENCE_CHROME_DIRECT_SETTING_H__ +#define CHROME_BROWSER_EXTENSIONS_API_PREFERENCE_CHROME_DIRECT_SETTING_H__ + +#include "base/lazy_instance.h" +#include "chrome/browser/extensions/extension_function.h" + +class PrefService; + +namespace extensions { +namespace chromedirectsetting { + +// Base class to host instance method helpers. +class DirectSettingFunctionBase : public SyncExtensionFunction { + protected: + DirectSettingFunctionBase(); + virtual ~DirectSettingFunctionBase(); + + // Returns the user pref service. + PrefService* GetPrefService(); + + // Returns true if the caller is a component extension. + bool IsCalledFromComponentExtension(); + + // Returns true if the preference is on the whitelist. + bool IsPreferenceOnWhitelist(const std::string& pref_key); + + private: + DISALLOW_COPY_AND_ASSIGN(DirectSettingFunctionBase); +}; + +class GetDirectSettingFunction : public DirectSettingFunctionBase { + public: + DECLARE_EXTENSION_FUNCTION("types.private.ChromeDirectSetting.get", + TYPES_PRIVATE_CHROMEDIRECTSETTING_GET) + + GetDirectSettingFunction(); + + protected: + // ExtensionFunction: + virtual bool RunImpl() OVERRIDE; + + private: + virtual ~GetDirectSettingFunction(); + DISALLOW_COPY_AND_ASSIGN(GetDirectSettingFunction); +}; + +class SetDirectSettingFunction : public DirectSettingFunctionBase { + public: + DECLARE_EXTENSION_FUNCTION("types.private.ChromeDirectSetting.set", + TYPES_PRIVATE_CHROMEDIRECTSETTING_SET) + + SetDirectSettingFunction(); + + protected: + // ExtensionFunction: + virtual bool RunImpl() OVERRIDE; + + private: + virtual ~SetDirectSettingFunction(); + DISALLOW_COPY_AND_ASSIGN(SetDirectSettingFunction); +}; + +class ClearDirectSettingFunction : public DirectSettingFunctionBase { + public: + DECLARE_EXTENSION_FUNCTION("types.private.ChromeDirectSetting.clear", + TYPES_PRIVATE_CHROMEDIRECTSETTING_CLEAR) + + ClearDirectSettingFunction(); + + protected: + // ExtensionFunction: + virtual bool RunImpl() OVERRIDE; + + private: + virtual ~ClearDirectSettingFunction(); + DISALLOW_COPY_AND_ASSIGN(ClearDirectSettingFunction); +}; + +} // namespace chromedirectsetting +} // namespace extensions + +#endif // CHROME_BROWSER_EXTENSIONS_API_PREFERENCE_CHROME_DIRECT_SETTING_H__ + diff --git a/chrome/browser/extensions/api/preference/preference_api.cc b/chrome/browser/extensions/api/preference/preference_api.cc index d6b79ed..b792fd4 100644 --- a/chrome/browser/extensions/api/preference/preference_api.cc +++ b/chrome/browser/extensions/api/preference/preference_api.cc @@ -107,12 +107,6 @@ PrefMappingEntry kPrefMapping[] = { prefs::kEnableTranslate, APIPermission::kPrivacy }, -#if defined(ENABLE_GOOGLE_NOW) - { "googlegeolocationaccessEnabled", - prefs::kGoogleGeolocationAccessEnabled, - APIPermission::kPreferencesPrivate - }, -#endif }; class IdentityPrefTransformer : public PrefTransformerInterface { diff --git a/chrome/browser/extensions/api/preference/preference_helpers.cc b/chrome/browser/extensions/api/preference/preference_helpers.cc index e4192dd..8649f8e 100644 --- a/chrome/browser/extensions/api/preference/preference_helpers.cc +++ b/chrome/browser/extensions/api/preference/preference_helpers.cc @@ -8,7 +8,6 @@ #include "base/prefs/pref_service.h" #include "base/values.h" #include "chrome/browser/extensions/api/preference/preference_api.h" -#include "chrome/browser/extensions/component_loader.h" #include "chrome/browser/extensions/event_router.h" #include "chrome/browser/extensions/extension_prefs.h" #include "chrome/browser/extensions/extension_service.h" @@ -73,10 +72,7 @@ const char* GetLevelOfControl( return kControlledByThisExtension; } - extensions::ComponentLoader* component_loader = - profile->GetExtensionService()->component_loader(); - if (component_loader->Exists(extension_id) || - PreferenceAPI::Get(profile)->CanExtensionControlPref(extension_id, + if (PreferenceAPI::Get(profile)->CanExtensionControlPref(extension_id, browser_pref, incognito)) { return kControllableByThisExtension; |