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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 Internal(); | 58 Internal(); |
59 | 59 |
60 // Update the value, either by calling |UpdateValueInternal| directly | 60 // Update the value, either by calling |UpdateValueInternal| directly |
61 // or by dispatching to the right thread. | 61 // or by dispatching to the right thread. |
62 // Takes ownership of |value|. | 62 // Takes ownership of |value|. |
63 void UpdateValue(base::Value* value, | 63 void UpdateValue(base::Value* value, |
64 bool is_managed, | 64 bool is_managed, |
65 bool is_user_modifiable, | 65 bool is_user_modifiable, |
66 const base::Closure& callback) const; | 66 const base::Closure& callback) const; |
67 | 67 |
68 void MoveToThread( | 68 void UseAlternateTaskRunner( |
69 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); | 69 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); |
70 | 70 |
71 // See PrefMember<> for description. | 71 // See PrefMember<> for description. |
72 bool IsManaged() const { | 72 bool IsManaged() const { |
73 return is_managed_; | 73 return is_managed_; |
74 } | 74 } |
75 | 75 |
76 bool IsUserModifiable() const { | 76 bool IsUserModifiable() const { |
77 return is_user_modifiable_; | 77 return is_user_modifiable_; |
78 } | 78 } |
79 | 79 |
80 protected: | 80 protected: |
81 friend class base::RefCountedThreadSafe<Internal>; | 81 friend class base::RefCountedThreadSafe<Internal>; |
82 virtual ~Internal(); | 82 virtual ~Internal(); |
83 | 83 |
84 void CheckOnCorrectThread() const { | 84 void CheckOnCorrectThread() const { |
85 DCHECK(IsOnCorrectThread()); | 85 DCHECK(IsOnCorrectThread()); |
86 } | 86 } |
87 | 87 |
88 private: | 88 private: |
89 // This method actually updates the value. It should only be called from | 89 // This method actually updates the value. It should only be called from |
90 // the thread the PrefMember is on. | 90 // the thread the PrefMember is on. |
91 virtual bool UpdateValueInternal(const base::Value& value) const = 0; | 91 virtual bool UpdateValueInternal(const base::Value& value) const = 0; |
92 | 92 |
93 bool IsOnCorrectThread() const; | 93 bool IsOnCorrectThread() const; |
94 | 94 |
95 scoped_refptr<base::SingleThreadTaskRunner> thread_loop_; | 95 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
96 mutable bool is_managed_; | 96 mutable bool is_managed_; |
97 mutable bool is_user_modifiable_; | 97 mutable bool is_user_modifiable_; |
98 | 98 |
99 DISALLOW_COPY_AND_ASSIGN(Internal); | 99 DISALLOW_COPY_AND_ASSIGN(Internal); |
100 }; | 100 }; |
101 | 101 |
102 PrefMemberBase(); | 102 PrefMemberBase(); |
103 virtual ~PrefMemberBase(); | 103 virtual ~PrefMemberBase(); |
104 | 104 |
105 // See PrefMember<> for description. | 105 // See PrefMember<> for description. |
106 void Init(const char* pref_name, PrefService* prefs, | 106 void Init(const char* pref_name, PrefService* prefs, |
107 const NamedChangeCallback& observer); | 107 const NamedChangeCallback& observer); |
108 void Init(const char* pref_name, PrefService* prefs); | 108 void Init(const char* pref_name, PrefService* prefs); |
109 | 109 |
110 virtual void CreateInternal() const = 0; | 110 virtual void CreateInternal() const = 0; |
111 | 111 |
112 // See PrefMember<> for description. | 112 // See PrefMember<> for description. |
113 void Destroy(); | 113 void Destroy(); |
114 | 114 |
115 void MoveToThread( | 115 void UseAlternateTaskRunner( |
116 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); | 116 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); |
117 | 117 |
118 // PrefObserver | 118 // PrefObserver |
119 virtual void OnPreferenceChanged(PrefService* service, | 119 virtual void OnPreferenceChanged(PrefService* service, |
120 const std::string& pref_name) override; | 120 const std::string& pref_name) override; |
121 | 121 |
122 void VerifyValuePrefName() const { | 122 void VerifyValuePrefName() const { |
123 DCHECK(!pref_name_.empty()); | 123 DCHECK(!pref_name_.empty()); |
124 } | 124 } |
125 | 125 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 subtle::PrefMemberBase::Init( | 178 subtle::PrefMemberBase::Init( |
179 pref_name, prefs, | 179 pref_name, prefs, |
180 base::Bind(&PrefMemberBase::InvokeUnnamedCallback, observer)); | 180 base::Bind(&PrefMemberBase::InvokeUnnamedCallback, observer)); |
181 } | 181 } |
182 void Init(const char* pref_name, PrefService* prefs) { | 182 void Init(const char* pref_name, PrefService* prefs) { |
183 subtle::PrefMemberBase::Init(pref_name, prefs); | 183 subtle::PrefMemberBase::Init(pref_name, prefs); |
184 } | 184 } |
185 | 185 |
186 // Unsubscribes the PrefMember from the PrefService. After calling this | 186 // Unsubscribes the PrefMember from the PrefService. After calling this |
187 // function, the PrefMember may not be used any more on the UI thread. | 187 // function, the PrefMember may not be used any more on the UI thread. |
188 // Assuming |MoveToThread| was previously called, |GetValue|, |IsManaged|, | 188 // Assuming |UseAlternateTaskRunner| was previously called, |GetValue|, |
189 // and |IsUserModifiable| can still be called from the other thread but | 189 // |IsManaged|, and |IsUserModifiable| can still be called from the other |
190 // the results will no longer update from the PrefService. | 190 // thread but the results will no longer update from the PrefService. |
191 // This method should only be called on the UI thread. | 191 // This method should only be called on the UI thread. |
192 void Destroy() { | 192 void Destroy() { |
193 subtle::PrefMemberBase::Destroy(); | 193 subtle::PrefMemberBase::Destroy(); |
194 } | 194 } |
195 | 195 |
196 // Moves the PrefMember to another thread, allowing read accesses from there. | 196 // Moves the PrefMember to another thread, allowing read accesses from there. |
197 // Changes from the PrefService will be propagated asynchronously | 197 // Changes from the PrefService will be propagated asynchronously |
198 // via PostTask. | 198 // via PostTask. |
199 // This method should only be used from the thread the PrefMember is currently | 199 // This method should only be used from the thread the PrefMember is currently |
200 // on, which is the UI thread by default. | 200 // on, which is the UI thread by default. |
201 void MoveToThread( | 201 void UseAlternateTaskRunner( |
202 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { | 202 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { |
203 subtle::PrefMemberBase::MoveToThread(task_runner); | 203 subtle::PrefMemberBase::UseAlternateTaskRunner(task_runner); |
204 } | 204 } |
205 | 205 |
206 // Check whether the pref is managed, i.e. controlled externally through | 206 // Check whether the pref is managed, i.e. controlled externally through |
207 // enterprise configuration management (e.g. windows group policy). Returns | 207 // enterprise configuration management (e.g. windows group policy). Returns |
208 // false for unknown prefs. | 208 // false for unknown prefs. |
209 // This method should only be used from the thread the PrefMember is currently | 209 // This method should only be used from the thread the PrefMember is currently |
210 // on, which is the UI thread unless changed by |MoveToThread|. | 210 // on, which is the UI thread unless changed by |UseAlternateTaskRunner|. |
211 bool IsManaged() const { | 211 bool IsManaged() const { |
212 VerifyPref(); | 212 VerifyPref(); |
213 return internal_->IsManaged(); | 213 return internal_->IsManaged(); |
214 } | 214 } |
215 | 215 |
216 // Checks whether the pref can be modified by the user. This returns false | 216 // Checks whether the pref can be modified by the user. This returns false |
217 // when the pref is managed by a policy or an extension, and when a command | 217 // when the pref is managed by a policy or an extension, and when a command |
218 // line flag overrides the pref. | 218 // line flag overrides the pref. |
219 // This method should only be used from the thread the PrefMember is currently | 219 // This method should only be used from the thread the PrefMember is currently |
220 // on, which is the UI thread unless changed by |MoveToThread|. | 220 // on, which is the UI thread unless changed by |UseAlternateTaskRunner|. |
221 bool IsUserModifiable() const { | 221 bool IsUserModifiable() const { |
222 VerifyPref(); | 222 VerifyPref(); |
223 return internal_->IsUserModifiable(); | 223 return internal_->IsUserModifiable(); |
224 } | 224 } |
225 | 225 |
226 // Retrieve the value of the member variable. | 226 // Retrieve the value of the member variable. |
227 // This method should only be used from the thread the PrefMember is currently | 227 // This method should only be used from the thread the PrefMember is currently |
228 // on, which is the UI thread unless changed by |MoveToThread|. | 228 // on, which is the UI thread unless changed by |UseAlternateTaskRunner|. |
229 ValueType GetValue() const { | 229 ValueType GetValue() const { |
230 VerifyPref(); | 230 VerifyPref(); |
231 return internal_->value(); | 231 return internal_->value(); |
232 } | 232 } |
233 | 233 |
234 // Provided as a convenience. | 234 // Provided as a convenience. |
235 ValueType operator*() const { | 235 ValueType operator*() const { |
236 return GetValue(); | 236 return GetValue(); |
237 } | 237 } |
238 | 238 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 | 345 |
346 typedef PrefMember<bool> BooleanPrefMember; | 346 typedef PrefMember<bool> BooleanPrefMember; |
347 typedef PrefMember<int> IntegerPrefMember; | 347 typedef PrefMember<int> IntegerPrefMember; |
348 typedef PrefMember<double> DoublePrefMember; | 348 typedef PrefMember<double> DoublePrefMember; |
349 typedef PrefMember<std::string> StringPrefMember; | 349 typedef PrefMember<std::string> StringPrefMember; |
350 typedef PrefMember<base::FilePath> FilePathPrefMember; | 350 typedef PrefMember<base::FilePath> FilePathPrefMember; |
351 // This preference member is expensive for large string arrays. | 351 // This preference member is expensive for large string arrays. |
352 typedef PrefMember<std::vector<std::string> > StringListPrefMember; | 352 typedef PrefMember<std::vector<std::string> > StringListPrefMember; |
353 | 353 |
354 #endif // BASE_PREFS_PREF_MEMBER_H_ | 354 #endif // BASE_PREFS_PREF_MEMBER_H_ |
OLD | NEW |