| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef CHROME_BROWSER_PREF_SERVICE_H_ | 7 #ifndef CHROME_BROWSER_PREF_SERVICE_H_ |
| 8 #define CHROME_BROWSER_PREF_SERVICE_H_ | 8 #define CHROME_BROWSER_PREF_SERVICE_H_ |
| 9 | 9 |
| 10 #include <set> | 10 #include <set> |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 Value::ValueType type_; | 62 Value::ValueType type_; |
| 63 std::wstring name_; | 63 std::wstring name_; |
| 64 scoped_ptr<Value> default_value_; | 64 scoped_ptr<Value> default_value_; |
| 65 | 65 |
| 66 // A reference to the pref service's pref_value_store_. | 66 // A reference to the pref service's pref_value_store_. |
| 67 PrefValueStore* pref_value_store_; | 67 PrefValueStore* pref_value_store_; |
| 68 | 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(Preference); | 69 DISALLOW_COPY_AND_ASSIGN(Preference); |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 // Factory method that creates a new instance of a |PrefService|. | 72 // Factory method that creates a new instance of a |PrefService| with |
| 73 // all platform-applicable PrefStores (managed, extension, user, etc.). |
| 74 // This is the usual way to create a new PrefService. |
| 73 static PrefService* CreatePrefService(const FilePath& pref_filename); | 75 static PrefService* CreatePrefService(const FilePath& pref_filename); |
| 74 | 76 |
| 75 // The |PrefValueStore| provides preference values. | 77 // Convenience factory method for use in unit tests. Creates a new |
| 78 // PrefService that uses a PrefValueStore with user preferences at the given |
| 79 // |pref_filename|, and no other PrefStores (i.e., no other types of |
| 80 // preferences). |
| 81 static PrefService* CreateUserPrefService(const FilePath& pref_filename); |
| 82 |
| 83 // This constructor is primarily used by tests. The |PrefValueStore| provides |
| 84 // preference values. |
| 76 explicit PrefService(PrefValueStore* pref_value_store); | 85 explicit PrefService(PrefValueStore* pref_value_store); |
| 86 |
| 77 ~PrefService(); | 87 ~PrefService(); |
| 78 | 88 |
| 79 // Reloads the data from file. This should only be called when the importer | 89 // Reloads the data from file. This should only be called when the importer |
| 80 // is running during first run, and the main process may not change pref | 90 // is running during first run, and the main process may not change pref |
| 81 // values while the importer process is running. Returns true on success. | 91 // values while the importer process is running. Returns true on success. |
| 82 bool ReloadPersistentPrefs(); | 92 bool ReloadPersistentPrefs(); |
| 83 | 93 |
| 84 // Writes the data to disk. The return value only reflects whether | 94 // Writes the data to disk. The return value only reflects whether |
| 85 // serialization was successful; we don't know whether the data actually made | 95 // serialization was successful; we don't know whether the data actually made |
| 86 // it on disk (since it's on a different thread). This should only be used if | 96 // it on disk (since it's on a different thread). This should only be used if |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 return lhs->name() < rhs->name(); | 184 return lhs->name() < rhs->name(); |
| 175 } | 185 } |
| 176 }; | 186 }; |
| 177 typedef std::set<Preference*, PreferencePathComparator> PreferenceSet; | 187 typedef std::set<Preference*, PreferencePathComparator> PreferenceSet; |
| 178 const PreferenceSet& preference_set() const { return prefs_; } | 188 const PreferenceSet& preference_set() const { return prefs_; } |
| 179 | 189 |
| 180 // A helper method to quickly look up a preference. Returns NULL if the | 190 // A helper method to quickly look up a preference. Returns NULL if the |
| 181 // preference is not registered. | 191 // preference is not registered. |
| 182 const Preference* FindPreference(const wchar_t* pref_name) const; | 192 const Preference* FindPreference(const wchar_t* pref_name) const; |
| 183 | 193 |
| 194 // For the given pref_name, fire any observer of the pref only if |old_value| |
| 195 // is different from the current value. Virtual so it can be mocked for a |
| 196 // unit test. |
| 197 virtual void FireObserversIfChanged(const wchar_t* pref_name, |
| 198 const Value* old_value); |
| 199 |
| 184 bool read_only() const { return pref_value_store_->ReadOnly(); } | 200 bool read_only() const { return pref_value_store_->ReadOnly(); } |
| 185 | 201 |
| 202 protected: |
| 203 // This should only be accessed by subclasses for unit-testing. |
| 204 bool PrefIsChanged(const wchar_t* path, const Value* old_value); |
| 205 |
| 186 private: | 206 private: |
| 187 // Add a preference to the PreferenceMap. If the pref already exists, return | 207 // Add a preference to the PreferenceMap. If the pref already exists, return |
| 188 // false. This method takes ownership of |pref|. | 208 // false. This method takes ownership of |pref|. |
| 189 void RegisterPreference(Preference* pref); | 209 void RegisterPreference(Preference* pref); |
| 190 | 210 |
| 191 // Returns a copy of the current pref value. The caller is responsible for | 211 // Returns a copy of the current pref value. The caller is responsible for |
| 192 // deleting the returned object. | 212 // deleting the returned object. |
| 193 Value* GetPrefCopy(const wchar_t* pref_name); | 213 Value* GetPrefCopy(const wchar_t* pref_name); |
| 194 | 214 |
| 195 // For the given pref_name, fire any observer of the pref. | 215 // For the given pref_name, fire any observer of the pref. |
| 196 void FireObservers(const wchar_t* pref_name); | 216 void FireObservers(const wchar_t* pref_name); |
| 197 | 217 |
| 198 // For the given pref_name, fire any observer of the pref only if |old_value| | |
| 199 // is different from the current value. | |
| 200 void FireObserversIfChanged(const wchar_t* pref_name, | |
| 201 const Value* old_value); | |
| 202 | |
| 203 // Load from disk. Returns a non-zero error code on failure. | 218 // Load from disk. Returns a non-zero error code on failure. |
| 204 PrefStore::PrefReadError LoadPersistentPrefs(); | 219 PrefStore::PrefReadError LoadPersistentPrefs(); |
| 205 | 220 |
| 206 // Load preferences from storage, attempting to diagnose and handle errors. | 221 // Load preferences from storage, attempting to diagnose and handle errors. |
| 207 // This should only be called from the constructor. | 222 // This should only be called from the constructor. |
| 208 void InitFromStorage(); | 223 void InitFromStorage(); |
| 209 | 224 |
| 210 // The value of a Preference can be: | 225 // The value of a Preference can be: |
| 211 // managed, user defined, recommended or default. | 226 // managed, user defined, recommended or default. |
| 212 // The PrefValueStore manages enforced, user defined and recommended values | 227 // The PrefValueStore manages enforced, user defined and recommended values |
| (...skipping 11 matching lines...) Expand all Loading... |
| 224 typedef base::hash_map<std::wstring, NotificationObserverList*> | 239 typedef base::hash_map<std::wstring, NotificationObserverList*> |
| 225 PrefObserverMap; | 240 PrefObserverMap; |
| 226 PrefObserverMap pref_observers_; | 241 PrefObserverMap pref_observers_; |
| 227 | 242 |
| 228 friend class ScopedPrefUpdate; | 243 friend class ScopedPrefUpdate; |
| 229 | 244 |
| 230 DISALLOW_COPY_AND_ASSIGN(PrefService); | 245 DISALLOW_COPY_AND_ASSIGN(PrefService); |
| 231 }; | 246 }; |
| 232 | 247 |
| 233 #endif // CHROME_BROWSER_PREF_SERVICE_H_ | 248 #endif // CHROME_BROWSER_PREF_SERVICE_H_ |
| OLD | NEW |