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 namespace { |
| 17 |
| 18 class PreferenceWhitelist { |
| 19 public: |
| 20 PreferenceWhitelist() { |
| 21 whitelist_.insert("googlegeolocationaccess.enabled"); |
| 22 } |
| 23 |
| 24 ~PreferenceWhitelist() {} |
| 25 |
| 26 bool IsPreferenceOnWhitelist(const std::string& pref_key){ |
| 27 return whitelist_.find(pref_key) != whitelist_.end(); |
| 28 } |
| 29 |
| 30 private: |
| 31 base::hash_set<std::string> whitelist_; |
| 32 |
| 33 DISALLOW_COPY_AND_ASSIGN(PreferenceWhitelist); |
| 34 }; |
| 35 |
| 36 static base::LazyInstance<PreferenceWhitelist> preference_whitelist_ = |
| 37 LAZY_INSTANCE_INITIALIZER; |
| 38 |
| 39 } // namespace |
| 40 |
| 41 PrefService* DirectSettingFunctionBase::GetPrefService() { |
| 42 return profile()->GetPrefs(); |
| 43 } |
| 44 |
| 45 bool DirectSettingFunctionBase::IsCalledFromComponentExtension() { |
| 46 return GetExtension()->location() == Manifest::COMPONENT; |
| 47 } |
| 48 |
| 49 bool DirectSettingFunctionBase::IsPreferenceOnWhitelist( |
| 50 const std::string& pref_key) { |
| 51 return preference_whitelist_.Get().IsPreferenceOnWhitelist(pref_key); |
| 52 } |
| 53 |
| 54 bool GetDirectSettingFunction::RunImpl() { |
| 55 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); |
| 56 |
| 57 std::string pref_key; |
| 58 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 59 EXTENSION_FUNCTION_VALIDATE(IsPreferenceOnWhitelist(pref_key)); |
| 60 |
| 61 const PrefService::Preference* preference = |
| 62 GetPrefService()->FindPreference(pref_key.c_str()); |
| 63 EXTENSION_FUNCTION_VALIDATE(preference); |
| 64 const base::Value* value = preference->GetValue(); |
| 65 |
| 66 scoped_ptr<DictionaryValue> result(new DictionaryValue); |
| 67 result->Set(preference_api_constants::kValue, value->DeepCopy()); |
| 68 SetResult(result.release()); |
| 69 |
| 70 return true; |
| 71 } |
| 72 |
| 73 bool SetDirectSettingFunction::RunImpl() { |
| 74 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); |
| 75 |
| 76 std::string pref_key; |
| 77 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 78 EXTENSION_FUNCTION_VALIDATE(IsPreferenceOnWhitelist(pref_key)); |
| 79 |
| 80 DictionaryValue* details = NULL; |
| 81 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
| 82 |
| 83 Value* value = NULL; |
| 84 EXTENSION_FUNCTION_VALIDATE( |
| 85 details->Get(preference_api_constants::kValue, &value)); |
| 86 |
| 87 PrefService* pref_service = GetPrefService(); |
| 88 const PrefService::Preference* preference = |
| 89 pref_service->FindPreference(pref_key.c_str()); |
| 90 EXTENSION_FUNCTION_VALIDATE(preference); |
| 91 |
| 92 EXTENSION_FUNCTION_VALIDATE(value->GetType() == preference->GetType()); |
| 93 |
| 94 pref_service->Set(pref_key.c_str(), *value); |
| 95 |
| 96 return true; |
| 97 } |
| 98 |
| 99 bool ClearDirectSettingFunction::RunImpl() { |
| 100 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); |
| 101 |
| 102 std::string pref_key; |
| 103 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 104 EXTENSION_FUNCTION_VALIDATE(IsPreferenceOnWhitelist(pref_key)); |
| 105 GetPrefService()->ClearPref(pref_key.c_str()); |
| 106 |
| 107 return true; |
| 108 } |
| 109 |
| 110 } // namespace chromedirectsetting |
| 111 } // namespace extensions |
| 112 |
OLD | NEW |