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 DirectSettingFunctionBase::DirectSettingFunctionBase() {} |
| 42 |
| 43 DirectSettingFunctionBase::~DirectSettingFunctionBase() {} |
| 44 |
| 45 PrefService* DirectSettingFunctionBase::GetPrefService() { |
| 46 return profile()->GetPrefs(); |
| 47 } |
| 48 |
| 49 bool DirectSettingFunctionBase::IsCalledFromComponentExtension() { |
| 50 return GetExtension()->location() == Manifest::COMPONENT; |
| 51 } |
| 52 |
| 53 bool DirectSettingFunctionBase::IsPreferenceOnWhitelist( |
| 54 const std::string& pref_key) { |
| 55 return preference_whitelist_.Get().IsPreferenceOnWhitelist(pref_key); |
| 56 } |
| 57 |
| 58 GetDirectSettingFunction::GetDirectSettingFunction() {} |
| 59 |
| 60 bool GetDirectSettingFunction::RunImpl() { |
| 61 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); |
| 62 |
| 63 std::string pref_key; |
| 64 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 65 EXTENSION_FUNCTION_VALIDATE(IsPreferenceOnWhitelist(pref_key)); |
| 66 |
| 67 const PrefService::Preference* preference = |
| 68 GetPrefService()->FindPreference(pref_key.c_str()); |
| 69 EXTENSION_FUNCTION_VALIDATE(preference); |
| 70 const base::Value* value = preference->GetValue(); |
| 71 |
| 72 scoped_ptr<DictionaryValue> result(new DictionaryValue); |
| 73 result->Set(preference_api_constants::kValue, value->DeepCopy()); |
| 74 SetResult(result.release()); |
| 75 |
| 76 return true; |
| 77 } |
| 78 |
| 79 GetDirectSettingFunction::~GetDirectSettingFunction() {} |
| 80 |
| 81 SetDirectSettingFunction::SetDirectSettingFunction() {} |
| 82 |
| 83 bool SetDirectSettingFunction::RunImpl() { |
| 84 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); |
| 85 |
| 86 std::string pref_key; |
| 87 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 88 EXTENSION_FUNCTION_VALIDATE(IsPreferenceOnWhitelist(pref_key)); |
| 89 |
| 90 DictionaryValue* details = NULL; |
| 91 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
| 92 |
| 93 Value* value = NULL; |
| 94 EXTENSION_FUNCTION_VALIDATE( |
| 95 details->Get(preference_api_constants::kValue, &value)); |
| 96 |
| 97 PrefService* pref_service = GetPrefService(); |
| 98 const PrefService::Preference* preference = |
| 99 pref_service->FindPreference(pref_key.c_str()); |
| 100 EXTENSION_FUNCTION_VALIDATE(preference); |
| 101 |
| 102 EXTENSION_FUNCTION_VALIDATE(value->GetType() == preference->GetType()); |
| 103 |
| 104 pref_service->Set(pref_key.c_str(), *value); |
| 105 |
| 106 return true; |
| 107 } |
| 108 |
| 109 SetDirectSettingFunction::~SetDirectSettingFunction() {} |
| 110 |
| 111 ClearDirectSettingFunction::ClearDirectSettingFunction() {} |
| 112 |
| 113 bool ClearDirectSettingFunction::RunImpl() { |
| 114 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); |
| 115 |
| 116 std::string pref_key; |
| 117 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 118 EXTENSION_FUNCTION_VALIDATE(IsPreferenceOnWhitelist(pref_key)); |
| 119 GetPrefService()->ClearPref(pref_key.c_str()); |
| 120 |
| 121 return true; |
| 122 } |
| 123 |
| 124 ClearDirectSettingFunction::~ClearDirectSettingFunction() {} |
| 125 |
| 126 } // namespace chromedirectsetting |
| 127 } // namespace extensions |
| 128 |
OLD | NEW |