/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Documentation for libpref is in modules/libpref/docs/index.rst.
// The callback function will get passed the pref name which triggered the call // and the void* data which was passed to the registered callback function. typedefvoid (*PrefChangedFunc)(constchar* aPref, void* aData);
class nsPrefBranch;
namespace mozilla {
struct RegisterCallbacksInternal;
void UnloadPrefsModule();
class PreferenceServiceReporter;
namespace dom { class Pref; class PrefValue;
} // namespace dom
namespace ipc { class FileDescriptor;
} // namespace ipc
struct PrefsSizes;
// Xlib.h defines Bool as a macro constant. Don't try to define this enum if // it's already been included. #ifndefBool
// Keep this in sync with PrefType in parser/src/lib.rs. enumclass PrefType : uint8_t {
None = 0, // only used when neither the default nor user value is set
String = 1, Int = 2, Bool = 3,
};
#endif
#ifdef XP_UNIX // We need to send two shared memory descriptors to every child process: // // 1) A read-only/write-protected snapshot of the initial state of the // preference database. This memory is shared between all processes, and // therefore cannot be modified once it has been created. // // 2) A set of changes on top of the snapshot, containing the current values of // all preferences which have changed since it was created. // // Since the second set will be different for every process, and the first set // cannot be modified, it is unfortunately not possible to combine them into a // single file descriptor. // // XXX: bug 1440207 is about improving how fixed fds such as this are used. staticconstint kPrefsFileDescriptor = 8; staticconstint kPrefMapFileDescriptor = 9; #endif
// Keep this in sync with PrefType in parser/src/lib.rs. enumclass PrefValueKind : uint8_t { Default, User };
class Preferences final : public nsIPrefService, public nsIObserver, public nsIPrefBranch, public nsSupportsWeakReference { friendclass ::nsPrefBranch;
public: using WritePrefFilePromise = MozPromise<bool, nsresult, false>;
// Gets the type of the pref. static int32_t GetType(constchar* aPrefName);
// Fallible value getters. When `aKind` is `User` they will get the user // value if possible, and fall back to the default value otherwise. static nsresult GetBool(constchar* aPrefName, bool* aResult,
PrefValueKind aKind = PrefValueKind::User); static nsresult GetInt(constchar* aPrefName, int32_t* aResult,
PrefValueKind aKind = PrefValueKind::User); static nsresult GetUint(constchar* aPrefName, uint32_t* aResult,
PrefValueKind aKind = PrefValueKind::User) { return GetInt(aPrefName, reinterpret_cast<int32_t*>(aResult), aKind);
} static nsresult GetFloat(constchar* aPrefName, float* aResult,
PrefValueKind aKind = PrefValueKind::User); static nsresult GetCString(constchar* aPrefName, nsACString& aResult,
PrefValueKind aKind = PrefValueKind::User); static nsresult GetString(constchar* aPrefName, nsAString& aResult,
PrefValueKind aKind = PrefValueKind::User); static nsresult GetLocalizedCString( constchar* aPrefName, nsACString& aResult,
PrefValueKind aKind = PrefValueKind::User); static nsresult GetLocalizedString(constchar* aPrefName, nsAString& aResult,
PrefValueKind aKind = PrefValueKind::User); static nsresult GetComplex(constchar* aPrefName, const nsIID& aType, void** aResult,
PrefValueKind aKind = PrefValueKind::User);
// Infallible getters of user or default values, with fallback results on // failure. When `aKind` is `User` they will get the user value if possible, // and fall back to the default value otherwise. staticbool GetBool(constchar* aPrefName, bool aFallback = false,
PrefValueKind aKind = PrefValueKind::User); static int32_t GetInt(constchar* aPrefName, int32_t aFallback = 0,
PrefValueKind aKind = PrefValueKind::User); static uint32_t GetUint(constchar* aPrefName, uint32_t aFallback = 0,
PrefValueKind aKind = PrefValueKind::User); staticfloat GetFloat(constchar* aPrefName, float aFallback = 0.0f,
PrefValueKind aKind = PrefValueKind::User);
// Value setters. These fail if run outside the parent process.
// Adds/Removes two or more observers for the root pref branch. Pass to // aPrefs an array of const char* whose last item is nullptr. // Note: All preference strings *must* be statically-allocated string // literals. static nsresult AddStrongObservers(nsIObserver* aObserver, constchar* const* aPrefs); static nsresult AddWeakObservers(nsIObserver* aObserver, constchar* const* aPrefs); static nsresult RemoveObservers(nsIObserver* aObserver, constchar* const* aPrefs);
// Registers/Unregisters the callback function for the aPref. template <typename T = void> static nsresult RegisterCallback(PrefChangedFunc aCallback, const nsACString& aPref,
T* aClosure = nullptr) { return RegisterCallback(aCallback, aPref, aClosure, ExactMatch);
}
// Like RegisterCallback, but also calls the callback immediately for // initialization. template <typename T = void> static nsresult RegisterCallbackAndCall(PrefChangedFunc aCallback, const nsACString& aPref,
T* aClosure = nullptr) { return RegisterCallbackAndCall(aCallback, aPref, aClosure, ExactMatch);
}
// Like RegisterCallback, but registers a callback for a prefix of multiple // pref names, not a single pref name. template <typename T = void> static nsresult RegisterPrefixCallback(PrefChangedFunc aCallback, const nsACString& aPref,
T* aClosure = nullptr) { return RegisterCallback(aCallback, aPref, aClosure, PrefixMatch);
}
// Like RegisterPrefixCallback, but also calls the callback immediately for // initialization. template <typename T = void> static nsresult RegisterPrefixCallbackAndCall(PrefChangedFunc aCallback, const nsACString& aPref,
T* aClosure = nullptr) { return RegisterCallbackAndCall(aCallback, aPref, aClosure, PrefixMatch);
}
// Unregister a callback registered with RegisterPrefixCallback or // RegisterPrefixCallbackAndCall. template <typename T = void> static nsresult UnregisterPrefixCallback(PrefChangedFunc aCallback, const nsACString& aPref,
T* aClosure = nullptr) { return UnregisterCallback(aCallback, aPref, aClosure, PrefixMatch);
}
// Variants of the above which register a single callback to handle multiple // preferences. // // The array of preference names must be null terminated. It may be // dynamically allocated, but the caller is responsible for keeping it alive // until the callback is unregistered. // // Also note that the exact same aPrefs pointer must be passed to the // Unregister call as was passed to the Register call. template <typename T = void> static nsresult RegisterCallbacks(PrefChangedFunc aCallback, constchar* const* aPrefs,
T* aClosure = nullptr) { return RegisterCallbacks(aCallback, aPrefs, aClosure, ExactMatch);
} static nsresult RegisterCallbacksAndCall(PrefChangedFunc aCallback, constchar* const* aPrefs, void* aClosure = nullptr); template <typename T = void> static nsresult UnregisterCallbacks(PrefChangedFunc aCallback, constchar* const* aPrefs,
T* aClosure = nullptr) { return UnregisterCallbacks(aCallback, aPrefs, aClosure, ExactMatch);
} template <typename T = void> static nsresult RegisterPrefixCallbacks(PrefChangedFunc aCallback, constchar* const* aPrefs,
T* aClosure = nullptr) { return RegisterCallbacks(aCallback, aPrefs, aClosure, PrefixMatch);
} template <typename T = void> static nsresult UnregisterPrefixCallbacks(PrefChangedFunc aCallback, constchar* const* aPrefs,
T* aClosure = nullptr) { return UnregisterCallbacks(aCallback, aPrefs, aClosure, PrefixMatch);
}
// When a content process is created these methods are used to pass changed // prefs in bulk from the parent process, via shared memory. staticvoid SerializePreferences(nsCString& aStr, bool aIsDestinationWebContentProcess); staticvoid DeserializePreferences(char* aStr, size_t aPrefsLen);
// When a single pref is changed in the parent process, these methods are // used to pass the update to content processes. staticvoid GetPreference(dom::Pref* aPref, const GeckoProcessType aDestinationProcessType, const nsACString& aDestinationRemoteType); staticvoid SetPreference(const dom::Pref& aPref);
// Explicitly choosing synchronous or asynchronous (if allowed) preferences // file write. Only for the default file. The guarantee for the "blocking" // is that when it returns, the file on disk reflect the current state of // preferences.
nsresult SavePrefFileBlocking();
nsresult SavePrefFileAsynchronous();
// If this is false, only blocking writes, on main thread are allowed. bool AllowOffMainThreadSave();
// Loads the prefs.js file from the profile, or creates a new one. Returns // the prefs file if successful, or nullptr on failure.
already_AddRefed<nsIFile> ReadSavedPrefs();
// Loads the user.js file from the profile if present. void ReadUserOverridePrefs();
nsresult MakeBackupPrefFile(nsIFile* aFile);
// Default pref file save can be blocking or not. enumclass SaveMethod { Blocking, Asynchronous };
// Off main thread is only respected for the default aFile value (nullptr).
nsresult SavePrefFileInternal(nsIFile* aFile, SaveMethod aSaveMethod);
nsresult WritePrefFile(
nsIFile* aFile, SaveMethod aSaveMethod,
UniquePtr<MozPromiseHolder<WritePrefFilePromise>> aPromise = nullptr);
nsresult ResetUserPrefs();
// Helpers for implementing // Register(Prefix)Callback/Unregister(Prefix)Callback. public: // Public so the ValueObserver classes can use it. enum MatchKind {
PrefixMatch,
ExactMatch,
};
private:
nsCOMPtr<nsIFile> mCurrentFile; // Time since unix epoch in ms (JS Date compatible)
PRTime mUserPrefsFileLastModifiedAtStartup = 0; bool mDirty = false; bool mProfileShutdown = false; // We wait a bit after prefs are dirty before writing them. In this period, // mDirty and mSavePending will both be true. bool mSavePending = false;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.