Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef mozilla_Preferences_h |
michael@0 | 7 | #define mozilla_Preferences_h |
michael@0 | 8 | |
michael@0 | 9 | #ifndef MOZILLA_INTERNAL_API |
michael@0 | 10 | #error "This header is only usable from within libxul (MOZILLA_INTERNAL_API)." |
michael@0 | 11 | #endif |
michael@0 | 12 | |
michael@0 | 13 | #include "nsIPrefService.h" |
michael@0 | 14 | #include "nsIPrefBranch.h" |
michael@0 | 15 | #include "nsIPrefBranchInternal.h" |
michael@0 | 16 | #include "nsIObserver.h" |
michael@0 | 17 | #include "nsCOMPtr.h" |
michael@0 | 18 | #include "nsTArray.h" |
michael@0 | 19 | #include "nsWeakReference.h" |
michael@0 | 20 | #include "mozilla/MemoryReporting.h" |
michael@0 | 21 | |
michael@0 | 22 | class nsIFile; |
michael@0 | 23 | class nsCString; |
michael@0 | 24 | class nsString; |
michael@0 | 25 | class nsAdoptingString; |
michael@0 | 26 | class nsAdoptingCString; |
michael@0 | 27 | |
michael@0 | 28 | #ifndef have_PrefChangedFunc_typedef |
michael@0 | 29 | typedef void (*PrefChangedFunc)(const char *, void *); |
michael@0 | 30 | #define have_PrefChangedFunc_typedef |
michael@0 | 31 | #endif |
michael@0 | 32 | |
michael@0 | 33 | namespace mozilla { |
michael@0 | 34 | |
michael@0 | 35 | namespace dom { |
michael@0 | 36 | class PrefSetting; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | class Preferences : public nsIPrefService, |
michael@0 | 40 | public nsIObserver, |
michael@0 | 41 | public nsIPrefBranchInternal, |
michael@0 | 42 | public nsSupportsWeakReference |
michael@0 | 43 | { |
michael@0 | 44 | public: |
michael@0 | 45 | typedef mozilla::dom::PrefSetting PrefSetting; |
michael@0 | 46 | |
michael@0 | 47 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 48 | NS_DECL_NSIPREFSERVICE |
michael@0 | 49 | NS_FORWARD_NSIPREFBRANCH(sRootBranch->) |
michael@0 | 50 | NS_DECL_NSIOBSERVER |
michael@0 | 51 | |
michael@0 | 52 | Preferences(); |
michael@0 | 53 | virtual ~Preferences(); |
michael@0 | 54 | |
michael@0 | 55 | nsresult Init(); |
michael@0 | 56 | |
michael@0 | 57 | /** |
michael@0 | 58 | * Reset loaded user prefs then read them |
michael@0 | 59 | */ |
michael@0 | 60 | static nsresult ResetAndReadUserPrefs(); |
michael@0 | 61 | |
michael@0 | 62 | /** |
michael@0 | 63 | * Returns the singleton instance which is addreffed. |
michael@0 | 64 | */ |
michael@0 | 65 | static Preferences* GetInstanceForService(); |
michael@0 | 66 | |
michael@0 | 67 | /** |
michael@0 | 68 | * Finallizes global members. |
michael@0 | 69 | */ |
michael@0 | 70 | static void Shutdown(); |
michael@0 | 71 | |
michael@0 | 72 | /** |
michael@0 | 73 | * Returns shared pref service instance |
michael@0 | 74 | * NOTE: not addreffed. |
michael@0 | 75 | */ |
michael@0 | 76 | static nsIPrefService* GetService() |
michael@0 | 77 | { |
michael@0 | 78 | NS_ENSURE_TRUE(InitStaticMembers(), nullptr); |
michael@0 | 79 | return sPreferences; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | /** |
michael@0 | 83 | * Returns shared pref branch instance. |
michael@0 | 84 | * NOTE: not addreffed. |
michael@0 | 85 | */ |
michael@0 | 86 | static nsIPrefBranch* GetRootBranch() |
michael@0 | 87 | { |
michael@0 | 88 | NS_ENSURE_TRUE(InitStaticMembers(), nullptr); |
michael@0 | 89 | return sRootBranch; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | /** |
michael@0 | 93 | * Returns shared default pref branch instance. |
michael@0 | 94 | * NOTE: not addreffed. |
michael@0 | 95 | */ |
michael@0 | 96 | static nsIPrefBranch* GetDefaultRootBranch() |
michael@0 | 97 | { |
michael@0 | 98 | NS_ENSURE_TRUE(InitStaticMembers(), nullptr); |
michael@0 | 99 | return sDefaultRootBranch; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | /** |
michael@0 | 103 | * Gets int or bool type pref value with default value if failed to get |
michael@0 | 104 | * the pref. |
michael@0 | 105 | */ |
michael@0 | 106 | static bool GetBool(const char* aPref, bool aDefault = false) |
michael@0 | 107 | { |
michael@0 | 108 | bool result = aDefault; |
michael@0 | 109 | GetBool(aPref, &result); |
michael@0 | 110 | return result; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | static int32_t GetInt(const char* aPref, int32_t aDefault = 0) |
michael@0 | 114 | { |
michael@0 | 115 | int32_t result = aDefault; |
michael@0 | 116 | GetInt(aPref, &result); |
michael@0 | 117 | return result; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | static uint32_t GetUint(const char* aPref, uint32_t aDefault = 0) |
michael@0 | 121 | { |
michael@0 | 122 | uint32_t result = aDefault; |
michael@0 | 123 | GetUint(aPref, &result); |
michael@0 | 124 | return result; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | static float GetFloat(const char* aPref, float aDefault = 0) |
michael@0 | 128 | { |
michael@0 | 129 | float result = aDefault; |
michael@0 | 130 | GetFloat(aPref, &result); |
michael@0 | 131 | return result; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | /** |
michael@0 | 135 | * Gets char type pref value directly. If failed, the get() of result |
michael@0 | 136 | * returns nullptr. Even if succeeded but the result was empty string, the |
michael@0 | 137 | * get() does NOT return nullptr. So, you can check whether the method |
michael@0 | 138 | * succeeded or not by: |
michael@0 | 139 | * |
michael@0 | 140 | * nsAdoptingString value = Prefereces::GetString("foo.bar"); |
michael@0 | 141 | * if (!value) { |
michael@0 | 142 | * // failed |
michael@0 | 143 | * } |
michael@0 | 144 | * |
michael@0 | 145 | * Be aware. If you wrote as: |
michael@0 | 146 | * |
michael@0 | 147 | * nsAutoString value = Preferences::GetString("foo.bar"); |
michael@0 | 148 | * if (!value.get()) { |
michael@0 | 149 | * // the condition is always FALSE!! |
michael@0 | 150 | * } |
michael@0 | 151 | * |
michael@0 | 152 | * The value.get() doesn't return nullptr. You must use nsAdoptingString |
michael@0 | 153 | * when you need to check whether it was failure or not. |
michael@0 | 154 | */ |
michael@0 | 155 | static nsAdoptingCString GetCString(const char* aPref); |
michael@0 | 156 | static nsAdoptingString GetString(const char* aPref); |
michael@0 | 157 | static nsAdoptingCString GetLocalizedCString(const char* aPref); |
michael@0 | 158 | static nsAdoptingString GetLocalizedString(const char* aPref); |
michael@0 | 159 | |
michael@0 | 160 | /** |
michael@0 | 161 | * Gets int, float, or bool type pref value with raw return value of |
michael@0 | 162 | * nsIPrefBranch. |
michael@0 | 163 | * |
michael@0 | 164 | * @param aPref A pref name. |
michael@0 | 165 | * @param aResult Must not be nullptr. The value is never modified |
michael@0 | 166 | * when these methods fail. |
michael@0 | 167 | */ |
michael@0 | 168 | static nsresult GetBool(const char* aPref, bool* aResult); |
michael@0 | 169 | static nsresult GetInt(const char* aPref, int32_t* aResult); |
michael@0 | 170 | static nsresult GetFloat(const char* aPref, float* aResult); |
michael@0 | 171 | static nsresult GetUint(const char* aPref, uint32_t* aResult) |
michael@0 | 172 | { |
michael@0 | 173 | int32_t result; |
michael@0 | 174 | nsresult rv = GetInt(aPref, &result); |
michael@0 | 175 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 176 | *aResult = static_cast<uint32_t>(result); |
michael@0 | 177 | } |
michael@0 | 178 | return rv; |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | /** |
michael@0 | 182 | * Gets string type pref value with raw return value of nsIPrefBranch. |
michael@0 | 183 | * |
michael@0 | 184 | * @param aPref A pref name. |
michael@0 | 185 | * @param aResult Must not be nullptr. The value is never modified |
michael@0 | 186 | * when these methods fail. |
michael@0 | 187 | */ |
michael@0 | 188 | static nsresult GetCString(const char* aPref, nsACString* aResult); |
michael@0 | 189 | static nsresult GetString(const char* aPref, nsAString* aResult); |
michael@0 | 190 | static nsresult GetLocalizedCString(const char* aPref, nsACString* aResult); |
michael@0 | 191 | static nsresult GetLocalizedString(const char* aPref, nsAString* aResult); |
michael@0 | 192 | |
michael@0 | 193 | static nsresult GetComplex(const char* aPref, const nsIID &aType, |
michael@0 | 194 | void** aResult); |
michael@0 | 195 | |
michael@0 | 196 | /** |
michael@0 | 197 | * Sets various type pref values. |
michael@0 | 198 | */ |
michael@0 | 199 | static nsresult SetBool(const char* aPref, bool aValue); |
michael@0 | 200 | static nsresult SetInt(const char* aPref, int32_t aValue); |
michael@0 | 201 | static nsresult SetUint(const char* aPref, uint32_t aValue) |
michael@0 | 202 | { |
michael@0 | 203 | return SetInt(aPref, static_cast<int32_t>(aValue)); |
michael@0 | 204 | } |
michael@0 | 205 | static nsresult SetCString(const char* aPref, const char* aValue); |
michael@0 | 206 | static nsresult SetCString(const char* aPref, const nsACString &aValue); |
michael@0 | 207 | static nsresult SetString(const char* aPref, const char16_t* aValue); |
michael@0 | 208 | static nsresult SetString(const char* aPref, const nsAString &aValue); |
michael@0 | 209 | |
michael@0 | 210 | static nsresult SetComplex(const char* aPref, const nsIID &aType, |
michael@0 | 211 | nsISupports* aValue); |
michael@0 | 212 | |
michael@0 | 213 | /** |
michael@0 | 214 | * Clears user set pref. |
michael@0 | 215 | */ |
michael@0 | 216 | static nsresult ClearUser(const char* aPref); |
michael@0 | 217 | |
michael@0 | 218 | /** |
michael@0 | 219 | * Whether the pref has a user value or not. |
michael@0 | 220 | */ |
michael@0 | 221 | static bool HasUserValue(const char* aPref); |
michael@0 | 222 | |
michael@0 | 223 | /** |
michael@0 | 224 | * Gets the type of the pref. |
michael@0 | 225 | */ |
michael@0 | 226 | static int32_t GetType(const char* aPref); |
michael@0 | 227 | |
michael@0 | 228 | /** |
michael@0 | 229 | * Adds/Removes the observer for the root pref branch. |
michael@0 | 230 | * The observer is referenced strongly if AddStrongObserver is used. On the |
michael@0 | 231 | * other hand, it is referenced weakly, if AddWeakObserver is used. |
michael@0 | 232 | * See nsIPrefBranch.idl for details. |
michael@0 | 233 | */ |
michael@0 | 234 | static nsresult AddStrongObserver(nsIObserver* aObserver, const char* aPref); |
michael@0 | 235 | static nsresult AddWeakObserver(nsIObserver* aObserver, const char* aPref); |
michael@0 | 236 | static nsresult RemoveObserver(nsIObserver* aObserver, const char* aPref); |
michael@0 | 237 | |
michael@0 | 238 | /** |
michael@0 | 239 | * Adds/Removes two or more observers for the root pref branch. |
michael@0 | 240 | * Pass to aPrefs an array of const char* whose last item is nullptr. |
michael@0 | 241 | */ |
michael@0 | 242 | static nsresult AddStrongObservers(nsIObserver* aObserver, |
michael@0 | 243 | const char** aPrefs); |
michael@0 | 244 | static nsresult AddWeakObservers(nsIObserver* aObserver, |
michael@0 | 245 | const char** aPrefs); |
michael@0 | 246 | static nsresult RemoveObservers(nsIObserver* aObserver, |
michael@0 | 247 | const char** aPrefs); |
michael@0 | 248 | |
michael@0 | 249 | /** |
michael@0 | 250 | * Registers/Unregisters the callback function for the aPref. |
michael@0 | 251 | */ |
michael@0 | 252 | static nsresult RegisterCallback(PrefChangedFunc aCallback, |
michael@0 | 253 | const char* aPref, |
michael@0 | 254 | void* aClosure = nullptr); |
michael@0 | 255 | static nsresult UnregisterCallback(PrefChangedFunc aCallback, |
michael@0 | 256 | const char* aPref, |
michael@0 | 257 | void* aClosure = nullptr); |
michael@0 | 258 | // Like RegisterCallback, but also calls the callback immediately for |
michael@0 | 259 | // initialization. |
michael@0 | 260 | static nsresult RegisterCallbackAndCall(PrefChangedFunc aCallback, |
michael@0 | 261 | const char* aPref, |
michael@0 | 262 | void* aClosure = nullptr); |
michael@0 | 263 | |
michael@0 | 264 | /** |
michael@0 | 265 | * Adds the aVariable to cache table. aVariable must be a pointer for a |
michael@0 | 266 | * static variable. The value will be modified when the pref value is |
michael@0 | 267 | * changed but note that even if you modified it, the value isn't assigned to |
michael@0 | 268 | * the pref. |
michael@0 | 269 | */ |
michael@0 | 270 | static nsresult AddBoolVarCache(bool* aVariable, |
michael@0 | 271 | const char* aPref, |
michael@0 | 272 | bool aDefault = false); |
michael@0 | 273 | static nsresult AddIntVarCache(int32_t* aVariable, |
michael@0 | 274 | const char* aPref, |
michael@0 | 275 | int32_t aDefault = 0); |
michael@0 | 276 | static nsresult AddUintVarCache(uint32_t* aVariable, |
michael@0 | 277 | const char* aPref, |
michael@0 | 278 | uint32_t aDefault = 0); |
michael@0 | 279 | static nsresult AddFloatVarCache(float* aVariable, |
michael@0 | 280 | const char* aPref, |
michael@0 | 281 | float aDefault = 0.0f); |
michael@0 | 282 | |
michael@0 | 283 | /** |
michael@0 | 284 | * Gets the default bool, int or uint value of the pref. |
michael@0 | 285 | * The result is raw result of nsIPrefBranch::Get*Pref(). |
michael@0 | 286 | * If the pref could have any value, you needed to use these methods. |
michael@0 | 287 | * If not so, you could use below methods. |
michael@0 | 288 | */ |
michael@0 | 289 | static nsresult GetDefaultBool(const char* aPref, bool* aResult); |
michael@0 | 290 | static nsresult GetDefaultInt(const char* aPref, int32_t* aResult); |
michael@0 | 291 | static nsresult GetDefaultUint(const char* aPref, uint32_t* aResult) |
michael@0 | 292 | { |
michael@0 | 293 | return GetDefaultInt(aPref, reinterpret_cast<int32_t*>(aResult)); |
michael@0 | 294 | } |
michael@0 | 295 | |
michael@0 | 296 | /** |
michael@0 | 297 | * Gets the default bool, int or uint value of the pref directly. |
michael@0 | 298 | * You can set an invalid value of the pref to aFailedResult. If these |
michael@0 | 299 | * methods failed to get the default value, they would return the |
michael@0 | 300 | * aFailedResult value. |
michael@0 | 301 | */ |
michael@0 | 302 | static bool GetDefaultBool(const char* aPref, bool aFailedResult) |
michael@0 | 303 | { |
michael@0 | 304 | bool result; |
michael@0 | 305 | return NS_SUCCEEDED(GetDefaultBool(aPref, &result)) ? result : |
michael@0 | 306 | aFailedResult; |
michael@0 | 307 | } |
michael@0 | 308 | static int32_t GetDefaultInt(const char* aPref, int32_t aFailedResult) |
michael@0 | 309 | { |
michael@0 | 310 | int32_t result; |
michael@0 | 311 | return NS_SUCCEEDED(GetDefaultInt(aPref, &result)) ? result : aFailedResult; |
michael@0 | 312 | } |
michael@0 | 313 | static uint32_t GetDefaultUint(const char* aPref, uint32_t aFailedResult) |
michael@0 | 314 | { |
michael@0 | 315 | return static_cast<uint32_t>( |
michael@0 | 316 | GetDefaultInt(aPref, static_cast<int32_t>(aFailedResult))); |
michael@0 | 317 | } |
michael@0 | 318 | |
michael@0 | 319 | /** |
michael@0 | 320 | * Gets the default value of the char type pref. |
michael@0 | 321 | * If the get() of the result returned nullptr, that meant the value didn't |
michael@0 | 322 | * have default value. |
michael@0 | 323 | * |
michael@0 | 324 | * See the comment at definition at GetString() and GetCString() for more |
michael@0 | 325 | * details of the result. |
michael@0 | 326 | */ |
michael@0 | 327 | static nsAdoptingString GetDefaultString(const char* aPref); |
michael@0 | 328 | static nsAdoptingCString GetDefaultCString(const char* aPref); |
michael@0 | 329 | static nsAdoptingString GetDefaultLocalizedString(const char* aPref); |
michael@0 | 330 | static nsAdoptingCString GetDefaultLocalizedCString(const char* aPref); |
michael@0 | 331 | |
michael@0 | 332 | static nsresult GetDefaultCString(const char* aPref, nsACString* aResult); |
michael@0 | 333 | static nsresult GetDefaultString(const char* aPref, nsAString* aResult); |
michael@0 | 334 | static nsresult GetDefaultLocalizedCString(const char* aPref, |
michael@0 | 335 | nsACString* aResult); |
michael@0 | 336 | static nsresult GetDefaultLocalizedString(const char* aPref, |
michael@0 | 337 | nsAString* aResult); |
michael@0 | 338 | |
michael@0 | 339 | static nsresult GetDefaultComplex(const char* aPref, const nsIID &aType, |
michael@0 | 340 | void** aResult); |
michael@0 | 341 | |
michael@0 | 342 | /** |
michael@0 | 343 | * Gets the type of the pref. |
michael@0 | 344 | */ |
michael@0 | 345 | static int32_t GetDefaultType(const char* aPref); |
michael@0 | 346 | |
michael@0 | 347 | // Used to synchronise preferences between chrome and content processes. |
michael@0 | 348 | static void GetPreferences(InfallibleTArray<PrefSetting>* aPrefs); |
michael@0 | 349 | static void GetPreference(PrefSetting* aPref); |
michael@0 | 350 | static void SetPreference(const PrefSetting& aPref); |
michael@0 | 351 | |
michael@0 | 352 | static int64_t SizeOfIncludingThisAndOtherStuff(mozilla::MallocSizeOf aMallocSizeOf); |
michael@0 | 353 | static nsresult SetFloat(const char* aPref, float aValue); |
michael@0 | 354 | |
michael@0 | 355 | protected: |
michael@0 | 356 | nsresult NotifyServiceObservers(const char *aSubject); |
michael@0 | 357 | /** |
michael@0 | 358 | * Reads the default pref file or, if that failed, try to save a new one. |
michael@0 | 359 | * |
michael@0 | 360 | * @return NS_OK if either action succeeded, |
michael@0 | 361 | * or the error code related to the read attempt. |
michael@0 | 362 | */ |
michael@0 | 363 | nsresult UseDefaultPrefFile(); |
michael@0 | 364 | nsresult UseUserPrefFile(); |
michael@0 | 365 | nsresult ReadAndOwnUserPrefFile(nsIFile *aFile); |
michael@0 | 366 | nsresult ReadAndOwnSharedUserPrefFile(nsIFile *aFile); |
michael@0 | 367 | nsresult SavePrefFileInternal(nsIFile* aFile); |
michael@0 | 368 | nsresult WritePrefFile(nsIFile* aFile); |
michael@0 | 369 | nsresult MakeBackupPrefFile(nsIFile *aFile); |
michael@0 | 370 | |
michael@0 | 371 | private: |
michael@0 | 372 | nsCOMPtr<nsIFile> mCurrentFile; |
michael@0 | 373 | |
michael@0 | 374 | static Preferences* sPreferences; |
michael@0 | 375 | static nsIPrefBranch* sRootBranch; |
michael@0 | 376 | static nsIPrefBranch* sDefaultRootBranch; |
michael@0 | 377 | static bool sShutdown; |
michael@0 | 378 | |
michael@0 | 379 | /** |
michael@0 | 380 | * Init static members. TRUE if it succeeded. Otherwise, FALSE. |
michael@0 | 381 | */ |
michael@0 | 382 | static bool InitStaticMembers(); |
michael@0 | 383 | }; |
michael@0 | 384 | |
michael@0 | 385 | } // namespace mozilla |
michael@0 | 386 | |
michael@0 | 387 | #endif // mozilla_Preferences_h |