Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Unified Diff: chrome/browser/extensions/api/settings_private/settings_private_delegate.cc

Issue 1061613002: chrome.settingsPrivate: Implement onPrefsChanged event handling. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Respond to Steven's comments + add an APItest Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/settings_private/settings_private_delegate.cc
diff --git a/chrome/browser/extensions/api/settings_private/settings_private_delegate.cc b/chrome/browser/extensions/api/settings_private/settings_private_delegate.cc
index 57ba0a08eaaa9b613934cfc48e2c196d111b570f..ef3ec9189c20cccb972586ad5515a08baa727bdc 100644
--- a/chrome/browser/extensions/api/settings_private/settings_private_delegate.cc
+++ b/chrome/browser/extensions/api/settings_private/settings_private_delegate.cc
@@ -8,6 +8,8 @@
#include "base/prefs/pref_service.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
+#include "chrome/browser/extensions/api/settings_private/prefs_util.h"
+#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
#include "components/url_fixer/url_fixer.h"
#include "extensions/browser/extension_registry.h"
@@ -18,93 +20,6 @@ namespace extensions {
namespace settings_private = api::settings_private;
-namespace {
-
-// NOTE: Consider moving this function to a separate file, e.g. in
-// chrome/browser/prefs.
-bool IsPrefUserModifiable(Profile* profile,
- PrefService* pref_service,
- const std::string& pref_name) {
- if (pref_name != prefs::kBrowserGuestModeEnabled &&
- pref_name != prefs::kBrowserAddPersonEnabled) {
- return true;
- }
-
- const PrefService::Preference* pref =
- pref_service->FindPreference(pref_name.c_str());
- if (!pref || !pref->IsUserModifiable() || profile->IsSupervised())
- return false;
-
- return true;
-}
-
-const TypedPrefMap& GetWhitelistedKeys() {
- static TypedPrefMap* s_whitelist = nullptr;
- if (s_whitelist)
- return *s_whitelist;
- s_whitelist = new TypedPrefMap();
- (*s_whitelist)["download.default_directory"] =
- settings_private::PrefType::PREF_TYPE_STRING;
- (*s_whitelist)["download.prompt_for_download"] =
- settings_private::PrefType::PREF_TYPE_BOOLEAN;
- (*s_whitelist)["homepage"] =
- settings_private::PrefType::PREF_TYPE_URL;
- return *s_whitelist;
-}
-
-#if defined(OS_CHROMEOS)
-const TypedPrefMap& GetWhitelistedCrosKeys() {
- static TypedPrefMap* s_whitelist = nullptr;
- if (s_whitelist)
- return *s_whitelist;
- s_whitelist = new TypedPrefMap();
- (*s_whitelist)["settings.accessibility"] =
- settings_private::PrefType::PREF_TYPE_BOOLEAN;
- (*s_whitelist)["settings.a11y.autoclick"] =
- settings_private::PrefType::PREF_TYPE_BOOLEAN;
- (*s_whitelist)["settings.a11y.autoclick_delay_ms"] =
- settings_private::PrefType::PREF_TYPE_BOOLEAN;
- (*s_whitelist)["settings.a11y.enable_menu"] =
- settings_private::PrefType::PREF_TYPE_BOOLEAN;
- (*s_whitelist)["settings.a11y.high_contrast_enabled"] =
- settings_private::PrefType::PREF_TYPE_BOOLEAN;
- (*s_whitelist)["settings.a11y.large_cursor_enabled"] =
- settings_private::PrefType::PREF_TYPE_BOOLEAN;
- (*s_whitelist)["settings.a11y.screen_magnifier"] =
- settings_private::PrefType::PREF_TYPE_BOOLEAN;
- (*s_whitelist)["settings.a11y.sticky_keys_enabled"] =
- settings_private::PrefType::PREF_TYPE_BOOLEAN;
- (*s_whitelist)["settings.a11y.virtual_keyboard"] =
- settings_private::PrefType::PREF_TYPE_BOOLEAN;
- (*s_whitelist)["settings.touchpad.enable_tap_dragging"] =
- settings_private::PrefType::PREF_TYPE_BOOLEAN;
- return *s_whitelist;
-}
-#endif
-
-bool IsPrefTypeURL(const std::string& pref_name) {
- settings_private::PrefType pref_type =
- settings_private::PrefType::PREF_TYPE_NONE;
-
- const TypedPrefMap keys = GetWhitelistedKeys();
- const auto& iter = keys.find(pref_name);
- if (iter != keys.end()) {
- pref_type = iter->second;
- }
-
-#if defined(OS_CHROMEOS)
- const TypedPrefMap cros_keys = GetWhitelistedCrosKeys();
- const auto& cros_iter = cros_keys.find(pref_name);
- if (cros_iter != cros_keys.end()) {
- pref_type = cros_iter->second;
- }
-#endif
-
- return pref_type == settings_private::PrefType::PREF_TYPE_URL;
-}
-
-} // namespace
-
SettingsPrivateDelegate::SettingsPrivateDelegate(Profile* profile)
: profile_(profile) {
}
@@ -114,111 +29,26 @@ SettingsPrivateDelegate::~SettingsPrivateDelegate() {
scoped_ptr<base::Value> SettingsPrivateDelegate::GetPref(
const std::string& name) {
- PrefService* pref_service = profile_->GetPrefs();
- const PrefService::Preference* pref = pref_service->FindPreference(name);
- if (!pref)
- return make_scoped_ptr(base::Value::CreateNullValue());
-
- api::settings_private::PrefObject* pref_object =
- new api::settings_private::PrefObject();
- pref_object->key = pref->name();
- switch (pref->GetType()) {
- case base::Value::Type::TYPE_BOOLEAN:
- pref_object->type = api::settings_private::PrefType::PREF_TYPE_BOOLEAN;
- break;
- case base::Value::Type::TYPE_INTEGER:
- case base::Value::Type::TYPE_DOUBLE:
- pref_object->type = api::settings_private::PrefType::PREF_TYPE_NUMBER;
- break;
- case base::Value::Type::TYPE_STRING:
- pref_object->type = IsPrefTypeURL(name)
- ? api::settings_private::PrefType::PREF_TYPE_URL
- : api::settings_private::PrefType::PREF_TYPE_STRING;
- break;
- case base::Value::Type::TYPE_LIST:
- pref_object->type = api::settings_private::PrefType::PREF_TYPE_LIST;
- break;
- default:
- break;
- }
-
- pref_object->value.reset(pref->GetValue()->DeepCopy());
-
- if (pref->IsManaged()) {
- if (pref->IsManagedByCustodian()) {
- pref_object->policy_source =
- api::settings_private::PolicySource::POLICY_SOURCE_DEVICE;
- } else {
- pref_object->policy_source =
- api::settings_private::PolicySource::POLICY_SOURCE_USER;
- }
- pref_object->policy_enforcement =
- pref->IsRecommended() ? api::settings_private::PolicyEnforcement::
- POLICY_ENFORCEMENT_RECOMMENDED
- : api::settings_private::PolicyEnforcement::
- POLICY_ENFORCEMENT_ENFORCED;
- } else if (!IsPrefUserModifiable(profile_, pref_service, name)) {
- pref_object->policy_source =
- api::settings_private::PolicySource::POLICY_SOURCE_USER;
- pref_object->policy_enforcement =
- api::settings_private::PolicyEnforcement::POLICY_ENFORCEMENT_ENFORCED;
- }
-
- return pref_object->ToValue();
+ return prefs_util::GetPref(profile_, name)->ToValue();
}
scoped_ptr<base::Value> SettingsPrivateDelegate::GetAllPrefs() {
scoped_ptr<base::ListValue> prefs(new base::ListValue());
- const TypedPrefMap& keys = GetWhitelistedKeys();
+ const TypedPrefMap& keys = prefs_util::GetWhitelistedKeys();
for (const auto& it : keys) {
prefs->Append(GetPref(it.first).release());
}
-#if defined(OS_CHROMEOS)
- const TypedPrefMap cros_keys = GetWhitelistedCrosKeys();
- for (const auto& it : cros_keys) {
- prefs->Append(GetPref(it.first).release());
- }
-#endif
-
return prefs.Pass();
}
-PrefService* SettingsPrivateDelegate::FindServiceForPref(
- const std::string& pref_name) {
- PrefService* user_prefs = profile_->GetPrefs();
-
- // Proxy is a peculiar case: on ChromeOS, settings exist in both user
- // prefs and local state, but chrome://settings should affect only user prefs.
- // Elsewhere the proxy settings are stored in local state.
- // See http://crbug.com/157147
-
- if (pref_name == prefs::kProxy) {
-#if defined(OS_CHROMEOS)
- return user_prefs;
-#else
- return g_browser_process->local_state();
-#endif
- }
-
- // Find which PrefService contains the given pref. Pref names should not
- // be duplicated across services, however if they are, prefer the user's
- // prefs.
- if (user_prefs->FindPreference(pref_name))
- return user_prefs;
-
- if (g_browser_process->local_state()->FindPreference(pref_name))
- return g_browser_process->local_state();
-
- return user_prefs;
-}
-
bool SettingsPrivateDelegate::SetPref(const std::string& pref_name,
const base::Value* value) {
- PrefService* pref_service = FindServiceForPref(pref_name);
+ PrefService* pref_service =
+ prefs_util::FindServiceForPref(profile_, pref_name);
- if (!IsPrefUserModifiable(profile_, pref_service, pref_name))
+ if (!prefs_util::IsPrefUserModifiable(profile_, pref_name))
return false;
const PrefService::Preference* pref =
@@ -247,7 +77,7 @@ bool SettingsPrivateDelegate::SetPref(const std::string& pref_name,
if (!value->GetAsString(&original))
return false;
- if (IsPrefTypeURL(pref_name)) {
+ if (prefs_util::IsPrefTypeURL(pref_name)) {
GURL fixed = url_fixer::FixupURL(original, std::string());
temp_value.reset(new base::StringValue(fixed.spec()));
value = temp_value.get();

Powered by Google App Engine
This is Rietveld 408576698