Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: chrome/browser/api/prefs/pref_member.h

Issue 11345008: Remove content::NotificationObserver dependency from most Prefs code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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. 160 // function, the PrefMember may not be used any more.
162 // This method should only be called on the UI thread. 161 // This method should only be called on the UI thread.
163 void Destroy() { 162 void Destroy() {
164 subtle::PrefMemberBase::Destroy(); 163 subtle::PrefMemberBase::Destroy();
165 } 164 }
166 165
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 256
258 typedef PrefMember<bool> BooleanPrefMember; 257 typedef PrefMember<bool> BooleanPrefMember;
259 typedef PrefMember<int> IntegerPrefMember; 258 typedef PrefMember<int> IntegerPrefMember;
260 typedef PrefMember<double> DoublePrefMember; 259 typedef PrefMember<double> DoublePrefMember;
261 typedef PrefMember<std::string> StringPrefMember; 260 typedef PrefMember<std::string> StringPrefMember;
262 typedef PrefMember<FilePath> FilePathPrefMember; 261 typedef PrefMember<FilePath> FilePathPrefMember;
263 // This preference member is expensive for large string arrays. 262 // This preference member is expensive for large string arrays.
264 typedef PrefMember<std::vector<std::string> > StringListPrefMember; 263 typedef PrefMember<std::vector<std::string> > StringListPrefMember;
265 264
266 #endif // CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ 265 #endif // CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698