Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: chrome/browser/prefs/pref_service.h

Issue 12211105: Move remaining non-test, non-Chrome-specific Prefs code to base/prefs/. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address review comments, merge to LKGR. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/prefs/pref_registry_syncable.h ('k') | chrome/browser/prefs/pref_service.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 // This provides a way to access the application's current preferences.
6
7 // Chromium settings and storage represent user-selected preferences and
8 // information and MUST not be extracted, overwritten or modified except
9 // through Chromium defined APIs.
10
11 #ifndef CHROME_BROWSER_PREFS_PREF_SERVICE_H_
12 #define CHROME_BROWSER_PREFS_PREF_SERVICE_H_
13
14 #include <set>
15 #include <string>
16
17 #include "base/callback.h"
18 #include "base/hash_tables.h"
19 #include "base/memory/ref_counted.h"
20 #include "base/memory/scoped_ptr.h"
21 #include "base/observer_list.h"
22 #include "base/prefs/persistent_pref_store.h"
23 #include "base/prefs/public/pref_service_base.h"
24 #include "base/threading/non_thread_safe.h"
25
26 class PrefNotifier;
27 class PrefNotifierImpl;
28 class PrefObserver;
29 class PrefRegistry;
30 class PrefValueStore;
31 class PrefStore;
32
33 namespace subtle {
34 class ScopedUserPrefUpdateBase;
35 };
36
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
39 // that see e.g. PrefRegistrySimple.
40 class PrefService : public PrefServiceBase, public base::NonThreadSafe {
41 public:
42 enum PrefInitializationStatus {
43 INITIALIZATION_STATUS_WAITING,
44 INITIALIZATION_STATUS_SUCCESS,
45 INITIALIZATION_STATUS_CREATED_NEW_PROFILE,
46 INITIALIZATION_STATUS_ERROR
47 };
48
49 // A helper class to store all the information associated with a preference.
50 class Preference : public PrefServiceBase::Preference {
51 public:
52 // The type of the preference is determined by the type with which it is
53 // registered. This type needs to be a boolean, integer, double, string,
54 // dictionary (a branch), or list. You shouldn't need to construct this on
55 // your own; use the PrefService::Register*Pref methods instead.
56 Preference(const PrefService* service,
57 const char* name,
58 base::Value::Type type);
59 virtual ~Preference() {}
60
61 // PrefServiceBase::Preference implementation.
62 virtual const std::string name() const OVERRIDE;
63 virtual base::Value::Type GetType() const OVERRIDE;
64 virtual const base::Value* GetValue() const OVERRIDE;
65 virtual const base::Value* GetRecommendedValue() const OVERRIDE;
66 virtual bool IsManaged() const OVERRIDE;
67 virtual bool IsRecommended() const OVERRIDE;
68 virtual bool HasExtensionSetting() const OVERRIDE;
69 virtual bool HasUserSetting() const OVERRIDE;
70 virtual bool IsExtensionControlled() const OVERRIDE;
71 virtual bool IsUserControlled() const OVERRIDE;
72 virtual bool IsDefaultValue() const OVERRIDE;
73 virtual bool IsUserModifiable() const OVERRIDE;
74 virtual bool IsExtensionModifiable() const OVERRIDE;
75
76 private:
77 friend class PrefService;
78
79 PrefValueStore* pref_value_store() const {
80 return pref_service_->pref_value_store_.get();
81 }
82
83 const std::string name_;
84
85 const base::Value::Type type_;
86
87 // Reference to the PrefService in which this pref was created.
88 const PrefService* pref_service_;
89 };
90
91 // You may wish to use PrefServiceBuilder or one of its subclasses
92 // for simplified construction.
93 PrefService(
94 PrefNotifierImpl* pref_notifier,
95 PrefValueStore* pref_value_store,
96 PersistentPrefStore* user_prefs,
97 PrefRegistry* pref_registry,
98 base::Callback<void(PersistentPrefStore::PrefReadError)>
99 read_error_callback,
100 bool async);
101 virtual ~PrefService();
102
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
105 // values while the importer process is running. Returns true on success.
106 bool ReloadPersistentPrefs();
107
108 // Lands pending writes to disk. This should only be used if we need to save
109 // immediately (basically, during shutdown).
110 void CommitPendingWrite();
111
112 // PrefServiceBase implementation.
113 virtual bool IsManagedPreference(const char* pref_name) const OVERRIDE;
114 virtual bool IsUserModifiablePreference(const char* pref_name) const OVERRIDE;
115 virtual const PrefService::Preference* FindPreference(
116 const char* path) const OVERRIDE;
117 virtual bool GetBoolean(const char* path) const OVERRIDE;
118 virtual int GetInteger(const char* path) const OVERRIDE;
119 virtual double GetDouble(const char* path) const OVERRIDE;
120 virtual std::string GetString(const char* path) const OVERRIDE;
121 virtual base::FilePath GetFilePath(const char* path) const OVERRIDE;
122 virtual const base::DictionaryValue* GetDictionary(
123 const char* path) const OVERRIDE;
124 virtual const base::ListValue* GetList(const char* path) const OVERRIDE;
125 virtual void ClearPref(const char* path) OVERRIDE;
126 virtual void Set(const char* path, const base::Value& value) OVERRIDE;
127 virtual void SetBoolean(const char* path, bool value) OVERRIDE;
128 virtual void SetInteger(const char* path, int value) OVERRIDE;
129 virtual void SetDouble(const char* path, double value) OVERRIDE;
130 virtual void SetString(const char* path, const std::string& value) OVERRIDE;
131 virtual void SetFilePath(const char* path,
132 const base::FilePath& value) OVERRIDE;
133 virtual void SetInt64(const char* path, int64 value) OVERRIDE;
134 virtual int64 GetInt64(const char* path) const OVERRIDE;
135 virtual void SetUint64(const char* path, uint64 value) OVERRIDE;
136 virtual uint64 GetUint64(const char* path) const OVERRIDE;
137
138 // Returns the value of the given preference, from the user pref store. If
139 // the preference is not set in the user pref store, returns NULL.
140 const base::Value* GetUserPrefValue(const char* path) const;
141
142 // Returns the default value of the given preference. |path| must point to a
143 // registered preference. In that case, will never return NULL.
144 const base::Value* GetDefaultPrefValue(const char* path) const;
145
146 // Returns true if a value has been set for the specified path.
147 // NOTE: this is NOT the same as FindPreference. In particular
148 // FindPreference returns whether RegisterXXX has been invoked, where as
149 // this checks if a value exists for the path.
150 bool HasPrefPath(const char* path) const;
151
152 // Returns a dictionary with effective preference values. The ownership
153 // is passed to the caller.
154 base::DictionaryValue* GetPreferenceValues() const;
155
156 bool ReadOnly() const;
157
158 PrefInitializationStatus GetInitializationStatus() const;
159
160 // Tell our PrefValueStore to update itself to |command_line_store|.
161 // Takes ownership of the store.
162 virtual void UpdateCommandLinePrefStore(PrefStore* command_line_store);
163
164 // We run the callback once, when initialization completes. The bool
165 // parameter will be set to true for successful initialization,
166 // false for unsuccessful.
167 void AddPrefInitObserver(base::Callback<void(bool)> callback);
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
174 protected:
175 // Adds the registered preferences from the PrefRegistry instance
176 // passed to us at construction time.
177 void AddInitialPreferences();
178
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 // Updates local caches for a preference unregistered at |path|.
187 virtual void RemoveRegisteredPreference(const char* path);
188
189 // The PrefNotifier handles registering and notifying preference observers.
190 // It is created and owned by this PrefService. Subclasses may access it for
191 // unit testing.
192 scoped_ptr<PrefNotifierImpl> pref_notifier_;
193
194 // The PrefValueStore provides prioritized preference values. It is owned by
195 // this PrefService. Subclasses may access it for unit testing.
196 scoped_ptr<PrefValueStore> pref_value_store_;
197
198 scoped_refptr<PrefRegistry> pref_registry_;
199
200 // Pref Stores and profile that we passed to the PrefValueStore.
201 scoped_refptr<PersistentPrefStore> user_pref_store_;
202
203 // Callback to call when a read error occurs.
204 base::Callback<void(PersistentPrefStore::PrefReadError)> read_error_callback_;
205
206 private:
207 // Hash map expected to be fastest here since it minimises expensive
208 // string comparisons. Order is unimportant, and deletions are rare.
209 // Confirmed on Android where this speeded Chrome startup by roughly 50ms
210 // vs. std::map, and by roughly 180ms vs. std::set of Preference pointers.
211 typedef base::hash_map<std::string, Preference> PreferenceMap;
212
213 // Give access to ReportUserPrefChanged() and GetMutableUserPref().
214 friend class subtle::ScopedUserPrefUpdateBase;
215
216 // PrefServiceBase implementation (protected in base, private here).
217 virtual void AddPrefObserver(const char* path,
218 PrefObserver* obs) OVERRIDE;
219 virtual void RemovePrefObserver(const char* path,
220 PrefObserver* obs) OVERRIDE;
221
222 // Sends notification of a changed preference. This needs to be called by
223 // a ScopedUserPrefUpdate if a DictionaryValue or ListValue is changed.
224 void ReportUserPrefChanged(const std::string& key);
225
226 // Sets the value for this pref path in the user pref store and informs the
227 // PrefNotifier of the change.
228 void SetUserPrefValue(const char* path, base::Value* new_value);
229
230 // Load preferences from storage, attempting to diagnose and handle errors.
231 // This should only be called from the constructor.
232 void InitFromStorage(bool async);
233
234 // Used to set the value of dictionary or list values in the user pref store.
235 // This will create a dictionary or list if one does not exist in the user
236 // pref store. This method returns NULL only if you're requesting an
237 // unregistered pref or a non-dict/non-list pref.
238 // |type| may only be Values::TYPE_DICTIONARY or Values::TYPE_LIST and
239 // |path| must point to a registered preference of type |type|.
240 // Ownership of the returned value remains at the user pref store.
241 base::Value* GetMutableUserPref(const char* path,
242 base::Value::Type type);
243
244 // GetPreferenceValue is the equivalent of FindPreference(path)->GetValue(),
245 // it has been added for performance. If is faster because it does
246 // not need to find or create a Preference object to get the
247 // value (GetValue() calls back though the preference service to
248 // actually get the value.).
249 const base::Value* GetPreferenceValue(const std::string& path) const;
250
251 // Local cache of registered Preference objects. The pref_registry_
252 // is authoritative with respect to what the types and default values
253 // of registered preferences are.
254 mutable PreferenceMap prefs_map_;
255
256 DISALLOW_COPY_AND_ASSIGN(PrefService);
257 };
258
259 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_
OLDNEW
« no previous file with comments | « chrome/browser/prefs/pref_registry_syncable.h ('k') | chrome/browser/prefs/pref_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698