Bug 1249157 - prefapi enums into class enums, explicit conversion, cleanup. r=bsmedberg

This commit is contained in:
Milan Sreckovic 2016-02-22 09:15:00 +01:00
parent c355797f79
commit 9fd3273e6e
6 changed files with 221 additions and 163 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) == PREF_INVALID) { if (Preferences::GetDefaultType(kTelemetryPref) == nsIPrefBranch::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,7 +127,21 @@ 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);
*_retval = PREF_GetPrefType(pref); switch (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->flags & PREF_STRING) if (pref->prefFlags.IsTypeString())
{ {
if (pref->defaultPref.stringVal) if (pref->defaultPref.stringVal)
PL_strfree(pref->defaultPref.stringVal); PL_strfree(pref->defaultPref.stringVal);
@ -117,13 +117,7 @@ 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;
@ -249,7 +243,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, PREF_STRING, set_default ? kPrefSetDefault : 0); return pref_HashPref(pref_name, pref, PrefType::String, set_default ? kPrefSetDefault : 0);
} }
nsresult nsresult
@ -258,7 +252,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, PREF_INT, set_default ? kPrefSetDefault : 0); return pref_HashPref(pref_name, pref, PrefType::Int, set_default ? kPrefSetDefault : 0);
} }
nsresult nsresult
@ -267,7 +261,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, PREF_BOOL, set_default ? kPrefSetDefault : 0); return pref_HashPref(pref_name, pref, PrefType::Bool, set_default ? kPrefSetDefault : 0);
} }
enum WhichValue { DEFAULT_VALUE, USER_VALUE }; enum WhichValue { DEFAULT_VALUE, USER_VALUE };
@ -335,12 +329,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_HAS_USER_VALUE(pref) && if (pref->prefFlags.HasUserValue() &&
(pref_ValueChanged(pref->defaultPref, (pref_ValueChanged(pref->defaultPref,
pref->userPref, pref->userPref,
(PrefType) PREF_TYPE(pref)) || pref->prefFlags.GetPrefType()) ||
!(pref->flags & PREF_HAS_DEFAULT) || !(pref->prefFlags.HasDefault()) ||
pref->flags & PREF_STICKY_DEFAULT)) { pref->prefFlags.HasStickyDefault())) {
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
@ -348,15 +342,15 @@ pref_savePrefs(PLDHashTable* aTable)
} }
// strings are in quotes! // strings are in quotes!
if (pref->flags & PREF_STRING) { if (pref->prefFlags.IsTypeString()) {
prefValue = '\"'; prefValue = '\"';
str_escape(sourcePref->stringVal, prefValue); str_escape(sourcePref->stringVal, prefValue);
prefValue += '\"'; prefValue += '\"';
} else if (pref->flags & PREF_INT) { } else if (pref->prefFlags.IsTypeInt()) {
prefValue.AppendInt(sourcePref->intVal); prefValue.AppendInt(sourcePref->intVal);
} else if (pref->flags & PREF_BOOL) { } else if (pref->prefFlags.IsTypeBool()) {
prefValue = (sourcePref->boolVal) ? "true" : "false"; prefValue = (sourcePref->boolVal) ? "true" : "false";
} }
@ -389,14 +383,14 @@ GetPrefValueFromEntry(PrefHashEntry *aHashEntry, dom::PrefSetting* aPref,
settingValue = &aPref->defaultValue().get_PrefValue(); settingValue = &aPref->defaultValue().get_PrefValue();
} }
switch (aHashEntry->flags & PREF_VALUETYPE_MASK) { switch (aHashEntry->prefFlags.GetPrefType()) {
case PREF_STRING: case PrefType::String:
*settingValue = nsDependentCString(value->stringVal); *settingValue = nsDependentCString(value->stringVal);
return; return;
case PREF_INT: case PrefType::Int:
*settingValue = value->intVal; *settingValue = value->intVal;
return; return;
case PREF_BOOL: case PrefType::Bool:
*settingValue = !!value->boolVal; *settingValue = !!value->boolVal;
return; return;
default: default:
@ -408,12 +402,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 (PREF_HAS_DEFAULT_VALUE(aHashEntry)) { if (aHashEntry->prefFlags.HasDefault()) {
GetPrefValueFromEntry(aHashEntry, aPref, DEFAULT_VALUE); GetPrefValueFromEntry(aHashEntry, aPref, DEFAULT_VALUE);
} else { } else {
aPref->defaultValue() = null_t(); aPref->defaultValue() = null_t();
} }
if (PREF_HAS_USER_VALUE(aHashEntry)) { if (aHashEntry->prefFlags.HasUserValue()) {
GetPrefValueFromEntry(aHashEntry, aPref, USER_VALUE); GetPrefValueFromEntry(aHashEntry, aPref, USER_VALUE);
} else { } else {
aPref->userValue() = null_t(); aPref->userValue() = null_t();
@ -451,11 +445,7 @@ bool PREF_HasUserPref(const char *pref_name)
return false; return false;
PrefHashEntry *pref = pref_HashTableLookup(pref_name); PrefHashEntry *pref = pref_HashTableLookup(pref_name);
if (!pref) return false; return pref && pref->prefFlags.HasUserValue();
/* convert PREF_HAS_USER_VALUE to bool */
return (PREF_HAS_USER_VALUE(pref) != 0);
} }
nsresult nsresult
@ -468,12 +458,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->flags & PREF_STRING)) if (pref && (pref->prefFlags.IsTypeString())) {
{ 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);
@ -490,18 +480,17 @@ 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->flags & PREF_INT)) if (pref && (pref->prefFlags.IsTypeInt())) {
{ 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->flags & PREF_HAS_DEFAULT)) if (!pref->prefFlags.HasDefault()) {
return NS_ERROR_UNEXPECTED; return NS_ERROR_UNEXPECTED;
*return_int = tempInt;
} }
else *return_int = tempInt;
} else {
*return_int = pref->userPref.intVal; *return_int = pref->userPref.intVal;
}
rv = NS_OK; rv = NS_OK;
} }
return rv; return rv;
@ -515,18 +504,15 @@ 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->flags & PREF_BOOL)) if (pref && (pref->prefFlags.IsTypeBool())) {
{ 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->flags & PREF_HAS_DEFAULT) { if (pref->prefFlags.HasDefault()) {
*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;
} }
@ -584,11 +570,10 @@ 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_HAS_USER_VALUE(pref)) if (pref && pref->prefFlags.HasUserValue()) {
{ pref->prefFlags.SetHasUserValue(false);
pref->flags &= ~PREF_USERSET;
if (!(pref->flags & PREF_HAS_DEFAULT)) { if (!pref->prefFlags.HasDefault()) {
gHashTable->RemoveEntry(pref); gHashTable->RemoveEntry(pref);
} }
@ -612,11 +597,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_HAS_USER_VALUE(pref)) { if (pref->prefFlags.HasUserValue()) {
prefStrings.push_back(std::string(pref->key)); prefStrings.push_back(std::string(pref->key));
pref->flags &= ~PREF_USERSET; pref->prefFlags.SetHasUserValue(false);
if (!(pref->flags & PREF_HAS_DEFAULT)) { if (!pref->prefFlags.HasDefault()) {
iter.Remove(); iter.Remove();
} }
} }
@ -640,18 +625,14 @@ nsresult PREF_LockPref(const char *key, bool lockit)
return NS_ERROR_UNEXPECTED; return NS_ERROR_UNEXPECTED;
if (lockit) { if (lockit) {
if (!PREF_IS_LOCKED(pref)) if (!pref->prefFlags.IsLocked()) {
{ pref->prefFlags.SetLocked(true);
pref->flags |= PREF_LOCKED;
gIsAnyPrefLocked = true; gIsAnyPrefLocked = true;
pref_DoCallback(key); pref_DoCallback(key);
} }
} } else {
else if (pref->prefFlags.IsLocked()) {
{ pref->prefFlags.SetLocked(false);
if (PREF_IS_LOCKED(pref))
{
pref->flags &= ~PREF_LOCKED;
pref_DoCallback(key); pref_DoCallback(key);
} }
} }
@ -664,15 +645,23 @@ 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;
if (type & PREF_STRING) switch(type) {
{ 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);
} }
else if (type & PREF_INT) break;
case PrefType::Int:
changed = oldValue.intVal != newValue.intVal; changed = oldValue.intVal != newValue.intVal;
else if (type & PREF_BOOL) break;
case PrefType::Bool:
changed = oldValue.boolVal != newValue.boolVal; changed = oldValue.boolVal != newValue.boolVal;
break;
case PrefType::Invalid:
default:
changed = false;
break;
}
return changed; return changed;
} }
@ -681,20 +670,21 @@ 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 void pref_SetValue(PrefValue* existingValue, uint16_t *existingFlags, static PrefTypeFlags pref_SetValue(PrefValue* existingValue, PrefTypeFlags flags,
PrefValue newValue, PrefType newType) PrefValue newValue, PrefType newType)
{ {
if ((*existingFlags & PREF_STRING) && existingValue->stringVal) { if (flags.IsTypeString() && existingValue->stringVal) {
PL_strfree(existingValue->stringVal); PL_strfree(existingValue->stringVal);
} }
*existingFlags = (*existingFlags & ~PREF_VALUETYPE_MASK) | newType; flags.SetPrefType(newType);
if (newType & PREF_STRING) { if (flags.IsTypeString()) {
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)
@ -723,63 +713,53 @@ 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->flags = type; pref->prefFlags.Reset().SetPrefType(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()) {
if (!PREF_IS_LOCKED(pref)) /* ?? change of semantics? */
{ /* ?? change of semantics? */
if (pref_ValueChanged(pref->defaultPref, value, type) || if (pref_ValueChanged(pref->defaultPref, value, type) ||
!(pref->flags & PREF_HAS_DEFAULT)) !pref->prefFlags.HasDefault()) {
{ pref->prefFlags = pref_SetValue(&pref->defaultPref, pref->prefFlags, value, type).SetHasDefault(true);
pref_SetValue(&pref->defaultPref, &pref->flags, value, type); if (flags & kPrefStickyDefault) {
pref->flags |= PREF_HAS_DEFAULT; pref->prefFlags.SetHasStickyDefault(true);
if (flags & kPrefStickyDefault) }
pref->flags |= PREF_STICKY_DEFAULT; if (!pref->prefFlags.HasUserValue()) {
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->flags & PREF_HAS_DEFAULT) && if ((pref->prefFlags.HasDefault()) &&
!(pref->flags & PREF_STICKY_DEFAULT) && !(pref->prefFlags.HasStickyDefault()) &&
!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->flags &= ~PREF_USERSET; pref->prefFlags.SetHasUserValue(false);
if (!PREF_IS_LOCKED(pref)) { if (!pref->prefFlags.IsLocked()) {
gDirty = true; gDirty = true;
valueChanged = true; valueChanged = true;
} }
} }
} } else if (!pref->prefFlags.HasUserValue() ||
else if (!PREF_HAS_USER_VALUE(pref) || !pref->prefFlags.IsPrefType(type) ||
PREF_TYPE(pref) != type || pref_ValueChanged(pref->userPref, value, type) ) {
pref_ValueChanged(pref->userPref, value, type) ) pref->prefFlags = pref_SetValue(&pref->userPref, pref->prefFlags, value, type).SetHasUserValue(true);
{ 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;
} }
@ -808,17 +788,11 @@ 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 PREF_INVALID; return PrefType::Invalid;
} }
/* -- */ /* -- */
@ -829,9 +803,10 @@ 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_IS_LOCKED(pref)) if (pref && pref->prefFlags.IsLocked()) {
result = true; result = true;
} }
}
return result; return result;
} }
@ -973,10 +948,16 @@ void PREF_ReaderCallback(void *closure,
PrefType type, PrefType type,
bool isDefault, bool isDefault,
bool isStickyDefault) bool isStickyDefault)
{ {
uint32_t flags = isDefault ? kPrefSetDefault : kPrefForceSet; uint32_t flags = 0;
if (isDefault && isStickyDefault) { if (isDefault) {
flags |= kPrefSetDefault;
if (isStickyDefault) {
flags |= kPrefStickyDefault; flags |= kPrefStickyDefault;
} }
} else {
flags |= kPrefForceSet;
}
pref_HashPref(pref, value, type, flags); pref_HashPref(pref, value, type, flags);
} }

View File

@ -28,14 +28,6 @@ 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
@ -53,18 +45,83 @@ void PREF_CleanupPrefs();
/* /*
// <font color=blue> // <font color=blue>
// Preference flags, including the native type of the preference // Preference flags, including the native type of the preference. Changing any of these
// values will require modifying the code inside of PrefTypeFlags class.
// </font> // </font>
*/ */
typedef enum { PREF_INVALID = 0, enum class PrefType {
PREF_LOCKED = 1, PREF_USERSET = 2, PREF_CONFIG = 4, PREF_REMOTE = 8, Invalid = 0,
PREF_LILOCAL = 16, PREF_STRING = 32, PREF_INT = 64, PREF_BOOL = 128, String = 1,
PREF_HAS_DEFAULT = 256, Int = 2,
// pref is default pref with "sticky" semantics Bool = 3,
PREF_STICKY_DEFAULT = 512, };
PREF_VALUETYPE_MASK = (PREF_STRING | PREF_INT | PREF_BOOL)
} PrefType; // Keep the type of the preference, as well as the flags guiding its behaviour.
class PrefTypeFlags
{
public:
PrefTypeFlags() : mValue(AsInt(PrefType::Invalid)) {}
explicit 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>
@ -173,9 +230,9 @@ 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 );
/* /*

View File

@ -114,17 +114,17 @@ pref_DoCallback(PrefParseState *ps)
PrefValue value; PrefValue value;
switch (ps->vtype) { switch (ps->vtype) {
case PREF_STRING: case PrefType::String:
value.stringVal = ps->vb; value.stringVal = ps->vb;
break; break;
case PREF_INT: case PrefType::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 PREF_BOOL: case PrefType::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" * function-name = "user_pref" | "pref" | "sticky_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 = PREF_INVALID; ps->vtype = PrefType::Invalid;
ps->fdefault = false; ps->fdefault = false;
ps->fstickydefault = false; ps->fstickydefault = false;
} }
@ -200,10 +200,15 @@ 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 'p': /* indicating pref */
case 's': /* indicating sticky_pref */ case 's': /* indicating sticky_pref */
ps->smatch = (c == 'u' ? kUserPref : case 'p': /* indicating pref */
(c == 's' ? kPrefSticky : kPref)); if (c == 'u') {
ps->smatch = kUserPref;
} else if (c == 's') {
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;
@ -248,6 +253,8 @@ 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 */
@ -284,21 +291,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 = PREF_STRING; ps->vtype = PrefType::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 = PREF_BOOL; ps->vtype = PrefType::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 = PREF_INT; ps->vtype = PrefType::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 */
@ -511,13 +518,12 @@ 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;
} }