| OLD | NEW |
| 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 // 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 // Chromium settings and storage represent user-selected preferences and | 7 // Chromium settings and storage represent user-selected preferences and |
| 8 // information and MUST not be extracted, overwritten or modified except | 8 // information and MUST not be extracted, overwritten or modified except |
| 9 // through Chromium defined APIs. | 9 // through Chromium defined APIs. |
| 10 | 10 |
| 11 #ifndef CHROME_BROWSER_PREFS_PREF_SERVICE_H_ | 11 #ifndef CHROME_BROWSER_PREFS_PREF_SERVICE_H_ |
| 12 #define CHROME_BROWSER_PREFS_PREF_SERVICE_H_ | 12 #define CHROME_BROWSER_PREFS_PREF_SERVICE_H_ |
| 13 | 13 |
| 14 #include <set> | 14 #include <set> |
| 15 #include <string> | 15 #include <string> |
| 16 | 16 |
| 17 #include "base/callback.h" | 17 #include "base/callback.h" |
| 18 #include "base/hash_tables.h" |
| 18 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
| 19 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
| 20 #include "base/hash_tables.h" | |
| 21 #include "base/observer_list.h" | 21 #include "base/observer_list.h" |
| 22 #include "base/prefs/persistent_pref_store.h" | 22 #include "base/prefs/persistent_pref_store.h" |
| 23 #include "base/prefs/public/pref_service_base.h" | 23 #include "base/prefs/public/pref_service_base.h" |
| 24 #include "base/threading/non_thread_safe.h" | 24 #include "base/threading/non_thread_safe.h" |
| 25 | 25 |
| 26 class DefaultPrefStore; | |
| 27 class PrefNotifier; | 26 class PrefNotifier; |
| 28 class PrefNotifierImpl; | 27 class PrefNotifierImpl; |
| 29 class PrefObserver; | 28 class PrefObserver; |
| 29 class PrefRegistry; |
| 30 class PrefValueStore; |
| 30 class PrefStore; | 31 class PrefStore; |
| 31 class PrefValueStore; | |
| 32 | 32 |
| 33 namespace subtle { | 33 namespace subtle { |
| 34 class ScopedUserPrefUpdateBase; | 34 class ScopedUserPrefUpdateBase; |
| 35 }; | 35 }; |
| 36 | 36 |
| 37 // Base class for PrefServices. You can use the base class to read and | 37 // Base class for PrefServices. You can use the base class to read and |
| 38 // interact with preferences, but not to register new preferences; for | 38 // interact with preferences, but not to register new preferences; for |
| 39 // that see subclasses like PrefServiceSimple. | 39 // that see e.g. PrefRegistrySimple. |
| 40 class PrefService : public PrefServiceBase, public base::NonThreadSafe { | 40 class PrefService : public PrefServiceBase, public base::NonThreadSafe { |
| 41 public: | 41 public: |
| 42 enum PrefInitializationStatus { | 42 enum PrefInitializationStatus { |
| 43 INITIALIZATION_STATUS_WAITING, | 43 INITIALIZATION_STATUS_WAITING, |
| 44 INITIALIZATION_STATUS_SUCCESS, | 44 INITIALIZATION_STATUS_SUCCESS, |
| 45 INITIALIZATION_STATUS_CREATED_NEW_PROFILE, | 45 INITIALIZATION_STATUS_CREATED_NEW_PROFILE, |
| 46 INITIALIZATION_STATUS_ERROR | 46 INITIALIZATION_STATUS_ERROR |
| 47 }; | 47 }; |
| 48 | 48 |
| 49 // A helper class to store all the information associated with a preference. | 49 // A helper class to store all the information associated with a preference. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 // Reference to the PrefService in which this pref was created. | 87 // Reference to the PrefService in which this pref was created. |
| 88 const PrefService* pref_service_; | 88 const PrefService* pref_service_; |
| 89 }; | 89 }; |
| 90 | 90 |
| 91 // You may wish to use PrefServiceBuilder or one of its subclasses | 91 // You may wish to use PrefServiceBuilder or one of its subclasses |
| 92 // for simplified construction. | 92 // for simplified construction. |
| 93 PrefService( | 93 PrefService( |
| 94 PrefNotifierImpl* pref_notifier, | 94 PrefNotifierImpl* pref_notifier, |
| 95 PrefValueStore* pref_value_store, | 95 PrefValueStore* pref_value_store, |
| 96 PersistentPrefStore* user_prefs, | 96 PersistentPrefStore* user_prefs, |
| 97 DefaultPrefStore* default_store, | 97 PrefRegistry* pref_registry, |
| 98 base::Callback<void(PersistentPrefStore::PrefReadError)> | 98 base::Callback<void(PersistentPrefStore::PrefReadError)> |
| 99 read_error_callback, | 99 read_error_callback, |
| 100 bool async); | 100 bool async); |
| 101 virtual ~PrefService(); | 101 virtual ~PrefService(); |
| 102 | 102 |
| 103 // Reloads the data from file. This should only be called when the importer | 103 // Reloads the data from file. This should only be called when the importer |
| 104 // is running during first run, and the main process may not change pref | 104 // is running during first run, and the main process may not change pref |
| 105 // values while the importer process is running. Returns true on success. | 105 // values while the importer process is running. Returns true on success. |
| 106 bool ReloadPersistentPrefs(); | 106 bool ReloadPersistentPrefs(); |
| 107 | 107 |
| 108 // Lands pending writes to disk. This should only be used if we need to save | 108 // Lands pending writes to disk. This should only be used if we need to save |
| 109 // immediately (basically, during shutdown). | 109 // immediately (basically, during shutdown). |
| 110 void CommitPendingWrite(); | 110 void CommitPendingWrite(); |
| 111 | 111 |
| 112 // PrefServiceBase implementation. | 112 // PrefServiceBase implementation. |
| 113 virtual bool IsManagedPreference(const char* pref_name) const OVERRIDE; | 113 virtual bool IsManagedPreference(const char* pref_name) const OVERRIDE; |
| 114 virtual bool IsUserModifiablePreference(const char* pref_name) const OVERRIDE; | 114 virtual bool IsUserModifiablePreference(const char* pref_name) const OVERRIDE; |
| 115 virtual void UnregisterPreference(const char* path) OVERRIDE; | |
| 116 virtual const PrefService::Preference* FindPreference( | 115 virtual const PrefService::Preference* FindPreference( |
| 117 const char* path) const OVERRIDE; | 116 const char* path) const OVERRIDE; |
| 118 virtual bool GetBoolean(const char* path) const OVERRIDE; | 117 virtual bool GetBoolean(const char* path) const OVERRIDE; |
| 119 virtual int GetInteger(const char* path) const OVERRIDE; | 118 virtual int GetInteger(const char* path) const OVERRIDE; |
| 120 virtual double GetDouble(const char* path) const OVERRIDE; | 119 virtual double GetDouble(const char* path) const OVERRIDE; |
| 121 virtual std::string GetString(const char* path) const OVERRIDE; | 120 virtual std::string GetString(const char* path) const OVERRIDE; |
| 122 virtual base::FilePath GetFilePath(const char* path) const OVERRIDE; | 121 virtual base::FilePath GetFilePath(const char* path) const OVERRIDE; |
| 123 virtual const base::DictionaryValue* GetDictionary( | 122 virtual const base::DictionaryValue* GetDictionary( |
| 124 const char* path) const OVERRIDE; | 123 const char* path) const OVERRIDE; |
| 125 virtual const base::ListValue* GetList(const char* path) const OVERRIDE; | 124 virtual const base::ListValue* GetList(const char* path) const OVERRIDE; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 | 159 |
| 161 // Tell our PrefValueStore to update itself to |command_line_store|. | 160 // Tell our PrefValueStore to update itself to |command_line_store|. |
| 162 // Takes ownership of the store. | 161 // Takes ownership of the store. |
| 163 virtual void UpdateCommandLinePrefStore(PrefStore* command_line_store); | 162 virtual void UpdateCommandLinePrefStore(PrefStore* command_line_store); |
| 164 | 163 |
| 165 // We run the callback once, when initialization completes. The bool | 164 // We run the callback once, when initialization completes. The bool |
| 166 // parameter will be set to true for successful initialization, | 165 // parameter will be set to true for successful initialization, |
| 167 // false for unsuccessful. | 166 // false for unsuccessful. |
| 168 void AddPrefInitObserver(base::Callback<void(bool)> callback); | 167 void AddPrefInitObserver(base::Callback<void(bool)> callback); |
| 169 | 168 |
| 169 // Returns the PrefRegistry object for this service. You should not |
| 170 // use this; the intent is for no registrations to take place after |
| 171 // PrefService has been constructed. |
| 172 PrefRegistry* DeprecatedGetPrefRegistry(); |
| 173 |
| 170 protected: | 174 protected: |
| 171 // Registers a new preference at |path|. The |default_value| must not be | 175 // Adds the registered preferences from the PrefRegistry instance |
| 172 // NULL as it determines the preference value's type. | 176 // passed to us at construction time. |
| 173 // RegisterPreference must not be called twice for the same path. | 177 void AddInitialPreferences(); |
| 174 // This method takes ownership of |default_value|. | 178 |
| 175 void RegisterPreference(const char* path, base::Value* default_value); | 179 // Updates local caches for a preference registered at |path|. The |
| 180 // |default_value| must not be NULL as it determines the preference |
| 181 // value's type. AddRegisteredPreference must not be called twice |
| 182 // for the same path. |
| 183 void AddRegisteredPreference(const char* path, |
| 184 base::Value* default_value); |
| 185 |
| 186 void RemoveRegisteredPreference(const char* path); |
| 176 | 187 |
| 177 // The PrefNotifier handles registering and notifying preference observers. | 188 // The PrefNotifier handles registering and notifying preference observers. |
| 178 // It is created and owned by this PrefService. Subclasses may access it for | 189 // It is created and owned by this PrefService. Subclasses may access it for |
| 179 // unit testing. | 190 // unit testing. |
| 180 scoped_ptr<PrefNotifierImpl> pref_notifier_; | 191 scoped_ptr<PrefNotifierImpl> pref_notifier_; |
| 181 | 192 |
| 182 // The PrefValueStore provides prioritized preference values. It is owned by | 193 // The PrefValueStore provides prioritized preference values. It is owned by |
| 183 // this PrefService. Subclasses may access it for unit testing. | 194 // this PrefService. Subclasses may access it for unit testing. |
| 184 scoped_ptr<PrefValueStore> pref_value_store_; | 195 scoped_ptr<PrefValueStore> pref_value_store_; |
| 185 | 196 |
| 197 scoped_refptr<PrefRegistry> pref_registry_; |
| 198 |
| 186 // Pref Stores and profile that we passed to the PrefValueStore. | 199 // Pref Stores and profile that we passed to the PrefValueStore. |
| 187 scoped_refptr<PersistentPrefStore> user_pref_store_; | 200 scoped_refptr<PersistentPrefStore> user_pref_store_; |
| 188 scoped_refptr<DefaultPrefStore> default_store_; | |
| 189 | 201 |
| 190 // Callback to call when a read error occurs. | 202 // Callback to call when a read error occurs. |
| 191 base::Callback<void(PersistentPrefStore::PrefReadError)> read_error_callback_; | 203 base::Callback<void(PersistentPrefStore::PrefReadError)> read_error_callback_; |
| 192 | 204 |
| 193 private: | 205 private: |
| 194 // Hash map expected to be fastest here since it minimises expensive | 206 // Hash map expected to be fastest here since it minimises expensive |
| 195 // string comparisons. Order is unimportant, and deletions are rare. | 207 // string comparisons. Order is unimportant, and deletions are rare. |
| 196 // Confirmed on Android where this speeded Chrome startup by roughly 50ms | 208 // Confirmed on Android where this speeded Chrome startup by roughly 50ms |
| 197 // vs. std::map, and by roughly 180ms vs. std::set of Preference pointers. | 209 // vs. std::map, and by roughly 180ms vs. std::set of Preference pointers. |
| 198 typedef base::hash_map<std::string, Preference> PreferenceMap; | 210 typedef base::hash_map<std::string, Preference> PreferenceMap; |
| 199 | 211 |
| 200 // Give access to Initialize(). | |
| 201 friend class PrefServiceBuilder; | |
| 202 | |
| 203 // Give access to ReportUserPrefChanged() and GetMutableUserPref(). | 212 // Give access to ReportUserPrefChanged() and GetMutableUserPref(). |
| 204 friend class subtle::ScopedUserPrefUpdateBase; | 213 friend class subtle::ScopedUserPrefUpdateBase; |
| 205 | 214 |
| 206 // PrefServiceBase implementation (protected in base, private here). | 215 // PrefServiceBase implementation (protected in base, private here). |
| 207 virtual void AddPrefObserver(const char* path, | 216 virtual void AddPrefObserver(const char* path, |
| 208 PrefObserver* obs) OVERRIDE; | 217 PrefObserver* obs) OVERRIDE; |
| 209 virtual void RemovePrefObserver(const char* path, | 218 virtual void RemovePrefObserver(const char* path, |
| 210 PrefObserver* obs) OVERRIDE; | 219 PrefObserver* obs) OVERRIDE; |
| 211 | 220 |
| 212 // Sends notification of a changed preference. This needs to be called by | 221 // Sends notification of a changed preference. This needs to be called by |
| (...skipping 18 matching lines...) Expand all Loading... |
| 231 base::Value* GetMutableUserPref(const char* path, | 240 base::Value* GetMutableUserPref(const char* path, |
| 232 base::Value::Type type); | 241 base::Value::Type type); |
| 233 | 242 |
| 234 // GetPreferenceValue is the equivalent of FindPreference(path)->GetValue(), | 243 // GetPreferenceValue is the equivalent of FindPreference(path)->GetValue(), |
| 235 // it has been added for performance. If is faster because it does | 244 // it has been added for performance. If is faster because it does |
| 236 // not need to find or create a Preference object to get the | 245 // not need to find or create a Preference object to get the |
| 237 // value (GetValue() calls back though the preference service to | 246 // value (GetValue() calls back though the preference service to |
| 238 // actually get the value.). | 247 // actually get the value.). |
| 239 const base::Value* GetPreferenceValue(const std::string& path) const; | 248 const base::Value* GetPreferenceValue(const std::string& path) const; |
| 240 | 249 |
| 241 // Local cache of registered Preference objects. The default_store_ | 250 // Local cache of registered Preference objects. The pref_registry_ |
| 242 // is authoritative with respect to what the types and default values | 251 // is authoritative with respect to what the types and default values |
| 243 // of registered preferences are. | 252 // of registered preferences are. |
| 244 mutable PreferenceMap prefs_map_; | 253 mutable PreferenceMap prefs_map_; |
| 245 | 254 |
| 246 DISALLOW_COPY_AND_ASSIGN(PrefService); | 255 DISALLOW_COPY_AND_ASSIGN(PrefService); |
| 247 }; | 256 }; |
| 248 | 257 |
| 249 // TODO(joi): Remove these forwards. They were placed here temporarily | 258 // TODO(joi): Remove these forwards. They were placed here temporarily |
| 250 // to limit the size of the initial change that split | 259 // to limit the size of the initial change that split |
| 251 // PrefServiceSimple and PrefServiceSyncable out of PrefService. | 260 // PrefServiceSimple and PrefServiceSyncable out of PrefService. |
| 252 #include "chrome/browser/prefs/pref_service_simple.h" | |
| 253 #include "chrome/browser/prefs/pref_service_syncable.h" | 261 #include "chrome/browser/prefs/pref_service_syncable.h" |
| 254 | 262 |
| 255 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ | 263 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ |
| OLD | NEW |