OLD | NEW |
(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 is the base interface for a preference services that provides |
| 6 // a way to access the application's current preferences. |
| 7 // |
| 8 // This base interface assumes all preferences are local. See |
| 9 // SyncablePrefServiceBase for the interface to a preference service |
| 10 // that stores preferences that can be synced. |
| 11 // |
| 12 // Chromium settings and storage represent user-selected preferences and |
| 13 // information and MUST not be extracted, overwritten or modified except |
| 14 // through Chromium defined APIs. |
| 15 |
| 16 #ifndef CHROME_BROWSER_API_PREFS_PREF_SERVICE_BASE_H_ |
| 17 #define CHROME_BROWSER_API_PREFS_PREF_SERVICE_BASE_H_ |
| 18 |
| 19 #include "base/values.h" |
| 20 |
| 21 namespace content { |
| 22 class NotificationObserver; |
| 23 } |
| 24 |
| 25 namespace subtle { |
| 26 class PrefMemberBase; |
| 27 } |
| 28 |
| 29 class FilePath; |
| 30 class Profile; |
| 31 class TabContents; |
| 32 |
| 33 class PrefServiceBase { |
| 34 public: |
| 35 // Retrieves a PrefServiceBase for the given profile. |
| 36 static PrefServiceBase* ForProfile(Profile* profile); |
| 37 |
| 38 virtual ~PrefServiceBase() {} |
| 39 |
| 40 // Enum used when registering preferences to determine if it should be synced |
| 41 // or not. This is only used for profile prefs, not local state prefs. |
| 42 // See the Register*Pref methods for profile prefs below. |
| 43 enum PrefSyncStatus { |
| 44 UNSYNCABLE_PREF, |
| 45 SYNCABLE_PREF |
| 46 }; |
| 47 |
| 48 // Interface to a single preference. |
| 49 class Preference { |
| 50 public: |
| 51 virtual ~Preference() {} |
| 52 |
| 53 // Returns the name of the Preference (i.e., the key, e.g., |
| 54 // browser.window_placement). |
| 55 virtual const std::string name() const = 0; |
| 56 |
| 57 // Returns the registered type of the preference. |
| 58 virtual base::Value::Type GetType() const = 0; |
| 59 |
| 60 // Returns the value of the Preference, falling back to the registered |
| 61 // default value if no other has been set. |
| 62 virtual const base::Value* GetValue() const = 0; |
| 63 |
| 64 // Returns the value recommended by the admin, if any. |
| 65 virtual const base::Value* GetRecommendedValue() const = 0; |
| 66 |
| 67 // Returns true if the Preference is managed, i.e. set by an admin policy. |
| 68 // Since managed prefs have the highest priority, this also indicates |
| 69 // whether the pref is actually being controlled by the policy setting. |
| 70 virtual bool IsManaged() const = 0; |
| 71 |
| 72 // Returns true if the Preference is recommended, i.e. set by an admin |
| 73 // policy but the user is allowed to change it. |
| 74 virtual bool IsRecommended() const = 0; |
| 75 |
| 76 // Returns true if the Preference has a value set by an extension, even if |
| 77 // that value is being overridden by a higher-priority source. |
| 78 virtual bool HasExtensionSetting() const = 0; |
| 79 |
| 80 // Returns true if the Preference has a user setting, even if that value is |
| 81 // being overridden by a higher-priority source. |
| 82 virtual bool HasUserSetting() const = 0; |
| 83 |
| 84 // Returns true if the Preference value is currently being controlled by an |
| 85 // extension, and not by any higher-priority source. |
| 86 virtual bool IsExtensionControlled() const = 0; |
| 87 |
| 88 // Returns true if the Preference value is currently being controlled by a |
| 89 // user setting, and not by any higher-priority source. |
| 90 virtual bool IsUserControlled() const = 0; |
| 91 |
| 92 // Returns true if the Preference is currently using its default value, |
| 93 // and has not been set by any higher-priority source (even with the same |
| 94 // value). |
| 95 virtual bool IsDefaultValue() const = 0; |
| 96 |
| 97 // Returns true if the user can change the Preference value, which is the |
| 98 // case if no higher-priority source than the user store controls the |
| 99 // Preference. |
| 100 virtual bool IsUserModifiable() const = 0; |
| 101 |
| 102 // Returns true if an extension can change the Preference value, which is |
| 103 // the case if no higher-priority source than the extension store controls |
| 104 // the Preference. |
| 105 virtual bool IsExtensionModifiable() const = 0; |
| 106 }; |
| 107 |
| 108 // Returns true if the preference for the given preference name is available |
| 109 // and is managed. |
| 110 virtual bool IsManagedPreference(const char* pref_name) const = 0; |
| 111 |
| 112 // Returns |true| if a preference with the given name is available and its |
| 113 // value can be changed by the user. |
| 114 virtual bool IsUserModifiablePreference(const char* pref_name) const = 0; |
| 115 |
| 116 // Make the PrefService aware of a pref. |
| 117 // TODO(zea): split local state and profile prefs into their own subclasses. |
| 118 // ---------- Local state prefs ---------- |
| 119 virtual void RegisterBooleanPref(const char* path, |
| 120 bool default_value) = 0; |
| 121 virtual void RegisterIntegerPref(const char* path, |
| 122 int default_value) = 0; |
| 123 virtual void RegisterDoublePref(const char* path, |
| 124 double default_value) = 0; |
| 125 virtual void RegisterStringPref(const char* path, |
| 126 const std::string& default_value) = 0; |
| 127 virtual void RegisterFilePathPref(const char* path, |
| 128 const FilePath& default_value) = 0; |
| 129 virtual void RegisterListPref(const char* path) = 0; |
| 130 virtual void RegisterDictionaryPref(const char* path) = 0; |
| 131 // These take ownership of the default_value: |
| 132 virtual void RegisterListPref(const char* path, |
| 133 base::ListValue* default_value) = 0; |
| 134 virtual void RegisterDictionaryPref( |
| 135 const char* path, base::DictionaryValue* default_value) = 0; |
| 136 // These variants use a default value from the locale dll instead. |
| 137 virtual void RegisterLocalizedBooleanPref( |
| 138 const char* path, int locale_default_message_id) = 0; |
| 139 virtual void RegisterLocalizedIntegerPref( |
| 140 const char* path, int locale_default_message_id) = 0; |
| 141 virtual void RegisterLocalizedDoublePref( |
| 142 const char* path, int locale_default_message_id) = 0; |
| 143 virtual void RegisterLocalizedStringPref( |
| 144 const char* path, int locale_default_message_id) = 0; |
| 145 virtual void RegisterInt64Pref(const char* path, |
| 146 int64 default_value) = 0; |
| 147 |
| 148 // ---------- Profile prefs ---------- |
| 149 // Profile prefs must specify whether the pref should be synchronized across |
| 150 // machines or not (see PrefSyncStatus enum above). |
| 151 virtual void RegisterBooleanPref(const char* path, |
| 152 bool default_value, |
| 153 PrefSyncStatus sync_status) = 0; |
| 154 virtual void RegisterIntegerPref(const char* path, |
| 155 int default_value, |
| 156 PrefSyncStatus sync_status) = 0; |
| 157 virtual void RegisterDoublePref(const char* path, |
| 158 double default_value, |
| 159 PrefSyncStatus sync_status) = 0; |
| 160 virtual void RegisterStringPref(const char* path, |
| 161 const std::string& default_value, |
| 162 PrefSyncStatus sync_status) = 0; |
| 163 virtual void RegisterFilePathPref(const char* path, |
| 164 const FilePath& default_value, |
| 165 PrefSyncStatus sync_status) = 0; |
| 166 virtual void RegisterListPref(const char* path, |
| 167 PrefSyncStatus sync_status) = 0; |
| 168 virtual void RegisterDictionaryPref(const char* path, |
| 169 PrefSyncStatus sync_status) = 0; |
| 170 // These take ownership of the default_value: |
| 171 virtual void RegisterListPref(const char* path, |
| 172 base::ListValue* default_value, |
| 173 PrefSyncStatus sync_status) = 0; |
| 174 virtual void RegisterDictionaryPref(const char* path, |
| 175 base::DictionaryValue* default_value, |
| 176 PrefSyncStatus sync_status) = 0; |
| 177 // These variants use a default value from the locale dll instead. |
| 178 virtual void RegisterLocalizedBooleanPref( |
| 179 const char* path, |
| 180 int locale_default_message_id, |
| 181 PrefSyncStatus sync_status) = 0; |
| 182 virtual void RegisterLocalizedIntegerPref( |
| 183 const char* path, |
| 184 int locale_default_message_id, |
| 185 PrefSyncStatus sync_status) = 0; |
| 186 virtual void RegisterLocalizedDoublePref( |
| 187 const char* path, |
| 188 int locale_default_message_id, |
| 189 PrefSyncStatus sync_status) = 0; |
| 190 virtual void RegisterLocalizedStringPref( |
| 191 const char* path, |
| 192 int locale_default_message_id, |
| 193 PrefSyncStatus sync_status) = 0; |
| 194 virtual void RegisterInt64Pref(const char* path, |
| 195 int64 default_value, |
| 196 PrefSyncStatus sync_status) = 0; |
| 197 virtual void RegisterUint64Pref(const char* path, |
| 198 uint64 default_value, |
| 199 PrefSyncStatus sync_status) = 0; |
| 200 // Unregisters a preference. |
| 201 virtual void UnregisterPreference(const char* path) = 0; |
| 202 |
| 203 // Look up a preference. Returns NULL if the preference is not |
| 204 // registered. |
| 205 virtual const Preference* FindPreference(const char* pref_name) const = 0; |
| 206 |
| 207 // If the path is valid and the value at the end of the path matches the type |
| 208 // specified, it will return the specified value. Otherwise, the default |
| 209 // value (set when the pref was registered) will be returned. |
| 210 virtual bool GetBoolean(const char* path) const = 0; |
| 211 virtual int GetInteger(const char* path) const = 0; |
| 212 virtual double GetDouble(const char* path) const = 0; |
| 213 virtual std::string GetString(const char* path) const = 0; |
| 214 virtual FilePath GetFilePath(const char* path) const = 0; |
| 215 |
| 216 // Returns the branch if it exists, or the registered default value otherwise. |
| 217 // Note that |path| must point to a registered preference. In that case, these |
| 218 // functions will never return NULL. |
| 219 virtual const base::DictionaryValue* GetDictionary( |
| 220 const char* path) const = 0; |
| 221 virtual const base::ListValue* GetList(const char* path) const = 0; |
| 222 |
| 223 // Removes a user pref and restores the pref to its default value. |
| 224 virtual void ClearPref(const char* path) = 0; |
| 225 |
| 226 // If the path is valid (i.e., registered), update the pref value in the user |
| 227 // prefs. |
| 228 // To set the value of dictionary or list values in the pref tree use |
| 229 // Set(), but to modify the value of a dictionary or list use either |
| 230 // ListPrefUpdate or DictionaryPrefUpdate from scoped_user_pref_update.h. |
| 231 virtual void Set(const char* path, const base::Value& value) = 0; |
| 232 virtual void SetBoolean(const char* path, bool value) = 0; |
| 233 virtual void SetInteger(const char* path, int value) = 0; |
| 234 virtual void SetDouble(const char* path, double value) = 0; |
| 235 virtual void SetString(const char* path, const std::string& value) = 0; |
| 236 virtual void SetFilePath(const char* path, const FilePath& value) = 0; |
| 237 |
| 238 // Int64 helper methods that actually store the given value as a string. |
| 239 // Note that if obtaining the named value via GetDictionary or GetList, the |
| 240 // Value type will be TYPE_STRING. |
| 241 virtual void SetInt64(const char* path, int64 value) = 0; |
| 242 virtual int64 GetInt64(const char* path) const = 0; |
| 243 |
| 244 // As above, but for unsigned values. |
| 245 virtual void SetUint64(const char* path, uint64 value) = 0; |
| 246 virtual uint64 GetUint64(const char* path) const = 0; |
| 247 |
| 248 protected: |
| 249 // Registration of pref change observers must be done using the |
| 250 // PrefChangeRegistrar, which is declared as a friend here to grant it |
| 251 // access to the otherwise protected members Add/RemovePrefObserver. |
| 252 // PrefMember registers for preferences changes notification directly to |
| 253 // avoid the storage overhead of the registrar, so its base class must be |
| 254 // declared as a friend, too. |
| 255 friend class PrefChangeRegistrar; |
| 256 friend class subtle::PrefMemberBase; |
| 257 |
| 258 // These are protected so they can only be accessed by the friend |
| 259 // classes listed above. |
| 260 // |
| 261 // If the pref at the given path changes, we call the observer's Observe |
| 262 // method with PREF_CHANGED. Note that observers should not call these methods |
| 263 // directly but rather use a PrefChangeRegistrar to make sure the observer |
| 264 // gets cleaned up properly. |
| 265 virtual void AddPrefObserver(const char* path, |
| 266 content::NotificationObserver* obs) = 0; |
| 267 virtual void RemovePrefObserver(const char* path, |
| 268 content::NotificationObserver* obs) = 0; |
| 269 }; |
| 270 |
| 271 #endif // CHROME_BROWSER_API_PREFS_PREF_SERVICE_BASE_H_ |
OLD | NEW |