OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/api/preference/chrome_direct_setting.h" |
| 6 |
| 7 #include "base/containers/hash_tables.h" |
| 8 #include "base/prefs/pref_service.h" |
| 9 #include "base/values.h" |
| 10 #include "chrome/browser/extensions/api/preference/preference_api_constants.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 |
| 13 namespace extensions { |
| 14 namespace chromedirectsetting { |
| 15 |
| 16 PrefService* DirectSettingFunctionBase::GetPrefService() { |
| 17 return profile()->GetPrefs(); |
| 18 } |
| 19 |
| 20 bool DirectSettingFunctionBase::IsCalledFromComponentExtension() { |
| 21 return GetExtension()->location() == Manifest::COMPONENT; |
| 22 } |
| 23 |
| 24 bool DirectSettingFunctionBase::IsPreferenceOnWhitelist( |
| 25 const std::string& pref_key) { |
| 26 return preference_whitelist_.Get().IsPreferenceOnWhitelist(pref_key); |
| 27 } |
| 28 |
| 29 base::LazyInstance<DirectSettingFunctionBase::PreferenceWhitelist> |
| 30 DirectSettingFunctionBase::preference_whitelist_ = |
| 31 LAZY_INSTANCE_INITIALIZER; |
| 32 |
| 33 DirectSettingFunctionBase::PreferenceWhitelist::PreferenceWhitelist() { |
| 34 whitelist_.insert("googlegeolocationaccess.enabled"); |
| 35 } |
| 36 |
| 37 bool DirectSettingFunctionBase::PreferenceWhitelist::IsPreferenceOnWhitelist( |
| 38 const std::string& pref_key) { |
| 39 return whitelist_.find(pref_key) != whitelist_.end(); |
| 40 } |
| 41 |
| 42 bool GetDirectSettingFunction::RunImpl() { |
| 43 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); |
| 44 |
| 45 std::string pref_key; |
| 46 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 47 EXTENSION_FUNCTION_VALIDATE(IsPreferenceOnWhitelist(pref_key)); |
| 48 |
| 49 const PrefService::Preference* preference = |
| 50 GetPrefService()->FindPreference(pref_key.c_str()); |
| 51 EXTENSION_FUNCTION_VALIDATE(preference); |
| 52 const base::Value* value = preference->GetValue(); |
| 53 |
| 54 scoped_ptr<DictionaryValue> result(new DictionaryValue); |
| 55 result->Set(preference_api_constants::kValue, value->DeepCopy()); |
| 56 SetResult(result.release()); |
| 57 |
| 58 return true; |
| 59 } |
| 60 |
| 61 bool SetDirectSettingFunction::RunImpl() { |
| 62 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); |
| 63 |
| 64 std::string pref_key; |
| 65 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 66 EXTENSION_FUNCTION_VALIDATE(IsPreferenceOnWhitelist(pref_key)); |
| 67 |
| 68 DictionaryValue* details = NULL; |
| 69 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
| 70 |
| 71 Value* value = NULL; |
| 72 EXTENSION_FUNCTION_VALIDATE( |
| 73 details->Get(preference_api_constants::kValue, &value)); |
| 74 |
| 75 PrefService* pref_service = GetPrefService(); |
| 76 const PrefService::Preference* preference = |
| 77 pref_service->FindPreference(pref_key.c_str()); |
| 78 EXTENSION_FUNCTION_VALIDATE(preference); |
| 79 |
| 80 EXTENSION_FUNCTION_VALIDATE(value->GetType() == preference->GetType()); |
| 81 |
| 82 pref_service->Set(pref_key.c_str(), *value); |
| 83 |
| 84 return true; |
| 85 } |
| 86 |
| 87 bool ClearDirectSettingFunction::RunImpl() { |
| 88 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); |
| 89 |
| 90 std::string pref_key; |
| 91 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 92 EXTENSION_FUNCTION_VALIDATE(IsPreferenceOnWhitelist(pref_key)); |
| 93 GetPrefService()->ClearPref(pref_key.c_str()); |
| 94 |
| 95 return true; |
| 96 } |
| 97 |
| 98 } // namespace chromedirectsetting |
| 99 } // namespace extensions |
| 100 |
OLD | NEW |