OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // A helper class that stays in sync with a preference (bool, int, real, | 5 // A helper class that stays in sync with a preference (bool, int, real, |
6 // string or filepath). For example: | 6 // string or filepath). For example: |
7 // | 7 // |
8 // class MyClass { | 8 // class MyClass { |
9 // public: | 9 // public: |
10 // MyClass(PrefService* prefs) { | 10 // MyClass(PrefService* prefs) { |
(...skipping 15 matching lines...) Expand all Loading... |
26 #pragma once | 26 #pragma once |
27 | 27 |
28 #include <string> | 28 #include <string> |
29 | 29 |
30 #include "base/basictypes.h" | 30 #include "base/basictypes.h" |
31 #include "base/file_path.h" | 31 #include "base/file_path.h" |
32 #include "base/memory/ref_counted.h" | 32 #include "base/memory/ref_counted.h" |
33 #include "base/values.h" | 33 #include "base/values.h" |
34 #include "content/browser/browser_thread.h" | 34 #include "content/browser/browser_thread.h" |
35 #include "content/common/notification_observer.h" | 35 #include "content/common/notification_observer.h" |
| 36 #include "content/common/notification_registrar.h" |
36 | 37 |
37 class PrefService; | 38 class PrefService; |
| 39 class Profile; |
38 | 40 |
39 namespace subtle { | 41 namespace subtle { |
40 | 42 |
41 class PrefMemberBase : public NotificationObserver { | 43 class PrefMemberBase : public NotificationObserver { |
42 protected: | 44 protected: |
43 class Internal : public base::RefCountedThreadSafe<Internal> { | 45 class Internal : public base::RefCountedThreadSafe<Internal> { |
44 public: | 46 public: |
45 Internal(); | 47 Internal(); |
46 | 48 |
47 // Update the value, either by calling |UpdateValueInternal| directly | 49 // Update the value, either by calling |UpdateValueInternal| directly |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 | 81 |
80 PrefMemberBase(); | 82 PrefMemberBase(); |
81 virtual ~PrefMemberBase(); | 83 virtual ~PrefMemberBase(); |
82 | 84 |
83 // See PrefMember<> for description. | 85 // See PrefMember<> for description. |
84 void Init(const char* pref_name, PrefService* prefs, | 86 void Init(const char* pref_name, PrefService* prefs, |
85 NotificationObserver* observer); | 87 NotificationObserver* observer); |
86 | 88 |
87 virtual void CreateInternal() const = 0; | 89 virtual void CreateInternal() const = 0; |
88 | 90 |
| 91 void ObserveProfileDestruction(Profile* profile); |
| 92 |
89 // See PrefMember<> for description. | 93 // See PrefMember<> for description. |
90 void Destroy(); | 94 void Destroy(); |
91 | 95 |
92 void MoveToThread(BrowserThread::ID thread_id); | 96 void MoveToThread(BrowserThread::ID thread_id); |
93 | 97 |
94 // NotificationObserver | 98 // NotificationObserver |
95 virtual void Observe(NotificationType type, | 99 virtual void Observe(NotificationType type, |
96 const NotificationSource& source, | 100 const NotificationSource& source, |
97 const NotificationDetails& details); | 101 const NotificationDetails& details); |
98 | 102 |
99 void VerifyValuePrefName() const; | 103 void VerifyValuePrefName() const { |
| 104 DCHECK(!pref_name_.empty()); |
| 105 } |
100 | 106 |
101 // This method is used to do the actual sync with the preference. | 107 // This method is used to do the actual sync with the preference. |
102 // Note: it is logically const, because it doesn't modify the state | 108 // Note: it is logically const, because it doesn't modify the state |
103 // seen by the outside world. It is just doing a lazy load behind the scenes. | 109 // seen by the outside world. It is just doing a lazy load behind the scenes. |
104 virtual void UpdateValueFromPref() const; | 110 virtual void UpdateValueFromPref() const; |
105 | 111 |
106 // Verifies the preference name, and lazily loads the preference value if | 112 // Verifies the preference name, and lazily loads the preference value if |
107 // it hasn't been loaded yet. | 113 // it hasn't been loaded yet. |
108 void VerifyPref() const { | 114 void VerifyPref() const; |
109 VerifyValuePrefName(); | |
110 if (!internal()) | |
111 UpdateValueFromPref(); | |
112 } | |
113 | 115 |
114 const std::string& pref_name() const { return pref_name_; } | 116 const std::string& pref_name() const { return pref_name_; } |
115 PrefService* prefs() { return prefs_; } | 117 PrefService* prefs() { return prefs_; } |
116 const PrefService* prefs() const { return prefs_; } | 118 const PrefService* prefs() const { return prefs_; } |
117 | 119 |
118 virtual Internal* internal() const = 0; | 120 virtual Internal* internal() const = 0; |
119 | 121 |
120 // Ordered the members to compact the class instance. | 122 // Ordered the members to compact the class instance. |
121 private: | 123 private: |
122 std::string pref_name_; | 124 std::string pref_name_; |
| 125 NotificationRegistrar registrar_; |
123 NotificationObserver* observer_; | 126 NotificationObserver* observer_; |
124 PrefService* prefs_; | 127 PrefService* prefs_; |
125 | 128 |
126 protected: | 129 protected: |
127 bool setting_value_; | 130 bool setting_value_; |
128 }; | 131 }; |
129 | 132 |
130 } // namespace subtle | 133 } // namespace subtle |
131 | 134 |
132 template <typename ValueType> | 135 template <typename ValueType> |
133 class PrefMember : public subtle::PrefMemberBase { | 136 class PrefMember : public subtle::PrefMemberBase { |
134 public: | 137 public: |
135 // Defer initialization to an Init method so it's easy to make this class be | 138 // Defer initialization to an Init method so it's easy to make this class be |
136 // a member variable. | 139 // a member variable. |
137 PrefMember() {} | 140 PrefMember() {} |
138 virtual ~PrefMember() {} | 141 virtual ~PrefMember() {} |
139 | 142 |
140 // Do the actual initialization of the class. |observer| may be null if you | 143 // Do the actual initialization of the class. |observer| may be null if you |
141 // don't want any notifications of changes. | 144 // don't want any notifications of changes. |
142 // This method should only be called on the UI thread. | 145 // This method should only be called on the UI thread. |
143 void Init(const char* pref_name, PrefService* prefs, | 146 void Init(const char* pref_name, PrefService* prefs, |
144 NotificationObserver* observer) { | 147 NotificationObserver* observer) { |
145 subtle::PrefMemberBase::Init(pref_name, prefs, observer); | 148 subtle::PrefMemberBase::Init(pref_name, prefs, observer); |
146 } | 149 } |
147 | 150 |
| 151 // Starts observing PROFILE_DESTROYED notifications for the given |profile|, |
| 152 // to automatically unsubscribe from the PrefService. |
| 153 void ObserveProfileDestruction(Profile* profile) { |
| 154 subtle::PrefMemberBase::ObserveProfileDestruction(profile); |
| 155 } |
| 156 |
148 // Unsubscribes the PrefMember from the PrefService. After calling this | 157 // Unsubscribes the PrefMember from the PrefService. After calling this |
149 // function, the PrefMember may not be used any more. | 158 // function, the PrefMember may not be used any more. |
150 // This method should only be called on the UI thread. | 159 // This method should only be called on the UI thread. |
151 void Destroy() { | 160 void Destroy() { |
152 subtle::PrefMemberBase::Destroy(); | 161 subtle::PrefMemberBase::Destroy(); |
153 } | 162 } |
154 | 163 |
155 // Moves the PrefMember to another thread, allowing read accesses from there. | 164 // Moves the PrefMember to another thread, allowing read accesses from there. |
156 // Changes from the PrefService will be propagated asynchronously | 165 // Changes from the PrefService will be propagated asynchronously |
157 // via PostTask. | 166 // via PostTask. |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 DISALLOW_COPY_AND_ASSIGN(PrefMember); | 250 DISALLOW_COPY_AND_ASSIGN(PrefMember); |
242 }; | 251 }; |
243 | 252 |
244 typedef PrefMember<bool> BooleanPrefMember; | 253 typedef PrefMember<bool> BooleanPrefMember; |
245 typedef PrefMember<int> IntegerPrefMember; | 254 typedef PrefMember<int> IntegerPrefMember; |
246 typedef PrefMember<double> DoublePrefMember; | 255 typedef PrefMember<double> DoublePrefMember; |
247 typedef PrefMember<std::string> StringPrefMember; | 256 typedef PrefMember<std::string> StringPrefMember; |
248 typedef PrefMember<FilePath> FilePathPrefMember; | 257 typedef PrefMember<FilePath> FilePathPrefMember; |
249 | 258 |
250 #endif // CHROME_BROWSER_PREFS_PREF_MEMBER_H_ | 259 #endif // CHROME_BROWSER_PREFS_PREF_MEMBER_H_ |
OLD | NEW |