Bug 1176159 (part 2) - Remove uses of PL_DHashTableEnumerate() from libpref. r=bsmedberg.

This commit is contained in:
Nicholas Nethercote 2015-06-18 17:58:24 -07:00
parent 38c2aad78a
commit 3ecfa5467b
4 changed files with 86 additions and 144 deletions

View File

@ -737,7 +737,11 @@ void
Preferences::GetPreferences(InfallibleTArray<PrefSetting>* aPrefs)
{
aPrefs->SetCapacity(gHashTable->Capacity());
PL_DHashTableEnumerate(gHashTable, pref_GetPrefs, aPrefs);
for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) {
auto entry = static_cast<PrefHashEntry*>(iter.Get());
dom::PrefSetting *pref = aPrefs->AppendElement();
pref_GetPrefFromEntry(entry, pref);
}
}
NS_IMETHODIMP
@ -958,11 +962,9 @@ Preferences::WritePrefFile(nsIFile* aFile)
nsAutoArrayPtr<char*> valueArray(new char*[gHashTable->EntryCount()]);
memset(valueArray, 0, gHashTable->EntryCount() * sizeof(char*));
pref_saveArgs saveArgs;
saveArgs.prefArray = valueArray;
// get the lines that we're supposed to be writing to the file
PL_DHashTableEnumerate(gHashTable, pref_savePref, &saveArgs);
pref_savePrefs(gHashTable, valueArray);
/* Sort the preferences to make a readable file on disk */
NS_QuickSort(valueArray, gHashTable->EntryCount(), sizeof(char *),

View File

@ -46,17 +46,6 @@
}
#endif
// Definitions
struct EnumerateData {
const char *parent;
nsTArray<nsCString> *pref_list;
};
// Prototypes
static PLDHashOperator
pref_enumChild(PLDHashTable *table, PLDHashEntryHdr *heh,
uint32_t i, void *arg);
using mozilla::dom::ContentChild;
static ContentChild*
@ -545,7 +534,6 @@ NS_IMETHODIMP nsPrefBranch::GetChildList(const char *aStartingAt, uint32_t *aCou
char **outArray;
int32_t numPrefs;
int32_t dwIndex;
EnumerateData ed;
nsAutoTArray<nsCString, 32> prefArray;
NS_ENSURE_ARG(aStartingAt);
@ -558,9 +546,14 @@ NS_IMETHODIMP nsPrefBranch::GetChildList(const char *aStartingAt, uint32_t *aCou
// this will contain a list of all the pref name strings
// allocate on the stack for speed
ed.parent = getPrefName(aStartingAt);
ed.pref_list = &prefArray;
PL_DHashTableEnumerate(gHashTable, pref_enumChild, &ed);
const char* parent = getPrefName(aStartingAt);
size_t parentLen = strlen(parent);
for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) {
auto entry = static_cast<PrefHashEntry*>(iter.Get());
if (strncmp(entry->key, parent, parentLen) == 0) {
prefArray.AppendElement(entry->key);
}
}
// now that we've built up the list, run the callback on
// all the matching elements
@ -773,18 +766,6 @@ const char *nsPrefBranch::getPrefName(const char *aPrefName)
return mPrefRoot.get();
}
static PLDHashOperator
pref_enumChild(PLDHashTable *table, PLDHashEntryHdr *heh,
uint32_t i, void *arg)
{
PrefHashEntry *he = static_cast<PrefHashEntry*>(heh);
EnumerateData *d = reinterpret_cast<EnumerateData *>(arg);
if (strncmp(he->key, d->parent, strlen(d->parent)) == 0) {
d->pref_list->AppendElement(he->key);
}
return PL_DHASH_NEXT;
}
//----------------------------------------------------------------------------
// nsPrefLocalizedString
//----------------------------------------------------------------------------

View File

@ -318,74 +318,54 @@ pref_SetPref(const dom::PrefSetting& aPref)
return rv;
}
PLDHashOperator
pref_savePref(PLDHashTable *table, PLDHashEntryHdr *heh, uint32_t i, void *arg)
void
pref_savePrefs(PLDHashTable* aTable, char** aPrefArray)
{
pref_saveArgs *argData = static_cast<pref_saveArgs *>(arg);
PrefHashEntry *pref = static_cast<PrefHashEntry *>(heh);
int32_t j = 0;
for (auto iter = aTable->Iter(); !iter.Done(); iter.Next()) {
auto pref = static_cast<PrefHashEntry*>(iter.Get());
PR_ASSERT(pref);
if (!pref)
return PL_DHASH_NEXT;
nsAutoCString prefValue;
nsAutoCString prefPrefix;
prefPrefix.AssignLiteral("user_pref(\"");
nsAutoCString prefValue;
nsAutoCString prefPrefix;
prefPrefix.AssignLiteral("user_pref(\"");
// where we're getting our pref from
PrefValue* sourcePref;
// where we're getting our pref from
PrefValue* sourcePref;
if (PREF_HAS_USER_VALUE(pref) &&
(pref_ValueChanged(pref->defaultPref,
pref->userPref,
(PrefType) PREF_TYPE(pref)) ||
!(pref->flags & PREF_HAS_DEFAULT) ||
pref->flags & PREF_STICKY_DEFAULT)) {
sourcePref = &pref->userPref;
} else {
// do not save default prefs that haven't changed
continue;
}
if (PREF_HAS_USER_VALUE(pref) &&
(pref_ValueChanged(pref->defaultPref,
pref->userPref,
(PrefType) PREF_TYPE(pref)) ||
!(pref->flags & PREF_HAS_DEFAULT) ||
pref->flags & PREF_STICKY_DEFAULT)) {
sourcePref = &pref->userPref;
} else {
// do not save default prefs that haven't changed
return PL_DHASH_NEXT;
// strings are in quotes!
if (pref->flags & PREF_STRING) {
prefValue = '\"';
str_escape(sourcePref->stringVal, prefValue);
prefValue += '\"';
} else if (pref->flags & PREF_INT) {
prefValue.AppendInt(sourcePref->intVal);
} else if (pref->flags & PREF_BOOL) {
prefValue = (sourcePref->boolVal) ? "true" : "false";
}
nsAutoCString prefName;
str_escape(pref->key, prefName);
aPrefArray[j++] = ToNewCString(prefPrefix +
prefName +
NS_LITERAL_CSTRING("\", ") +
prefValue +
NS_LITERAL_CSTRING(");"));
}
// strings are in quotes!
if (pref->flags & PREF_STRING) {
prefValue = '\"';
str_escape(sourcePref->stringVal, prefValue);
prefValue += '\"';
}
else if (pref->flags & PREF_INT)
prefValue.AppendInt(sourcePref->intVal);
else if (pref->flags & PREF_BOOL)
prefValue = (sourcePref->boolVal) ? "true" : "false";
nsAutoCString prefName;
str_escape(pref->key, prefName);
argData->prefArray[i] = ToNewCString(prefPrefix +
prefName +
NS_LITERAL_CSTRING("\", ") +
prefValue +
NS_LITERAL_CSTRING(");"));
return PL_DHASH_NEXT;
}
PLDHashOperator
pref_GetPrefs(PLDHashTable *table,
PLDHashEntryHdr *heh,
uint32_t i,
void *arg)
{
if (heh) {
PrefHashEntry *entry = static_cast<PrefHashEntry *>(heh);
dom::PrefSetting *pref =
static_cast<InfallibleTArray<dom::PrefSetting>*>(arg)->AppendElement();
pref_GetPrefFromEntry(entry, pref);
}
return PL_DHASH_NEXT;
}
static void
@ -549,23 +529,6 @@ nsresult PREF_GetBoolPref(const char *pref_name, bool * return_value, bool get_d
return rv;
}
/* Delete a branch. Used for deleting mime types */
static PLDHashOperator
pref_DeleteItem(PLDHashTable *table, PLDHashEntryHdr *heh, uint32_t i, void *arg)
{
PrefHashEntry* he = static_cast<PrefHashEntry*>(heh);
const char *to_delete = (const char *) arg;
int len = strlen(to_delete);
/* note if we're deleting "ldap" then we want to delete "ldap.xxx"
and "ldap" (if such a leaf node exists) but not "ldap_1.xxx" */
if (to_delete && (PL_strncmp(he->key, to_delete, (uint32_t) len) == 0 ||
(len-1 == (int)strlen(he->key) && PL_strncmp(he->key, to_delete, (uint32_t)(len-1)) == 0)))
return PL_DHASH_REMOVE;
return PL_DHASH_NEXT;
}
nsresult
PREF_DeleteBranch(const char *branch_name)
{
@ -588,8 +551,22 @@ PREF_DeleteBranch(const char *branch_name)
if ((len > 1) && branch_name[len - 1] != '.')
branch_dot += '.';
PL_DHashTableEnumerate(gHashTable, pref_DeleteItem,
(void*) branch_dot.get());
/* Delete a branch. Used for deleting mime types */
const char *to_delete = branch_dot.get();
MOZ_ASSERT(to_delete);
len = strlen(to_delete);
for (auto iter = gHashTable->RemovingIter(); !iter.Done(); iter.Next()) {
auto entry = static_cast<PrefHashEntry*>(iter.Get());
/* note if we're deleting "ldap" then we want to delete "ldap.xxx"
and "ldap" (if such a leaf node exists) but not "ldap_1.xxx" */
if (PL_strncmp(entry->key, to_delete, (uint32_t) len) == 0 ||
(len-1 == (int)strlen(entry->key) &&
PL_strncmp(entry->key, to_delete, (uint32_t)(len-1)) == 0)) {
iter.Remove();
}
}
gDirty = true;
return NS_OK;
}
@ -616,26 +593,6 @@ PREF_ClearUserPref(const char *pref_name)
return NS_OK;
}
static PLDHashOperator
pref_ClearUserPref(PLDHashTable *table, PLDHashEntryHdr *he, uint32_t,
void *arg)
{
PrefHashEntry *pref = static_cast<PrefHashEntry*>(he);
PLDHashOperator nextOp = PL_DHASH_NEXT;
if (PREF_HAS_USER_VALUE(pref))
{
pref->flags &= ~PREF_USERSET;
if (!(pref->flags & PREF_HAS_DEFAULT)) {
nextOp = PL_DHASH_REMOVE;
}
static_cast<std::vector<std::string>*>(arg)->push_back(std::string(pref->key));
}
return nextOp;
}
nsresult
PREF_ClearAllUserPrefs()
{
@ -647,7 +604,18 @@ PREF_ClearAllUserPrefs()
return NS_ERROR_NOT_INITIALIZED;
std::vector<std::string> prefStrings;
PL_DHashTableEnumerate(gHashTable, pref_ClearUserPref, static_cast<void*>(&prefStrings));
for (auto iter = gHashTable->RemovingIter(); !iter.Done(); iter.Next()) {
auto pref = static_cast<PrefHashEntry*>(iter.Get());
if (PREF_HAS_USER_VALUE(pref)) {
prefStrings.push_back(std::string(pref->key));
pref->flags &= ~PREF_USERSET;
if (!(pref->flags & PREF_HAS_DEFAULT)) {
iter.Remove();
}
}
}
for (std::string& prefString : prefStrings) {
pref_DoCallback(prefString.c_str());

View File

@ -19,17 +19,8 @@ class PrefSetting;
}
}
// Passed as the arg to pref_savePref
struct pref_saveArgs {
char **prefArray;
};
PLDHashOperator
pref_savePref(PLDHashTable *table, PLDHashEntryHdr *heh, uint32_t i, void *arg);
PLDHashOperator
pref_GetPrefs(PLDHashTable *table,
PLDHashEntryHdr *heh, uint32_t i, void *arg);
void
pref_savePrefs(PLDHashTable* aTable, char** aPrefArray);
nsresult
pref_SetPref(const mozilla::dom::PrefSetting& aPref);