Bug 1418156 - Move pref_GetPrefFromEntry into PrefHashEntry. r=glandium
authorNicholas Nethercote <nnethercote@mozilla.com>
Fri, 17 Nov 2017 15:29:30 +1100
changeset 437154 bcc25f198c1761c8eab5a68bb4905e86c6a572ad
parent 437153 4bec38deb68bc09cedd3d99c5e1f8f7cb72a43e7
child 437155 acfdad86187d760ade47a36528ba340c6c5d4ac1
push id117
push userfmarier@mozilla.com
push dateTue, 28 Nov 2017 20:17:16 +0000
reviewersglandium
bugs1418156
milestone59.0a1
Bug 1418156 - Move pref_GetPrefFromEntry into PrefHashEntry. r=glandium And rename it as ToSetting(). This requires also moving AssignPrefValueToDomPrefValue into PrefHashEntry. MozReview-Commit-ID: 8ntNZJt9rhv
modules/libpref/Preferences.cpp
--- a/modules/libpref/Preferences.cpp
+++ b/modules/libpref/Preferences.cpp
@@ -215,16 +215,67 @@ public:
     }
 
     // Don't need to free this because it's allocated in memory owned by
     // gPrefNameArena.
     pref->mKey = nullptr;
     memset(aEntry, 0, aTable->EntrySize());
   }
 
+private:
+  static void AssignPrefValueToDomPrefValue(PrefType aType,
+                                            PrefValue* aValue,
+                                            dom::PrefValue* aDomValue)
+  {
+    switch (aType) {
+      case PrefType::String:
+        *aDomValue = nsDependentCString(aValue->mStringVal);
+        return;
+
+      case PrefType::Int:
+        *aDomValue = aValue->mIntVal;
+        return;
+
+      case PrefType::Bool:
+        *aDomValue = !!aValue->mBoolVal;
+        return;
+
+      default:
+        MOZ_CRASH();
+    }
+  }
+
+public:
+  void ToSetting(dom::PrefSetting* aSetting)
+  {
+    aSetting->name() = mKey;
+
+    if (HasDefaultValue()) {
+      aSetting->defaultValue() = dom::PrefValue();
+      AssignPrefValueToDomPrefValue(
+        Type(), &mDefaultValue, &aSetting->defaultValue().get_PrefValue());
+    } else {
+      aSetting->defaultValue() = null_t();
+    }
+
+    if (HasUserValue()) {
+      aSetting->userValue() = dom::PrefValue();
+      AssignPrefValueToDomPrefValue(
+        Type(), &mUserValue, &aSetting->userValue().get_PrefValue());
+    } else {
+      aSetting->userValue() = null_t();
+    }
+
+    MOZ_ASSERT(aSetting->defaultValue().type() ==
+                 dom::MaybePrefValue::Tnull_t ||
+               aSetting->userValue().type() == dom::MaybePrefValue::Tnull_t ||
+               (aSetting->defaultValue().get_PrefValue().type() ==
+                aSetting->userValue().get_PrefValue().type()));
+  }
+
   size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf)
   {
     // Note: mKey is allocated in gPrefNameArena, measured elsewhere.
     size_t n = 0;
     if (IsTypeString()) {
       if (HasDefaultValue()) {
         n += aMallocSizeOf(mDefaultValue.mStringVal);
       }
@@ -426,68 +477,16 @@ pref_EntryHasAdvisablySizedValues(PrefHa
     if (strlen(stringVal) > MAX_ADVISABLE_PREF_LENGTH) {
       return false;
     }
   }
 
   return true;
 }
 
-static void
-AssignPrefValueToDomPrefValue(PrefType aType,
-                              PrefValue* aValue,
-                              dom::PrefValue* aDomValue)
-{
-  switch (aType) {
-    case PrefType::String:
-      *aDomValue = nsDependentCString(aValue->mStringVal);
-      return;
-
-    case PrefType::Int:
-      *aDomValue = aValue->mIntVal;
-      return;
-
-    case PrefType::Bool:
-      *aDomValue = !!aValue->mBoolVal;
-      return;
-
-    default:
-      MOZ_CRASH();
-  }
-}
-
-static void
-pref_GetPrefFromEntry(PrefHashEntry* aPref, dom::PrefSetting* aSetting)
-{
-  aSetting->name() = aPref->mKey;
-
-  if (aPref->HasDefaultValue()) {
-    aSetting->defaultValue() = dom::PrefValue();
-    AssignPrefValueToDomPrefValue(aPref->Type(),
-                                  &aPref->mDefaultValue,
-                                  &aSetting->defaultValue().get_PrefValue());
-  } else {
-    aSetting->defaultValue() = null_t();
-  }
-
-  if (aPref->HasUserValue()) {
-    aSetting->userValue() = dom::PrefValue();
-    AssignPrefValueToDomPrefValue(aPref->Type(),
-                                  &aPref->mUserValue,
-                                  &aSetting->userValue().get_PrefValue());
-  } else {
-    aSetting->userValue() = null_t();
-  }
-
-  MOZ_ASSERT(aSetting->defaultValue().type() == dom::MaybePrefValue::Tnull_t ||
-             aSetting->userValue().type() == dom::MaybePrefValue::Tnull_t ||
-             (aSetting->defaultValue().get_PrefValue().type() ==
-              aSetting->userValue().get_PrefValue().type()));
-}
-
 static bool
 PREF_HasUserPref(const char* aPrefName)
 {
   if (!gHashTable) {
     return false;
   }
 
   PrefHashEntry* pref = pref_HashTableLookup(aPrefName);
@@ -3766,33 +3765,33 @@ void
 Preferences::GetPreference(PrefSetting* aSetting)
 {
   PrefHashEntry* pref = pref_HashTableLookup(aSetting->name().get());
   if (!pref) {
     return;
   }
 
   if (pref_EntryHasAdvisablySizedValues(pref)) {
-    pref_GetPrefFromEntry(pref, aSetting);
+    pref->ToSetting(aSetting);
   }
 }
 
 void
 Preferences::GetPreferences(InfallibleTArray<PrefSetting>* aSettings)
 {
   aSettings->SetCapacity(gHashTable->Capacity());
   for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) {
     auto pref = static_cast<PrefHashEntry*>(iter.Get());
 
     if (!pref_EntryHasAdvisablySizedValues(pref)) {
       continue;
     }
 
     dom::PrefSetting* setting = aSettings->AppendElement();
-    pref_GetPrefFromEntry(pref, setting);
+    pref->ToSetting(setting);
   }
 }
 
 #ifdef DEBUG
 void
 Preferences::SetInitPhase(pref_initPhase aPhase)
 {
   gPhase = aPhase;