Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(387)

Side by Side Diff: chrome/browser/extensions/api/preference/chrome_direct_setting.cc

Issue 18341016: Add types.private.ChromeDirectSetting and Connect it to preferencesPrivate.googleGeolocationAccessE… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added Preference Whitelist Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 bool DirectSettingFunctionBase::IsPreferenceOnWhitelist(
20 const std::string &pref_key) {
Bernhard Bauer 2013/07/09 22:27:22 Nit: the ampersand comes after the type, not befor
robliao 2013/07/09 23:07:23 Done.
21 return preference_whitelist_.find(pref_key) != preference_whitelist_.end();
22 }
23
24 PreferenceWhitelist DirectSettingFunctionBase::CreatePreferenceWhitelist() {
25 base::hash_set<std::string> whitelist;
26 whitelist.insert("googlegeolocationaccess.enabled");
27 return whitelist;
28 }
29
30 const PreferenceWhitelist DirectSettingFunctionBase::preference_whitelist_ =
Bernhard Bauer 2013/07/09 22:27:22 This creates a static initializer, which isn't all
robliao 2013/07/09 23:07:23 I was afraid of that. Changed to use LazyInstance.
31 DirectSettingFunctionBase::CreatePreferenceWhitelist();
32
33 PrefService* DirectSettingFunctionBase::GetPrefService() {
34 return profile()->GetPrefs();
35 }
36
37 bool GetDirectSettingFunction::RunImpl() {
38 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension());
39
40 std::string pref_key;
41 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key));
42 EXTENSION_FUNCTION_VALIDATE(IsPreferenceOnWhitelist(pref_key));
43
44 const PrefService::Preference* preference =
45 GetPrefService()->FindPreference(pref_key.c_str());
46 EXTENSION_FUNCTION_VALIDATE(preference);
47 const base::Value* value = preference->GetValue();
48
49 scoped_ptr<DictionaryValue> result(new DictionaryValue);
50 result->Set(preference_api_constants::kValue, value->DeepCopy());
51 SetResult(result.release());
52
53 return true;
54 }
55
56 bool SetDirectSettingFunction::RunImpl() {
57 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension());
58
59 std::string pref_key;
60 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key));
61 EXTENSION_FUNCTION_VALIDATE(IsPreferenceOnWhitelist(pref_key));
62
63 DictionaryValue* details = NULL;
64 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details));
65
66 Value* value = NULL;
67 EXTENSION_FUNCTION_VALIDATE(
68 details->Get(preference_api_constants::kValue, &value));
69
70 PrefService* pref_service = GetPrefService();
71 const PrefService::Preference* preference =
72 pref_service->FindPreference(pref_key.c_str());
73 EXTENSION_FUNCTION_VALIDATE(preference);
74
75 EXTENSION_FUNCTION_VALIDATE(value->GetType() == preference->GetType());
76
77 pref_service->Set(pref_key.c_str(), *value);
78
79 return true;
80 }
81
82 bool ClearDirectSettingFunction::RunImpl() {
83 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension());
84
85 std::string pref_key;
86 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key));
87 EXTENSION_FUNCTION_VALIDATE(IsPreferenceOnWhitelist(pref_key));
88 GetPrefService()->ClearPref(pref_key.c_str());
89
90 return true;
91 }
92
93 } // namespace chromedirectsetting
94 } // namespace extensions
95
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698