Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: chrome/browser/chromeos/settings/cros_settings.cc

Issue 23494053: Remove NOTIFICATION_SYSTEM_SETTING_CHANGED, switch CrosSettings to base::CallbackRegistry. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: oops Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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" 13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/chromeos/settings/device_settings_provider.h" 14 #include "chrome/browser/chromeos/settings/device_settings_provider.h"
15 #include "chrome/browser/chromeos/settings/device_settings_service.h" 15 #include "chrome/browser/chromeos/settings/device_settings_service.h"
16 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" 16 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h"
17 #include "chrome/browser/chromeos/settings/system_settings_provider.h" 17 #include "chrome/browser/chromeos/settings/system_settings_provider.h"
18 #include "chromeos/chromeos_switches.h" 18 #include "chromeos/chromeos_switches.h"
19 #include "content/public/browser/notification_details.h" 19 #include "content/public/browser/notification_details.h"
Lei Zhang 2013/09/18 05:00:53 Remove
Avi (use Gerrit) 2013/09/18 16:41:44 Done.
20 #include "content/public/browser/notification_source.h" 20 #include "content/public/browser/notification_source.h"
21 #include "content/public/browser/notification_types.h" 21 #include "content/public/browser/notification_types.h"
22 #include "google_apis/gaia/gaia_auth_util.h" 22 #include "google_apis/gaia/gaia_auth_util.h"
23 23
24 namespace chromeos { 24 namespace chromeos {
25 25
26 static CrosSettings* g_cros_settings = NULL; 26 static CrosSettings* g_cros_settings = NULL;
27 27
28 // static 28 // static
29 void CrosSettings::Initialize() { 29 void CrosSettings::Initialize() {
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 DCHECK(CalledOnValidThread()); 255 DCHECK(CalledOnValidThread());
256 std::vector<CrosSettingsProvider*>::iterator it = 256 std::vector<CrosSettingsProvider*>::iterator it =
257 std::find(providers_.begin(), providers_.end(), provider); 257 std::find(providers_.begin(), providers_.end(), provider);
258 if (it != providers_.end()) { 258 if (it != providers_.end()) {
259 providers_.erase(it); 259 providers_.erase(it);
260 return true; 260 return true;
261 } 261 }
262 return false; 262 return false;
263 } 263 }
264 264
265 void CrosSettings::AddSettingsObserver(const char* path, 265 scoped_ptr<CrosSettings::ObserverSubscription>
266 content::NotificationObserver* obs) { 266 CrosSettings::AddSettingsObserver(
267 const char* path, const base::Closure& callback) {
Lei Zhang 2013/09/18 05:00:53 Why can't |path| be a const std::string& ?
Mattias Nissler (ping if slow) 2013/09/18 09:09:48 nit: parameters on separate lines.
Avi (use Gerrit) 2013/09/18 16:41:44 It's been that way since day 1, three years ago: h
Avi (use Gerrit) 2013/09/18 16:41:44 Done.
Avi (use Gerrit) 2013/09/18 18:50:38 Actually, you're right in that you will construct
267 DCHECK(path); 268 DCHECK(path);
268 DCHECK(obs); 269 DCHECK(!callback.is_null());
269 DCHECK(CalledOnValidThread()); 270 DCHECK(CalledOnValidThread());
270 271
271 if (!GetProvider(std::string(path))) { 272 if (!GetProvider(std::string(path))) {
272 NOTREACHED() << "Trying to add an observer for an unregistered setting: " 273 NOTREACHED() << "Trying to add an observer for an unregistered setting: "
273 << path; 274 << path;
274 return; 275 return scoped_ptr<CrosSettings::ObserverSubscription>();
275 } 276 }
276 277
277 // Get the settings observer list associated with the path. 278 // Get the callback registry associated with the path.
278 NotificationObserverList* observer_list = NULL; 279 base::CallbackRegistry<void>* registry = NULL;
279 SettingsObserverMap::iterator observer_iterator = 280 SettingsObserverMap::iterator observer_iterator =
280 settings_observers_.find(path); 281 settings_observers_.find(path);
281 if (observer_iterator == settings_observers_.end()) { 282 if (observer_iterator == settings_observers_.end()) {
282 observer_list = new NotificationObserverList; 283 registry = new base::CallbackRegistry<void>;
283 settings_observers_[path] = observer_list; 284 settings_observers_[path] = registry;
284 } else { 285 } else {
285 observer_list = observer_iterator->second; 286 registry = observer_iterator->second;
286 } 287 }
287 288
288 // Verify that this observer doesn't already exist. 289 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 } 290 }
312 291
313 CrosSettingsProvider* CrosSettings::GetProvider( 292 CrosSettingsProvider* CrosSettings::GetProvider(
314 const std::string& path) const { 293 const std::string& path) const {
315 for (size_t i = 0; i < providers_.size(); ++i) { 294 for (size_t i = 0; i < providers_.size(); ++i) {
316 if (providers_[i]->HandlesSetting(path)) 295 if (providers_[i]->HandlesSetting(path))
317 return providers_[i]; 296 return providers_[i];
318 } 297 }
319 return NULL; 298 return NULL;
320 } 299 }
321 300
322 void CrosSettings::FireObservers(const std::string& path) { 301 void CrosSettings::FireObservers(const std::string& path) {
323 DCHECK(CalledOnValidThread()); 302 DCHECK(CalledOnValidThread());
324 SettingsObserverMap::iterator observer_iterator = 303 SettingsObserverMap::iterator observer_iterator =
325 settings_observers_.find(path); 304 settings_observers_.find(path);
326 if (observer_iterator == settings_observers_.end()) 305 if (observer_iterator == settings_observers_.end())
327 return; 306 return;
328 307
329 NotificationObserverList::Iterator it(*(observer_iterator->second)); 308 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 } 309 }
337 310
338 ScopedTestCrosSettings::ScopedTestCrosSettings() { 311 ScopedTestCrosSettings::ScopedTestCrosSettings() {
339 CrosSettings::Initialize(); 312 CrosSettings::Initialize();
340 } 313 }
341 314
342 ScopedTestCrosSettings::~ScopedTestCrosSettings() { 315 ScopedTestCrosSettings::~ScopedTestCrosSettings() {
343 CrosSettings::Shutdown(); 316 CrosSettings::Shutdown();
344 } 317 }
345 318
346 } // namespace chromeos 319 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698