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 16 matching lines...) Expand all Loading... |
27 #include <string> | 27 #include <string> |
28 | 28 |
29 #include "base/basictypes.h" | 29 #include "base/basictypes.h" |
30 #include "base/file_path.h" | 30 #include "base/file_path.h" |
31 #include "base/logging.h" | 31 #include "base/logging.h" |
32 #include "base/memory/ref_counted.h" | 32 #include "base/memory/ref_counted.h" |
33 #include "base/values.h" | 33 #include "base/values.h" |
34 #include "content/public/browser/browser_thread.h" | 34 #include "content/public/browser/browser_thread.h" |
35 #include "content/public/browser/notification_observer.h" | 35 #include "content/public/browser/notification_observer.h" |
36 | 36 |
37 class PrefService; | 37 class PrefServiceBase; |
38 | 38 |
39 namespace subtle { | 39 namespace subtle { |
40 | 40 |
41 class PrefMemberBase : public content::NotificationObserver { | 41 class PrefMemberBase : public content::NotificationObserver { |
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 |
(...skipping 26 matching lines...) Expand all Loading... |
74 content::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, PrefServiceBase* 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(content::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) OVERRIDE; | 97 const content::NotificationDetails& details) OVERRIDE; |
98 | 98 |
99 void VerifyValuePrefName() const { | 99 void VerifyValuePrefName() const { |
100 DCHECK(!pref_name_.empty()); | 100 DCHECK(!pref_name_.empty()); |
101 } | 101 } |
102 | 102 |
103 // This method is used to do the actual sync with the preference. | 103 // This method is used to do the actual sync with the preference. |
104 // Note: it is logically const, because it doesn't modify the state | 104 // Note: it is logically const, because it doesn't modify the state |
105 // seen by the outside world. It is just doing a lazy load behind the scenes. | 105 // seen by the outside world. It is just doing a lazy load behind the scenes. |
106 virtual void UpdateValueFromPref() const; | 106 virtual void UpdateValueFromPref() const; |
107 | 107 |
108 // Verifies the preference name, and lazily loads the preference value if | 108 // Verifies the preference name, and lazily loads the preference value if |
109 // it hasn't been loaded yet. | 109 // it hasn't been loaded yet. |
110 void VerifyPref() const; | 110 void VerifyPref() const; |
111 | 111 |
112 const std::string& pref_name() const { return pref_name_; } | 112 const std::string& pref_name() const { return pref_name_; } |
113 PrefService* prefs() { return prefs_; } | 113 PrefServiceBase* prefs() { return prefs_; } |
114 const PrefService* prefs() const { return prefs_; } | 114 const PrefServiceBase* prefs() const { return prefs_; } |
115 | 115 |
116 virtual Internal* internal() const = 0; | 116 virtual Internal* internal() const = 0; |
117 | 117 |
118 private: | 118 private: |
119 // Ordered the members to compact the class instance. | 119 // Ordered the members to compact the class instance. |
120 std::string pref_name_; | 120 std::string pref_name_; |
121 content::NotificationObserver* observer_; | 121 content::NotificationObserver* observer_; |
122 PrefService* prefs_; | 122 PrefServiceBase* prefs_; |
123 | 123 |
124 protected: | 124 protected: |
125 bool setting_value_; | 125 bool setting_value_; |
126 }; | 126 }; |
127 | 127 |
128 } // namespace subtle | 128 } // namespace subtle |
129 | 129 |
130 template <typename ValueType> | 130 template <typename ValueType> |
131 class PrefMember : public subtle::PrefMemberBase { | 131 class PrefMember : public subtle::PrefMemberBase { |
132 public: | 132 public: |
133 // Defer initialization to an Init method so it's easy to make this class be | 133 // Defer initialization to an Init method so it's easy to make this class be |
134 // a member variable. | 134 // a member variable. |
135 PrefMember() {} | 135 PrefMember() {} |
136 virtual ~PrefMember() {} | 136 virtual ~PrefMember() {} |
137 | 137 |
138 // Do the actual initialization of the class. |observer| may be null if you | 138 // Do the actual initialization of the class. |observer| may be null if you |
139 // don't want any notifications of changes. | 139 // don't want any notifications of changes. |
140 // This method should only be called on the UI thread. | 140 // This method should only be called on the UI thread. |
141 void Init(const char* pref_name, PrefService* prefs, | 141 void Init(const char* pref_name, PrefServiceBase* prefs, |
142 content::NotificationObserver* observer) { | 142 content::NotificationObserver* observer) { |
143 subtle::PrefMemberBase::Init(pref_name, prefs, observer); | 143 subtle::PrefMemberBase::Init(pref_name, prefs, observer); |
144 } | 144 } |
145 | 145 |
146 // Unsubscribes the PrefMember from the PrefService. After calling this | 146 // Unsubscribes the PrefMember from the PrefService. After calling this |
147 // function, the PrefMember may not be used any more. | 147 // function, the PrefMember may not be used any more. |
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 } |
(...skipping 87 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_API_PREFS_PREF_MEMBER_H_ | 248 #endif // CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ |
OLD | NEW |