| 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/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/chrome_notification_types.h" | |
| 14 #include "chrome/browser/chromeos/settings/device_settings_provider.h" | 13 #include "chrome/browser/chromeos/settings/device_settings_provider.h" |
| 15 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 14 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 16 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" | 15 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" |
| 17 #include "chrome/browser/chromeos/settings/system_settings_provider.h" | 16 #include "chrome/browser/chromeos/settings/system_settings_provider.h" |
| 18 #include "chromeos/chromeos_switches.h" | 17 #include "chromeos/chromeos_switches.h" |
| 19 #include "content/public/browser/notification_details.h" | |
| 20 #include "content/public/browser/notification_source.h" | |
| 21 #include "content/public/browser/notification_types.h" | |
| 22 #include "google_apis/gaia/gaia_auth_util.h" | 18 #include "google_apis/gaia/gaia_auth_util.h" |
| 23 | 19 |
| 24 namespace chromeos { | 20 namespace chromeos { |
| 25 | 21 |
| 26 static CrosSettings* g_cros_settings = NULL; | 22 static CrosSettings* g_cros_settings = NULL; |
| 27 | 23 |
| 28 // static | 24 // static |
| 29 void CrosSettings::Initialize() { | 25 void CrosSettings::Initialize() { |
| 30 CHECK(!g_cros_settings); | 26 CHECK(!g_cros_settings); |
| 31 g_cros_settings = new CrosSettings(DeviceSettingsService::Get()); | 27 g_cros_settings = new CrosSettings(DeviceSettingsService::Get()); |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 DCHECK(CalledOnValidThread()); | 251 DCHECK(CalledOnValidThread()); |
| 256 std::vector<CrosSettingsProvider*>::iterator it = | 252 std::vector<CrosSettingsProvider*>::iterator it = |
| 257 std::find(providers_.begin(), providers_.end(), provider); | 253 std::find(providers_.begin(), providers_.end(), provider); |
| 258 if (it != providers_.end()) { | 254 if (it != providers_.end()) { |
| 259 providers_.erase(it); | 255 providers_.erase(it); |
| 260 return true; | 256 return true; |
| 261 } | 257 } |
| 262 return false; | 258 return false; |
| 263 } | 259 } |
| 264 | 260 |
| 265 void CrosSettings::AddSettingsObserver(const char* path, | 261 scoped_ptr<CrosSettings::ObserverSubscription> |
| 266 content::NotificationObserver* obs) { | 262 CrosSettings::AddSettingsObserver(const std::string& path, |
| 267 DCHECK(path); | 263 const base::Closure& callback) { |
| 268 DCHECK(obs); | 264 DCHECK(!path.empty()); |
| 265 DCHECK(!callback.is_null()); |
| 269 DCHECK(CalledOnValidThread()); | 266 DCHECK(CalledOnValidThread()); |
| 270 | 267 |
| 271 if (!GetProvider(std::string(path))) { | 268 if (!GetProvider(path)) { |
| 272 NOTREACHED() << "Trying to add an observer for an unregistered setting: " | 269 NOTREACHED() << "Trying to add an observer for an unregistered setting: " |
| 273 << path; | 270 << path; |
| 274 return; | 271 return scoped_ptr<CrosSettings::ObserverSubscription>(); |
| 275 } | 272 } |
| 276 | 273 |
| 277 // Get the settings observer list associated with the path. | 274 // Get the callback registry associated with the path. |
| 278 NotificationObserverList* observer_list = NULL; | 275 base::CallbackRegistry<void>* registry = NULL; |
| 279 SettingsObserverMap::iterator observer_iterator = | 276 SettingsObserverMap::iterator observer_iterator = |
| 280 settings_observers_.find(path); | 277 settings_observers_.find(path); |
| 281 if (observer_iterator == settings_observers_.end()) { | 278 if (observer_iterator == settings_observers_.end()) { |
| 282 observer_list = new NotificationObserverList; | 279 registry = new base::CallbackRegistry<void>; |
| 283 settings_observers_[path] = observer_list; | 280 settings_observers_[path] = registry; |
| 284 } else { | 281 } else { |
| 285 observer_list = observer_iterator->second; | 282 registry = observer_iterator->second; |
| 286 } | 283 } |
| 287 | 284 |
| 288 // Verify that this observer doesn't already exist. | 285 return registry->Add(callback); |
| 289 NotificationObserverList::Iterator it(*observer_list); | |
| 290 content::NotificationObserver* existing_obs; | |
| 291 while ((existing_obs = it.GetNext()) != NULL) { | |
| 292 if (existing_obs == obs) | |
| 293 return; | |
| 294 } | |
| 295 | |
| 296 // Ok, safe to add the pref observer. | |
| 297 observer_list->AddObserver(obs); | |
| 298 } | |
| 299 | |
| 300 void CrosSettings::RemoveSettingsObserver(const char* path, | |
| 301 content::NotificationObserver* obs) { | |
| 302 DCHECK(CalledOnValidThread()); | |
| 303 | |
| 304 SettingsObserverMap::iterator observer_iterator = | |
| 305 settings_observers_.find(path); | |
| 306 if (observer_iterator == settings_observers_.end()) | |
| 307 return; | |
| 308 | |
| 309 NotificationObserverList* observer_list = observer_iterator->second; | |
| 310 observer_list->RemoveObserver(obs); | |
| 311 } | 286 } |
| 312 | 287 |
| 313 CrosSettingsProvider* CrosSettings::GetProvider( | 288 CrosSettingsProvider* CrosSettings::GetProvider( |
| 314 const std::string& path) const { | 289 const std::string& path) const { |
| 315 for (size_t i = 0; i < providers_.size(); ++i) { | 290 for (size_t i = 0; i < providers_.size(); ++i) { |
| 316 if (providers_[i]->HandlesSetting(path)) | 291 if (providers_[i]->HandlesSetting(path)) |
| 317 return providers_[i]; | 292 return providers_[i]; |
| 318 } | 293 } |
| 319 return NULL; | 294 return NULL; |
| 320 } | 295 } |
| 321 | 296 |
| 322 void CrosSettings::FireObservers(const std::string& path) { | 297 void CrosSettings::FireObservers(const std::string& path) { |
| 323 DCHECK(CalledOnValidThread()); | 298 DCHECK(CalledOnValidThread()); |
| 324 SettingsObserverMap::iterator observer_iterator = | 299 SettingsObserverMap::iterator observer_iterator = |
| 325 settings_observers_.find(path); | 300 settings_observers_.find(path); |
| 326 if (observer_iterator == settings_observers_.end()) | 301 if (observer_iterator == settings_observers_.end()) |
| 327 return; | 302 return; |
| 328 | 303 |
| 329 NotificationObserverList::Iterator it(*(observer_iterator->second)); | 304 observer_iterator->second->Notify(); |
| 330 content::NotificationObserver* observer; | |
| 331 while ((observer = it.GetNext()) != NULL) { | |
| 332 observer->Observe(chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED, | |
| 333 content::Source<CrosSettings>(this), | |
| 334 content::Details<const std::string>(&path)); | |
| 335 } | |
| 336 } | 305 } |
| 337 | 306 |
| 338 ScopedTestCrosSettings::ScopedTestCrosSettings() { | 307 ScopedTestCrosSettings::ScopedTestCrosSettings() { |
| 339 CrosSettings::Initialize(); | 308 CrosSettings::Initialize(); |
| 340 } | 309 } |
| 341 | 310 |
| 342 ScopedTestCrosSettings::~ScopedTestCrosSettings() { | 311 ScopedTestCrosSettings::~ScopedTestCrosSettings() { |
| 343 CrosSettings::Shutdown(); | 312 CrosSettings::Shutdown(); |
| 344 } | 313 } |
| 345 | 314 |
| 346 } // namespace chromeos | 315 } // namespace chromeos |
| OLD | NEW |