Bug 1349949 - Add the notion of an "override pref" to gfxPrefs.h. r?mchang draft
authorKartikaya Gupta <kgupta@mozilla.com>
Thu, 23 Mar 2017 16:29:52 -0400
changeset 503919 0d8ae0674cedef89bd0f771821b3247fb3c36254
parent 503906 200182ef115692c4ed2909f1a8beae8a6f19d127
child 503920 e0fabadef2e35aa9fab95f352819fe63c23c80dd
push id50708
push userkgupta@mozilla.com
push dateThu, 23 Mar 2017 20:30:23 +0000
reviewersmchang
bugs1349949
milestone55.0a1
Bug 1349949 - Add the notion of an "override pref" to gfxPrefs.h. r?mchang An override pref is one that is stored as a tri-state int32_t pref in the gecko preferences system (with values 0, 1, or 2), but is exposed as a bool via the gfxPrefs API. The int32_t value defaults to 2, which means that gfxPrefs will take the value from somewhere else (which can be customized per-pref). If the int32_t value stores 0 or 1, those correspond to force-disabled and force-enabled respectively, and the gfxPrefs API will return false or true. This allows the default value of a pref to be conditional upon other features (e.g. webrender being enabled or not), but still allows the user or code to override it into the enabled or disabled state. MozReview-Commit-ID: KaihVyHrw2q
gfx/thebes/gfxPrefs.h
--- a/gfx/thebes/gfxPrefs.h
+++ b/gfx/thebes/gfxPrefs.h
@@ -70,16 +70,42 @@ static void Set##Name(Type aVal) { MOZ_A
 static const char* Get##Name##PrefName() { return Prefname; }                 \
 static Type Get##Name##PrefDefault() { return Default; }                      \
 static void Set##Name##ChangeCallback(Pref::ChangeCallback aCallback) {       \
     MOZ_ASSERT(SingletonExists());                                            \
     GetSingleton().mPref##Name.SetChangeCallback(aCallback); }                \
 private:                                                                      \
 PrefTemplate<UpdatePolicy::Update, Type, Get##Name##PrefDefault, Get##Name##PrefName> mPref##Name
 
+// This declares an "override" pref, which is exposed as a "bool" pref by the API,
+// but is internally stored as a tri-state int pref with three possible values:
+// - A value of 0 means that it has been force-disabled, and is exposed as a
+//   false-valued bool.
+// - A value of 1 means that it has been force-enabled, and is exposed as a
+//   true-valued bool.
+// - A value of 2 (the default) means that it returns the provided BaseValue
+//   as a boolean. The BaseValue may be a constant expression or a function.
+// If the prefs defined with this macro are listed in prefs files (e.g. all.js),
+// then they must be listed with an int value (default to 2, but you can use 0
+// or 1 if you want to force it on or off).
+#define DECL_OVERRIDE_PREF(Update, Prefname, Name, BaseValue)                 \
+public:                                                                       \
+static bool Name() { MOZ_ASSERT(SingletonExists());                           \
+    int32_t val = GetSingleton().mPref##Name.mValue;                          \
+    return val == 2 ? !!(BaseValue) : !!val; }                                  \
+static void Set##Name(bool aVal) { MOZ_ASSERT(SingletonExists());             \
+    GetSingleton().mPref##Name.Set(UpdatePolicy::Update, Get##Name##PrefName(), aVal ? 1 : 0); } \
+static const char* Get##Name##PrefName() { return Prefname; }                 \
+static int32_t Get##Name##PrefDefault() { return 2; }                         \
+static void Set##Name##ChangeCallback(Pref::ChangeCallback aCallback) {       \
+    MOZ_ASSERT(SingletonExists());                                            \
+    GetSingleton().mPref##Name.SetChangeCallback(aCallback); }                \
+private:                                                                      \
+PrefTemplate<UpdatePolicy::Update, int32_t, Get##Name##PrefDefault, Get##Name##PrefName> mPref##Name
+
 namespace mozilla {
 namespace gfx {
 class GfxPrefValue;   // defined in PGPU.ipdl
 } // namespace gfx
 } // namespace mozilla
 
 class gfxPrefs;
 class gfxPrefs final