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

Side by Side Diff: chrome/browser/prefs/pref_service.h

Issue 3304015: Use PrefChangeRegistrar everywhere (Closed)
Patch Set: final version for commit Created 10 years, 2 months 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
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 // This provides a way to access the application's current preferences. 5 // This provides a way to access the application's current preferences.
6 6
7 #ifndef CHROME_BROWSER_PREFS_PREF_SERVICE_H_ 7 #ifndef CHROME_BROWSER_PREFS_PREF_SERVICE_H_
8 #define CHROME_BROWSER_PREFS_PREF_SERVICE_H_ 8 #define CHROME_BROWSER_PREFS_PREF_SERVICE_H_
9 #pragma once 9 #pragma once
10 10
11 #include <set> 11 #include <set>
12 #include <string> 12 #include <string>
13 13
14 #include "base/non_thread_safe.h" 14 #include "base/non_thread_safe.h"
15 #include "base/scoped_ptr.h" 15 #include "base/scoped_ptr.h"
16 #include "base/values.h" 16 #include "base/values.h"
17 #include "chrome/browser/prefs/pref_value_store.h" 17 #include "chrome/browser/prefs/pref_value_store.h"
18 #include "chrome/common/pref_store.h" 18 #include "chrome/common/pref_store.h"
19 19
20 class FilePath; 20 class FilePath;
21 class NotificationObserver; 21 class NotificationObserver;
22 class PrefChangeObserver;
22 class PrefNotifier; 23 class PrefNotifier;
23 class Profile; 24 class Profile;
24 25
26 namespace subtle {
27 class PrefMemberBase;
28 };
29
25 class PrefService : public NonThreadSafe { 30 class PrefService : public NonThreadSafe {
26 public: 31 public:
27 // A helper class to store all the information associated with a preference. 32 // A helper class to store all the information associated with a preference.
28 class Preference { 33 class Preference {
29 public: 34 public:
30 35
31 // The type of the preference is determined by the type with which it is 36 // The type of the preference is determined by the type with which it is
32 // registered. This type needs to be a boolean, integer, real, string, 37 // registered. This type needs to be a boolean, integer, real, string,
33 // dictionary (a branch), or list. You shouldn't need to construct this on 38 // dictionary (a branch), or list. You shouldn't need to construct this on
34 // your own; use the PrefService::Register*Pref methods instead. 39 // your own; use the PrefService::Register*Pref methods instead.
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 int GetInteger(const char* path) const; 159 int GetInteger(const char* path) const;
155 double GetReal(const char* path) const; 160 double GetReal(const char* path) const;
156 std::string GetString(const char* path) const; 161 std::string GetString(const char* path) const;
157 FilePath GetFilePath(const char* path) const; 162 FilePath GetFilePath(const char* path) const;
158 163
159 // Returns the branch if it exists. If it's not a branch or the branch does 164 // Returns the branch if it exists. If it's not a branch or the branch does
160 // not exist, returns NULL. 165 // not exist, returns NULL.
161 const DictionaryValue* GetDictionary(const char* path) const; 166 const DictionaryValue* GetDictionary(const char* path) const;
162 const ListValue* GetList(const char* path) const; 167 const ListValue* GetList(const char* path) const;
163 168
164 // If the pref at the given path changes, we call the observer's Observe
165 // method with NOTIFY_PREF_CHANGED.
166 virtual void AddPrefObserver(const char* path, NotificationObserver* obs);
167 virtual void RemovePrefObserver(const char* path, NotificationObserver* obs);
168
169 // Removes a user pref and restores the pref to its default value. 169 // Removes a user pref and restores the pref to its default value.
170 void ClearPref(const char* path); 170 void ClearPref(const char* path);
171 171
172 // If the path is valid (i.e., registered), update the pref value in the user 172 // If the path is valid (i.e., registered), update the pref value in the user
173 // prefs. Seting a null value on a preference of List or Dictionary type is 173 // prefs. Seting a null value on a preference of List or Dictionary type is
174 // equivalent to removing the user value for that preference, allowing the 174 // equivalent to removing the user value for that preference, allowing the
175 // default value to take effect unless another value takes precedence. 175 // default value to take effect unless another value takes precedence.
176 void Set(const char* path, const Value& value); 176 void Set(const char* path, const Value& value);
177 void SetBoolean(const char* path, bool value); 177 void SetBoolean(const char* path, bool value);
178 void SetInteger(const char* path, int value); 178 void SetInteger(const char* path, int value);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 222
223 PrefValueStore* pref_value_store() const { return pref_value_store_.get(); } 223 PrefValueStore* pref_value_store() const { return pref_value_store_.get(); }
224 224
225 protected: 225 protected:
226 // The PrefNotifier handles registering and notifying preference observers. 226 // The PrefNotifier handles registering and notifying preference observers.
227 // It is created and owned by this PrefService. Subclasses may access it for 227 // It is created and owned by this PrefService. Subclasses may access it for
228 // unit testing. 228 // unit testing.
229 scoped_ptr<PrefNotifier> pref_notifier_; 229 scoped_ptr<PrefNotifier> pref_notifier_;
230 230
231 private: 231 private:
232 // Registration of pref change observers must be done using the
233 // PrefChangeRegistrar, which is declared as a friend here to grant it
234 // access to the otherwise protected members Add/RemovePrefObserver.
235 // PrefMember registers for preferences changes notification directly to
236 // avoid the storage overhead of the registrar, so its base class must be
237 // declared as a friend, too.
238 friend class PrefChangeRegistrar;
239 friend class subtle::PrefMemberBase;
240
241 // If the pref at the given path changes, we call the observer's Observe
242 // method with NOTIFY_PREF_CHANGED. Note that observers should not call
243 // these methods directly but rather use a PrefChangeRegistrar to make sure
244 // the observer gets cleaned up properly.
245 virtual void AddPrefObserver(const char* path, NotificationObserver* obs);
246 virtual void RemovePrefObserver(const char* path, NotificationObserver* obs);
247
232 // Add a preference to the PreferenceMap. If the pref already exists, return 248 // Add a preference to the PreferenceMap. If the pref already exists, return
233 // false. This method takes ownership of |default_value|. 249 // false. This method takes ownership of |default_value|.
234 void RegisterPreference(const char* path, Value* default_value); 250 void RegisterPreference(const char* path, Value* default_value);
235 251
236 // Returns a copy of the current pref value. The caller is responsible for 252 // Returns a copy of the current pref value. The caller is responsible for
237 // deleting the returned object. 253 // deleting the returned object.
238 Value* GetPrefCopy(const char* pref_name); 254 Value* GetPrefCopy(const char* pref_name);
239 255
240 // Sets the value for this pref path in the user pref store and informs the 256 // Sets the value for this pref path in the user pref store and informs the
241 // PrefNotifier of the change. 257 // PrefNotifier of the change.
(...skipping 10 matching lines...) Expand all
252 // and owned by this PrefService. Subclasses may access it for unit testing. 268 // and owned by this PrefService. Subclasses may access it for unit testing.
253 scoped_refptr<PrefValueStore> pref_value_store_; 269 scoped_refptr<PrefValueStore> pref_value_store_;
254 270
255 // A set of all the registered Preference objects. 271 // A set of all the registered Preference objects.
256 PreferenceSet prefs_; 272 PreferenceSet prefs_;
257 273
258 DISALLOW_COPY_AND_ASSIGN(PrefService); 274 DISALLOW_COPY_AND_ASSIGN(PrefService);
259 }; 275 };
260 276
261 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ 277 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_
OLDNEW
« no previous file with comments | « chrome/browser/prefs/pref_change_registrar_unittest.cc ('k') | chrome/browser/prefs/pref_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698