1 // Copyright 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.
5 #include "chrome/browser/extensions/api/preference/chrome_direct_setting.h"
7 #include "base/containers/hash_tables.h"
8 #include "base/lazy_instance.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/values.h"
11 #include "chrome/browser/extensions/api/preference/chrome_direct_setting_api.h"
12 #include "chrome/browser/extensions/api/preference/preference_api_constants.h"
13 #include "chrome/browser/profiles/profile.h"
15 namespace extensions
{
16 namespace chromedirectsetting
{
18 DirectSettingFunctionBase::DirectSettingFunctionBase() {}
20 DirectSettingFunctionBase::~DirectSettingFunctionBase() {}
22 PrefService
* DirectSettingFunctionBase::GetPrefService() {
23 return GetProfile()->GetPrefs();
26 bool DirectSettingFunctionBase::IsCalledFromComponentExtension() {
27 return GetExtension()->location() == Manifest::COMPONENT
;
30 GetDirectSettingFunction::GetDirectSettingFunction() {}
32 bool GetDirectSettingFunction::RunImpl() {
33 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension());
36 EXTENSION_FUNCTION_VALIDATE(args_
->GetString(0, &pref_key
));
37 EXTENSION_FUNCTION_VALIDATE(ChromeDirectSettingAPI::Get(GetProfile())
38 ->IsPreferenceOnWhitelist(pref_key
));
40 const PrefService::Preference
* preference
=
41 GetPrefService()->FindPreference(pref_key
.c_str());
42 EXTENSION_FUNCTION_VALIDATE(preference
);
43 const base::Value
* value
= preference
->GetValue();
45 scoped_ptr
<base::DictionaryValue
> result(new base::DictionaryValue
);
46 result
->Set(preference_api_constants::kValue
, value
->DeepCopy());
47 SetResult(result
.release());
52 GetDirectSettingFunction::~GetDirectSettingFunction() {}
54 SetDirectSettingFunction::SetDirectSettingFunction() {}
56 bool SetDirectSettingFunction::RunImpl() {
57 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension());
60 EXTENSION_FUNCTION_VALIDATE(args_
->GetString(0, &pref_key
));
61 EXTENSION_FUNCTION_VALIDATE(ChromeDirectSettingAPI::Get(GetProfile())
62 ->IsPreferenceOnWhitelist(pref_key
));
64 base::DictionaryValue
* details
= NULL
;
65 EXTENSION_FUNCTION_VALIDATE(args_
->GetDictionary(1, &details
));
67 base::Value
* value
= NULL
;
68 EXTENSION_FUNCTION_VALIDATE(
69 details
->Get(preference_api_constants::kValue
, &value
));
71 PrefService
* pref_service
= GetPrefService();
72 const PrefService::Preference
* preference
=
73 pref_service
->FindPreference(pref_key
.c_str());
74 EXTENSION_FUNCTION_VALIDATE(preference
);
76 EXTENSION_FUNCTION_VALIDATE(value
->GetType() == preference
->GetType());
78 pref_service
->Set(pref_key
.c_str(), *value
);
83 SetDirectSettingFunction::~SetDirectSettingFunction() {}
85 ClearDirectSettingFunction::ClearDirectSettingFunction() {}
87 bool ClearDirectSettingFunction::RunImpl() {
88 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension());
91 EXTENSION_FUNCTION_VALIDATE(args_
->GetString(0, &pref_key
));
92 EXTENSION_FUNCTION_VALIDATE(ChromeDirectSettingAPI::Get(GetProfile())
93 ->IsPreferenceOnWhitelist(pref_key
));
94 GetPrefService()->ClearPref(pref_key
.c_str());
99 ClearDirectSettingFunction::~ClearDirectSettingFunction() {}
101 } // namespace chromedirectsetting
102 } // namespace extensions