Backed out changeset dd911452e3f7 (bug 1249157) for build bustage CLOSED TREE

This commit is contained in:
Wes Kocher 2016-02-22 09:05:21 -08:00
parent 41cfa98849
commit c6c6b6476a
6 changed files with 164 additions and 222 deletions

View File

@ -1315,7 +1315,7 @@ static nsresult pref_InitInitialObjects()
// channel, telemetry is on by default, otherwise not. This is necessary // channel, telemetry is on by default, otherwise not. This is necessary
// so that beta users who are testing final release builds don't flipflop // so that beta users who are testing final release builds don't flipflop
// defaults. // defaults.
if (Preferences::GetDefaultType(kTelemetryPref) == nsIPrefBranch::PREF_INVALID) { if (Preferences::GetDefaultType(kTelemetryPref) == PREF_INVALID) {
bool prerelease = false; bool prerelease = false;
#ifdef MOZ_TELEMETRY_ON_BY_DEFAULT #ifdef MOZ_TELEMETRY_ON_BY_DEFAULT
prerelease = true; prerelease = true;

View File

@ -127,21 +127,7 @@ NS_IMETHODIMP nsPrefBranch::GetPrefType(const char *aPrefName, int32_t *_retval)
{ {
NS_ENSURE_ARG(aPrefName); NS_ENSURE_ARG(aPrefName);
const char *pref = getPrefName(aPrefName); const char *pref = getPrefName(aPrefName);
switch (PREF_GetPrefType(pref)) { *_retval = PREF_GetPrefType(pref);
case PrefType::String:
*_retval = PREF_STRING;
break;
case PrefType::Int:
*_retval = PREF_INT;
break;
case PrefType::Bool:
*_retval = PREF_BOOL;
break;
case PrefType::Invalid:
default:
*_retval = PREF_INVALID;
break;
}
return NS_OK; return NS_OK;
} }

View File

@ -40,7 +40,7 @@ static void
clearPrefEntry(PLDHashTable *table, PLDHashEntryHdr *entry) clearPrefEntry(PLDHashTable *table, PLDHashEntryHdr *entry)
{ {
PrefHashEntry *pref = static_cast<PrefHashEntry *>(entry); PrefHashEntry *pref = static_cast<PrefHashEntry *>(entry);
if (pref->prefFlags.IsTypeString()) if (pref->flags & PREF_STRING)
{ {
if (pref->defaultPref.stringVal) if (pref->defaultPref.stringVal)
PL_strfree(pref->defaultPref.stringVal); PL_strfree(pref->defaultPref.stringVal);
@ -117,7 +117,13 @@ static char *ArenaStrDup(const char* str, PLArenaPool* aArena)
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#define PREF_IS_LOCKED(pref) ((pref)->flags & PREF_LOCKED)
#define PREF_HAS_DEFAULT_VALUE(pref) ((pref)->flags & PREF_HAS_DEFAULT)
#define PREF_HAS_USER_VALUE(pref) ((pref)->flags & PREF_USERSET)
#define PREF_TYPE(pref) (PrefType)((pref)->flags & PREF_VALUETYPE_MASK)
static bool pref_ValueChanged(PrefValue oldValue, PrefValue newValue, PrefType type); static bool pref_ValueChanged(PrefValue oldValue, PrefValue newValue, PrefType type);
/* -- Privates */ /* -- Privates */
struct CallbackNode { struct CallbackNode {
char* domain; char* domain;
@ -243,7 +249,7 @@ PREF_SetCharPref(const char *pref_name, const char *value, bool set_default)
PrefValue pref; PrefValue pref;
pref.stringVal = (char*)value; pref.stringVal = (char*)value;
return pref_HashPref(pref_name, pref, PrefType::String, set_default ? kPrefSetDefault : 0); return pref_HashPref(pref_name, pref, PREF_STRING, set_default ? kPrefSetDefault : 0);
} }
nsresult nsresult
@ -252,7 +258,7 @@ PREF_SetIntPref(const char *pref_name, int32_t value, bool set_default)
PrefValue pref; PrefValue pref;
pref.intVal = value; pref.intVal = value;
return pref_HashPref(pref_name, pref, PrefType::Int, set_default ? kPrefSetDefault : 0); return pref_HashPref(pref_name, pref, PREF_INT, set_default ? kPrefSetDefault : 0);
} }
nsresult nsresult
@ -261,7 +267,7 @@ PREF_SetBoolPref(const char *pref_name, bool value, bool set_default)
PrefValue pref; PrefValue pref;
pref.boolVal = value; pref.boolVal = value;
return pref_HashPref(pref_name, pref, PrefType::Bool, set_default ? kPrefSetDefault : 0); return pref_HashPref(pref_name, pref, PREF_BOOL, set_default ? kPrefSetDefault : 0);
} }
enum WhichValue { DEFAULT_VALUE, USER_VALUE }; enum WhichValue { DEFAULT_VALUE, USER_VALUE };
@ -329,12 +335,12 @@ pref_savePrefs(PLDHashTable* aTable)
// where we're getting our pref from // where we're getting our pref from
PrefValue* sourcePref; PrefValue* sourcePref;
if (pref->prefFlags.HasUserValue() && if (PREF_HAS_USER_VALUE(pref) &&
(pref_ValueChanged(pref->defaultPref, (pref_ValueChanged(pref->defaultPref,
pref->userPref, pref->userPref,
pref->prefFlags.GetPrefType()) || (PrefType) PREF_TYPE(pref)) ||
!(pref->prefFlags.HasDefault()) || !(pref->flags & PREF_HAS_DEFAULT) ||
pref->prefFlags.HasStickyDefault())) { pref->flags & PREF_STICKY_DEFAULT)) {
sourcePref = &pref->userPref; sourcePref = &pref->userPref;
} else { } else {
// do not save default prefs that haven't changed // do not save default prefs that haven't changed
@ -342,15 +348,15 @@ pref_savePrefs(PLDHashTable* aTable)
} }
// strings are in quotes! // strings are in quotes!
if (pref->prefFlags.IsTypeString()) { if (pref->flags & PREF_STRING) {
prefValue = '\"'; prefValue = '\"';
str_escape(sourcePref->stringVal, prefValue); str_escape(sourcePref->stringVal, prefValue);
prefValue += '\"'; prefValue += '\"';
} else if (pref->prefFlags.IsTypeInt()) { } else if (pref->flags & PREF_INT) {
prefValue.AppendInt(sourcePref->intVal); prefValue.AppendInt(sourcePref->intVal);
} else if (pref->prefFlags.IsTypeBool()) { } else if (pref->flags & PREF_BOOL) {
prefValue = (sourcePref->boolVal) ? "true" : "false"; prefValue = (sourcePref->boolVal) ? "true" : "false";
} }
@ -383,14 +389,14 @@ GetPrefValueFromEntry(PrefHashEntry *aHashEntry, dom::PrefSetting* aPref,
settingValue = &aPref->defaultValue().get_PrefValue(); settingValue = &aPref->defaultValue().get_PrefValue();
} }
switch (aHashEntry->prefFlags.GetPrefType()) { switch (aHashEntry->flags & PREF_VALUETYPE_MASK) {
case PrefType::String: case PREF_STRING:
*settingValue = nsDependentCString(value->stringVal); *settingValue = nsDependentCString(value->stringVal);
return; return;
case PrefType::Int: case PREF_INT:
*settingValue = value->intVal; *settingValue = value->intVal;
return; return;
case PrefType::Bool: case PREF_BOOL:
*settingValue = !!value->boolVal; *settingValue = !!value->boolVal;
return; return;
default: default:
@ -402,12 +408,12 @@ void
pref_GetPrefFromEntry(PrefHashEntry *aHashEntry, dom::PrefSetting* aPref) pref_GetPrefFromEntry(PrefHashEntry *aHashEntry, dom::PrefSetting* aPref)
{ {
aPref->name() = aHashEntry->key; aPref->name() = aHashEntry->key;
if (aHashEntry->prefFlags.HasDefault()) { if (PREF_HAS_DEFAULT_VALUE(aHashEntry)) {
GetPrefValueFromEntry(aHashEntry, aPref, DEFAULT_VALUE); GetPrefValueFromEntry(aHashEntry, aPref, DEFAULT_VALUE);
} else { } else {
aPref->defaultValue() = null_t(); aPref->defaultValue() = null_t();
} }
if (aHashEntry->prefFlags.HasUserValue()) { if (PREF_HAS_USER_VALUE(aHashEntry)) {
GetPrefValueFromEntry(aHashEntry, aPref, USER_VALUE); GetPrefValueFromEntry(aHashEntry, aPref, USER_VALUE);
} else { } else {
aPref->userValue() = null_t(); aPref->userValue() = null_t();
@ -445,7 +451,11 @@ bool PREF_HasUserPref(const char *pref_name)
return false; return false;
PrefHashEntry *pref = pref_HashTableLookup(pref_name); PrefHashEntry *pref = pref_HashTableLookup(pref_name);
return pref && pref->prefFlags.HasUserValue(); if (!pref) return false;
/* convert PREF_HAS_USER_VALUE to bool */
return (PREF_HAS_USER_VALUE(pref) != 0);
} }
nsresult nsresult
@ -458,12 +468,12 @@ PREF_CopyCharPref(const char *pref_name, char ** return_buffer, bool get_default
char* stringVal; char* stringVal;
PrefHashEntry* pref = pref_HashTableLookup(pref_name); PrefHashEntry* pref = pref_HashTableLookup(pref_name);
if (pref && (pref->prefFlags.IsTypeString())) { if (pref && (pref->flags & PREF_STRING))
if (get_default || pref->prefFlags.IsLocked() || !pref->prefFlags.HasUserValue()) { {
if (get_default || PREF_IS_LOCKED(pref) || !PREF_HAS_USER_VALUE(pref))
stringVal = pref->defaultPref.stringVal; stringVal = pref->defaultPref.stringVal;
} else { else
stringVal = pref->userPref.stringVal; stringVal = pref->userPref.stringVal;
}
if (stringVal) { if (stringVal) {
*return_buffer = NS_strdup(stringVal); *return_buffer = NS_strdup(stringVal);
@ -480,17 +490,18 @@ nsresult PREF_GetIntPref(const char *pref_name,int32_t * return_int, bool get_de
nsresult rv = NS_ERROR_UNEXPECTED; nsresult rv = NS_ERROR_UNEXPECTED;
PrefHashEntry* pref = pref_HashTableLookup(pref_name); PrefHashEntry* pref = pref_HashTableLookup(pref_name);
if (pref && (pref->prefFlags.IsTypeInt())) { if (pref && (pref->flags & PREF_INT))
if (get_default || pref->prefFlags.IsLocked() || !pref->prefFlags.HasUserValue()) { {
if (get_default || PREF_IS_LOCKED(pref) || !PREF_HAS_USER_VALUE(pref))
{
int32_t tempInt = pref->defaultPref.intVal; int32_t tempInt = pref->defaultPref.intVal;
/* check to see if we even had a default */ /* check to see if we even had a default */
if (!pref->prefFlags.HasDefault()) { if (!(pref->flags & PREF_HAS_DEFAULT))
return NS_ERROR_UNEXPECTED; return NS_ERROR_UNEXPECTED;
}
*return_int = tempInt; *return_int = tempInt;
} else {
*return_int = pref->userPref.intVal;
} }
else
*return_int = pref->userPref.intVal;
rv = NS_OK; rv = NS_OK;
} }
return rv; return rv;
@ -504,15 +515,18 @@ nsresult PREF_GetBoolPref(const char *pref_name, bool * return_value, bool get_d
nsresult rv = NS_ERROR_UNEXPECTED; nsresult rv = NS_ERROR_UNEXPECTED;
PrefHashEntry* pref = pref_HashTableLookup(pref_name); PrefHashEntry* pref = pref_HashTableLookup(pref_name);
//NS_ASSERTION(pref, pref_name); //NS_ASSERTION(pref, pref_name);
if (pref && (pref->prefFlags.IsTypeBool())) { if (pref && (pref->flags & PREF_BOOL))
if (get_default || pref->prefFlags.IsLocked() || !pref->prefFlags.HasUserValue()) { {
if (get_default || PREF_IS_LOCKED(pref) || !PREF_HAS_USER_VALUE(pref))
{
bool tempBool = pref->defaultPref.boolVal; bool tempBool = pref->defaultPref.boolVal;
/* check to see if we even had a default */ /* check to see if we even had a default */
if (pref->prefFlags.HasDefault()) { if (pref->flags & PREF_HAS_DEFAULT) {
*return_value = tempBool; *return_value = tempBool;
rv = NS_OK; rv = NS_OK;
} }
} else { }
else {
*return_value = pref->userPref.boolVal; *return_value = pref->userPref.boolVal;
rv = NS_OK; rv = NS_OK;
} }
@ -570,10 +584,11 @@ PREF_ClearUserPref(const char *pref_name)
return NS_ERROR_NOT_INITIALIZED; return NS_ERROR_NOT_INITIALIZED;
PrefHashEntry* pref = pref_HashTableLookup(pref_name); PrefHashEntry* pref = pref_HashTableLookup(pref_name);
if (pref && pref->prefFlags.HasUserValue()) { if (pref && PREF_HAS_USER_VALUE(pref))
pref->prefFlags.SetHasUserValue(false); {
pref->flags &= ~PREF_USERSET;
if (!pref->prefFlags.HasDefault()) { if (!(pref->flags & PREF_HAS_DEFAULT)) {
gHashTable->RemoveEntry(pref); gHashTable->RemoveEntry(pref);
} }
@ -597,11 +612,11 @@ PREF_ClearAllUserPrefs()
for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) { for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) {
auto pref = static_cast<PrefHashEntry*>(iter.Get()); auto pref = static_cast<PrefHashEntry*>(iter.Get());
if (pref->prefFlags.HasUserValue()) { if (PREF_HAS_USER_VALUE(pref)) {
prefStrings.push_back(std::string(pref->key)); prefStrings.push_back(std::string(pref->key));
pref->prefFlags.SetHasUserValue(false); pref->flags &= ~PREF_USERSET;
if (!pref->prefFlags.HasDefault()) { if (!(pref->flags & PREF_HAS_DEFAULT)) {
iter.Remove(); iter.Remove();
} }
} }
@ -625,14 +640,18 @@ nsresult PREF_LockPref(const char *key, bool lockit)
return NS_ERROR_UNEXPECTED; return NS_ERROR_UNEXPECTED;
if (lockit) { if (lockit) {
if (!pref->prefFlags.IsLocked()) { if (!PREF_IS_LOCKED(pref))
pref->prefFlags.SetLocked(true); {
pref->flags |= PREF_LOCKED;
gIsAnyPrefLocked = true; gIsAnyPrefLocked = true;
pref_DoCallback(key); pref_DoCallback(key);
} }
} else { }
if (pref->prefFlags.IsLocked()) { else
pref->prefFlags.SetLocked(false); {
if (PREF_IS_LOCKED(pref))
{
pref->flags &= ~PREF_LOCKED;
pref_DoCallback(key); pref_DoCallback(key);
} }
} }
@ -645,23 +664,15 @@ nsresult PREF_LockPref(const char *key, bool lockit)
static bool pref_ValueChanged(PrefValue oldValue, PrefValue newValue, PrefType type) static bool pref_ValueChanged(PrefValue oldValue, PrefValue newValue, PrefType type)
{ {
bool changed = true; bool changed = true;
switch(type) { if (type & PREF_STRING)
case PrefType::String: {
if (oldValue.stringVal && newValue.stringVal) { if (oldValue.stringVal && newValue.stringVal)
changed = (strcmp(oldValue.stringVal, newValue.stringVal) != 0); changed = (strcmp(oldValue.stringVal, newValue.stringVal) != 0);
}
break;
case PrefType::Int:
changed = oldValue.intVal != newValue.intVal;
break;
case PrefType::Bool:
changed = oldValue.boolVal != newValue.boolVal;
break;
case PrefType::Invalid:
default:
changed = false;
break;
} }
else if (type & PREF_INT)
changed = oldValue.intVal != newValue.intVal;
else if (type & PREF_BOOL)
changed = oldValue.boolVal != newValue.boolVal;
return changed; return changed;
} }
@ -670,21 +681,20 @@ static bool pref_ValueChanged(PrefValue oldValue, PrefValue newValue, PrefType t
* ensure that they are not changing the type of a preference that has * ensure that they are not changing the type of a preference that has
* a default value. * a default value.
*/ */
static PrefTypeFlags pref_SetValue(PrefValue* existingValue, PrefTypeFlags flags, static void pref_SetValue(PrefValue* existingValue, uint16_t *existingFlags,
PrefValue newValue, PrefType newType) PrefValue newValue, PrefType newType)
{ {
if (flags.IsTypeString() && existingValue->stringVal) { if ((*existingFlags & PREF_STRING) && existingValue->stringVal) {
PL_strfree(existingValue->stringVal); PL_strfree(existingValue->stringVal);
} }
flags.SetPrefType(newType); *existingFlags = (*existingFlags & ~PREF_VALUETYPE_MASK) | newType;
if (flags.IsTypeString()) { if (newType & PREF_STRING) {
PR_ASSERT(newValue.stringVal); PR_ASSERT(newValue.stringVal);
existingValue->stringVal = newValue.stringVal ? PL_strdup(newValue.stringVal) : nullptr; existingValue->stringVal = newValue.stringVal ? PL_strdup(newValue.stringVal) : nullptr;
} }
else { else {
*existingValue = newValue; *existingValue = newValue;
} }
return flags;
} }
PrefHashEntry* pref_HashTableLookup(const char *key) PrefHashEntry* pref_HashTableLookup(const char *key)
@ -713,53 +723,63 @@ nsresult pref_HashPref(const char *key, PrefValue value, PrefType type, uint32_t
if (!pref->key) { if (!pref->key) {
// initialize the pref entry // initialize the pref entry
pref->prefFlags.Reset().SetPrefType(type); pref->flags = type;
pref->key = ArenaStrDup(key, &gPrefNameArena); pref->key = ArenaStrDup(key, &gPrefNameArena);
memset(&pref->defaultPref, 0, sizeof(pref->defaultPref)); memset(&pref->defaultPref, 0, sizeof(pref->defaultPref));
memset(&pref->userPref, 0, sizeof(pref->userPref)); memset(&pref->userPref, 0, sizeof(pref->userPref));
} else if (pref->prefFlags.HasDefault() && !pref->prefFlags.IsPrefType(type)) { }
else if ((pref->flags & PREF_HAS_DEFAULT) && PREF_TYPE(pref) != type)
{
NS_WARNING(nsPrintfCString("Trying to overwrite value of default pref %s with the wrong type!", key).get()); NS_WARNING(nsPrintfCString("Trying to overwrite value of default pref %s with the wrong type!", key).get());
return NS_ERROR_UNEXPECTED; return NS_ERROR_UNEXPECTED;
} }
bool valueChanged = false; bool valueChanged = false;
if (flags & kPrefSetDefault) { if (flags & kPrefSetDefault)
if (!pref->prefFlags.IsLocked()) { {
/* ?? change of semantics? */ if (!PREF_IS_LOCKED(pref))
{ /* ?? change of semantics? */
if (pref_ValueChanged(pref->defaultPref, value, type) || if (pref_ValueChanged(pref->defaultPref, value, type) ||
!pref->prefFlags.HasDefault()) { !(pref->flags & PREF_HAS_DEFAULT))
pref->prefFlags = pref_SetValue(&pref->defaultPref, pref->prefFlags, value, type).SetHasDefault(true); {
if (flags & kPrefStickyDefault) { pref_SetValue(&pref->defaultPref, &pref->flags, value, type);
pref->prefFlags.SetHasStickyDefault(true); pref->flags |= PREF_HAS_DEFAULT;
} if (flags & kPrefStickyDefault)
if (!pref->prefFlags.HasUserValue()) { pref->flags |= PREF_STICKY_DEFAULT;
if (!PREF_HAS_USER_VALUE(pref))
valueChanged = true; valueChanged = true;
}
} }
// What if we change the default to be the same as the user value? // What if we change the default to be the same as the user value?
// Should we clear the user value? // Should we clear the user value?
} }
} else { }
else
{
/* If new value is same as the default value and it's not a "sticky" /* If new value is same as the default value and it's not a "sticky"
pref, then un-set the user value. pref, then un-set the user value.
Otherwise, set the user value only if it has changed */ Otherwise, set the user value only if it has changed */
if ((pref->prefFlags.HasDefault()) && if ((pref->flags & PREF_HAS_DEFAULT) &&
!(pref->prefFlags.HasStickyDefault()) && !(pref->flags & PREF_STICKY_DEFAULT) &&
!pref_ValueChanged(pref->defaultPref, value, type) && !pref_ValueChanged(pref->defaultPref, value, type) &&
!(flags & kPrefForceSet)) { !(flags & kPrefForceSet))
if (pref->prefFlags.HasUserValue()) { {
if (PREF_HAS_USER_VALUE(pref))
{
/* XXX should we free a user-set string value if there is one? */ /* XXX should we free a user-set string value if there is one? */
pref->prefFlags.SetHasUserValue(false); pref->flags &= ~PREF_USERSET;
if (!pref->prefFlags.IsLocked()) { if (!PREF_IS_LOCKED(pref)) {
gDirty = true; gDirty = true;
valueChanged = true; valueChanged = true;
} }
} }
} else if (!pref->prefFlags.HasUserValue() || }
!pref->prefFlags.IsPrefType(type) || else if (!PREF_HAS_USER_VALUE(pref) ||
pref_ValueChanged(pref->userPref, value, type) ) { PREF_TYPE(pref) != type ||
pref->prefFlags = pref_SetValue(&pref->userPref, pref->prefFlags, value, type).SetHasUserValue(true); pref_ValueChanged(pref->userPref, value, type) )
if (!pref->prefFlags.IsLocked()) { {
pref_SetValue(&pref->userPref, &pref->flags, value, type);
pref->flags |= PREF_USERSET;
if (!PREF_IS_LOCKED(pref)) {
gDirty = true; gDirty = true;
valueChanged = true; valueChanged = true;
} }
@ -788,11 +808,17 @@ PREF_GetPrefType(const char *pref_name)
{ {
if (gHashTable) { if (gHashTable) {
PrefHashEntry* pref = pref_HashTableLookup(pref_name); PrefHashEntry* pref = pref_HashTableLookup(pref_name);
if (pref) { if (pref)
return pref->prefFlags.GetPrefType(); {
if (pref->flags & PREF_STRING)
return PREF_STRING;
else if (pref->flags & PREF_INT)
return PREF_INT;
else if (pref->flags & PREF_BOOL)
return PREF_BOOL;
} }
} }
return PrefType::Invalid; return PREF_INVALID;
} }
/* -- */ /* -- */
@ -803,9 +829,8 @@ PREF_PrefIsLocked(const char *pref_name)
bool result = false; bool result = false;
if (gIsAnyPrefLocked && gHashTable) { if (gIsAnyPrefLocked && gHashTable) {
PrefHashEntry* pref = pref_HashTableLookup(pref_name); PrefHashEntry* pref = pref_HashTableLookup(pref_name);
if (pref && pref->prefFlags.IsLocked()) { if (pref && PREF_IS_LOCKED(pref))
result = true; result = true;
}
} }
return result; return result;
@ -948,16 +973,10 @@ void PREF_ReaderCallback(void *closure,
PrefType type, PrefType type,
bool isDefault, bool isDefault,
bool isStickyDefault) bool isStickyDefault)
{ {
uint32_t flags = 0; uint32_t flags = isDefault ? kPrefSetDefault : kPrefForceSet;
if (isDefault) { if (isDefault && isStickyDefault) {
flags |= kPrefSetDefault; flags |= kPrefStickyDefault;
if (isStickyDefault) {
flags |= kPrefStickyDefault;
}
} else {
flags |= kPrefForceSet;
} }
pref_HashPref(pref, value, type, flags); pref_HashPref(pref, value, type, flags);
} }

View File

@ -28,6 +28,14 @@ typedef union
bool boolVal; bool boolVal;
} PrefValue; } PrefValue;
struct PrefHashEntry : PLDHashEntryHdr
{
uint16_t flags; // This field goes first to minimize struct size on 64-bit.
const char *key;
PrefValue defaultPref;
PrefValue userPref;
};
/* /*
// <font color=blue> // <font color=blue>
// The Init function initializes the preference context and creates // The Init function initializes the preference context and creates
@ -37,7 +45,7 @@ typedef union
void PREF_Init(); void PREF_Init();
/* /*
// Cleanup should be called at program exit to free the // Cleanup should be called at program exit to free the
// list of registered callbacks. // list of registered callbacks.
*/ */
void PREF_Cleanup(); void PREF_Cleanup();
@ -45,88 +53,23 @@ void PREF_CleanupPrefs();
/* /*
// <font color=blue> // <font color=blue>
// Preference flags, including the native type of the preference. Changing any of these // Preference flags, including the native type of the preference
// values will require modifying the code inside of PrefTypeFlags class.
// </font> // </font>
*/ */
enum class PrefType { typedef enum { PREF_INVALID = 0,
Invalid = 0, PREF_LOCKED = 1, PREF_USERSET = 2, PREF_CONFIG = 4, PREF_REMOTE = 8,
String = 1, PREF_LILOCAL = 16, PREF_STRING = 32, PREF_INT = 64, PREF_BOOL = 128,
Int = 2, PREF_HAS_DEFAULT = 256,
Bool = 3, // pref is default pref with "sticky" semantics
}; PREF_STICKY_DEFAULT = 512,
PREF_VALUETYPE_MASK = (PREF_STRING | PREF_INT | PREF_BOOL)
// Keep the type of the preference, as well as the flags guiding its behaviour. } PrefType;
class PrefTypeFlags
{
public:
PrefTypeFlags() : mValue(AsInt(PrefType::Invalid)) {}
PrefTypeFlags(PrefType aType) : mValue(AsInt(aType)) {}
PrefTypeFlags& Reset() { mValue = AsInt(PrefType::Invalid); return *this; }
bool IsTypeValid() const { return !IsPrefType(PrefType::Invalid); }
bool IsTypeString() const { return IsPrefType(PrefType::String); }
bool IsTypeInt() const { return IsPrefType(PrefType::Int); }
bool IsTypeBool() const { return IsPrefType(PrefType::Bool); }
bool IsPrefType(PrefType type) const { return GetPrefType() == type; }
PrefTypeFlags& SetPrefType(PrefType aType) {
mValue = mValue - AsInt(GetPrefType()) + AsInt(aType);
return *this;
}
PrefType GetPrefType() const {
return (PrefType)(mValue & (AsInt(PrefType::String) |
AsInt(PrefType::Int) |
AsInt(PrefType::Bool)));
}
bool HasDefault() const { return mValue & PREF_FLAG_HAS_DEFAULT; }
PrefTypeFlags& SetHasDefault(bool aSetOrUnset) { return SetFlag(PREF_FLAG_HAS_DEFAULT, aSetOrUnset); }
bool HasStickyDefault() const { return mValue & PREF_FLAG_STICKY_DEFAULT; }
PrefTypeFlags& SetHasStickyDefault(bool aSetOrUnset) { return SetFlag(PREF_FLAG_STICKY_DEFAULT, aSetOrUnset); }
bool IsLocked() const { return mValue & PREF_FLAG_LOCKED; }
PrefTypeFlags& SetLocked(bool aSetOrUnset) { return SetFlag(PREF_FLAG_LOCKED, aSetOrUnset); }
bool HasUserValue() const { return mValue & PREF_FLAG_USERSET; }
PrefTypeFlags& SetHasUserValue(bool aSetOrUnset) { return SetFlag(PREF_FLAG_USERSET, aSetOrUnset); }
private:
static uint16_t AsInt(PrefType aType) { return (uint16_t)aType; }
PrefTypeFlags& SetFlag(uint16_t aFlag, bool aSetOrUnset) {
mValue = aSetOrUnset ? mValue | aFlag : mValue & ~aFlag;
return *this;
}
// Pack both the value of type (PrefType) and flags into the same int. This is why
// the flag enum starts at 4, as PrefType occupies the bottom two bits.
enum {
PREF_FLAG_LOCKED = 4,
PREF_FLAG_USERSET = 8,
PREF_FLAG_CONFIG = 16,
PREF_FLAG_REMOTE = 32,
PREF_FLAG_LILOCAL = 64,
PREF_FLAG_HAS_DEFAULT = 128,
PREF_FLAG_STICKY_DEFAULT = 256,
};
uint16_t mValue;
};
struct PrefHashEntry : PLDHashEntryHdr
{
PrefTypeFlags prefFlags; // This field goes first to minimize struct size on 64-bit.
const char *key;
PrefValue defaultPref;
PrefValue userPref;
};
/* /*
// <font color=blue> // <font color=blue>
// Set the various types of preferences. These functions take a dotted // Set the various types of preferences. These functions take a dotted
// notation of the preference name (e.g. "browser.startup.homepage"). // notation of the preference name (e.g. "browser.startup.homepage").
// Note that this will cause the preference to be saved to the file if // Note that this will cause the preference to be saved to the file if
// it is different from the default. In other words, these are used // it is different from the default. In other words, these are used
// to set the _user_ preferences. // to set the _user_ preferences.
@ -160,8 +103,8 @@ bool PREF_HasUserPref(const char* pref_name);
// </font> // </font>
*/ */
nsresult PREF_GetIntPref(const char *pref, nsresult PREF_GetIntPref(const char *pref,
int32_t * return_int, bool get_default); int32_t * return_int, bool get_default);
nsresult PREF_GetBoolPref(const char *pref, bool * return_val, bool get_default); nsresult PREF_GetBoolPref(const char *pref, bool * return_val, bool get_default);
/* /*
// <font color=blue> // <font color=blue>
// These functions are similar to the above "Get" version with the significant // These functions are similar to the above "Get" version with the significant
@ -230,10 +173,10 @@ typedef void (*PrefChangedFunc) (const char *, void *);
// matched all the parameters; otherwise it returns PREF_ERROR. // matched all the parameters; otherwise it returns PREF_ERROR.
// </font> // </font>
*/ */
void PREF_RegisterCallback(const char* domain, void PREF_RegisterCallback( const char* domain,
PrefChangedFunc callback, void* instance_data ); PrefChangedFunc callback, void* instance_data );
nsresult PREF_UnregisterCallback(const char* domain, nsresult PREF_UnregisterCallback( const char* domain,
PrefChangedFunc callback, void* instance_data ); PrefChangedFunc callback, void* instance_data );
/* /*
* Used by nsPrefService as the callback function of the 'pref' parser * Used by nsPrefService as the callback function of the 'pref' parser

View File

@ -114,17 +114,17 @@ pref_DoCallback(PrefParseState *ps)
PrefValue value; PrefValue value;
switch (ps->vtype) { switch (ps->vtype) {
case PrefType::String: case PREF_STRING:
value.stringVal = ps->vb; value.stringVal = ps->vb;
break; break;
case PrefType::Int: case PREF_INT:
if ((ps->vb[0] == '-' || ps->vb[0] == '+') && ps->vb[1] == '\0') { if ((ps->vb[0] == '-' || ps->vb[0] == '+') && ps->vb[1] == '\0') {
NS_WARNING("malformed integer value"); NS_WARNING("malformed integer value");
return false; return false;
} }
value.intVal = atoi(ps->vb); value.intVal = atoi(ps->vb);
break; break;
case PrefType::Bool: case PREF_BOOL:
value.boolVal = (ps->vb == kTrue); value.boolVal = (ps->vb == kTrue);
break; break;
default: default:
@ -154,7 +154,7 @@ PREF_FinalizeParseState(PrefParseState *ps)
* Pseudo-BNF * Pseudo-BNF
* ---------- * ----------
* function = LJUNK function-name JUNK function-args * function = LJUNK function-name JUNK function-args
* function-name = "user_pref" | "pref" | "sticky_pref" * function-name = "user_pref" | "pref"
* function-args = "(" JUNK pref-name JUNK "," JUNK pref-value JUNK ")" JUNK ";" * function-args = "(" JUNK pref-name JUNK "," JUNK pref-value JUNK ")" JUNK ";"
* pref-name = quoted-string * pref-name = quoted-string
* pref-value = quoted-string | "true" | "false" | integer-value * pref-value = quoted-string | "true" | "false" | integer-value
@ -188,7 +188,7 @@ PREF_ParseBuf(PrefParseState *ps, const char *buf, int bufLen)
if (ps->lbcur != ps->lb) { /* reset state */ if (ps->lbcur != ps->lb) { /* reset state */
ps->lbcur = ps->lb; ps->lbcur = ps->lb;
ps->vb = nullptr; ps->vb = nullptr;
ps->vtype = PrefType::Invalid; ps->vtype = PREF_INVALID;
ps->fdefault = false; ps->fdefault = false;
ps->fstickydefault = false; ps->fstickydefault = false;
} }
@ -200,15 +200,10 @@ PREF_ParseBuf(PrefParseState *ps, const char *buf, int bufLen)
state = PREF_PARSE_UNTIL_EOL; state = PREF_PARSE_UNTIL_EOL;
break; break;
case 'u': /* indicating user_pref */ case 'u': /* indicating user_pref */
case 's': /* indicating sticky_pref */
case 'p': /* indicating pref */ case 'p': /* indicating pref */
if (c == 'u') { case 's': /* indicating sticky_pref */
ps->smatch = kUserPref; ps->smatch = (c == 'u' ? kUserPref :
} else if (c == 's') { (c == 's' ? kPrefSticky : kPref));
ps->smatch = kPrefSticky;
} else {
ps->smatch = kPref;
}
ps->sindex = 1; ps->sindex = 1;
ps->nextstate = PREF_PARSE_UNTIL_OPEN_PAREN; ps->nextstate = PREF_PARSE_UNTIL_OPEN_PAREN;
state = PREF_PARSE_MATCH_STRING; state = PREF_PARSE_MATCH_STRING;
@ -253,8 +248,6 @@ PREF_ParseBuf(PrefParseState *ps, const char *buf, int bufLen)
case PREF_PARSE_UNTIL_NAME: case PREF_PARSE_UNTIL_NAME:
if (c == '\"' || c == '\'') { if (c == '\"' || c == '\'') {
ps->fdefault = (ps->smatch == kPref || ps->smatch == kPrefSticky); ps->fdefault = (ps->smatch == kPref || ps->smatch == kPrefSticky);
ps->fdefault = (ps->smatch == kPref ||
ps->smatch == kPrefSticky);
ps->fstickydefault = (ps->smatch == kPrefSticky); ps->fstickydefault = (ps->smatch == kPrefSticky);
ps->quotechar = c; ps->quotechar = c;
ps->nextstate = PREF_PARSE_UNTIL_COMMA; /* return here when done */ ps->nextstate = PREF_PARSE_UNTIL_COMMA; /* return here when done */
@ -291,21 +284,21 @@ PREF_ParseBuf(PrefParseState *ps, const char *buf, int bufLen)
/* the pref value type is unknown. so, we scan for the first /* the pref value type is unknown. so, we scan for the first
* character of the value, and determine the type from that. */ * character of the value, and determine the type from that. */
if (c == '\"' || c == '\'') { if (c == '\"' || c == '\'') {
ps->vtype = PrefType::String; ps->vtype = PREF_STRING;
ps->quotechar = c; ps->quotechar = c;
ps->nextstate = PREF_PARSE_UNTIL_CLOSE_PAREN; ps->nextstate = PREF_PARSE_UNTIL_CLOSE_PAREN;
state = PREF_PARSE_QUOTED_STRING; state = PREF_PARSE_QUOTED_STRING;
} }
else if (c == 't' || c == 'f') { else if (c == 't' || c == 'f') {
ps->vb = (char *) (c == 't' ? kTrue : kFalse); ps->vb = (char *) (c == 't' ? kTrue : kFalse);
ps->vtype = PrefType::Bool; ps->vtype = PREF_BOOL;
ps->smatch = ps->vb; ps->smatch = ps->vb;
ps->sindex = 1; ps->sindex = 1;
ps->nextstate = PREF_PARSE_UNTIL_CLOSE_PAREN; ps->nextstate = PREF_PARSE_UNTIL_CLOSE_PAREN;
state = PREF_PARSE_MATCH_STRING; state = PREF_PARSE_MATCH_STRING;
} }
else if (isdigit(c) || (c == '-') || (c == '+')) { else if (isdigit(c) || (c == '-') || (c == '+')) {
ps->vtype = PrefType::Int; ps->vtype = PREF_INT;
/* write c to line buffer... */ /* write c to line buffer... */
if (ps->lbcur == ps->lbend && !pref_GrowBuf(ps)) if (ps->lbcur == ps->lbend && !pref_GrowBuf(ps))
return false; /* out of memory */ return false; /* out of memory */
@ -518,12 +511,13 @@ PREF_ParseBuf(PrefParseState *ps, const char *buf, int bufLen)
break; break;
case PREF_PARSE_UNTIL_CLOSE_PAREN: case PREF_PARSE_UNTIL_CLOSE_PAREN:
/* tolerate only whitespace and embedded comments */ /* tolerate only whitespace and embedded comments */
if (c == ')') { if (c == ')')
state = PREF_PARSE_UNTIL_SEMICOLON; state = PREF_PARSE_UNTIL_SEMICOLON;
} else if (c == '/') { else if (c == '/') {
ps->nextstate = state; /* return here when done with comment */ ps->nextstate = state; /* return here when done with comment */
state = PREF_PARSE_COMMENT_MAYBE_START; state = PREF_PARSE_COMMENT_MAYBE_START;
} else if (!isspace(c)) { }
else if (!isspace(c)) {
NS_WARNING("malformed pref file"); NS_WARNING("malformed pref file");
return false; return false;
} }

View File

@ -54,7 +54,7 @@ typedef struct PrefParseState {
char *lbend; /* line buffer end */ char *lbend; /* line buffer end */
char *vb; /* value buffer (ptr into lb) */ char *vb; /* value buffer (ptr into lb) */
PrefType vtype; /* PREF_STRING,INT,BOOL */ PrefType vtype; /* PREF_STRING,INT,BOOL */
bool fdefault; /* true if (default) pref */ bool fdefault; /* true if (default) pref */
bool fstickydefault; /* true if (sticky) pref */ bool fstickydefault; /* true if (sticky) pref */
} PrefParseState; } PrefParseState;