| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/api/settings_private/settings_private_delega
te.h" | 5 #include "chrome/browser/extensions/api/settings_private/settings_private_delega
te.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | |
| 8 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
| 9 #include "base/values.h" | 8 #include "base/values.h" |
| 10 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 11 #include "chrome/browser/extensions/api/settings_private/prefs_util.h" | 11 #include "chrome/browser/extensions/api/settings_private/prefs_util.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 14 #include "components/url_fixer/url_fixer.h" | |
| 15 #include "extensions/browser/extension_registry.h" | 14 #include "extensions/browser/extension_registry.h" |
| 16 #include "extensions/common/extension.h" | 15 #include "extensions/common/extension.h" |
| 17 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 18 | 17 |
| 19 namespace extensions { | 18 namespace extensions { |
| 20 | 19 |
| 21 namespace settings_private = api::settings_private; | 20 namespace settings_private = api::settings_private; |
| 22 | 21 |
| 23 SettingsPrivateDelegate::SettingsPrivateDelegate(Profile* profile) | 22 SettingsPrivateDelegate::SettingsPrivateDelegate(Profile* profile) |
| 24 : profile_(profile) { | 23 : profile_(profile) { |
| 24 prefs_util_.reset(new PrefsUtil(profile)); |
| 25 } | 25 } |
| 26 | 26 |
| 27 SettingsPrivateDelegate::~SettingsPrivateDelegate() { | 27 SettingsPrivateDelegate::~SettingsPrivateDelegate() { |
| 28 } | 28 } |
| 29 | 29 |
| 30 scoped_ptr<base::Value> SettingsPrivateDelegate::GetPref( | 30 scoped_ptr<base::Value> SettingsPrivateDelegate::GetPref( |
| 31 const std::string& name) { | 31 const std::string& name) { |
| 32 return prefs_util::GetPref(profile_, name)->ToValue(); | 32 return prefs_util_->GetPref(name)->ToValue(); |
| 33 } | 33 } |
| 34 | 34 |
| 35 scoped_ptr<base::Value> SettingsPrivateDelegate::GetAllPrefs() { | 35 scoped_ptr<base::Value> SettingsPrivateDelegate::GetAllPrefs() { |
| 36 scoped_ptr<base::ListValue> prefs(new base::ListValue()); | 36 scoped_ptr<base::ListValue> prefs(new base::ListValue()); |
| 37 | 37 |
| 38 const TypedPrefMap& keys = prefs_util::GetWhitelistedKeys(); | 38 const TypedPrefMap& keys = prefs_util_->GetWhitelistedKeys(); |
| 39 for (const auto& it : keys) { | 39 for (const auto& it : keys) { |
| 40 prefs->Append(GetPref(it.first).release()); | 40 prefs->Append(GetPref(it.first).release()); |
| 41 } | 41 } |
| 42 | 42 |
| 43 return prefs.Pass(); | 43 return prefs.Pass(); |
| 44 } | 44 } |
| 45 | 45 |
| 46 bool SettingsPrivateDelegate::SetPref(const std::string& pref_name, | 46 bool SettingsPrivateDelegate::SetPref(const std::string& pref_name, |
| 47 const base::Value* value) { | 47 const base::Value* value) { |
| 48 PrefService* pref_service = | 48 return prefs_util_->SetPref(pref_name, value); |
| 49 prefs_util::FindServiceForPref(profile_, pref_name); | |
| 50 | |
| 51 if (!prefs_util::IsPrefUserModifiable(profile_, pref_name)) | |
| 52 return false; | |
| 53 | |
| 54 const PrefService::Preference* pref = | |
| 55 pref_service->FindPreference(pref_name.c_str()); | |
| 56 if (!pref) | |
| 57 return false; | |
| 58 | |
| 59 DCHECK_EQ(pref->GetType(), value->GetType()); | |
| 60 | |
| 61 scoped_ptr<base::Value> temp_value; | |
| 62 | |
| 63 switch (pref->GetType()) { | |
| 64 case base::Value::TYPE_INTEGER: { | |
| 65 // In JS all numbers are doubles. | |
| 66 double double_value; | |
| 67 if (!value->GetAsDouble(&double_value)) | |
| 68 return false; | |
| 69 | |
| 70 int int_value = static_cast<int>(double_value); | |
| 71 temp_value.reset(new base::FundamentalValue(int_value)); | |
| 72 value = temp_value.get(); | |
| 73 break; | |
| 74 } | |
| 75 case base::Value::TYPE_STRING: { | |
| 76 std::string original; | |
| 77 if (!value->GetAsString(&original)) | |
| 78 return false; | |
| 79 | |
| 80 if (prefs_util::IsPrefTypeURL(pref_name)) { | |
| 81 GURL fixed = url_fixer::FixupURL(original, std::string()); | |
| 82 temp_value.reset(new base::StringValue(fixed.spec())); | |
| 83 value = temp_value.get(); | |
| 84 } | |
| 85 break; | |
| 86 } | |
| 87 case base::Value::TYPE_LIST: { | |
| 88 // In case we have a List pref we got a JSON string. | |
| 89 std::string json_string; | |
| 90 if (!value->GetAsString(&json_string)) | |
| 91 return false; | |
| 92 | |
| 93 temp_value.reset(base::JSONReader::DeprecatedRead(json_string)); | |
| 94 value = temp_value.get(); | |
| 95 if (!value->IsType(base::Value::TYPE_LIST)) | |
| 96 return false; | |
| 97 | |
| 98 break; | |
| 99 } | |
| 100 case base::Value::TYPE_BOOLEAN: | |
| 101 case base::Value::TYPE_DOUBLE: | |
| 102 break; | |
| 103 default: | |
| 104 return false; | |
| 105 } | |
| 106 | |
| 107 // TODO(orenb): Process setting metrics here (like "ProcessUserMetric" in | |
| 108 // CoreOptionsHandler). | |
| 109 pref_service->Set(pref_name.c_str(), *value); | |
| 110 return true; | |
| 111 } | 49 } |
| 112 | 50 |
| 113 } // namespace extensions | 51 } // namespace extensions |
| OLD | NEW |