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