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"
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"
23 static CrosSettings
* g_cros_settings
= NULL
;
26 void CrosSettings::Initialize() {
27 CHECK(!g_cros_settings
);
28 g_cros_settings
= new CrosSettings(DeviceSettingsService::Get());
32 bool CrosSettings::IsInitialized() {
33 return g_cros_settings
;
37 void CrosSettings::Shutdown() {
38 DCHECK(g_cros_settings
);
39 delete g_cros_settings
;
40 g_cros_settings
= NULL
;
44 CrosSettings
* CrosSettings::Get() {
45 CHECK(g_cros_settings
);
46 return g_cros_settings
;
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
)) {
58 CrosSettings
* cros_settings
= CrosSettings::Get();
59 bool allow_new_user
= false;
60 cros_settings
->GetBoolean(kAccountsPrefAllowNewUser
, &allow_new_user
);
63 return cros_settings
->FindEmailInList(kAccountsPrefUsers
, username
,
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
));
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
);
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
);
105 return provider
->Get(path
);
106 NOTREACHED() << path
<< " preference was not found in the signed settings.";
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
)
119 return CrosSettingsProvider::TRUSTED
;
122 void CrosSettings::SetBoolean(const std::string
& path
, bool in_value
) {
123 DCHECK(CalledOnValidThread());
124 base::FundamentalValue
value(in_value
);
128 void CrosSettings::SetInteger(const std::string
& path
, int in_value
) {
129 DCHECK(CalledOnValidThread());
130 base::FundamentalValue
value(in_value
);
134 void CrosSettings::SetDouble(const std::string
& path
, double in_value
) {
135 DCHECK(CalledOnValidThread());
136 base::FundamentalValue
value(in_value
);
140 void CrosSettings::SetString(const std::string
& path
,
141 const std::string
& in_value
) {
142 DCHECK(CalledOnValidThread());
143 base::StringValue
value(in_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
);
172 return value
->GetAsBoolean(bool_value
);
176 bool CrosSettings::GetInteger(const std::string
& path
,
177 int* out_value
) const {
178 DCHECK(CalledOnValidThread());
179 const base::Value
* value
= GetPref(path
);
181 return value
->GetAsInteger(out_value
);
185 bool CrosSettings::GetDouble(const std::string
& path
,
186 double* out_value
) const {
187 DCHECK(CalledOnValidThread());
188 const base::Value
* value
= GetPref(path
);
190 return value
->GetAsDouble(out_value
);
194 bool CrosSettings::GetString(const std::string
& path
,
195 std::string
* out_value
) const {
196 DCHECK(CalledOnValidThread());
197 const base::Value
* value
= GetPref(path
);
199 return value
->GetAsString(out_value
);
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
);
208 return value
->GetAsList(out_value
);
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
);
218 return value
->GetAsDictionary(out_value
);
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
) {
232 std::string("*").append(canonicalized_email
.substr(at_pos
));
236 *wildcard_match
= false;
238 const base::ListValue
* list
;
239 if (!GetList(path
, &list
))
242 bool found_wildcard_match
= false;
243 for (base::ListValue::const_iterator
entry(list
->begin());
244 entry
!= list
->end();
246 std::string entry_string
;
247 if (!(*entry
)->GetAsString(&entry_string
)) {
251 std::string
canonicalized_entry(
252 gaia::CanonicalizeEmail(gaia::SanitizeEmail(entry_string
)));
254 if (canonicalized_entry
!= wildcard_email
&&
255 canonicalized_entry
== canonicalized_email
) {
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;
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
);
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
);
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: "
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
;
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
];
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())
339 observer_iterator
->second
->Notify();
342 ScopedTestCrosSettings::ScopedTestCrosSettings() {
343 CrosSettings::Initialize();
346 ScopedTestCrosSettings::~ScopedTestCrosSettings() {
347 CrosSettings::Shutdown();
350 } // namespace chromeos