Bug 1418156 - Move pref_SetValue() into PrefHashEntry. r=glandium
MozReview-Commit-ID: LE7vQ8Iz5Dg
--- 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;
}
}
}