Bug 1435943 - Tweak Preferences::GetType() and nsPrefBranch::GetPrefType(). r=glandium draft
authorNicholas Nethercote <nnethercote@mozilla.com>
Tue, 06 Feb 2018 17:08:07 +1100
changeset 751397 bc405710e55a5943b042c27d56fab18f4316a60b
parent 751394 c8825182a7c8b6a986a89b1ecda2567f2fef0ac6
push id97961
push usernnethercote@mozilla.com
push dateTue, 06 Feb 2018 06:08:37 +0000
reviewersglandium
bugs1435943
milestone60.0a1
Bug 1435943 - Tweak Preferences::GetType() and nsPrefBranch::GetPrefType(). r=glandium This patch rearranges these functions so that nsPrefBranch::GetPrefType() calls into Preferences::GetType(), because all other nsPrefBranch methods depend on Preferences methods. The patch also removes the `aKind` argument from GetType(), because it has no effect -- a pref only has one type, regardless of whether it has a default value, a user value, or both. MozReview-Commit-ID: J3vxFPaP8S3
modules/libpref/Preferences.cpp
modules/libpref/Preferences.h
--- a/modules/libpref/Preferences.cpp
+++ b/modules/libpref/Preferences.cpp
@@ -1408,37 +1408,17 @@ nsPrefBranch::GetRoot(nsACString& aRoot)
 }
 
 NS_IMETHODIMP
 nsPrefBranch::GetPrefType(const char* aPrefName, int32_t* aRetVal)
 {
   NS_ENSURE_ARG(aPrefName);
 
   const PrefName& prefName = GetPrefName(aPrefName);
-  Pref* pref;
-  if (gHashTable && (pref = pref_HashTableLookup(prefName.get()))) {
-    switch (pref->Type()) {
-      case PrefType::String:
-        *aRetVal = PREF_STRING;
-        break;
-
-      case PrefType::Int:
-        *aRetVal = PREF_INT;
-        break;
-
-      case PrefType::Bool:
-        *aRetVal = PREF_BOOL;
-        break;
-
-      default:
-        MOZ_CRASH();
-    }
-  } else {
-    *aRetVal = PREF_INVALID;
-  }
+  *aRetVal = Preferences::GetType(prefName.get());
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsPrefBranch::GetBoolPrefWithDefault(const char* aPrefName,
                                      bool aDefaultValue,
                                      uint8_t aArgc,
                                      bool* aRetVal)
@@ -3818,18 +3798,17 @@ Preferences::InitInitialObjects()
     }
   }
 
 #ifdef MOZ_WIDGET_ANDROID
   // Set up the correct default for toolkit.telemetry.enabled. If this build
   // has MOZ_TELEMETRY_ON_BY_DEFAULT *or* we're on the beta channel, telemetry
   // is on by default, otherwise not. This is necessary so that beta users who
   // are testing final release builds don't flipflop defaults.
-  if (Preferences::GetType(kTelemetryPref, PrefValueKind::Default) ==
-      nsIPrefBranch::PREF_INVALID) {
+  if (Preferences::GetType(kTelemetryPref) == nsIPrefBranch::PREF_INVALID) {
     bool prerelease = false;
 #ifdef MOZ_TELEMETRY_ON_BY_DEFAULT
     prerelease = true;
 #else
     nsAutoCString prefValue;
     Preferences::GetCString(kChannelPref, prefValue, PrefValueKind::Default);
     if (prefValue.EqualsLiteral("beta")) {
       prerelease = true;
@@ -4161,23 +4140,38 @@ Preferences::HasUserValue(const char* aP
 {
   NS_ENSURE_TRUE(InitStaticMembers(), false);
 
   Pref* pref = pref_HashTableLookup(aPrefName);
   return pref && pref->HasUserValue();
 }
 
 /* static */ int32_t
-Preferences::GetType(const char* aPrefName, PrefValueKind aKind)
+Preferences::GetType(const char* aPrefName)
 {
   NS_ENSURE_TRUE(InitStaticMembers(), nsIPrefBranch::PREF_INVALID);
-  int32_t result;
-  return NS_SUCCEEDED(GetRootBranch(aKind)->GetPrefType(aPrefName, &result))
-           ? result
-           : nsIPrefBranch::PREF_INVALID;
+
+  Pref* pref;
+  if (!gHashTable || !(pref = pref_HashTableLookup(aPrefName))) {
+    return PREF_INVALID;
+  }
+
+  switch (pref->Type()) {
+    case PrefType::String:
+      return PREF_STRING;
+
+    case PrefType::Int:
+      return PREF_INT;
+
+    case PrefType::Bool:
+      return PREF_BOOL;
+
+    default:
+      MOZ_CRASH();
+  }
 }
 
 /* static */ nsresult
 Preferences::AddStrongObserver(nsIObserver* aObserver, const char* aPref)
 {
   MOZ_ASSERT(aObserver);
   NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
   return sPreferences->mRootBranch->AddObserver(aPref, aObserver, false);
--- a/modules/libpref/Preferences.h
+++ b/modules/libpref/Preferences.h
@@ -87,18 +87,17 @@ public:
   static nsIPrefBranch* GetRootBranch(PrefValueKind aKind = PrefValueKind::User)
   {
     NS_ENSURE_TRUE(InitStaticMembers(), nullptr);
     return (aKind == PrefValueKind::Default) ? sPreferences->mDefaultRootBranch
                                              : sPreferences->mRootBranch;
   }
 
   // Gets the type of the pref.
-  static int32_t GetType(const char* aPrefName,
-                         PrefValueKind aKind = PrefValueKind::User);
+  static int32_t GetType(const char* aPrefName);
 
   // Fallible value getters.
   static nsresult GetBool(const char* aPrefName,
                           bool* aResult,
                           PrefValueKind aKind = PrefValueKind::User);
   static nsresult GetInt(const char* aPrefName,
                          int32_t* aResult,
                          PrefValueKind aKind = PrefValueKind::User);