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