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 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
);
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
);
104 return provider
->Get(path
);
105 NOTREACHED() << path
<< " preference was not found in the signed settings.";
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
)
118 return CrosSettingsProvider::TRUSTED
;
121 void CrosSettings::SetBoolean(const std::string
& path
, bool in_value
) {
122 DCHECK(CalledOnValidThread());
123 base::FundamentalValue
value(in_value
);
127 void CrosSettings::SetInteger(const std::string
& path
, int in_value
) {
128 DCHECK(CalledOnValidThread());
129 base::FundamentalValue
value(in_value
);
133 void CrosSettings::SetDouble(const std::string
& path
, double in_value
) {
134 DCHECK(CalledOnValidThread());
135 base::FundamentalValue
value(in_value
);
139 void CrosSettings::SetString(const std::string
& path
,
140 const std::string
& in_value
) {
141 DCHECK(CalledOnValidThread());
142 base::StringValue
value(in_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
);
171 return value
->GetAsBoolean(bool_value
);
175 bool CrosSettings::GetInteger(const std::string
& path
,
176 int* out_value
) const {
177 DCHECK(CalledOnValidThread());
178 const base::Value
* value
= GetPref(path
);
180 return value
->GetAsInteger(out_value
);
184 bool CrosSettings::GetDouble(const std::string
& path
,
185 double* out_value
) const {
186 DCHECK(CalledOnValidThread());
187 const base::Value
* value
= GetPref(path
);
189 return value
->GetAsDouble(out_value
);
193 bool CrosSettings::GetString(const std::string
& path
,
194 std::string
* out_value
) const {
195 DCHECK(CalledOnValidThread());
196 const base::Value
* value
= GetPref(path
);
198 return value
->GetAsString(out_value
);
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
);
207 return value
->GetAsList(out_value
);
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
);
217 return value
->GetAsDictionary(out_value
);
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
) {
231 std::string("*").append(canonicalized_email
.substr(at_pos
));
235 *wildcard_match
= false;
237 const base::ListValue
* list
;
238 if (!GetList(path
, &list
))
241 bool found_wildcard_match
= false;
242 for (base::ListValue::const_iterator
entry(list
->begin());
243 entry
!= list
->end();
245 std::string entry_string
;
246 if (!(*entry
)->GetAsString(&entry_string
)) {
250 std::string
canonicalized_entry(
251 gaia::CanonicalizeEmail(gaia::SanitizeEmail(entry_string
)));
253 if (canonicalized_entry
!= wildcard_email
&&
254 canonicalized_entry
== canonicalized_email
) {
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;
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
);
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
);
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: "
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
;
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
];
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())
338 observer_iterator
->second
->Notify();
341 ScopedTestCrosSettings::ScopedTestCrosSettings() {
342 CrosSettings::Initialize();
345 ScopedTestCrosSettings::~ScopedTestCrosSettings() {
346 CrosSettings::Shutdown();
349 } // namespace chromeos