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); |
102 void Init(const char* pref_name, PrefServiceBase* prefs); | |
95 | 103 |
96 virtual void CreateInternal() const = 0; | 104 virtual void CreateInternal() const = 0; |
97 | 105 |
98 // See PrefMember<> for description. | 106 // See PrefMember<> for description. |
99 void Destroy(); | 107 void Destroy(); |
100 | 108 |
101 void MoveToThread(const scoped_refptr<base::MessageLoopProxy>& message_loop); | 109 void MoveToThread(const scoped_refptr<base::MessageLoopProxy>& message_loop); |
102 | 110 |
103 // PrefObserver | 111 // PrefObserver |
104 virtual void OnPreferenceChanged(PrefServiceBase* service, | 112 virtual void OnPreferenceChanged(PrefServiceBase* service, |
(...skipping 12 matching lines...) Expand all Loading... | |
117 // it hasn't been loaded yet. | 125 // it hasn't been loaded yet. |
118 void VerifyPref() const; | 126 void VerifyPref() const; |
119 | 127 |
120 const std::string& pref_name() const { return pref_name_; } | 128 const std::string& pref_name() const { return pref_name_; } |
121 PrefServiceBase* prefs() { return prefs_; } | 129 PrefServiceBase* prefs() { return prefs_; } |
122 const PrefServiceBase* prefs() const { return prefs_; } | 130 const PrefServiceBase* prefs() const { return prefs_; } |
123 | 131 |
124 virtual Internal* internal() const = 0; | 132 virtual Internal* internal() const = 0; |
125 | 133 |
126 private: | 134 private: |
135 // Used to allow registering plain base::Closure callbacks. | |
136 static void InvokeUnnamedCallback(const base::Closure& callback, | |
137 const std::string& pref_name); | |
138 | |
127 // Ordered the members to compact the class instance. | 139 // Ordered the members to compact the class instance. |
128 std::string pref_name_; | 140 std::string pref_name_; |
129 PrefObserver* observer_; | 141 scoped_ptr<NamedChangeCallback> observer_; |
Mattias Nissler (ping if slow)
2012/11/09 14:47:50
Other code uses base::Callback::is_null() for dete
| |
130 PrefServiceBase* prefs_; | 142 PrefServiceBase* prefs_; |
131 | 143 |
132 protected: | 144 protected: |
133 bool setting_value_; | 145 bool setting_value_; |
134 }; | 146 }; |
135 | 147 |
136 // This function implements StringListPrefMember::UpdateValue(). | 148 // This function implements StringListPrefMember::UpdateValue(). |
137 // It is exposed here for testing purposes. | 149 // It is exposed here for testing purposes. |
138 bool PrefMemberVectorStringUpdate(const Value& value, | 150 bool PrefMemberVectorStringUpdate(const Value& value, |
139 std::vector<std::string>* string_vector); | 151 std::vector<std::string>* string_vector); |
140 | 152 |
141 } // namespace subtle | 153 } // namespace subtle |
142 | 154 |
143 template <typename ValueType> | 155 template <typename ValueType> |
144 class PrefMember : public subtle::PrefMemberBase { | 156 class PrefMember : public subtle::PrefMemberBase { |
145 public: | 157 public: |
146 // Defer initialization to an Init method so it's easy to make this class be | 158 // Defer initialization to an Init method so it's easy to make this class be |
147 // a member variable. | 159 // a member variable. |
148 PrefMember() {} | 160 PrefMember() {} |
149 virtual ~PrefMember() {} | 161 virtual ~PrefMember() {} |
150 | 162 |
151 // Do the actual initialization of the class. |observer| may be null if you | 163 // Do the actual initialization of the class. Use the two-parameter |
152 // don't want any notifications of changes. | 164 // version if you don't want any notifications of changes. This |
153 // This method should only be called on the UI thread. | 165 // method should only be called on the UI thread. |
166 void Init(const char* pref_name, PrefServiceBase* prefs, | |
167 const NamedChangeCallback& observer) { | |
168 subtle::PrefMemberBase::Init(pref_name, prefs, observer); | |
169 } | |
170 void Init(const char* pref_name, PrefServiceBase* prefs, | |
171 const base::Closure& observer) { | |
172 subtle::PrefMemberBase::Init( | |
173 pref_name, prefs, | |
174 base::Bind(&PrefMemberBase::InvokeUnnamedCallback, observer)); | |
175 } | |
176 void Init(const char* pref_name, PrefServiceBase* prefs) { | |
177 subtle::PrefMemberBase::Init(pref_name, prefs); | |
178 } | |
179 | |
180 // Deprecated version of Init. | |
154 void Init(const char* pref_name, PrefServiceBase* prefs, | 181 void Init(const char* pref_name, PrefServiceBase* prefs, |
155 PrefObserver* observer) { | 182 PrefObserver* observer) { |
156 subtle::PrefMemberBase::Init(pref_name, prefs, observer); | 183 if (observer) { |
184 Init(pref_name, prefs, base::Bind(&PrefObserver::OnPreferenceChanged, | |
185 base::Unretained(observer), prefs)); | |
186 } else { | |
187 Init(pref_name, prefs); | |
188 } | |
157 } | 189 } |
158 | 190 |
159 // Unsubscribes the PrefMember from the PrefService. After calling this | 191 // Unsubscribes the PrefMember from the PrefService. After calling this |
160 // function, the PrefMember may not be used any more on the UI thread. | 192 // function, the PrefMember may not be used any more on the UI thread. |
161 // Assuming |MoveToThread| was previously called, |GetValue|, |IsManaged|, | 193 // Assuming |MoveToThread| was previously called, |GetValue|, |IsManaged|, |
162 // and |IsUserModifiable| can still be called from the other thread but | 194 // and |IsUserModifiable| can still be called from the other thread but |
163 // the results will no longer update from the PrefService. | 195 // the results will no longer update from the PrefService. |
164 // This method should only be called on the UI thread. | 196 // This method should only be called on the UI thread. |
165 void Destroy() { | 197 void Destroy() { |
166 subtle::PrefMemberBase::Destroy(); | 198 subtle::PrefMemberBase::Destroy(); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
259 | 291 |
260 typedef PrefMember<bool> BooleanPrefMember; | 292 typedef PrefMember<bool> BooleanPrefMember; |
261 typedef PrefMember<int> IntegerPrefMember; | 293 typedef PrefMember<int> IntegerPrefMember; |
262 typedef PrefMember<double> DoublePrefMember; | 294 typedef PrefMember<double> DoublePrefMember; |
263 typedef PrefMember<std::string> StringPrefMember; | 295 typedef PrefMember<std::string> StringPrefMember; |
264 typedef PrefMember<FilePath> FilePathPrefMember; | 296 typedef PrefMember<FilePath> FilePathPrefMember; |
265 // This preference member is expensive for large string arrays. | 297 // This preference member is expensive for large string arrays. |
266 typedef PrefMember<std::vector<std::string> > StringListPrefMember; | 298 typedef PrefMember<std::vector<std::string> > StringListPrefMember; |
267 | 299 |
268 #endif // CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ | 300 #endif // CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ |
OLD | NEW |