| 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 14 matching lines...) Expand all Loading... |
| 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/file_path.h" | 31 #include "base/file_path.h" |
| 32 #include "base/logging.h" | 32 #include "base/logging.h" |
| 33 #include "base/memory/ref_counted.h" | 33 #include "base/memory/ref_counted.h" |
| 34 #include "base/message_loop_proxy.h" | 34 #include "base/message_loop_proxy.h" |
| 35 #include "base/prefs/public/pref_observer.h" |
| 35 #include "base/values.h" | 36 #include "base/values.h" |
| 36 #include "content/public/browser/notification_observer.h" | |
| 37 | 37 |
| 38 class PrefServiceBase; | 38 class PrefServiceBase; |
| 39 | 39 |
| 40 namespace subtle { | 40 namespace subtle { |
| 41 | 41 |
| 42 class PrefMemberBase : public content::NotificationObserver { | 42 class PrefMemberBase : public PrefObserver { |
| 43 protected: | 43 protected: |
| 44 class Internal : public base::RefCountedThreadSafe<Internal> { | 44 class Internal : public base::RefCountedThreadSafe<Internal> { |
| 45 public: | 45 public: |
| 46 Internal(); | 46 Internal(); |
| 47 | 47 |
| 48 // Update the value, either by calling |UpdateValueInternal| directly | 48 // Update the value, either by calling |UpdateValueInternal| directly |
| 49 // or by dispatching to the right thread. | 49 // or by dispatching to the right thread. |
| 50 // Takes ownership of |value|. | 50 // Takes ownership of |value|. |
| 51 void UpdateValue(base::Value* value, | 51 void UpdateValue(base::Value* value, |
| 52 bool is_managed, | 52 bool is_managed, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 mutable bool is_user_modifiable_; | 84 mutable bool is_user_modifiable_; |
| 85 | 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(Internal); | 86 DISALLOW_COPY_AND_ASSIGN(Internal); |
| 87 }; | 87 }; |
| 88 | 88 |
| 89 PrefMemberBase(); | 89 PrefMemberBase(); |
| 90 virtual ~PrefMemberBase(); | 90 virtual ~PrefMemberBase(); |
| 91 | 91 |
| 92 // See PrefMember<> for description. | 92 // See PrefMember<> for description. |
| 93 void Init(const char* pref_name, PrefServiceBase* prefs, | 93 void Init(const char* pref_name, PrefServiceBase* prefs, |
| 94 content::NotificationObserver* observer); | 94 PrefObserver* observer); |
| 95 | 95 |
| 96 virtual void CreateInternal() const = 0; | 96 virtual void CreateInternal() const = 0; |
| 97 | 97 |
| 98 // See PrefMember<> for description. | 98 // See PrefMember<> for description. |
| 99 void Destroy(); | 99 void Destroy(); |
| 100 | 100 |
| 101 void MoveToThread(const scoped_refptr<base::MessageLoopProxy>& message_loop); | 101 void MoveToThread(const scoped_refptr<base::MessageLoopProxy>& message_loop); |
| 102 | 102 |
| 103 // content::NotificationObserver | 103 // PrefObserver |
| 104 virtual void Observe(int type, | 104 virtual void OnPreferenceChanged(PrefServiceBase* service, |
| 105 const content::NotificationSource& source, | 105 const std::string& pref_name) OVERRIDE; |
| 106 const content::NotificationDetails& details) OVERRIDE; | |
| 107 | 106 |
| 108 void VerifyValuePrefName() const { | 107 void VerifyValuePrefName() const { |
| 109 DCHECK(!pref_name_.empty()); | 108 DCHECK(!pref_name_.empty()); |
| 110 } | 109 } |
| 111 | 110 |
| 112 // This method is used to do the actual sync with the preference. | 111 // This method is used to do the actual sync with the preference. |
| 113 // Note: it is logically const, because it doesn't modify the state | 112 // Note: it is logically const, because it doesn't modify the state |
| 114 // seen by the outside world. It is just doing a lazy load behind the scenes. | 113 // seen by the outside world. It is just doing a lazy load behind the scenes. |
| 115 void UpdateValueFromPref() const; | 114 void UpdateValueFromPref() const; |
| 116 | 115 |
| 117 // Verifies the preference name, and lazily loads the preference value if | 116 // Verifies the preference name, and lazily loads the preference value if |
| 118 // it hasn't been loaded yet. | 117 // it hasn't been loaded yet. |
| 119 void VerifyPref() const; | 118 void VerifyPref() const; |
| 120 | 119 |
| 121 const std::string& pref_name() const { return pref_name_; } | 120 const std::string& pref_name() const { return pref_name_; } |
| 122 PrefServiceBase* prefs() { return prefs_; } | 121 PrefServiceBase* prefs() { return prefs_; } |
| 123 const PrefServiceBase* prefs() const { return prefs_; } | 122 const PrefServiceBase* prefs() const { return prefs_; } |
| 124 | 123 |
| 125 virtual Internal* internal() const = 0; | 124 virtual Internal* internal() const = 0; |
| 126 | 125 |
| 127 private: | 126 private: |
| 128 // Ordered the members to compact the class instance. | 127 // Ordered the members to compact the class instance. |
| 129 std::string pref_name_; | 128 std::string pref_name_; |
| 130 content::NotificationObserver* observer_; | 129 PrefObserver* observer_; |
| 131 PrefServiceBase* prefs_; | 130 PrefServiceBase* prefs_; |
| 132 | 131 |
| 133 protected: | 132 protected: |
| 134 bool setting_value_; | 133 bool setting_value_; |
| 135 }; | 134 }; |
| 136 | 135 |
| 137 // This function implements StringListPrefMember::UpdateValue(). | 136 // This function implements StringListPrefMember::UpdateValue(). |
| 138 // It is exposed here for testing purposes. | 137 // It is exposed here for testing purposes. |
| 139 bool PrefMemberVectorStringUpdate(const Value& value, | 138 bool PrefMemberVectorStringUpdate(const Value& value, |
| 140 std::vector<std::string>* string_vector); | 139 std::vector<std::string>* string_vector); |
| 141 | 140 |
| 142 } // namespace subtle | 141 } // namespace subtle |
| 143 | 142 |
| 144 template <typename ValueType> | 143 template <typename ValueType> |
| 145 class PrefMember : public subtle::PrefMemberBase { | 144 class PrefMember : public subtle::PrefMemberBase { |
| 146 public: | 145 public: |
| 147 // Defer initialization to an Init method so it's easy to make this class be | 146 // Defer initialization to an Init method so it's easy to make this class be |
| 148 // a member variable. | 147 // a member variable. |
| 149 PrefMember() {} | 148 PrefMember() {} |
| 150 virtual ~PrefMember() {} | 149 virtual ~PrefMember() {} |
| 151 | 150 |
| 152 // Do the actual initialization of the class. |observer| may be null if you | 151 // Do the actual initialization of the class. |observer| may be null if you |
| 153 // don't want any notifications of changes. | 152 // don't want any notifications of changes. |
| 154 // This method should only be called on the UI thread. | 153 // This method should only be called on the UI thread. |
| 155 void Init(const char* pref_name, PrefServiceBase* prefs, | 154 void Init(const char* pref_name, PrefServiceBase* prefs, |
| 156 content::NotificationObserver* observer) { | 155 PrefObserver* observer) { |
| 157 subtle::PrefMemberBase::Init(pref_name, prefs, observer); | 156 subtle::PrefMemberBase::Init(pref_name, prefs, observer); |
| 158 } | 157 } |
| 159 | 158 |
| 160 // Unsubscribes the PrefMember from the PrefService. After calling this | 159 // Unsubscribes the PrefMember from the PrefService. After calling this |
| 161 // function, the PrefMember may not be used any more on the UI thread. | 160 // function, the PrefMember may not be used any more on the UI thread. |
| 162 // Assuming |MoveToThread| was previously called, |GetValue|, |IsManaged|, | 161 // Assuming |MoveToThread| was previously called, |GetValue|, |IsManaged|, |
| 163 // and |IsUserModifiable| can still be called from the other thread but | 162 // and |IsUserModifiable| can still be called from the other thread but |
| 164 // the results will no longer update from the PrefService. | 163 // the results will no longer update from the PrefService. |
| 165 // This method should only be called on the UI thread. | 164 // This method should only be called on the UI thread. |
| 166 void Destroy() { | 165 void Destroy() { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 | 259 |
| 261 typedef PrefMember<bool> BooleanPrefMember; | 260 typedef PrefMember<bool> BooleanPrefMember; |
| 262 typedef PrefMember<int> IntegerPrefMember; | 261 typedef PrefMember<int> IntegerPrefMember; |
| 263 typedef PrefMember<double> DoublePrefMember; | 262 typedef PrefMember<double> DoublePrefMember; |
| 264 typedef PrefMember<std::string> StringPrefMember; | 263 typedef PrefMember<std::string> StringPrefMember; |
| 265 typedef PrefMember<FilePath> FilePathPrefMember; | 264 typedef PrefMember<FilePath> FilePathPrefMember; |
| 266 // This preference member is expensive for large string arrays. | 265 // This preference member is expensive for large string arrays. |
| 267 typedef PrefMember<std::vector<std::string> > StringListPrefMember; | 266 typedef PrefMember<std::vector<std::string> > StringListPrefMember; |
| 268 | 267 |
| 269 #endif // CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ | 268 #endif // CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ |
| OLD | NEW |