2012-08-23 00:00:21 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04: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/. */
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2015-03-18 11:34:09 +03:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2012-08-23 00:00:21 +04:00
|
|
|
#include "base/basictypes.h"
|
|
|
|
|
1999-04-22 02:04:35 +04:00
|
|
|
#include "prefapi.h"
|
2002-04-16 22:48:19 +04:00
|
|
|
#include "prefapi_private_data.h"
|
2003-09-16 20:19:00 +04:00
|
|
|
#include "prefread.h"
|
2014-03-06 05:58:01 +04:00
|
|
|
#include "MainThreadUtils.h"
|
2002-04-16 22:48:19 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2002-05-15 22:55:21 +04:00
|
|
|
#include "nsCRT.h"
|
2000-02-28 18:07:12 +03:00
|
|
|
|
2003-04-04 06:19:25 +04:00
|
|
|
#define PL_ARENA_CONST_ALIGN_MASK 3
|
|
|
|
#include "plarena.h"
|
|
|
|
|
2001-02-17 04:55:32 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include "windows.h"
|
|
|
|
#endif /* _WIN32 */
|
1998-03-28 05:44:41 +03:00
|
|
|
|
1998-11-12 01:29:18 +03:00
|
|
|
#include "plstr.h"
|
2015-09-16 06:49:53 +03:00
|
|
|
#include "PLDHashTable.h"
|
1998-11-12 01:29:18 +03:00
|
|
|
#include "plbase64.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
1998-11-12 01:29:18 +03:00
|
|
|
#include "prprf.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2012-08-23 00:00:21 +04:00
|
|
|
#include "mozilla/dom/PContent.h"
|
1999-06-03 22:15:53 +04:00
|
|
|
#include "nsQuickSort.h"
|
2001-12-05 01:01:22 +03:00
|
|
|
#include "nsString.h"
|
2003-01-20 22:08:54 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2001-12-12 01:41:19 +03:00
|
|
|
#include "prlink.h"
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2012-08-23 00:00:21 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static void
|
2001-12-04 04:52:44 +03:00
|
|
|
clearPrefEntry(PLDHashTable *table, PLDHashEntryHdr *entry)
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
PrefHashEntry *pref = static_cast<PrefHashEntry *>(entry);
|
2016-02-22 11:15:00 +03:00
|
|
|
if (pref->prefFlags.IsTypeString())
|
2001-12-04 04:52:44 +03:00
|
|
|
{
|
2009-03-29 15:45:19 +04:00
|
|
|
if (pref->defaultPref.stringVal)
|
|
|
|
PL_strfree(pref->defaultPref.stringVal);
|
|
|
|
if (pref->userPref.stringVal)
|
|
|
|
PL_strfree(pref->userPref.stringVal);
|
2001-12-04 04:52:44 +03:00
|
|
|
}
|
2002-01-05 03:09:53 +03:00
|
|
|
// don't need to free this as it's allocated in memory owned by
|
2003-04-04 06:19:25 +04:00
|
|
|
// gPrefNameArena
|
2012-07-30 18:20:58 +04:00
|
|
|
pref->key = nullptr;
|
2014-08-26 03:56:33 +04:00
|
|
|
memset(entry, 0, table->EntrySize());
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2016-03-16 07:33:44 +03:00
|
|
|
matchPrefEntry(const PLDHashEntryHdr* entry, const void* key)
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
2001-12-04 04:52:44 +03:00
|
|
|
const PrefHashEntry *prefEntry =
|
2007-07-08 11:08:04 +04:00
|
|
|
static_cast<const PrefHashEntry*>(entry);
|
2003-09-23 21:57:22 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
if (prefEntry->key == key) return true;
|
2003-09-23 21:57:22 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
if (!prefEntry->key || !key) return false;
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
const char *otherKey = reinterpret_cast<const char*>(key);
|
2001-12-04 04:52:44 +03:00
|
|
|
return (strcmp(prefEntry->key, otherKey) == 0);
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2015-05-20 02:46:17 +03:00
|
|
|
PLDHashTable* gHashTable;
|
2003-04-04 06:19:25 +04:00
|
|
|
static PLArenaPool gPrefNameArena;
|
2001-12-12 01:41:19 +03:00
|
|
|
|
2013-10-24 00:36:26 +04:00
|
|
|
static struct CallbackNode* gCallbacks = nullptr;
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool gIsAnyPrefLocked = false;
|
2007-11-17 07:25:41 +03:00
|
|
|
// These are only used during the call to pref_DoCallback
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool gCallbacksInProgress = false;
|
|
|
|
static bool gShouldCleanupDeadNodes = false;
|
2001-12-04 04:52:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
static PLDHashTableOps pref_HashTableOps = {
|
2015-09-15 00:23:47 +03:00
|
|
|
PLDHashTable::HashStringKey,
|
2001-12-04 04:52:44 +03:00
|
|
|
matchPrefEntry,
|
2015-09-15 00:23:47 +03:00
|
|
|
PLDHashTable::MoveEntryStub,
|
2001-12-04 04:52:44 +03:00
|
|
|
clearPrefEntry,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr,
|
2001-12-04 04:52:44 +03:00
|
|
|
};
|
2003-09-23 21:57:22 +04:00
|
|
|
|
2002-01-05 18:53:51 +03: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
|
2002-01-05 19:40:21 +03:00
|
|
|
#define PR_ALIGN_OF_WORD PR_ALIGN_OF_POINTER
|
2002-01-05 11:29:55 +03:00
|
|
|
#endif
|
|
|
|
|
2003-04-04 06:19:25 +04:00
|
|
|
// making PrefName arena 8k for nice allocation
|
|
|
|
#define PREFNAME_ARENA_SIZE 8192
|
2002-01-05 11:29:55 +03:00
|
|
|
|
2002-01-05 18:53:51 +03:00
|
|
|
#define WORD_ALIGN_MASK (PR_ALIGN_OF_WORD - 1)
|
2002-01-05 03:09:53 +03:00
|
|
|
|
|
|
|
// sanity checking
|
2002-01-05 18:53:51 +03:00
|
|
|
#if (PR_ALIGN_OF_WORD & WORD_ALIGN_MASK) != 0
|
|
|
|
#error "PR_ALIGN_OF_WORD must be a power of 2!"
|
2002-01-05 03:09:53 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// equivalent to strdup() - does no error checking,
|
|
|
|
// we're assuming we're only called with a valid pointer
|
2003-04-04 06:19:25 +04:00
|
|
|
static char *ArenaStrDup(const char* str, PLArenaPool* aArena)
|
2002-01-05 03:09:53 +03:00
|
|
|
{
|
2003-04-04 06:19:25 +04:00
|
|
|
void* mem;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len = strlen(str);
|
2003-04-04 06:19:25 +04:00
|
|
|
PL_ARENA_ALLOCATE(mem, aArena, len+1);
|
|
|
|
if (mem)
|
|
|
|
memcpy(mem, str, len+1);
|
2007-07-08 11:08:04 +04:00
|
|
|
return static_cast<char*>(mem);
|
2002-01-05 03:09:53 +03:00
|
|
|
}
|
|
|
|
|
2016-07-21 22:46:15 +03:00
|
|
|
static PrefsDirtyFunc gDirtyCallback = nullptr;
|
|
|
|
|
|
|
|
inline void MakeDirtyCallback()
|
|
|
|
{
|
|
|
|
// Right now the callback function is always set, so we don't need
|
|
|
|
// to complicate the code to cover the scenario where we set the callback
|
|
|
|
// after we've already tried to make it dirty. If this assert triggers
|
|
|
|
// we will add that code.
|
|
|
|
MOZ_ASSERT(gDirtyCallback);
|
|
|
|
if (gDirtyCallback) {
|
|
|
|
gDirtyCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PREF_SetDirtyCallback(PrefsDirtyFunc aFunc)
|
|
|
|
{
|
|
|
|
gDirtyCallback = aFunc;
|
|
|
|
}
|
|
|
|
|
2002-01-05 03:09:53 +03:00
|
|
|
/*---------------------------------------------------------------------------*/
|
1999-04-22 02:04:35 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool pref_ValueChanged(PrefValue oldValue, PrefValue newValue, PrefType type);
|
1998-03-28 05:44:41 +03:00
|
|
|
/* -- Privates */
|
|
|
|
struct CallbackNode {
|
2000-08-31 06:59:59 +04:00
|
|
|
char* domain;
|
2007-11-17 07:25:41 +03:00
|
|
|
// If someone attempts to remove the node from the callback list while
|
2012-07-30 18:20:58 +04:00
|
|
|
// pref_DoCallback is running, |func| is set to nullptr. Such nodes will
|
2007-11-17 07:25:41 +03:00
|
|
|
// be removed at the end of pref_DoCallback.
|
2000-08-31 06:59:59 +04:00
|
|
|
PrefChangedFunc func;
|
|
|
|
void* data;
|
|
|
|
struct CallbackNode* next;
|
1998-03-28 05:44:41 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/* -- Prototypes */
|
2003-09-23 21:57:22 +04:00
|
|
|
static nsresult pref_DoCallback(const char* changed_pref);
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2012-04-04 04:08:28 +04:00
|
|
|
enum {
|
|
|
|
kPrefSetDefault = 1,
|
2015-04-30 10:13:40 +03:00
|
|
|
kPrefForceSet = 2,
|
|
|
|
kPrefStickyDefault = 4,
|
2012-04-04 04:08:28 +04:00
|
|
|
};
|
2012-08-22 19:56:38 +04:00
|
|
|
static nsresult pref_HashPref(const char *key, PrefValue value, PrefType type, uint32_t flags);
|
2001-12-12 01:41:19 +03:00
|
|
|
|
2014-08-06 17:31:21 +04:00
|
|
|
#define PREF_HASHTABLE_INITIAL_LENGTH 1024
|
2003-09-23 21:57:22 +04:00
|
|
|
|
2015-06-05 02:14:48 +03:00
|
|
|
void PREF_Init()
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
2015-05-18 10:52:08 +03:00
|
|
|
if (!gHashTable) {
|
2015-05-20 02:46:17 +03:00
|
|
|
gHashTable = new PLDHashTable(&pref_HashTableOps,
|
|
|
|
sizeof(PrefHashEntry),
|
|
|
|
PREF_HASHTABLE_INITIAL_LENGTH);
|
2015-02-11 01:39:49 +03:00
|
|
|
|
2003-04-04 06:19:25 +04:00
|
|
|
PL_INIT_ARENA_POOL(&gPrefNameArena, "PrefNameArena",
|
|
|
|
PREFNAME_ARENA_SIZE);
|
2001-12-04 04:52:44 +03:00
|
|
|
}
|
2003-09-16 20:19:00 +04:00
|
|
|
}
|
1998-03-28 05:44:41 +03:00
|
|
|
|
|
|
|
/* Frees the callback list. */
|
2000-05-09 09:58:29 +04:00
|
|
|
void PREF_Cleanup()
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
2007-11-17 07:25:41 +03:00
|
|
|
NS_ASSERTION(!gCallbacksInProgress,
|
2011-10-17 18:59:28 +04:00
|
|
|
"PREF_Cleanup was called while gCallbacksInProgress is true!");
|
2000-08-31 06:59:59 +04:00
|
|
|
struct CallbackNode* node = gCallbacks;
|
|
|
|
struct CallbackNode* next_node;
|
2003-09-23 21:57:22 +04:00
|
|
|
|
2000-08-31 06:59:59 +04:00
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
next_node = node->next;
|
2009-03-29 15:45:19 +04:00
|
|
|
PL_strfree(node->domain);
|
|
|
|
free(node);
|
2000-08-31 06:59:59 +04:00
|
|
|
node = next_node;
|
|
|
|
}
|
2013-10-24 00:36:26 +04:00
|
|
|
gCallbacks = nullptr;
|
1999-04-22 02:17:30 +04:00
|
|
|
|
1999-09-21 00:52:48 +04:00
|
|
|
PREF_CleanupPrefs();
|
1999-08-28 09:19:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Frees up all the objects except the callback list. */
|
2000-05-09 09:58:29 +04:00
|
|
|
void PREF_CleanupPrefs()
|
1999-08-28 09:19:29 +04:00
|
|
|
{
|
2015-05-18 10:52:08 +03:00
|
|
|
if (gHashTable) {
|
|
|
|
delete gHashTable;
|
|
|
|
gHashTable = nullptr;
|
2003-04-04 06:19:25 +04:00
|
|
|
PL_FinishArenaPool(&gPrefNameArena);
|
2001-12-04 04:52:44 +03:00
|
|
|
}
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2001-12-05 01:01:22 +03:00
|
|
|
// note that this appends to aResult, and does not assign!
|
2001-12-12 01:41:19 +03:00
|
|
|
static void str_escape(const char * original, nsAFlatCString& aResult)
|
1999-05-04 23:12:37 +04:00
|
|
|
{
|
2002-07-16 06:09:34 +04:00
|
|
|
/* 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
|
|
|
|
*/
|
2000-08-31 06:59:59 +04:00
|
|
|
const char *p;
|
2002-07-16 06:09:34 +04:00
|
|
|
|
2013-10-24 00:36:26 +04:00
|
|
|
if (original == nullptr)
|
2001-12-05 01:01:22 +03:00
|
|
|
return;
|
|
|
|
|
2000-08-31 06:59:59 +04:00
|
|
|
/* Paranoid worst case all slashes will free quickly */
|
2002-07-16 06:09:34 +04:00
|
|
|
for (p=original; *p; ++p)
|
2000-08-31 06:59:59 +04:00
|
|
|
{
|
1999-05-04 23:12:37 +04:00
|
|
|
switch (*p)
|
|
|
|
{
|
2002-07-16 06:09:34 +04:00
|
|
|
case '\n':
|
2014-05-22 07:48:51 +04:00
|
|
|
aResult.AppendLiteral("\\n");
|
2002-07-16 06:09:34 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '\r':
|
2014-05-22 07:48:51 +04:00
|
|
|
aResult.AppendLiteral("\\r");
|
2002-07-16 06:09:34 +04:00
|
|
|
break;
|
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
case '\\':
|
2014-05-22 07:48:51 +04:00
|
|
|
aResult.AppendLiteral("\\\\");
|
2002-07-16 06:09:34 +04:00
|
|
|
break;
|
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
case '\"':
|
2014-05-22 07:48:51 +04:00
|
|
|
aResult.AppendLiteral("\\\"");
|
1999-03-10 23:02:22 +03:00
|
|
|
break;
|
2002-07-16 06:09:34 +04:00
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
default:
|
2002-07-16 06:09:34 +04:00
|
|
|
aResult.Append(*p);
|
1998-03-28 05:44:41 +03:00
|
|
|
break;
|
|
|
|
}
|
1999-05-04 23:12:37 +04:00
|
|
|
}
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-01-13 05:32:09 +03:00
|
|
|
** External calls
|
|
|
|
*/
|
2003-09-23 21:57:22 +04:00
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
PREF_SetCharPref(const char *pref_name, const char *value, bool set_default)
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
2014-01-22 00:51:05 +04:00
|
|
|
if ((uint32_t)strlen(value) > MAX_PREF_LENGTH) {
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
2000-08-31 06:59:59 +04:00
|
|
|
PrefValue pref;
|
2014-01-22 00:51:05 +04:00
|
|
|
pref.stringVal = (char*)value;
|
2003-09-23 21:57:22 +04:00
|
|
|
|
2016-02-22 11:15:00 +03:00
|
|
|
return pref_HashPref(pref_name, pref, PrefType::String, set_default ? kPrefSetDefault : 0);
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2003-09-23 21:57:22 +04:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
PREF_SetIntPref(const char *pref_name, int32_t value, bool set_default)
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
2000-08-31 06:59:59 +04:00
|
|
|
PrefValue pref;
|
|
|
|
pref.intVal = value;
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2016-02-22 11:15:00 +03:00
|
|
|
return pref_HashPref(pref_name, pref, PrefType::Int, set_default ? kPrefSetDefault : 0);
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2003-09-23 21:57:22 +04:00
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
PREF_SetBoolPref(const char *pref_name, bool value, bool set_default)
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
2000-08-31 06:59:59 +04:00
|
|
|
PrefValue pref;
|
2011-08-23 06:15:52 +04:00
|
|
|
pref.boolVal = value;
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2016-02-22 11:15:00 +03:00
|
|
|
return pref_HashPref(pref_name, pref, PrefType::Bool, set_default ? kPrefSetDefault : 0);
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2012-08-23 00:00:21 +04:00
|
|
|
enum WhichValue { DEFAULT_VALUE, USER_VALUE };
|
|
|
|
static nsresult
|
|
|
|
SetPrefValue(const char* aPrefName, const dom::PrefValue& aValue,
|
|
|
|
WhichValue aWhich)
|
2010-10-20 00:35:08 +04:00
|
|
|
{
|
2012-08-23 00:00:21 +04: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-29 05:38:30 +04:00
|
|
|
MOZ_CRASH();
|
2012-08-23 00:00:21 +04:00
|
|
|
}
|
|
|
|
}
|
2010-10-20 00:35:08 +04:00
|
|
|
|
2012-08-23 00:00:21 +04: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-20 00:35:08 +04:00
|
|
|
|
2012-08-23 00:00:21 +04:00
|
|
|
if (userValue.type() == dom::MaybePrefValue::TPrefValue) {
|
|
|
|
rv = SetPrefValue(prefName, userValue.get_PrefValue(), USER_VALUE);
|
|
|
|
} else {
|
2014-09-04 17:52:28 +04:00
|
|
|
rv = PREF_ClearUserPref(prefName);
|
2010-10-20 00:35:08 +04:00
|
|
|
}
|
|
|
|
|
2012-08-23 00:00:21 +04:00
|
|
|
// NB: we should never try to clear a default value, that doesn't
|
|
|
|
// make sense
|
|
|
|
|
|
|
|
return rv;
|
2010-10-20 00:35:08 +04:00
|
|
|
}
|
|
|
|
|
2015-12-06 17:06:02 +03:00
|
|
|
UniquePtr<char*[]>
|
2016-07-19 16:10:00 +03:00
|
|
|
pref_savePrefs(PLDHashTable* aTable, uint32_t* aPrefCount)
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
2016-07-19 16:10:00 +03:00
|
|
|
// This function allocates the entries in the savedPrefs array it returns.
|
|
|
|
// It is the callers responsibility to go through the array and free
|
|
|
|
// all of them. The aPrefCount entries will be non-null. Any end padding
|
|
|
|
// is an implementation detail and may change.
|
|
|
|
MOZ_ASSERT(aPrefCount);
|
2015-12-06 17:06:02 +03:00
|
|
|
auto savedPrefs = MakeUnique<char*[]>(aTable->EntryCount());
|
2016-07-19 16:10:00 +03:00
|
|
|
|
|
|
|
// This is not necessary, but leaving it in for now
|
2015-12-06 17:06:02 +03:00
|
|
|
memset(savedPrefs.get(), 0, aTable->EntryCount() * sizeof(char*));
|
|
|
|
|
2015-06-19 03:58:24 +03:00
|
|
|
int32_t j = 0;
|
|
|
|
for (auto iter = aTable->Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
auto pref = static_cast<PrefHashEntry*>(iter.Get());
|
|
|
|
|
|
|
|
nsAutoCString prefValue;
|
|
|
|
nsAutoCString prefPrefix;
|
|
|
|
prefPrefix.AssignLiteral("user_pref(\"");
|
|
|
|
|
|
|
|
// where we're getting our pref from
|
|
|
|
PrefValue* sourcePref;
|
|
|
|
|
2016-02-22 11:15:00 +03:00
|
|
|
if (pref->prefFlags.HasUserValue() &&
|
2015-06-19 03:58:24 +03:00
|
|
|
(pref_ValueChanged(pref->defaultPref,
|
|
|
|
pref->userPref,
|
2016-02-22 11:15:00 +03:00
|
|
|
pref->prefFlags.GetPrefType()) ||
|
|
|
|
!(pref->prefFlags.HasDefault()) ||
|
|
|
|
pref->prefFlags.HasStickyDefault())) {
|
2015-06-19 03:58:24 +03:00
|
|
|
sourcePref = &pref->userPref;
|
|
|
|
} else {
|
|
|
|
// do not save default prefs that haven't changed
|
|
|
|
continue;
|
|
|
|
}
|
2001-12-04 18:44:38 +03:00
|
|
|
|
2015-06-19 03:58:24 +03:00
|
|
|
// strings are in quotes!
|
2016-02-22 11:15:00 +03:00
|
|
|
if (pref->prefFlags.IsTypeString()) {
|
2015-06-19 03:58:24 +03:00
|
|
|
prefValue = '\"';
|
|
|
|
str_escape(sourcePref->stringVal, prefValue);
|
|
|
|
prefValue += '\"';
|
2001-12-05 01:01:22 +03:00
|
|
|
|
2016-02-22 11:15:00 +03:00
|
|
|
} else if (pref->prefFlags.IsTypeInt()) {
|
2015-06-19 03:58:24 +03:00
|
|
|
prefValue.AppendInt(sourcePref->intVal);
|
2010-09-28 01:23:35 +04:00
|
|
|
|
2016-02-22 11:15:00 +03:00
|
|
|
} else if (pref->prefFlags.IsTypeBool()) {
|
2015-06-19 03:58:24 +03:00
|
|
|
prefValue = (sourcePref->boolVal) ? "true" : "false";
|
|
|
|
}
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2015-06-19 03:58:24 +03:00
|
|
|
nsAutoCString prefName;
|
|
|
|
str_escape(pref->key, prefName);
|
2010-10-20 00:35:08 +04:00
|
|
|
|
2015-12-06 17:06:02 +03:00
|
|
|
savedPrefs[j++] = ToNewCString(prefPrefix +
|
2015-06-19 03:58:24 +03:00
|
|
|
prefName +
|
|
|
|
NS_LITERAL_CSTRING("\", ") +
|
|
|
|
prefValue +
|
|
|
|
NS_LITERAL_CSTRING(");"));
|
2010-10-20 00:35:08 +04:00
|
|
|
}
|
2016-07-19 16:10:00 +03:00
|
|
|
*aPrefCount = j;
|
2015-12-06 17:06:02 +03:00
|
|
|
|
|
|
|
return savedPrefs;
|
2010-10-20 00:35:08 +04:00
|
|
|
}
|
|
|
|
|
2016-05-17 17:55:49 +03:00
|
|
|
bool
|
|
|
|
pref_EntryHasAdvisablySizedValues(PrefHashEntry* aHashEntry)
|
|
|
|
{
|
|
|
|
if (aHashEntry->prefFlags.GetPrefType() != PrefType::String) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* stringVal;
|
|
|
|
if (aHashEntry->prefFlags.HasDefault()) {
|
|
|
|
stringVal = aHashEntry->defaultPref.stringVal;
|
|
|
|
if (strlen(stringVal) > MAX_ADVISABLE_PREF_LENGTH) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aHashEntry->prefFlags.HasUserValue()) {
|
|
|
|
stringVal = aHashEntry->userPref.stringVal;
|
|
|
|
if (strlen(stringVal) > MAX_ADVISABLE_PREF_LENGTH) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-23 00:00:21 +04:00
|
|
|
static void
|
|
|
|
GetPrefValueFromEntry(PrefHashEntry *aHashEntry, dom::PrefSetting* aPref,
|
|
|
|
WhichValue aWhich)
|
2010-10-20 00:35:08 +04:00
|
|
|
{
|
2012-08-23 00:00:21 +04: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-20 00:35:08 +04:00
|
|
|
|
2016-02-22 11:15:00 +03:00
|
|
|
switch (aHashEntry->prefFlags.GetPrefType()) {
|
|
|
|
case PrefType::String:
|
2012-08-23 00:00:21 +04:00
|
|
|
*settingValue = nsDependentCString(value->stringVal);
|
|
|
|
return;
|
2016-02-22 11:15:00 +03:00
|
|
|
case PrefType::Int:
|
2012-08-23 00:00:21 +04:00
|
|
|
*settingValue = value->intVal;
|
|
|
|
return;
|
2016-02-22 11:15:00 +03:00
|
|
|
case PrefType::Bool:
|
2012-08-23 00:00:21 +04:00
|
|
|
*settingValue = !!value->boolVal;
|
|
|
|
return;
|
|
|
|
default:
|
2013-06-29 05:38:30 +04:00
|
|
|
MOZ_CRASH();
|
2012-08-23 00:00:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pref_GetPrefFromEntry(PrefHashEntry *aHashEntry, dom::PrefSetting* aPref)
|
|
|
|
{
|
|
|
|
aPref->name() = aHashEntry->key;
|
2016-02-22 11:15:00 +03:00
|
|
|
if (aHashEntry->prefFlags.HasDefault()) {
|
2012-08-23 00:00:21 +04:00
|
|
|
GetPrefValueFromEntry(aHashEntry, aPref, DEFAULT_VALUE);
|
|
|
|
} else {
|
|
|
|
aPref->defaultValue() = null_t();
|
2010-10-20 00:35:08 +04:00
|
|
|
}
|
2016-02-22 11:15:00 +03:00
|
|
|
if (aHashEntry->prefFlags.HasUserValue()) {
|
2012-08-23 00:00:21 +04:00
|
|
|
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-20 00:35:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
int
|
1999-06-03 22:15:53 +04:00
|
|
|
pref_CompareStrings(const void *v1, const void *v2, void *unused)
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
2000-08-31 06:59:59 +04:00
|
|
|
char *s1 = *(char**) v1;
|
|
|
|
char *s2 = *(char**) v2;
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2000-08-31 06:59:59 +04:00
|
|
|
if (!s1)
|
|
|
|
{
|
|
|
|
if (!s2)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (!s2)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return strcmp(s1, s2);
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool PREF_HasUserPref(const char *pref_name)
|
2000-05-05 04:22:20 +04:00
|
|
|
{
|
2015-05-18 10:52:08 +03:00
|
|
|
if (!gHashTable)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2000-05-05 04:22:20 +04:00
|
|
|
|
2003-09-23 21:57:22 +04:00
|
|
|
PrefHashEntry *pref = pref_HashTableLookup(pref_name);
|
2016-02-22 11:15:00 +03:00
|
|
|
return pref && pref->prefFlags.HasUserValue();
|
2000-05-05 04:22:20 +04:00
|
|
|
}
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2003-09-23 21:57:22 +04:00
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
PREF_CopyCharPref(const char *pref_name, char ** return_buffer, bool get_default)
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
2015-05-18 10:52:08 +03:00
|
|
|
if (!gHashTable)
|
2003-09-23 21:57:22 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2003-09-23 21:57:22 +04:00
|
|
|
nsresult rv = NS_ERROR_UNEXPECTED;
|
|
|
|
char* stringVal;
|
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(pref_name);
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2016-02-22 11:15:00 +03:00
|
|
|
if (pref && (pref->prefFlags.IsTypeString())) {
|
|
|
|
if (get_default || pref->prefFlags.IsLocked() || !pref->prefFlags.HasUserValue()) {
|
2000-08-31 06:59:59 +04:00
|
|
|
stringVal = pref->defaultPref.stringVal;
|
2016-02-22 11:15:00 +03:00
|
|
|
} else {
|
2000-08-31 06:59:59 +04:00
|
|
|
stringVal = pref->userPref.stringVal;
|
2016-02-22 11:15:00 +03:00
|
|
|
}
|
2003-09-23 21:57:22 +04:00
|
|
|
|
2000-08-31 06:59:59 +04:00
|
|
|
if (stringVal) {
|
2009-03-29 15:45:19 +04:00
|
|
|
*return_buffer = NS_strdup(stringVal);
|
2003-09-23 21:57:22 +04:00
|
|
|
rv = NS_OK;
|
2000-08-31 06:59:59 +04:00
|
|
|
}
|
|
|
|
}
|
2003-09-23 21:57:22 +04:00
|
|
|
return rv;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult PREF_GetIntPref(const char *pref_name,int32_t * return_int, bool get_default)
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
2015-05-18 10:52:08 +03:00
|
|
|
if (!gHashTable)
|
2003-09-23 21:57:22 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2003-09-23 21:57:22 +04:00
|
|
|
nsresult rv = NS_ERROR_UNEXPECTED;
|
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(pref_name);
|
2016-02-22 11:15:00 +03:00
|
|
|
if (pref && (pref->prefFlags.IsTypeInt())) {
|
|
|
|
if (get_default || pref->prefFlags.IsLocked() || !pref->prefFlags.HasUserValue()) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t tempInt = pref->defaultPref.intVal;
|
2001-08-30 02:22:52 +04:00
|
|
|
/* check to see if we even had a default */
|
2016-02-22 11:15:00 +03:00
|
|
|
if (!pref->prefFlags.HasDefault()) {
|
2003-09-23 21:57:22 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2016-02-22 11:15:00 +03:00
|
|
|
}
|
2001-08-30 02:22:52 +04:00
|
|
|
*return_int = tempInt;
|
2016-02-22 11:15:00 +03:00
|
|
|
} else {
|
2016-02-22 20:05:21 +03:00
|
|
|
*return_int = pref->userPref.intVal;
|
2016-02-22 11:15:00 +03:00
|
|
|
}
|
2003-09-23 21:57:22 +04:00
|
|
|
rv = NS_OK;
|
2000-08-31 06:59:59 +04:00
|
|
|
}
|
2003-09-23 21:57:22 +04:00
|
|
|
return rv;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
nsresult PREF_GetBoolPref(const char *pref_name, bool * return_value, bool get_default)
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
2015-05-18 10:52:08 +03:00
|
|
|
if (!gHashTable)
|
2003-09-23 21:57:22 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2003-09-23 21:57:22 +04:00
|
|
|
nsresult rv = NS_ERROR_UNEXPECTED;
|
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(pref_name);
|
2001-12-04 04:52:44 +03:00
|
|
|
//NS_ASSERTION(pref, pref_name);
|
2016-02-22 11:15:00 +03:00
|
|
|
if (pref && (pref->prefFlags.IsTypeBool())) {
|
|
|
|
if (get_default || pref->prefFlags.IsLocked() || !pref->prefFlags.HasUserValue()) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool tempBool = pref->defaultPref.boolVal;
|
2001-08-30 02:22:52 +04:00
|
|
|
/* check to see if we even had a default */
|
2016-02-22 11:15:00 +03:00
|
|
|
if (pref->prefFlags.HasDefault()) {
|
2003-09-23 21:57:22 +04:00
|
|
|
*return_value = tempBool;
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
2016-02-22 11:15:00 +03:00
|
|
|
} else {
|
2000-08-31 06:59:59 +04:00
|
|
|
*return_value = pref->userPref.boolVal;
|
2003-09-23 21:57:22 +04:00
|
|
|
rv = NS_OK;
|
|
|
|
}
|
2000-08-31 06:59:59 +04:00
|
|
|
}
|
2003-09-23 21:57:22 +04:00
|
|
|
return rv;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2003-09-23 21:57:22 +04:00
|
|
|
nsresult
|
1998-03-28 05:44:41 +03:00
|
|
|
PREF_DeleteBranch(const char *branch_name)
|
|
|
|
{
|
2014-03-06 05:58:01 +04:00
|
|
|
#ifndef MOZ_B2G
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
#endif
|
|
|
|
|
2013-05-08 07:40:12 +04:00
|
|
|
int len = (int)strlen(branch_name);
|
2001-06-09 00:35:01 +04:00
|
|
|
|
2015-05-18 10:52:08 +03:00
|
|
|
if (!gHashTable)
|
2003-09-23 21:57:22 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2001-06-09 00:35:01 +04:00
|
|
|
/* 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
|
2003-09-23 21:57:22 +04:00
|
|
|
* does not. When nsIPref goes away this function should be fixed to
|
2001-06-09 00:35:01 +04:00
|
|
|
* never add the period at all.
|
|
|
|
*/
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString branch_dot(branch_name);
|
2001-12-05 01:01:22 +03:00
|
|
|
if ((len > 1) && branch_name[len - 1] != '.')
|
|
|
|
branch_dot += '.';
|
2001-06-09 00:35:01 +04:00
|
|
|
|
2015-06-19 03:58:24 +03:00
|
|
|
/* Delete a branch. Used for deleting mime types */
|
|
|
|
const char *to_delete = branch_dot.get();
|
|
|
|
MOZ_ASSERT(to_delete);
|
|
|
|
len = strlen(to_delete);
|
2015-07-07 08:02:26 +03:00
|
|
|
for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) {
|
2015-06-19 03:58:24 +03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 22:46:15 +03:00
|
|
|
MakeDirtyCallback();
|
2003-09-23 21:57:22 +04:00
|
|
|
return NS_OK;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-23 21:57:22 +04:00
|
|
|
nsresult
|
1998-03-28 05:44:41 +03:00
|
|
|
PREF_ClearUserPref(const char *pref_name)
|
|
|
|
{
|
2015-05-18 10:52:08 +03:00
|
|
|
if (!gHashTable)
|
2003-09-23 21:57:22 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2000-08-31 06:59:59 +04:00
|
|
|
|
2003-09-23 21:57:22 +04:00
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(pref_name);
|
2016-02-22 11:15:00 +03:00
|
|
|
if (pref && pref->prefFlags.HasUserValue()) {
|
|
|
|
pref->prefFlags.SetHasUserValue(false);
|
2006-06-12 04:47:26 +04:00
|
|
|
|
2016-02-22 11:15:00 +03:00
|
|
|
if (!pref->prefFlags.HasDefault()) {
|
2015-09-08 05:21:03 +03:00
|
|
|
gHashTable->RemoveEntry(pref);
|
2006-06-12 04:47:26 +04:00
|
|
|
}
|
|
|
|
|
2008-12-06 17:44:35 +03:00
|
|
|
pref_DoCallback(pref_name);
|
2016-07-21 22:46:15 +03:00
|
|
|
MakeDirtyCallback();
|
2000-08-31 06:59:59 +04:00
|
|
|
}
|
2011-04-17 23:53:25 +04:00
|
|
|
return NS_OK;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2003-09-23 21:57:22 +04:00
|
|
|
nsresult
|
2001-01-10 23:25:52 +03:00
|
|
|
PREF_ClearAllUserPrefs()
|
|
|
|
{
|
2014-03-06 05:58:01 +04:00
|
|
|
#ifndef MOZ_B2G
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
#endif
|
|
|
|
|
2015-05-18 10:52:08 +03:00
|
|
|
if (!gHashTable)
|
2003-09-23 21:57:22 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2015-03-18 11:34:09 +03:00
|
|
|
std::vector<std::string> prefStrings;
|
2015-07-07 08:02:26 +03:00
|
|
|
for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) {
|
2015-06-19 03:58:24 +03:00
|
|
|
auto pref = static_cast<PrefHashEntry*>(iter.Get());
|
|
|
|
|
2016-02-22 11:15:00 +03:00
|
|
|
if (pref->prefFlags.HasUserValue()) {
|
2015-06-19 03:58:24 +03:00
|
|
|
prefStrings.push_back(std::string(pref->key));
|
|
|
|
|
2016-02-22 11:15:00 +03:00
|
|
|
pref->prefFlags.SetHasUserValue(false);
|
|
|
|
if (!pref->prefFlags.HasDefault()) {
|
2015-06-19 03:58:24 +03:00
|
|
|
iter.Remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-18 11:34:09 +03:00
|
|
|
|
|
|
|
for (std::string& prefString : prefStrings) {
|
|
|
|
pref_DoCallback(prefString.c_str());
|
|
|
|
}
|
2003-01-20 22:08:54 +03:00
|
|
|
|
2016-07-21 22:46:15 +03:00
|
|
|
MakeDirtyCallback();
|
2003-09-23 21:57:22 +04:00
|
|
|
return NS_OK;
|
2001-01-10 23:25:52 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
nsresult PREF_LockPref(const char *key, bool lockit)
|
1999-01-13 05:32:09 +03:00
|
|
|
{
|
2015-05-18 10:52:08 +03:00
|
|
|
if (!gHashTable)
|
2003-09-23 21:57:22 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
1999-01-13 05:32:09 +03:00
|
|
|
|
2003-09-23 21:57:22 +04:00
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(key);
|
2000-08-31 06:59:59 +04:00
|
|
|
if (!pref)
|
2003-09-23 21:57:22 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
1999-01-13 05:32:09 +03:00
|
|
|
|
2003-09-23 21:57:22 +04:00
|
|
|
if (lockit) {
|
2016-02-22 11:15:00 +03:00
|
|
|
if (!pref->prefFlags.IsLocked()) {
|
|
|
|
pref->prefFlags.SetLocked(true);
|
2011-10-17 18:59:28 +04:00
|
|
|
gIsAnyPrefLocked = true;
|
2008-12-06 17:44:35 +03:00
|
|
|
pref_DoCallback(key);
|
2003-09-23 21:57:22 +04:00
|
|
|
}
|
2016-02-22 11:15:00 +03:00
|
|
|
} else {
|
|
|
|
if (pref->prefFlags.IsLocked()) {
|
|
|
|
pref->prefFlags.SetLocked(false);
|
2008-12-06 17:44:35 +03:00
|
|
|
pref_DoCallback(key);
|
2003-09-23 21:57:22 +04:00
|
|
|
}
|
1999-01-13 05:32:09 +03:00
|
|
|
}
|
2003-09-23 21:57:22 +04:00
|
|
|
return NS_OK;
|
1999-01-13 05:32:09 +03:00
|
|
|
}
|
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
/*
|
|
|
|
* Hash table functions
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool pref_ValueChanged(PrefValue oldValue, PrefValue newValue, PrefType type)
|
1999-01-13 05:32:09 +03:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool changed = true;
|
2016-02-22 11:15:00 +03:00
|
|
|
switch(type) {
|
|
|
|
case PrefType::String:
|
|
|
|
if (oldValue.stringVal && newValue.stringVal) {
|
2000-08-31 06:59:59 +04:00
|
|
|
changed = (strcmp(oldValue.stringVal, newValue.stringVal) != 0);
|
2016-02-22 11:15:00 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PrefType::Int:
|
2000-08-31 06:59:59 +04:00
|
|
|
changed = oldValue.intVal != newValue.intVal;
|
2016-02-22 11:15:00 +03:00
|
|
|
break;
|
|
|
|
case PrefType::Bool:
|
2000-08-31 06:59:59 +04:00
|
|
|
changed = oldValue.boolVal != newValue.boolVal;
|
2016-02-22 11:15:00 +03:00
|
|
|
break;
|
|
|
|
case PrefType::Invalid:
|
|
|
|
default:
|
|
|
|
changed = false;
|
|
|
|
break;
|
|
|
|
}
|
2000-08-31 06:59:59 +04:00
|
|
|
return changed;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2014-04-23 17:48:24 +04: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.
|
|
|
|
*/
|
2016-02-22 11:15:00 +03:00
|
|
|
static PrefTypeFlags pref_SetValue(PrefValue* existingValue, PrefTypeFlags flags,
|
|
|
|
PrefValue newValue, PrefType newType)
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
2016-02-22 11:15:00 +03:00
|
|
|
if (flags.IsTypeString() && existingValue->stringVal) {
|
2014-04-23 17:48:24 +04:00
|
|
|
PL_strfree(existingValue->stringVal);
|
|
|
|
}
|
2016-02-22 11:15:00 +03:00
|
|
|
flags.SetPrefType(newType);
|
|
|
|
if (flags.IsTypeString()) {
|
2016-06-28 20:47:22 +03:00
|
|
|
MOZ_ASSERT(newValue.stringVal);
|
2014-04-23 17:48:24 +04:00
|
|
|
existingValue->stringVal = newValue.stringVal ? PL_strdup(newValue.stringVal) : nullptr;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*existingValue = newValue;
|
2000-08-31 06:59:59 +04:00
|
|
|
}
|
2016-02-22 11:15:00 +03:00
|
|
|
return flags;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2015-07-31 23:06:12 +03:00
|
|
|
PrefHashEntry* pref_HashTableLookup(const char *key)
|
2001-11-01 18:41:53 +03:00
|
|
|
{
|
2014-03-06 05:58:01 +04:00
|
|
|
#ifndef MOZ_B2G
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
#endif
|
|
|
|
|
2015-05-21 10:34:25 +03:00
|
|
|
return static_cast<PrefHashEntry*>(gHashTable->Search(key));
|
2001-11-01 18:41:53 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult pref_HashPref(const char *key, PrefValue value, PrefType type, uint32_t flags)
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
2014-03-06 05:58:01 +04:00
|
|
|
#ifndef MOZ_B2G
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
#endif
|
|
|
|
|
2015-05-18 10:52:08 +03:00
|
|
|
if (!gHashTable)
|
2003-09-23 21:57:22 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2015-09-15 00:23:12 +03:00
|
|
|
auto pref = static_cast<PrefHashEntry*>(gHashTable->Add(key, fallible));
|
2000-08-31 06:59:59 +04:00
|
|
|
if (!pref)
|
2003-09-23 21:57:22 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2001-12-04 04:52:44 +03:00
|
|
|
|
2014-04-23 17:48:24 +04:00
|
|
|
// new entry, better initialize
|
2001-12-04 04:52:44 +03:00
|
|
|
if (!pref->key) {
|
2003-09-23 21:57:22 +04:00
|
|
|
|
2001-12-04 04:52:44 +03:00
|
|
|
// initialize the pref entry
|
2016-02-22 11:15:00 +03:00
|
|
|
pref->prefFlags.Reset().SetPrefType(type);
|
2003-04-04 06:19:25 +04:00
|
|
|
pref->key = ArenaStrDup(key, &gPrefNameArena);
|
2008-01-31 04:14:42 +03:00
|
|
|
memset(&pref->defaultPref, 0, sizeof(pref->defaultPref));
|
|
|
|
memset(&pref->userPref, 0, sizeof(pref->userPref));
|
2016-02-22 11:15:00 +03:00
|
|
|
} else if (pref->prefFlags.HasDefault() && !pref->prefFlags.IsPrefType(type)) {
|
2014-04-23 17:48:24 +04:00
|
|
|
NS_WARNING(nsPrintfCString("Trying to overwrite value of default pref %s with the wrong type!", key).get());
|
2003-09-23 21:57:22 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
2000-08-19 02:32:05 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool valueChanged = false;
|
2016-02-22 11:15:00 +03:00
|
|
|
if (flags & kPrefSetDefault) {
|
|
|
|
if (!pref->prefFlags.IsLocked()) {
|
|
|
|
/* ?? change of semantics? */
|
2011-08-23 06:15:52 +04:00
|
|
|
if (pref_ValueChanged(pref->defaultPref, value, type) ||
|
2016-02-22 11:15:00 +03:00
|
|
|
!pref->prefFlags.HasDefault()) {
|
|
|
|
pref->prefFlags = pref_SetValue(&pref->defaultPref, pref->prefFlags, value, type).SetHasDefault(true);
|
|
|
|
if (flags & kPrefStickyDefault) {
|
|
|
|
pref->prefFlags.SetHasStickyDefault(true);
|
|
|
|
}
|
|
|
|
if (!pref->prefFlags.HasUserValue()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
valueChanged = true;
|
2016-02-22 11:15:00 +03:00
|
|
|
}
|
2000-08-31 06:59:59 +04:00
|
|
|
}
|
2014-09-04 17:52:28 +04:00
|
|
|
// What if we change the default to be the same as the user value?
|
|
|
|
// Should we clear the user value?
|
2003-09-23 21:57:22 +04:00
|
|
|
}
|
2016-02-22 11:15:00 +03:00
|
|
|
} else {
|
2015-04-30 10:13:40 +03:00
|
|
|
/* If new value is same as the default value and it's not a "sticky"
|
|
|
|
pref, then un-set the user value.
|
2003-09-23 21:57:22 +04:00
|
|
|
Otherwise, set the user value only if it has changed */
|
2016-02-22 11:15:00 +03:00
|
|
|
if ((pref->prefFlags.HasDefault()) &&
|
|
|
|
!(pref->prefFlags.HasStickyDefault()) &&
|
2014-04-23 17:48:24 +04:00
|
|
|
!pref_ValueChanged(pref->defaultPref, value, type) &&
|
2016-02-22 11:15:00 +03:00
|
|
|
!(flags & kPrefForceSet)) {
|
|
|
|
if (pref->prefFlags.HasUserValue()) {
|
2014-04-23 17:48:24 +04:00
|
|
|
/* XXX should we free a user-set string value if there is one? */
|
2016-02-22 11:15:00 +03:00
|
|
|
pref->prefFlags.SetHasUserValue(false);
|
|
|
|
if (!pref->prefFlags.IsLocked()) {
|
2016-07-21 22:46:15 +03:00
|
|
|
MakeDirtyCallback();
|
2011-10-17 18:59:28 +04:00
|
|
|
valueChanged = true;
|
2014-09-04 17:52:28 +04:00
|
|
|
}
|
2000-08-31 06:59:59 +04:00
|
|
|
}
|
2016-02-22 11:15:00 +03:00
|
|
|
} else if (!pref->prefFlags.HasUserValue() ||
|
|
|
|
!pref->prefFlags.IsPrefType(type) ||
|
|
|
|
pref_ValueChanged(pref->userPref, value, type) ) {
|
|
|
|
pref->prefFlags = pref_SetValue(&pref->userPref, pref->prefFlags, value, type).SetHasUserValue(true);
|
|
|
|
if (!pref->prefFlags.IsLocked()) {
|
2016-07-21 22:46:15 +03:00
|
|
|
MakeDirtyCallback();
|
2011-10-17 18:59:28 +04:00
|
|
|
valueChanged = true;
|
2014-09-04 17:52:28 +04:00
|
|
|
}
|
2003-09-23 21:57:22 +04:00
|
|
|
}
|
2000-08-31 06:59:59 +04:00
|
|
|
}
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2003-09-23 21:57:22 +04:00
|
|
|
if (valueChanged) {
|
2014-09-04 17:52:28 +04:00
|
|
|
return pref_DoCallback(key);
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
2014-09-04 17:52:28 +04:00
|
|
|
return NS_OK;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2012-10-11 19:36:21 +04:00
|
|
|
size_t
|
2013-06-23 16:03:39 +04:00
|
|
|
pref_SizeOfPrivateData(MallocSizeOf aMallocSizeOf)
|
2012-10-11 19:36:21 +04:00
|
|
|
{
|
2013-02-12 20:54:38 +04:00
|
|
|
size_t n = PL_SizeOfArenaPoolExcludingPool(&gPrefNameArena, aMallocSizeOf);
|
2012-10-11 19:36:21 +04:00
|
|
|
for (struct CallbackNode* node = gCallbacks; node; node = node->next) {
|
|
|
|
n += aMallocSizeOf(node);
|
|
|
|
n += aMallocSizeOf(node->domain);
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2000-05-09 09:58:29 +04:00
|
|
|
PrefType
|
1998-03-28 05:44:41 +03:00
|
|
|
PREF_GetPrefType(const char *pref_name)
|
|
|
|
{
|
2015-05-18 10:52:08 +03:00
|
|
|
if (gHashTable) {
|
2001-12-04 04:52:44 +03:00
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(pref_name);
|
2016-02-22 11:15:00 +03:00
|
|
|
if (pref) {
|
|
|
|
return pref->prefFlags.GetPrefType();
|
2000-08-31 06:59:59 +04:00
|
|
|
}
|
|
|
|
}
|
2016-02-22 11:15:00 +03:00
|
|
|
return PrefType::Invalid;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -- */
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
1998-03-28 05:44:41 +03:00
|
|
|
PREF_PrefIsLocked(const char *pref_name)
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool result = false;
|
2015-05-18 10:52:08 +03:00
|
|
|
if (gIsAnyPrefLocked && gHashTable) {
|
2001-12-04 04:52:44 +03:00
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(pref_name);
|
2016-02-22 11:15:00 +03:00
|
|
|
if (pref && pref->prefFlags.IsLocked()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
result = true;
|
2016-02-22 11:15:00 +03:00
|
|
|
}
|
2000-08-31 06:59:59 +04:00
|
|
|
}
|
2003-09-23 21:57:22 +04:00
|
|
|
|
2000-08-31 06:59:59 +04:00
|
|
|
return result;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Adds a node to the beginning of the callback list. */
|
2000-05-09 09:58:29 +04:00
|
|
|
void
|
1998-03-28 05:44:41 +03:00
|
|
|
PREF_RegisterCallback(const char *pref_node,
|
2000-08-31 06:59:59 +04:00
|
|
|
PrefChangedFunc callback,
|
|
|
|
void * instance_data)
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_PRECONDITION(pref_node, "pref_node must not be nullptr");
|
|
|
|
NS_PRECONDITION(callback, "callback must not be nullptr");
|
2007-11-17 07:25:41 +03:00
|
|
|
|
2000-08-31 06:59:59 +04: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;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2007-11-17 07:25:41 +03: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 18:59:28 +04:00
|
|
|
"modifying the callback list while gCallbacksInProgress is true");
|
2007-11-17 07:25:41 +03:00
|
|
|
|
|
|
|
struct CallbackNode* next_node = node->next;
|
|
|
|
if (prev_node)
|
|
|
|
prev_node->next = next_node;
|
|
|
|
else
|
|
|
|
gCallbacks = next_node;
|
2009-03-29 15:45:19 +04:00
|
|
|
PL_strfree(node->domain);
|
|
|
|
free(node);
|
2007-11-17 07:25:41 +03:00
|
|
|
return next_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deletes a node from the callback list or marks it for deletion. */
|
2003-09-23 21:57:22 +04:00
|
|
|
nsresult
|
1998-03-28 05:44:41 +03:00
|
|
|
PREF_UnregisterCallback(const char *pref_node,
|
2000-08-31 06:59:59 +04:00
|
|
|
PrefChangedFunc callback,
|
|
|
|
void * instance_data)
|
|
|
|
{
|
2003-09-23 21:57:22 +04:00
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
2000-08-31 06:59:59 +04:00
|
|
|
struct CallbackNode* node = gCallbacks;
|
2013-10-24 00:36:26 +04:00
|
|
|
struct CallbackNode* prev_node = nullptr;
|
2003-09-23 21:57:22 +04:00
|
|
|
|
2013-10-24 00:36:26 +04:00
|
|
|
while (node != nullptr)
|
2000-08-31 06:59:59 +04:00
|
|
|
{
|
2010-07-31 21:17:49 +04:00
|
|
|
if ( node->func == callback &&
|
|
|
|
node->data == instance_data &&
|
|
|
|
strcmp(node->domain, pref_node) == 0)
|
2000-08-31 06:59:59 +04:00
|
|
|
{
|
2007-11-17 07:25:41 +03:00
|
|
|
if (gCallbacksInProgress)
|
|
|
|
{
|
|
|
|
// postpone the node removal until after
|
|
|
|
// gCallbacks enumeration is finished.
|
2012-07-30 18:20:58 +04:00
|
|
|
node->func = nullptr;
|
2011-10-17 18:59:28 +04:00
|
|
|
gShouldCleanupDeadNodes = true;
|
2007-11-17 07:25:41 +03:00
|
|
|
prev_node = node;
|
|
|
|
node = node->next;
|
|
|
|
}
|
2000-08-31 06:59:59 +04:00
|
|
|
else
|
2007-11-17 07:25:41 +03:00
|
|
|
{
|
|
|
|
node = pref_RemoveCallbackNode(node, prev_node);
|
|
|
|
}
|
2003-09-23 21:57:22 +04:00
|
|
|
rv = NS_OK;
|
2000-08-31 06:59:59 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prev_node = node;
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
}
|
2003-09-23 21:57:22 +04:00
|
|
|
return rv;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2003-09-23 21:57:22 +04:00
|
|
|
static nsresult pref_DoCallback(const char* changed_pref)
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
2003-09-23 21:57:22 +04:00
|
|
|
nsresult rv = NS_OK;
|
2000-08-31 06:59:59 +04:00
|
|
|
struct CallbackNode* node;
|
2007-11-17 07:25:41 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool reentered = gCallbacksInProgress;
|
2011-10-17 18:59:28 +04:00
|
|
|
gCallbacksInProgress = true;
|
|
|
|
// Nodes must not be deleted while gCallbacksInProgress is true.
|
2007-11-17 07:25:41 +03: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-24 00:36:26 +04:00
|
|
|
for (node = gCallbacks; node != nullptr; node = node->next)
|
2000-08-31 06:59:59 +04:00
|
|
|
{
|
2007-11-17 07:25:41 +03:00
|
|
|
if ( node->func &&
|
|
|
|
PL_strncmp(changed_pref,
|
|
|
|
node->domain,
|
2013-05-08 07:40:12 +04:00
|
|
|
strlen(node->domain)) == 0 )
|
2000-08-31 06:59:59 +04:00
|
|
|
{
|
2013-12-11 03:10:01 +04:00
|
|
|
(*node->func) (changed_pref, node->data);
|
2000-08-31 06:59:59 +04:00
|
|
|
}
|
|
|
|
}
|
2007-11-17 07:25:41 +03:00
|
|
|
|
|
|
|
gCallbacksInProgress = reentered;
|
|
|
|
|
|
|
|
if (gShouldCleanupDeadNodes && !gCallbacksInProgress)
|
|
|
|
{
|
2013-10-24 00:36:26 +04:00
|
|
|
struct CallbackNode* prev_node = nullptr;
|
2007-11-17 07:25:41 +03:00
|
|
|
node = gCallbacks;
|
|
|
|
|
2013-10-24 00:36:26 +04:00
|
|
|
while (node != nullptr)
|
2007-11-17 07:25:41 +03:00
|
|
|
{
|
|
|
|
if (!node->func)
|
|
|
|
{
|
|
|
|
node = pref_RemoveCallbackNode(node, prev_node);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prev_node = node;
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
gShouldCleanupDeadNodes = false;
|
2007-11-17 07:25:41 +03:00
|
|
|
}
|
|
|
|
|
2003-09-23 21:57:22 +04:00
|
|
|
return rv;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2003-09-23 21:57:22 +04:00
|
|
|
void PREF_ReaderCallback(void *closure,
|
|
|
|
const char *pref,
|
|
|
|
PrefValue value,
|
|
|
|
PrefType type,
|
2015-04-30 10:13:40 +03:00
|
|
|
bool isDefault,
|
|
|
|
bool isStickyDefault)
|
2016-02-22 11:15:00 +03:00
|
|
|
|
2000-08-31 06:59:59 +04:00
|
|
|
{
|
2016-02-22 11:15:00 +03:00
|
|
|
uint32_t flags = 0;
|
|
|
|
if (isDefault) {
|
|
|
|
flags |= kPrefSetDefault;
|
|
|
|
if (isStickyDefault) {
|
|
|
|
flags |= kPrefStickyDefault;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
flags |= kPrefForceSet;
|
2015-04-30 10:13:40 +03:00
|
|
|
}
|
|
|
|
pref_HashPref(pref, value, type, flags);
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|