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

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

Issue 5646003: Sanitize PrefStore interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix PrefService mock construction in PrefServiceTest to include command line store. Created 10 years 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) 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/ref_counted.h" 15 #include "base/ref_counted.h"
16 #include "base/scoped_ptr.h" 16 #include "base/scoped_ptr.h"
17 #include "base/values.h" 17 #include "base/values.h"
18 #include "chrome/common/pref_store.h"
19 18
20 class DefaultPrefStore; 19 class DefaultPrefStore;
21 class FilePath; 20 class FilePath;
22 class NotificationObserver; 21 class NotificationObserver;
22 class PersistentPrefStore;
23 class PrefChangeObserver; 23 class PrefChangeObserver;
24 class PrefNotifier; 24 class PrefNotifier;
25 class PrefNotifierImpl; 25 class PrefNotifierImpl;
26 class PrefStore;
26 class PrefValueStore; 27 class PrefValueStore;
27 class Profile; 28 class Profile;
28 29
29 namespace subtle { 30 namespace subtle {
30 class PrefMemberBase; 31 class PrefMemberBase;
31 }; 32 };
32 33
33 class PrefService : public NonThreadSafe { 34 class PrefService : public NonThreadSafe {
34 public: 35 public:
35 // A helper class to store all the information associated with a preference. 36 // A helper class to store all the information associated with a preference.
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 // Reference to the PrefService in which this pref was created. 95 // Reference to the PrefService in which this pref was created.
95 const PrefService* pref_service_; 96 const PrefService* pref_service_;
96 97
97 DISALLOW_COPY_AND_ASSIGN(Preference); 98 DISALLOW_COPY_AND_ASSIGN(Preference);
98 }; 99 };
99 100
100 // Factory method that creates a new instance of a PrefService with the 101 // Factory method that creates a new instance of a PrefService with the
101 // applicable PrefStores. The |pref_filename| points to the user preference 102 // applicable PrefStores. The |pref_filename| points to the user preference
102 // file. The |profile| is the one to which these preferences apply; it may be 103 // file. The |profile| is the one to which these preferences apply; it may be
103 // NULL if we're dealing with the local state. This is the usual way to create 104 // NULL if we're dealing with the local state. This is the usual way to create
104 // a new PrefService. 105 // a new PrefService. |extension_pref_store| is used as the source for
106 // extension-controlled preferences and may be NULL. The PrefService takes
107 // ownership of |extension_pref_store|.
105 static PrefService* CreatePrefService(const FilePath& pref_filename, 108 static PrefService* CreatePrefService(const FilePath& pref_filename,
109 PrefStore* extension_pref_store,
106 Profile* profile); 110 Profile* profile);
107 111
108 // Convenience factory method for use in unit tests. Creates a new
109 // PrefService that uses a PrefValueStore with user preferences at the given
110 // |pref_filename|, a default PrefStore, and no other PrefStores (i.e., no
111 // other types of preferences).
112 static PrefService* CreateUserPrefService(const FilePath& pref_filename);
113
114 virtual ~PrefService(); 112 virtual ~PrefService();
115 113
116 // Reloads the data from file. This should only be called when the importer 114 // Reloads the data from file. This should only be called when the importer
117 // is running during first run, and the main process may not change pref 115 // is running during first run, and the main process may not change pref
118 // values while the importer process is running. Returns true on success. 116 // values while the importer process is running. Returns true on success.
119 bool ReloadPersistentPrefs(); 117 bool ReloadPersistentPrefs();
120 118
121 // Returns true if the preference for the given preference name is available 119 // Returns true if the preference for the given preference name is available
122 // and is managed. 120 // and is managed.
123 bool IsManagedPreference(const char* pref_name) const; 121 bool IsManagedPreference(const char* pref_name) const;
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 // A helper method to quickly look up a preference. Returns NULL if the 214 // A helper method to quickly look up a preference. Returns NULL if the
217 // preference is not registered. 215 // preference is not registered.
218 const Preference* FindPreference(const char* pref_name) const; 216 const Preference* FindPreference(const char* pref_name) const;
219 217
220 bool ReadOnly() const; 218 bool ReadOnly() const;
221 219
222 // TODO(mnissler): This should not be public. Change client code to call a 220 // TODO(mnissler): This should not be public. Change client code to call a
223 // preference setter or use ScopedPrefUpdate. 221 // preference setter or use ScopedPrefUpdate.
224 PrefNotifier* pref_notifier() const; 222 PrefNotifier* pref_notifier() const;
225 223
226 // Get the extension PrefStore.
227 PrefStore* GetExtensionPrefStore();
228
229 protected: 224 protected:
230 // Construct a new pref service, specifying the pref sources as explicit 225 // Construct a new pref service, specifying the pref sources as explicit
231 // PrefStore pointers. This constructor is what CreatePrefService() ends up 226 // PrefStore pointers. This constructor is what CreatePrefService() ends up
232 // calling. It's also used for unit tests. 227 // calling. It's also used for unit tests.
233 PrefService(PrefStore* managed_platform_prefs, 228 PrefService(PrefStore* managed_platform_prefs,
234 PrefStore* device_management_prefs, 229 PrefStore* device_management_prefs,
235 PrefStore* extension_prefs, 230 PrefStore* extension_prefs,
236 PrefStore* command_line_prefs, 231 PrefStore* command_line_prefs,
237 PrefStore* user_prefs, 232 PersistentPrefStore* user_prefs,
238 PrefStore* recommended_prefs, 233 PrefStore* recommended_prefs,
239 Profile* profile); 234 Profile* profile);
240 235
241 // The PrefNotifier handles registering and notifying preference observers. 236 // The PrefNotifier handles registering and notifying preference observers.
242 // It is created and owned by this PrefService. Subclasses may access it for 237 // It is created and owned by this PrefService. Subclasses may access it for
243 // unit testing. 238 // unit testing.
244 scoped_ptr<PrefNotifierImpl> pref_notifier_; 239 scoped_ptr<PrefNotifierImpl> pref_notifier_;
245 240
246 private: 241 private:
247 friend class TestingPrefService; 242 friend class PrefServiceMockBuilder;
248 243
249 // Registration of pref change observers must be done using the 244 // Registration of pref change observers must be done using the
250 // PrefChangeRegistrar, which is declared as a friend here to grant it 245 // PrefChangeRegistrar, which is declared as a friend here to grant it
251 // access to the otherwise protected members Add/RemovePrefObserver. 246 // access to the otherwise protected members Add/RemovePrefObserver.
252 // PrefMember registers for preferences changes notification directly to 247 // PrefMember registers for preferences changes notification directly to
253 // avoid the storage overhead of the registrar, so its base class must be 248 // avoid the storage overhead of the registrar, so its base class must be
254 // declared as a friend, too. 249 // declared as a friend, too.
255 friend class PrefChangeRegistrar; 250 friend class PrefChangeRegistrar;
256 friend class subtle::PrefMemberBase; 251 friend class subtle::PrefMemberBase;
257 252
258 // If the pref at the given path changes, we call the observer's Observe 253 // If the pref at the given path changes, we call the observer's Observe
259 // method with PREF_CHANGED. Note that observers should not call these methods 254 // method with PREF_CHANGED. Note that observers should not call these methods
260 // directly but rather use a PrefChangeRegistrar to make sure the observer 255 // directly but rather use a PrefChangeRegistrar to make sure the observer
261 // gets cleaned up properly. 256 // gets cleaned up properly.
262 virtual void AddPrefObserver(const char* path, NotificationObserver* obs); 257 virtual void AddPrefObserver(const char* path, NotificationObserver* obs);
263 virtual void RemovePrefObserver(const char* path, NotificationObserver* obs); 258 virtual void RemovePrefObserver(const char* path, NotificationObserver* obs);
264 259
265 // Add a preference to the PreferenceMap. If the pref already exists, return 260 // Add a preference to the PreferenceMap. If the pref already exists, return
266 // false. This method takes ownership of |default_value|. 261 // false. This method takes ownership of |default_value|.
267 void RegisterPreference(const char* path, Value* default_value); 262 void RegisterPreference(const char* path, Value* default_value);
268 263
269 // Returns a copy of the current pref value. The caller is responsible for
270 // deleting the returned object.
271 Value* GetPrefCopy(const char* pref_name);
272
273 // Sets the value for this pref path in the user pref store and informs the 264 // Sets the value for this pref path in the user pref store and informs the
274 // PrefNotifier of the change. 265 // PrefNotifier of the change.
275 void SetUserPrefValue(const char* path, Value* new_value); 266 void SetUserPrefValue(const char* path, Value* new_value);
276 267
277 // Load from disk. Returns a non-zero error code on failure.
278 PrefStore::PrefReadError LoadPersistentPrefs();
279
280 // Load preferences from storage, attempting to diagnose and handle errors. 268 // Load preferences from storage, attempting to diagnose and handle errors.
281 // This should only be called from the constructor. 269 // This should only be called from the constructor.
282 void InitFromStorage(); 270 void InitFromStorage();
283 271
284 // The PrefValueStore provides prioritized preference values. It is created 272 // The PrefValueStore provides prioritized preference values. It is created
285 // and owned by this PrefService. Subclasses may access it for unit testing. 273 // and owned by this PrefService. Subclasses may access it for unit testing.
286 scoped_refptr<PrefValueStore> pref_value_store_; 274 scoped_refptr<PrefValueStore> pref_value_store_;
287 275
288 // The extension pref store registered with the PrefValueStore. 276 // The persistent pref store used for actual user data.
289 PrefStore* extension_store_; 277 PersistentPrefStore* user_pref_store_;
290 278
291 // Points to the default pref store we passed to the PrefValueStore. 279 // Points to the default pref store we passed to the PrefValueStore.
292 PrefStore* default_store_; 280 DefaultPrefStore* default_store_;
293 281
294 // A set of all the registered Preference objects. 282 // A set of all the registered Preference objects.
295 PreferenceSet prefs_; 283 PreferenceSet prefs_;
296 284
297 DISALLOW_COPY_AND_ASSIGN(PrefService); 285 DISALLOW_COPY_AND_ASSIGN(PrefService);
298 }; 286 };
299 287
300 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ 288 #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.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698