| 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_PREFS_PREF_SERVICE_H_ | 7 #ifndef CHROME_BROWSER_PREFS_PREF_SERVICE_H_ |
| 8 #define CHROME_BROWSER_PREFS_PREF_SERVICE_H_ | 8 #define CHROME_BROWSER_PREFS_PREF_SERVICE_H_ |
| 9 #pragma once | 9 #pragma once |
| 10 | 10 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 class NotificationObserver; | 21 class NotificationObserver; |
| 22 class PrefNotifier; | 22 class PrefNotifier; |
| 23 class Profile; | 23 class Profile; |
| 24 | 24 |
| 25 class PrefService : public NonThreadSafe { | 25 class PrefService : public NonThreadSafe { |
| 26 public: | 26 public: |
| 27 // A helper class to store all the information associated with a preference. | 27 // A helper class to store all the information associated with a preference. |
| 28 class Preference { | 28 class Preference { |
| 29 public: | 29 public: |
| 30 | 30 |
| 31 // The type of the preference is determined by the type of |default_value|. | 31 // The type of the preference is determined by the type with which it is |
| 32 // Therefore, the type needs to be a boolean, integer, real, string, | 32 // registered. This type needs to be a boolean, integer, real, string, |
| 33 // dictionary (a branch), or list. You shouldn't need to construct this on | 33 // dictionary (a branch), or list. You shouldn't need to construct this on |
| 34 // your own, use the PrefService::Register*Pref methods instead. | 34 // your own; use the PrefService::Register*Pref methods instead. |
| 35 // |default_value| will be owned by the Preference object. | 35 Preference(const PrefService* service, |
| 36 Preference(PrefValueStore* pref_value_store, | |
| 37 const char* name, | 36 const char* name, |
| 38 Value* default_value); | 37 Value::ValueType type); |
| 39 ~Preference() {} | 38 ~Preference() {} |
| 40 | 39 |
| 41 Value::ValueType type() const { return type_; } | 40 Value::ValueType type() const { return type_; } |
| 42 | 41 |
| 43 // Returns the name of the Preference (i.e., the key, e.g., | 42 // Returns the name of the Preference (i.e., the key, e.g., |
| 44 // browser.window_placement). | 43 // browser.window_placement). |
| 45 const std::string name() const { return name_; } | 44 const std::string name() const { return name_; } |
| 46 | 45 |
| 47 // Returns the value of the Preference. If there is no user specified | 46 // Returns the value of the Preference, falling back to the registered |
| 48 // value, it returns the default value. | 47 // default value if no other has been set. |
| 49 const Value* GetValue() const; | 48 const Value* GetValue() const; |
| 50 | 49 |
| 51 // Returns true if the current value matches the default value. | |
| 52 bool IsDefaultValue() const; | |
| 53 | |
| 54 // Returns true if the Preference is managed, i.e. set by an admin policy. | 50 // Returns true if the Preference is managed, i.e. set by an admin policy. |
| 55 // Since managed prefs have the highest priority, this also indicates | 51 // Since managed prefs have the highest priority, this also indicates |
| 56 // whether the pref is actually being controlled by the policy setting. | 52 // whether the pref is actually being controlled by the policy setting. |
| 57 bool IsManaged() const; | 53 bool IsManaged() const; |
| 58 | 54 |
| 59 // Returns true if the Preference has a value set by an extension, even if | 55 // Returns true if the Preference has a value set by an extension, even if |
| 60 // that value is being overridden by a higher-priority source. | 56 // that value is being overridden by a higher-priority source. |
| 61 bool HasExtensionSetting() const; | 57 bool HasExtensionSetting() const; |
| 62 | 58 |
| 63 // Returns true if the Preference has a user setting, even if that value is | 59 // Returns true if the Preference has a user setting, even if that value is |
| 64 // being overridden by a higher-priority source. | 60 // being overridden by a higher-priority source. |
| 65 bool HasUserSetting() const; | 61 bool HasUserSetting() const; |
| 66 | 62 |
| 67 // Returns true if the Preference value is currently being controlled by an | 63 // Returns true if the Preference value is currently being controlled by an |
| 68 // extension, and not by any higher-priority source. | 64 // extension, and not by any higher-priority source. |
| 69 bool IsExtensionControlled() const; | 65 bool IsExtensionControlled() const; |
| 70 | 66 |
| 71 // Returns true if the Preference value is currently being controlled by a | 67 // Returns true if the Preference value is currently being controlled by a |
| 72 // user setting, and not by any higher-priority source. | 68 // user setting, and not by any higher-priority source. |
| 73 bool IsUserControlled() const; | 69 bool IsUserControlled() const; |
| 74 | 70 |
| 71 // Returns true if the Preference is currently using its default value, |
| 72 // and has not been set by any higher-priority source (even with the same |
| 73 // value). |
| 74 bool IsDefaultValue() const; |
| 75 |
| 75 // Returns true if the user can change the Preference value, which is the | 76 // Returns true if the user can change the Preference value, which is the |
| 76 // case if no higher-priority source than the user store controls the | 77 // case if no higher-priority source than the user store controls the |
| 77 // Preference. | 78 // Preference. |
| 78 bool IsUserModifiable() const; | 79 bool IsUserModifiable() const; |
| 79 | 80 |
| 80 private: | 81 private: |
| 81 friend class PrefService; | 82 friend class PrefService; |
| 82 | 83 |
| 83 Value::ValueType type_; | 84 Value::ValueType type_; |
| 84 std::string name_; | 85 std::string name_; |
| 85 scoped_ptr<Value> default_value_; | |
| 86 | 86 |
| 87 // A reference to the pref service's pref_value_store_. | 87 // Reference to the PrefService in which this pref was created. |
| 88 PrefValueStore* pref_value_store_; | 88 const PrefService* pref_service_; |
| 89 | 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(Preference); | 90 DISALLOW_COPY_AND_ASSIGN(Preference); |
| 91 }; | 91 }; |
| 92 | 92 |
| 93 // Factory method that creates a new instance of a PrefService with | 93 // Factory method that creates a new instance of a PrefService with |
| 94 // a PrefValueStore containing all platform-applicable PrefStores. | 94 // a PrefValueStore containing all platform-applicable PrefStores. |
| 95 // The |pref_filename| points to the user preference file. The |profile| is | 95 // The |pref_filename| points to the user preference file. The |profile| is |
| 96 // the one to which these preferences apply; it may be NULL if we're dealing | 96 // the one to which these preferences apply; it may be NULL if we're dealing |
| 97 // with the local state. This is the usual way to create a new PrefService. | 97 // with the local state. This is the usual way to create a new PrefService. |
| 98 static PrefService* CreatePrefService(const FilePath& pref_filename, | 98 static PrefService* CreatePrefService(const FilePath& pref_filename, |
| 99 Profile* profile); | 99 Profile* profile); |
| 100 | 100 |
| 101 // Convenience factory method for use in unit tests. Creates a new | 101 // Convenience factory method for use in unit tests. Creates a new |
| 102 // PrefService that uses a PrefValueStore with user preferences at the given | 102 // PrefService that uses a PrefValueStore with user preferences at the given |
| 103 // |pref_filename|, and no other PrefStores (i.e., no other types of | 103 // |pref_filename|, a default PrefStore, and no other PrefStores (i.e., no |
| 104 // preferences). | 104 // other types of preferences). |
| 105 static PrefService* CreateUserPrefService(const FilePath& pref_filename); | 105 static PrefService* CreateUserPrefService(const FilePath& pref_filename); |
| 106 | 106 |
| 107 // This constructor is primarily used by tests. The |pref_value_store| | 107 // This constructor is primarily used by tests. The |pref_value_store| |
| 108 // provides preference values. | 108 // provides preference values. |
| 109 explicit PrefService(PrefValueStore* pref_value_store); | 109 explicit PrefService(PrefValueStore* pref_value_store); |
| 110 | 110 |
| 111 virtual ~PrefService(); | 111 virtual ~PrefService(); |
| 112 | 112 |
| 113 // Reloads the data from file. This should only be called when the importer | 113 // Reloads the data from file. This should only be called when the importer |
| 114 // is running during first run, and the main process may not change pref | 114 // is running during first run, and the main process may not change pref |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 | 164 |
| 165 // If the pref at the given path changes, we call the observer's Observe | 165 // If the pref at the given path changes, we call the observer's Observe |
| 166 // method with NOTIFY_PREF_CHANGED. | 166 // method with NOTIFY_PREF_CHANGED. |
| 167 virtual void AddPrefObserver(const char* path, NotificationObserver* obs); | 167 virtual void AddPrefObserver(const char* path, NotificationObserver* obs); |
| 168 virtual void RemovePrefObserver(const char* path, NotificationObserver* obs); | 168 virtual void RemovePrefObserver(const char* path, NotificationObserver* obs); |
| 169 | 169 |
| 170 // Removes a user pref and restores the pref to its default value. | 170 // Removes a user pref and restores the pref to its default value. |
| 171 void ClearPref(const char* path); | 171 void ClearPref(const char* path); |
| 172 | 172 |
| 173 // If the path is valid (i.e., registered), update the pref value in the user | 173 // If the path is valid (i.e., registered), update the pref value in the user |
| 174 // prefs. | 174 // prefs. Seting a null value on a preference of List or Dictionary type is |
| 175 // equivalent to removing the user value for that preference, allowing the |
| 176 // default value to take effect unless another value takes precedence. |
| 175 void Set(const char* path, const Value& value); | 177 void Set(const char* path, const Value& value); |
| 176 void SetBoolean(const char* path, bool value); | 178 void SetBoolean(const char* path, bool value); |
| 177 void SetInteger(const char* path, int value); | 179 void SetInteger(const char* path, int value); |
| 178 void SetReal(const char* path, double value); | 180 void SetReal(const char* path, double value); |
| 179 void SetString(const char* path, const std::string& value); | 181 void SetString(const char* path, const std::string& value); |
| 180 void SetFilePath(const char* path, const FilePath& value); | 182 void SetFilePath(const char* path, const FilePath& value); |
| 181 | 183 |
| 182 // Int64 helper methods that actually store the given value as a string. | 184 // Int64 helper methods that actually store the given value as a string. |
| 183 // Note that if obtaining the named value via GetDictionary or GetList, the | 185 // Note that if obtaining the named value via GetDictionary or GetList, the |
| 184 // Value type will be TYPE_STRING. | 186 // Value type will be TYPE_STRING. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 212 const PreferenceSet& preference_set() const { return prefs_; } | 214 const PreferenceSet& preference_set() const { return prefs_; } |
| 213 | 215 |
| 214 // A helper method to quickly look up a preference. Returns NULL if the | 216 // A helper method to quickly look up a preference. Returns NULL if the |
| 215 // preference is not registered. | 217 // preference is not registered. |
| 216 const Preference* FindPreference(const char* pref_name) const; | 218 const Preference* FindPreference(const char* pref_name) const; |
| 217 | 219 |
| 218 bool read_only() const { return pref_value_store_->ReadOnly(); } | 220 bool read_only() const { return pref_value_store_->ReadOnly(); } |
| 219 | 221 |
| 220 PrefNotifier* pref_notifier() const { return pref_notifier_.get(); } | 222 PrefNotifier* pref_notifier() const { return pref_notifier_.get(); } |
| 221 | 223 |
| 224 PrefValueStore* pref_value_store() const { return pref_value_store_.get(); } |
| 225 |
| 222 protected: | 226 protected: |
| 223 // The PrefNotifier handles registering and notifying preference observers. | 227 // The PrefNotifier handles registering and notifying preference observers. |
| 224 // It is created and owned by this PrefService. Subclasses may access it for | 228 // It is created and owned by this PrefService. Subclasses may access it for |
| 225 // unit testing. | 229 // unit testing. |
| 226 scoped_ptr<PrefNotifier> pref_notifier_; | 230 scoped_ptr<PrefNotifier> pref_notifier_; |
| 227 | 231 |
| 228 private: | 232 private: |
| 229 // Add a preference to the PreferenceMap. If the pref already exists, return | 233 // Add a preference to the PreferenceMap. If the pref already exists, return |
| 230 // false. This method takes ownership of |pref|. | 234 // false. This method takes ownership of |default_value|. |
| 231 void RegisterPreference(Preference* pref); | 235 void RegisterPreference(const char* path, Value* default_value); |
| 232 | 236 |
| 233 // Returns a copy of the current pref value. The caller is responsible for | 237 // Returns a copy of the current pref value. The caller is responsible for |
| 234 // deleting the returned object. | 238 // deleting the returned object. |
| 235 Value* GetPrefCopy(const char* pref_name); | 239 Value* GetPrefCopy(const char* pref_name); |
| 236 | 240 |
| 241 // Sets the value for this pref path in the user pref store and informs the |
| 242 // PrefNotifier of the change. |
| 243 void SetUserPrefValue(const char* path, Value* new_value); |
| 244 |
| 237 // Load from disk. Returns a non-zero error code on failure. | 245 // Load from disk. Returns a non-zero error code on failure. |
| 238 PrefStore::PrefReadError LoadPersistentPrefs(); | 246 PrefStore::PrefReadError LoadPersistentPrefs(); |
| 239 | 247 |
| 240 // Load preferences from storage, attempting to diagnose and handle errors. | 248 // Load preferences from storage, attempting to diagnose and handle errors. |
| 241 // This should only be called from the constructor. | 249 // This should only be called from the constructor. |
| 242 void InitFromStorage(); | 250 void InitFromStorage(); |
| 243 | 251 |
| 244 // The PrefValueStore provides prioritized preference values. It is created | 252 // The PrefValueStore provides prioritized preference values. It is created |
| 245 // and owned by this PrefService. Subclasses may access it for unit testing. | 253 // and owned by this PrefService. Subclasses may access it for unit testing. |
| 246 scoped_refptr<PrefValueStore> pref_value_store_; | 254 scoped_refptr<PrefValueStore> pref_value_store_; |
| 247 | 255 |
| 248 // A set of all the registered Preference objects. | 256 // A set of all the registered Preference objects. |
| 249 PreferenceSet prefs_; | 257 PreferenceSet prefs_; |
| 250 | 258 |
| 251 DISALLOW_COPY_AND_ASSIGN(PrefService); | 259 DISALLOW_COPY_AND_ASSIGN(PrefService); |
| 252 }; | 260 }; |
| 253 | 261 |
| 254 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ | 262 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ |
| OLD | NEW |