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