Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: base/prefs/pref_member.h

Issue 369703003: Reduce usage of MessageLoopProxy in base/ (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Explicit Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 15 matching lines...) Expand all
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" 31 #include "base/bind.h"
32 #include "base/callback_forward.h" 32 #include "base/callback_forward.h"
33 #include "base/files/file_path.h" 33 #include "base/files/file_path.h"
34 #include "base/logging.h" 34 #include "base/logging.h"
35 #include "base/memory/ref_counted.h" 35 #include "base/memory/ref_counted.h"
36 #include "base/message_loop/message_loop_proxy.h"
37 #include "base/prefs/base_prefs_export.h" 36 #include "base/prefs/base_prefs_export.h"
38 #include "base/prefs/pref_observer.h" 37 #include "base/prefs/pref_observer.h"
38 #include "base/sequenced_task_runner.h"
39 #include "base/values.h" 39 #include "base/values.h"
40 40
41 class PrefService; 41 class PrefService;
42 42
43 namespace subtle { 43 namespace subtle {
44 44
45 class BASE_PREFS_EXPORT PrefMemberBase : public PrefObserver { 45 class BASE_PREFS_EXPORT PrefMemberBase : public PrefObserver {
46 public: 46 public:
47 // Type of callback you can register if you need to know the name of 47 // Type of callback you can register if you need to know the name of
48 // the pref that is changing. 48 // the pref that is changing.
49 typedef base::Callback<void(const std::string&)> NamedChangeCallback; 49 typedef base::Callback<void(const std::string&)> NamedChangeCallback;
50 50
51 PrefService* prefs() { return prefs_; } 51 PrefService* prefs() { return prefs_; }
52 const PrefService* prefs() const { return prefs_; } 52 const PrefService* prefs() const { return prefs_; }
53 53
54 protected: 54 protected:
55 class BASE_PREFS_EXPORT Internal 55 class BASE_PREFS_EXPORT Internal
56 : public base::RefCountedThreadSafe<Internal> { 56 : public base::RefCountedThreadSafe<Internal> {
57 public: 57 public:
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 MoveToTaskRunner(
darin (slow to review) 2014/07/10 22:12:05 Hmm, MoveToThread seems like a better name. The co
Ryan Sleevi 2014/07/10 22:46:37 Correct, but with the caveat explained (below) tha
69 const scoped_refptr<base::MessageLoopProxy>& message_loop); 69 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
darin (slow to review) 2014/07/10 22:12:05 shouldn't this be a SingleThreadTaskRunner given t
Ryan Sleevi 2014/07/10 22:46:37 This is not actually coupled to a particular threa
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::MessageLoopProxy> thread_loop_; 95 scoped_refptr<base::SequencedTaskRunner> 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(const scoped_refptr<base::MessageLoopProxy>& message_loop); 115 void MoveToTaskRunner(
116 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
116 117
117 // PrefObserver 118 // PrefObserver
118 virtual void OnPreferenceChanged(PrefService* service, 119 virtual void OnPreferenceChanged(PrefService* service,
119 const std::string& pref_name) OVERRIDE; 120 const std::string& pref_name) OVERRIDE;
120 121
121 void VerifyValuePrefName() const { 122 void VerifyValuePrefName() const {
122 DCHECK(!pref_name_.empty()); 123 DCHECK(!pref_name_.empty());
123 } 124 }
124 125
125 // This method is used to do the actual sync with the preference. 126 // This method is used to do the actual sync with the preference.
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 subtle::PrefMemberBase::Init( 178 subtle::PrefMemberBase::Init(
178 pref_name, prefs, 179 pref_name, prefs,
179 base::Bind(&PrefMemberBase::InvokeUnnamedCallback, observer)); 180 base::Bind(&PrefMemberBase::InvokeUnnamedCallback, observer));
180 } 181 }
181 void Init(const char* pref_name, PrefService* prefs) { 182 void Init(const char* pref_name, PrefService* prefs) {
182 subtle::PrefMemberBase::Init(pref_name, prefs); 183 subtle::PrefMemberBase::Init(pref_name, prefs);
183 } 184 }
184 185
185 // Unsubscribes the PrefMember from the PrefService. After calling this 186 // Unsubscribes the PrefMember from the PrefService. After calling this
186 // 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.
187 // Assuming |MoveToThread| was previously called, |GetValue|, |IsManaged|, 188 // Assuming |MoveToTaskRunner| was previously called, |GetValue|, |IsManaged|,
188 // and |IsUserModifiable| can still be called from the other thread but 189 // and |IsUserModifiable| can still be called from the other thread but
189 // the results will no longer update from the PrefService. 190 // the results will no longer update from the PrefService.
190 // This method should only be called on the UI thread. 191 // This method should only be called on the UI thread.
191 void Destroy() { 192 void Destroy() {
192 subtle::PrefMemberBase::Destroy(); 193 subtle::PrefMemberBase::Destroy();
193 } 194 }
194 195
195 // Moves the PrefMember to another thread, allowing read accesses from there. 196 // Moves the PrefMember to another thread, allowing read accesses from there.
196 // Changes from the PrefService will be propagated asynchronously 197 // Changes from the PrefService will be propagated asynchronously
197 // via PostTask. 198 // via PostTask.
198 // 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
199 // on, which is the UI thread by default. 200 // on, which is the UI thread by default.
200 void MoveToThread(const scoped_refptr<base::MessageLoopProxy>& message_loop) { 201 void MoveToTaskRunner(
201 subtle::PrefMemberBase::MoveToThread(message_loop); 202 const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
203 subtle::PrefMemberBase::MoveToTaskRunner(task_runner);
202 } 204 }
203 205
204 // Check whether the pref is managed, i.e. controlled externally through 206 // Check whether the pref is managed, i.e. controlled externally through
205 // enterprise configuration management (e.g. windows group policy). Returns 207 // enterprise configuration management (e.g. windows group policy). Returns
206 // false for unknown prefs. 208 // false for unknown prefs.
207 // 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
208 // on, which is the UI thread unless changed by |MoveToThread|. 210 // on, which is the UI thread unless changed by |MoveToTaskRunner|.
209 bool IsManaged() const { 211 bool IsManaged() const {
210 VerifyPref(); 212 VerifyPref();
211 return internal_->IsManaged(); 213 return internal_->IsManaged();
212 } 214 }
213 215
214 // 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
215 // 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
216 // line flag overrides the pref. 218 // line flag overrides the pref.
217 // 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
218 // on, which is the UI thread unless changed by |MoveToThread|. 220 // on, which is the UI thread unless changed by |MoveToTaskRunner|.
219 bool IsUserModifiable() const { 221 bool IsUserModifiable() const {
220 VerifyPref(); 222 VerifyPref();
221 return internal_->IsUserModifiable(); 223 return internal_->IsUserModifiable();
222 } 224 }
223 225
224 // Retrieve the value of the member variable. 226 // Retrieve the value of the member variable.
225 // 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
226 // on, which is the UI thread unless changed by |MoveToThread|. 228 // on, which is the UI thread unless changed by |MoveToTaskRunner|.
227 ValueType GetValue() const { 229 ValueType GetValue() const {
228 VerifyPref(); 230 VerifyPref();
229 return internal_->value(); 231 return internal_->value();
230 } 232 }
231 233
232 // Provided as a convenience. 234 // Provided as a convenience.
233 ValueType operator*() const { 235 ValueType operator*() const {
234 return GetValue(); 236 return GetValue();
235 } 237 }
236 238
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 345
344 typedef PrefMember<bool> BooleanPrefMember; 346 typedef PrefMember<bool> BooleanPrefMember;
345 typedef PrefMember<int> IntegerPrefMember; 347 typedef PrefMember<int> IntegerPrefMember;
346 typedef PrefMember<double> DoublePrefMember; 348 typedef PrefMember<double> DoublePrefMember;
347 typedef PrefMember<std::string> StringPrefMember; 349 typedef PrefMember<std::string> StringPrefMember;
348 typedef PrefMember<base::FilePath> FilePathPrefMember; 350 typedef PrefMember<base::FilePath> FilePathPrefMember;
349 // This preference member is expensive for large string arrays. 351 // This preference member is expensive for large string arrays.
350 typedef PrefMember<std::vector<std::string> > StringListPrefMember; 352 typedef PrefMember<std::vector<std::string> > StringListPrefMember;
351 353
352 #endif // BASE_PREFS_PREF_MEMBER_H_ 354 #endif // BASE_PREFS_PREF_MEMBER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698