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/prefs/pref_service.h" | |
8 #include "base/values.h" | |
9 #include "chrome/browser/extensions/api/preference/preference_api_constants.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 | |
12 namespace extensions { | |
13 namespace chromedirectsetting { | |
14 | |
15 bool DirectSettingFunctionBase::IsCalledFromComponentExtension() { | |
16 return GetExtension()->location() == Manifest::COMPONENT; | |
17 } | |
18 | |
19 PrefService* DirectSettingFunctionBase::GetPrefService() { | |
20 return profile()->GetPrefs(); | |
21 } | |
22 | |
23 bool GetDirectSettingFunction::RunImpl() { | |
24 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); | |
25 | |
26 std::string pref_key; | |
27 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | |
28 | |
29 const PrefService::Preference* preference = | |
30 GetPrefService()->FindPreference(pref_key.c_str()); | |
31 const base::Value* value = preference->GetValue(); | |
32 EXTENSION_FUNCTION_VALIDATE(preference); | |
Bernhard Bauer
2013/07/09 18:45:41
Move this one line up please? Otherwise you'll cra
robliao
2013/07/09 18:53:38
Oops! Fixed.
On 2013/07/09 18:45:41, Bernhard Baue
| |
33 | |
34 scoped_ptr<DictionaryValue> result(new DictionaryValue); | |
35 result->Set(preference_api_constants::kValue, value->DeepCopy()); | |
36 SetResult(result.release()); | |
37 | |
38 return true; | |
39 } | |
40 | |
41 bool SetDirectSettingFunction::RunImpl() { | |
42 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); | |
43 | |
44 std::string pref_key; | |
45 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | |
46 | |
47 DictionaryValue* details = NULL; | |
48 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | |
49 | |
50 Value* value = NULL; | |
51 EXTENSION_FUNCTION_VALIDATE( | |
52 details->Get(preference_api_constants::kValue, &value)); | |
53 | |
54 PrefService* pref_service = GetPrefService(); | |
55 const PrefService::Preference* preference = | |
56 pref_service->FindPreference(pref_key.c_str()); | |
57 EXTENSION_FUNCTION_VALIDATE(preference); | |
58 | |
59 EXTENSION_FUNCTION_VALIDATE(value->GetType() == preference->GetType()); | |
60 | |
61 pref_service->Set(pref_key.c_str(), *value); | |
62 | |
63 return true; | |
64 } | |
65 | |
66 bool ClearDirectSettingFunction::RunImpl() { | |
67 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); | |
68 | |
69 std::string pref_key; | |
70 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | |
71 GetPrefService()->ClearPref(pref_key.c_str()); | |
72 | |
73 return true; | |
74 } | |
75 | |
76 } // namespace chromedirectsetting | |
77 } // namespace extensions | |
78 | |
OLD | NEW |