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 GetDirectSettingFunction::GetDirectSettingFunction() {}
28 bool GetDirectSettingFunction::RunSync() {
30 EXTENSION_FUNCTION_VALIDATE(args_
->GetString(0, &pref_key
));
31 EXTENSION_FUNCTION_VALIDATE(ChromeDirectSettingAPI::Get(GetProfile())
32 ->IsPreferenceOnWhitelist(pref_key
));
34 const PrefService::Preference
* preference
=
35 GetPrefService()->FindPreference(pref_key
.c_str());
36 EXTENSION_FUNCTION_VALIDATE(preference
);
37 const base::Value
* value
= preference
->GetValue();
39 scoped_ptr
<base::DictionaryValue
> result(new base::DictionaryValue
);
40 result
->Set(preference_api_constants::kValue
, value
->DeepCopy());
41 SetResult(result
.release());
46 GetDirectSettingFunction::~GetDirectSettingFunction() {}
48 SetDirectSettingFunction::SetDirectSettingFunction() {}
50 bool SetDirectSettingFunction::RunSync() {
52 EXTENSION_FUNCTION_VALIDATE(args_
->GetString(0, &pref_key
));
53 EXTENSION_FUNCTION_VALIDATE(ChromeDirectSettingAPI::Get(GetProfile())
54 ->IsPreferenceOnWhitelist(pref_key
));
56 base::DictionaryValue
* details
= NULL
;
57 EXTENSION_FUNCTION_VALIDATE(args_
->GetDictionary(1, &details
));
59 base::Value
* value
= NULL
;
60 EXTENSION_FUNCTION_VALIDATE(
61 details
->Get(preference_api_constants::kValue
, &value
));
63 PrefService
* pref_service
= GetPrefService();
64 const PrefService::Preference
* preference
=
65 pref_service
->FindPreference(pref_key
.c_str());
66 EXTENSION_FUNCTION_VALIDATE(preference
);
68 EXTENSION_FUNCTION_VALIDATE(value
->GetType() == preference
->GetType());
70 pref_service
->Set(pref_key
.c_str(), *value
);
75 SetDirectSettingFunction::~SetDirectSettingFunction() {}
77 ClearDirectSettingFunction::ClearDirectSettingFunction() {}
79 bool ClearDirectSettingFunction::RunSync() {
81 EXTENSION_FUNCTION_VALIDATE(args_
->GetString(0, &pref_key
));
82 EXTENSION_FUNCTION_VALIDATE(ChromeDirectSettingAPI::Get(GetProfile())
83 ->IsPreferenceOnWhitelist(pref_key
));
84 GetPrefService()->ClearPref(pref_key
.c_str());
89 ClearDirectSettingFunction::~ClearDirectSettingFunction() {}
91 } // namespace chromedirectsetting
92 } // namespace extensions