OLD | NEW |
1 // Copyright (c) 2010 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 // 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> |
(...skipping 23 matching lines...) Expand all Loading... |
35 public: | 35 public: |
36 // 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. |
37 class Preference { | 37 class Preference { |
38 public: | 38 public: |
39 | 39 |
40 // The type of the preference is determined by the type with which it is | 40 // The type of the preference is determined by the type with which it is |
41 // registered. This type needs to be a boolean, integer, real, string, | 41 // registered. This type needs to be a boolean, integer, real, string, |
42 // dictionary (a branch), or list. You shouldn't need to construct this on | 42 // dictionary (a branch), or list. You shouldn't need to construct this on |
43 // your own; use the PrefService::Register*Pref methods instead. | 43 // your own; use the PrefService::Register*Pref methods instead. |
44 Preference(const PrefService* service, | 44 Preference(const PrefService* service, |
45 const char* name); | 45 const char* name, |
| 46 Value::ValueType type); |
46 ~Preference() {} | 47 ~Preference() {} |
47 | 48 |
48 // Returns the name of the Preference (i.e., the key, e.g., | 49 // Returns the name of the Preference (i.e., the key, e.g., |
49 // browser.window_placement). | 50 // browser.window_placement). |
50 const std::string name() const { return name_; } | 51 const std::string name() const { return name_; } |
51 | 52 |
52 // Returns the registered type of the preference. | 53 // Returns the registered type of the preference. |
53 Value::ValueType GetType() const; | 54 Value::ValueType GetType() const; |
54 | 55 |
55 // Returns the value of the Preference, falling back to the registered | 56 // Returns the value of the Preference, falling back to the registered |
(...skipping 29 matching lines...) Expand all Loading... |
85 // Returns true if the user can change the Preference value, which is the | 86 // Returns true if the user can change the Preference value, which is the |
86 // case if no higher-priority source than the user store controls the | 87 // case if no higher-priority source than the user store controls the |
87 // Preference. | 88 // Preference. |
88 bool IsUserModifiable() const; | 89 bool IsUserModifiable() const; |
89 | 90 |
90 private: | 91 private: |
91 friend class PrefService; | 92 friend class PrefService; |
92 | 93 |
93 std::string name_; | 94 std::string name_; |
94 | 95 |
| 96 Value::ValueType type_; |
| 97 |
95 // Reference to the PrefService in which this pref was created. | 98 // Reference to the PrefService in which this pref was created. |
96 const PrefService* pref_service_; | 99 const PrefService* pref_service_; |
97 | 100 |
98 DISALLOW_COPY_AND_ASSIGN(Preference); | 101 DISALLOW_COPY_AND_ASSIGN(Preference); |
99 }; | 102 }; |
100 | 103 |
101 // Factory method that creates a new instance of a PrefService with the | 104 // Factory method that creates a new instance of a PrefService with the |
102 // applicable PrefStores. The |pref_filename| points to the user preference | 105 // applicable PrefStores. The |pref_filename| points to the user preference |
103 // file. The |profile| is the one to which these preferences apply; it may be | 106 // file. The |profile| is the one to which these preferences apply; it may be |
104 // NULL if we're dealing with the local state. This is the usual way to create | 107 // NULL if we're dealing with the local state. This is the usual way to create |
105 // a new PrefService. |extension_pref_store| is used as the source for | 108 // a new PrefService. |extension_pref_store| is used as the source for |
106 // extension-controlled preferences and may be NULL. The PrefService takes | 109 // extension-controlled preferences and may be NULL. The PrefService takes |
107 // ownership of |extension_pref_store|. | 110 // ownership of |extension_pref_store|. |
108 static PrefService* CreatePrefService(const FilePath& pref_filename, | 111 static PrefService* CreatePrefService(const FilePath& pref_filename, |
109 PrefStore* extension_pref_store, | 112 PrefStore* extension_pref_store, |
110 Profile* profile); | 113 Profile* profile); |
111 | 114 |
| 115 // Creates an incognito copy of the pref service that shares most pref stores |
| 116 // but uses a fresh non-persistent overlay for the user pref store and an |
| 117 // individual extension pref store (to cache the effective extension prefs for |
| 118 // incognito windows). |
| 119 PrefService* CreateIncognitoPrefService(PrefStore* incognito_extension_prefs); |
| 120 |
112 virtual ~PrefService(); | 121 virtual ~PrefService(); |
113 | 122 |
114 // Reloads the data from file. This should only be called when the importer | 123 // Reloads the data from file. This should only be called when the importer |
115 // is running during first run, and the main process may not change pref | 124 // is running during first run, and the main process may not change pref |
116 // values while the importer process is running. Returns true on success. | 125 // values while the importer process is running. Returns true on success. |
117 bool ReloadPersistentPrefs(); | 126 bool ReloadPersistentPrefs(); |
118 | 127 |
119 // Returns true if the preference for the given preference name is available | 128 // Returns true if the preference for the given preference name is available |
120 // and is managed. | 129 // and is managed. |
121 bool IsManagedPreference(const char* pref_name) const; | 130 bool IsManagedPreference(const char* pref_name) const; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 | 232 |
224 protected: | 233 protected: |
225 // Construct a new pref service, specifying the pref sources as explicit | 234 // Construct a new pref service, specifying the pref sources as explicit |
226 // PrefStore pointers. This constructor is what CreatePrefService() ends up | 235 // PrefStore pointers. This constructor is what CreatePrefService() ends up |
227 // calling. It's also used for unit tests. | 236 // calling. It's also used for unit tests. |
228 PrefService(PrefStore* managed_platform_prefs, | 237 PrefService(PrefStore* managed_platform_prefs, |
229 PrefStore* device_management_prefs, | 238 PrefStore* device_management_prefs, |
230 PrefStore* extension_prefs, | 239 PrefStore* extension_prefs, |
231 PrefStore* command_line_prefs, | 240 PrefStore* command_line_prefs, |
232 PersistentPrefStore* user_prefs, | 241 PersistentPrefStore* user_prefs, |
233 PrefStore* recommended_prefs); | 242 PrefStore* recommended_prefs, |
| 243 DefaultPrefStore* default_store); |
234 | 244 |
235 // The PrefNotifier handles registering and notifying preference observers. | 245 // The PrefNotifier handles registering and notifying preference observers. |
236 // It is created and owned by this PrefService. Subclasses may access it for | 246 // It is created and owned by this PrefService. Subclasses may access it for |
237 // unit testing. | 247 // unit testing. |
238 scoped_ptr<PrefNotifierImpl> pref_notifier_; | 248 scoped_ptr<PrefNotifierImpl> pref_notifier_; |
239 | 249 |
240 private: | 250 private: |
241 friend class PrefServiceMockBuilder; | 251 friend class PrefServiceMockBuilder; |
242 | 252 |
243 // Registration of pref change observers must be done using the | 253 // Registration of pref change observers must be done using the |
244 // PrefChangeRegistrar, which is declared as a friend here to grant it | 254 // PrefChangeRegistrar, which is declared as a friend here to grant it |
245 // access to the otherwise protected members Add/RemovePrefObserver. | 255 // access to the otherwise protected members Add/RemovePrefObserver. |
246 // PrefMember registers for preferences changes notification directly to | 256 // PrefMember registers for preferences changes notification directly to |
247 // avoid the storage overhead of the registrar, so its base class must be | 257 // avoid the storage overhead of the registrar, so its base class must be |
248 // declared as a friend, too. | 258 // declared as a friend, too. |
249 friend class PrefChangeRegistrar; | 259 friend class PrefChangeRegistrar; |
250 friend class subtle::PrefMemberBase; | 260 friend class subtle::PrefMemberBase; |
251 | 261 |
| 262 // Construct an incognito version of the pref service. Use |
| 263 // CreateIncognitoPrefService() instead of calling this constructor directly. |
| 264 PrefService(const PrefService& original, |
| 265 PrefStore* incognito_extension_prefs); |
| 266 |
252 // If the pref at the given path changes, we call the observer's Observe | 267 // If the pref at the given path changes, we call the observer's Observe |
253 // method with PREF_CHANGED. Note that observers should not call these methods | 268 // method with PREF_CHANGED. Note that observers should not call these methods |
254 // directly but rather use a PrefChangeRegistrar to make sure the observer | 269 // directly but rather use a PrefChangeRegistrar to make sure the observer |
255 // gets cleaned up properly. | 270 // gets cleaned up properly. |
256 virtual void AddPrefObserver(const char* path, NotificationObserver* obs); | 271 virtual void AddPrefObserver(const char* path, NotificationObserver* obs); |
257 virtual void RemovePrefObserver(const char* path, NotificationObserver* obs); | 272 virtual void RemovePrefObserver(const char* path, NotificationObserver* obs); |
258 | 273 |
259 // Add a preference to the PreferenceMap. If the pref already exists, return | 274 // Add a preference to the PreferenceMap. If the pref already exists, return |
260 // false. This method takes ownership of |default_value|. | 275 // false. This method takes ownership of |default_value|. |
261 void RegisterPreference(const char* path, Value* default_value); | 276 void RegisterPreference(const char* path, Value* default_value); |
262 | 277 |
263 // Sets the value for this pref path in the user pref store and informs the | 278 // Sets the value for this pref path in the user pref store and informs the |
264 // PrefNotifier of the change. | 279 // PrefNotifier of the change. |
265 void SetUserPrefValue(const char* path, Value* new_value); | 280 void SetUserPrefValue(const char* path, Value* new_value); |
266 | 281 |
267 // Load preferences from storage, attempting to diagnose and handle errors. | 282 // Load preferences from storage, attempting to diagnose and handle errors. |
268 // This should only be called from the constructor. | 283 // This should only be called from the constructor. |
269 void InitFromStorage(); | 284 void InitFromStorage(); |
270 | 285 |
271 // The PrefValueStore provides prioritized preference values. It is created | 286 // The PrefValueStore provides prioritized preference values. It is created |
272 // and owned by this PrefService. Subclasses may access it for unit testing. | 287 // and owned by this PrefService. Subclasses may access it for unit testing. |
273 scoped_refptr<PrefValueStore> pref_value_store_; | 288 scoped_refptr<PrefValueStore> pref_value_store_; |
274 | 289 |
275 // The persistent pref store used for actual user data. | 290 // Pref Stores and profile that we passed to the PrefValueStore. |
276 PersistentPrefStore* user_pref_store_; | 291 scoped_refptr<PersistentPrefStore> user_pref_store_; |
| 292 scoped_refptr<DefaultPrefStore> default_store_; |
277 | 293 |
278 // Points to the default pref store we passed to the PrefValueStore. | 294 // Local cache of registered Preference objects. The default_store_ |
279 DefaultPrefStore* default_store_; | 295 // is authoritative with respect to what the types and default values |
280 | 296 // of registered preferences are. |
281 // A set of all the registered Preference objects. | 297 mutable PreferenceSet prefs_; |
282 PreferenceSet prefs_; | |
283 | 298 |
284 DISALLOW_COPY_AND_ASSIGN(PrefService); | 299 DISALLOW_COPY_AND_ASSIGN(PrefService); |
285 }; | 300 }; |
286 | 301 |
287 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ | 302 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ |
OLD | NEW |