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 15 matching lines...) Expand all Loading... |
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/single_thread_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. |
(...skipping 10 matching lines...) Expand all Loading... |
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 MoveToThread( |
69 const scoped_refptr<base::MessageLoopProxy>& message_loop); | 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::MessageLoopProxy> thread_loop_; | 95 scoped_refptr<base::SingleThreadTaskRunner> thread_loop_; |
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 MoveToThread( |
| 116 const scoped_refptr<base::SingleThreadTaskRunner>& 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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 MoveToThread( |
201 subtle::PrefMemberBase::MoveToThread(message_loop); | 202 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { |
| 203 subtle::PrefMemberBase::MoveToThread(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 |MoveToThread|. |
209 bool IsManaged() const { | 211 bool IsManaged() const { |
210 VerifyPref(); | 212 VerifyPref(); |
211 return internal_->IsManaged(); | 213 return internal_->IsManaged(); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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_ |
OLD | NEW |