1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/cros_settings.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/lazy_instance.h"
#include "base/stl_util.h"
#include "base/string_util.h"
#include "base/values.h"
#include "chrome/browser/chromeos/cros_settings_provider.h"
#include "chrome/browser/chromeos/device_settings_provider.h"
#include "chrome/browser/chromeos/login/authenticator.h"
#include "chrome/browser/chromeos/login/signed_settings_helper.h"
#include "chrome/browser/chromeos/stub_cros_settings_provider.h"
#include "chrome/browser/ui/webui/options2/chromeos/system_settings_provider2.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_switches.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
namespace chromeos {
static base::LazyInstance<CrosSettings> g_cros_settings =
LAZY_INSTANCE_INITIALIZER;
CrosSettings* CrosSettings::Get() {
// TODO(xiyaun): Use real stuff when underlying libcros is ready.
return g_cros_settings.Pointer();
}
bool CrosSettings::IsCrosSettings(const std::string& path) {
return StartsWithASCII(path, kCrosSettingsPrefix, true);
}
void CrosSettings::FireObservers(const std::string& path) {
DCHECK(CalledOnValidThread());
SettingsObserverMap::iterator observer_iterator =
settings_observers_.find(path);
if (observer_iterator == settings_observers_.end())
return;
NotificationObserverList::Iterator it(*(observer_iterator->second));
content::NotificationObserver* observer;
while ((observer = it.GetNext()) != NULL) {
observer->Observe(chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED,
content::Source<CrosSettings>(this),
content::Details<const std::string>(&path));
}
}
void CrosSettings::Set(const std::string& path, const base::Value& in_value) {
DCHECK(CalledOnValidThread());
CrosSettingsProvider* provider;
provider = GetProvider(path);
if (provider)
provider->Set(path, in_value);
}
void CrosSettings::SetBoolean(const std::string& path, bool in_value) {
DCHECK(CalledOnValidThread());
base::FundamentalValue value(in_value);
Set(path, value);
}
void CrosSettings::SetInteger(const std::string& path, int in_value) {
DCHECK(CalledOnValidThread());
base::FundamentalValue value(in_value);
Set(path, value);
}
void CrosSettings::SetDouble(const std::string& path, double in_value) {
DCHECK(CalledOnValidThread());
base::FundamentalValue value(in_value);
Set(path, value);
}
void CrosSettings::SetString(const std::string& path,
const std::string& in_value) {
DCHECK(CalledOnValidThread());
base::StringValue value(in_value);
Set(path, value);
}
void CrosSettings::AppendToList(const std::string& path,
const base::Value* value) {
DCHECK(CalledOnValidThread());
const base::Value* old_value = GetPref(path);
scoped_ptr<base::Value> new_value(
old_value ? old_value->DeepCopy() : new base::ListValue());
static_cast<base::ListValue*>(new_value.get())->Append(value->DeepCopy());
Set(path, *new_value);
}
void CrosSettings::RemoveFromList(const std::string& path,
const base::Value* value) {
DCHECK(CalledOnValidThread());
const base::Value* old_value = GetPref(path);
scoped_ptr<base::Value> new_value(
old_value ? old_value->DeepCopy() : new base::ListValue());
static_cast<base::ListValue*>(new_value.get())->Remove(*value, NULL);
Set(path, *new_value);
}
bool CrosSettings::FindEmailInList(const std::string& path,
const std::string& email) const {
DCHECK(CalledOnValidThread());
std::string canonicalized_email(
Authenticator::Canonicalize(Authenticator::Sanitize(email)));
std::string wildcard_email;
std::string::size_type at_pos = canonicalized_email.find('@');
if (at_pos != std::string::npos) {
wildcard_email =
std::string("*").append(canonicalized_email.substr(at_pos));
}
const base::ListValue* list;
if (!GetList(path, &list))
return false;
for (base::ListValue::const_iterator entry(list->begin());
entry != list->end();
++entry) {
std::string entry_string;
if (!(*entry)->GetAsString(&entry_string)) {
NOTREACHED();
continue;
}
std::string canonicalized_entry(
Authenticator::Canonicalize(Authenticator::Sanitize(entry_string)));
if (canonicalized_entry == canonicalized_email ||
canonicalized_entry == wildcard_email) {
return true;
}
}
return false;
}
bool CrosSettings::AddSettingsProvider(CrosSettingsProvider* provider) {
DCHECK(CalledOnValidThread());
providers_.push_back(provider);
// Allow the provider to notify this object when settings have changed.
// Providers instantiated inside this class will have the same callback
// passed to their constructor, but doing it here allows for providers
// to be instantiated outside this class.
CrosSettingsProvider::NotifyObserversCallback notify_cb(
base::Bind(&CrosSettings::FireObservers, base::Unretained(this)));
provider->SetNotifyObserversCallback(notify_cb);
return true;
}
bool CrosSettings::RemoveSettingsProvider(CrosSettingsProvider* provider) {
DCHECK(CalledOnValidThread());
std::vector<CrosSettingsProvider*>::iterator it =
std::find(providers_.begin(), providers_.end(), provider);
if (it != providers_.end()) {
providers_.erase(it);
return true;
}
return false;
}
void CrosSettings::AddSettingsObserver(const char* path,
content::NotificationObserver* obs) {
DCHECK(path);
DCHECK(obs);
DCHECK(CalledOnValidThread());
if (!GetProvider(std::string(path))) {
NOTREACHED() << "Trying to add an observer for an unregistered setting: "
<< path;
return;
}
// Get the settings observer list associated with the path.
NotificationObserverList* observer_list = NULL;
SettingsObserverMap::iterator observer_iterator =
settings_observers_.find(path);
if (observer_iterator == settings_observers_.end()) {
observer_list = new NotificationObserverList;
settings_observers_[path] = observer_list;
} else {
observer_list = observer_iterator->second;
}
// Verify that this observer doesn't already exist.
NotificationObserverList::Iterator it(*observer_list);
content::NotificationObserver* existing_obs;
while ((existing_obs = it.GetNext()) != NULL) {
if (existing_obs == obs)
return;
}
// Ok, safe to add the pref observer.
observer_list->AddObserver(obs);
}
void CrosSettings::RemoveSettingsObserver(const char* path,
content::NotificationObserver* obs) {
DCHECK(CalledOnValidThread());
SettingsObserverMap::iterator observer_iterator =
settings_observers_.find(path);
if (observer_iterator == settings_observers_.end())
return;
NotificationObserverList* observer_list = observer_iterator->second;
observer_list->RemoveObserver(obs);
}
CrosSettingsProvider* CrosSettings::GetProvider(
const std::string& path) const {
for (size_t i = 0; i < providers_.size(); ++i) {
if (providers_[i]->HandlesSetting(path))
return providers_[i];
}
return NULL;
}
void CrosSettings::ReloadProviders() {
for (size_t i = 0; i < providers_.size(); ++i)
providers_[i]->Reload();
}
const base::Value* CrosSettings::GetPref(const std::string& path) const {
DCHECK(CalledOnValidThread());
CrosSettingsProvider* provider = GetProvider(path);
if (provider)
return provider->Get(path);
NOTREACHED() << path << " preference was not found in the signed settings.";
return NULL;
}
bool CrosSettings::PrepareTrustedValues(const base::Closure& callback) const {
DCHECK(CalledOnValidThread());
for (size_t i = 0; i < providers_.size(); ++i) {
if (!providers_[i]->PrepareTrustedValues(callback))
return false;
}
return true;
}
bool CrosSettings::GetBoolean(const std::string& path,
bool* bool_value) const {
DCHECK(CalledOnValidThread());
const base::Value* value = GetPref(path);
if (value)
return value->GetAsBoolean(bool_value);
return false;
}
bool CrosSettings::GetInteger(const std::string& path,
int* out_value) const {
DCHECK(CalledOnValidThread());
const base::Value* value = GetPref(path);
if (value)
return value->GetAsInteger(out_value);
return false;
}
bool CrosSettings::GetDouble(const std::string& path,
double* out_value) const {
DCHECK(CalledOnValidThread());
const base::Value* value = GetPref(path);
if (value)
return value->GetAsDouble(out_value);
return false;
}
bool CrosSettings::GetString(const std::string& path,
std::string* out_value) const {
DCHECK(CalledOnValidThread());
const base::Value* value = GetPref(path);
if (value)
return value->GetAsString(out_value);
return false;
}
bool CrosSettings::GetList(const std::string& path,
const base::ListValue** out_value) const {
DCHECK(CalledOnValidThread());
const base::Value* value = GetPref(path);
if (value)
return value->GetAsList(out_value);
return false;
}
CrosSettings::CrosSettings() {
CrosSettingsProvider::NotifyObserversCallback notify_cb(
base::Bind(&CrosSettings::FireObservers,
// This is safe since |this| is never deleted.
base::Unretained(this)));
if (CommandLine::ForCurrentProcess()->HasSwitch(
switches::kStubCrosSettings)) {
AddSettingsProvider(new StubCrosSettingsProvider(notify_cb));
} else {
AddSettingsProvider(
new DeviceSettingsProvider(notify_cb, SignedSettingsHelper::Get()));
}
// System settings are not mocked currently.
AddSettingsProvider(new options2::SystemSettingsProvider(notify_cb));
}
CrosSettings::~CrosSettings() {
STLDeleteElements(&providers_);
STLDeleteValues(&settings_observers_);
}
} // namespace chromeos
|