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