Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / settings / cros_settings.cc
blob016b33a5ce0be0e84813a2d53273e96651a3d929
1 // Copyright (c) 2012 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/chromeos/settings/cros_settings.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/logging.h"
10 #include "base/stl_util.h"
11 #include "base/strings/string_util.h"
12 #include "base/values.h"
13 #include "chrome/browser/chromeos/settings/device_settings_provider.h"
14 #include "chrome/browser/chromeos/settings/device_settings_service.h"
15 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h"
16 #include "chrome/browser/chromeos/settings/system_settings_provider.h"
17 #include "chromeos/chromeos_switches.h"
18 #include "chromeos/settings/cros_settings_names.h"
19 #include "google_apis/gaia/gaia_auth_util.h"
21 namespace chromeos {
23 static CrosSettings* g_cros_settings = NULL;
25 // static
26 void CrosSettings::Initialize() {
27 CHECK(!g_cros_settings);
28 g_cros_settings = new CrosSettings(DeviceSettingsService::Get());
31 // static
32 bool CrosSettings::IsInitialized() {
33 return g_cros_settings;
36 // static
37 void CrosSettings::Shutdown() {
38 DCHECK(g_cros_settings);
39 delete g_cros_settings;
40 g_cros_settings = NULL;
43 // static
44 CrosSettings* CrosSettings::Get() {
45 CHECK(g_cros_settings);
46 return g_cros_settings;
49 // static
50 bool CrosSettings::IsWhitelisted(const std::string& username,
51 bool* wildcard_match) {
52 // Skip whitelist check for tests.
53 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
54 chromeos::switches::kOobeSkipPostLogin)) {
55 return true;
58 CrosSettings* cros_settings = CrosSettings::Get();
59 bool allow_new_user = false;
60 cros_settings->GetBoolean(kAccountsPrefAllowNewUser, &allow_new_user);
61 if (allow_new_user)
62 return true;
63 return cros_settings->FindEmailInList(kAccountsPrefUsers, username,
64 wildcard_match);
67 CrosSettings::CrosSettings(DeviceSettingsService* device_settings_service) {
68 CrosSettingsProvider::NotifyObserversCallback notify_cb(
69 base::Bind(&CrosSettings::FireObservers,
70 // This is safe since |this| is never deleted.
71 base::Unretained(this)));
72 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
73 switches::kStubCrosSettings)) {
74 AddSettingsProvider(new StubCrosSettingsProvider(notify_cb));
75 } else {
76 AddSettingsProvider(
77 new DeviceSettingsProvider(notify_cb, device_settings_service));
79 // System settings are not mocked currently.
80 AddSettingsProvider(new SystemSettingsProvider(notify_cb));
83 CrosSettings::~CrosSettings() {
84 STLDeleteElements(&providers_);
85 STLDeleteValues(&settings_observers_);
88 bool CrosSettings::IsCrosSettings(const std::string& path) {
89 return StartsWithASCII(path, kCrosSettingsPrefix, true);
92 void CrosSettings::Set(const std::string& path, const base::Value& in_value) {
93 DCHECK(CalledOnValidThread());
94 CrosSettingsProvider* provider;
95 provider = GetProvider(path);
96 if (provider)
97 provider->Set(path, in_value);
100 const base::Value* CrosSettings::GetPref(const std::string& path) const {
101 DCHECK(CalledOnValidThread());
102 CrosSettingsProvider* provider = GetProvider(path);
103 if (provider)
104 return provider->Get(path);
105 NOTREACHED() << path << " preference was not found in the signed settings.";
106 return NULL;
109 CrosSettingsProvider::TrustedStatus CrosSettings::PrepareTrustedValues(
110 const base::Closure& callback) const {
111 DCHECK(CalledOnValidThread());
112 for (size_t i = 0; i < providers_.size(); ++i) {
113 CrosSettingsProvider::TrustedStatus status =
114 providers_[i]->PrepareTrustedValues(callback);
115 if (status != CrosSettingsProvider::TRUSTED)
116 return status;
118 return CrosSettingsProvider::TRUSTED;
121 void CrosSettings::SetBoolean(const std::string& path, bool in_value) {
122 DCHECK(CalledOnValidThread());
123 base::FundamentalValue value(in_value);
124 Set(path, value);
127 void CrosSettings::SetInteger(const std::string& path, int in_value) {
128 DCHECK(CalledOnValidThread());
129 base::FundamentalValue value(in_value);
130 Set(path, value);
133 void CrosSettings::SetDouble(const std::string& path, double in_value) {
134 DCHECK(CalledOnValidThread());
135 base::FundamentalValue value(in_value);
136 Set(path, value);
139 void CrosSettings::SetString(const std::string& path,
140 const std::string& in_value) {
141 DCHECK(CalledOnValidThread());
142 base::StringValue value(in_value);
143 Set(path, value);
146 void CrosSettings::AppendToList(const std::string& path,
147 const base::Value* value) {
148 DCHECK(CalledOnValidThread());
149 const base::Value* old_value = GetPref(path);
150 scoped_ptr<base::Value> new_value(
151 old_value ? old_value->DeepCopy() : new base::ListValue());
152 static_cast<base::ListValue*>(new_value.get())->Append(value->DeepCopy());
153 Set(path, *new_value);
156 void CrosSettings::RemoveFromList(const std::string& path,
157 const base::Value* value) {
158 DCHECK(CalledOnValidThread());
159 const base::Value* old_value = GetPref(path);
160 scoped_ptr<base::Value> new_value(
161 old_value ? old_value->DeepCopy() : new base::ListValue());
162 static_cast<base::ListValue*>(new_value.get())->Remove(*value, NULL);
163 Set(path, *new_value);
166 bool CrosSettings::GetBoolean(const std::string& path,
167 bool* bool_value) const {
168 DCHECK(CalledOnValidThread());
169 const base::Value* value = GetPref(path);
170 if (value)
171 return value->GetAsBoolean(bool_value);
172 return false;
175 bool CrosSettings::GetInteger(const std::string& path,
176 int* out_value) const {
177 DCHECK(CalledOnValidThread());
178 const base::Value* value = GetPref(path);
179 if (value)
180 return value->GetAsInteger(out_value);
181 return false;
184 bool CrosSettings::GetDouble(const std::string& path,
185 double* out_value) const {
186 DCHECK(CalledOnValidThread());
187 const base::Value* value = GetPref(path);
188 if (value)
189 return value->GetAsDouble(out_value);
190 return false;
193 bool CrosSettings::GetString(const std::string& path,
194 std::string* out_value) const {
195 DCHECK(CalledOnValidThread());
196 const base::Value* value = GetPref(path);
197 if (value)
198 return value->GetAsString(out_value);
199 return false;
202 bool CrosSettings::GetList(const std::string& path,
203 const base::ListValue** out_value) const {
204 DCHECK(CalledOnValidThread());
205 const base::Value* value = GetPref(path);
206 if (value)
207 return value->GetAsList(out_value);
208 return false;
211 bool CrosSettings::GetDictionary(
212 const std::string& path,
213 const base::DictionaryValue** out_value) const {
214 DCHECK(CalledOnValidThread());
215 const base::Value* value = GetPref(path);
216 if (value)
217 return value->GetAsDictionary(out_value);
218 return false;
221 bool CrosSettings::FindEmailInList(const std::string& path,
222 const std::string& email,
223 bool* wildcard_match) const {
224 DCHECK(CalledOnValidThread());
225 std::string canonicalized_email(
226 gaia::CanonicalizeEmail(gaia::SanitizeEmail(email)));
227 std::string wildcard_email;
228 std::string::size_type at_pos = canonicalized_email.find('@');
229 if (at_pos != std::string::npos) {
230 wildcard_email =
231 std::string("*").append(canonicalized_email.substr(at_pos));
234 if (wildcard_match)
235 *wildcard_match = false;
237 const base::ListValue* list;
238 if (!GetList(path, &list))
239 return false;
241 bool found_wildcard_match = false;
242 for (base::ListValue::const_iterator entry(list->begin());
243 entry != list->end();
244 ++entry) {
245 std::string entry_string;
246 if (!(*entry)->GetAsString(&entry_string)) {
247 NOTREACHED();
248 continue;
250 std::string canonicalized_entry(
251 gaia::CanonicalizeEmail(gaia::SanitizeEmail(entry_string)));
253 if (canonicalized_entry != wildcard_email &&
254 canonicalized_entry == canonicalized_email) {
255 return true;
258 // If there is a wildcard match, don't exit early. There might be an exact
259 // match further down the list that should take precedence if present.
260 if (canonicalized_entry == wildcard_email)
261 found_wildcard_match = true;
264 if (wildcard_match)
265 *wildcard_match = found_wildcard_match;
267 return found_wildcard_match;
270 bool CrosSettings::AddSettingsProvider(CrosSettingsProvider* provider) {
271 DCHECK(CalledOnValidThread());
272 providers_.push_back(provider);
274 // Allow the provider to notify this object when settings have changed.
275 // Providers instantiated inside this class will have the same callback
276 // passed to their constructor, but doing it here allows for providers
277 // to be instantiated outside this class.
278 CrosSettingsProvider::NotifyObserversCallback notify_cb(
279 base::Bind(&CrosSettings::FireObservers, base::Unretained(this)));
280 provider->SetNotifyObserversCallback(notify_cb);
281 return true;
284 bool CrosSettings::RemoveSettingsProvider(CrosSettingsProvider* provider) {
285 DCHECK(CalledOnValidThread());
286 std::vector<CrosSettingsProvider*>::iterator it =
287 std::find(providers_.begin(), providers_.end(), provider);
288 if (it != providers_.end()) {
289 providers_.erase(it);
290 return true;
292 return false;
295 scoped_ptr<CrosSettings::ObserverSubscription>
296 CrosSettings::AddSettingsObserver(const std::string& path,
297 const base::Closure& callback) {
298 DCHECK(!path.empty());
299 DCHECK(!callback.is_null());
300 DCHECK(CalledOnValidThread());
302 if (!GetProvider(path)) {
303 NOTREACHED() << "Trying to add an observer for an unregistered setting: "
304 << path;
305 return scoped_ptr<CrosSettings::ObserverSubscription>();
308 // Get the callback registry associated with the path.
309 base::CallbackList<void(void)>* registry = NULL;
310 SettingsObserverMap::iterator observer_iterator =
311 settings_observers_.find(path);
312 if (observer_iterator == settings_observers_.end()) {
313 registry = new base::CallbackList<void(void)>;
314 settings_observers_[path] = registry;
315 } else {
316 registry = observer_iterator->second;
319 return registry->Add(callback);
322 CrosSettingsProvider* CrosSettings::GetProvider(
323 const std::string& path) const {
324 for (size_t i = 0; i < providers_.size(); ++i) {
325 if (providers_[i]->HandlesSetting(path))
326 return providers_[i];
328 return NULL;
331 void CrosSettings::FireObservers(const std::string& path) {
332 DCHECK(CalledOnValidThread());
333 SettingsObserverMap::iterator observer_iterator =
334 settings_observers_.find(path);
335 if (observer_iterator == settings_observers_.end())
336 return;
338 observer_iterator->second->Notify();
341 ScopedTestCrosSettings::ScopedTestCrosSettings() {
342 CrosSettings::Initialize();
345 ScopedTestCrosSettings::~ScopedTestCrosSettings() {
346 CrosSettings::Shutdown();
349 } // namespace chromeos