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 // 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 10 matching lines...) Expand all Loading... |
21 // notify MyClass of changes. Note that if you use SetValue(), the observer | 21 // notify MyClass of changes. Note that if you use SetValue(), the observer |
22 // will not be notified. | 22 // will not be notified. |
23 | 23 |
24 #ifndef CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ | 24 #ifndef CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ |
25 #define CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ | 25 #define CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ |
26 | 26 |
27 #include <string> | 27 #include <string> |
28 #include <vector> | 28 #include <vector> |
29 | 29 |
30 #include "base/basictypes.h" | 30 #include "base/basictypes.h" |
| 31 #include "base/bind.h" |
| 32 #include "base/callback_forward.h" |
31 #include "base/file_path.h" | 33 #include "base/file_path.h" |
32 #include "base/logging.h" | 34 #include "base/logging.h" |
33 #include "base/memory/ref_counted.h" | 35 #include "base/memory/ref_counted.h" |
34 #include "base/message_loop_proxy.h" | 36 #include "base/message_loop_proxy.h" |
35 #include "base/prefs/public/pref_observer.h" | 37 #include "base/prefs/public/pref_observer.h" |
36 #include "base/values.h" | 38 #include "base/values.h" |
37 | 39 |
38 class PrefServiceBase; | 40 class PrefServiceBase; |
39 | 41 |
40 namespace subtle { | 42 namespace subtle { |
41 | 43 |
42 class PrefMemberBase : public PrefObserver { | 44 class PrefMemberBase : public PrefObserver { |
| 45 public: |
| 46 // Type of callback you can register if you need to know the name of |
| 47 // the pref that is changing. |
| 48 typedef base::Callback<void(const std::string&)> NamedChangeCallback; |
| 49 |
43 protected: | 50 protected: |
44 class Internal : public base::RefCountedThreadSafe<Internal> { | 51 class Internal : public base::RefCountedThreadSafe<Internal> { |
45 public: | 52 public: |
46 Internal(); | 53 Internal(); |
47 | 54 |
48 // Update the value, either by calling |UpdateValueInternal| directly | 55 // Update the value, either by calling |UpdateValueInternal| directly |
49 // or by dispatching to the right thread. | 56 // or by dispatching to the right thread. |
50 // Takes ownership of |value|. | 57 // Takes ownership of |value|. |
51 void UpdateValue(base::Value* value, | 58 void UpdateValue(base::Value* value, |
52 bool is_managed, | 59 bool is_managed, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 mutable bool is_user_modifiable_; | 91 mutable bool is_user_modifiable_; |
85 | 92 |
86 DISALLOW_COPY_AND_ASSIGN(Internal); | 93 DISALLOW_COPY_AND_ASSIGN(Internal); |
87 }; | 94 }; |
88 | 95 |
89 PrefMemberBase(); | 96 PrefMemberBase(); |
90 virtual ~PrefMemberBase(); | 97 virtual ~PrefMemberBase(); |
91 | 98 |
92 // See PrefMember<> for description. | 99 // See PrefMember<> for description. |
93 void Init(const char* pref_name, PrefServiceBase* prefs, | 100 void Init(const char* pref_name, PrefServiceBase* prefs, |
94 PrefObserver* observer); | 101 const NamedChangeCallback& observer); |
95 | 102 |
96 virtual void CreateInternal() const = 0; | 103 virtual void CreateInternal() const = 0; |
97 | 104 |
98 // See PrefMember<> for description. | 105 // See PrefMember<> for description. |
99 void Destroy(); | 106 void Destroy(); |
100 | 107 |
101 void MoveToThread(const scoped_refptr<base::MessageLoopProxy>& message_loop); | 108 void MoveToThread(const scoped_refptr<base::MessageLoopProxy>& message_loop); |
102 | 109 |
103 // PrefObserver | 110 // PrefObserver |
104 virtual void OnPreferenceChanged(PrefServiceBase* service, | 111 virtual void OnPreferenceChanged(PrefServiceBase* service, |
(...skipping 11 matching lines...) Expand all Loading... |
116 // Verifies the preference name, and lazily loads the preference value if | 123 // Verifies the preference name, and lazily loads the preference value if |
117 // it hasn't been loaded yet. | 124 // it hasn't been loaded yet. |
118 void VerifyPref() const; | 125 void VerifyPref() const; |
119 | 126 |
120 const std::string& pref_name() const { return pref_name_; } | 127 const std::string& pref_name() const { return pref_name_; } |
121 PrefServiceBase* prefs() { return prefs_; } | 128 PrefServiceBase* prefs() { return prefs_; } |
122 const PrefServiceBase* prefs() const { return prefs_; } | 129 const PrefServiceBase* prefs() const { return prefs_; } |
123 | 130 |
124 virtual Internal* internal() const = 0; | 131 virtual Internal* internal() const = 0; |
125 | 132 |
| 133 // Used to allow registering plain base::Closure callbacks. |
| 134 static void InvokeUnnamedCallback(const base::Closure& callback, |
| 135 const std::string& pref_name); |
| 136 |
126 private: | 137 private: |
127 // Ordered the members to compact the class instance. | 138 // Ordered the members to compact the class instance. |
128 std::string pref_name_; | 139 std::string pref_name_; |
129 PrefObserver* observer_; | 140 NamedChangeCallback observer_; |
130 PrefServiceBase* prefs_; | 141 PrefServiceBase* prefs_; |
131 | 142 |
132 protected: | 143 protected: |
133 bool setting_value_; | 144 bool setting_value_; |
134 }; | 145 }; |
135 | 146 |
136 // This function implements StringListPrefMember::UpdateValue(). | 147 // This function implements StringListPrefMember::UpdateValue(). |
137 // It is exposed here for testing purposes. | 148 // It is exposed here for testing purposes. |
138 bool PrefMemberVectorStringUpdate(const Value& value, | 149 bool PrefMemberVectorStringUpdate(const Value& value, |
139 std::vector<std::string>* string_vector); | 150 std::vector<std::string>* string_vector); |
140 | 151 |
141 } // namespace subtle | 152 } // namespace subtle |
142 | 153 |
143 template <typename ValueType> | 154 template <typename ValueType> |
144 class PrefMember : public subtle::PrefMemberBase { | 155 class PrefMember : public subtle::PrefMemberBase { |
145 public: | 156 public: |
146 // Defer initialization to an Init method so it's easy to make this class be | 157 // Defer initialization to an Init method so it's easy to make this class be |
147 // a member variable. | 158 // a member variable. |
148 PrefMember() {} | 159 PrefMember() {} |
149 virtual ~PrefMember() {} | 160 virtual ~PrefMember() {} |
150 | 161 |
151 // Do the actual initialization of the class. |observer| may be null if you | 162 // Do the actual initialization of the class. Use the two-parameter |
152 // don't want any notifications of changes. | 163 // version if you don't want any notifications of changes. This |
153 // This method should only be called on the UI thread. | 164 // method should only be called on the UI thread. |
| 165 void Init(const char* pref_name, PrefServiceBase* prefs, |
| 166 const NamedChangeCallback& observer) { |
| 167 subtle::PrefMemberBase::Init(pref_name, prefs, observer); |
| 168 } |
| 169 void Init(const char* pref_name, PrefServiceBase* prefs, |
| 170 const base::Closure& observer) { |
| 171 subtle::PrefMemberBase::Init( |
| 172 pref_name, prefs, |
| 173 base::Bind(&PrefMemberBase::InvokeUnnamedCallback, observer)); |
| 174 } |
| 175 void Init(const char* pref_name, PrefServiceBase* prefs) { |
| 176 subtle::PrefMemberBase::Init(pref_name, prefs); |
| 177 } |
| 178 |
| 179 // Deprecated version of Init. |
154 void Init(const char* pref_name, PrefServiceBase* prefs, | 180 void Init(const char* pref_name, PrefServiceBase* prefs, |
155 PrefObserver* observer) { | 181 PrefObserver* observer) { |
156 subtle::PrefMemberBase::Init(pref_name, prefs, observer); | 182 if (observer) { |
| 183 Init(pref_name, prefs, base::Bind(&PrefObserver::OnPreferenceChanged, |
| 184 base::Unretained(observer), prefs)); |
| 185 } else { |
| 186 Init(pref_name, prefs, NamedChangeCallback()); |
| 187 } |
157 } | 188 } |
158 | 189 |
159 // Unsubscribes the PrefMember from the PrefService. After calling this | 190 // Unsubscribes the PrefMember from the PrefService. After calling this |
160 // function, the PrefMember may not be used any more on the UI thread. | 191 // function, the PrefMember may not be used any more on the UI thread. |
161 // Assuming |MoveToThread| was previously called, |GetValue|, |IsManaged|, | 192 // Assuming |MoveToThread| was previously called, |GetValue|, |IsManaged|, |
162 // and |IsUserModifiable| can still be called from the other thread but | 193 // and |IsUserModifiable| can still be called from the other thread but |
163 // the results will no longer update from the PrefService. | 194 // the results will no longer update from the PrefService. |
164 // This method should only be called on the UI thread. | 195 // This method should only be called on the UI thread. |
165 void Destroy() { | 196 void Destroy() { |
166 subtle::PrefMemberBase::Destroy(); | 197 subtle::PrefMemberBase::Destroy(); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 | 290 |
260 typedef PrefMember<bool> BooleanPrefMember; | 291 typedef PrefMember<bool> BooleanPrefMember; |
261 typedef PrefMember<int> IntegerPrefMember; | 292 typedef PrefMember<int> IntegerPrefMember; |
262 typedef PrefMember<double> DoublePrefMember; | 293 typedef PrefMember<double> DoublePrefMember; |
263 typedef PrefMember<std::string> StringPrefMember; | 294 typedef PrefMember<std::string> StringPrefMember; |
264 typedef PrefMember<FilePath> FilePathPrefMember; | 295 typedef PrefMember<FilePath> FilePathPrefMember; |
265 // This preference member is expensive for large string arrays. | 296 // This preference member is expensive for large string arrays. |
266 typedef PrefMember<std::vector<std::string> > StringListPrefMember; | 297 typedef PrefMember<std::vector<std::string> > StringListPrefMember; |
267 | 298 |
268 #endif // CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ | 299 #endif // CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ |
OLD | NEW |