| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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/preference/chrome_direct_setting.h" | 5 #include "chrome/browser/extensions/api/preference/chrome_direct_setting.h" |
| 6 | 6 |
| 7 #include "base/containers/hash_tables.h" | 7 #include "base/containers/hash_tables.h" |
| 8 #include "base/lazy_instance.h" |
| 8 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 9 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/extensions/api/preference/chrome_direct_setting_api.h" |
| 10 #include "chrome/browser/extensions/api/preference/preference_api_constants.h" | 12 #include "chrome/browser/extensions/api/preference/preference_api_constants.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 12 | 14 |
| 13 namespace extensions { | 15 namespace extensions { |
| 14 namespace chromedirectsetting { | 16 namespace chromedirectsetting { |
| 15 | 17 |
| 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() {} | 18 DirectSettingFunctionBase::DirectSettingFunctionBase() {} |
| 42 | 19 |
| 43 DirectSettingFunctionBase::~DirectSettingFunctionBase() {} | 20 DirectSettingFunctionBase::~DirectSettingFunctionBase() {} |
| 44 | 21 |
| 45 PrefService* DirectSettingFunctionBase::GetPrefService() { | 22 PrefService* DirectSettingFunctionBase::GetPrefService() { |
| 46 return profile()->GetPrefs(); | 23 return profile()->GetPrefs(); |
| 47 } | 24 } |
| 48 | 25 |
| 49 bool DirectSettingFunctionBase::IsCalledFromComponentExtension() { | 26 bool DirectSettingFunctionBase::IsCalledFromComponentExtension() { |
| 50 return GetExtension()->location() == Manifest::COMPONENT; | 27 return GetExtension()->location() == Manifest::COMPONENT; |
| 51 } | 28 } |
| 52 | 29 |
| 53 bool DirectSettingFunctionBase::IsPreferenceOnWhitelist( | |
| 54 const std::string& pref_key) { | |
| 55 return preference_whitelist_.Get().IsPreferenceOnWhitelist(pref_key); | |
| 56 } | |
| 57 | |
| 58 GetDirectSettingFunction::GetDirectSettingFunction() {} | 30 GetDirectSettingFunction::GetDirectSettingFunction() {} |
| 59 | 31 |
| 60 bool GetDirectSettingFunction::RunImpl() { | 32 bool GetDirectSettingFunction::RunImpl() { |
| 61 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); | 33 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); |
| 62 | 34 |
| 63 std::string pref_key; | 35 std::string pref_key; |
| 64 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | 36 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 65 EXTENSION_FUNCTION_VALIDATE(IsPreferenceOnWhitelist(pref_key)); | 37 EXTENSION_FUNCTION_VALIDATE( |
| 38 ChromeDirectSettingAPI::Get(profile())->IsPreferenceOnWhitelist(pref_key)); |
| 66 | 39 |
| 67 const PrefService::Preference* preference = | 40 const PrefService::Preference* preference = |
| 68 GetPrefService()->FindPreference(pref_key.c_str()); | 41 GetPrefService()->FindPreference(pref_key.c_str()); |
| 69 EXTENSION_FUNCTION_VALIDATE(preference); | 42 EXTENSION_FUNCTION_VALIDATE(preference); |
| 70 const base::Value* value = preference->GetValue(); | 43 const base::Value* value = preference->GetValue(); |
| 71 | 44 |
| 72 scoped_ptr<DictionaryValue> result(new DictionaryValue); | 45 scoped_ptr<DictionaryValue> result(new DictionaryValue); |
| 73 result->Set(preference_api_constants::kValue, value->DeepCopy()); | 46 result->Set(preference_api_constants::kValue, value->DeepCopy()); |
| 74 SetResult(result.release()); | 47 SetResult(result.release()); |
| 75 | 48 |
| 76 return true; | 49 return true; |
| 77 } | 50 } |
| 78 | 51 |
| 79 GetDirectSettingFunction::~GetDirectSettingFunction() {} | 52 GetDirectSettingFunction::~GetDirectSettingFunction() {} |
| 80 | 53 |
| 81 SetDirectSettingFunction::SetDirectSettingFunction() {} | 54 SetDirectSettingFunction::SetDirectSettingFunction() {} |
| 82 | 55 |
| 83 bool SetDirectSettingFunction::RunImpl() { | 56 bool SetDirectSettingFunction::RunImpl() { |
| 84 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); | 57 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); |
| 85 | 58 |
| 86 std::string pref_key; | 59 std::string pref_key; |
| 87 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | 60 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 88 EXTENSION_FUNCTION_VALIDATE(IsPreferenceOnWhitelist(pref_key)); | 61 EXTENSION_FUNCTION_VALIDATE( |
| 62 ChromeDirectSettingAPI::Get(profile())->IsPreferenceOnWhitelist(pref_key)); |
| 89 | 63 |
| 90 DictionaryValue* details = NULL; | 64 DictionaryValue* details = NULL; |
| 91 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | 65 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
| 92 | 66 |
| 93 Value* value = NULL; | 67 Value* value = NULL; |
| 94 EXTENSION_FUNCTION_VALIDATE( | 68 EXTENSION_FUNCTION_VALIDATE( |
| 95 details->Get(preference_api_constants::kValue, &value)); | 69 details->Get(preference_api_constants::kValue, &value)); |
| 96 | 70 |
| 97 PrefService* pref_service = GetPrefService(); | 71 PrefService* pref_service = GetPrefService(); |
| 98 const PrefService::Preference* preference = | 72 const PrefService::Preference* preference = |
| 99 pref_service->FindPreference(pref_key.c_str()); | 73 pref_service->FindPreference(pref_key.c_str()); |
| 100 EXTENSION_FUNCTION_VALIDATE(preference); | 74 EXTENSION_FUNCTION_VALIDATE(preference); |
| 101 | 75 |
| 102 EXTENSION_FUNCTION_VALIDATE(value->GetType() == preference->GetType()); | 76 EXTENSION_FUNCTION_VALIDATE(value->GetType() == preference->GetType()); |
| 103 | 77 |
| 104 pref_service->Set(pref_key.c_str(), *value); | 78 pref_service->Set(pref_key.c_str(), *value); |
| 105 | 79 |
| 106 return true; | 80 return true; |
| 107 } | 81 } |
| 108 | 82 |
| 109 SetDirectSettingFunction::~SetDirectSettingFunction() {} | 83 SetDirectSettingFunction::~SetDirectSettingFunction() {} |
| 110 | 84 |
| 111 ClearDirectSettingFunction::ClearDirectSettingFunction() {} | 85 ClearDirectSettingFunction::ClearDirectSettingFunction() {} |
| 112 | 86 |
| 113 bool ClearDirectSettingFunction::RunImpl() { | 87 bool ClearDirectSettingFunction::RunImpl() { |
| 114 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); | 88 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); |
| 115 | 89 |
| 116 std::string pref_key; | 90 std::string pref_key; |
| 117 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | 91 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 118 EXTENSION_FUNCTION_VALIDATE(IsPreferenceOnWhitelist(pref_key)); | 92 EXTENSION_FUNCTION_VALIDATE( |
| 93 ChromeDirectSettingAPI::Get(profile())->IsPreferenceOnWhitelist(pref_key)); |
| 119 GetPrefService()->ClearPref(pref_key.c_str()); | 94 GetPrefService()->ClearPref(pref_key.c_str()); |
| 120 | 95 |
| 121 return true; | 96 return true; |
| 122 } | 97 } |
| 123 | 98 |
| 124 ClearDirectSettingFunction::~ClearDirectSettingFunction() {} | 99 ClearDirectSettingFunction::~ClearDirectSettingFunction() {} |
| 125 | 100 |
| 126 } // namespace chromedirectsetting | 101 } // namespace chromedirectsetting |
| 127 } // namespace extensions | 102 } // namespace extensions |
| 128 | 103 |
| OLD | NEW |