OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 // This service has two preference stores, one for "persistent" preferences, | 6 // This service has two preference stores, one for "persistent" preferences, |
7 // which get serialized for use in the next session, and one for "transient" | 7 // which get serialized for use in the next session, and one for "transient" |
8 // preferences, which are in effect for only the current session | 8 // preferences, which are in effect for only the current session |
9 // (this usually encodes things like command-line switches). | 9 // (this usually encodes things like command-line switches). |
10 // | 10 // |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 typedef std::set<Preference*, PreferencePathComparator> PreferenceSet; | 177 typedef std::set<Preference*, PreferencePathComparator> PreferenceSet; |
178 const PreferenceSet& preference_set() const { return prefs_; } | 178 const PreferenceSet& preference_set() const { return prefs_; } |
179 | 179 |
180 // A helper method to quickly look up a preference. Returns NULL if the | 180 // A helper method to quickly look up a preference. Returns NULL if the |
181 // preference is not registered. | 181 // preference is not registered. |
182 const Preference* FindPreference(const wchar_t* pref_name) const; | 182 const Preference* FindPreference(const wchar_t* pref_name) const; |
183 | 183 |
184 // ImportantFileWriter::DataSerializer | 184 // ImportantFileWriter::DataSerializer |
185 virtual bool SerializeData(std::string* output); | 185 virtual bool SerializeData(std::string* output); |
186 | 186 |
| 187 bool read_only() const { return read_only_; } |
| 188 |
187 private: | 189 private: |
| 190 // Unique integer code for each type of error so we can report them |
| 191 // distinctly in a histogram. |
| 192 // NOTE: Don't change the order here as it will change the server's meaning |
| 193 // of the histogram. |
| 194 enum PrefReadError { |
| 195 PREF_READ_ERROR_NONE = 0, |
| 196 PREF_READ_ERROR_JSON_PARSE, |
| 197 PREF_READ_ERROR_JSON_TYPE, |
| 198 PREF_READ_ERROR_ACCESS_DENIED, |
| 199 PREF_READ_ERROR_FILE_OTHER, |
| 200 PREF_READ_ERROR_FILE_LOCKED, |
| 201 PREF_READ_ERROR_NO_FILE, |
| 202 }; |
| 203 |
188 // Add a preference to the PreferenceMap. If the pref already exists, return | 204 // Add a preference to the PreferenceMap. If the pref already exists, return |
189 // false. This method takes ownership of |pref|. | 205 // false. This method takes ownership of |pref|. |
190 void RegisterPreference(Preference* pref); | 206 void RegisterPreference(Preference* pref); |
191 | 207 |
192 // Returns a copy of the current pref value. The caller is responsible for | 208 // Returns a copy of the current pref value. The caller is responsible for |
193 // deleting the returned object. | 209 // deleting the returned object. |
194 Value* GetPrefCopy(const wchar_t* pref_name); | 210 Value* GetPrefCopy(const wchar_t* pref_name); |
195 | 211 |
196 // For the given pref_name, fire any observer of the pref. | 212 // For the given pref_name, fire any observer of the pref. |
197 void FireObservers(const wchar_t* pref_name); | 213 void FireObservers(const wchar_t* pref_name); |
198 | 214 |
199 // For the given pref_name, fire any observer of the pref only if |old_value| | 215 // For the given pref_name, fire any observer of the pref only if |old_value| |
200 // is different from the current value. | 216 // is different from the current value. |
201 void FireObserversIfChanged(const wchar_t* pref_name, | 217 void FireObserversIfChanged(const wchar_t* pref_name, |
202 const Value* old_value); | 218 const Value* old_value); |
203 | 219 |
| 220 // Load from disk. Returns a non-zero error code on failure. |
| 221 PrefReadError LoadPersistentPrefs(); |
| 222 |
| 223 // Load preferences from disk, attempting to diagnose and handle errors. |
| 224 // This should only be called from the constructor. |
| 225 void InitFromDisk(); |
| 226 |
204 scoped_ptr<DictionaryValue> persistent_; | 227 scoped_ptr<DictionaryValue> persistent_; |
205 | 228 |
206 // Helper for safe writing pref data. | 229 // Helper for safe writing pref data. |
207 ImportantFileWriter writer_; | 230 ImportantFileWriter writer_; |
208 | 231 |
209 // A set of all the registered Preference objects. | 232 // A set of all the registered Preference objects. |
210 PreferenceSet prefs_; | 233 PreferenceSet prefs_; |
211 | 234 |
212 // A map from pref names to a list of observers. Observers get fired in the | 235 // A map from pref names to a list of observers. Observers get fired in the |
213 // order they are added. | 236 // order they are added. |
214 typedef ObserverList<NotificationObserver> NotificationObserverList; | 237 typedef ObserverList<NotificationObserver> NotificationObserverList; |
215 typedef base::hash_map<std::wstring, NotificationObserverList*> | 238 typedef base::hash_map<std::wstring, NotificationObserverList*> |
216 PrefObserverMap; | 239 PrefObserverMap; |
217 PrefObserverMap pref_observers_; | 240 PrefObserverMap pref_observers_; |
218 | 241 |
| 242 // Whether the service is in a pseudo-read-only mode where changes are not |
| 243 // actually persisted to disk. This happens in some cases when there are |
| 244 // read errors during startup. |
| 245 bool read_only_; |
| 246 |
219 DISALLOW_COPY_AND_ASSIGN(PrefService); | 247 DISALLOW_COPY_AND_ASSIGN(PrefService); |
220 }; | 248 }; |
221 | 249 |
222 #endif // CHROME_BROWSER_PREF_SERVICE_H_ | 250 #endif // CHROME_BROWSER_PREF_SERVICE_H_ |
OLD | NEW |