| 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 29 matching lines...) Expand all Loading... |
| 40 | 40 |
| 41 class PrefMemberBase : public NotificationObserver { | 41 class PrefMemberBase : public 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 |
| 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(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(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 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 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(); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 Internal() : value_(ValueType()) {} | 210 Internal() : value_(ValueType()) {} |
| 211 | 211 |
| 212 ValueType value() { | 212 ValueType value() { |
| 213 CheckOnCorrectThread(); | 213 CheckOnCorrectThread(); |
| 214 return value_; | 214 return value_; |
| 215 } | 215 } |
| 216 | 216 |
| 217 protected: | 217 protected: |
| 218 virtual ~Internal() {} | 218 virtual ~Internal() {} |
| 219 | 219 |
| 220 virtual bool UpdateValueInternal(const Value& value) const; | 220 virtual bool UpdateValueInternal(const base::Value& value) const; |
| 221 | 221 |
| 222 // We cache the value of the pref so we don't have to keep walking the pref | 222 // We cache the value of the pref so we don't have to keep walking the pref |
| 223 // tree. | 223 // tree. |
| 224 mutable ValueType value_; | 224 mutable ValueType value_; |
| 225 | 225 |
| 226 DISALLOW_COPY_AND_ASSIGN(Internal); | 226 DISALLOW_COPY_AND_ASSIGN(Internal); |
| 227 }; | 227 }; |
| 228 | 228 |
| 229 virtual Internal* internal() const { return internal_; } | 229 virtual Internal* internal() const { return internal_; } |
| 230 virtual void CreateInternal() const { | 230 virtual void CreateInternal() const { |
| 231 internal_ = new Internal(); | 231 internal_ = new Internal(); |
| 232 } | 232 } |
| 233 | 233 |
| 234 // This method is used to do the actual sync with pref of the specified type. | 234 // This method is used to do the actual sync with pref of the specified type. |
| 235 virtual void UpdatePref(const ValueType& value); | 235 virtual void UpdatePref(const ValueType& value); |
| 236 | 236 |
| 237 mutable scoped_refptr<Internal> internal_; | 237 mutable scoped_refptr<Internal> internal_; |
| 238 | 238 |
| 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 |