| 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 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 #include "base/basictypes.h" | 30 #include "base/basictypes.h" |
| 31 #include "base/bind.h" | 31 #include "base/bind.h" |
| 32 #include "base/callback_forward.h" | 32 #include "base/callback_forward.h" |
| 33 #include "base/files/file_path.h" | 33 #include "base/files/file_path.h" |
| 34 #include "base/logging.h" | 34 #include "base/logging.h" |
| 35 #include "base/memory/ref_counted.h" | 35 #include "base/memory/ref_counted.h" |
| 36 #include "base/message_loop/message_loop_proxy.h" | 36 #include "base/message_loop/message_loop_proxy.h" |
| 37 #include "base/prefs/base_prefs_export.h" | 37 #include "base/prefs/base_prefs_export.h" |
| 38 #include "base/prefs/pref_observer.h" | 38 #include "base/prefs/pref_observer.h" |
| 39 #include "base/single_thread_task_runner.h" |
| 39 #include "base/values.h" | 40 #include "base/values.h" |
| 40 | 41 |
| 41 class PrefService; | 42 class PrefService; |
| 42 | 43 |
| 43 namespace subtle { | 44 namespace subtle { |
| 44 | 45 |
| 45 class BASE_PREFS_EXPORT PrefMemberBase : public PrefObserver { | 46 class BASE_PREFS_EXPORT PrefMemberBase : public PrefObserver { |
| 46 public: | 47 public: |
| 47 // Type of callback you can register if you need to know the name of | 48 // Type of callback you can register if you need to know the name of |
| 48 // the pref that is changing. | 49 // the pref that is changing. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 59 | 60 |
| 60 // Update the value, either by calling |UpdateValueInternal| directly | 61 // Update the value, either by calling |UpdateValueInternal| directly |
| 61 // or by dispatching to the right thread. | 62 // or by dispatching to the right thread. |
| 62 // Takes ownership of |value|. | 63 // Takes ownership of |value|. |
| 63 void UpdateValue(base::Value* value, | 64 void UpdateValue(base::Value* value, |
| 64 bool is_managed, | 65 bool is_managed, |
| 65 bool is_user_modifiable, | 66 bool is_user_modifiable, |
| 66 const base::Closure& callback) const; | 67 const base::Closure& callback) const; |
| 67 | 68 |
| 68 void MoveToThread( | 69 void MoveToThread( |
| 69 const scoped_refptr<base::MessageLoopProxy>& message_loop); | 70 const scoped_refptr<base::SingleThreadTaskRunner>& message_loop); |
| 70 | 71 |
| 71 // See PrefMember<> for description. | 72 // See PrefMember<> for description. |
| 72 bool IsManaged() const { | 73 bool IsManaged() const { |
| 73 return is_managed_; | 74 return is_managed_; |
| 74 } | 75 } |
| 75 | 76 |
| 76 bool IsUserModifiable() const { | 77 bool IsUserModifiable() const { |
| 77 return is_user_modifiable_; | 78 return is_user_modifiable_; |
| 78 } | 79 } |
| 79 | 80 |
| 80 protected: | 81 protected: |
| 81 friend class base::RefCountedThreadSafe<Internal>; | 82 friend class base::RefCountedThreadSafe<Internal>; |
| 82 virtual ~Internal(); | 83 virtual ~Internal(); |
| 83 | 84 |
| 84 void CheckOnCorrectThread() const { | 85 void CheckOnCorrectThread() const { |
| 85 DCHECK(IsOnCorrectThread()); | 86 DCHECK(IsOnCorrectThread()); |
| 86 } | 87 } |
| 87 | 88 |
| 88 private: | 89 private: |
| 89 // This method actually updates the value. It should only be called from | 90 // This method actually updates the value. It should only be called from |
| 90 // the thread the PrefMember is on. | 91 // the thread the PrefMember is on. |
| 91 virtual bool UpdateValueInternal(const base::Value& value) const = 0; | 92 virtual bool UpdateValueInternal(const base::Value& value) const = 0; |
| 92 | 93 |
| 93 bool IsOnCorrectThread() const; | 94 bool IsOnCorrectThread() const; |
| 94 | 95 |
| 95 scoped_refptr<base::MessageLoopProxy> thread_loop_; | 96 scoped_refptr<base::SingleThreadTaskRunner> thread_loop_; |
| 96 mutable bool is_managed_; | 97 mutable bool is_managed_; |
| 97 mutable bool is_user_modifiable_; | 98 mutable bool is_user_modifiable_; |
| 98 | 99 |
| 99 DISALLOW_COPY_AND_ASSIGN(Internal); | 100 DISALLOW_COPY_AND_ASSIGN(Internal); |
| 100 }; | 101 }; |
| 101 | 102 |
| 102 PrefMemberBase(); | 103 PrefMemberBase(); |
| 103 virtual ~PrefMemberBase(); | 104 virtual ~PrefMemberBase(); |
| 104 | 105 |
| 105 // See PrefMember<> for description. | 106 // See PrefMember<> for description. |
| 106 void Init(const char* pref_name, PrefService* prefs, | 107 void Init(const char* pref_name, PrefService* prefs, |
| 107 const NamedChangeCallback& observer); | 108 const NamedChangeCallback& observer); |
| 108 void Init(const char* pref_name, PrefService* prefs); | 109 void Init(const char* pref_name, PrefService* prefs); |
| 109 | 110 |
| 110 virtual void CreateInternal() const = 0; | 111 virtual void CreateInternal() const = 0; |
| 111 | 112 |
| 112 // See PrefMember<> for description. | 113 // See PrefMember<> for description. |
| 113 void Destroy(); | 114 void Destroy(); |
| 114 | 115 |
| 115 void MoveToThread(const scoped_refptr<base::MessageLoopProxy>& message_loop); | 116 void MoveToThread( |
| 117 const scoped_refptr<base::SingleThreadTaskRunner>& message_loop); |
| 116 | 118 |
| 117 // PrefObserver | 119 // PrefObserver |
| 118 virtual void OnPreferenceChanged(PrefService* service, | 120 virtual void OnPreferenceChanged(PrefService* service, |
| 119 const std::string& pref_name) OVERRIDE; | 121 const std::string& pref_name) OVERRIDE; |
| 120 | 122 |
| 121 void VerifyValuePrefName() const { | 123 void VerifyValuePrefName() const { |
| 122 DCHECK(!pref_name_.empty()); | 124 DCHECK(!pref_name_.empty()); |
| 123 } | 125 } |
| 124 | 126 |
| 125 // This method is used to do the actual sync with the preference. | 127 // This method is used to do the actual sync with the preference. |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 // This method should only be called on the UI thread. | 192 // This method should only be called on the UI thread. |
| 191 void Destroy() { | 193 void Destroy() { |
| 192 subtle::PrefMemberBase::Destroy(); | 194 subtle::PrefMemberBase::Destroy(); |
| 193 } | 195 } |
| 194 | 196 |
| 195 // Moves the PrefMember to another thread, allowing read accesses from there. | 197 // Moves the PrefMember to another thread, allowing read accesses from there. |
| 196 // Changes from the PrefService will be propagated asynchronously | 198 // Changes from the PrefService will be propagated asynchronously |
| 197 // via PostTask. | 199 // via PostTask. |
| 198 // This method should only be used from the thread the PrefMember is currently | 200 // This method should only be used from the thread the PrefMember is currently |
| 199 // on, which is the UI thread by default. | 201 // on, which is the UI thread by default. |
| 200 void MoveToThread(const scoped_refptr<base::MessageLoopProxy>& message_loop) { | 202 void MoveToThread( |
| 203 const scoped_refptr<base::SingleThreadTaskRunner>& message_loop) { |
| 201 subtle::PrefMemberBase::MoveToThread(message_loop); | 204 subtle::PrefMemberBase::MoveToThread(message_loop); |
| 202 } | 205 } |
| 203 | 206 |
| 204 // Check whether the pref is managed, i.e. controlled externally through | 207 // Check whether the pref is managed, i.e. controlled externally through |
| 205 // enterprise configuration management (e.g. windows group policy). Returns | 208 // enterprise configuration management (e.g. windows group policy). Returns |
| 206 // false for unknown prefs. | 209 // false for unknown prefs. |
| 207 // This method should only be used from the thread the PrefMember is currently | 210 // This method should only be used from the thread the PrefMember is currently |
| 208 // on, which is the UI thread unless changed by |MoveToThread|. | 211 // on, which is the UI thread unless changed by |MoveToThread|. |
| 209 bool IsManaged() const { | 212 bool IsManaged() const { |
| 210 VerifyPref(); | 213 VerifyPref(); |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 | 346 |
| 344 typedef PrefMember<bool> BooleanPrefMember; | 347 typedef PrefMember<bool> BooleanPrefMember; |
| 345 typedef PrefMember<int> IntegerPrefMember; | 348 typedef PrefMember<int> IntegerPrefMember; |
| 346 typedef PrefMember<double> DoublePrefMember; | 349 typedef PrefMember<double> DoublePrefMember; |
| 347 typedef PrefMember<std::string> StringPrefMember; | 350 typedef PrefMember<std::string> StringPrefMember; |
| 348 typedef PrefMember<base::FilePath> FilePathPrefMember; | 351 typedef PrefMember<base::FilePath> FilePathPrefMember; |
| 349 // This preference member is expensive for large string arrays. | 352 // This preference member is expensive for large string arrays. |
| 350 typedef PrefMember<std::vector<std::string> > StringListPrefMember; | 353 typedef PrefMember<std::vector<std::string> > StringListPrefMember; |
| 351 | 354 |
| 352 #endif // BASE_PREFS_PREF_MEMBER_H_ | 355 #endif // BASE_PREFS_PREF_MEMBER_H_ |
| OLD | NEW |