Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / chromeos / settings / cros_settings.cc
blob76cc290dbf5e2563cc57c9bba7fcc813d3cd257a
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 base::StartsWith(path, kCrosSettingsPrefix,
90 base::CompareCase::SENSITIVE);
93 void CrosSettings::Set(const std::string& path, const base::Value& in_value) {
94 DCHECK(CalledOnValidThread());
95 CrosSettingsProvider* provider;
96 provider = GetProvider(path);
97 if (provider)
98 provider->Set(path, in_value);
101 const base::Value* CrosSettings::GetPref(const std::string& path) const {
102 DCHECK(CalledOnValidThread());
103 CrosSettingsProvider* provider = GetProvider(path);
104 if (provider)
105 return provider->Get(path);
106 NOTREACHED() << path << " preference was not found in the signed settings.";
107 return NULL;
110 CrosSettingsProvider::TrustedStatus CrosSettings::PrepareTrustedValues(
111 const base::Closure& callback) const {
112 DCHECK(CalledOnValidThread());
113 for (size_t i = 0; i < providers_.size(); ++i) {
114 CrosSettingsProvider::TrustedStatus status =
115 providers_[i]->PrepareTrustedValues(callback);
116 if (status != CrosSettingsProvider::TRUSTED)
117 return status;
119 return CrosSettingsProvider::TRUSTED;
122 void CrosSettings::SetBoolean(const std::string& path, bool in_value) {
123 DCHECK(CalledOnValidThread());
124 base::FundamentalValue value(in_value);
125 Set(path, value);
128 void CrosSettings::SetInteger(const std::string& path, int in_value) {
129 DCHECK(CalledOnValidThread());
130 base::FundamentalValue value(in_value);
131 Set(path, value);
134 void CrosSettings::SetDouble(const std::string& path, double in_value) {
135 DCHECK(CalledOnValidThread());
136 base::FundamentalValue value(in_value);
137 Set(path, value);
140 void CrosSettings::SetString(const std::string& path,
141 const std::string& in_value) {
142 DCHECK(CalledOnValidThread());
143 base::StringValue value(in_value);
144 Set(path, value);
147 void CrosSettings::AppendToList(const std::string& path,
148 const base::Value* value) {
149 DCHECK(CalledOnValidThread());
150 const base::Value* old_value = GetPref(path);
151 scoped_ptr<base::Value> new_value(
152 old_value ? old_value->DeepCopy() : new base::ListValue());
153 static_cast<base::ListValue*>(new_value.get())->Append(value->DeepCopy());
154 Set(path, *new_value);
157 void CrosSettings::RemoveFromList(const std::string& path,
158 const base::Value* value) {
159 DCHECK(CalledOnValidThread());
160 const base::Value* old_value = GetPref(path);
161 scoped_ptr<base::Value> new_value(
162 old_value ? old_value->DeepCopy() : new base::ListValue());
163 static_cast<base::ListValue*>(new_value.get())->Remove(*value, NULL);
164 Set(path, *new_value);
167 bool CrosSettings::GetBoolean(const std::string& path,
168 bool* bool_value) const {
169 DCHECK(CalledOnValidThread());
170 const base::Value* value = GetPref(path);
171 if (value)
172 return value->GetAsBoolean(bool_value);
173 return false;
176 bool CrosSettings::GetInteger(const std::string& path,
177 int* out_value) const {
178 DCHECK(CalledOnValidThread());
179 const base::Value* value = GetPref(path);
180 if (value)
181 return value->GetAsInteger(out_value);
182 return false;
185 bool CrosSettings::GetDouble(const std::string& path,
186 double* out_value) const {
187 DCHECK(CalledOnValidThread());
188 const base::Value* value = GetPref(path);
189 if (value)
190 return value->GetAsDouble(out_value);
191 return false;
194 bool CrosSettings::GetString(const std::string& path,
195 std::string* out_value) const {
196 DCHECK(CalledOnValidThread());
197 const base::Value* value = GetPref(path);
198 if (value)
199 return value->GetAsString(out_value);
200 return false;
203 bool CrosSettings::GetList(const std::string& path,
204 const base::ListValue** out_value) const {
205 DCHECK(CalledOnValidThread());
206 const base::Value* value = GetPref(path);
207 if (value)
208 return value->GetAsList(out_value);
209 return false;
212 bool CrosSettings::GetDictionary(
213 const std::string& path,
214 const base::DictionaryValue** out_value) const {
215 DCHECK(CalledOnValidThread());
216 const base::Value* value = GetPref(path);
217 if (value)
218 return value->GetAsDictionary(out_value);
219 return false;
222 bool CrosSettings::FindEmailInList(const std::string& path,
223 const std::string& email,
224 bool* wildcard_match) const {
225 DCHECK(CalledOnValidThread());
226 std::string canonicalized_email(
227 gaia::CanonicalizeEmail(gaia::SanitizeEmail(email)));
228 std::string wildcard_email;
229 std::string::size_type at_pos = canonicalized_email.find('@');
230 if (at_pos != std::string::npos) {
231 wildcard_email =
232 std::string("*").append(canonicalized_email.substr(at_pos));
235 if (wildcard_match)
236 *wildcard_match = false;
238 const base::ListValue* list;
239 if (!GetList(path, &list))
240 return false;
242 bool found_wildcard_match = false;
243 for (base::ListValue::const_iterator entry(list->begin());
244 entry != list->end();
245 ++entry) {
246 std::string entry_string;
247 if (!(*entry)->GetAsString(&entry_string)) {
248 NOTREACHED();
249 continue;
251 std::string canonicalized_entry(
252 gaia::CanonicalizeEmail(gaia::SanitizeEmail(entry_string)));
254 if (canonicalized_entry != wildcard_email &&
255 canonicalized_entry == canonicalized_email) {
256 return true;
259 // If there is a wildcard match, don't exit early. There might be an exact
260 // match further down the list that should take precedence if present.
261 if (canonicalized_entry == wildcard_email)
262 found_wildcard_match = true;
265 if (wildcard_match)
266 *wildcard_match = found_wildcard_match;
268 return found_wildcard_match;
271 bool CrosSettings::AddSettingsProvider(CrosSettingsProvider* provider) {
272 DCHECK(CalledOnValidThread());
273 providers_.push_back(provider);
275 // Allow the provider to notify this object when settings have changed.
276 // Providers instantiated inside this class will have the same callback
277 // passed to their constructor, but doing it here allows for providers
278 // to be instantiated outside this class.
279 CrosSettingsProvider::NotifyObserversCallback notify_cb(
280 base::Bind(&CrosSettings::FireObservers, base::Unretained(this)));
281 provider->SetNotifyObserversCallback(notify_cb);
282 return true;
285 bool CrosSettings::RemoveSettingsProvider(CrosSettingsProvider* provider) {
286 DCHECK(CalledOnValidThread());
287 std::vector<CrosSettingsProvider*>::iterator it =
288 std::find(providers_.begin(), providers_.end(), provider);
289 if (it != providers_.end()) {
290 providers_.erase(it);
291 return true;
293 return false;
296 scoped_ptr<CrosSettings::ObserverSubscription>
297 CrosSettings::AddSettingsObserver(const std::string& path,
298 const base::Closure& callback) {
299 DCHECK(!path.empty());
300 DCHECK(!callback.is_null());
301 DCHECK(CalledOnValidThread());
303 if (!GetProvider(path)) {
304 NOTREACHED() << "Trying to add an observer for an unregistered setting: "
305 << path;
306 return scoped_ptr<CrosSettings::ObserverSubscription>();
309 // Get the callback registry associated with the path.
310 base::CallbackList<void(void)>* registry = NULL;
311 SettingsObserverMap::iterator observer_iterator =
312 settings_observers_.find(path);
313 if (observer_iterator == settings_observers_.end()) {
314 registry = new base::CallbackList<void(void)>;
315 settings_observers_[path] = registry;
316 } else {
317 registry = observer_iterator->second;
320 return registry->Add(callback);
323 CrosSettingsProvider* CrosSettings::GetProvider(
324 const std::string& path) const {
325 for (size_t i = 0; i < providers_.size(); ++i) {
326 if (providers_[i]->HandlesSetting(path))
327 return providers_[i];
329 return NULL;
332 void CrosSettings::FireObservers(const std::string& path) {
333 DCHECK(CalledOnValidThread());
334 SettingsObserverMap::iterator observer_iterator =
335 settings_observers_.find(path);
336 if (observer_iterator == settings_observers_.end())
337 return;
339 observer_iterator->second->Notify();
342 ScopedTestCrosSettings::ScopedTestCrosSettings() {
343 CrosSettings::Initialize();
346 ScopedTestCrosSettings::~ScopedTestCrosSettings() {
347 CrosSettings::Shutdown();
350 } // namespace chromeos