Bug 1418156 - Move pref_SetValue() into PrefHashEntry. r=glandium draft
authorNicholas Nethercote <nnethercote@mozilla.com>
Fri, 17 Nov 2017 13:30:40 +1100
changeset 699360 3916d6f4ef11561d7355a4fd5947e2c696924dca
parent 699359 372322783fbb342a736eeacebee68fb67f4286a0
child 699361 56013faca5f9f48f4b0ed1ed4b01c42c83304664
push id89542
push usernnethercote@mozilla.com
push dateFri, 17 Nov 2017 02:31:06 +0000
reviewersglandium
bugs1418156
milestone59.0a1
Bug 1418156 - Move pref_SetValue() into PrefHashEntry. r=glandium MozReview-Commit-ID: LE7vQ8Iz5Dg
modules/libpref/Preferences.cpp
--- a/modules/libpref/Preferences.cpp
+++ b/modules/libpref/Preferences.cpp
@@ -302,16 +302,38 @@ public:
       if (strlen(stringVal) > MAX_ADVISABLE_PREF_LENGTH) {
         return false;
       }
     }
 
     return true;
   }
 
+  // Overwrite the type and value of an existing preference. Caller must ensure
+  // that they are not changing the type of a preference that has a default
+  // value.
+  void ReplaceValue(PrefValueKind aKind, PrefValue aNewValue, PrefType aNewType)
+  {
+    PrefValue* value =
+      aKind == PrefValueKind::Default ? &mDefaultValue : &mUserValue;
+
+    if (Type() == PrefType::String) {
+      free(const_cast<char*>(value->mStringVal));
+    }
+
+    SetType(aNewType);
+
+    if (aNewType == PrefType::String) {
+      MOZ_ASSERT(aNewValue.mStringVal);
+      value->mStringVal = moz_xstrdup(aNewValue.mStringVal);
+    } else {
+      *value = aNewValue;
+    }
+  }
+
   size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf)
   {
     // Note: mName is allocated in gPrefNameArena, measured elsewhere.
     size_t n = 0;
     if (IsTypeString()) {
       if (HasDefaultValue()) {
         n += aMallocSizeOf(mDefaultValue.mStringVal);
       }
@@ -614,38 +636,16 @@ pref_ValueChanged(PrefValue aOldValue, P
     default:
       changed = false;
       break;
   }
 
   return changed;
 }
 
-// Overwrite the type and value of an existing preference. Caller must ensure
-// that they are not changing the type of a preference that has a default
-// value.
-static void
-pref_SetValue(PrefValue* aExistingValue,
-              PrefType aExistingType,
-              PrefValue aNewValue,
-              PrefType aNewType)
-{
-  if (aExistingType == PrefType::String) {
-    free(const_cast<char*>(aExistingValue->mStringVal));
-  }
-
-  if (aNewType == PrefType::String) {
-    MOZ_ASSERT(aNewValue.mStringVal);
-    aExistingValue->mStringVal =
-      aNewValue.mStringVal ? moz_xstrdup(aNewValue.mStringVal) : nullptr;
-  } else {
-    *aExistingValue = aNewValue;
-  }
-}
-
 #ifdef DEBUG
 
 static pref_initPhase gPhase = START;
 
 struct StringComparator
 {
   const char* mKey;
   explicit StringComparator(const char* aKey)
@@ -742,18 +742,17 @@ pref_SetPref(const char* aPrefName,
   }
 
   bool valueChanged = false;
   if (aFlags & kPrefSetDefault) {
     if (!pref->IsLocked()) {
       // ?? change of semantics?
       if (pref_ValueChanged(pref->mDefaultValue, aValue, aType) ||
           !pref->HasDefaultValue()) {
-        pref_SetValue(&pref->mDefaultValue, pref->Type(), aValue, aType);
-        pref->SetType(aType);
+        pref->ReplaceValue(PrefValueKind::Default, aValue, aType);
         pref->SetHasDefaultValue(true);
         if (aFlags & kPrefSticky) {
           pref->SetIsSticky(true);
         }
         if (!pref->HasUserValue()) {
           valueChanged = true;
         }
       }
@@ -772,18 +771,17 @@ pref_SetPref(const char* aPrefName,
         pref->SetHasUserValue(false);
         if (!pref->IsLocked()) {
           Preferences::HandleDirty();
           valueChanged = true;
         }
       }
     } else if (!pref->HasUserValue() || !pref->IsType(aType) ||
                pref_ValueChanged(pref->mUserValue, aValue, aType)) {
-      pref_SetValue(&pref->mUserValue, pref->Type(), aValue, aType);
-      pref->SetType(aType);
+      pref->ReplaceValue(PrefValueKind::User, aValue, aType);
       pref->SetHasUserValue(true);
       if (!pref->IsLocked()) {
         Preferences::HandleDirty();
         valueChanged = true;
       }
     }
   }