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 { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 mutable bool is_user_modifiable_; | 86 mutable bool is_user_modifiable_; |
85 | 87 |
86 DISALLOW_COPY_AND_ASSIGN(Internal); | 88 DISALLOW_COPY_AND_ASSIGN(Internal); |
87 }; | 89 }; |
88 | 90 |
89 PrefMemberBase(); | 91 PrefMemberBase(); |
90 virtual ~PrefMemberBase(); | 92 virtual ~PrefMemberBase(); |
91 | 93 |
92 // See PrefMember<> for description. | 94 // See PrefMember<> for description. |
93 void Init(const char* pref_name, PrefServiceBase* prefs, | 95 void Init(const char* pref_name, PrefServiceBase* prefs, |
94 PrefObserver* observer); | 96 const base::Closure& observer); |
| 97 void Init(const char* pref_name, PrefServiceBase* prefs); |
95 | 98 |
96 virtual void CreateInternal() const = 0; | 99 virtual void CreateInternal() const = 0; |
97 | 100 |
98 // See PrefMember<> for description. | 101 // See PrefMember<> for description. |
99 void Destroy(); | 102 void Destroy(); |
100 | 103 |
101 void MoveToThread(const scoped_refptr<base::MessageLoopProxy>& message_loop); | 104 void MoveToThread(const scoped_refptr<base::MessageLoopProxy>& message_loop); |
102 | 105 |
103 // PrefObserver | 106 // PrefObserver |
104 virtual void OnPreferenceChanged(PrefServiceBase* service, | 107 virtual void OnPreferenceChanged(PrefServiceBase* service, |
(...skipping 14 matching lines...) Expand all Loading... |
119 | 122 |
120 const std::string& pref_name() const { return pref_name_; } | 123 const std::string& pref_name() const { return pref_name_; } |
121 PrefServiceBase* prefs() { return prefs_; } | 124 PrefServiceBase* prefs() { return prefs_; } |
122 const PrefServiceBase* prefs() const { return prefs_; } | 125 const PrefServiceBase* prefs() const { return prefs_; } |
123 | 126 |
124 virtual Internal* internal() const = 0; | 127 virtual Internal* internal() const = 0; |
125 | 128 |
126 private: | 129 private: |
127 // Ordered the members to compact the class instance. | 130 // Ordered the members to compact the class instance. |
128 std::string pref_name_; | 131 std::string pref_name_; |
129 PrefObserver* observer_; | 132 base::Closure observer_; // Initially bound to base::DoNothing. |
130 PrefServiceBase* prefs_; | 133 PrefServiceBase* prefs_; |
131 | 134 |
132 protected: | 135 protected: |
133 bool setting_value_; | 136 bool setting_value_; |
134 }; | 137 }; |
135 | 138 |
136 // This function implements StringListPrefMember::UpdateValue(). | 139 // This function implements StringListPrefMember::UpdateValue(). |
137 // It is exposed here for testing purposes. | 140 // It is exposed here for testing purposes. |
138 bool PrefMemberVectorStringUpdate(const Value& value, | 141 bool PrefMemberVectorStringUpdate(const Value& value, |
139 std::vector<std::string>* string_vector); | 142 std::vector<std::string>* string_vector); |
140 | 143 |
141 } // namespace subtle | 144 } // namespace subtle |
142 | 145 |
143 template <typename ValueType> | 146 template <typename ValueType> |
144 class PrefMember : public subtle::PrefMemberBase { | 147 class PrefMember : public subtle::PrefMemberBase { |
145 public: | 148 public: |
146 // Defer initialization to an Init method so it's easy to make this class be | 149 // Defer initialization to an Init method so it's easy to make this class be |
147 // a member variable. | 150 // a member variable. |
148 PrefMember() {} | 151 PrefMember() {} |
149 virtual ~PrefMember() {} | 152 virtual ~PrefMember() {} |
150 | 153 |
151 // Do the actual initialization of the class. |observer| may be null if you | 154 // Do the actual initialization of the class. Use the two-parameter |
152 // don't want any notifications of changes. | 155 // version if you don't want any notifications of changes. This |
153 // This method should only be called on the UI thread. | 156 // method should only be called on the UI thread. |
| 157 void Init(const char* pref_name, PrefServiceBase* prefs, |
| 158 const base::Closure& observer) { |
| 159 subtle::PrefMemberBase::Init(pref_name, prefs, observer); |
| 160 } |
| 161 |
| 162 void Init(const char* pref_name, PrefServiceBase* prefs) { |
| 163 subtle::PrefMemberBase::Init(pref_name, prefs); |
| 164 } |
| 165 |
| 166 // Deprecated version of Init. |
154 void Init(const char* pref_name, PrefServiceBase* prefs, | 167 void Init(const char* pref_name, PrefServiceBase* prefs, |
155 PrefObserver* observer) { | 168 PrefObserver* observer) { |
156 subtle::PrefMemberBase::Init(pref_name, prefs, observer); | 169 if (observer) { |
| 170 Init(pref_name, prefs, base::Bind(&PrefObserver::OnPreferenceChanged, |
| 171 base::Unretained(observer), |
| 172 prefs, std::string(pref_name))); |
| 173 } else { |
| 174 Init(pref_name, prefs); |
| 175 } |
157 } | 176 } |
158 | 177 |
159 // Unsubscribes the PrefMember from the PrefService. After calling this | 178 // Unsubscribes the PrefMember from the PrefService. After calling this |
160 // function, the PrefMember may not be used any more on the UI thread. | 179 // function, the PrefMember may not be used any more on the UI thread. |
161 // Assuming |MoveToThread| was previously called, |GetValue|, |IsManaged|, | 180 // Assuming |MoveToThread| was previously called, |GetValue|, |IsManaged|, |
162 // and |IsUserModifiable| can still be called from the other thread but | 181 // and |IsUserModifiable| can still be called from the other thread but |
163 // the results will no longer update from the PrefService. | 182 // the results will no longer update from the PrefService. |
164 // This method should only be called on the UI thread. | 183 // This method should only be called on the UI thread. |
165 void Destroy() { | 184 void Destroy() { |
166 subtle::PrefMemberBase::Destroy(); | 185 subtle::PrefMemberBase::Destroy(); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 | 278 |
260 typedef PrefMember<bool> BooleanPrefMember; | 279 typedef PrefMember<bool> BooleanPrefMember; |
261 typedef PrefMember<int> IntegerPrefMember; | 280 typedef PrefMember<int> IntegerPrefMember; |
262 typedef PrefMember<double> DoublePrefMember; | 281 typedef PrefMember<double> DoublePrefMember; |
263 typedef PrefMember<std::string> StringPrefMember; | 282 typedef PrefMember<std::string> StringPrefMember; |
264 typedef PrefMember<FilePath> FilePathPrefMember; | 283 typedef PrefMember<FilePath> FilePathPrefMember; |
265 // This preference member is expensive for large string arrays. | 284 // This preference member is expensive for large string arrays. |
266 typedef PrefMember<std::vector<std::string> > StringListPrefMember; | 285 typedef PrefMember<std::vector<std::string> > StringListPrefMember; |
267 | 286 |
268 #endif // CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ | 287 #endif // CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ |
OLD | NEW |