Chromium Code Reviews| 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 10 matching lines...) Expand all Loading... | |
| 21 // notify MyClass of changes. Note that if you use SetValue(), the observer | 21 // notify MyClass of changes. Note that if you use SetValue(), the observer |
| 22 // will not be notified. | 22 // will not be notified. |
| 23 | 23 |
| 24 #ifndef CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ | 24 #ifndef CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ |
| 25 #define CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ | 25 #define CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ |
| 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" | |
| 32 #include "base/callback_forward.h" | |
| 31 #include "base/file_path.h" | 33 #include "base/file_path.h" |
| 32 #include "base/logging.h" | 34 #include "base/logging.h" |
| 33 #include "base/memory/ref_counted.h" | 35 #include "base/memory/ref_counted.h" |
| 34 #include "base/message_loop_proxy.h" | 36 #include "base/message_loop_proxy.h" |
| 35 #include "base/prefs/public/pref_observer.h" | 37 #include "base/prefs/public/pref_observer.h" |
| 36 #include "base/values.h" | 38 #include "base/values.h" |
| 37 | 39 |
| 38 class PrefServiceBase; | 40 class PrefServiceBase; |
| 39 | 41 |
| 40 namespace subtle { | 42 namespace subtle { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 mutable bool is_user_modifiable_; | 86 mutable bool is_user_modifiable_; |
| 85 | 87 |
| 86 DISALLOW_COPY_AND_ASSIGN(Internal); | 88 DISALLOW_COPY_AND_ASSIGN(Internal); |
| 87 }; | 89 }; |
| 88 | 90 |
| 89 PrefMemberBase(); | 91 PrefMemberBase(); |
| 90 virtual ~PrefMemberBase(); | 92 virtual ~PrefMemberBase(); |
| 91 | 93 |
| 92 // See PrefMember<> for description. | 94 // See PrefMember<> for description. |
| 93 void Init(const char* pref_name, PrefServiceBase* prefs, | 95 void Init(const char* pref_name, PrefServiceBase* prefs, |
| 94 PrefObserver* observer); | 96 const base::Closure& observer); |
| 97 void Init(const char* pref_name, PrefServiceBase* prefs); | |
| 95 | 98 |
| 96 virtual void CreateInternal() const = 0; | 99 virtual void CreateInternal() const = 0; |
| 97 | 100 |
| 98 // See PrefMember<> for description. | 101 // See PrefMember<> for description. |
| 99 void Destroy(); | 102 void Destroy(); |
| 100 | 103 |
| 101 void MoveToThread(const scoped_refptr<base::MessageLoopProxy>& message_loop); | 104 void MoveToThread(const scoped_refptr<base::MessageLoopProxy>& message_loop); |
| 102 | 105 |
| 103 // PrefObserver | 106 // PrefObserver |
| 104 virtual void OnPreferenceChanged(PrefServiceBase* service, | 107 virtual void OnPreferenceChanged(PrefServiceBase* service, |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 117 // it hasn't been loaded yet. | 120 // it hasn't been loaded yet. |
| 118 void VerifyPref() const; | 121 void VerifyPref() const; |
| 119 | 122 |
| 120 const std::string& pref_name() const { return pref_name_; } | 123 const std::string& pref_name() const { return pref_name_; } |
| 121 PrefServiceBase* prefs() { return prefs_; } | 124 PrefServiceBase* prefs() { return prefs_; } |
| 122 const PrefServiceBase* prefs() const { return prefs_; } | 125 const PrefServiceBase* prefs() const { return prefs_; } |
| 123 | 126 |
| 124 virtual Internal* internal() const = 0; | 127 virtual Internal* internal() const = 0; |
| 125 | 128 |
| 126 private: | 129 private: |
| 130 // |observer_| is bound to this unless an external observer is provided. | |
| 131 void DoNothing() {} | |
|
Mattias Nissler (ping if slow)
2012/11/07 16:02:24
base/bind_helpers.h has a DoNothing that you can u
Jói
2012/11/08 11:03:14
Thanks, switched.
| |
| 132 | |
| 127 // Ordered the members to compact the class instance. | 133 // Ordered the members to compact the class instance. |
| 128 std::string pref_name_; | 134 std::string pref_name_; |
| 129 PrefObserver* observer_; | 135 base::Closure observer_; |
| 130 PrefServiceBase* prefs_; | 136 PrefServiceBase* prefs_; |
| 131 | 137 |
| 132 protected: | 138 protected: |
| 133 bool setting_value_; | 139 bool setting_value_; |
| 134 }; | 140 }; |
| 135 | 141 |
| 136 // This function implements StringListPrefMember::UpdateValue(). | 142 // This function implements StringListPrefMember::UpdateValue(). |
| 137 // It is exposed here for testing purposes. | 143 // It is exposed here for testing purposes. |
| 138 bool PrefMemberVectorStringUpdate(const Value& value, | 144 bool PrefMemberVectorStringUpdate(const Value& value, |
| 139 std::vector<std::string>* string_vector); | 145 std::vector<std::string>* string_vector); |
| 140 | 146 |
| 141 } // namespace subtle | 147 } // namespace subtle |
| 142 | 148 |
| 143 template <typename ValueType> | 149 template <typename ValueType> |
| 144 class PrefMember : public subtle::PrefMemberBase { | 150 class PrefMember : public subtle::PrefMemberBase { |
| 145 public: | 151 public: |
| 146 // Defer initialization to an Init method so it's easy to make this class be | 152 // Defer initialization to an Init method so it's easy to make this class be |
| 147 // a member variable. | 153 // a member variable. |
| 148 PrefMember() {} | 154 PrefMember() {} |
| 149 virtual ~PrefMember() {} | 155 virtual ~PrefMember() {} |
| 150 | 156 |
| 151 // Do the actual initialization of the class. |observer| may be null if you | 157 // Do the actual initialization of the class. Use the two-parameter |
| 152 // don't want any notifications of changes. | 158 // version if you don't want any notifications of changes. This |
| 153 // This method should only be called on the UI thread. | 159 // method should only be called on the UI thread. |
| 160 void Init(const char* pref_name, PrefServiceBase* prefs, | |
| 161 const base::Closure& observer) { | |
| 162 subtle::PrefMemberBase::Init(pref_name, prefs, observer); | |
| 163 } | |
| 164 | |
| 165 void Init(const char* pref_name, PrefServiceBase* prefs) { | |
| 166 subtle::PrefMemberBase::Init(pref_name, prefs); | |
| 167 } | |
| 168 | |
| 169 // Deprecated version of Init. | |
| 154 void Init(const char* pref_name, PrefServiceBase* prefs, | 170 void Init(const char* pref_name, PrefServiceBase* prefs, |
| 155 PrefObserver* observer) { | 171 PrefObserver* observer) { |
| 156 subtle::PrefMemberBase::Init(pref_name, prefs, observer); | 172 if (observer) { |
| 173 Init(pref_name, prefs, base::Bind(&PrefObserver::OnPreferenceChanged, | |
| 174 base::Unretained(observer), | |
| 175 prefs, pref_name)); | |
| 176 } else { | |
| 177 Init(pref_name, prefs); | |
| 178 } | |
| 157 } | 179 } |
| 158 | 180 |
| 159 // Unsubscribes the PrefMember from the PrefService. After calling this | 181 // Unsubscribes the PrefMember from the PrefService. After calling this |
| 160 // function, the PrefMember may not be used any more on the UI thread. | 182 // function, the PrefMember may not be used any more on the UI thread. |
| 161 // Assuming |MoveToThread| was previously called, |GetValue|, |IsManaged|, | 183 // Assuming |MoveToThread| was previously called, |GetValue|, |IsManaged|, |
| 162 // and |IsUserModifiable| can still be called from the other thread but | 184 // and |IsUserModifiable| can still be called from the other thread but |
| 163 // the results will no longer update from the PrefService. | 185 // the results will no longer update from the PrefService. |
| 164 // This method should only be called on the UI thread. | 186 // This method should only be called on the UI thread. |
| 165 void Destroy() { | 187 void Destroy() { |
| 166 subtle::PrefMemberBase::Destroy(); | 188 subtle::PrefMemberBase::Destroy(); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 259 | 281 |
| 260 typedef PrefMember<bool> BooleanPrefMember; | 282 typedef PrefMember<bool> BooleanPrefMember; |
| 261 typedef PrefMember<int> IntegerPrefMember; | 283 typedef PrefMember<int> IntegerPrefMember; |
| 262 typedef PrefMember<double> DoublePrefMember; | 284 typedef PrefMember<double> DoublePrefMember; |
| 263 typedef PrefMember<std::string> StringPrefMember; | 285 typedef PrefMember<std::string> StringPrefMember; |
| 264 typedef PrefMember<FilePath> FilePathPrefMember; | 286 typedef PrefMember<FilePath> FilePathPrefMember; |
| 265 // This preference member is expensive for large string arrays. | 287 // This preference member is expensive for large string arrays. |
| 266 typedef PrefMember<std::vector<std::string> > StringListPrefMember; | 288 typedef PrefMember<std::vector<std::string> > StringListPrefMember; |
| 267 | 289 |
| 268 #endif // CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ | 290 #endif // CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ |
| OLD | NEW |