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 PrefStore; | 30 class PrefStore; |
31 class PrefValueStore; | 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. PrefRegistrarSimple. |
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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 FilePath GetFilePath(const char* path) const OVERRIDE; | 121 virtual 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 // Tell our PrefValueStore to update itself to |command_line_store|. | 159 // Tell our PrefValueStore to update itself to |command_line_store|. |
161 // Takes ownership of the store. | 160 // Takes ownership of the store. |
162 virtual void UpdateCommandLinePrefStore(PrefStore* command_line_store); | 161 virtual void UpdateCommandLinePrefStore(PrefStore* command_line_store); |
163 | 162 |
164 // We run the callback once, when initialization completes. The bool | 163 // We run the callback once, when initialization completes. The bool |
165 // parameter will be set to true for successful initialization, | 164 // parameter will be set to true for successful initialization, |
166 // false for unsuccessful. | 165 // false for unsuccessful. |
167 void AddPrefInitObserver(base::Callback<void(bool)> callback); | 166 void AddPrefInitObserver(base::Callback<void(bool)> callback); |
168 | 167 |
169 protected: | 168 protected: |
| 169 friend class PrefRegistrarSimple; |
| 170 |
170 // Registers a new preference at |path|. The |default_value| must not be | 171 // Registers a new preference at |path|. The |default_value| must not be |
171 // NULL as it determines the preference value's type. | 172 // NULL as it determines the preference value's type. |
172 // RegisterPreference must not be called twice for the same path. | 173 // RegisterPreference must not be called twice for the same path. |
173 // This method takes ownership of |default_value|. | 174 // This method takes ownership of |default_value|. |
174 void RegisterPreference(const char* path, base::Value* default_value); | 175 void RegisterPreference(const char* path, base::Value* default_value); |
175 | 176 |
| 177 void UnregisterPreference(const char* path); |
| 178 |
| 179 // Subclasses should override this to return false if they do not |
| 180 // wish to allow PrefRegistrarSimple to be used to register |
| 181 // preferences on this object, e.g. if they wish to ensure more |
| 182 // parameters are given for each registration than |
| 183 // PrefRegistrarSimple takes. |
| 184 virtual bool AllowPrefRegistrarSimple() const; |
| 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 |
185 // Pref Stores and profile that we passed to the PrefValueStore. | 195 // Pref Stores and profile that we passed to the PrefValueStore. |
186 scoped_refptr<PersistentPrefStore> user_pref_store_; | 196 scoped_refptr<PersistentPrefStore> user_pref_store_; |
187 scoped_refptr<DefaultPrefStore> default_store_; | 197 scoped_refptr<DefaultPrefStore> default_store_; |
188 | 198 |
189 // Callback to call when a read error occurs. | 199 // Callback to call when a read error occurs. |
190 base::Callback<void(PersistentPrefStore::PrefReadError)> read_error_callback_; | 200 base::Callback<void(PersistentPrefStore::PrefReadError)> read_error_callback_; |
191 | 201 |
192 private: | 202 private: |
193 // Hash map expected to be fastest here since it minimises expensive | 203 // Hash map expected to be fastest here since it minimises expensive |
194 // string comparisons. Order is unimportant, and deletions are rare. | 204 // string comparisons. Order is unimportant, and deletions are rare. |
195 // Confirmed on Android where this speeded Chrome startup by roughly 50ms | 205 // 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. | 206 // vs. std::map, and by roughly 180ms vs. std::set of Preference pointers. |
197 typedef base::hash_map<std::string, Preference> PreferenceMap; | 207 typedef base::hash_map<std::string, Preference> PreferenceMap; |
198 | 208 |
199 // Give access to Initialize(). | |
200 friend class PrefServiceBuilder; | |
201 | |
202 // Give access to ReportUserPrefChanged() and GetMutableUserPref(). | 209 // Give access to ReportUserPrefChanged() and GetMutableUserPref(). |
203 friend class subtle::ScopedUserPrefUpdateBase; | 210 friend class subtle::ScopedUserPrefUpdateBase; |
204 | 211 |
205 // PrefServiceBase implementation (protected in base, private here). | 212 // PrefServiceBase implementation (protected in base, private here). |
206 virtual void AddPrefObserver(const char* path, | 213 virtual void AddPrefObserver(const char* path, |
207 PrefObserver* obs) OVERRIDE; | 214 PrefObserver* obs) OVERRIDE; |
208 virtual void RemovePrefObserver(const char* path, | 215 virtual void RemovePrefObserver(const char* path, |
209 PrefObserver* obs) OVERRIDE; | 216 PrefObserver* obs) OVERRIDE; |
210 | 217 |
211 // Sends notification of a changed preference. This needs to be called by | 218 // Sends notification of a changed preference. This needs to be called by |
(...skipping 29 matching lines...) Expand all Loading... |
241 // is authoritative with respect to what the types and default values | 248 // is authoritative with respect to what the types and default values |
242 // of registered preferences are. | 249 // of registered preferences are. |
243 mutable PreferenceMap prefs_map_; | 250 mutable PreferenceMap prefs_map_; |
244 | 251 |
245 DISALLOW_COPY_AND_ASSIGN(PrefService); | 252 DISALLOW_COPY_AND_ASSIGN(PrefService); |
246 }; | 253 }; |
247 | 254 |
248 // TODO(joi): Remove these forwards. They were placed here temporarily | 255 // TODO(joi): Remove these forwards. They were placed here temporarily |
249 // to limit the size of the initial change that split | 256 // to limit the size of the initial change that split |
250 // PrefServiceSimple and PrefServiceSyncable out of PrefService. | 257 // PrefServiceSimple and PrefServiceSyncable out of PrefService. |
251 #include "chrome/browser/prefs/pref_service_simple.h" | |
252 #include "chrome/browser/prefs/pref_service_syncable.h" | 258 #include "chrome/browser/prefs/pref_service_syncable.h" |
253 | 259 |
254 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ | 260 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ |
OLD | NEW |