OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef CHROME_BROWSER_PREFS_PREF_CHANGE_REGISTRAR_H_ | 5 #ifndef CHROME_BROWSER_PREFS_PREF_CHANGE_REGISTRAR_H_ |
6 #define CHROME_BROWSER_PREFS_PREF_CHANGE_REGISTRAR_H_ | 6 #define CHROME_BROWSER_PREFS_PREF_CHANGE_REGISTRAR_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 | 13 |
14 class PrefService; | 14 class PrefService; |
15 class NotificationObserver; | 15 class NotificationObserver; |
16 | 16 |
17 // Automatically manages the registration of one or more pref change observers | 17 // Automatically manages the registration of one or more pref change observers |
18 // with a PrefStore. Functions much like NotificationRegistrar, but specifically | 18 // with a PrefStore. Functions much like NotificationRegistrar, but specifically |
19 // manages observers of preference changes. When the Registrar is destroyed, | 19 // manages observers of preference changes. When the Registrar is destroyed, |
20 // all registered observers are automatically unregistered with the PrefStore. | 20 // all registered observers are automatically unregistered with the PrefStore. |
21 class PrefChangeRegistrar { | 21 class PrefChangeRegistrar { |
22 public: | 22 public: |
23 PrefChangeRegistrar(); | 23 PrefChangeRegistrar(); |
24 virtual ~PrefChangeRegistrar(); | 24 virtual ~PrefChangeRegistrar(); |
25 | 25 |
26 // Must be called before adding or removing observers. | 26 // Must be called before adding or removing observers. Can be called more |
| 27 // than once as long as the value of |service| doesn't change. |
27 void Init(PrefService* service); | 28 void Init(PrefService* service); |
28 | 29 |
29 // Adds an pref observer for the specified pref |path| and |obs| observer | 30 // Adds an pref observer for the specified pref |path| and |obs| observer |
30 // object. All registered observers will be automatically unregistered | 31 // object. All registered observers will be automatically unregistered |
31 // when the registrar's destructor is called unless the observer has been | 32 // when the registrar's destructor is called unless the observer has been |
32 // explicitly removed by a call to Remove beforehand. | 33 // explicitly removed by a call to Remove beforehand. |
33 void Add(const char* path, | 34 void Add(const char* path, |
34 NotificationObserver* obs); | 35 NotificationObserver* obs); |
35 | 36 |
36 // Removes a preference observer that has previously been added with a call to | 37 // Removes a preference observer that has previously been added with a call to |
37 // Add. | 38 // Add. |
38 void Remove(const char* path, | 39 void Remove(const char* path, |
39 NotificationObserver* obs); | 40 NotificationObserver* obs); |
40 | 41 |
| 42 // Removes all observers that have been previously added with a call to Add. |
| 43 void RemoveAll(); |
| 44 |
| 45 // Returns true if no pref observers are registered. |
| 46 bool IsEmpty() const; |
| 47 |
41 private: | 48 private: |
42 typedef std::pair<std::string, NotificationObserver*> ObserverRegistration; | 49 typedef std::pair<std::string, NotificationObserver*> ObserverRegistration; |
43 | 50 |
44 std::set<ObserverRegistration> observers_; | 51 std::set<ObserverRegistration> observers_; |
45 PrefService* service_; | 52 PrefService* service_; |
46 | 53 |
47 DISALLOW_COPY_AND_ASSIGN(PrefChangeRegistrar); | 54 DISALLOW_COPY_AND_ASSIGN(PrefChangeRegistrar); |
48 }; | 55 }; |
49 | 56 |
50 #endif // CHROME_BROWSER_PREFS_PREF_CHANGE_REGISTRAR_H_ | 57 #endif // CHROME_BROWSER_PREFS_PREF_CHANGE_REGISTRAR_H_ |
OLD | NEW |