OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 protected: | 42 protected: |
43 class Internal : public base::RefCountedThreadSafe<Internal> { | 43 class Internal : public base::RefCountedThreadSafe<Internal> { |
44 public: | 44 public: |
45 Internal(); | 45 Internal(); |
46 | 46 |
47 // Update the value, either by calling |UpdateValueInternal| directly | 47 // Update the value, either by calling |UpdateValueInternal| directly |
48 // or by dispatching to the right thread. | 48 // or by dispatching to the right thread. |
49 // Takes ownership of |value|. | 49 // Takes ownership of |value|. |
50 virtual void UpdateValue(base::Value* value, bool is_managed) const; | 50 virtual void UpdateValue(base::Value* value, bool is_managed) const; |
51 | 51 |
52 void MoveToThread(BrowserThread::ID thread_id); | 52 void MoveToThread(content::BrowserThread::ID thread_id); |
53 | 53 |
54 // See PrefMember<> for description. | 54 // See PrefMember<> for description. |
55 bool IsManaged() const { | 55 bool IsManaged() const { |
56 return is_managed_; | 56 return is_managed_; |
57 } | 57 } |
58 | 58 |
59 protected: | 59 protected: |
60 friend class base::RefCountedThreadSafe<Internal>; | 60 friend class base::RefCountedThreadSafe<Internal>; |
61 virtual ~Internal(); | 61 virtual ~Internal(); |
62 | 62 |
63 void CheckOnCorrectThread() const { | 63 void CheckOnCorrectThread() const { |
64 DCHECK(IsOnCorrectThread()); | 64 DCHECK(IsOnCorrectThread()); |
65 } | 65 } |
66 | 66 |
67 private: | 67 private: |
68 // This method actually updates the value. It should only be called from | 68 // This method actually updates the value. It should only be called from |
69 // the thread the PrefMember is on. | 69 // the thread the PrefMember is on. |
70 virtual bool UpdateValueInternal(const base::Value& value) const = 0; | 70 virtual bool UpdateValueInternal(const base::Value& value) const = 0; |
71 | 71 |
72 bool IsOnCorrectThread() const; | 72 bool IsOnCorrectThread() const; |
73 | 73 |
74 BrowserThread::ID thread_id_; | 74 content::BrowserThread::ID thread_id_; |
75 mutable bool is_managed_; | 75 mutable bool is_managed_; |
76 | 76 |
77 DISALLOW_COPY_AND_ASSIGN(Internal); | 77 DISALLOW_COPY_AND_ASSIGN(Internal); |
78 }; | 78 }; |
79 | 79 |
80 PrefMemberBase(); | 80 PrefMemberBase(); |
81 virtual ~PrefMemberBase(); | 81 virtual ~PrefMemberBase(); |
82 | 82 |
83 // See PrefMember<> for description. | 83 // See PrefMember<> for description. |
84 void Init(const char* pref_name, PrefService* prefs, | 84 void Init(const char* pref_name, PrefService* prefs, |
85 content::NotificationObserver* observer); | 85 content::NotificationObserver* observer); |
86 | 86 |
87 virtual void CreateInternal() const = 0; | 87 virtual void CreateInternal() const = 0; |
88 | 88 |
89 // See PrefMember<> for description. | 89 // See PrefMember<> for description. |
90 void Destroy(); | 90 void Destroy(); |
91 | 91 |
92 void MoveToThread(BrowserThread::ID thread_id); | 92 void MoveToThread(content::BrowserThread::ID thread_id); |
93 | 93 |
94 // content::NotificationObserver | 94 // content::NotificationObserver |
95 virtual void Observe(int type, | 95 virtual void Observe(int type, |
96 const content::NotificationSource& source, | 96 const content::NotificationSource& source, |
97 const content::NotificationDetails& details); | 97 const content::NotificationDetails& details); |
98 | 98 |
99 void VerifyValuePrefName() const { | 99 void VerifyValuePrefName() const { |
100 DCHECK(!pref_name_.empty()); | 100 DCHECK(!pref_name_.empty()); |
101 } | 101 } |
102 | 102 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 // This method should only be called on the UI thread. | 148 // This method should only be called on the UI thread. |
149 void Destroy() { | 149 void Destroy() { |
150 subtle::PrefMemberBase::Destroy(); | 150 subtle::PrefMemberBase::Destroy(); |
151 } | 151 } |
152 | 152 |
153 // Moves the PrefMember to another thread, allowing read accesses from there. | 153 // Moves the PrefMember to another thread, allowing read accesses from there. |
154 // Changes from the PrefService will be propagated asynchronously | 154 // Changes from the PrefService will be propagated asynchronously |
155 // via PostTask. | 155 // via PostTask. |
156 // This method should only be used from the thread the PrefMember is currently | 156 // This method should only be used from the thread the PrefMember is currently |
157 // on, which is the UI thread by default. | 157 // on, which is the UI thread by default. |
158 void MoveToThread(BrowserThread::ID thread_id) { | 158 void MoveToThread(content::BrowserThread::ID thread_id) { |
159 subtle::PrefMemberBase::MoveToThread(thread_id); | 159 subtle::PrefMemberBase::MoveToThread(thread_id); |
160 } | 160 } |
161 | 161 |
162 // Check whether the pref is managed, i.e. controlled externally through | 162 // Check whether the pref is managed, i.e. controlled externally through |
163 // enterprise configuration management (e.g. windows group policy). Returns | 163 // enterprise configuration management (e.g. windows group policy). Returns |
164 // false for unknown prefs. | 164 // false for unknown prefs. |
165 // This method should only be used from the thread the PrefMember is currently | 165 // This method should only be used from the thread the PrefMember is currently |
166 // on, which is the UI thread unless changed by |MoveToThread|. | 166 // on, which is the UI thread unless changed by |MoveToThread|. |
167 bool IsManaged() const { | 167 bool IsManaged() const { |
168 VerifyPref(); | 168 VerifyPref(); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 DISALLOW_COPY_AND_ASSIGN(PrefMember); | 239 DISALLOW_COPY_AND_ASSIGN(PrefMember); |
240 }; | 240 }; |
241 | 241 |
242 typedef PrefMember<bool> BooleanPrefMember; | 242 typedef PrefMember<bool> BooleanPrefMember; |
243 typedef PrefMember<int> IntegerPrefMember; | 243 typedef PrefMember<int> IntegerPrefMember; |
244 typedef PrefMember<double> DoublePrefMember; | 244 typedef PrefMember<double> DoublePrefMember; |
245 typedef PrefMember<std::string> StringPrefMember; | 245 typedef PrefMember<std::string> StringPrefMember; |
246 typedef PrefMember<FilePath> FilePathPrefMember; | 246 typedef PrefMember<FilePath> FilePathPrefMember; |
247 | 247 |
248 #endif // CHROME_BROWSER_PREFS_PREF_MEMBER_H_ | 248 #endif // CHROME_BROWSER_PREFS_PREF_MEMBER_H_ |
OLD | NEW |