2012-08-22 13:00:21 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 13:00:21 -07:00
|
|
|
#include "base/basictypes.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "prefapi.h"
|
|
|
|
#include "prefapi_private_data.h"
|
|
|
|
#include "prefread.h"
|
2014-03-05 17:58:01 -08:00
|
|
|
#include "MainThreadUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
|
|
|
|
#define PL_ARENA_CONST_ALIGN_MASK 3
|
|
|
|
#include "plarena.h"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include "windows.h"
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
#include "plstr.h"
|
|
|
|
#include "pldhash.h"
|
|
|
|
#include "plbase64.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
#include "prprf.h"
|
2013-06-23 05:03:39 -07:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2012-08-22 13:00:21 -07:00
|
|
|
#include "mozilla/dom/PContent.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsQuickSort.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsPrintfCString.h"
|
|
|
|
#include "prlink.h"
|
|
|
|
|
2012-08-22 13:00:21 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static void
|
2007-03-22 10:30:00 -07:00
|
|
|
clearPrefEntry(PLDHashTable *table, PLDHashEntryHdr *entry)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
PrefHashEntry *pref = static_cast<PrefHashEntry *>(entry);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (pref->flags & PREF_STRING)
|
|
|
|
{
|
2009-03-29 04:45:19 -07:00
|
|
|
if (pref->defaultPref.stringVal)
|
|
|
|
PL_strfree(pref->defaultPref.stringVal);
|
|
|
|
if (pref->userPref.stringVal)
|
|
|
|
PL_strfree(pref->userPref.stringVal);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
// don't need to free this as it's allocated in memory owned by
|
|
|
|
// gPrefNameArena
|
2012-07-30 07:20:58 -07:00
|
|
|
pref->key = nullptr;
|
2014-08-25 16:56:33 -07:00
|
|
|
memset(entry, 0, table->EntrySize());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2007-03-22 10:30:00 -07:00
|
|
|
matchPrefEntry(PLDHashTable*, const PLDHashEntryHdr* entry,
|
|
|
|
const void* key)
|
|
|
|
{
|
|
|
|
const PrefHashEntry *prefEntry =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<const PrefHashEntry*>(entry);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
if (prefEntry->key == key) return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
if (!prefEntry->key || !key) return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-08 00:08:04 -07:00
|
|
|
const char *otherKey = reinterpret_cast<const char*>(key);
|
2007-03-22 10:30:00 -07:00
|
|
|
return (strcmp(prefEntry->key, otherKey) == 0);
|
|
|
|
}
|
|
|
|
|
2014-08-25 16:56:33 -07:00
|
|
|
PLDHashTable gHashTable;
|
2007-03-22 10:30:00 -07:00
|
|
|
static PLArenaPool gPrefNameArena;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool gDirty = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-23 13:36:26 -07:00
|
|
|
static struct CallbackNode* gCallbacks = nullptr;
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool gIsAnyPrefLocked = false;
|
2007-11-16 20:25:41 -08:00
|
|
|
// These are only used during the call to pref_DoCallback
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool gCallbacksInProgress = false;
|
|
|
|
static bool gShouldCleanupDeadNodes = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
static PLDHashTableOps pref_HashTableOps = {
|
|
|
|
PL_DHashAllocTable,
|
|
|
|
PL_DHashFreeTable,
|
|
|
|
PL_DHashStringKey,
|
|
|
|
matchPrefEntry,
|
|
|
|
PL_DHashMoveEntryStub,
|
|
|
|
clearPrefEntry,
|
|
|
|
PL_DHashFinalizeStub,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr,
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// PR_ALIGN_OF_WORD is only defined on some platforms. ALIGN_OF_WORD has
|
|
|
|
// already been defined to PR_ALIGN_OF_WORD everywhere
|
|
|
|
#ifndef PR_ALIGN_OF_WORD
|
|
|
|
#define PR_ALIGN_OF_WORD PR_ALIGN_OF_POINTER
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// making PrefName arena 8k for nice allocation
|
|
|
|
#define PREFNAME_ARENA_SIZE 8192
|
|
|
|
|
|
|
|
#define WORD_ALIGN_MASK (PR_ALIGN_OF_WORD - 1)
|
|
|
|
|
|
|
|
// sanity checking
|
|
|
|
#if (PR_ALIGN_OF_WORD & WORD_ALIGN_MASK) != 0
|
|
|
|
#error "PR_ALIGN_OF_WORD must be a power of 2!"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// equivalent to strdup() - does no error checking,
|
|
|
|
// we're assuming we're only called with a valid pointer
|
|
|
|
static char *ArenaStrDup(const char* str, PLArenaPool* aArena)
|
|
|
|
{
|
|
|
|
void* mem;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len = strlen(str);
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_ARENA_ALLOCATE(mem, aArena, len+1);
|
|
|
|
if (mem)
|
|
|
|
memcpy(mem, str, len+1);
|
2007-07-08 00:08:04 -07:00
|
|
|
return static_cast<char*>(mem);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#define PREF_IS_LOCKED(pref) ((pref)->flags & PREF_LOCKED)
|
2012-08-22 13:00:21 -07:00
|
|
|
#define PREF_HAS_DEFAULT_VALUE(pref) ((pref)->flags & PREF_HAS_DEFAULT)
|
2007-03-22 10:30:00 -07:00
|
|
|
#define PREF_HAS_USER_VALUE(pref) ((pref)->flags & PREF_USERSET)
|
|
|
|
#define PREF_TYPE(pref) (PrefType)((pref)->flags & PREF_VALUETYPE_MASK)
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool pref_ValueChanged(PrefValue oldValue, PrefValue newValue, PrefType type);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* -- Privates */
|
|
|
|
struct CallbackNode {
|
|
|
|
char* domain;
|
2007-11-16 20:25:41 -08:00
|
|
|
// If someone attempts to remove the node from the callback list while
|
2012-07-30 07:20:58 -07:00
|
|
|
// pref_DoCallback is running, |func| is set to nullptr. Such nodes will
|
2007-11-16 20:25:41 -08:00
|
|
|
// be removed at the end of pref_DoCallback.
|
2007-03-22 10:30:00 -07:00
|
|
|
PrefChangedFunc func;
|
|
|
|
void* data;
|
|
|
|
struct CallbackNode* next;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* -- Prototypes */
|
|
|
|
static nsresult pref_DoCallback(const char* changed_pref);
|
|
|
|
|
2012-04-03 17:08:28 -07:00
|
|
|
enum {
|
|
|
|
kPrefSetDefault = 1,
|
|
|
|
kPrefForceSet = 2
|
|
|
|
};
|
2012-08-22 08:56:38 -07:00
|
|
|
static nsresult pref_HashPref(const char *key, PrefValue value, PrefType type, uint32_t flags);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-06 06:31:21 -07:00
|
|
|
#define PREF_HASHTABLE_INITIAL_LENGTH 1024
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult PREF_Init()
|
|
|
|
{
|
|
|
|
if (!gHashTable.ops) {
|
2012-07-30 07:20:58 -07:00
|
|
|
if (!PL_DHashTableInit(&gHashTable, &pref_HashTableOps, nullptr,
|
2014-08-06 06:31:21 -07:00
|
|
|
sizeof(PrefHashEntry), fallible_t(),
|
|
|
|
PREF_HASHTABLE_INITIAL_LENGTH)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
gHashTable.ops = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
PL_INIT_ARENA_POOL(&gPrefNameArena, "PrefNameArena",
|
|
|
|
PREFNAME_ARENA_SIZE);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Frees the callback list. */
|
|
|
|
void PREF_Cleanup()
|
|
|
|
{
|
2007-11-16 20:25:41 -08:00
|
|
|
NS_ASSERTION(!gCallbacksInProgress,
|
2011-10-17 07:59:28 -07:00
|
|
|
"PREF_Cleanup was called while gCallbacksInProgress is true!");
|
2007-03-22 10:30:00 -07:00
|
|
|
struct CallbackNode* node = gCallbacks;
|
|
|
|
struct CallbackNode* next_node;
|
|
|
|
|
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
next_node = node->next;
|
2009-03-29 04:45:19 -07:00
|
|
|
PL_strfree(node->domain);
|
|
|
|
free(node);
|
2007-03-22 10:30:00 -07:00
|
|
|
node = next_node;
|
|
|
|
}
|
2013-10-23 13:36:26 -07:00
|
|
|
gCallbacks = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
PREF_CleanupPrefs();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Frees up all the objects except the callback list. */
|
|
|
|
void PREF_CleanupPrefs()
|
|
|
|
{
|
|
|
|
if (gHashTable.ops) {
|
|
|
|
PL_DHashTableFinish(&gHashTable);
|
2012-07-30 07:20:58 -07:00
|
|
|
gHashTable.ops = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_FinishArenaPool(&gPrefNameArena);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// note that this appends to aResult, and does not assign!
|
|
|
|
static void str_escape(const char * original, nsAFlatCString& aResult)
|
|
|
|
{
|
|
|
|
/* JavaScript does not allow quotes, slashes, or line terminators inside
|
|
|
|
* strings so we must escape them. ECMAScript defines four line
|
|
|
|
* terminators, but we're only worrying about \r and \n here. We currently
|
|
|
|
* feed our pref script to the JS interpreter as Latin-1 so we won't
|
|
|
|
* encounter \u2028 (line separator) or \u2029 (paragraph separator).
|
|
|
|
*
|
|
|
|
* WARNING: There are hints that we may be moving to storing prefs
|
|
|
|
* as utf8. If we ever feed them to the JS compiler as UTF8 then
|
|
|
|
* we'll have to worry about the multibyte sequences that would be
|
|
|
|
* interpreted as \u2028 and \u2029
|
|
|
|
*/
|
|
|
|
const char *p;
|
|
|
|
|
2013-10-23 13:36:26 -07:00
|
|
|
if (original == nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Paranoid worst case all slashes will free quickly */
|
|
|
|
for (p=original; *p; ++p)
|
|
|
|
{
|
|
|
|
switch (*p)
|
|
|
|
{
|
|
|
|
case '\n':
|
2014-05-21 20:48:51 -07:00
|
|
|
aResult.AppendLiteral("\\n");
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '\r':
|
2014-05-21 20:48:51 -07:00
|
|
|
aResult.AppendLiteral("\\r");
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '\\':
|
2014-05-21 20:48:51 -07:00
|
|
|
aResult.AppendLiteral("\\\\");
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '\"':
|
2014-05-21 20:48:51 -07:00
|
|
|
aResult.AppendLiteral("\\\"");
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
aResult.Append(*p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** External calls
|
|
|
|
*/
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
PREF_SetCharPref(const char *pref_name, const char *value, bool set_default)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-01-21 12:51:05 -08:00
|
|
|
if ((uint32_t)strlen(value) > MAX_PREF_LENGTH) {
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
PrefValue pref;
|
2014-01-21 12:51:05 -08:00
|
|
|
pref.stringVal = (char*)value;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-04-03 17:08:28 -07:00
|
|
|
return pref_HashPref(pref_name, pref, PREF_STRING, set_default ? kPrefSetDefault : 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
PREF_SetIntPref(const char *pref_name, int32_t value, bool set_default)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
PrefValue pref;
|
|
|
|
pref.intVal = value;
|
|
|
|
|
2012-04-03 17:08:28 -07:00
|
|
|
return pref_HashPref(pref_name, pref, PREF_INT, set_default ? kPrefSetDefault : 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
PREF_SetBoolPref(const char *pref_name, bool value, bool set_default)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
PrefValue pref;
|
2011-08-22 19:15:52 -07:00
|
|
|
pref.boolVal = value;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-04-03 17:08:28 -07:00
|
|
|
return pref_HashPref(pref_name, pref, PREF_BOOL, set_default ? kPrefSetDefault : 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 13:00:21 -07:00
|
|
|
enum WhichValue { DEFAULT_VALUE, USER_VALUE };
|
|
|
|
static nsresult
|
|
|
|
SetPrefValue(const char* aPrefName, const dom::PrefValue& aValue,
|
|
|
|
WhichValue aWhich)
|
2010-10-19 13:35:08 -07:00
|
|
|
{
|
2012-08-22 13:00:21 -07:00
|
|
|
bool setDefault = (aWhich == DEFAULT_VALUE);
|
|
|
|
switch (aValue.type()) {
|
|
|
|
case dom::PrefValue::TnsCString:
|
|
|
|
return PREF_SetCharPref(aPrefName, aValue.get_nsCString().get(),
|
|
|
|
setDefault);
|
|
|
|
case dom::PrefValue::Tint32_t:
|
|
|
|
return PREF_SetIntPref(aPrefName, aValue.get_int32_t(),
|
|
|
|
setDefault);
|
|
|
|
case dom::PrefValue::Tbool:
|
|
|
|
return PREF_SetBoolPref(aPrefName, aValue.get_bool(),
|
|
|
|
setDefault);
|
|
|
|
default:
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH();
|
2012-08-22 13:00:21 -07:00
|
|
|
}
|
|
|
|
}
|
2010-10-19 13:35:08 -07:00
|
|
|
|
2012-08-22 13:00:21 -07:00
|
|
|
nsresult
|
|
|
|
pref_SetPref(const dom::PrefSetting& aPref)
|
|
|
|
{
|
|
|
|
const char* prefName = aPref.name().get();
|
|
|
|
const dom::MaybePrefValue& defaultValue = aPref.defaultValue();
|
|
|
|
const dom::MaybePrefValue& userValue = aPref.userValue();
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
if (defaultValue.type() == dom::MaybePrefValue::TPrefValue) {
|
|
|
|
rv = SetPrefValue(prefName, defaultValue.get_PrefValue(), DEFAULT_VALUE);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2010-10-19 13:35:08 -07:00
|
|
|
|
2012-08-22 13:00:21 -07:00
|
|
|
if (userValue.type() == dom::MaybePrefValue::TPrefValue) {
|
|
|
|
rv = SetPrefValue(prefName, userValue.get_PrefValue(), USER_VALUE);
|
|
|
|
} else {
|
2014-09-04 06:52:28 -07:00
|
|
|
rv = PREF_ClearUserPref(prefName);
|
2010-10-19 13:35:08 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 13:00:21 -07:00
|
|
|
// NB: we should never try to clear a default value, that doesn't
|
|
|
|
// make sense
|
|
|
|
|
|
|
|
return rv;
|
2010-10-19 13:35:08 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
PLDHashOperator
|
2012-08-22 08:56:38 -07:00
|
|
|
pref_savePref(PLDHashTable *table, PLDHashEntryHdr *heh, uint32_t i, void *arg)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
pref_saveArgs *argData = static_cast<pref_saveArgs *>(arg);
|
|
|
|
PrefHashEntry *pref = static_cast<PrefHashEntry *>(heh);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
PR_ASSERT(pref);
|
|
|
|
if (!pref)
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString prefValue;
|
|
|
|
nsAutoCString prefPrefix;
|
2014-05-21 20:48:52 -07:00
|
|
|
prefPrefix.AssignLiteral("user_pref(\"");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// where we're getting our pref from
|
|
|
|
PrefValue* sourcePref;
|
|
|
|
|
|
|
|
if (PREF_HAS_USER_VALUE(pref) &&
|
2011-08-22 19:15:52 -07:00
|
|
|
(pref_ValueChanged(pref->defaultPref,
|
|
|
|
pref->userPref,
|
|
|
|
(PrefType) PREF_TYPE(pref)) ||
|
|
|
|
!(pref->flags & PREF_HAS_DEFAULT))) {
|
2007-03-22 10:30:00 -07:00
|
|
|
sourcePref = &pref->userPref;
|
2010-09-27 14:23:35 -07:00
|
|
|
} else {
|
|
|
|
if (argData->saveTypes == SAVE_ALL_AND_DEFAULTS) {
|
2014-05-21 20:48:52 -07:00
|
|
|
prefPrefix.AssignLiteral("pref(\"");
|
2010-09-27 14:23:35 -07:00
|
|
|
sourcePref = &pref->defaultPref;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
// do not save default prefs that haven't changed
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// 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";
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString prefName;
|
2007-03-22 10:30:00 -07:00
|
|
|
str_escape(pref->key, prefName);
|
|
|
|
|
2010-09-27 14:23:35 -07:00
|
|
|
argData->prefArray[i] = ToNewCString(prefPrefix +
|
|
|
|
prefName +
|
|
|
|
NS_LITERAL_CSTRING("\", ") +
|
|
|
|
prefValue +
|
|
|
|
NS_LITERAL_CSTRING(");"));
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2010-10-19 13:35:08 -07:00
|
|
|
PLDHashOperator
|
2012-08-22 13:00:21 -07:00
|
|
|
pref_GetPrefs(PLDHashTable *table,
|
|
|
|
PLDHashEntryHdr *heh,
|
|
|
|
uint32_t i,
|
|
|
|
void *arg)
|
2010-10-19 13:35:08 -07:00
|
|
|
{
|
|
|
|
if (heh) {
|
|
|
|
PrefHashEntry *entry = static_cast<PrefHashEntry *>(heh);
|
2012-08-22 13:00:21 -07:00
|
|
|
dom::PrefSetting *pref =
|
|
|
|
static_cast<InfallibleTArray<dom::PrefSetting>*>(arg)->AppendElement();
|
2010-10-19 13:35:08 -07:00
|
|
|
|
2012-08-22 13:00:21 -07:00
|
|
|
pref_GetPrefFromEntry(entry, pref);
|
2010-10-19 13:35:08 -07:00
|
|
|
}
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2012-08-22 13:00:21 -07:00
|
|
|
static void
|
|
|
|
GetPrefValueFromEntry(PrefHashEntry *aHashEntry, dom::PrefSetting* aPref,
|
|
|
|
WhichValue aWhich)
|
2010-10-19 13:35:08 -07:00
|
|
|
{
|
2012-08-22 13:00:21 -07:00
|
|
|
PrefValue* value;
|
|
|
|
dom::PrefValue* settingValue;
|
|
|
|
if (aWhich == USER_VALUE) {
|
|
|
|
value = &aHashEntry->userPref;
|
|
|
|
aPref->userValue() = dom::PrefValue();
|
|
|
|
settingValue = &aPref->userValue().get_PrefValue();
|
|
|
|
} else {
|
|
|
|
value = &aHashEntry->defaultPref;
|
|
|
|
aPref->defaultValue() = dom::PrefValue();
|
|
|
|
settingValue = &aPref->defaultValue().get_PrefValue();
|
|
|
|
}
|
2010-10-19 13:35:08 -07:00
|
|
|
|
|
|
|
switch (aHashEntry->flags & PREF_VALUETYPE_MASK) {
|
2012-08-22 13:00:21 -07:00
|
|
|
case PREF_STRING:
|
|
|
|
*settingValue = nsDependentCString(value->stringVal);
|
|
|
|
return;
|
|
|
|
case PREF_INT:
|
|
|
|
*settingValue = value->intVal;
|
|
|
|
return;
|
|
|
|
case PREF_BOOL:
|
|
|
|
*settingValue = !!value->boolVal;
|
|
|
|
return;
|
|
|
|
default:
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH();
|
2012-08-22 13:00:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pref_GetPrefFromEntry(PrefHashEntry *aHashEntry, dom::PrefSetting* aPref)
|
|
|
|
{
|
|
|
|
aPref->name() = aHashEntry->key;
|
|
|
|
if (PREF_HAS_DEFAULT_VALUE(aHashEntry)) {
|
|
|
|
GetPrefValueFromEntry(aHashEntry, aPref, DEFAULT_VALUE);
|
|
|
|
} else {
|
|
|
|
aPref->defaultValue() = null_t();
|
2010-10-19 13:35:08 -07:00
|
|
|
}
|
2012-08-22 13:00:21 -07:00
|
|
|
if (PREF_HAS_USER_VALUE(aHashEntry)) {
|
|
|
|
GetPrefValueFromEntry(aHashEntry, aPref, USER_VALUE);
|
|
|
|
} else {
|
|
|
|
aPref->userValue() = null_t();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aPref->defaultValue().type() == dom::MaybePrefValue::Tnull_t ||
|
|
|
|
aPref->userValue().type() == dom::MaybePrefValue::Tnull_t ||
|
|
|
|
(aPref->defaultValue().get_PrefValue().type() ==
|
|
|
|
aPref->userValue().get_PrefValue().type()));
|
2010-10-19 13:35:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
int
|
2007-03-22 10:30:00 -07:00
|
|
|
pref_CompareStrings(const void *v1, const void *v2, void *unused)
|
|
|
|
{
|
|
|
|
char *s1 = *(char**) v1;
|
|
|
|
char *s2 = *(char**) v2;
|
|
|
|
|
|
|
|
if (!s1)
|
|
|
|
{
|
|
|
|
if (!s2)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (!s2)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return strcmp(s1, s2);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool PREF_HasUserPref(const char *pref_name)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!gHashTable.ops)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
PrefHashEntry *pref = pref_HashTableLookup(pref_name);
|
2011-10-17 07:59:28 -07:00
|
|
|
if (!pref) return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* convert PREF_HAS_USER_VALUE to bool */
|
|
|
|
return (PREF_HAS_USER_VALUE(pref) != 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
PREF_CopyCharPref(const char *pref_name, char ** return_buffer, bool get_default)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!gHashTable.ops)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
nsresult rv = NS_ERROR_UNEXPECTED;
|
|
|
|
char* stringVal;
|
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(pref_name);
|
|
|
|
|
|
|
|
if (pref && (pref->flags & PREF_STRING))
|
|
|
|
{
|
|
|
|
if (get_default || PREF_IS_LOCKED(pref) || !PREF_HAS_USER_VALUE(pref))
|
|
|
|
stringVal = pref->defaultPref.stringVal;
|
|
|
|
else
|
|
|
|
stringVal = pref->userPref.stringVal;
|
|
|
|
|
|
|
|
if (stringVal) {
|
2009-03-29 04:45:19 -07:00
|
|
|
*return_buffer = NS_strdup(stringVal);
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult PREF_GetIntPref(const char *pref_name,int32_t * return_int, bool get_default)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!gHashTable.ops)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
nsresult rv = NS_ERROR_UNEXPECTED;
|
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(pref_name);
|
|
|
|
if (pref && (pref->flags & PREF_INT))
|
|
|
|
{
|
|
|
|
if (get_default || PREF_IS_LOCKED(pref) || !PREF_HAS_USER_VALUE(pref))
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t tempInt = pref->defaultPref.intVal;
|
2007-03-22 10:30:00 -07:00
|
|
|
/* check to see if we even had a default */
|
2011-08-22 19:15:52 -07:00
|
|
|
if (!(pref->flags & PREF_HAS_DEFAULT))
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
*return_int = tempInt;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*return_int = pref->userPref.intVal;
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
nsresult PREF_GetBoolPref(const char *pref_name, bool * return_value, bool get_default)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!gHashTable.ops)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
nsresult rv = NS_ERROR_UNEXPECTED;
|
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(pref_name);
|
|
|
|
//NS_ASSERTION(pref, pref_name);
|
|
|
|
if (pref && (pref->flags & PREF_BOOL))
|
|
|
|
{
|
|
|
|
if (get_default || PREF_IS_LOCKED(pref) || !PREF_HAS_USER_VALUE(pref))
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool tempBool = pref->defaultPref.boolVal;
|
2007-03-22 10:30:00 -07:00
|
|
|
/* check to see if we even had a default */
|
2011-08-22 19:15:52 -07:00
|
|
|
if (pref->flags & PREF_HAS_DEFAULT) {
|
2007-03-22 10:30:00 -07:00
|
|
|
*return_value = tempBool;
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*return_value = pref->userPref.boolVal;
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete a branch. Used for deleting mime types */
|
2008-10-10 08:04:34 -07:00
|
|
|
static PLDHashOperator
|
2012-08-22 08:56:38 -07:00
|
|
|
pref_DeleteItem(PLDHashTable *table, PLDHashEntryHdr *heh, uint32_t i, void *arg)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
PrefHashEntry* he = static_cast<PrefHashEntry*>(heh);
|
2007-03-22 10:30:00 -07:00
|
|
|
const char *to_delete = (const char *) arg;
|
2013-05-07 20:40:12 -07:00
|
|
|
int len = strlen(to_delete);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* 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" */
|
2012-08-22 08:56:38 -07:00
|
|
|
if (to_delete && (PL_strncmp(he->key, to_delete, (uint32_t) len) == 0 ||
|
2013-05-07 20:40:12 -07:00
|
|
|
(len-1 == (int)strlen(he->key) && PL_strncmp(he->key, to_delete, (uint32_t)(len-1)) == 0)))
|
2007-03-22 10:30:00 -07:00
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PREF_DeleteBranch(const char *branch_name)
|
|
|
|
{
|
2014-03-05 17:58:01 -08:00
|
|
|
#ifndef MOZ_B2G
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
#endif
|
|
|
|
|
2013-05-07 20:40:12 -07:00
|
|
|
int len = (int)strlen(branch_name);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!gHashTable.ops)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
/* The following check insures that if the branch name already has a "."
|
|
|
|
* at the end, we don't end up with a "..". This fixes an incompatibility
|
|
|
|
* between nsIPref, which needs the period added, and nsIPrefBranch which
|
|
|
|
* does not. When nsIPref goes away this function should be fixed to
|
|
|
|
* never add the period at all.
|
|
|
|
*/
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString branch_dot(branch_name);
|
2007-03-22 10:30:00 -07:00
|
|
|
if ((len > 1) && branch_name[len - 1] != '.')
|
|
|
|
branch_dot += '.';
|
|
|
|
|
|
|
|
PL_DHashTableEnumerate(&gHashTable, pref_DeleteItem,
|
|
|
|
(void*) branch_dot.get());
|
2011-10-17 07:59:28 -07:00
|
|
|
gDirty = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PREF_ClearUserPref(const char *pref_name)
|
|
|
|
{
|
|
|
|
if (!gHashTable.ops)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(pref_name);
|
|
|
|
if (pref && PREF_HAS_USER_VALUE(pref))
|
|
|
|
{
|
|
|
|
pref->flags &= ~PREF_USERSET;
|
|
|
|
|
2011-08-22 19:15:52 -07:00
|
|
|
if (!(pref->flags & PREF_HAS_DEFAULT)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_DHashTableOperate(&gHashTable, pref_name, PL_DHASH_REMOVE);
|
|
|
|
}
|
|
|
|
|
2008-12-06 06:44:35 -08:00
|
|
|
pref_DoCallback(pref_name);
|
2011-10-17 07:59:28 -07:00
|
|
|
gDirty = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-04-17 12:53:25 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static PLDHashOperator
|
2012-08-22 08:56:38 -07:00
|
|
|
pref_ClearUserPref(PLDHashTable *table, PLDHashEntryHdr *he, uint32_t,
|
2007-03-22 10:30:00 -07:00
|
|
|
void *arg)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
PrefHashEntry *pref = static_cast<PrefHashEntry*>(he);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
PLDHashOperator nextOp = PL_DHASH_NEXT;
|
|
|
|
|
|
|
|
if (PREF_HAS_USER_VALUE(pref))
|
|
|
|
{
|
|
|
|
pref->flags &= ~PREF_USERSET;
|
|
|
|
|
2011-08-22 19:15:52 -07:00
|
|
|
if (!(pref->flags & PREF_HAS_DEFAULT)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nextOp = PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
2008-12-06 06:44:35 -08:00
|
|
|
pref_DoCallback(pref->key);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return nextOp;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PREF_ClearAllUserPrefs()
|
|
|
|
{
|
2014-03-05 17:58:01 -08:00
|
|
|
#ifndef MOZ_B2G
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!gHashTable.ops)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
PL_DHashTableEnumerate(&gHashTable, pref_ClearUserPref, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
gDirty = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
nsresult PREF_LockPref(const char *key, bool lockit)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!gHashTable.ops)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(key);
|
|
|
|
if (!pref)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
if (lockit) {
|
|
|
|
if (!PREF_IS_LOCKED(pref))
|
|
|
|
{
|
|
|
|
pref->flags |= PREF_LOCKED;
|
2011-10-17 07:59:28 -07:00
|
|
|
gIsAnyPrefLocked = true;
|
2008-12-06 06:44:35 -08:00
|
|
|
pref_DoCallback(key);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (PREF_IS_LOCKED(pref))
|
|
|
|
{
|
|
|
|
pref->flags &= ~PREF_LOCKED;
|
2008-12-06 06:44:35 -08:00
|
|
|
pref_DoCallback(key);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hash table functions
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool pref_ValueChanged(PrefValue oldValue, PrefValue newValue, PrefType type)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool changed = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (type & PREF_STRING)
|
|
|
|
{
|
|
|
|
if (oldValue.stringVal && newValue.stringVal)
|
|
|
|
changed = (strcmp(oldValue.stringVal, newValue.stringVal) != 0);
|
|
|
|
}
|
|
|
|
else if (type & PREF_INT)
|
|
|
|
changed = oldValue.intVal != newValue.intVal;
|
|
|
|
else if (type & PREF_BOOL)
|
|
|
|
changed = oldValue.boolVal != newValue.boolVal;
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2014-04-23 06:48:24 -07:00
|
|
|
/*
|
|
|
|
* 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* existingValue, uint16_t *existingFlags,
|
|
|
|
PrefValue newValue, PrefType newType)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-04-23 06:48:24 -07:00
|
|
|
if ((*existingFlags & PREF_STRING) && existingValue->stringVal) {
|
|
|
|
PL_strfree(existingValue->stringVal);
|
|
|
|
}
|
|
|
|
*existingFlags = (*existingFlags & ~PREF_VALUETYPE_MASK) | newType;
|
|
|
|
if (newType & PREF_STRING) {
|
|
|
|
PR_ASSERT(newValue.stringVal);
|
|
|
|
existingValue->stringVal = newValue.stringVal ? PL_strdup(newValue.stringVal) : nullptr;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*existingValue = newValue;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-28 10:02:37 -07:00
|
|
|
PrefHashEntry* pref_HashTableLookup(const void *key)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-03-05 17:58:01 -08:00
|
|
|
#ifndef MOZ_B2G
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
PrefHashEntry* result =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<PrefHashEntry*>(PL_DHashTableOperate(&gHashTable, key, PL_DHASH_LOOKUP));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (PL_DHASH_ENTRY_IS_FREE(result))
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult pref_HashPref(const char *key, PrefValue value, PrefType type, uint32_t flags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-03-05 17:58:01 -08:00
|
|
|
#ifndef MOZ_B2G
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!gHashTable.ops)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2007-07-08 00:08:04 -07:00
|
|
|
PrefHashEntry* pref = static_cast<PrefHashEntry*>(PL_DHashTableOperate(&gHashTable, key, PL_DHASH_ADD));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!pref)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2014-04-23 06:48:24 -07:00
|
|
|
// new entry, better initialize
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!pref->key) {
|
|
|
|
|
|
|
|
// initialize the pref entry
|
|
|
|
pref->flags = type;
|
|
|
|
pref->key = ArenaStrDup(key, &gPrefNameArena);
|
2008-01-30 17:14:42 -08:00
|
|
|
memset(&pref->defaultPref, 0, sizeof(pref->defaultPref));
|
|
|
|
memset(&pref->userPref, 0, sizeof(pref->userPref));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-04-23 06:48:24 -07:00
|
|
|
else if ((pref->flags & PREF_HAS_DEFAULT) && PREF_TYPE(pref) != type)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-04-23 06:48:24 -07:00
|
|
|
NS_WARNING(nsPrintfCString("Trying to overwrite value of default pref %s with the wrong type!", key).get());
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool valueChanged = false;
|
2012-04-03 17:08:28 -07:00
|
|
|
if (flags & kPrefSetDefault)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!PREF_IS_LOCKED(pref))
|
|
|
|
{ /* ?? change of semantics? */
|
2011-08-22 19:15:52 -07:00
|
|
|
if (pref_ValueChanged(pref->defaultPref, value, type) ||
|
|
|
|
!(pref->flags & PREF_HAS_DEFAULT))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-04-23 06:48:24 -07:00
|
|
|
pref_SetValue(&pref->defaultPref, &pref->flags, value, type);
|
2011-08-22 19:15:52 -07:00
|
|
|
pref->flags |= PREF_HAS_DEFAULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!PREF_HAS_USER_VALUE(pref))
|
2011-10-17 07:59:28 -07:00
|
|
|
valueChanged = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-09-04 06:52:28 -07:00
|
|
|
// What if we change the default to be the same as the user value?
|
|
|
|
// Should we clear the user value?
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If new value is same as the default value, then un-set the user value.
|
|
|
|
Otherwise, set the user value only if it has changed */
|
2014-04-23 06:48:24 -07:00
|
|
|
if ((pref->flags & PREF_HAS_DEFAULT) &&
|
|
|
|
!pref_ValueChanged(pref->defaultPref, value, type) &&
|
2012-04-03 17:08:28 -07:00
|
|
|
!(flags & kPrefForceSet))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (PREF_HAS_USER_VALUE(pref))
|
|
|
|
{
|
2014-04-23 06:48:24 -07:00
|
|
|
/* XXX should we free a user-set string value if there is one? */
|
2007-03-22 10:30:00 -07:00
|
|
|
pref->flags &= ~PREF_USERSET;
|
2014-09-04 06:52:28 -07:00
|
|
|
if (!PREF_IS_LOCKED(pref)) {
|
|
|
|
gDirty = true;
|
2011-10-17 07:59:28 -07:00
|
|
|
valueChanged = true;
|
2014-09-04 06:52:28 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2014-04-23 06:48:24 -07:00
|
|
|
else if (!PREF_HAS_USER_VALUE(pref) ||
|
|
|
|
PREF_TYPE(pref) != type ||
|
|
|
|
pref_ValueChanged(pref->userPref, value, type) )
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-04-23 06:48:24 -07:00
|
|
|
pref_SetValue(&pref->userPref, &pref->flags, value, type);
|
2007-03-22 10:30:00 -07:00
|
|
|
pref->flags |= PREF_USERSET;
|
2014-09-04 06:52:28 -07:00
|
|
|
if (!PREF_IS_LOCKED(pref)) {
|
|
|
|
gDirty = true;
|
2011-10-17 07:59:28 -07:00
|
|
|
valueChanged = true;
|
2014-09-04 06:52:28 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (valueChanged) {
|
2014-09-04 06:52:28 -07:00
|
|
|
return pref_DoCallback(key);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-09-04 06:52:28 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-10-11 08:36:21 -07:00
|
|
|
size_t
|
2013-06-23 05:03:39 -07:00
|
|
|
pref_SizeOfPrivateData(MallocSizeOf aMallocSizeOf)
|
2012-10-11 08:36:21 -07:00
|
|
|
{
|
2013-02-12 08:54:38 -08:00
|
|
|
size_t n = PL_SizeOfArenaPoolExcludingPool(&gPrefNameArena, aMallocSizeOf);
|
2012-10-11 08:36:21 -07:00
|
|
|
for (struct CallbackNode* node = gCallbacks; node; node = node->next) {
|
|
|
|
n += aMallocSizeOf(node);
|
|
|
|
n += aMallocSizeOf(node->domain);
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
PrefType
|
|
|
|
PREF_GetPrefType(const char *pref_name)
|
|
|
|
{
|
|
|
|
if (gHashTable.ops)
|
|
|
|
{
|
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(pref_name);
|
|
|
|
if (pref)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -- */
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
PREF_PrefIsLocked(const char *pref_name)
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool result = false;
|
2010-02-20 05:55:34 -08:00
|
|
|
if (gIsAnyPrefLocked && gHashTable.ops) {
|
2007-03-22 10:30:00 -07:00
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(pref_name);
|
|
|
|
if (pref && PREF_IS_LOCKED(pref))
|
2011-10-17 07:59:28 -07:00
|
|
|
result = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Adds a node to the beginning of the callback list. */
|
|
|
|
void
|
|
|
|
PREF_RegisterCallback(const char *pref_node,
|
|
|
|
PrefChangedFunc callback,
|
|
|
|
void * instance_data)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_PRECONDITION(pref_node, "pref_node must not be nullptr");
|
|
|
|
NS_PRECONDITION(callback, "callback must not be nullptr");
|
2007-11-16 20:25:41 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
struct CallbackNode* node = (struct CallbackNode*) malloc(sizeof(struct CallbackNode));
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
node->domain = PL_strdup(pref_node);
|
|
|
|
node->func = callback;
|
|
|
|
node->data = instance_data;
|
|
|
|
node->next = gCallbacks;
|
|
|
|
gCallbacks = node;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-16 20:25:41 -08:00
|
|
|
/* Removes |node| from gCallbacks list.
|
|
|
|
Returns the node after the deleted one. */
|
|
|
|
struct CallbackNode*
|
|
|
|
pref_RemoveCallbackNode(struct CallbackNode* node,
|
|
|
|
struct CallbackNode* prev_node)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(!prev_node || prev_node->next == node, "invalid params");
|
|
|
|
NS_PRECONDITION(prev_node || gCallbacks == node, "invalid params");
|
|
|
|
|
|
|
|
NS_ASSERTION(!gCallbacksInProgress,
|
2011-10-17 07:59:28 -07:00
|
|
|
"modifying the callback list while gCallbacksInProgress is true");
|
2007-11-16 20:25:41 -08:00
|
|
|
|
|
|
|
struct CallbackNode* next_node = node->next;
|
|
|
|
if (prev_node)
|
|
|
|
prev_node->next = next_node;
|
|
|
|
else
|
|
|
|
gCallbacks = next_node;
|
2009-03-29 04:45:19 -07:00
|
|
|
PL_strfree(node->domain);
|
|
|
|
free(node);
|
2007-11-16 20:25:41 -08:00
|
|
|
return next_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deletes a node from the callback list or marks it for deletion. */
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
PREF_UnregisterCallback(const char *pref_node,
|
|
|
|
PrefChangedFunc callback,
|
|
|
|
void * instance_data)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
struct CallbackNode* node = gCallbacks;
|
2013-10-23 13:36:26 -07:00
|
|
|
struct CallbackNode* prev_node = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-23 13:36:26 -07:00
|
|
|
while (node != nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-31 10:17:49 -07:00
|
|
|
if ( node->func == callback &&
|
|
|
|
node->data == instance_data &&
|
|
|
|
strcmp(node->domain, pref_node) == 0)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-11-16 20:25:41 -08:00
|
|
|
if (gCallbacksInProgress)
|
|
|
|
{
|
|
|
|
// postpone the node removal until after
|
|
|
|
// gCallbacks enumeration is finished.
|
2012-07-30 07:20:58 -07:00
|
|
|
node->func = nullptr;
|
2011-10-17 07:59:28 -07:00
|
|
|
gShouldCleanupDeadNodes = true;
|
2007-11-16 20:25:41 -08:00
|
|
|
prev_node = node;
|
|
|
|
node = node->next;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
else
|
2007-11-16 20:25:41 -08:00
|
|
|
{
|
|
|
|
node = pref_RemoveCallbackNode(node, prev_node);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prev_node = node;
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult pref_DoCallback(const char* changed_pref)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
struct CallbackNode* node;
|
2007-11-16 20:25:41 -08:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool reentered = gCallbacksInProgress;
|
2011-10-17 07:59:28 -07:00
|
|
|
gCallbacksInProgress = true;
|
|
|
|
// Nodes must not be deleted while gCallbacksInProgress is true.
|
2007-11-16 20:25:41 -08:00
|
|
|
// Nodes that need to be deleted are marked for deletion by nulling
|
|
|
|
// out the |func| pointer. We release them at the end of this function
|
|
|
|
// if we haven't reentered.
|
|
|
|
|
2013-10-23 13:36:26 -07:00
|
|
|
for (node = gCallbacks; node != nullptr; node = node->next)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-11-16 20:25:41 -08:00
|
|
|
if ( node->func &&
|
|
|
|
PL_strncmp(changed_pref,
|
|
|
|
node->domain,
|
2013-05-07 20:40:12 -07:00
|
|
|
strlen(node->domain)) == 0 )
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-12-10 15:10:01 -08:00
|
|
|
(*node->func) (changed_pref, node->data);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2007-11-16 20:25:41 -08:00
|
|
|
|
|
|
|
gCallbacksInProgress = reentered;
|
|
|
|
|
|
|
|
if (gShouldCleanupDeadNodes && !gCallbacksInProgress)
|
|
|
|
{
|
2013-10-23 13:36:26 -07:00
|
|
|
struct CallbackNode* prev_node = nullptr;
|
2007-11-16 20:25:41 -08:00
|
|
|
node = gCallbacks;
|
|
|
|
|
2013-10-23 13:36:26 -07:00
|
|
|
while (node != nullptr)
|
2007-11-16 20:25:41 -08:00
|
|
|
{
|
|
|
|
if (!node->func)
|
|
|
|
{
|
|
|
|
node = pref_RemoveCallbackNode(node, prev_node);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prev_node = node;
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
gShouldCleanupDeadNodes = false;
|
2007-11-16 20:25:41 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PREF_ReaderCallback(void *closure,
|
|
|
|
const char *pref,
|
|
|
|
PrefValue value,
|
|
|
|
PrefType type,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isDefault)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-04-03 17:08:28 -07:00
|
|
|
pref_HashPref(pref, value, type, isDefault ? kPrefSetDefault : kPrefForceSet);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|