| OLD | NEW |
| 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" | 18 #include "chrome/common/pref_store.h" |
| 19 | 19 |
| 20 class DefaultPrefStore; |
| 20 class FilePath; | 21 class FilePath; |
| 21 class NotificationObserver; | 22 class NotificationObserver; |
| 22 class PrefChangeObserver; | 23 class PrefChangeObserver; |
| 23 class PrefNotifier; | 24 class PrefNotifier; |
| 25 class PrefNotifierImpl; |
| 24 class PrefValueStore; | 26 class PrefValueStore; |
| 25 class Profile; | 27 class Profile; |
| 26 | 28 |
| 27 namespace subtle { | 29 namespace subtle { |
| 28 class PrefMemberBase; | 30 class PrefMemberBase; |
| 29 }; | 31 }; |
| 30 | 32 |
| 31 class PrefService : public NonThreadSafe { | 33 class PrefService : public NonThreadSafe { |
| 32 public: | 34 public: |
| 33 // A helper class to store all the information associated with a preference. | 35 // A helper class to store all the information associated with a preference. |
| 34 class Preference { | 36 class Preference { |
| 35 public: | 37 public: |
| 36 | 38 |
| 37 // The type of the preference is determined by the type with which it is | 39 // The type of the preference is determined by the type with which it is |
| 38 // registered. This type needs to be a boolean, integer, real, string, | 40 // registered. This type needs to be a boolean, integer, real, string, |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 friend class PrefService; | 90 friend class PrefService; |
| 89 | 91 |
| 90 std::string name_; | 92 std::string name_; |
| 91 | 93 |
| 92 // Reference to the PrefService in which this pref was created. | 94 // Reference to the PrefService in which this pref was created. |
| 93 const PrefService* pref_service_; | 95 const PrefService* pref_service_; |
| 94 | 96 |
| 95 DISALLOW_COPY_AND_ASSIGN(Preference); | 97 DISALLOW_COPY_AND_ASSIGN(Preference); |
| 96 }; | 98 }; |
| 97 | 99 |
| 98 // Factory method that creates a new instance of a PrefService with | 100 // Factory method that creates a new instance of a PrefService with the |
| 99 // a PrefValueStore containing all platform-applicable PrefStores. | 101 // applicable PrefStores. The |pref_filename| points to the user preference |
| 100 // The |pref_filename| points to the user preference file. The |profile| is | 102 // file. The |profile| is the one to which these preferences apply; it may be |
| 101 // the one to which these preferences apply; it may be NULL if we're dealing | 103 // NULL if we're dealing with the local state. This is the usual way to create |
| 102 // with the local state. This is the usual way to create a new PrefService. | 104 // a new PrefService. |
| 103 static PrefService* CreatePrefService(const FilePath& pref_filename, | 105 static PrefService* CreatePrefService(const FilePath& pref_filename, |
| 104 Profile* profile); | 106 Profile* profile); |
| 105 | 107 |
| 106 // Convenience factory method for use in unit tests. Creates a new | 108 // Convenience factory method for use in unit tests. Creates a new |
| 107 // PrefService that uses a PrefValueStore with user preferences at the given | 109 // PrefService that uses a PrefValueStore with user preferences at the given |
| 108 // |pref_filename|, a default PrefStore, and no other PrefStores (i.e., no | 110 // |pref_filename|, a default PrefStore, and no other PrefStores (i.e., no |
| 109 // other types of preferences). | 111 // other types of preferences). |
| 110 static PrefService* CreateUserPrefService(const FilePath& pref_filename); | 112 static PrefService* CreateUserPrefService(const FilePath& pref_filename); |
| 111 | 113 |
| 112 // This constructor is primarily used by tests. The |pref_value_store| | |
| 113 // provides preference values. | |
| 114 explicit PrefService(PrefValueStore* pref_value_store); | |
| 115 | |
| 116 virtual ~PrefService(); | 114 virtual ~PrefService(); |
| 117 | 115 |
| 118 // Reloads the data from file. This should only be called when the importer | 116 // Reloads the data from file. This should only be called when the importer |
| 119 // is running during first run, and the main process may not change pref | 117 // is running during first run, and the main process may not change pref |
| 120 // values while the importer process is running. Returns true on success. | 118 // values while the importer process is running. Returns true on success. |
| 121 bool ReloadPersistentPrefs(); | 119 bool ReloadPersistentPrefs(); |
| 122 | 120 |
| 123 // Returns true if the preference for the given preference name is available | 121 // Returns true if the preference for the given preference name is available |
| 124 // and is managed. | 122 // and is managed. |
| 125 bool IsManagedPreference(const char* pref_name) const; | 123 bool IsManagedPreference(const char* pref_name) const; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 }; | 212 }; |
| 215 typedef std::set<Preference*, PreferencePathComparator> PreferenceSet; | 213 typedef std::set<Preference*, PreferencePathComparator> PreferenceSet; |
| 216 const PreferenceSet& preference_set() const { return prefs_; } | 214 const PreferenceSet& preference_set() const { return prefs_; } |
| 217 | 215 |
| 218 // A helper method to quickly look up a preference. Returns NULL if the | 216 // A helper method to quickly look up a preference. Returns NULL if the |
| 219 // preference is not registered. | 217 // preference is not registered. |
| 220 const Preference* FindPreference(const char* pref_name) const; | 218 const Preference* FindPreference(const char* pref_name) const; |
| 221 | 219 |
| 222 bool ReadOnly() const; | 220 bool ReadOnly() const; |
| 223 | 221 |
| 224 PrefNotifier* pref_notifier() const { return pref_notifier_.get(); } | 222 // TODO(mnissler): This should not be public. Change client code to call a |
| 225 | 223 // preference setter or use ScopedPrefUpdate. |
| 226 PrefValueStore* pref_value_store() const { return pref_value_store_.get(); } | 224 PrefNotifier* pref_notifier() const; |
| 227 | 225 |
| 228 protected: | 226 protected: |
| 227 // Construct a new pref service, specifying the pref sources as explicit |
| 228 // PrefStore pointers. This constructor is what CreatePrefService() ends up |
| 229 // calling. It's also used for unit tests. |
| 230 PrefService(PrefStore* managed_platform_prefs, |
| 231 PrefStore* device_management_prefs, |
| 232 PrefStore* extension_prefs, |
| 233 PrefStore* command_line_prefs, |
| 234 PrefStore* user_prefs, |
| 235 PrefStore* recommended_prefs, |
| 236 Profile* profile); |
| 237 |
| 229 // The PrefNotifier handles registering and notifying preference observers. | 238 // The PrefNotifier handles registering and notifying preference observers. |
| 230 // It is created and owned by this PrefService. Subclasses may access it for | 239 // It is created and owned by this PrefService. Subclasses may access it for |
| 231 // unit testing. | 240 // unit testing. |
| 232 scoped_ptr<PrefNotifier> pref_notifier_; | 241 scoped_ptr<PrefNotifierImpl> pref_notifier_; |
| 233 | 242 |
| 234 private: | 243 private: |
| 244 friend class TestingPrefService; |
| 245 |
| 235 // Registration of pref change observers must be done using the | 246 // Registration of pref change observers must be done using the |
| 236 // PrefChangeRegistrar, which is declared as a friend here to grant it | 247 // PrefChangeRegistrar, which is declared as a friend here to grant it |
| 237 // access to the otherwise protected members Add/RemovePrefObserver. | 248 // access to the otherwise protected members Add/RemovePrefObserver. |
| 238 // PrefMember registers for preferences changes notification directly to | 249 // PrefMember registers for preferences changes notification directly to |
| 239 // avoid the storage overhead of the registrar, so its base class must be | 250 // avoid the storage overhead of the registrar, so its base class must be |
| 240 // declared as a friend, too. | 251 // declared as a friend, too. |
| 241 friend class PrefChangeRegistrar; | 252 friend class PrefChangeRegistrar; |
| 242 friend class subtle::PrefMemberBase; | 253 friend class subtle::PrefMemberBase; |
| 243 | 254 |
| 244 // If the pref at the given path changes, we call the observer's Observe | 255 // If the pref at the given path changes, we call the observer's Observe |
| (...skipping 19 matching lines...) Expand all Loading... |
| 264 PrefStore::PrefReadError LoadPersistentPrefs(); | 275 PrefStore::PrefReadError LoadPersistentPrefs(); |
| 265 | 276 |
| 266 // Load preferences from storage, attempting to diagnose and handle errors. | 277 // Load preferences from storage, attempting to diagnose and handle errors. |
| 267 // This should only be called from the constructor. | 278 // This should only be called from the constructor. |
| 268 void InitFromStorage(); | 279 void InitFromStorage(); |
| 269 | 280 |
| 270 // The PrefValueStore provides prioritized preference values. It is created | 281 // The PrefValueStore provides prioritized preference values. It is created |
| 271 // and owned by this PrefService. Subclasses may access it for unit testing. | 282 // and owned by this PrefService. Subclasses may access it for unit testing. |
| 272 scoped_refptr<PrefValueStore> pref_value_store_; | 283 scoped_refptr<PrefValueStore> pref_value_store_; |
| 273 | 284 |
| 285 // Points to the default pref store we passed to the PrefValueStore. |
| 286 DefaultPrefStore* default_store_; |
| 287 |
| 274 // A set of all the registered Preference objects. | 288 // A set of all the registered Preference objects. |
| 275 PreferenceSet prefs_; | 289 PreferenceSet prefs_; |
| 276 | 290 |
| 277 DISALLOW_COPY_AND_ASSIGN(PrefService); | 291 DISALLOW_COPY_AND_ASSIGN(PrefService); |
| 278 }; | 292 }; |
| 279 | 293 |
| 280 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ | 294 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ |
| OLD | NEW |