| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/settings/cros_settings.h" | 5 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/chromeos/settings/device_settings_provider.h" | 13 #include "chrome/browser/chromeos/settings/device_settings_provider.h" |
| 14 #include "chrome/browser/chromeos/settings/signed_settings_helper.h" | 14 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 15 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" | 15 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" |
| 16 #include "chrome/browser/chromeos/settings/system_settings_provider.h" | 16 #include "chrome/browser/chromeos/settings/system_settings_provider.h" |
| 17 #include "chrome/common/chrome_notification_types.h" | 17 #include "chrome/common/chrome_notification_types.h" |
| 18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 19 #include "chrome/common/net/gaia/gaia_auth_util.h" | 19 #include "chrome/common/net/gaia/gaia_auth_util.h" |
| 20 #include "content/public/browser/notification_details.h" | 20 #include "content/public/browser/notification_details.h" |
| 21 #include "content/public/browser/notification_source.h" | 21 #include "content/public/browser/notification_source.h" |
| 22 #include "content/public/browser/notification_types.h" | 22 #include "content/public/browser/notification_types.h" |
| 23 | 23 |
| 24 namespace chromeos { | 24 namespace chromeos { |
| 25 | 25 |
| 26 static base::LazyInstance<CrosSettings> g_cros_settings = | 26 static base::LazyInstance<CrosSettings> g_cros_settings = |
| 27 LAZY_INSTANCE_INITIALIZER; | 27 LAZY_INSTANCE_INITIALIZER; |
| 28 | 28 |
| 29 CrosSettings* CrosSettings::Get() { | 29 CrosSettings* CrosSettings::Get() { |
| 30 // TODO(xiyaun): Use real stuff when underlying libcros is ready. | 30 // TODO(xiyaun): Use real stuff when underlying libcros is ready. |
| 31 return g_cros_settings.Pointer(); | 31 return g_cros_settings.Pointer(); |
| 32 } | 32 } |
| 33 | 33 |
| 34 bool CrosSettings::IsCrosSettings(const std::string& path) { | 34 bool CrosSettings::IsCrosSettings(const std::string& path) { |
| 35 return StartsWithASCII(path, kCrosSettingsPrefix, true); | 35 return StartsWithASCII(path, kCrosSettingsPrefix, true); |
| 36 } | 36 } |
| 37 | 37 |
| 38 void CrosSettings::FireObservers(const std::string& path) { | |
| 39 DCHECK(CalledOnValidThread()); | |
| 40 SettingsObserverMap::iterator observer_iterator = | |
| 41 settings_observers_.find(path); | |
| 42 if (observer_iterator == settings_observers_.end()) | |
| 43 return; | |
| 44 | |
| 45 NotificationObserverList::Iterator it(*(observer_iterator->second)); | |
| 46 content::NotificationObserver* observer; | |
| 47 while ((observer = it.GetNext()) != NULL) { | |
| 48 observer->Observe(chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED, | |
| 49 content::Source<CrosSettings>(this), | |
| 50 content::Details<const std::string>(&path)); | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 void CrosSettings::Set(const std::string& path, const base::Value& in_value) { | 38 void CrosSettings::Set(const std::string& path, const base::Value& in_value) { |
| 55 DCHECK(CalledOnValidThread()); | 39 DCHECK(CalledOnValidThread()); |
| 56 CrosSettingsProvider* provider; | 40 CrosSettingsProvider* provider; |
| 57 provider = GetProvider(path); | 41 provider = GetProvider(path); |
| 58 if (provider) | 42 if (provider) |
| 59 provider->Set(path, in_value); | 43 provider->Set(path, in_value); |
| 60 } | 44 } |
| 61 | 45 |
| 46 const base::Value* CrosSettings::GetPref(const std::string& path) const { |
| 47 DCHECK(CalledOnValidThread()); |
| 48 CrosSettingsProvider* provider = GetProvider(path); |
| 49 if (provider) |
| 50 return provider->Get(path); |
| 51 NOTREACHED() << path << " preference was not found in the signed settings."; |
| 52 return NULL; |
| 53 } |
| 54 |
| 55 CrosSettingsProvider::TrustedStatus CrosSettings::PrepareTrustedValues( |
| 56 const base::Closure& callback) const { |
| 57 DCHECK(CalledOnValidThread()); |
| 58 for (size_t i = 0; i < providers_.size(); ++i) { |
| 59 CrosSettingsProvider::TrustedStatus status = |
| 60 providers_[i]->PrepareTrustedValues(callback); |
| 61 if (status != CrosSettingsProvider::TRUSTED) |
| 62 return status; |
| 63 } |
| 64 return CrosSettingsProvider::TRUSTED; |
| 65 } |
| 66 |
| 62 void CrosSettings::SetBoolean(const std::string& path, bool in_value) { | 67 void CrosSettings::SetBoolean(const std::string& path, bool in_value) { |
| 63 DCHECK(CalledOnValidThread()); | 68 DCHECK(CalledOnValidThread()); |
| 64 base::FundamentalValue value(in_value); | 69 base::FundamentalValue value(in_value); |
| 65 Set(path, value); | 70 Set(path, value); |
| 66 } | 71 } |
| 67 | 72 |
| 68 void CrosSettings::SetInteger(const std::string& path, int in_value) { | 73 void CrosSettings::SetInteger(const std::string& path, int in_value) { |
| 69 DCHECK(CalledOnValidThread()); | 74 DCHECK(CalledOnValidThread()); |
| 70 base::FundamentalValue value(in_value); | 75 base::FundamentalValue value(in_value); |
| 71 Set(path, value); | 76 Set(path, value); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 97 void CrosSettings::RemoveFromList(const std::string& path, | 102 void CrosSettings::RemoveFromList(const std::string& path, |
| 98 const base::Value* value) { | 103 const base::Value* value) { |
| 99 DCHECK(CalledOnValidThread()); | 104 DCHECK(CalledOnValidThread()); |
| 100 const base::Value* old_value = GetPref(path); | 105 const base::Value* old_value = GetPref(path); |
| 101 scoped_ptr<base::Value> new_value( | 106 scoped_ptr<base::Value> new_value( |
| 102 old_value ? old_value->DeepCopy() : new base::ListValue()); | 107 old_value ? old_value->DeepCopy() : new base::ListValue()); |
| 103 static_cast<base::ListValue*>(new_value.get())->Remove(*value, NULL); | 108 static_cast<base::ListValue*>(new_value.get())->Remove(*value, NULL); |
| 104 Set(path, *new_value); | 109 Set(path, *new_value); |
| 105 } | 110 } |
| 106 | 111 |
| 112 bool CrosSettings::GetBoolean(const std::string& path, |
| 113 bool* bool_value) const { |
| 114 DCHECK(CalledOnValidThread()); |
| 115 const base::Value* value = GetPref(path); |
| 116 if (value) |
| 117 return value->GetAsBoolean(bool_value); |
| 118 return false; |
| 119 } |
| 120 |
| 121 bool CrosSettings::GetInteger(const std::string& path, |
| 122 int* out_value) const { |
| 123 DCHECK(CalledOnValidThread()); |
| 124 const base::Value* value = GetPref(path); |
| 125 if (value) |
| 126 return value->GetAsInteger(out_value); |
| 127 return false; |
| 128 } |
| 129 |
| 130 bool CrosSettings::GetDouble(const std::string& path, |
| 131 double* out_value) const { |
| 132 DCHECK(CalledOnValidThread()); |
| 133 const base::Value* value = GetPref(path); |
| 134 if (value) |
| 135 return value->GetAsDouble(out_value); |
| 136 return false; |
| 137 } |
| 138 |
| 139 bool CrosSettings::GetString(const std::string& path, |
| 140 std::string* out_value) const { |
| 141 DCHECK(CalledOnValidThread()); |
| 142 const base::Value* value = GetPref(path); |
| 143 if (value) |
| 144 return value->GetAsString(out_value); |
| 145 return false; |
| 146 } |
| 147 |
| 148 bool CrosSettings::GetList(const std::string& path, |
| 149 const base::ListValue** out_value) const { |
| 150 DCHECK(CalledOnValidThread()); |
| 151 const base::Value* value = GetPref(path); |
| 152 if (value) |
| 153 return value->GetAsList(out_value); |
| 154 return false; |
| 155 } |
| 156 |
| 107 bool CrosSettings::FindEmailInList(const std::string& path, | 157 bool CrosSettings::FindEmailInList(const std::string& path, |
| 108 const std::string& email) const { | 158 const std::string& email) const { |
| 109 DCHECK(CalledOnValidThread()); | 159 DCHECK(CalledOnValidThread()); |
| 110 std::string canonicalized_email( | 160 std::string canonicalized_email( |
| 111 gaia::CanonicalizeEmail(gaia::SanitizeEmail(email))); | 161 gaia::CanonicalizeEmail(gaia::SanitizeEmail(email))); |
| 112 std::string wildcard_email; | 162 std::string wildcard_email; |
| 113 std::string::size_type at_pos = canonicalized_email.find('@'); | 163 std::string::size_type at_pos = canonicalized_email.find('@'); |
| 114 if (at_pos != std::string::npos) { | 164 if (at_pos != std::string::npos) { |
| 115 wildcard_email = | 165 wildcard_email = |
| 116 std::string("*").append(canonicalized_email.substr(at_pos)); | 166 std::string("*").append(canonicalized_email.substr(at_pos)); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 | 263 |
| 214 CrosSettingsProvider* CrosSettings::GetProvider( | 264 CrosSettingsProvider* CrosSettings::GetProvider( |
| 215 const std::string& path) const { | 265 const std::string& path) const { |
| 216 for (size_t i = 0; i < providers_.size(); ++i) { | 266 for (size_t i = 0; i < providers_.size(); ++i) { |
| 217 if (providers_[i]->HandlesSetting(path)) | 267 if (providers_[i]->HandlesSetting(path)) |
| 218 return providers_[i]; | 268 return providers_[i]; |
| 219 } | 269 } |
| 220 return NULL; | 270 return NULL; |
| 221 } | 271 } |
| 222 | 272 |
| 223 void CrosSettings::ReloadProviders() { | |
| 224 for (size_t i = 0; i < providers_.size(); ++i) | |
| 225 providers_[i]->Reload(); | |
| 226 } | |
| 227 | |
| 228 const base::Value* CrosSettings::GetPref(const std::string& path) const { | |
| 229 DCHECK(CalledOnValidThread()); | |
| 230 CrosSettingsProvider* provider = GetProvider(path); | |
| 231 if (provider) | |
| 232 return provider->Get(path); | |
| 233 NOTREACHED() << path << " preference was not found in the signed settings."; | |
| 234 return NULL; | |
| 235 } | |
| 236 | |
| 237 CrosSettingsProvider::TrustedStatus CrosSettings::PrepareTrustedValues( | |
| 238 const base::Closure& callback) const { | |
| 239 DCHECK(CalledOnValidThread()); | |
| 240 for (size_t i = 0; i < providers_.size(); ++i) { | |
| 241 CrosSettingsProvider::TrustedStatus status = | |
| 242 providers_[i]->PrepareTrustedValues(callback); | |
| 243 if (status != CrosSettingsProvider::TRUSTED) | |
| 244 return status; | |
| 245 } | |
| 246 return CrosSettingsProvider::TRUSTED; | |
| 247 } | |
| 248 | |
| 249 bool CrosSettings::GetBoolean(const std::string& path, | |
| 250 bool* bool_value) const { | |
| 251 DCHECK(CalledOnValidThread()); | |
| 252 const base::Value* value = GetPref(path); | |
| 253 if (value) | |
| 254 return value->GetAsBoolean(bool_value); | |
| 255 return false; | |
| 256 } | |
| 257 | |
| 258 bool CrosSettings::GetInteger(const std::string& path, | |
| 259 int* out_value) const { | |
| 260 DCHECK(CalledOnValidThread()); | |
| 261 const base::Value* value = GetPref(path); | |
| 262 if (value) | |
| 263 return value->GetAsInteger(out_value); | |
| 264 return false; | |
| 265 } | |
| 266 | |
| 267 bool CrosSettings::GetDouble(const std::string& path, | |
| 268 double* out_value) const { | |
| 269 DCHECK(CalledOnValidThread()); | |
| 270 const base::Value* value = GetPref(path); | |
| 271 if (value) | |
| 272 return value->GetAsDouble(out_value); | |
| 273 return false; | |
| 274 } | |
| 275 | |
| 276 bool CrosSettings::GetString(const std::string& path, | |
| 277 std::string* out_value) const { | |
| 278 DCHECK(CalledOnValidThread()); | |
| 279 const base::Value* value = GetPref(path); | |
| 280 if (value) | |
| 281 return value->GetAsString(out_value); | |
| 282 return false; | |
| 283 } | |
| 284 | |
| 285 bool CrosSettings::GetList(const std::string& path, | |
| 286 const base::ListValue** out_value) const { | |
| 287 DCHECK(CalledOnValidThread()); | |
| 288 const base::Value* value = GetPref(path); | |
| 289 if (value) | |
| 290 return value->GetAsList(out_value); | |
| 291 return false; | |
| 292 } | |
| 293 | |
| 294 CrosSettings::CrosSettings() { | 273 CrosSettings::CrosSettings() { |
| 295 CrosSettingsProvider::NotifyObserversCallback notify_cb( | 274 CrosSettingsProvider::NotifyObserversCallback notify_cb( |
| 296 base::Bind(&CrosSettings::FireObservers, | 275 base::Bind(&CrosSettings::FireObservers, |
| 297 // This is safe since |this| is never deleted. | 276 // This is safe since |this| is never deleted. |
| 298 base::Unretained(this))); | 277 base::Unretained(this))); |
| 299 if (CommandLine::ForCurrentProcess()->HasSwitch( | 278 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 300 switches::kStubCrosSettings)) { | 279 switches::kStubCrosSettings)) { |
| 301 AddSettingsProvider(new StubCrosSettingsProvider(notify_cb)); | 280 AddSettingsProvider(new StubCrosSettingsProvider(notify_cb)); |
| 302 } else { | 281 } else { |
| 303 AddSettingsProvider( | 282 AddSettingsProvider( |
| 304 new DeviceSettingsProvider(notify_cb, SignedSettingsHelper::Get())); | 283 new DeviceSettingsProvider(notify_cb, DeviceSettingsService::Get())); |
| 305 } | 284 } |
| 306 // System settings are not mocked currently. | 285 // System settings are not mocked currently. |
| 307 AddSettingsProvider(new SystemSettingsProvider(notify_cb)); | 286 AddSettingsProvider(new SystemSettingsProvider(notify_cb)); |
| 308 } | 287 } |
| 309 | 288 |
| 310 CrosSettings::~CrosSettings() { | 289 CrosSettings::~CrosSettings() { |
| 311 STLDeleteElements(&providers_); | 290 STLDeleteElements(&providers_); |
| 312 STLDeleteValues(&settings_observers_); | 291 STLDeleteValues(&settings_observers_); |
| 313 } | 292 } |
| 314 | 293 |
| 294 void CrosSettings::FireObservers(const std::string& path) { |
| 295 DCHECK(CalledOnValidThread()); |
| 296 SettingsObserverMap::iterator observer_iterator = |
| 297 settings_observers_.find(path); |
| 298 if (observer_iterator == settings_observers_.end()) |
| 299 return; |
| 300 |
| 301 NotificationObserverList::Iterator it(*(observer_iterator->second)); |
| 302 content::NotificationObserver* observer; |
| 303 while ((observer = it.GetNext()) != NULL) { |
| 304 observer->Observe(chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED, |
| 305 content::Source<CrosSettings>(this), |
| 306 content::Details<const std::string>(&path)); |
| 307 } |
| 308 } |
| 309 |
| 315 } // namespace chromeos | 310 } // namespace chromeos |
| OLD | NEW |