| 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 15 matching lines...) Expand all Loading... |
| 26 #pragma once | 26 #pragma once |
| 27 | 27 |
| 28 #include <string> | 28 #include <string> |
| 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/memory/ref_counted.h" | 32 #include "base/memory/ref_counted.h" |
| 33 #include "base/values.h" | 33 #include "base/values.h" |
| 34 #include "content/browser/browser_thread.h" | 34 #include "content/browser/browser_thread.h" |
| 35 #include "content/common/notification_observer.h" | 35 #include "content/common/notification_observer.h" |
| 36 #include "content/common/notification_registrar.h" |
| 36 | 37 |
| 37 class PrefService; | 38 class PrefService; |
| 39 class Profile; |
| 38 | 40 |
| 39 namespace subtle { | 41 namespace subtle { |
| 40 | 42 |
| 41 class PrefMemberBase : public NotificationObserver { | 43 class PrefMemberBase : public NotificationObserver { |
| 42 protected: | 44 protected: |
| 43 class Internal : public base::RefCountedThreadSafe<Internal> { | 45 class Internal : public base::RefCountedThreadSafe<Internal> { |
| 44 public: | 46 public: |
| 45 Internal(); | 47 Internal(); |
| 46 | 48 |
| 47 // Update the value, either by calling |UpdateValueInternal| directly | 49 // Update the value, either by calling |UpdateValueInternal| directly |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 | 81 |
| 80 PrefMemberBase(); | 82 PrefMemberBase(); |
| 81 virtual ~PrefMemberBase(); | 83 virtual ~PrefMemberBase(); |
| 82 | 84 |
| 83 // See PrefMember<> for description. | 85 // See PrefMember<> for description. |
| 84 void Init(const char* pref_name, PrefService* prefs, | 86 void Init(const char* pref_name, PrefService* prefs, |
| 85 NotificationObserver* observer); | 87 NotificationObserver* observer); |
| 86 | 88 |
| 87 virtual void CreateInternal() const = 0; | 89 virtual void CreateInternal() const = 0; |
| 88 | 90 |
| 91 void ObserveProfileDestruction(Profile* profile); |
| 92 |
| 89 // See PrefMember<> for description. | 93 // See PrefMember<> for description. |
| 90 void Destroy(); | 94 void Destroy(); |
| 91 | 95 |
| 92 void MoveToThread(BrowserThread::ID thread_id); | 96 void MoveToThread(BrowserThread::ID thread_id); |
| 93 | 97 |
| 94 // NotificationObserver | 98 // NotificationObserver |
| 95 virtual void Observe(NotificationType type, | 99 virtual void Observe(NotificationType type, |
| 96 const NotificationSource& source, | 100 const NotificationSource& source, |
| 97 const NotificationDetails& details); | 101 const NotificationDetails& details); |
| 98 | 102 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 113 | 117 |
| 114 const std::string& pref_name() const { return pref_name_; } | 118 const std::string& pref_name() const { return pref_name_; } |
| 115 PrefService* prefs() { return prefs_; } | 119 PrefService* prefs() { return prefs_; } |
| 116 const PrefService* prefs() const { return prefs_; } | 120 const PrefService* prefs() const { return prefs_; } |
| 117 | 121 |
| 118 virtual Internal* internal() const = 0; | 122 virtual Internal* internal() const = 0; |
| 119 | 123 |
| 120 // Ordered the members to compact the class instance. | 124 // Ordered the members to compact the class instance. |
| 121 private: | 125 private: |
| 122 std::string pref_name_; | 126 std::string pref_name_; |
| 127 NotificationRegistrar registrar_; |
| 123 NotificationObserver* observer_; | 128 NotificationObserver* observer_; |
| 124 PrefService* prefs_; | 129 PrefService* prefs_; |
| 125 | 130 |
| 126 protected: | 131 protected: |
| 127 bool setting_value_; | 132 bool setting_value_; |
| 128 }; | 133 }; |
| 129 | 134 |
| 130 } // namespace subtle | 135 } // namespace subtle |
| 131 | 136 |
| 132 template <typename ValueType> | 137 template <typename ValueType> |
| 133 class PrefMember : public subtle::PrefMemberBase { | 138 class PrefMember : public subtle::PrefMemberBase { |
| 134 public: | 139 public: |
| 135 // Defer initialization to an Init method so it's easy to make this class be | 140 // Defer initialization to an Init method so it's easy to make this class be |
| 136 // a member variable. | 141 // a member variable. |
| 137 PrefMember() {} | 142 PrefMember() {} |
| 138 virtual ~PrefMember() {} | 143 virtual ~PrefMember() {} |
| 139 | 144 |
| 140 // Do the actual initialization of the class. |observer| may be null if you | 145 // Do the actual initialization of the class. |observer| may be null if you |
| 141 // don't want any notifications of changes. | 146 // don't want any notifications of changes. |
| 142 // This method should only be called on the UI thread. | 147 // This method should only be called on the UI thread. |
| 143 void Init(const char* pref_name, PrefService* prefs, | 148 void Init(const char* pref_name, PrefService* prefs, |
| 144 NotificationObserver* observer) { | 149 NotificationObserver* observer) { |
| 145 subtle::PrefMemberBase::Init(pref_name, prefs, observer); | 150 subtle::PrefMemberBase::Init(pref_name, prefs, observer); |
| 146 } | 151 } |
| 147 | 152 |
| 153 // Starts observing PROFILE_DESTROYED notifications for the given |profile|, |
| 154 // to automatically unsubscribe from the PrefService. |
| 155 void ObserveProfileDestruction(Profile* profile) { |
| 156 subtle::PrefMemberBase::ObserveProfileDestruction(profile); |
| 157 } |
| 158 |
| 148 // Unsubscribes the PrefMember from the PrefService. After calling this | 159 // Unsubscribes the PrefMember from the PrefService. After calling this |
| 149 // function, the PrefMember may not be used any more. | 160 // function, the PrefMember may not be used any more. |
| 150 // This method should only be called on the UI thread. | 161 // This method should only be called on the UI thread. |
| 151 void Destroy() { | 162 void Destroy() { |
| 152 subtle::PrefMemberBase::Destroy(); | 163 subtle::PrefMemberBase::Destroy(); |
| 153 } | 164 } |
| 154 | 165 |
| 155 // Moves the PrefMember to another thread, allowing read accesses from there. | 166 // Moves the PrefMember to another thread, allowing read accesses from there. |
| 156 // Changes from the PrefService will be propagated asynchronously | 167 // Changes from the PrefService will be propagated asynchronously |
| 157 // via PostTask. | 168 // via PostTask. |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 DISALLOW_COPY_AND_ASSIGN(PrefMember); | 252 DISALLOW_COPY_AND_ASSIGN(PrefMember); |
| 242 }; | 253 }; |
| 243 | 254 |
| 244 typedef PrefMember<bool> BooleanPrefMember; | 255 typedef PrefMember<bool> BooleanPrefMember; |
| 245 typedef PrefMember<int> IntegerPrefMember; | 256 typedef PrefMember<int> IntegerPrefMember; |
| 246 typedef PrefMember<double> DoublePrefMember; | 257 typedef PrefMember<double> DoublePrefMember; |
| 247 typedef PrefMember<std::string> StringPrefMember; | 258 typedef PrefMember<std::string> StringPrefMember; |
| 248 typedef PrefMember<FilePath> FilePathPrefMember; | 259 typedef PrefMember<FilePath> FilePathPrefMember; |
| 249 | 260 |
| 250 #endif // CHROME_BROWSER_PREFS_PREF_MEMBER_H_ | 261 #endif // CHROME_BROWSER_PREFS_PREF_MEMBER_H_ |
| OLD | NEW |