2013-01-18 00:45:11 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00: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/. */
|
2001-03-22 23:34:41 +00:00
|
|
|
|
2017-10-11 09:31:35 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-10-11 10:02:03 +00:00
|
|
|
#include "base/basictypes.h"
|
2017-10-05 23:06:47 +00:00
|
|
|
#include "GeckoProfiler.h"
|
2017-10-11 10:02:03 +00:00
|
|
|
#include "MainThreadUtils.h"
|
|
|
|
#include "mozilla/ArenaAllocatorExtensions.h"
|
|
|
|
#include "mozilla/ArenaAllocator.h"
|
2013-12-09 02:52:54 +00:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2012-06-06 02:10:17 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2017-10-11 10:02:03 +00:00
|
|
|
#include "mozilla/dom/ContentPrefs.h"
|
2017-10-05 23:06:47 +00:00
|
|
|
#include "mozilla/dom/PContent.h"
|
2012-03-12 22:53:18 +00:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2017-10-11 10:02:03 +00:00
|
|
|
#include "mozilla/Logging.h"
|
2017-10-05 23:06:47 +00:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2017-10-11 08:03:23 +00:00
|
|
|
#include "mozilla/ModuleUtils.h"
|
2017-10-05 23:06:47 +00:00
|
|
|
#include "mozilla/Omnijar.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
2017-09-01 00:02:09 +00:00
|
|
|
#include "mozilla/ResultExtensions.h"
|
|
|
|
#include "mozilla/ScopeExit.h"
|
2017-10-11 09:31:32 +00:00
|
|
|
#include "mozilla/Services.h"
|
2017-04-11 13:39:32 +00:00
|
|
|
#include "mozilla/ServoStyleSet.h"
|
2017-08-08 21:14:28 +00:00
|
|
|
#include "mozilla/SyncRunnable.h"
|
2017-06-21 19:26:10 +00:00
|
|
|
#include "mozilla/Telemetry.h"
|
2015-12-06 14:06:02 +00:00
|
|
|
#include "mozilla/UniquePtrExtensions.h"
|
2017-10-05 23:06:47 +00:00
|
|
|
#include "mozilla/URLPreloader.h"
|
2017-10-11 09:31:32 +00:00
|
|
|
#include "mozilla/Variant.h"
|
2017-11-14 08:06:01 +00:00
|
|
|
#include "mozilla/Vector.h"
|
2001-10-08 19:52:17 +00:00
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
2017-10-05 23:06:47 +00:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCategoryManagerUtils.h"
|
2017-10-11 10:02:03 +00:00
|
|
|
#include "nsClassHashtable.h"
|
2017-10-05 23:06:47 +00:00
|
|
|
#include "nsCOMArray.h"
|
2017-10-11 09:31:32 +00:00
|
|
|
#include "nsCOMPtr.h"
|
2017-10-05 23:06:47 +00:00
|
|
|
#include "nsCRT.h"
|
2013-10-10 12:48:03 +00:00
|
|
|
#include "nsDataHashtable.h"
|
2001-10-08 19:52:17 +00:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
2001-12-21 22:29:31 +00:00
|
|
|
#include "nsICategoryManager.h"
|
2017-10-11 09:31:32 +00:00
|
|
|
#include "nsIConsoleService.h"
|
|
|
|
#include "nsIDirectoryService.h"
|
2002-04-16 18:48:19 +00:00
|
|
|
#include "nsIFile.h"
|
2010-09-09 03:38:12 +00:00
|
|
|
#include "nsIInputStream.h"
|
2017-10-05 23:06:47 +00:00
|
|
|
#include "nsIMemoryReporter.h"
|
2017-10-11 09:31:32 +00:00
|
|
|
#include "nsIObserver.h"
|
2001-03-22 23:34:41 +00:00
|
|
|
#include "nsIObserverService.h"
|
2015-07-07 02:17:00 +00:00
|
|
|
#include "nsIOutputStream.h"
|
2017-10-11 09:31:32 +00:00
|
|
|
#include "nsIPrefBranch.h"
|
|
|
|
#include "nsIPrefLocalizedString.h"
|
|
|
|
#include "nsIRelativeFilePref.h"
|
2015-07-07 02:17:00 +00:00
|
|
|
#include "nsISafeOutputStream.h"
|
|
|
|
#include "nsISimpleEnumerator.h"
|
2017-10-11 09:31:32 +00:00
|
|
|
#include "nsIStringBundle.h"
|
2010-09-09 03:38:12 +00:00
|
|
|
#include "nsIStringEnumerator.h"
|
2017-10-11 09:31:32 +00:00
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
2010-09-09 03:38:12 +00:00
|
|
|
#include "nsIZipReader.h"
|
2017-10-05 23:06:47 +00:00
|
|
|
#include "nsNetUtil.h"
|
2013-09-11 18:20:22 +00:00
|
|
|
#include "nsPrintfCString.h"
|
2001-03-22 23:34:41 +00:00
|
|
|
#include "nsQuickSort.h"
|
2017-10-11 09:31:32 +00:00
|
|
|
#include "nsReadableUtils.h"
|
2017-10-05 23:06:47 +00:00
|
|
|
#include "nsRefPtrHashtable.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2017-10-11 09:31:35 +00:00
|
|
|
#include "nsUTF8Utils.h"
|
2017-10-11 09:31:32 +00:00
|
|
|
#include "nsWeakReference.h"
|
2017-10-05 23:06:47 +00:00
|
|
|
#include "nsXPCOMCID.h"
|
2017-10-11 09:31:32 +00:00
|
|
|
#include "nsXPCOM.h"
|
2017-10-05 23:06:47 +00:00
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
#include "nsZipArchive.h"
|
2017-10-11 10:02:03 +00:00
|
|
|
#include "plbase64.h"
|
2015-09-16 03:49:53 +00:00
|
|
|
#include "PLDHashTable.h"
|
2017-10-11 10:02:03 +00:00
|
|
|
#include "plstr.h"
|
|
|
|
#include "prlink.h"
|
|
|
|
|
|
|
|
#ifdef MOZ_CRASHREPORTER
|
|
|
|
#include "nsICrashReporter.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#include "windows.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
#define ENSURE_MAIN_PROCESS(func, pref) \
|
|
|
|
do { \
|
|
|
|
if (MOZ_UNLIKELY(!XRE_IsParentProcess())) { \
|
|
|
|
nsPrintfCString msg( \
|
|
|
|
"ENSURE_MAIN_PROCESS: called %s on %s in a non-main process", \
|
2017-10-24 23:22:37 +00:00
|
|
|
func, \
|
|
|
|
pref); \
|
2017-10-11 10:02:03 +00:00
|
|
|
NS_ERROR(msg.get()); \
|
|
|
|
return NS_ERROR_NOT_AVAILABLE; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#else // DEBUG
|
|
|
|
|
|
|
|
#define ENSURE_MAIN_PROCESS(func, pref) \
|
|
|
|
if (MOZ_UNLIKELY(!XRE_IsParentProcess())) { \
|
|
|
|
return NS_ERROR_NOT_AVAILABLE; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
// The old low-level prefs API
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
struct PrefHashEntry;
|
|
|
|
|
2017-10-26 05:48:11 +00:00
|
|
|
typedef nsTArray<nsCString> PrefSaveData;
|
2017-10-11 10:02:03 +00:00
|
|
|
|
2017-10-13 04:24:59 +00:00
|
|
|
static PrefHashEntry*
|
2017-10-11 10:02:03 +00:00
|
|
|
pref_HashTableLookup(const char* aKey);
|
|
|
|
|
|
|
|
// 1 MB should be enough for everyone.
|
|
|
|
static const uint32_t MAX_PREF_LENGTH = 1 * 1024 * 1024;
|
|
|
|
// Actually, 4kb should be enough for everyone.
|
|
|
|
static const uint32_t MAX_ADVISABLE_PREF_LENGTH = 4 * 1024;
|
|
|
|
|
2017-10-13 04:24:58 +00:00
|
|
|
union PrefValue {
|
2017-10-26 05:27:09 +00:00
|
|
|
const char* mStringVal;
|
2017-10-11 10:02:03 +00:00
|
|
|
int32_t mIntVal;
|
|
|
|
bool mBoolVal;
|
2017-10-13 04:24:58 +00:00
|
|
|
};
|
2017-10-11 10:02:03 +00:00
|
|
|
|
|
|
|
// Preference flags, including the native type of the preference. Changing any
|
|
|
|
// of these values will require modifying the code inside of PrefTypeFlags
|
|
|
|
// class.
|
|
|
|
enum class PrefType
|
|
|
|
{
|
|
|
|
Invalid = 0,
|
|
|
|
String = 1,
|
|
|
|
Int = 2,
|
|
|
|
Bool = 3,
|
|
|
|
};
|
|
|
|
|
2017-10-11 18:10:43 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
const char*
|
|
|
|
PrefTypeToString(PrefType aType)
|
|
|
|
{
|
|
|
|
switch (aType) {
|
|
|
|
case PrefType::Invalid:
|
|
|
|
return "INVALID";
|
|
|
|
case PrefType::String:
|
|
|
|
return "string";
|
|
|
|
case PrefType::Int:
|
|
|
|
return "int";
|
|
|
|
case PrefType::Bool:
|
|
|
|
return "bool";
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unhandled enum value");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-10-11 10:02:03 +00:00
|
|
|
// Keep the type of the preference, as well as the flags guiding its behaviour.
|
|
|
|
class PrefTypeFlags
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PrefTypeFlags()
|
|
|
|
: mValue(AsInt(PrefType::Invalid))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit PrefTypeFlags(PrefType aType)
|
|
|
|
: mValue(AsInt(aType))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PrefTypeFlags& Reset()
|
|
|
|
{
|
|
|
|
mValue = AsInt(PrefType::Invalid);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsTypeValid() const { return !IsPrefType(PrefType::Invalid); }
|
|
|
|
bool IsTypeString() const { return IsPrefType(PrefType::String); }
|
|
|
|
bool IsTypeInt() const { return IsPrefType(PrefType::Int); }
|
|
|
|
bool IsTypeBool() const { return IsPrefType(PrefType::Bool); }
|
|
|
|
bool IsPrefType(PrefType type) const { return GetPrefType() == type; }
|
|
|
|
|
2017-10-26 05:36:17 +00:00
|
|
|
void SetPrefType(PrefType aType)
|
2017-10-11 10:02:03 +00:00
|
|
|
{
|
|
|
|
mValue = mValue - AsInt(GetPrefType()) + AsInt(aType);
|
|
|
|
}
|
|
|
|
|
|
|
|
PrefType GetPrefType() const
|
|
|
|
{
|
|
|
|
return (PrefType)(mValue & (AsInt(PrefType::String) | AsInt(PrefType::Int) |
|
|
|
|
AsInt(PrefType::Bool)));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasDefault() const { return mValue & PREF_FLAG_HAS_DEFAULT; }
|
|
|
|
|
2017-10-26 05:36:17 +00:00
|
|
|
void SetHasDefault(bool aSetOrUnset)
|
2017-10-11 10:02:03 +00:00
|
|
|
{
|
2017-10-26 05:36:17 +00:00
|
|
|
SetFlag(PREF_FLAG_HAS_DEFAULT, aSetOrUnset);
|
2017-10-11 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HasStickyDefault() const { return mValue & PREF_FLAG_STICKY_DEFAULT; }
|
|
|
|
|
2017-10-26 05:36:17 +00:00
|
|
|
void SetHasStickyDefault(bool aSetOrUnset)
|
2017-10-11 10:02:03 +00:00
|
|
|
{
|
2017-10-26 05:36:17 +00:00
|
|
|
SetFlag(PREF_FLAG_STICKY_DEFAULT, aSetOrUnset);
|
2017-10-11 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsLocked() const { return mValue & PREF_FLAG_LOCKED; }
|
|
|
|
|
2017-10-26 05:36:17 +00:00
|
|
|
void SetLocked(bool aSetOrUnset) { SetFlag(PREF_FLAG_LOCKED, aSetOrUnset); }
|
2017-10-11 10:02:03 +00:00
|
|
|
|
|
|
|
bool HasUserValue() const { return mValue & PREF_FLAG_USERSET; }
|
|
|
|
|
2017-10-26 05:36:17 +00:00
|
|
|
void SetHasUserValue(bool aSetOrUnset)
|
2017-10-11 10:02:03 +00:00
|
|
|
{
|
2017-10-26 05:36:17 +00:00
|
|
|
SetFlag(PREF_FLAG_USERSET, aSetOrUnset);
|
2017-10-11 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static uint16_t AsInt(PrefType aType) { return (uint16_t)aType; }
|
|
|
|
|
2017-10-26 05:36:17 +00:00
|
|
|
void SetFlag(uint16_t aFlag, bool aSetOrUnset)
|
2017-10-11 10:02:03 +00:00
|
|
|
{
|
|
|
|
mValue = aSetOrUnset ? mValue | aFlag : mValue & ~aFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We pack both the value of type (PrefType) and flags into the same int. The
|
|
|
|
// flag enum starts at 4 so that the PrefType can occupy the bottom two bits.
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PREF_FLAG_LOCKED = 4,
|
|
|
|
PREF_FLAG_USERSET = 8,
|
2017-10-24 23:22:37 +00:00
|
|
|
PREF_FLAG_HAS_DEFAULT = 16,
|
|
|
|
PREF_FLAG_STICKY_DEFAULT = 32,
|
2017-10-11 10:02:03 +00:00
|
|
|
};
|
|
|
|
uint16_t mValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PrefHashEntry : PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
PrefTypeFlags mPrefFlags; // this field first to minimize 64-bit struct size
|
|
|
|
const char* mKey;
|
|
|
|
PrefValue mDefaultPref;
|
|
|
|
PrefValue mUserPref;
|
2017-11-16 04:55:58 +00:00
|
|
|
|
|
|
|
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf)
|
|
|
|
{
|
|
|
|
// Note: mKey is allocated in gPrefNameArena, measured elsewhere.
|
|
|
|
size_t n = 0;
|
|
|
|
if (mPrefFlags.IsTypeString()) {
|
|
|
|
if (mPrefFlags.HasDefault()) {
|
|
|
|
n += aMallocSizeOf(mDefaultPref.mStringVal);
|
|
|
|
}
|
|
|
|
if (mPrefFlags.HasUserValue()) {
|
|
|
|
n += aMallocSizeOf(mUserPref.mStringVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
2017-10-11 10:02:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
ClearPrefEntry(PLDHashTable* aTable, PLDHashEntryHdr* aEntry)
|
|
|
|
{
|
|
|
|
auto pref = static_cast<PrefHashEntry*>(aEntry);
|
|
|
|
if (pref->mPrefFlags.IsTypeString()) {
|
2017-10-26 05:27:09 +00:00
|
|
|
free(const_cast<char*>(pref->mDefaultPref.mStringVal));
|
|
|
|
free(const_cast<char*>(pref->mUserPref.mStringVal));
|
2017-10-11 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't need to free this because it's allocated in memory owned by
|
|
|
|
// gPrefNameArena.
|
|
|
|
pref->mKey = nullptr;
|
|
|
|
memset(aEntry, 0, aTable->EntrySize());
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
MatchPrefEntry(const PLDHashEntryHdr* aEntry, const void* aKey)
|
|
|
|
{
|
|
|
|
auto prefEntry = static_cast<const PrefHashEntry*>(aEntry);
|
|
|
|
|
|
|
|
if (prefEntry->mKey == aKey) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!prefEntry->mKey || !aKey) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto otherKey = static_cast<const char*>(aKey);
|
|
|
|
return (strcmp(prefEntry->mKey, otherKey) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CallbackNode
|
|
|
|
{
|
2017-10-26 05:27:12 +00:00
|
|
|
const char* mDomain;
|
2017-10-11 10:02:03 +00:00
|
|
|
|
|
|
|
// If someone attempts to remove the node from the callback list while
|
|
|
|
// pref_DoCallback is running, |func| is set to nullptr. Such nodes will
|
|
|
|
// be removed at the end of pref_DoCallback.
|
|
|
|
PrefChangedFunc mFunc;
|
|
|
|
void* mData;
|
Bug 1414188 - Remove ValueObserver. r=glandium.
libpref has callbacks. You can register a callback function against a
particular pref (or pref prefix), and it'll be called when any matching pref
changes.
This is implemented in two layers. The lower layer is the list of CallbackNode
objects, pointed to by gFirstCallback.
The upper layer involves gObserverTable, which is a hash table of ValueObserver
objects. It is used for callbacks registered via
Preferences::RegisterCallback() and Preferences::Add*VarCache(), but not for
observers registered with Preferences::Add{Weak,Strong}Observer(). If multiple
callbacks with identical prefnames, callback functions and MatchKinds occur,
they are commoned up into a single ValueObserver, which is then wrapped by a
single CallbackNode. (The callbacks within a single ValueObserver can have
different void* aClosure args; ValueObserver keeps those in an nsTArray.)
Note also that gObserverTable is very inelegant, with duplication of data
between the keys and the values due it being a
nsRefPtrHashtable<ValueObserverHashKey, ValueObserver> and ValueObserver being
a subclass of ValueObserverHashKey(!)
This extra layer might make sense if there were a lot of commoning up
happening, but there's not. Across all process kinds there is an average of
between 1.2 and 1.7 closures per ValueObserver. The vast majority have 1
closure, and there are a handful that have multiple; the highest number I've
seen is 71.
(Note that this two-layer design probably seemed more natural back when libpref
was spread across multiple files.)
This patch removes the ValueObserver layer. The only tricky part is that there
is a notion of a MatchKind -- ExactMatch or PrefixMatch -- which exists in the
ValueObserverLayer but not in the CallbackNode layer. So the patch moves the
MatchKind into the CallbackNode layer, which is straightforward.
On Linux64 this reduces memory usage by about 40 KiB in the parent process and
about 30 KiB in each content processes.
The performance implications are minor.
- The list of CallbackNodes is a bit longer, so when a pref is changed we must
check more nodes. But we avoid possibly doing prefname matching twice.
- Callback registration is much faster, just a simple list prepend, without any
hash table insertion required.
- Callback unregistration is probably not much different. There is a longer
list to traverse, but no hash table removal.
- Pref lookup is by far the hottest libpref operation, and it's unchanged.
For perf to be genuinely affected would require (a) a *lot* more
almost-duplicate callbacks that would have been commoned up, and (b) frequent
changing of the pref those callbacks are observing. This seems highly unlikely.
MozReview-Commit-ID: 6xo4xmytOf3
--HG--
extra : rebase_source : cf9884a8a40b2cc3b00ab165ae5ab4ec3383f72d
2017-11-02 09:38:34 +00:00
|
|
|
Preferences::MatchKind mMatchKind;
|
2017-10-11 10:02:03 +00:00
|
|
|
CallbackNode* mNext;
|
|
|
|
};
|
|
|
|
|
2017-10-13 04:24:59 +00:00
|
|
|
static PLDHashTable* gHashTable;
|
2017-10-11 10:02:03 +00:00
|
|
|
|
|
|
|
static ArenaAllocator<8192, 4> gPrefNameArena;
|
|
|
|
|
2017-10-13 04:24:59 +00:00
|
|
|
// The callback list contains all the priority callbacks followed by the
|
|
|
|
// non-priority callbacks. gLastPriorityNode records where the first part ends.
|
2017-10-11 10:02:03 +00:00
|
|
|
static CallbackNode* gFirstCallback = nullptr;
|
|
|
|
static CallbackNode* gLastPriorityNode = nullptr;
|
|
|
|
|
|
|
|
static bool gIsAnyPrefLocked = false;
|
|
|
|
|
|
|
|
// These are only used during the call to pref_DoCallback.
|
|
|
|
static bool gCallbacksInProgress = false;
|
|
|
|
static bool gShouldCleanupDeadNodes = false;
|
|
|
|
|
|
|
|
static PLDHashTableOps pref_HashTableOps = {
|
|
|
|
PLDHashTable::HashStringKey,
|
|
|
|
MatchPrefEntry,
|
|
|
|
PLDHashTable::MoveEntryStub,
|
|
|
|
ClearPrefEntry,
|
|
|
|
nullptr,
|
|
|
|
};
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static bool
|
|
|
|
pref_ValueChanged(PrefValue aOldValue, PrefValue aNewValue, PrefType aType);
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
pref_DoCallback(const char* aChangedPref);
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
kPrefSetDefault = 1,
|
|
|
|
kPrefForceSet = 2,
|
|
|
|
kPrefStickyDefault = 4,
|
|
|
|
};
|
|
|
|
|
|
|
|
static nsresult
|
2017-11-09 07:00:42 +00:00
|
|
|
pref_SetPref(const char* aKey,
|
|
|
|
PrefValue aValue,
|
|
|
|
PrefType aType,
|
|
|
|
uint32_t aFlags);
|
2017-10-11 10:02:03 +00:00
|
|
|
|
|
|
|
#define PREF_HASHTABLE_INITIAL_LENGTH 1024
|
|
|
|
|
2017-10-26 05:48:11 +00:00
|
|
|
// Assign to aResult a quoted, escaped copy of aOriginal.
|
2017-10-11 10:02:03 +00:00
|
|
|
static void
|
|
|
|
StrEscape(const char* aOriginal, nsCString& aResult)
|
|
|
|
{
|
2017-10-26 05:48:11 +00:00
|
|
|
if (aOriginal == nullptr) {
|
|
|
|
aResult.AssignLiteral("\"\"");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-11 10:02:03 +00: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.
|
|
|
|
const char* p;
|
|
|
|
|
2017-10-26 05:48:11 +00:00
|
|
|
aResult.Assign('"');
|
2017-10-11 10:02:03 +00:00
|
|
|
|
|
|
|
// Paranoid worst case all slashes will free quickly.
|
|
|
|
for (p = aOriginal; *p; ++p) {
|
|
|
|
switch (*p) {
|
|
|
|
case '\n':
|
|
|
|
aResult.AppendLiteral("\\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\r':
|
|
|
|
aResult.AppendLiteral("\\r");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\\':
|
|
|
|
aResult.AppendLiteral("\\\\");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\"':
|
|
|
|
aResult.AppendLiteral("\\\"");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
aResult.Append(*p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-10-26 05:48:11 +00:00
|
|
|
|
|
|
|
aResult.Append('"');
|
2017-10-11 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 04:24:59 +00:00
|
|
|
static PrefSaveData
|
2017-10-26 05:26:24 +00:00
|
|
|
pref_savePrefs()
|
2017-10-11 10:02:03 +00:00
|
|
|
{
|
2017-10-26 05:26:24 +00:00
|
|
|
PrefSaveData savedPrefs(gHashTable->EntryCount());
|
2017-10-11 10:02:03 +00:00
|
|
|
|
2017-10-26 05:26:24 +00:00
|
|
|
for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) {
|
2017-10-11 10:02:03 +00:00
|
|
|
auto pref = static_cast<PrefHashEntry*>(iter.Get());
|
|
|
|
|
|
|
|
// where we're getting our pref from
|
|
|
|
PrefValue* sourcePref;
|
|
|
|
|
|
|
|
if (pref->mPrefFlags.HasUserValue() &&
|
|
|
|
(pref_ValueChanged(pref->mDefaultPref,
|
|
|
|
pref->mUserPref,
|
|
|
|
pref->mPrefFlags.GetPrefType()) ||
|
|
|
|
!pref->mPrefFlags.HasDefault() ||
|
|
|
|
pref->mPrefFlags.HasStickyDefault())) {
|
|
|
|
sourcePref = &pref->mUserPref;
|
|
|
|
} else {
|
|
|
|
// do not save default prefs that haven't changed
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-26 05:48:11 +00:00
|
|
|
nsAutoCString prefName;
|
|
|
|
StrEscape(pref->mKey, prefName);
|
|
|
|
|
|
|
|
nsAutoCString prefValue;
|
2017-10-11 10:02:03 +00:00
|
|
|
if (pref->mPrefFlags.IsTypeString()) {
|
|
|
|
StrEscape(sourcePref->mStringVal, prefValue);
|
|
|
|
|
|
|
|
} else if (pref->mPrefFlags.IsTypeInt()) {
|
|
|
|
prefValue.AppendInt(sourcePref->mIntVal);
|
|
|
|
|
|
|
|
} else if (pref->mPrefFlags.IsTypeBool()) {
|
2017-10-26 05:48:11 +00:00
|
|
|
prefValue = sourcePref->mBoolVal ? "true" : "false";
|
2017-10-11 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 05:48:11 +00:00
|
|
|
nsPrintfCString str("user_pref(%s, %s);", prefName.get(), prefValue.get());
|
|
|
|
savedPrefs.AppendElement(str);
|
2017-10-11 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return savedPrefs;
|
|
|
|
}
|
|
|
|
|
2017-10-13 04:24:59 +00:00
|
|
|
static bool
|
2017-10-11 10:02:03 +00:00
|
|
|
pref_EntryHasAdvisablySizedValues(PrefHashEntry* aHashEntry)
|
|
|
|
{
|
|
|
|
if (aHashEntry->mPrefFlags.GetPrefType() != PrefType::String) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-26 05:27:09 +00:00
|
|
|
const char* stringVal;
|
2017-10-11 10:02:03 +00:00
|
|
|
if (aHashEntry->mPrefFlags.HasDefault()) {
|
|
|
|
stringVal = aHashEntry->mDefaultPref.mStringVal;
|
|
|
|
if (strlen(stringVal) > MAX_ADVISABLE_PREF_LENGTH) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aHashEntry->mPrefFlags.HasUserValue()) {
|
|
|
|
stringVal = aHashEntry->mUserPref.mStringVal;
|
|
|
|
if (strlen(stringVal) > MAX_ADVISABLE_PREF_LENGTH) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
GetPrefValueFromEntry(PrefHashEntry* aHashEntry,
|
|
|
|
dom::PrefSetting* aPref,
|
2017-11-12 22:20:42 +00:00
|
|
|
PrefValueKind aKind)
|
2017-10-11 10:02:03 +00:00
|
|
|
{
|
|
|
|
PrefValue* value;
|
|
|
|
dom::PrefValue* settingValue;
|
2017-11-12 22:20:42 +00:00
|
|
|
if (aKind == PrefValueKind::User) {
|
2017-10-11 10:02:03 +00:00
|
|
|
value = &aHashEntry->mUserPref;
|
|
|
|
aPref->userValue() = dom::PrefValue();
|
|
|
|
settingValue = &aPref->userValue().get_PrefValue();
|
|
|
|
} else {
|
|
|
|
value = &aHashEntry->mDefaultPref;
|
|
|
|
aPref->defaultValue() = dom::PrefValue();
|
|
|
|
settingValue = &aPref->defaultValue().get_PrefValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (aHashEntry->mPrefFlags.GetPrefType()) {
|
|
|
|
case PrefType::String:
|
|
|
|
*settingValue = nsDependentCString(value->mStringVal);
|
|
|
|
return;
|
|
|
|
case PrefType::Int:
|
|
|
|
*settingValue = value->mIntVal;
|
|
|
|
return;
|
|
|
|
case PrefType::Bool:
|
|
|
|
*settingValue = !!value->mBoolVal;
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 04:24:59 +00:00
|
|
|
static void
|
2017-10-11 10:02:03 +00:00
|
|
|
pref_GetPrefFromEntry(PrefHashEntry* aHashEntry, dom::PrefSetting* aPref)
|
|
|
|
{
|
|
|
|
aPref->name() = aHashEntry->mKey;
|
|
|
|
|
|
|
|
if (aHashEntry->mPrefFlags.HasDefault()) {
|
2017-11-12 22:20:42 +00:00
|
|
|
GetPrefValueFromEntry(aHashEntry, aPref, PrefValueKind::Default);
|
2017-10-11 10:02:03 +00:00
|
|
|
} else {
|
|
|
|
aPref->defaultValue() = null_t();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aHashEntry->mPrefFlags.HasUserValue()) {
|
2017-11-12 22:20:42 +00:00
|
|
|
GetPrefValueFromEntry(aHashEntry, aPref, PrefValueKind::User);
|
2017-10-11 10:02:03 +00:00
|
|
|
} 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()));
|
|
|
|
}
|
|
|
|
|
2017-10-13 04:24:59 +00:00
|
|
|
static bool
|
2017-10-11 10:02:03 +00:00
|
|
|
PREF_HasUserPref(const char* aPrefName)
|
|
|
|
{
|
|
|
|
if (!gHashTable) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(aPrefName);
|
|
|
|
return pref && pref->mPrefFlags.HasUserValue();
|
|
|
|
}
|
|
|
|
|
2017-10-13 04:24:59 +00:00
|
|
|
// Clears the given pref (reverts it to its default value).
|
|
|
|
static nsresult
|
2017-10-11 10:02:03 +00:00
|
|
|
PREF_ClearUserPref(const char* aPrefName)
|
|
|
|
{
|
|
|
|
if (!gHashTable) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(aPrefName);
|
|
|
|
if (pref && pref->mPrefFlags.HasUserValue()) {
|
|
|
|
pref->mPrefFlags.SetHasUserValue(false);
|
|
|
|
|
|
|
|
if (!pref->mPrefFlags.HasDefault()) {
|
|
|
|
gHashTable->RemoveEntry(pref);
|
|
|
|
}
|
|
|
|
|
|
|
|
pref_DoCallback(aPrefName);
|
2017-10-26 05:14:01 +00:00
|
|
|
Preferences::HandleDirty();
|
2017-10-11 10:02:03 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-10-13 04:24:59 +00:00
|
|
|
// Clears all user prefs.
|
|
|
|
static nsresult
|
2017-10-11 10:02:03 +00:00
|
|
|
PREF_ClearAllUserPrefs()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!gHashTable) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2017-11-14 08:06:01 +00:00
|
|
|
Vector<const char*> prefNames;
|
2017-10-11 10:02:03 +00:00
|
|
|
for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
auto pref = static_cast<PrefHashEntry*>(iter.Get());
|
|
|
|
|
|
|
|
if (pref->mPrefFlags.HasUserValue()) {
|
2017-11-14 08:06:01 +00:00
|
|
|
if (!prefNames.append(pref->mKey)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2017-10-11 10:02:03 +00:00
|
|
|
|
|
|
|
pref->mPrefFlags.SetHasUserValue(false);
|
|
|
|
if (!pref->mPrefFlags.HasDefault()) {
|
|
|
|
iter.Remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-14 08:06:01 +00:00
|
|
|
for (const char* prefName : prefNames) {
|
|
|
|
pref_DoCallback(prefName);
|
2017-10-11 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 05:14:01 +00:00
|
|
|
Preferences::HandleDirty();
|
2017-10-11 10:02:03 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-10-13 04:24:59 +00:00
|
|
|
// Function that sets whether or not the preference is locked and therefore
|
|
|
|
// cannot be changed.
|
|
|
|
static nsresult
|
2017-10-11 10:02:03 +00:00
|
|
|
PREF_LockPref(const char* aKey, bool aLockIt)
|
|
|
|
{
|
|
|
|
if (!gHashTable) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(aKey);
|
|
|
|
if (!pref) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aLockIt) {
|
|
|
|
if (!pref->mPrefFlags.IsLocked()) {
|
|
|
|
pref->mPrefFlags.SetLocked(true);
|
|
|
|
gIsAnyPrefLocked = true;
|
|
|
|
pref_DoCallback(aKey);
|
|
|
|
}
|
|
|
|
} else if (pref->mPrefFlags.IsLocked()) {
|
|
|
|
pref->mPrefFlags.SetLocked(false);
|
|
|
|
pref_DoCallback(aKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Hash table functions
|
|
|
|
//
|
|
|
|
|
|
|
|
static bool
|
|
|
|
pref_ValueChanged(PrefValue aOldValue, PrefValue aNewValue, PrefType aType)
|
|
|
|
{
|
|
|
|
bool changed = true;
|
|
|
|
switch (aType) {
|
|
|
|
case PrefType::String:
|
|
|
|
if (aOldValue.mStringVal && aNewValue.mStringVal) {
|
|
|
|
changed = (strcmp(aOldValue.mStringVal, aNewValue.mStringVal) != 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PrefType::Int:
|
|
|
|
changed = aOldValue.mIntVal != aNewValue.mIntVal;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PrefType::Bool:
|
|
|
|
changed = aOldValue.mBoolVal != aNewValue.mBoolVal;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PrefType::Invalid:
|
|
|
|
default:
|
|
|
|
changed = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2017-10-26 05:36:17 +00:00
|
|
|
static void
|
2017-10-11 10:02:03 +00:00
|
|
|
pref_SetValue(PrefValue* aExistingValue,
|
2017-10-26 05:36:17 +00:00
|
|
|
PrefType aExistingType,
|
2017-10-11 10:02:03 +00:00
|
|
|
PrefValue aNewValue,
|
|
|
|
PrefType aNewType)
|
|
|
|
{
|
2017-10-26 05:36:17 +00:00
|
|
|
if (aExistingType == PrefType::String) {
|
2017-10-26 05:27:09 +00:00
|
|
|
free(const_cast<char*>(aExistingValue->mStringVal));
|
2017-10-11 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 05:36:17 +00:00
|
|
|
if (aNewType == PrefType::String) {
|
2017-10-11 10:02:03 +00:00
|
|
|
MOZ_ASSERT(aNewValue.mStringVal);
|
|
|
|
aExistingValue->mStringVal =
|
2017-10-13 04:24:58 +00:00
|
|
|
aNewValue.mStringVal ? moz_xstrdup(aNewValue.mStringVal) : nullptr;
|
2017-10-11 10:02:03 +00:00
|
|
|
} else {
|
|
|
|
*aExistingValue = aNewValue;
|
|
|
|
}
|
|
|
|
}
|
2017-06-21 19:26:10 +00:00
|
|
|
|
2013-12-03 23:18:09 +00:00
|
|
|
#ifdef DEBUG
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2017-10-11 10:02:03 +00:00
|
|
|
static pref_initPhase gPhase = START;
|
2017-10-11 09:31:32 +00:00
|
|
|
|
2017-10-11 10:02:03 +00:00
|
|
|
struct StringComparator
|
|
|
|
{
|
|
|
|
const char* mKey;
|
|
|
|
explicit StringComparator(const char* aKey)
|
|
|
|
: mKey(aKey)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
int operator()(const char* aString) const { return strcmp(mKey, aString); }
|
|
|
|
};
|
|
|
|
|
2017-10-13 04:24:59 +00:00
|
|
|
static bool
|
2017-10-11 10:02:03 +00:00
|
|
|
InInitArray(const char* aKey)
|
|
|
|
{
|
|
|
|
size_t prefsLen;
|
|
|
|
size_t found;
|
|
|
|
const char** list = mozilla::dom::ContentPrefs::GetContentPrefs(&prefsLen);
|
|
|
|
return BinarySearchIf(list, 0, prefsLen, StringComparator(aKey), &found);
|
|
|
|
}
|
|
|
|
|
2017-10-26 05:14:00 +00:00
|
|
|
static bool gWatchingPref = false;
|
|
|
|
|
2017-10-11 10:02:03 +00:00
|
|
|
class WatchingPrefRAII
|
2017-10-05 23:06:47 +00:00
|
|
|
{
|
2017-02-22 03:17:52 +00:00
|
|
|
public:
|
2017-10-26 05:14:00 +00:00
|
|
|
WatchingPrefRAII() { gWatchingPref = true; }
|
|
|
|
~WatchingPrefRAII() { gWatchingPref = false; }
|
2017-02-22 03:17:52 +00:00
|
|
|
};
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2017-10-11 10:02:03 +00:00
|
|
|
#define WATCHING_PREF_RAII() WatchingPrefRAII watchingPrefRAII
|
2017-10-05 23:06:47 +00:00
|
|
|
|
|
|
|
#else // DEBUG
|
|
|
|
|
2017-10-11 10:02:03 +00:00
|
|
|
#define WATCHING_PREF_RAII()
|
|
|
|
|
|
|
|
#endif // DEBUG
|
|
|
|
|
2017-10-13 04:24:59 +00:00
|
|
|
static PrefHashEntry*
|
2017-10-11 10:02:03 +00:00
|
|
|
pref_HashTableLookup(const char* aKey)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread() || mozilla::ServoStyleSet::IsInServoTraversal());
|
2017-11-12 22:19:55 +00:00
|
|
|
MOZ_ASSERT((XRE_IsParentProcess() || gPhase != START),
|
2017-10-11 10:02:03 +00:00
|
|
|
"pref access before commandline prefs set");
|
|
|
|
|
|
|
|
// If you're hitting this assertion, you've added a pref access to start up.
|
|
|
|
// Consider moving it later or add it to the whitelist in ContentPrefs.cpp
|
2017-11-12 22:19:55 +00:00
|
|
|
// and get review from a DOM peer.
|
2017-10-11 10:02:03 +00:00
|
|
|
#ifdef DEBUG
|
2017-11-12 22:19:55 +00:00
|
|
|
if (!XRE_IsParentProcess() && gPhase <= END_INIT_PREFS && !gWatchingPref &&
|
2017-10-11 10:02:03 +00:00
|
|
|
!InInitArray(aKey)) {
|
|
|
|
MOZ_CRASH_UNSAFE_PRINTF(
|
|
|
|
"accessing non-init pref %s before the rest of the prefs are sent", aKey);
|
2017-10-11 09:31:32 +00:00
|
|
|
}
|
2017-10-11 10:02:03 +00:00
|
|
|
#endif
|
2017-10-11 09:31:32 +00:00
|
|
|
|
2017-10-11 10:02:03 +00:00
|
|
|
return static_cast<PrefHashEntry*>(gHashTable->Search(aKey));
|
|
|
|
}
|
|
|
|
|
2017-10-13 04:24:59 +00:00
|
|
|
static nsresult
|
2017-11-09 07:00:42 +00:00
|
|
|
pref_SetPref(const char* aKey,
|
|
|
|
PrefValue aValue,
|
|
|
|
PrefType aType,
|
|
|
|
uint32_t aFlags)
|
2017-10-11 10:02:03 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!gHashTable) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2013-12-03 23:18:09 +00:00
|
|
|
}
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2017-10-11 10:02:03 +00:00
|
|
|
auto pref = static_cast<PrefHashEntry*>(gHashTable->Add(aKey, fallible));
|
|
|
|
if (!pref) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2017-10-11 10:02:03 +00:00
|
|
|
// New entry, need to initialize.
|
|
|
|
if (!pref->mKey) {
|
|
|
|
// Initialize the pref entry.
|
|
|
|
pref->mPrefFlags.Reset().SetPrefType(aType);
|
|
|
|
pref->mKey = ArenaStrdup(aKey, gPrefNameArena);
|
|
|
|
memset(&pref->mDefaultPref, 0, sizeof(pref->mDefaultPref));
|
|
|
|
memset(&pref->mUserPref, 0, sizeof(pref->mUserPref));
|
|
|
|
|
|
|
|
} else if (pref->mPrefFlags.HasDefault() &&
|
|
|
|
!pref->mPrefFlags.IsPrefType(aType)) {
|
|
|
|
NS_WARNING(
|
|
|
|
nsPrintfCString(
|
2017-10-11 18:10:43 +00:00
|
|
|
"Ignoring attempt to overwrite value of default pref %s (type %s) with "
|
|
|
|
"the wrong type (%s)!",
|
|
|
|
aKey,
|
|
|
|
PrefTypeToString(pref->mPrefFlags.GetPrefType()),
|
|
|
|
PrefTypeToString(aType))
|
2017-10-11 10:02:03 +00:00
|
|
|
.get());
|
|
|
|
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool valueChanged = false;
|
|
|
|
if (aFlags & kPrefSetDefault) {
|
|
|
|
if (!pref->mPrefFlags.IsLocked()) {
|
|
|
|
// ?? change of semantics?
|
|
|
|
if (pref_ValueChanged(pref->mDefaultPref, aValue, aType) ||
|
|
|
|
!pref->mPrefFlags.HasDefault()) {
|
2017-10-26 05:36:17 +00:00
|
|
|
pref_SetValue(
|
|
|
|
&pref->mDefaultPref, pref->mPrefFlags.GetPrefType(), aValue, aType);
|
|
|
|
pref->mPrefFlags.SetPrefType(aType);
|
|
|
|
pref->mPrefFlags.SetHasDefault(true);
|
2017-10-11 10:02:03 +00:00
|
|
|
if (aFlags & kPrefStickyDefault) {
|
|
|
|
pref->mPrefFlags.SetHasStickyDefault(true);
|
|
|
|
}
|
|
|
|
if (!pref->mPrefFlags.HasUserValue()) {
|
|
|
|
valueChanged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// What if we change the default to be the same as the user value?
|
|
|
|
// Should we clear the user value?
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If new value is same as the default value and it's not a "sticky" pref,
|
|
|
|
// then un-set the user value. Otherwise, set the user value only if it has
|
|
|
|
// changed.
|
|
|
|
if ((pref->mPrefFlags.HasDefault()) &&
|
|
|
|
!(pref->mPrefFlags.HasStickyDefault()) &&
|
|
|
|
!pref_ValueChanged(pref->mDefaultPref, aValue, aType) &&
|
|
|
|
!(aFlags & kPrefForceSet)) {
|
|
|
|
if (pref->mPrefFlags.HasUserValue()) {
|
|
|
|
// XXX should we free a user-set string value if there is one?
|
|
|
|
pref->mPrefFlags.SetHasUserValue(false);
|
|
|
|
if (!pref->mPrefFlags.IsLocked()) {
|
2017-10-26 05:14:01 +00:00
|
|
|
Preferences::HandleDirty();
|
2017-10-11 10:02:03 +00:00
|
|
|
valueChanged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (!pref->mPrefFlags.HasUserValue() ||
|
|
|
|
!pref->mPrefFlags.IsPrefType(aType) ||
|
|
|
|
pref_ValueChanged(pref->mUserPref, aValue, aType)) {
|
2017-10-26 05:36:17 +00:00
|
|
|
pref_SetValue(
|
|
|
|
&pref->mUserPref, pref->mPrefFlags.GetPrefType(), aValue, aType);
|
|
|
|
pref->mPrefFlags.SetPrefType(aType);
|
|
|
|
pref->mPrefFlags.SetHasUserValue(true);
|
2017-10-11 10:02:03 +00:00
|
|
|
if (!pref->mPrefFlags.IsLocked()) {
|
2017-10-26 05:14:01 +00:00
|
|
|
Preferences::HandleDirty();
|
2017-10-11 10:02:03 +00:00
|
|
|
valueChanged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (valueChanged) {
|
|
|
|
return pref_DoCallback(aKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-10-13 04:24:59 +00:00
|
|
|
// Bool function that returns whether or not the preference is locked and
|
|
|
|
// therefore cannot be changed.
|
|
|
|
static bool
|
2017-10-11 10:02:03 +00:00
|
|
|
PREF_PrefIsLocked(const char* aPrefName)
|
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
if (gIsAnyPrefLocked && gHashTable) {
|
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(aPrefName);
|
|
|
|
if (pref && pref->mPrefFlags.IsLocked()) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-10-13 04:24:59 +00:00
|
|
|
// Adds a node to the callback list; the position depends on aIsPriority. The
|
|
|
|
// callback function will be called if anything below that node is modified.
|
|
|
|
static void
|
2017-10-11 10:02:03 +00:00
|
|
|
PREF_RegisterCallback(const char* aPrefNode,
|
|
|
|
PrefChangedFunc aCallback,
|
2017-10-13 04:24:59 +00:00
|
|
|
void* aData,
|
Bug 1414188 - Remove ValueObserver. r=glandium.
libpref has callbacks. You can register a callback function against a
particular pref (or pref prefix), and it'll be called when any matching pref
changes.
This is implemented in two layers. The lower layer is the list of CallbackNode
objects, pointed to by gFirstCallback.
The upper layer involves gObserverTable, which is a hash table of ValueObserver
objects. It is used for callbacks registered via
Preferences::RegisterCallback() and Preferences::Add*VarCache(), but not for
observers registered with Preferences::Add{Weak,Strong}Observer(). If multiple
callbacks with identical prefnames, callback functions and MatchKinds occur,
they are commoned up into a single ValueObserver, which is then wrapped by a
single CallbackNode. (The callbacks within a single ValueObserver can have
different void* aClosure args; ValueObserver keeps those in an nsTArray.)
Note also that gObserverTable is very inelegant, with duplication of data
between the keys and the values due it being a
nsRefPtrHashtable<ValueObserverHashKey, ValueObserver> and ValueObserver being
a subclass of ValueObserverHashKey(!)
This extra layer might make sense if there were a lot of commoning up
happening, but there's not. Across all process kinds there is an average of
between 1.2 and 1.7 closures per ValueObserver. The vast majority have 1
closure, and there are a handful that have multiple; the highest number I've
seen is 71.
(Note that this two-layer design probably seemed more natural back when libpref
was spread across multiple files.)
This patch removes the ValueObserver layer. The only tricky part is that there
is a notion of a MatchKind -- ExactMatch or PrefixMatch -- which exists in the
ValueObserverLayer but not in the CallbackNode layer. So the patch moves the
MatchKind into the CallbackNode layer, which is straightforward.
On Linux64 this reduces memory usage by about 40 KiB in the parent process and
about 30 KiB in each content processes.
The performance implications are minor.
- The list of CallbackNodes is a bit longer, so when a pref is changed we must
check more nodes. But we avoid possibly doing prefname matching twice.
- Callback registration is much faster, just a simple list prepend, without any
hash table insertion required.
- Callback unregistration is probably not much different. There is a longer
list to traverse, but no hash table removal.
- Pref lookup is by far the hottest libpref operation, and it's unchanged.
For perf to be genuinely affected would require (a) a *lot* more
almost-duplicate callbacks that would have been commoned up, and (b) frequent
changing of the pref those callbacks are observing. This seems highly unlikely.
MozReview-Commit-ID: 6xo4xmytOf3
--HG--
extra : rebase_source : cf9884a8a40b2cc3b00ab165ae5ab4ec3383f72d
2017-11-02 09:38:34 +00:00
|
|
|
Preferences::MatchKind aMatchKind,
|
2017-10-13 04:24:59 +00:00
|
|
|
bool aIsPriority)
|
2017-10-11 10:02:03 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aPrefNode, "aPrefNode must not be nullptr");
|
|
|
|
NS_PRECONDITION(aCallback, "aCallback must not be nullptr");
|
|
|
|
|
2017-10-18 05:31:23 +00:00
|
|
|
auto node = (CallbackNode*)moz_xmalloc(sizeof(CallbackNode));
|
2017-10-13 04:24:59 +00:00
|
|
|
node->mDomain = moz_xstrdup(aPrefNode);
|
|
|
|
node->mFunc = aCallback;
|
|
|
|
node->mData = aData;
|
Bug 1414188 - Remove ValueObserver. r=glandium.
libpref has callbacks. You can register a callback function against a
particular pref (or pref prefix), and it'll be called when any matching pref
changes.
This is implemented in two layers. The lower layer is the list of CallbackNode
objects, pointed to by gFirstCallback.
The upper layer involves gObserverTable, which is a hash table of ValueObserver
objects. It is used for callbacks registered via
Preferences::RegisterCallback() and Preferences::Add*VarCache(), but not for
observers registered with Preferences::Add{Weak,Strong}Observer(). If multiple
callbacks with identical prefnames, callback functions and MatchKinds occur,
they are commoned up into a single ValueObserver, which is then wrapped by a
single CallbackNode. (The callbacks within a single ValueObserver can have
different void* aClosure args; ValueObserver keeps those in an nsTArray.)
Note also that gObserverTable is very inelegant, with duplication of data
between the keys and the values due it being a
nsRefPtrHashtable<ValueObserverHashKey, ValueObserver> and ValueObserver being
a subclass of ValueObserverHashKey(!)
This extra layer might make sense if there were a lot of commoning up
happening, but there's not. Across all process kinds there is an average of
between 1.2 and 1.7 closures per ValueObserver. The vast majority have 1
closure, and there are a handful that have multiple; the highest number I've
seen is 71.
(Note that this two-layer design probably seemed more natural back when libpref
was spread across multiple files.)
This patch removes the ValueObserver layer. The only tricky part is that there
is a notion of a MatchKind -- ExactMatch or PrefixMatch -- which exists in the
ValueObserverLayer but not in the CallbackNode layer. So the patch moves the
MatchKind into the CallbackNode layer, which is straightforward.
On Linux64 this reduces memory usage by about 40 KiB in the parent process and
about 30 KiB in each content processes.
The performance implications are minor.
- The list of CallbackNodes is a bit longer, so when a pref is changed we must
check more nodes. But we avoid possibly doing prefname matching twice.
- Callback registration is much faster, just a simple list prepend, without any
hash table insertion required.
- Callback unregistration is probably not much different. There is a longer
list to traverse, but no hash table removal.
- Pref lookup is by far the hottest libpref operation, and it's unchanged.
For perf to be genuinely affected would require (a) a *lot* more
almost-duplicate callbacks that would have been commoned up, and (b) frequent
changing of the pref those callbacks are observing. This seems highly unlikely.
MozReview-Commit-ID: 6xo4xmytOf3
--HG--
extra : rebase_source : cf9884a8a40b2cc3b00ab165ae5ab4ec3383f72d
2017-11-02 09:38:34 +00:00
|
|
|
node->mMatchKind = aMatchKind;
|
2017-10-13 04:24:59 +00:00
|
|
|
|
|
|
|
if (aIsPriority) {
|
|
|
|
// Add to the start of the list.
|
2017-10-13 04:24:59 +00:00
|
|
|
node->mNext = gFirstCallback;
|
|
|
|
gFirstCallback = node;
|
2017-10-13 04:24:59 +00:00
|
|
|
if (!gLastPriorityNode) {
|
|
|
|
gLastPriorityNode = node;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Add to the start of the non-priority part of the list.
|
|
|
|
if (gLastPriorityNode) {
|
|
|
|
node->mNext = gLastPriorityNode->mNext;
|
|
|
|
gLastPriorityNode->mNext = node;
|
|
|
|
} else {
|
|
|
|
node->mNext = gFirstCallback;
|
|
|
|
gFirstCallback = node;
|
|
|
|
}
|
2017-10-11 10:02:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes |node| from callback list. Returns the node after the deleted one.
|
2017-10-13 04:24:59 +00:00
|
|
|
static CallbackNode*
|
2017-10-11 10:02:03 +00:00
|
|
|
pref_RemoveCallbackNode(CallbackNode* aNode, CallbackNode* aPrevNode)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(!aPrevNode || aPrevNode->mNext == aNode, "invalid params");
|
|
|
|
NS_PRECONDITION(aPrevNode || gFirstCallback == aNode, "invalid params");
|
|
|
|
|
|
|
|
NS_ASSERTION(
|
|
|
|
!gCallbacksInProgress,
|
|
|
|
"modifying the callback list while gCallbacksInProgress is true");
|
|
|
|
|
|
|
|
CallbackNode* next_node = aNode->mNext;
|
|
|
|
if (aPrevNode) {
|
|
|
|
aPrevNode->mNext = next_node;
|
|
|
|
} else {
|
|
|
|
gFirstCallback = next_node;
|
|
|
|
}
|
|
|
|
if (gLastPriorityNode == aNode) {
|
|
|
|
gLastPriorityNode = aPrevNode;
|
|
|
|
}
|
2017-10-26 05:27:12 +00:00
|
|
|
free(const_cast<char*>(aNode->mDomain));
|
2017-10-11 10:02:03 +00:00
|
|
|
free(aNode);
|
|
|
|
return next_node;
|
|
|
|
}
|
|
|
|
|
2017-10-13 04:24:59 +00:00
|
|
|
// Deletes a node from the callback list or marks it for deletion. Succeeds if
|
|
|
|
// a callback was found that matched all the parameters.
|
|
|
|
static nsresult
|
2017-10-11 10:02:03 +00:00
|
|
|
PREF_UnregisterCallback(const char* aPrefNode,
|
|
|
|
PrefChangedFunc aCallback,
|
Bug 1414188 - Remove ValueObserver. r=glandium.
libpref has callbacks. You can register a callback function against a
particular pref (or pref prefix), and it'll be called when any matching pref
changes.
This is implemented in two layers. The lower layer is the list of CallbackNode
objects, pointed to by gFirstCallback.
The upper layer involves gObserverTable, which is a hash table of ValueObserver
objects. It is used for callbacks registered via
Preferences::RegisterCallback() and Preferences::Add*VarCache(), but not for
observers registered with Preferences::Add{Weak,Strong}Observer(). If multiple
callbacks with identical prefnames, callback functions and MatchKinds occur,
they are commoned up into a single ValueObserver, which is then wrapped by a
single CallbackNode. (The callbacks within a single ValueObserver can have
different void* aClosure args; ValueObserver keeps those in an nsTArray.)
Note also that gObserverTable is very inelegant, with duplication of data
between the keys and the values due it being a
nsRefPtrHashtable<ValueObserverHashKey, ValueObserver> and ValueObserver being
a subclass of ValueObserverHashKey(!)
This extra layer might make sense if there were a lot of commoning up
happening, but there's not. Across all process kinds there is an average of
between 1.2 and 1.7 closures per ValueObserver. The vast majority have 1
closure, and there are a handful that have multiple; the highest number I've
seen is 71.
(Note that this two-layer design probably seemed more natural back when libpref
was spread across multiple files.)
This patch removes the ValueObserver layer. The only tricky part is that there
is a notion of a MatchKind -- ExactMatch or PrefixMatch -- which exists in the
ValueObserverLayer but not in the CallbackNode layer. So the patch moves the
MatchKind into the CallbackNode layer, which is straightforward.
On Linux64 this reduces memory usage by about 40 KiB in the parent process and
about 30 KiB in each content processes.
The performance implications are minor.
- The list of CallbackNodes is a bit longer, so when a pref is changed we must
check more nodes. But we avoid possibly doing prefname matching twice.
- Callback registration is much faster, just a simple list prepend, without any
hash table insertion required.
- Callback unregistration is probably not much different. There is a longer
list to traverse, but no hash table removal.
- Pref lookup is by far the hottest libpref operation, and it's unchanged.
For perf to be genuinely affected would require (a) a *lot* more
almost-duplicate callbacks that would have been commoned up, and (b) frequent
changing of the pref those callbacks are observing. This seems highly unlikely.
MozReview-Commit-ID: 6xo4xmytOf3
--HG--
extra : rebase_source : cf9884a8a40b2cc3b00ab165ae5ab4ec3383f72d
2017-11-02 09:38:34 +00:00
|
|
|
void* aData,
|
|
|
|
Preferences::MatchKind aMatchKind)
|
2017-10-11 10:02:03 +00:00
|
|
|
{
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
CallbackNode* node = gFirstCallback;
|
|
|
|
CallbackNode* prev_node = nullptr;
|
|
|
|
|
|
|
|
while (node != nullptr) {
|
|
|
|
if (node->mFunc == aCallback && node->mData == aData &&
|
Bug 1414188 - Remove ValueObserver. r=glandium.
libpref has callbacks. You can register a callback function against a
particular pref (or pref prefix), and it'll be called when any matching pref
changes.
This is implemented in two layers. The lower layer is the list of CallbackNode
objects, pointed to by gFirstCallback.
The upper layer involves gObserverTable, which is a hash table of ValueObserver
objects. It is used for callbacks registered via
Preferences::RegisterCallback() and Preferences::Add*VarCache(), but not for
observers registered with Preferences::Add{Weak,Strong}Observer(). If multiple
callbacks with identical prefnames, callback functions and MatchKinds occur,
they are commoned up into a single ValueObserver, which is then wrapped by a
single CallbackNode. (The callbacks within a single ValueObserver can have
different void* aClosure args; ValueObserver keeps those in an nsTArray.)
Note also that gObserverTable is very inelegant, with duplication of data
between the keys and the values due it being a
nsRefPtrHashtable<ValueObserverHashKey, ValueObserver> and ValueObserver being
a subclass of ValueObserverHashKey(!)
This extra layer might make sense if there were a lot of commoning up
happening, but there's not. Across all process kinds there is an average of
between 1.2 and 1.7 closures per ValueObserver. The vast majority have 1
closure, and there are a handful that have multiple; the highest number I've
seen is 71.
(Note that this two-layer design probably seemed more natural back when libpref
was spread across multiple files.)
This patch removes the ValueObserver layer. The only tricky part is that there
is a notion of a MatchKind -- ExactMatch or PrefixMatch -- which exists in the
ValueObserverLayer but not in the CallbackNode layer. So the patch moves the
MatchKind into the CallbackNode layer, which is straightforward.
On Linux64 this reduces memory usage by about 40 KiB in the parent process and
about 30 KiB in each content processes.
The performance implications are minor.
- The list of CallbackNodes is a bit longer, so when a pref is changed we must
check more nodes. But we avoid possibly doing prefname matching twice.
- Callback registration is much faster, just a simple list prepend, without any
hash table insertion required.
- Callback unregistration is probably not much different. There is a longer
list to traverse, but no hash table removal.
- Pref lookup is by far the hottest libpref operation, and it's unchanged.
For perf to be genuinely affected would require (a) a *lot* more
almost-duplicate callbacks that would have been commoned up, and (b) frequent
changing of the pref those callbacks are observing. This seems highly unlikely.
MozReview-Commit-ID: 6xo4xmytOf3
--HG--
extra : rebase_source : cf9884a8a40b2cc3b00ab165ae5ab4ec3383f72d
2017-11-02 09:38:34 +00:00
|
|
|
node->mMatchKind == aMatchKind &&
|
2017-10-11 10:02:03 +00:00
|
|
|
strcmp(node->mDomain, aPrefNode) == 0) {
|
|
|
|
if (gCallbacksInProgress) {
|
|
|
|
// postpone the node removal until after
|
|
|
|
// callbacks enumeration is finished.
|
|
|
|
node->mFunc = nullptr;
|
|
|
|
gShouldCleanupDeadNodes = true;
|
|
|
|
prev_node = node;
|
|
|
|
node = node->mNext;
|
|
|
|
} else {
|
|
|
|
node = pref_RemoveCallbackNode(node, prev_node);
|
|
|
|
}
|
|
|
|
rv = NS_OK;
|
|
|
|
} else {
|
|
|
|
prev_node = node;
|
|
|
|
node = node->mNext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
pref_DoCallback(const char* aChangedPref)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
CallbackNode* node;
|
|
|
|
|
|
|
|
bool reentered = gCallbacksInProgress;
|
|
|
|
|
|
|
|
// Nodes must not be deleted while gCallbacksInProgress is true.
|
|
|
|
// 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.
|
|
|
|
gCallbacksInProgress = true;
|
|
|
|
|
Bug 1414188 - Remove ValueObserver. r=glandium.
libpref has callbacks. You can register a callback function against a
particular pref (or pref prefix), and it'll be called when any matching pref
changes.
This is implemented in two layers. The lower layer is the list of CallbackNode
objects, pointed to by gFirstCallback.
The upper layer involves gObserverTable, which is a hash table of ValueObserver
objects. It is used for callbacks registered via
Preferences::RegisterCallback() and Preferences::Add*VarCache(), but not for
observers registered with Preferences::Add{Weak,Strong}Observer(). If multiple
callbacks with identical prefnames, callback functions and MatchKinds occur,
they are commoned up into a single ValueObserver, which is then wrapped by a
single CallbackNode. (The callbacks within a single ValueObserver can have
different void* aClosure args; ValueObserver keeps those in an nsTArray.)
Note also that gObserverTable is very inelegant, with duplication of data
between the keys and the values due it being a
nsRefPtrHashtable<ValueObserverHashKey, ValueObserver> and ValueObserver being
a subclass of ValueObserverHashKey(!)
This extra layer might make sense if there were a lot of commoning up
happening, but there's not. Across all process kinds there is an average of
between 1.2 and 1.7 closures per ValueObserver. The vast majority have 1
closure, and there are a handful that have multiple; the highest number I've
seen is 71.
(Note that this two-layer design probably seemed more natural back when libpref
was spread across multiple files.)
This patch removes the ValueObserver layer. The only tricky part is that there
is a notion of a MatchKind -- ExactMatch or PrefixMatch -- which exists in the
ValueObserverLayer but not in the CallbackNode layer. So the patch moves the
MatchKind into the CallbackNode layer, which is straightforward.
On Linux64 this reduces memory usage by about 40 KiB in the parent process and
about 30 KiB in each content processes.
The performance implications are minor.
- The list of CallbackNodes is a bit longer, so when a pref is changed we must
check more nodes. But we avoid possibly doing prefname matching twice.
- Callback registration is much faster, just a simple list prepend, without any
hash table insertion required.
- Callback unregistration is probably not much different. There is a longer
list to traverse, but no hash table removal.
- Pref lookup is by far the hottest libpref operation, and it's unchanged.
For perf to be genuinely affected would require (a) a *lot* more
almost-duplicate callbacks that would have been commoned up, and (b) frequent
changing of the pref those callbacks are observing. This seems highly unlikely.
MozReview-Commit-ID: 6xo4xmytOf3
--HG--
extra : rebase_source : cf9884a8a40b2cc3b00ab165ae5ab4ec3383f72d
2017-11-02 09:38:34 +00:00
|
|
|
for (node = gFirstCallback; node; node = node->mNext) {
|
|
|
|
if (node->mFunc) {
|
|
|
|
bool matches =
|
|
|
|
node->mMatchKind == Preferences::ExactMatch
|
|
|
|
? strcmp(node->mDomain, aChangedPref) == 0
|
|
|
|
: strncmp(node->mDomain, aChangedPref, strlen(node->mDomain)) == 0;
|
|
|
|
if (matches) {
|
|
|
|
(node->mFunc)(aChangedPref, node->mData);
|
|
|
|
}
|
2017-10-11 10:02:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gCallbacksInProgress = reentered;
|
|
|
|
|
|
|
|
if (gShouldCleanupDeadNodes && !gCallbacksInProgress) {
|
|
|
|
CallbackNode* prev_node = nullptr;
|
|
|
|
node = gFirstCallback;
|
|
|
|
|
|
|
|
while (node != nullptr) {
|
|
|
|
if (!node->mFunc) {
|
|
|
|
node = pref_RemoveCallbackNode(node, prev_node);
|
|
|
|
} else {
|
|
|
|
prev_node = node;
|
|
|
|
node = node->mNext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gShouldCleanupDeadNodes = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-10-13 04:24:59 +00:00
|
|
|
// The callback function of the prefs parser.
|
|
|
|
static void
|
2017-10-11 10:02:03 +00:00
|
|
|
PREF_ReaderCallback(void* aClosure,
|
|
|
|
const char* aPref,
|
|
|
|
PrefValue aValue,
|
|
|
|
PrefType aType,
|
|
|
|
bool aIsDefault,
|
|
|
|
bool aIsStickyDefault)
|
|
|
|
{
|
|
|
|
uint32_t flags = 0;
|
|
|
|
if (aIsDefault) {
|
|
|
|
flags |= kPrefSetDefault;
|
|
|
|
if (aIsStickyDefault) {
|
|
|
|
flags |= kPrefStickyDefault;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
flags |= kPrefForceSet;
|
|
|
|
}
|
2017-11-09 07:00:42 +00:00
|
|
|
pref_SetPref(aPref, aValue, aType, flags);
|
2017-10-11 10:02:03 +00:00
|
|
|
}
|
2013-12-03 23:18:09 +00:00
|
|
|
|
2017-10-11 09:31:35 +00:00
|
|
|
//===========================================================================
|
|
|
|
// Prefs parsing
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
// Callback function used to notify consumer of preference name value pairs.
|
|
|
|
// The pref name and value must be copied by the implementor of the callback
|
|
|
|
// if they are needed beyond the scope of the callback function.
|
|
|
|
//
|
|
|
|
// |aClosure| is user data passed to PREF_InitParseState.
|
|
|
|
// |aPref| is the preference name.
|
|
|
|
// |aValue| is the preference value.
|
|
|
|
// |aType| is the preference type (PREF_STRING, PREF_INT, or PREF_BOOL).
|
|
|
|
// |aIsDefault| indicates if it's a default preference.
|
|
|
|
// |aIsStickyDefault| indicates if it's a sticky default preference.
|
|
|
|
typedef void (*PrefReader)(void* aClosure,
|
|
|
|
const char* aPref,
|
|
|
|
PrefValue aValue,
|
|
|
|
PrefType aType,
|
|
|
|
bool aIsDefault,
|
|
|
|
bool aIsStickyDefault);
|
|
|
|
|
|
|
|
// Report any errors or warnings we encounter during parsing.
|
|
|
|
typedef void (*PrefParseErrorReporter)(const char* aMessage,
|
|
|
|
int aLine,
|
|
|
|
bool aError);
|
|
|
|
|
2017-10-13 04:24:58 +00:00
|
|
|
struct PrefParseState
|
2017-10-11 09:31:35 +00:00
|
|
|
{
|
|
|
|
PrefReader mReader;
|
|
|
|
PrefParseErrorReporter mReporter;
|
|
|
|
void* mClosure;
|
|
|
|
int mState; // PREF_PARSE_...
|
|
|
|
int mNextState; // sometimes used...
|
|
|
|
const char* mStrMatch; // string to match
|
|
|
|
int mStrIndex; // next char of smatch to check;
|
|
|
|
// also, counter in \u parsing
|
|
|
|
char16_t mUtf16[2]; // parsing UTF16 (\u) escape
|
|
|
|
int mEscLen; // length in mEscTmp
|
|
|
|
char mEscTmp[6]; // raw escape to put back if err
|
|
|
|
char mQuoteChar; // char delimiter for quotations
|
|
|
|
char* mLb; // line buffer (only allocation)
|
|
|
|
char* mLbCur; // line buffer cursor
|
|
|
|
char* mLbEnd; // line buffer end
|
|
|
|
char* mVb; // value buffer (ptr into mLb)
|
|
|
|
PrefType mVtype; // PREF_{STRING,INT,BOOL}
|
|
|
|
bool mIsDefault; // true if (default) pref
|
|
|
|
bool mIsStickyDefault; // true if (sticky) pref
|
2017-10-13 04:24:58 +00:00
|
|
|
};
|
2017-10-11 09:31:35 +00:00
|
|
|
|
|
|
|
// Pref parser states.
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PREF_PARSE_INIT,
|
|
|
|
PREF_PARSE_MATCH_STRING,
|
|
|
|
PREF_PARSE_UNTIL_NAME,
|
|
|
|
PREF_PARSE_QUOTED_STRING,
|
|
|
|
PREF_PARSE_UNTIL_COMMA,
|
|
|
|
PREF_PARSE_UNTIL_VALUE,
|
|
|
|
PREF_PARSE_INT_VALUE,
|
|
|
|
PREF_PARSE_COMMENT_MAYBE_START,
|
|
|
|
PREF_PARSE_COMMENT_BLOCK,
|
|
|
|
PREF_PARSE_COMMENT_BLOCK_MAYBE_END,
|
|
|
|
PREF_PARSE_ESC_SEQUENCE,
|
|
|
|
PREF_PARSE_HEX_ESCAPE,
|
|
|
|
PREF_PARSE_UTF16_LOW_SURROGATE,
|
|
|
|
PREF_PARSE_UNTIL_OPEN_PAREN,
|
|
|
|
PREF_PARSE_UNTIL_CLOSE_PAREN,
|
|
|
|
PREF_PARSE_UNTIL_SEMICOLON,
|
|
|
|
PREF_PARSE_UNTIL_EOL
|
|
|
|
};
|
|
|
|
|
|
|
|
#define UTF16_ESC_NUM_DIGITS 4
|
|
|
|
#define HEX_ESC_NUM_DIGITS 2
|
|
|
|
#define BITS_PER_HEX_DIGIT 4
|
|
|
|
|
|
|
|
static const char kUserPref[] = "user_pref";
|
|
|
|
static const char kPref[] = "pref";
|
|
|
|
static const char kPrefSticky[] = "sticky_pref";
|
|
|
|
static const char kTrue[] = "true";
|
|
|
|
static const char kFalse[] = "false";
|
|
|
|
|
|
|
|
// This function will increase the size of the buffer owned by the given pref
|
|
|
|
// parse state. We currently use a simple doubling algorithm, but the only hard
|
|
|
|
// requirement is that it increase the buffer by at least the size of the
|
|
|
|
// aPS->mEscTmp buffer used for escape processing (currently 6 bytes).
|
|
|
|
//
|
|
|
|
// The buffer is used to store partial pref lines. It is freed when the parse
|
|
|
|
// state is destroyed.
|
|
|
|
//
|
|
|
|
// @param aPS
|
|
|
|
// parse state instance
|
|
|
|
//
|
|
|
|
// This function updates all pointers that reference an address within mLb
|
|
|
|
// since realloc may relocate the buffer.
|
|
|
|
//
|
|
|
|
// @return false if insufficient memory.
|
|
|
|
static bool
|
|
|
|
pref_GrowBuf(PrefParseState* aPS)
|
|
|
|
{
|
|
|
|
int bufLen, curPos, valPos;
|
|
|
|
|
|
|
|
bufLen = aPS->mLbEnd - aPS->mLb;
|
|
|
|
curPos = aPS->mLbCur - aPS->mLb;
|
|
|
|
valPos = aPS->mVb - aPS->mLb;
|
|
|
|
|
|
|
|
if (bufLen == 0) {
|
|
|
|
bufLen = 128; // default buffer size
|
|
|
|
} else {
|
|
|
|
bufLen <<= 1; // double buffer size
|
|
|
|
}
|
|
|
|
|
|
|
|
aPS->mLb = (char*)realloc(aPS->mLb, bufLen);
|
|
|
|
if (!aPS->mLb) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aPS->mLbCur = aPS->mLb + curPos;
|
|
|
|
aPS->mLbEnd = aPS->mLb + bufLen;
|
|
|
|
aPS->mVb = aPS->mLb + valPos;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report an error or a warning. If not specified, just dump to stderr.
|
|
|
|
static void
|
|
|
|
pref_ReportParseProblem(PrefParseState& aPS,
|
|
|
|
const char* aMessage,
|
|
|
|
int aLine,
|
|
|
|
bool aError)
|
|
|
|
{
|
|
|
|
if (aPS.mReporter) {
|
|
|
|
aPS.mReporter(aMessage, aLine, aError);
|
|
|
|
} else {
|
|
|
|
printf_stderr("**** Preference parsing %s (line %d) = %s **\n",
|
|
|
|
(aError ? "error" : "warning"),
|
|
|
|
aLine,
|
|
|
|
aMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 04:24:59 +00:00
|
|
|
// Initialize a PrefParseState instance.
|
|
|
|
//
|
|
|
|
// |aPS| is the PrefParseState instance.
|
|
|
|
// |aReader| is the PrefReader callback function, which will be called once for
|
|
|
|
// each preference name value pair extracted.
|
|
|
|
// |aReporter| is the PrefParseErrorReporter callback function, which will be
|
|
|
|
// called if we encounter any errors (stop) or warnings (continue) during
|
|
|
|
// parsing.
|
|
|
|
// |aClosure| is extra data passed to |aReader|.
|
|
|
|
static void
|
2017-10-11 09:31:35 +00:00
|
|
|
PREF_InitParseState(PrefParseState* aPS,
|
|
|
|
PrefReader aReader,
|
|
|
|
PrefParseErrorReporter aReporter,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
memset(aPS, 0, sizeof(*aPS));
|
|
|
|
aPS->mReader = aReader;
|
|
|
|
aPS->mClosure = aClosure;
|
|
|
|
aPS->mReporter = aReporter;
|
|
|
|
}
|
|
|
|
|
2017-10-13 04:24:59 +00:00
|
|
|
// Release any memory in use by the PrefParseState instance.
|
|
|
|
static void
|
2017-10-11 09:31:35 +00:00
|
|
|
PREF_FinalizeParseState(PrefParseState* aPS)
|
|
|
|
{
|
|
|
|
if (aPS->mLb) {
|
|
|
|
free(aPS->mLb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 04:24:59 +00:00
|
|
|
// Parse a buffer containing some portion of a preference file. This function
|
|
|
|
// may be called repeatedly as new data is made available. The PrefReader
|
|
|
|
// callback function passed PREF_InitParseState will be called as preference
|
|
|
|
// name value pairs are extracted from the data. Returns false if buffer
|
|
|
|
// contains malformed content.
|
|
|
|
//
|
2017-10-11 09:31:35 +00:00
|
|
|
// Pseudo-BNF
|
|
|
|
// ----------
|
|
|
|
// function = LJUNK function-name JUNK function-args
|
|
|
|
// function-name = "user_pref" | "pref" | "sticky_pref"
|
|
|
|
// function-args = "(" JUNK pref-name JUNK "," JUNK pref-value JUNK ")" JUNK ";"
|
|
|
|
// pref-name = quoted-string
|
|
|
|
// pref-value = quoted-string | "true" | "false" | integer-value
|
|
|
|
// JUNK = *(WS | comment-block | comment-line)
|
|
|
|
// LJUNK = *(WS | comment-block | comment-line | bcomment-line)
|
|
|
|
// WS = SP | HT | LF | VT | FF | CR
|
|
|
|
// SP = <US-ASCII SP, space (32)>
|
|
|
|
// HT = <US-ASCII HT, horizontal-tab (9)>
|
|
|
|
// LF = <US-ASCII LF, linefeed (10)>
|
|
|
|
// VT = <US-ASCII HT, vertical-tab (11)>
|
|
|
|
// FF = <US-ASCII FF, form-feed (12)>
|
|
|
|
// CR = <US-ASCII CR, carriage return (13)>
|
|
|
|
// comment-block = <C/C++ style comment block>
|
|
|
|
// comment-line = <C++ style comment line>
|
|
|
|
// bcomment-line = <bourne-shell style comment line>
|
|
|
|
//
|
2017-10-13 04:24:59 +00:00
|
|
|
static bool
|
2017-10-11 09:31:35 +00:00
|
|
|
PREF_ParseBuf(PrefParseState* aPS, const char* aBuf, int aBufLen)
|
|
|
|
{
|
|
|
|
const char* end;
|
|
|
|
char c;
|
|
|
|
char udigit;
|
|
|
|
int state;
|
|
|
|
|
|
|
|
// The line number is currently only used for the error/warning reporting.
|
|
|
|
int lineNum = 0;
|
|
|
|
|
|
|
|
state = aPS->mState;
|
|
|
|
for (end = aBuf + aBufLen; aBuf != end; ++aBuf) {
|
|
|
|
c = *aBuf;
|
|
|
|
if (c == '\r' || c == '\n' || c == 0x1A) {
|
|
|
|
lineNum++;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
// initial state
|
|
|
|
case PREF_PARSE_INIT:
|
|
|
|
if (aPS->mLbCur != aPS->mLb) { // reset state
|
|
|
|
aPS->mLbCur = aPS->mLb;
|
|
|
|
aPS->mVb = nullptr;
|
|
|
|
aPS->mVtype = PrefType::Invalid;
|
|
|
|
aPS->mIsDefault = false;
|
|
|
|
aPS->mIsStickyDefault = false;
|
|
|
|
}
|
|
|
|
switch (c) {
|
|
|
|
case '/': // begin comment block or line?
|
|
|
|
state = PREF_PARSE_COMMENT_MAYBE_START;
|
|
|
|
break;
|
|
|
|
case '#': // accept shell style comments
|
|
|
|
state = PREF_PARSE_UNTIL_EOL;
|
|
|
|
break;
|
|
|
|
case 'u': // indicating user_pref
|
|
|
|
case 's': // indicating sticky_pref
|
|
|
|
case 'p': // indicating pref
|
|
|
|
if (c == 'u') {
|
|
|
|
aPS->mStrMatch = kUserPref;
|
|
|
|
} else if (c == 's') {
|
|
|
|
aPS->mStrMatch = kPrefSticky;
|
|
|
|
} else {
|
|
|
|
aPS->mStrMatch = kPref;
|
|
|
|
}
|
|
|
|
aPS->mStrIndex = 1;
|
|
|
|
aPS->mNextState = PREF_PARSE_UNTIL_OPEN_PAREN;
|
|
|
|
state = PREF_PARSE_MATCH_STRING;
|
|
|
|
break;
|
|
|
|
// else skip char
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// string matching
|
|
|
|
case PREF_PARSE_MATCH_STRING:
|
|
|
|
if (c == aPS->mStrMatch[aPS->mStrIndex++]) {
|
|
|
|
// If we've matched all characters, then move to next state.
|
|
|
|
if (aPS->mStrMatch[aPS->mStrIndex] == '\0') {
|
|
|
|
state = aPS->mNextState;
|
|
|
|
aPS->mNextState = PREF_PARSE_INIT; // reset next state
|
|
|
|
}
|
|
|
|
// else wait for next char
|
|
|
|
} else {
|
|
|
|
pref_ReportParseProblem(*aPS, "non-matching string", lineNum, true);
|
|
|
|
NS_WARNING("malformed pref file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// quoted string parsing
|
|
|
|
case PREF_PARSE_QUOTED_STRING:
|
|
|
|
// we assume that the initial quote has already been consumed
|
|
|
|
if (aPS->mLbCur == aPS->mLbEnd && !pref_GrowBuf(aPS)) {
|
|
|
|
return false; // out of memory
|
|
|
|
}
|
|
|
|
if (c == '\\') {
|
|
|
|
state = PREF_PARSE_ESC_SEQUENCE;
|
|
|
|
} else if (c == aPS->mQuoteChar) {
|
|
|
|
*aPS->mLbCur++ = '\0';
|
|
|
|
state = aPS->mNextState;
|
|
|
|
aPS->mNextState = PREF_PARSE_INIT; // reset next state
|
|
|
|
} else {
|
|
|
|
*aPS->mLbCur++ = c;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// name parsing
|
|
|
|
case PREF_PARSE_UNTIL_NAME:
|
|
|
|
if (c == '\"' || c == '\'') {
|
|
|
|
aPS->mIsDefault =
|
|
|
|
(aPS->mStrMatch == kPref || aPS->mStrMatch == kPrefSticky);
|
|
|
|
aPS->mIsStickyDefault = (aPS->mStrMatch == kPrefSticky);
|
|
|
|
aPS->mQuoteChar = c;
|
|
|
|
aPS->mNextState = PREF_PARSE_UNTIL_COMMA; // return here when done
|
|
|
|
state = PREF_PARSE_QUOTED_STRING;
|
|
|
|
} else if (c == '/') { // allow embedded comment
|
|
|
|
aPS->mNextState = state; // return here when done with comment
|
|
|
|
state = PREF_PARSE_COMMENT_MAYBE_START;
|
|
|
|
} else if (!isspace(c)) {
|
|
|
|
pref_ReportParseProblem(
|
|
|
|
*aPS, "need space, comment or quote", lineNum, true);
|
|
|
|
NS_WARNING("malformed pref file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// parse until we find a comma separating name and value
|
|
|
|
case PREF_PARSE_UNTIL_COMMA:
|
|
|
|
if (c == ',') {
|
|
|
|
aPS->mVb = aPS->mLbCur;
|
|
|
|
state = PREF_PARSE_UNTIL_VALUE;
|
|
|
|
} else if (c == '/') { // allow embedded comment
|
|
|
|
aPS->mNextState = state; // return here when done with comment
|
|
|
|
state = PREF_PARSE_COMMENT_MAYBE_START;
|
|
|
|
} else if (!isspace(c)) {
|
|
|
|
pref_ReportParseProblem(
|
|
|
|
*aPS, "need space, comment or comma", lineNum, true);
|
|
|
|
NS_WARNING("malformed pref file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// value parsing
|
|
|
|
case PREF_PARSE_UNTIL_VALUE:
|
|
|
|
// The pref value type is unknown. So, we scan for the first character
|
|
|
|
// of the value, and determine the type from that.
|
|
|
|
if (c == '\"' || c == '\'') {
|
|
|
|
aPS->mVtype = PrefType::String;
|
|
|
|
aPS->mQuoteChar = c;
|
|
|
|
aPS->mNextState = PREF_PARSE_UNTIL_CLOSE_PAREN;
|
|
|
|
state = PREF_PARSE_QUOTED_STRING;
|
|
|
|
} else if (c == 't' || c == 'f') {
|
|
|
|
aPS->mVb = (char*)(c == 't' ? kTrue : kFalse);
|
|
|
|
aPS->mVtype = PrefType::Bool;
|
|
|
|
aPS->mStrMatch = aPS->mVb;
|
|
|
|
aPS->mStrIndex = 1;
|
|
|
|
aPS->mNextState = PREF_PARSE_UNTIL_CLOSE_PAREN;
|
|
|
|
state = PREF_PARSE_MATCH_STRING;
|
|
|
|
} else if (isdigit(c) || (c == '-') || (c == '+')) {
|
|
|
|
aPS->mVtype = PrefType::Int;
|
|
|
|
// write c to line buffer...
|
|
|
|
if (aPS->mLbCur == aPS->mLbEnd && !pref_GrowBuf(aPS)) {
|
|
|
|
return false; // out of memory
|
|
|
|
}
|
|
|
|
*aPS->mLbCur++ = c;
|
|
|
|
state = PREF_PARSE_INT_VALUE;
|
|
|
|
} else if (c == '/') { // allow embedded comment
|
|
|
|
aPS->mNextState = state; // return here when done with comment
|
|
|
|
state = PREF_PARSE_COMMENT_MAYBE_START;
|
|
|
|
} else if (!isspace(c)) {
|
|
|
|
pref_ReportParseProblem(
|
|
|
|
*aPS, "need value, comment or space", lineNum, true);
|
|
|
|
NS_WARNING("malformed pref file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PREF_PARSE_INT_VALUE:
|
|
|
|
// grow line buffer if necessary...
|
|
|
|
if (aPS->mLbCur == aPS->mLbEnd && !pref_GrowBuf(aPS)) {
|
|
|
|
return false; // out of memory
|
|
|
|
}
|
|
|
|
if (isdigit(c)) {
|
|
|
|
*aPS->mLbCur++ = c;
|
|
|
|
} else {
|
|
|
|
*aPS->mLbCur++ = '\0'; // stomp null terminator; we are done.
|
|
|
|
if (c == ')') {
|
|
|
|
state = PREF_PARSE_UNTIL_SEMICOLON;
|
|
|
|
} else if (c == '/') { // allow embedded comment
|
|
|
|
aPS->mNextState = PREF_PARSE_UNTIL_CLOSE_PAREN;
|
|
|
|
state = PREF_PARSE_COMMENT_MAYBE_START;
|
|
|
|
} else if (isspace(c)) {
|
|
|
|
state = PREF_PARSE_UNTIL_CLOSE_PAREN;
|
|
|
|
} else {
|
|
|
|
pref_ReportParseProblem(
|
|
|
|
*aPS, "while parsing integer", lineNum, true);
|
|
|
|
NS_WARNING("malformed pref file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// comment parsing
|
|
|
|
case PREF_PARSE_COMMENT_MAYBE_START:
|
|
|
|
switch (c) {
|
|
|
|
case '*': // comment block
|
|
|
|
state = PREF_PARSE_COMMENT_BLOCK;
|
|
|
|
break;
|
|
|
|
case '/': // comment line
|
|
|
|
state = PREF_PARSE_UNTIL_EOL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// pref file is malformed
|
|
|
|
pref_ReportParseProblem(
|
|
|
|
*aPS, "while parsing comment", lineNum, true);
|
|
|
|
NS_WARNING("malformed pref file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PREF_PARSE_COMMENT_BLOCK:
|
|
|
|
if (c == '*') {
|
|
|
|
state = PREF_PARSE_COMMENT_BLOCK_MAYBE_END;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PREF_PARSE_COMMENT_BLOCK_MAYBE_END:
|
|
|
|
switch (c) {
|
|
|
|
case '/':
|
|
|
|
state = aPS->mNextState;
|
|
|
|
aPS->mNextState = PREF_PARSE_INIT;
|
|
|
|
break;
|
|
|
|
case '*': // stay in this state
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
state = PREF_PARSE_COMMENT_BLOCK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// string escape sequence parsing
|
|
|
|
case PREF_PARSE_ESC_SEQUENCE:
|
|
|
|
// It's not necessary to resize the buffer here since we should be
|
|
|
|
// writing only one character and the resize check would have been done
|
|
|
|
// for us in the previous state.
|
|
|
|
switch (c) {
|
|
|
|
case '\"':
|
|
|
|
case '\'':
|
|
|
|
case '\\':
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
c = '\r';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
c = '\n';
|
|
|
|
break;
|
|
|
|
case 'x': // hex escape -- always interpreted as Latin-1
|
|
|
|
case 'u': // UTF16 escape
|
|
|
|
aPS->mEscTmp[0] = c;
|
|
|
|
aPS->mEscLen = 1;
|
|
|
|
aPS->mUtf16[0] = aPS->mUtf16[1] = 0;
|
|
|
|
aPS->mStrIndex =
|
|
|
|
(c == 'x') ? HEX_ESC_NUM_DIGITS : UTF16_ESC_NUM_DIGITS;
|
|
|
|
state = PREF_PARSE_HEX_ESCAPE;
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
pref_ReportParseProblem(
|
|
|
|
*aPS, "preserving unexpected JS escape sequence", lineNum, false);
|
|
|
|
NS_WARNING("preserving unexpected JS escape sequence");
|
|
|
|
// Invalid escape sequence so we do have to write more than one
|
|
|
|
// character. Grow line buffer if necessary...
|
|
|
|
if ((aPS->mLbCur + 1) == aPS->mLbEnd && !pref_GrowBuf(aPS)) {
|
|
|
|
return false; // out of memory
|
|
|
|
}
|
|
|
|
*aPS->mLbCur++ = '\\'; // preserve the escape sequence
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*aPS->mLbCur++ = c;
|
|
|
|
state = PREF_PARSE_QUOTED_STRING;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// parsing a hex (\xHH) or mUtf16 escape (\uHHHH)
|
|
|
|
case PREF_PARSE_HEX_ESCAPE:
|
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
udigit = (c - '0');
|
|
|
|
} else if (c >= 'A' && c <= 'F') {
|
|
|
|
udigit = (c - 'A') + 10;
|
|
|
|
} else if (c >= 'a' && c <= 'f') {
|
|
|
|
udigit = (c - 'a') + 10;
|
|
|
|
} else {
|
|
|
|
// bad escape sequence found, write out broken escape as-is
|
|
|
|
pref_ReportParseProblem(*aPS,
|
|
|
|
"preserving invalid or incomplete hex escape",
|
|
|
|
lineNum,
|
|
|
|
false);
|
|
|
|
NS_WARNING("preserving invalid or incomplete hex escape");
|
|
|
|
*aPS->mLbCur++ = '\\'; // original escape slash
|
|
|
|
if ((aPS->mLbCur + aPS->mEscLen) >= aPS->mLbEnd &&
|
|
|
|
!pref_GrowBuf(aPS)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < aPS->mEscLen; ++i) {
|
|
|
|
*aPS->mLbCur++ = aPS->mEscTmp[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push the non-hex character back for re-parsing. (++aBuf at the top
|
|
|
|
// of the loop keeps this safe.)
|
|
|
|
--aBuf;
|
|
|
|
state = PREF_PARSE_QUOTED_STRING;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// have a digit
|
|
|
|
aPS->mEscTmp[aPS->mEscLen++] = c; // preserve it
|
|
|
|
aPS->mUtf16[1] <<= BITS_PER_HEX_DIGIT;
|
|
|
|
aPS->mUtf16[1] |= udigit;
|
|
|
|
aPS->mStrIndex--;
|
|
|
|
if (aPS->mStrIndex == 0) {
|
|
|
|
// we have the full escape, convert to UTF8
|
|
|
|
int utf16len = 0;
|
|
|
|
if (aPS->mUtf16[0]) {
|
|
|
|
// already have a high surrogate, this is a two char seq
|
|
|
|
utf16len = 2;
|
|
|
|
} else if (0xD800 == (0xFC00 & aPS->mUtf16[1])) {
|
|
|
|
// a high surrogate, can't convert until we have the low
|
|
|
|
aPS->mUtf16[0] = aPS->mUtf16[1];
|
|
|
|
aPS->mUtf16[1] = 0;
|
|
|
|
state = PREF_PARSE_UTF16_LOW_SURROGATE;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// a single mUtf16 character
|
|
|
|
aPS->mUtf16[0] = aPS->mUtf16[1];
|
|
|
|
utf16len = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The actual conversion.
|
|
|
|
// Make sure there's room, 6 bytes is max utf8 len (in theory; 4
|
|
|
|
// bytes covers the actual mUtf16 range).
|
|
|
|
if (aPS->mLbCur + 6 >= aPS->mLbEnd && !pref_GrowBuf(aPS)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConvertUTF16toUTF8 converter(aPS->mLbCur);
|
|
|
|
converter.write(aPS->mUtf16, utf16len);
|
|
|
|
aPS->mLbCur += converter.Size();
|
|
|
|
state = PREF_PARSE_QUOTED_STRING;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// looking for beginning of mUtf16 low surrogate
|
|
|
|
case PREF_PARSE_UTF16_LOW_SURROGATE:
|
|
|
|
if (aPS->mStrIndex == 0 && c == '\\') {
|
|
|
|
++aPS->mStrIndex;
|
|
|
|
} else if (aPS->mStrIndex == 1 && c == 'u') {
|
|
|
|
// escape sequence is correct, now parse hex
|
|
|
|
aPS->mStrIndex = UTF16_ESC_NUM_DIGITS;
|
|
|
|
aPS->mEscTmp[0] = 'u';
|
|
|
|
aPS->mEscLen = 1;
|
|
|
|
state = PREF_PARSE_HEX_ESCAPE;
|
|
|
|
} else {
|
|
|
|
// Didn't find expected low surrogate. Ignore high surrogate (it
|
|
|
|
// would just get converted to nothing anyway) and start over with
|
|
|
|
// this character.
|
|
|
|
--aBuf;
|
|
|
|
if (aPS->mStrIndex == 1) {
|
|
|
|
state = PREF_PARSE_ESC_SEQUENCE;
|
|
|
|
} else {
|
|
|
|
state = PREF_PARSE_QUOTED_STRING;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// function open and close parsing
|
|
|
|
case PREF_PARSE_UNTIL_OPEN_PAREN:
|
|
|
|
// tolerate only whitespace and embedded comments
|
|
|
|
if (c == '(') {
|
|
|
|
state = PREF_PARSE_UNTIL_NAME;
|
|
|
|
} else if (c == '/') {
|
|
|
|
aPS->mNextState = state; // return here when done with comment
|
|
|
|
state = PREF_PARSE_COMMENT_MAYBE_START;
|
|
|
|
} else if (!isspace(c)) {
|
|
|
|
pref_ReportParseProblem(
|
|
|
|
*aPS, "need space, comment or open parentheses", lineNum, true);
|
|
|
|
NS_WARNING("malformed pref file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PREF_PARSE_UNTIL_CLOSE_PAREN:
|
|
|
|
// tolerate only whitespace and embedded comments
|
|
|
|
if (c == ')') {
|
|
|
|
state = PREF_PARSE_UNTIL_SEMICOLON;
|
|
|
|
} else if (c == '/') {
|
|
|
|
aPS->mNextState = state; // return here when done with comment
|
|
|
|
state = PREF_PARSE_COMMENT_MAYBE_START;
|
|
|
|
} else if (!isspace(c)) {
|
|
|
|
pref_ReportParseProblem(
|
|
|
|
*aPS, "need space, comment or closing parentheses", lineNum, true);
|
|
|
|
NS_WARNING("malformed pref file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// function terminator ';' parsing
|
|
|
|
case PREF_PARSE_UNTIL_SEMICOLON:
|
|
|
|
// tolerate only whitespace and embedded comments
|
|
|
|
if (c == ';') {
|
2017-10-26 04:32:15 +00:00
|
|
|
|
|
|
|
PrefValue value;
|
|
|
|
|
|
|
|
switch (aPS->mVtype) {
|
|
|
|
case PrefType::String:
|
|
|
|
value.mStringVal = aPS->mVb;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PrefType::Int:
|
|
|
|
if ((aPS->mVb[0] == '-' || aPS->mVb[0] == '+') &&
|
|
|
|
aPS->mVb[1] == '\0') {
|
|
|
|
pref_ReportParseProblem(*aPS, "invalid integer value", 0, true);
|
|
|
|
NS_WARNING("malformed integer value");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
value.mIntVal = atoi(aPS->mVb);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PrefType::Bool:
|
|
|
|
value.mBoolVal = (aPS->mVb == kTrue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2017-10-11 09:31:35 +00:00
|
|
|
}
|
2017-10-26 04:32:15 +00:00
|
|
|
|
|
|
|
// We've extracted a complete name/value pair.
|
|
|
|
aPS->mReader(aPS->mClosure,
|
|
|
|
aPS->mLb,
|
|
|
|
value,
|
|
|
|
aPS->mVtype,
|
|
|
|
aPS->mIsDefault,
|
|
|
|
aPS->mIsStickyDefault);
|
|
|
|
|
2017-10-11 09:31:35 +00:00
|
|
|
state = PREF_PARSE_INIT;
|
|
|
|
} else if (c == '/') {
|
|
|
|
aPS->mNextState = state; // return here when done with comment
|
|
|
|
state = PREF_PARSE_COMMENT_MAYBE_START;
|
|
|
|
} else if (!isspace(c)) {
|
|
|
|
pref_ReportParseProblem(
|
|
|
|
*aPS, "need space, comment or semicolon", lineNum, true);
|
|
|
|
NS_WARNING("malformed pref file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// eol parsing
|
|
|
|
case PREF_PARSE_UNTIL_EOL:
|
|
|
|
// Need to handle mac, unix, or dos line endings. PREF_PARSE_INIT will
|
|
|
|
// eat the next \n in case we have \r\n.
|
|
|
|
if (c == '\r' || c == '\n' || c == 0x1A) {
|
|
|
|
state = aPS->mNextState;
|
|
|
|
aPS->mNextState = PREF_PARSE_INIT; // reset next state
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
aPS->mState = state;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-11 09:31:32 +00:00
|
|
|
//===========================================================================
|
|
|
|
// nsPrefBranch et al.
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
class PreferenceServiceReporter;
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
class PrefCallback : public PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
friend class mozilla::PreferenceServiceReporter;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef PrefCallback* KeyType;
|
|
|
|
typedef const PrefCallback* KeyTypePointer;
|
|
|
|
|
|
|
|
static const PrefCallback* KeyToPointer(PrefCallback* aKey) { return aKey; }
|
|
|
|
|
|
|
|
static PLDHashNumber HashKey(const PrefCallback* aKey)
|
|
|
|
{
|
|
|
|
uint32_t hash = mozilla::HashString(aKey->mDomain);
|
|
|
|
return mozilla::AddToHash(hash, aKey->mCanonical);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Create a PrefCallback with a strong reference to its observer.
|
|
|
|
PrefCallback(const char* aDomain,
|
|
|
|
nsIObserver* aObserver,
|
|
|
|
nsPrefBranch* aBranch)
|
|
|
|
: mDomain(aDomain)
|
|
|
|
, mBranch(aBranch)
|
|
|
|
, mWeakRef(nullptr)
|
|
|
|
, mStrongRef(aObserver)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(PrefCallback);
|
|
|
|
nsCOMPtr<nsISupports> canonical = do_QueryInterface(aObserver);
|
|
|
|
mCanonical = canonical;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a PrefCallback with a weak reference to its observer.
|
|
|
|
PrefCallback(const char* aDomain,
|
|
|
|
nsISupportsWeakReference* aObserver,
|
|
|
|
nsPrefBranch* aBranch)
|
|
|
|
: mDomain(aDomain)
|
|
|
|
, mBranch(aBranch)
|
|
|
|
, mWeakRef(do_GetWeakReference(aObserver))
|
|
|
|
, mStrongRef(nullptr)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(PrefCallback);
|
|
|
|
nsCOMPtr<nsISupports> canonical = do_QueryInterface(aObserver);
|
|
|
|
mCanonical = canonical;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy constructor needs to be explicit or the linker complains.
|
|
|
|
explicit PrefCallback(const PrefCallback*& aCopy)
|
|
|
|
: mDomain(aCopy->mDomain)
|
|
|
|
, mBranch(aCopy->mBranch)
|
|
|
|
, mWeakRef(aCopy->mWeakRef)
|
|
|
|
, mStrongRef(aCopy->mStrongRef)
|
|
|
|
, mCanonical(aCopy->mCanonical)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(PrefCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
~PrefCallback() { MOZ_COUNT_DTOR(PrefCallback); }
|
|
|
|
|
|
|
|
bool KeyEquals(const PrefCallback* aKey) const
|
|
|
|
{
|
|
|
|
// We want to be able to look up a weakly-referencing PrefCallback after
|
|
|
|
// its observer has died so we can remove it from the table. Once the
|
|
|
|
// callback's observer dies, its canonical pointer is stale -- in
|
|
|
|
// particular, we may have allocated a new observer in the same spot in
|
|
|
|
// memory! So we can't just compare canonical pointers to determine whether
|
|
|
|
// aKey refers to the same observer as this.
|
|
|
|
//
|
|
|
|
// Our workaround is based on the way we use this hashtable: When we ask
|
|
|
|
// the hashtable to remove a PrefCallback whose weak reference has expired,
|
|
|
|
// we use as the key for removal the same object as was inserted into the
|
|
|
|
// hashtable. Thus we can say that if one of the keys' weak references has
|
|
|
|
// expired, the two keys are equal iff they're the same object.
|
|
|
|
|
|
|
|
if (IsExpired() || aKey->IsExpired()) {
|
|
|
|
return this == aKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mCanonical != aKey->mCanonical) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mDomain.Equals(aKey->mDomain);
|
|
|
|
}
|
|
|
|
|
|
|
|
PrefCallback* GetKey() const { return const_cast<PrefCallback*>(this); }
|
|
|
|
|
|
|
|
// Get a reference to the callback's observer, or null if the observer was
|
|
|
|
// weakly referenced and has been destroyed.
|
|
|
|
already_AddRefed<nsIObserver> GetObserver() const
|
|
|
|
{
|
|
|
|
if (!IsWeak()) {
|
|
|
|
nsCOMPtr<nsIObserver> copy = mStrongRef;
|
|
|
|
return copy.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserver> observer = do_QueryReferent(mWeakRef);
|
|
|
|
return observer.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsCString& GetDomain() const { return mDomain; }
|
|
|
|
|
|
|
|
nsPrefBranch* GetPrefBranch() const { return mBranch; }
|
|
|
|
|
|
|
|
// Has this callback's weak reference died?
|
|
|
|
bool IsExpired() const
|
|
|
|
{
|
|
|
|
if (!IsWeak())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserver> observer(do_QueryReferent(mWeakRef));
|
|
|
|
return !observer;
|
|
|
|
}
|
|
|
|
|
2017-11-16 04:55:58 +00:00
|
|
|
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t n = aMallocSizeOf(this);
|
|
|
|
n += mDomain.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
|
|
|
|
|
|
|
// All the other fields are non-owning pointers, so we don't measure them.
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2017-10-11 09:31:32 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
ALLOW_MEMMOVE = true
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCString mDomain;
|
|
|
|
nsPrefBranch* mBranch;
|
|
|
|
|
|
|
|
// Exactly one of mWeakRef and mStrongRef should be non-null.
|
|
|
|
nsWeakPtr mWeakRef;
|
|
|
|
nsCOMPtr<nsIObserver> mStrongRef;
|
|
|
|
|
|
|
|
// We need a canonical nsISupports pointer, per bug 578392.
|
|
|
|
nsISupports* mCanonical;
|
|
|
|
|
|
|
|
bool IsWeak() const { return !!mWeakRef; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class nsPrefBranch final
|
|
|
|
: public nsIPrefBranch
|
|
|
|
, public nsIObserver
|
|
|
|
, public nsSupportsWeakReference
|
|
|
|
{
|
|
|
|
friend class mozilla::PreferenceServiceReporter;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIPREFBRANCH
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
2017-11-14 08:06:02 +00:00
|
|
|
nsPrefBranch(const char* aPrefRoot, PrefValueKind aKind);
|
2017-10-11 09:31:32 +00:00
|
|
|
nsPrefBranch() = delete;
|
|
|
|
|
|
|
|
int32_t GetRootLength() const { return mPrefRoot.Length(); }
|
|
|
|
|
|
|
|
static void NotifyObserver(const char* aNewpref, void* aData);
|
|
|
|
|
2017-11-16 04:55:58 +00:00
|
|
|
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
|
2017-10-11 09:31:32 +00:00
|
|
|
|
|
|
|
static void ReportToConsole(const nsAString& aMessage);
|
|
|
|
|
2017-10-26 05:26:24 +00:00
|
|
|
private:
|
2017-10-11 09:31:32 +00:00
|
|
|
// Helper class for either returning a raw cstring or nsCString.
|
|
|
|
typedef mozilla::Variant<const char*, const nsCString> PrefNameBase;
|
|
|
|
class PrefName : public PrefNameBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit PrefName(const char* aName)
|
|
|
|
: PrefNameBase(aName)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
explicit PrefName(const nsCString& aName)
|
|
|
|
: PrefNameBase(aName)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use default move constructors, disallow copy constructors.
|
|
|
|
PrefName(PrefName&& aOther) = default;
|
|
|
|
PrefName& operator=(PrefName&& aOther) = default;
|
|
|
|
PrefName(const PrefName&) = delete;
|
|
|
|
PrefName& operator=(const PrefName&) = delete;
|
|
|
|
|
|
|
|
struct PtrMatcher
|
|
|
|
{
|
|
|
|
static const char* match(const char* aVal) { return aVal; }
|
|
|
|
static const char* match(const nsCString& aVal) { return aVal.get(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LenMatcher
|
|
|
|
{
|
|
|
|
static size_t match(const char* aVal) { return strlen(aVal); }
|
|
|
|
static size_t match(const nsCString& aVal) { return aVal.Length(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
const char* get() const
|
|
|
|
{
|
|
|
|
static PtrMatcher m;
|
|
|
|
return match(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Length() const
|
|
|
|
{
|
|
|
|
static LenMatcher m;
|
|
|
|
return match(m);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual ~nsPrefBranch();
|
|
|
|
|
|
|
|
nsresult GetDefaultFromPropertiesFile(const char* aPrefName,
|
|
|
|
nsAString& aReturn);
|
|
|
|
|
|
|
|
// As SetCharPref, but without any check on the length of |aValue|.
|
2017-11-14 08:06:02 +00:00
|
|
|
nsresult SetCharPrefNoLengthCheck(const char* aPrefName,
|
|
|
|
const nsACString& aValue);
|
2017-10-11 09:31:32 +00:00
|
|
|
|
|
|
|
// Reject strings that are more than 1Mb, warn if strings are more than 16kb.
|
|
|
|
nsresult CheckSanityOfStringLength(const char* aPrefName,
|
|
|
|
const nsAString& aValue);
|
|
|
|
nsresult CheckSanityOfStringLength(const char* aPrefName,
|
|
|
|
const nsACString& aValue);
|
|
|
|
nsresult CheckSanityOfStringLength(const char* aPrefName,
|
|
|
|
const uint32_t aLength);
|
|
|
|
|
|
|
|
void RemoveExpiredCallback(PrefCallback* aCallback);
|
|
|
|
|
|
|
|
PrefName GetPrefName(const char* aPrefName) const;
|
|
|
|
|
|
|
|
void FreeObserverList(void);
|
|
|
|
|
|
|
|
const nsCString mPrefRoot;
|
2017-11-14 08:06:02 +00:00
|
|
|
PrefValueKind mKind;
|
2017-10-11 09:31:32 +00:00
|
|
|
|
|
|
|
bool mFreeingObserverList;
|
|
|
|
nsClassHashtable<PrefCallback, PrefCallback> mObservers;
|
|
|
|
};
|
|
|
|
|
2017-10-24 23:22:37 +00:00
|
|
|
class nsPrefLocalizedString final : public nsIPrefLocalizedString
|
2017-10-11 09:31:32 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsPrefLocalizedString();
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_FORWARD_NSISUPPORTSPRIMITIVE(mUnicodeString->)
|
Bug 1409635 (part 2) - Fix up nsIPrefLocalizedString. r=froydnj.
nsIPrefLocalizedString is meant to be a wrapper for nsISupportsString,
basically identical but with a different identifier. But it's not a
sub-interface of nsISupportsString. Instead it (almost) duplicates
nsISupportsString's internals.
Specifically, nsISupportsString has `attribute AString data`, resulting in
these C++ methods:
> NS_IMETHOD GetData(nsAString& aData)
> NS_IMETHOD SetData(const nsAString& aData)
nsIPrefLocalizedString has `attribute wstring data`, resulting in these C++
methods:
> NS_IMETHOD GetData(char16_t** aData)
> NS_IMETHOD SetData(const char16_t* aData)
Then we have nsPrefLocalizedString, the concrete subclass of
nsIPrefLocalizedString. It implements the AString methods via
NS_FORWARD_NSISUPPORTSSTRING, which forwards to mUnicodeString. It also
implements the wstring methods explicitly, and they just call the AString
methods. It's all a bit of a mess.
(Both interfaces also have `wstring toString()`. The forwarding works more
smoothly for that method.)
This patch changes nsIPrefLocalizedString so it is a trivial sub-interface of
nsISupportsString. This change eliminates the need for the wstring methods, so
the patch removes them as well. The net result is we have less code, and fewer
conversions between C strings and Gecko strings. The patch also merges the
nsISupportsString and nsIPrefLocalizedString cases in
nsPrefBranch::SetComplexValue(), because they are now identical. (The
nsISupportsString and nsIPrefLocalizedString cases in
nsPrefBranch::GetComplexValue() remain distinct; indeed, that's the whole
reason for having them as separate interfaces.)
2017-10-18 05:51:56 +00:00
|
|
|
NS_FORWARD_NSISUPPORTSSTRING(mUnicodeString->)
|
2017-10-11 09:31:32 +00:00
|
|
|
|
|
|
|
nsresult Init();
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~nsPrefLocalizedString();
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupportsString> mUnicodeString;
|
|
|
|
};
|
|
|
|
|
|
|
|
class nsRelativeFilePref : public nsIRelativeFilePref
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIRELATIVEFILEPREF
|
|
|
|
|
|
|
|
nsRelativeFilePref();
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~nsRelativeFilePref();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> mFile;
|
|
|
|
nsCString mRelativeToKey;
|
|
|
|
};
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// nsPrefBranch
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2017-11-14 08:06:02 +00:00
|
|
|
nsPrefBranch::nsPrefBranch(const char* aPrefRoot, PrefValueKind aKind)
|
2017-10-11 09:31:32 +00:00
|
|
|
: mPrefRoot(aPrefRoot)
|
2017-11-14 08:06:02 +00:00
|
|
|
, mKind(aKind)
|
2017-10-11 09:31:32 +00:00
|
|
|
, mFreeingObserverList(false)
|
|
|
|
, mObservers()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
++mRefCnt; // must be > 0 when we call this, or we'll get deleted!
|
|
|
|
|
|
|
|
// Add weakly so we don't have to clean up at shutdown.
|
|
|
|
observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, true);
|
|
|
|
--mRefCnt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPrefBranch::~nsPrefBranch()
|
|
|
|
{
|
|
|
|
FreeObserverList();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsPrefBranch)
|
|
|
|
NS_IMPL_RELEASE(nsPrefBranch)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsPrefBranch)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIPrefBranch)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIPrefBranch)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-10-26 23:30:33 +00:00
|
|
|
nsPrefBranch::GetRoot(nsACString& aRoot)
|
2017-10-11 09:31:32 +00:00
|
|
|
{
|
2017-10-26 23:30:33 +00:00
|
|
|
aRoot = mPrefRoot;
|
2017-10-11 09:31:32 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::GetPrefType(const char* aPrefName, int32_t* aRetVal)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aPrefName);
|
2017-10-26 05:14:00 +00:00
|
|
|
|
2017-10-11 09:31:32 +00:00
|
|
|
const PrefName& pref = GetPrefName(aPrefName);
|
2017-10-26 05:14:00 +00:00
|
|
|
PrefType type = PrefType::Invalid;
|
|
|
|
if (gHashTable) {
|
|
|
|
PrefHashEntry* entry = pref_HashTableLookup(pref.get());
|
|
|
|
if (entry) {
|
|
|
|
type = entry->mPrefFlags.GetPrefType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
2017-10-11 09:31:32 +00:00
|
|
|
case PrefType::String:
|
|
|
|
*aRetVal = PREF_STRING;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PrefType::Int:
|
|
|
|
*aRetVal = PREF_INT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PrefType::Bool:
|
|
|
|
*aRetVal = PREF_BOOL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PrefType::Invalid:
|
|
|
|
default:
|
|
|
|
*aRetVal = PREF_INVALID;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::GetBoolPrefWithDefault(const char* aPrefName,
|
|
|
|
bool aDefaultValue,
|
|
|
|
uint8_t aArgc,
|
|
|
|
bool* aRetVal)
|
|
|
|
{
|
|
|
|
nsresult rv = GetBoolPref(aPrefName, aRetVal);
|
|
|
|
if (NS_FAILED(rv) && aArgc == 1) {
|
|
|
|
*aRetVal = aDefaultValue;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::GetBoolPref(const char* aPrefName, bool* aRetVal)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aPrefName);
|
|
|
|
|
|
|
|
const PrefName& pref = GetPrefName(aPrefName);
|
2017-11-14 08:06:02 +00:00
|
|
|
return Preferences::GetBool(pref.get(), aRetVal, mKind);
|
2017-10-11 09:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::SetBoolPref(const char* aPrefName, bool aValue)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aPrefName);
|
|
|
|
|
|
|
|
const PrefName& pref = GetPrefName(aPrefName);
|
2017-11-14 08:06:02 +00:00
|
|
|
return Preferences::SetBool(pref.get(), aValue, mKind);
|
2017-10-11 09:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::GetFloatPrefWithDefault(const char* aPrefName,
|
|
|
|
float aDefaultValue,
|
|
|
|
uint8_t aArgc,
|
|
|
|
float* aRetVal)
|
|
|
|
{
|
|
|
|
nsresult rv = GetFloatPref(aPrefName, aRetVal);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) && aArgc == 1) {
|
|
|
|
*aRetVal = aDefaultValue;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::GetFloatPref(const char* aPrefName, float* aRetVal)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aPrefName);
|
|
|
|
|
|
|
|
nsAutoCString stringVal;
|
2017-10-26 23:30:33 +00:00
|
|
|
nsresult rv = GetCharPref(aPrefName, stringVal);
|
2017-10-11 09:31:32 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*aRetVal = stringVal.ToFloat(&rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::GetCharPrefWithDefault(const char* aPrefName,
|
2017-10-26 23:30:33 +00:00
|
|
|
const nsACString& aDefaultValue,
|
2017-10-11 09:31:32 +00:00
|
|
|
uint8_t aArgc,
|
2017-10-26 23:30:33 +00:00
|
|
|
nsACString& aRetVal)
|
2017-10-11 09:31:32 +00:00
|
|
|
{
|
|
|
|
nsresult rv = GetCharPref(aPrefName, aRetVal);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) && aArgc == 1) {
|
2017-10-26 23:30:33 +00:00
|
|
|
aRetVal = aDefaultValue;
|
2017-10-11 09:31:32 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-10-26 23:30:33 +00:00
|
|
|
nsPrefBranch::GetCharPref(const char* aPrefName, nsACString& aRetVal)
|
2017-10-11 09:31:32 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aPrefName);
|
2017-11-14 08:06:02 +00:00
|
|
|
|
2017-10-11 09:31:32 +00:00
|
|
|
const PrefName& pref = GetPrefName(aPrefName);
|
2017-11-14 08:06:02 +00:00
|
|
|
return Preferences::GetCString(pref.get(), aRetVal, mKind);
|
2017-10-11 09:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-10-26 23:30:33 +00:00
|
|
|
nsPrefBranch::SetCharPref(const char* aPrefName, const nsACString& aValue)
|
2017-10-11 09:31:32 +00:00
|
|
|
{
|
|
|
|
nsresult rv = CheckSanityOfStringLength(aPrefName, aValue);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2017-11-14 08:06:02 +00:00
|
|
|
return SetCharPrefNoLengthCheck(aPrefName, aValue);
|
2017-10-11 09:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2017-11-14 08:06:02 +00:00
|
|
|
nsPrefBranch::SetCharPrefNoLengthCheck(const char* aPrefName,
|
|
|
|
const nsACString& aValue)
|
2017-10-11 09:31:32 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aPrefName);
|
|
|
|
|
|
|
|
const PrefName& pref = GetPrefName(aPrefName);
|
2017-11-14 08:06:02 +00:00
|
|
|
return Preferences::SetCString(pref.get(), aValue, mKind);
|
2017-10-11 09:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::GetStringPref(const char* aPrefName,
|
|
|
|
const nsACString& aDefaultValue,
|
|
|
|
uint8_t aArgc,
|
|
|
|
nsACString& aRetVal)
|
|
|
|
{
|
|
|
|
nsCString utf8String;
|
2017-10-26 23:30:33 +00:00
|
|
|
nsresult rv = GetCharPref(aPrefName, utf8String);
|
2017-10-11 09:31:32 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
aRetVal = utf8String;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aArgc == 1) {
|
|
|
|
aRetVal = aDefaultValue;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::SetStringPref(const char* aPrefName, const nsACString& aValue)
|
|
|
|
{
|
|
|
|
nsresult rv = CheckSanityOfStringLength(aPrefName, aValue);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-11-14 08:06:02 +00:00
|
|
|
return SetCharPrefNoLengthCheck(aPrefName, aValue);
|
2017-10-11 09:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::GetIntPrefWithDefault(const char* aPrefName,
|
|
|
|
int32_t aDefaultValue,
|
|
|
|
uint8_t aArgc,
|
|
|
|
int32_t* aRetVal)
|
|
|
|
{
|
|
|
|
nsresult rv = GetIntPref(aPrefName, aRetVal);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) && aArgc == 1) {
|
|
|
|
*aRetVal = aDefaultValue;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::GetIntPref(const char* aPrefName, int32_t* aRetVal)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aPrefName);
|
|
|
|
const PrefName& pref = GetPrefName(aPrefName);
|
2017-11-14 08:06:02 +00:00
|
|
|
return Preferences::GetInt(pref.get(), aRetVal, mKind);
|
2017-10-11 09:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::SetIntPref(const char* aPrefName, int32_t aValue)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aPrefName);
|
2017-11-14 08:06:02 +00:00
|
|
|
|
2017-10-11 09:31:32 +00:00
|
|
|
const PrefName& pref = GetPrefName(aPrefName);
|
2017-11-14 08:06:02 +00:00
|
|
|
return Preferences::SetInt(pref.get(), aValue, mKind);
|
2017-10-11 09:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::GetComplexValue(const char* aPrefName,
|
|
|
|
const nsIID& aType,
|
|
|
|
void** aRetVal)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aPrefName);
|
|
|
|
|
|
|
|
nsresult rv;
|
2017-10-26 23:30:33 +00:00
|
|
|
nsAutoCString utf8String;
|
2017-10-11 09:31:32 +00:00
|
|
|
|
2017-11-12 22:19:55 +00:00
|
|
|
// We have to do this one first because it's different to all the rest.
|
2017-10-11 09:31:32 +00:00
|
|
|
if (aType.Equals(NS_GET_IID(nsIPrefLocalizedString))) {
|
|
|
|
nsCOMPtr<nsIPrefLocalizedString> theString(
|
|
|
|
do_CreateInstance(NS_PREFLOCALIZEDSTRING_CONTRACTID, &rv));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PrefName& pref = GetPrefName(aPrefName);
|
|
|
|
bool bNeedDefault = false;
|
|
|
|
|
2017-11-14 08:06:02 +00:00
|
|
|
if (mKind == PrefValueKind::Default) {
|
2017-10-11 09:31:32 +00:00
|
|
|
bNeedDefault = true;
|
|
|
|
} else {
|
|
|
|
// if there is no user (or locked) value
|
|
|
|
if (!PREF_HasUserPref(pref.get()) && !PREF_PrefIsLocked(pref.get())) {
|
|
|
|
bNeedDefault = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we need to fetch the default value, do that instead, otherwise use the
|
|
|
|
// value we pulled in at the top of this function
|
|
|
|
if (bNeedDefault) {
|
|
|
|
nsAutoString utf16String;
|
|
|
|
rv = GetDefaultFromPropertiesFile(pref.get(), utf16String);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
Bug 1409635 (part 2) - Fix up nsIPrefLocalizedString. r=froydnj.
nsIPrefLocalizedString is meant to be a wrapper for nsISupportsString,
basically identical but with a different identifier. But it's not a
sub-interface of nsISupportsString. Instead it (almost) duplicates
nsISupportsString's internals.
Specifically, nsISupportsString has `attribute AString data`, resulting in
these C++ methods:
> NS_IMETHOD GetData(nsAString& aData)
> NS_IMETHOD SetData(const nsAString& aData)
nsIPrefLocalizedString has `attribute wstring data`, resulting in these C++
methods:
> NS_IMETHOD GetData(char16_t** aData)
> NS_IMETHOD SetData(const char16_t* aData)
Then we have nsPrefLocalizedString, the concrete subclass of
nsIPrefLocalizedString. It implements the AString methods via
NS_FORWARD_NSISUPPORTSSTRING, which forwards to mUnicodeString. It also
implements the wstring methods explicitly, and they just call the AString
methods. It's all a bit of a mess.
(Both interfaces also have `wstring toString()`. The forwarding works more
smoothly for that method.)
This patch changes nsIPrefLocalizedString so it is a trivial sub-interface of
nsISupportsString. This change eliminates the need for the wstring methods, so
the patch removes them as well. The net result is we have less code, and fewer
conversions between C strings and Gecko strings. The patch also merges the
nsISupportsString and nsIPrefLocalizedString cases in
nsPrefBranch::SetComplexValue(), because they are now identical. (The
nsISupportsString and nsIPrefLocalizedString cases in
nsPrefBranch::GetComplexValue() remain distinct; indeed, that's the whole
reason for having them as separate interfaces.)
2017-10-18 05:51:56 +00:00
|
|
|
theString->SetData(utf16String);
|
2017-10-11 09:31:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-10-26 23:30:33 +00:00
|
|
|
rv = GetCharPref(aPrefName, utf8String);
|
2017-10-11 09:31:32 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
Bug 1409635 (part 2) - Fix up nsIPrefLocalizedString. r=froydnj.
nsIPrefLocalizedString is meant to be a wrapper for nsISupportsString,
basically identical but with a different identifier. But it's not a
sub-interface of nsISupportsString. Instead it (almost) duplicates
nsISupportsString's internals.
Specifically, nsISupportsString has `attribute AString data`, resulting in
these C++ methods:
> NS_IMETHOD GetData(nsAString& aData)
> NS_IMETHOD SetData(const nsAString& aData)
nsIPrefLocalizedString has `attribute wstring data`, resulting in these C++
methods:
> NS_IMETHOD GetData(char16_t** aData)
> NS_IMETHOD SetData(const char16_t* aData)
Then we have nsPrefLocalizedString, the concrete subclass of
nsIPrefLocalizedString. It implements the AString methods via
NS_FORWARD_NSISUPPORTSSTRING, which forwards to mUnicodeString. It also
implements the wstring methods explicitly, and they just call the AString
methods. It's all a bit of a mess.
(Both interfaces also have `wstring toString()`. The forwarding works more
smoothly for that method.)
This patch changes nsIPrefLocalizedString so it is a trivial sub-interface of
nsISupportsString. This change eliminates the need for the wstring methods, so
the patch removes them as well. The net result is we have less code, and fewer
conversions between C strings and Gecko strings. The patch also merges the
nsISupportsString and nsIPrefLocalizedString cases in
nsPrefBranch::SetComplexValue(), because they are now identical. (The
nsISupportsString and nsIPrefLocalizedString cases in
nsPrefBranch::GetComplexValue() remain distinct; indeed, that's the whole
reason for having them as separate interfaces.)
2017-10-18 05:51:56 +00:00
|
|
|
theString->SetData(NS_ConvertUTF8toUTF16(utf8String));
|
2017-10-11 09:31:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
theString.forget(reinterpret_cast<nsIPrefLocalizedString**>(aRetVal));
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we can't get the pref, there's no point in being here
|
2017-10-26 23:30:33 +00:00
|
|
|
rv = GetCharPref(aPrefName, utf8String);
|
2017-10-11 09:31:32 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aType.Equals(NS_GET_IID(nsIFile))) {
|
2017-11-12 22:19:55 +00:00
|
|
|
ENSURE_MAIN_PROCESS("GetComplexValue(nsIFile)", aPrefName);
|
2017-10-11 09:31:32 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = file->SetPersistentDescriptor(utf8String);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
file.forget(reinterpret_cast<nsIFile**>(aRetVal));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aType.Equals(NS_GET_IID(nsIRelativeFilePref))) {
|
2017-11-12 22:19:55 +00:00
|
|
|
ENSURE_MAIN_PROCESS("GetComplexValue(nsIRelativeFilePref)", aPrefName);
|
2017-10-11 09:31:32 +00:00
|
|
|
|
|
|
|
nsACString::const_iterator keyBegin, strEnd;
|
|
|
|
utf8String.BeginReading(keyBegin);
|
|
|
|
utf8String.EndReading(strEnd);
|
|
|
|
|
|
|
|
// The pref has the format: [fromKey]a/b/c
|
|
|
|
if (*keyBegin++ != '[') {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsACString::const_iterator keyEnd(keyBegin);
|
|
|
|
if (!FindCharInReadable(']', keyEnd, strEnd)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString key(Substring(keyBegin, keyEnd));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> fromFile;
|
|
|
|
nsCOMPtr<nsIProperties> directoryService(
|
|
|
|
do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = directoryService->Get(
|
|
|
|
key.get(), NS_GET_IID(nsIFile), getter_AddRefs(fromFile));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> theFile;
|
|
|
|
rv = NS_NewNativeLocalFile(EmptyCString(), true, getter_AddRefs(theFile));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = theFile->SetRelativeDescriptor(fromFile, Substring(++keyEnd, strEnd));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRelativeFilePref> relativePref;
|
|
|
|
rv = NS_NewRelativeFilePref(theFile, key, getter_AddRefs(relativePref));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
relativePref.forget(reinterpret_cast<nsIRelativeFilePref**>(aRetVal));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_WARNING("nsPrefBranch::GetComplexValue - Unsupported interface type");
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPrefBranch::CheckSanityOfStringLength(const char* aPrefName,
|
|
|
|
const nsAString& aValue)
|
|
|
|
{
|
|
|
|
return CheckSanityOfStringLength(aPrefName, aValue.Length());
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPrefBranch::CheckSanityOfStringLength(const char* aPrefName,
|
|
|
|
const nsACString& aValue)
|
|
|
|
{
|
|
|
|
return CheckSanityOfStringLength(aPrefName, aValue.Length());
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPrefBranch::CheckSanityOfStringLength(const char* aPrefName,
|
|
|
|
const uint32_t aLength)
|
|
|
|
{
|
|
|
|
if (aLength > MAX_PREF_LENGTH) {
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
if (aLength <= MAX_ADVISABLE_PREF_LENGTH) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIConsoleService> console =
|
|
|
|
do_GetService("@mozilla.org/consoleservice;1", &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString message(nsPrintfCString(
|
|
|
|
"Warning: attempting to write %d bytes to preference %s. This is bad "
|
|
|
|
"for general performance and memory usage. Such an amount of data "
|
|
|
|
"should rather be written to an external file. This preference will "
|
|
|
|
"not be sent to any content processes.",
|
|
|
|
aLength,
|
|
|
|
GetPrefName(aPrefName).get()));
|
|
|
|
|
|
|
|
rv = console->LogStringMessage(NS_ConvertUTF8toUTF16(message).get());
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
nsPrefBranch::ReportToConsole(const nsAString& aMessage)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIConsoleService> console =
|
|
|
|
do_GetService("@mozilla.org/consoleservice;1", &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString message(aMessage);
|
|
|
|
console->LogStringMessage(message.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::SetComplexValue(const char* aPrefName,
|
|
|
|
const nsIID& aType,
|
|
|
|
nsISupports* aValue)
|
|
|
|
{
|
|
|
|
ENSURE_MAIN_PROCESS("SetComplexValue", aPrefName);
|
|
|
|
NS_ENSURE_ARG(aPrefName);
|
|
|
|
|
|
|
|
nsresult rv = NS_NOINTERFACE;
|
|
|
|
|
|
|
|
if (aType.Equals(NS_GET_IID(nsIFile))) {
|
|
|
|
nsCOMPtr<nsIFile> file = do_QueryInterface(aValue);
|
|
|
|
if (!file) {
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString descriptorString;
|
|
|
|
rv = file->GetPersistentDescriptor(descriptorString);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2017-11-14 08:06:02 +00:00
|
|
|
rv = SetCharPrefNoLengthCheck(aPrefName, descriptorString);
|
2017-10-11 09:31:32 +00:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aType.Equals(NS_GET_IID(nsIRelativeFilePref))) {
|
|
|
|
nsCOMPtr<nsIRelativeFilePref> relFilePref = do_QueryInterface(aValue);
|
|
|
|
if (!relFilePref) {
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
relFilePref->GetFile(getter_AddRefs(file));
|
|
|
|
if (!file) {
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString relativeToKey;
|
|
|
|
(void)relFilePref->GetRelativeToKey(relativeToKey);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> relativeToFile;
|
|
|
|
nsCOMPtr<nsIProperties> directoryService(
|
|
|
|
do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = directoryService->Get(
|
|
|
|
relativeToKey.get(), NS_GET_IID(nsIFile), getter_AddRefs(relativeToFile));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString relDescriptor;
|
|
|
|
rv = file->GetRelativeDescriptor(relativeToFile, relDescriptor);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString descriptorString;
|
|
|
|
descriptorString.Append('[');
|
|
|
|
descriptorString.Append(relativeToKey);
|
|
|
|
descriptorString.Append(']');
|
|
|
|
descriptorString.Append(relDescriptor);
|
2017-11-14 08:06:02 +00:00
|
|
|
return SetCharPrefNoLengthCheck(aPrefName, descriptorString);
|
2017-10-11 09:31:32 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 22:07:48 +00:00
|
|
|
if (aType.Equals(NS_GET_IID(nsIPrefLocalizedString))) {
|
2017-10-11 09:31:32 +00:00
|
|
|
nsCOMPtr<nsISupportsString> theString = do_QueryInterface(aValue);
|
|
|
|
|
|
|
|
if (theString) {
|
|
|
|
nsString wideString;
|
|
|
|
|
|
|
|
rv = theString->GetData(wideString);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// Check sanity of string length before any lengthy conversion
|
|
|
|
rv = CheckSanityOfStringLength(aPrefName, wideString);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2017-11-14 08:06:02 +00:00
|
|
|
rv = SetCharPrefNoLengthCheck(aPrefName,
|
|
|
|
NS_ConvertUTF16toUTF8(wideString));
|
2017-10-11 09:31:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_WARNING("nsPrefBranch::SetComplexValue - Unsupported interface type");
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::ClearUserPref(const char* aPrefName)
|
|
|
|
{
|
|
|
|
ENSURE_MAIN_PROCESS("ClearUserPref", aPrefName);
|
|
|
|
NS_ENSURE_ARG(aPrefName);
|
|
|
|
|
|
|
|
const PrefName& pref = GetPrefName(aPrefName);
|
|
|
|
return PREF_ClearUserPref(pref.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::PrefHasUserValue(const char* aPrefName, bool* aRetVal)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aRetVal);
|
|
|
|
NS_ENSURE_ARG(aPrefName);
|
|
|
|
|
|
|
|
const PrefName& pref = GetPrefName(aPrefName);
|
|
|
|
*aRetVal = PREF_HasUserPref(pref.get());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::LockPref(const char* aPrefName)
|
|
|
|
{
|
|
|
|
ENSURE_MAIN_PROCESS("LockPref", aPrefName);
|
|
|
|
NS_ENSURE_ARG(aPrefName);
|
|
|
|
|
|
|
|
const PrefName& pref = GetPrefName(aPrefName);
|
|
|
|
return PREF_LockPref(pref.get(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::PrefIsLocked(const char* aPrefName, bool* aRetVal)
|
|
|
|
{
|
|
|
|
ENSURE_MAIN_PROCESS("PrefIsLocked", aPrefName);
|
|
|
|
NS_ENSURE_ARG_POINTER(aRetVal);
|
|
|
|
NS_ENSURE_ARG(aPrefName);
|
|
|
|
|
|
|
|
const PrefName& pref = GetPrefName(aPrefName);
|
|
|
|
*aRetVal = PREF_PrefIsLocked(pref.get());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::UnlockPref(const char* aPrefName)
|
|
|
|
{
|
|
|
|
ENSURE_MAIN_PROCESS("UnlockPref", aPrefName);
|
|
|
|
NS_ENSURE_ARG(aPrefName);
|
|
|
|
|
|
|
|
const PrefName& pref = GetPrefName(aPrefName);
|
|
|
|
return PREF_LockPref(pref.get(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::ResetBranch(const char* aStartingAt)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::DeleteBranch(const char* aStartingAt)
|
|
|
|
{
|
|
|
|
ENSURE_MAIN_PROCESS("DeleteBranch", aStartingAt);
|
|
|
|
NS_ENSURE_ARG(aStartingAt);
|
|
|
|
|
2017-10-31 20:59:40 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!gHashTable) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2017-10-11 09:31:32 +00:00
|
|
|
const PrefName& pref = GetPrefName(aStartingAt);
|
2017-10-31 21:28:10 +00:00
|
|
|
nsAutoCString branchName(pref.get());
|
|
|
|
|
|
|
|
// Add a trailing '.' if it doesn't already have one.
|
|
|
|
if (branchName.Length() > 1 &&
|
|
|
|
!StringEndsWith(branchName, NS_LITERAL_CSTRING("."))) {
|
|
|
|
branchName += '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsACString& branchNameNoDot =
|
|
|
|
Substring(branchName, 0, branchName.Length() - 1);
|
|
|
|
|
2017-10-31 20:59:40 +00:00
|
|
|
for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
auto entry = static_cast<PrefHashEntry*>(iter.Get());
|
|
|
|
|
2017-10-31 21:28:10 +00:00
|
|
|
// The first disjunct matches branches: e.g. a branch name "foo.bar."
|
|
|
|
// matches an mKey "foo.bar.baz" (but it won't match "foo.barrel.baz").
|
|
|
|
// The second disjunct matches leaf nodes: e.g. a branch name "foo.bar."
|
|
|
|
// matches an mKey "foo.bar" (by ignoring the trailing '.').
|
|
|
|
nsDependentCString key(entry->mKey);
|
|
|
|
if (StringBeginsWith(key, branchName) || key.Equals(branchNameNoDot)) {
|
2017-10-31 20:59:40 +00:00
|
|
|
iter.Remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Preferences::HandleDirty();
|
|
|
|
return NS_OK;
|
2017-10-11 09:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::GetChildList(const char* aStartingAt,
|
|
|
|
uint32_t* aCount,
|
|
|
|
char*** aChildArray)
|
|
|
|
{
|
|
|
|
char** outArray;
|
|
|
|
int32_t numPrefs;
|
|
|
|
int32_t dwIndex;
|
|
|
|
AutoTArray<nsCString, 32> prefArray;
|
|
|
|
|
|
|
|
NS_ENSURE_ARG(aStartingAt);
|
|
|
|
NS_ENSURE_ARG_POINTER(aCount);
|
|
|
|
NS_ENSURE_ARG_POINTER(aChildArray);
|
|
|
|
|
|
|
|
*aChildArray = nullptr;
|
|
|
|
*aCount = 0;
|
|
|
|
|
|
|
|
// This will contain a list of all the pref name strings. Allocated on the
|
|
|
|
// stack for speed.
|
|
|
|
|
|
|
|
const PrefName& parent = GetPrefName(aStartingAt);
|
|
|
|
size_t parentLen = parent.Length();
|
|
|
|
for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
auto entry = static_cast<PrefHashEntry*>(iter.Get());
|
|
|
|
if (strncmp(entry->mKey, parent.get(), parentLen) == 0) {
|
|
|
|
prefArray.AppendElement(entry->mKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we've built up the list, run the callback on all the matching
|
|
|
|
// elements.
|
|
|
|
numPrefs = prefArray.Length();
|
|
|
|
|
|
|
|
if (numPrefs) {
|
2017-10-18 05:31:23 +00:00
|
|
|
outArray = (char**)moz_xmalloc(numPrefs * sizeof(char*));
|
2017-10-11 09:31:32 +00:00
|
|
|
|
|
|
|
for (dwIndex = 0; dwIndex < numPrefs; ++dwIndex) {
|
|
|
|
// we need to lop off mPrefRoot in case the user is planning to pass this
|
|
|
|
// back to us because if they do we are going to add mPrefRoot again.
|
|
|
|
const nsCString& element = prefArray[dwIndex];
|
|
|
|
outArray[dwIndex] =
|
|
|
|
(char*)nsMemory::Clone(element.get() + mPrefRoot.Length(),
|
|
|
|
element.Length() - mPrefRoot.Length() + 1);
|
|
|
|
|
|
|
|
if (!outArray[dwIndex]) {
|
|
|
|
// We ran out of memory... this is annoying.
|
|
|
|
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(dwIndex, outArray);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*aChildArray = outArray;
|
|
|
|
}
|
|
|
|
*aCount = numPrefs;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::AddObserver(const char* aDomain,
|
|
|
|
nsIObserver* aObserver,
|
|
|
|
bool aHoldWeak)
|
|
|
|
{
|
|
|
|
PrefCallback* pCallback;
|
|
|
|
|
|
|
|
NS_ENSURE_ARG(aDomain);
|
|
|
|
NS_ENSURE_ARG(aObserver);
|
|
|
|
|
|
|
|
// Hold a weak reference to the observer if so requested.
|
|
|
|
if (aHoldWeak) {
|
|
|
|
nsCOMPtr<nsISupportsWeakReference> weakRefFactory =
|
|
|
|
do_QueryInterface(aObserver);
|
|
|
|
if (!weakRefFactory) {
|
|
|
|
// The caller didn't give us a object that supports weak reference...
|
|
|
|
// tell them.
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct a PrefCallback with a weak reference to the observer.
|
|
|
|
pCallback = new PrefCallback(aDomain, weakRefFactory, this);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Construct a PrefCallback with a strong reference to the observer.
|
|
|
|
pCallback = new PrefCallback(aDomain, aObserver, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto p = mObservers.LookupForAdd(pCallback);
|
|
|
|
if (p) {
|
|
|
|
NS_WARNING("Ignoring duplicate observer.");
|
|
|
|
delete pCallback;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.OrInsert([&pCallback]() { return pCallback; });
|
|
|
|
|
|
|
|
// We must pass a fully qualified preference name to the callback
|
|
|
|
// aDomain == nullptr is the only possible failure, and we trapped it with
|
|
|
|
// NS_ENSURE_ARG above.
|
|
|
|
const PrefName& pref = GetPrefName(aDomain);
|
Bug 1414188 - Remove ValueObserver. r=glandium.
libpref has callbacks. You can register a callback function against a
particular pref (or pref prefix), and it'll be called when any matching pref
changes.
This is implemented in two layers. The lower layer is the list of CallbackNode
objects, pointed to by gFirstCallback.
The upper layer involves gObserverTable, which is a hash table of ValueObserver
objects. It is used for callbacks registered via
Preferences::RegisterCallback() and Preferences::Add*VarCache(), but not for
observers registered with Preferences::Add{Weak,Strong}Observer(). If multiple
callbacks with identical prefnames, callback functions and MatchKinds occur,
they are commoned up into a single ValueObserver, which is then wrapped by a
single CallbackNode. (The callbacks within a single ValueObserver can have
different void* aClosure args; ValueObserver keeps those in an nsTArray.)
Note also that gObserverTable is very inelegant, with duplication of data
between the keys and the values due it being a
nsRefPtrHashtable<ValueObserverHashKey, ValueObserver> and ValueObserver being
a subclass of ValueObserverHashKey(!)
This extra layer might make sense if there were a lot of commoning up
happening, but there's not. Across all process kinds there is an average of
between 1.2 and 1.7 closures per ValueObserver. The vast majority have 1
closure, and there are a handful that have multiple; the highest number I've
seen is 71.
(Note that this two-layer design probably seemed more natural back when libpref
was spread across multiple files.)
This patch removes the ValueObserver layer. The only tricky part is that there
is a notion of a MatchKind -- ExactMatch or PrefixMatch -- which exists in the
ValueObserverLayer but not in the CallbackNode layer. So the patch moves the
MatchKind into the CallbackNode layer, which is straightforward.
On Linux64 this reduces memory usage by about 40 KiB in the parent process and
about 30 KiB in each content processes.
The performance implications are minor.
- The list of CallbackNodes is a bit longer, so when a pref is changed we must
check more nodes. But we avoid possibly doing prefname matching twice.
- Callback registration is much faster, just a simple list prepend, without any
hash table insertion required.
- Callback unregistration is probably not much different. There is a longer
list to traverse, but no hash table removal.
- Pref lookup is by far the hottest libpref operation, and it's unchanged.
For perf to be genuinely affected would require (a) a *lot* more
almost-duplicate callbacks that would have been commoned up, and (b) frequent
changing of the pref those callbacks are observing. This seems highly unlikely.
MozReview-Commit-ID: 6xo4xmytOf3
--HG--
extra : rebase_source : cf9884a8a40b2cc3b00ab165ae5ab4ec3383f72d
2017-11-02 09:38:34 +00:00
|
|
|
PREF_RegisterCallback(pref.get(),
|
|
|
|
NotifyObserver,
|
|
|
|
pCallback,
|
|
|
|
Preferences::PrefixMatch,
|
|
|
|
/* isPriority */ false);
|
|
|
|
|
2017-10-11 09:31:32 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::RemoveObserver(const char* aDomain, nsIObserver* aObserver)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aDomain);
|
|
|
|
NS_ENSURE_ARG(aObserver);
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// If we're in the middle of a call to FreeObserverList, don't process this
|
|
|
|
// RemoveObserver call -- the observer in question will be removed soon, if
|
|
|
|
// it hasn't been already.
|
|
|
|
//
|
|
|
|
// It's important that we don't touch mObservers in any way -- even a Get()
|
|
|
|
// which returns null might cause the hashtable to resize itself, which will
|
|
|
|
// break the iteration in FreeObserverList.
|
|
|
|
if (mFreeingObserverList) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the relevant PrefCallback from mObservers and get an owning pointer
|
|
|
|
// to it. Unregister the callback first, and then let the owning pointer go
|
|
|
|
// out of scope and destroy the callback.
|
|
|
|
PrefCallback key(aDomain, aObserver, this);
|
|
|
|
nsAutoPtr<PrefCallback> pCallback;
|
|
|
|
mObservers.Remove(&key, &pCallback);
|
|
|
|
if (pCallback) {
|
|
|
|
// aDomain == nullptr is the only possible failure, trapped above.
|
|
|
|
const PrefName& pref = GetPrefName(aDomain);
|
Bug 1414188 - Remove ValueObserver. r=glandium.
libpref has callbacks. You can register a callback function against a
particular pref (or pref prefix), and it'll be called when any matching pref
changes.
This is implemented in two layers. The lower layer is the list of CallbackNode
objects, pointed to by gFirstCallback.
The upper layer involves gObserverTable, which is a hash table of ValueObserver
objects. It is used for callbacks registered via
Preferences::RegisterCallback() and Preferences::Add*VarCache(), but not for
observers registered with Preferences::Add{Weak,Strong}Observer(). If multiple
callbacks with identical prefnames, callback functions and MatchKinds occur,
they are commoned up into a single ValueObserver, which is then wrapped by a
single CallbackNode. (The callbacks within a single ValueObserver can have
different void* aClosure args; ValueObserver keeps those in an nsTArray.)
Note also that gObserverTable is very inelegant, with duplication of data
between the keys and the values due it being a
nsRefPtrHashtable<ValueObserverHashKey, ValueObserver> and ValueObserver being
a subclass of ValueObserverHashKey(!)
This extra layer might make sense if there were a lot of commoning up
happening, but there's not. Across all process kinds there is an average of
between 1.2 and 1.7 closures per ValueObserver. The vast majority have 1
closure, and there are a handful that have multiple; the highest number I've
seen is 71.
(Note that this two-layer design probably seemed more natural back when libpref
was spread across multiple files.)
This patch removes the ValueObserver layer. The only tricky part is that there
is a notion of a MatchKind -- ExactMatch or PrefixMatch -- which exists in the
ValueObserverLayer but not in the CallbackNode layer. So the patch moves the
MatchKind into the CallbackNode layer, which is straightforward.
On Linux64 this reduces memory usage by about 40 KiB in the parent process and
about 30 KiB in each content processes.
The performance implications are minor.
- The list of CallbackNodes is a bit longer, so when a pref is changed we must
check more nodes. But we avoid possibly doing prefname matching twice.
- Callback registration is much faster, just a simple list prepend, without any
hash table insertion required.
- Callback unregistration is probably not much different. There is a longer
list to traverse, but no hash table removal.
- Pref lookup is by far the hottest libpref operation, and it's unchanged.
For perf to be genuinely affected would require (a) a *lot* more
almost-duplicate callbacks that would have been commoned up, and (b) frequent
changing of the pref those callbacks are observing. This seems highly unlikely.
MozReview-Commit-ID: 6xo4xmytOf3
--HG--
extra : rebase_source : cf9884a8a40b2cc3b00ab165ae5ab4ec3383f72d
2017-11-02 09:38:34 +00:00
|
|
|
rv = PREF_UnregisterCallback(
|
|
|
|
pref.get(), NotifyObserver, pCallback, Preferences::PrefixMatch);
|
2017-10-11 09:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefBranch::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const char16_t* aData)
|
|
|
|
{
|
|
|
|
// Watch for xpcom shutdown and free our observers to eliminate any cyclic
|
|
|
|
// references.
|
|
|
|
if (!nsCRT::strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
|
|
|
|
FreeObserverList();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
nsPrefBranch::NotifyObserver(const char* aNewPref, void* aData)
|
|
|
|
{
|
|
|
|
PrefCallback* pCallback = (PrefCallback*)aData;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserver> observer = pCallback->GetObserver();
|
|
|
|
if (!observer) {
|
|
|
|
// The observer has expired. Let's remove this callback.
|
|
|
|
pCallback->GetPrefBranch()->RemoveExpiredCallback(pCallback);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove any root this string may contain so as to not confuse the observer
|
|
|
|
// by passing them something other than what they passed us as a topic.
|
|
|
|
uint32_t len = pCallback->GetPrefBranch()->GetRootLength();
|
|
|
|
nsAutoCString suffix(aNewPref + len);
|
|
|
|
|
|
|
|
observer->Observe(static_cast<nsIPrefBranch*>(pCallback->GetPrefBranch()),
|
|
|
|
NS_PREFBRANCH_PREFCHANGE_TOPIC_ID,
|
|
|
|
NS_ConvertASCIItoUTF16(suffix).get());
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2017-11-16 04:55:58 +00:00
|
|
|
nsPrefBranch::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
2017-10-11 09:31:32 +00:00
|
|
|
{
|
|
|
|
size_t n = aMallocSizeOf(this);
|
2017-11-16 04:55:58 +00:00
|
|
|
|
2017-10-11 09:31:32 +00:00
|
|
|
n += mPrefRoot.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
2017-11-16 04:55:58 +00:00
|
|
|
|
2017-10-11 09:31:32 +00:00
|
|
|
n += mObservers.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2017-11-16 04:55:58 +00:00
|
|
|
for (auto iter = mObservers.ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
const PrefCallback* data = iter.UserData();
|
|
|
|
n += data->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2017-10-11 09:31:32 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPrefBranch::FreeObserverList()
|
|
|
|
{
|
|
|
|
// We need to prevent anyone from modifying mObservers while we're iterating
|
|
|
|
// over it. In particular, some clients will call RemoveObserver() when
|
|
|
|
// they're removed and destructed via the iterator; we set
|
|
|
|
// mFreeingObserverList to keep those calls from touching mObservers.
|
|
|
|
mFreeingObserverList = true;
|
|
|
|
for (auto iter = mObservers.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
nsAutoPtr<PrefCallback>& callback = iter.Data();
|
|
|
|
nsPrefBranch* prefBranch = callback->GetPrefBranch();
|
|
|
|
const PrefName& pref = prefBranch->GetPrefName(callback->GetDomain().get());
|
Bug 1414188 - Remove ValueObserver. r=glandium.
libpref has callbacks. You can register a callback function against a
particular pref (or pref prefix), and it'll be called when any matching pref
changes.
This is implemented in two layers. The lower layer is the list of CallbackNode
objects, pointed to by gFirstCallback.
The upper layer involves gObserverTable, which is a hash table of ValueObserver
objects. It is used for callbacks registered via
Preferences::RegisterCallback() and Preferences::Add*VarCache(), but not for
observers registered with Preferences::Add{Weak,Strong}Observer(). If multiple
callbacks with identical prefnames, callback functions and MatchKinds occur,
they are commoned up into a single ValueObserver, which is then wrapped by a
single CallbackNode. (The callbacks within a single ValueObserver can have
different void* aClosure args; ValueObserver keeps those in an nsTArray.)
Note also that gObserverTable is very inelegant, with duplication of data
between the keys and the values due it being a
nsRefPtrHashtable<ValueObserverHashKey, ValueObserver> and ValueObserver being
a subclass of ValueObserverHashKey(!)
This extra layer might make sense if there were a lot of commoning up
happening, but there's not. Across all process kinds there is an average of
between 1.2 and 1.7 closures per ValueObserver. The vast majority have 1
closure, and there are a handful that have multiple; the highest number I've
seen is 71.
(Note that this two-layer design probably seemed more natural back when libpref
was spread across multiple files.)
This patch removes the ValueObserver layer. The only tricky part is that there
is a notion of a MatchKind -- ExactMatch or PrefixMatch -- which exists in the
ValueObserverLayer but not in the CallbackNode layer. So the patch moves the
MatchKind into the CallbackNode layer, which is straightforward.
On Linux64 this reduces memory usage by about 40 KiB in the parent process and
about 30 KiB in each content processes.
The performance implications are minor.
- The list of CallbackNodes is a bit longer, so when a pref is changed we must
check more nodes. But we avoid possibly doing prefname matching twice.
- Callback registration is much faster, just a simple list prepend, without any
hash table insertion required.
- Callback unregistration is probably not much different. There is a longer
list to traverse, but no hash table removal.
- Pref lookup is by far the hottest libpref operation, and it's unchanged.
For perf to be genuinely affected would require (a) a *lot* more
almost-duplicate callbacks that would have been commoned up, and (b) frequent
changing of the pref those callbacks are observing. This seems highly unlikely.
MozReview-Commit-ID: 6xo4xmytOf3
--HG--
extra : rebase_source : cf9884a8a40b2cc3b00ab165ae5ab4ec3383f72d
2017-11-02 09:38:34 +00:00
|
|
|
PREF_UnregisterCallback(pref.get(),
|
|
|
|
nsPrefBranch::NotifyObserver,
|
|
|
|
callback,
|
|
|
|
Preferences::PrefixMatch);
|
2017-10-11 09:31:32 +00:00
|
|
|
iter.Remove();
|
|
|
|
}
|
|
|
|
mFreeingObserverList = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPrefBranch::RemoveExpiredCallback(PrefCallback* aCallback)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aCallback->IsExpired(), "Callback should be expired.");
|
|
|
|
mObservers.Remove(aCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPrefBranch::GetDefaultFromPropertiesFile(const char* aPrefName,
|
|
|
|
nsAString& aReturn)
|
|
|
|
{
|
|
|
|
// The default value contains a URL to a .properties file.
|
|
|
|
|
2017-10-26 23:30:33 +00:00
|
|
|
nsAutoCString propertyFileURL;
|
2017-11-14 08:06:02 +00:00
|
|
|
nsresult rv =
|
|
|
|
Preferences::GetCString(aPrefName, propertyFileURL, PrefValueKind::Default);
|
2017-10-11 09:31:32 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStringBundleService> bundleService =
|
|
|
|
mozilla::services::GetStringBundleService();
|
|
|
|
if (!bundleService) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStringBundle> bundle;
|
|
|
|
rv =
|
|
|
|
bundleService->CreateBundle(propertyFileURL.get(), getter_AddRefs(bundle));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bundle->GetStringFromName(aPrefName, aReturn);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPrefBranch::PrefName
|
|
|
|
nsPrefBranch::GetPrefName(const char* aPrefName) const
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aPrefName, "null pref name!");
|
|
|
|
|
|
|
|
// For speed, avoid strcpy if we can.
|
|
|
|
if (mPrefRoot.IsEmpty()) {
|
|
|
|
return PrefName(aPrefName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PrefName(mPrefRoot + nsDependentCString(aPrefName));
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// nsPrefLocalizedString
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsPrefLocalizedString::nsPrefLocalizedString() = default;
|
|
|
|
|
|
|
|
nsPrefLocalizedString::~nsPrefLocalizedString() = default;
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsISupports Implementation
|
|
|
|
//
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsPrefLocalizedString)
|
|
|
|
NS_IMPL_RELEASE(nsPrefLocalizedString)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsPrefLocalizedString)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIPrefLocalizedString)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIPrefLocalizedString)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsString)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPrefLocalizedString::Init()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
mUnicodeString = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// nsRelativeFilePref
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(nsRelativeFilePref, nsIRelativeFilePref)
|
|
|
|
|
|
|
|
nsRelativeFilePref::nsRelativeFilePref() = default;
|
|
|
|
|
|
|
|
nsRelativeFilePref::~nsRelativeFilePref() = default;
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRelativeFilePref::GetFile(nsIFile** aFile)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aFile);
|
|
|
|
*aFile = mFile;
|
|
|
|
NS_IF_ADDREF(*aFile);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRelativeFilePref::SetFile(nsIFile* aFile)
|
|
|
|
{
|
|
|
|
mFile = aFile;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRelativeFilePref::GetRelativeToKey(nsACString& aRelativeToKey)
|
|
|
|
{
|
|
|
|
aRelativeToKey.Assign(mRelativeToKey);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRelativeFilePref::SetRelativeToKey(const nsACString& aRelativeToKey)
|
|
|
|
{
|
|
|
|
mRelativeToKey.Assign(aRelativeToKey);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-10-11 10:02:03 +00:00
|
|
|
|
2017-10-11 08:03:23 +00:00
|
|
|
//===========================================================================
|
|
|
|
// Core prefs code
|
|
|
|
//===========================================================================
|
|
|
|
|
2011-05-23 14:54:02 +00:00
|
|
|
namespace mozilla {
|
|
|
|
|
2003-11-03 16:46:13 +00:00
|
|
|
#define INITIAL_PREF_FILES 10
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2010-09-09 03:38:12 +00:00
|
|
|
static NS_DEFINE_CID(kZipReaderCID, NS_ZIPREADER_CID);
|
2001-10-08 19:52:17 +00:00
|
|
|
|
2016-07-21 19:46:15 +00:00
|
|
|
void
|
2017-10-26 05:14:01 +00:00
|
|
|
Preferences::HandleDirty()
|
2016-07-21 19:46:15 +00:00
|
|
|
{
|
2017-06-21 19:26:10 +00:00
|
|
|
if (!XRE_IsParentProcess()) {
|
2017-11-12 22:19:55 +00:00
|
|
|
// This path is hit a lot when setting up prefs for content processes. Just
|
|
|
|
// ignore it in that case, because content processes aren't responsible for
|
|
|
|
// saving prefs.
|
2017-06-21 19:26:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2017-07-20 20:19:39 +00:00
|
|
|
if (!gHashTable || !sPreferences) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2017-07-20 20:19:39 +00:00
|
|
|
if (sPreferences->mProfileShutdown) {
|
|
|
|
NS_WARNING("Setting user pref after profile shutdown.");
|
|
|
|
return;
|
|
|
|
}
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2017-07-20 20:19:39 +00:00
|
|
|
if (!sPreferences->mDirty) {
|
2016-07-21 19:46:15 +00:00
|
|
|
sPreferences->mDirty = true;
|
2017-06-21 15:17:01 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
if (sPreferences->mCurrentFile && sPreferences->AllowOffMainThreadSave() &&
|
|
|
|
!sPreferences->mSavePending) {
|
2017-06-15 18:15:25 +00:00
|
|
|
sPreferences->mSavePending = true;
|
|
|
|
static const int PREF_DELAY_MS = 500;
|
|
|
|
NS_DelayedDispatchToCurrentThread(
|
|
|
|
mozilla::NewRunnableMethod("Preferences::SavePrefFileAsynchronous",
|
2017-11-01 02:55:28 +00:00
|
|
|
sPreferences.get(),
|
2017-06-15 18:15:25 +00:00
|
|
|
&Preferences::SavePrefFileAsynchronous),
|
|
|
|
PREF_DELAY_MS);
|
|
|
|
}
|
2016-07-21 19:46:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
static nsresult
|
|
|
|
openPrefFile(nsIFile* aFile);
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
pref_LoadPrefsInDirList(const char* aListId);
|
|
|
|
|
2014-04-03 18:55:42 +00:00
|
|
|
static const char kTelemetryPref[] = "toolkit.telemetry.enabled";
|
|
|
|
static const char kOldTelemetryPref[] = "toolkit.telemetry.enabledPreRelease";
|
|
|
|
static const char kChannelPref[] = "app.update.channel";
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// clang-format off
|
2016-07-19 13:10:00 +00:00
|
|
|
static const char kPrefFileHeader[] =
|
|
|
|
"# Mozilla User Preferences"
|
|
|
|
NS_LINEBREAK
|
|
|
|
NS_LINEBREAK
|
|
|
|
"/* Do not edit this file."
|
|
|
|
NS_LINEBREAK
|
|
|
|
" *"
|
|
|
|
NS_LINEBREAK
|
|
|
|
" * If you make changes to this file while the application is running,"
|
|
|
|
NS_LINEBREAK
|
|
|
|
" * the changes will be overwritten when the application exits."
|
|
|
|
NS_LINEBREAK
|
|
|
|
" *"
|
|
|
|
NS_LINEBREAK
|
2017-10-05 23:06:47 +00:00
|
|
|
" * To make a manual change to preferences, you can visit the URL "
|
|
|
|
"about:config"
|
2016-07-19 13:10:00 +00:00
|
|
|
NS_LINEBREAK
|
|
|
|
" */"
|
|
|
|
NS_LINEBREAK
|
|
|
|
NS_LINEBREAK;
|
2017-10-05 23:06:47 +00:00
|
|
|
// clang-format on
|
2016-07-19 13:10:00 +00:00
|
|
|
|
2017-11-12 22:19:49 +00:00
|
|
|
// Note: if sShutdown is true, sPreferences will be nullptr.
|
2017-11-01 02:55:28 +00:00
|
|
|
StaticRefPtr<Preferences> Preferences::sPreferences;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool Preferences::sShutdown = false;
|
2011-05-23 14:54:03 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// This globally enables or disables OMT pref writing, both sync and async.
|
2017-06-14 17:37:28 +00:00
|
|
|
static int32_t sAllowOMTPrefWrite = -1;
|
|
|
|
|
2016-01-01 12:39:02 +00:00
|
|
|
// Write the preference data to a file.
|
|
|
|
class PreferencesWriter final
|
|
|
|
{
|
|
|
|
public:
|
2017-10-05 23:06:47 +00:00
|
|
|
PreferencesWriter() = default;
|
2016-01-01 12:39:02 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
static nsresult Write(nsIFile* aFile, PrefSaveData& aPrefs)
|
2016-01-01 12:39:02 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIOutputStream> outStreamSink;
|
|
|
|
nsCOMPtr<nsIOutputStream> outStream;
|
2017-10-05 23:06:47 +00:00
|
|
|
uint32_t writeAmount;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Execute a "safe" save by saving through a tempfile.
|
|
|
|
rv = NS_NewSafeLocalFileOutputStream(
|
|
|
|
getter_AddRefs(outStreamSink), aFile, -1, 0600);
|
2016-01-01 12:39:02 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
rv = NS_NewBufferedOutputStream(
|
2017-10-24 12:38:23 +00:00
|
|
|
getter_AddRefs(outStream), outStreamSink.forget(), 4096);
|
2016-01-01 12:39:02 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-06-15 21:33:20 +00:00
|
|
|
struct CharComparator
|
|
|
|
{
|
2017-10-26 05:48:11 +00:00
|
|
|
bool LessThan(const nsCString& aA, const nsCString& aB) const
|
2017-06-15 21:33:20 +00:00
|
|
|
{
|
2017-10-26 05:48:11 +00:00
|
|
|
return aA < aB;
|
2017-06-15 21:33:20 +00:00
|
|
|
}
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2017-10-26 05:48:11 +00:00
|
|
|
bool Equals(const nsCString& aA, const nsCString& aB) const
|
2017-06-15 21:33:20 +00:00
|
|
|
{
|
2017-10-26 05:48:11 +00:00
|
|
|
return aA == aB;
|
2017-06-15 21:33:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// Sort the preferences to make a readable file on disk.
|
2017-06-15 21:33:20 +00:00
|
|
|
aPrefs.Sort(CharComparator());
|
2016-01-01 12:39:02 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// Write out the file header.
|
|
|
|
outStream->Write(
|
|
|
|
kPrefFileHeader, sizeof(kPrefFileHeader) - 1, &writeAmount);
|
2016-01-01 12:39:02 +00:00
|
|
|
|
2017-10-26 05:48:11 +00:00
|
|
|
for (nsCString& pref : aPrefs) {
|
|
|
|
outStream->Write(pref.get(), pref.Length(), &writeAmount);
|
2016-01-01 12:39:02 +00:00
|
|
|
outStream->Write(NS_LINEBREAK, NS_LINEBREAK_LEN, &writeAmount);
|
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// Tell the safe output stream to overwrite the real prefs file.
|
|
|
|
// (It'll abort if there were any errors during writing.)
|
2016-01-01 12:39:02 +00:00
|
|
|
nsCOMPtr<nsISafeOutputStream> safeStream = do_QueryInterface(outStream);
|
|
|
|
NS_ASSERTION(safeStream, "expected a safe output stream!");
|
|
|
|
if (safeStream) {
|
|
|
|
rv = safeStream->Finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("failed to save prefs file! possible data loss");
|
|
|
|
}
|
|
|
|
#endif
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2016-01-01 12:39:02 +00:00
|
|
|
return rv;
|
|
|
|
}
|
2017-06-14 17:37:28 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
static void Flush()
|
2017-06-14 17:37:28 +00:00
|
|
|
{
|
2017-10-05 23:06:47 +00:00
|
|
|
// This can be further optimized; instead of waiting for all of the writer
|
|
|
|
// thread to be available, we just have to wait for all the pending writes
|
|
|
|
// to be done.
|
2017-06-14 17:37:28 +00:00
|
|
|
if (!sPendingWriteData.compareExchange(nullptr, nullptr)) {
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIEventTarget> target =
|
|
|
|
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2017-06-12 19:34:10 +00:00
|
|
|
target->Dispatch(NS_NewRunnableFunction("Preferences_dummy", [] {}),
|
2017-06-14 17:37:28 +00:00
|
|
|
nsIEventTarget::DISPATCH_SYNC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is the data that all of the runnables (see below) will attempt
|
|
|
|
// to write. It will always have the most up to date version, or be
|
|
|
|
// null, if the up to date information has already been written out.
|
2017-06-15 21:33:20 +00:00
|
|
|
static Atomic<PrefSaveData*> sPendingWriteData;
|
2017-06-14 17:37:28 +00:00
|
|
|
};
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2017-06-15 21:33:20 +00:00
|
|
|
Atomic<PrefSaveData*> PreferencesWriter::sPendingWriteData(nullptr);
|
2017-06-14 17:37:28 +00:00
|
|
|
|
|
|
|
class PWRunnable : public Runnable
|
|
|
|
{
|
|
|
|
public:
|
2017-06-12 19:34:10 +00:00
|
|
|
explicit PWRunnable(nsIFile* aFile)
|
|
|
|
: Runnable("PWRunnable")
|
|
|
|
, mFile(aFile)
|
2017-10-05 23:06:47 +00:00
|
|
|
{
|
|
|
|
}
|
2017-06-14 17:37:28 +00:00
|
|
|
|
|
|
|
NS_IMETHOD Run() override
|
|
|
|
{
|
|
|
|
// If we get a nullptr on the exchange, it means that somebody
|
|
|
|
// else has already processed the request, and we can just return.
|
2017-10-05 23:06:47 +00:00
|
|
|
mozilla::UniquePtr<PrefSaveData> prefs(
|
|
|
|
PreferencesWriter::sPendingWriteData.exchange(nullptr));
|
2017-06-14 17:37:28 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (prefs) {
|
2017-06-15 21:33:20 +00:00
|
|
|
rv = PreferencesWriter::Write(mFile, *prefs);
|
2017-06-14 17:37:28 +00:00
|
|
|
|
|
|
|
// Make a copy of these so we can have them in runnable lambda.
|
|
|
|
// nsIFile is only there so that we would never release the
|
|
|
|
// ref counted pointer off main thread.
|
|
|
|
nsresult rvCopy = rv;
|
|
|
|
nsCOMPtr<nsIFile> fileCopy(mFile);
|
2017-10-05 23:06:47 +00:00
|
|
|
SystemGroup::Dispatch(
|
|
|
|
TaskCategory::Other,
|
|
|
|
NS_NewRunnableFunction("Preferences::WriterRunnable",
|
|
|
|
[fileCopy, rvCopy] {
|
|
|
|
MOZ_RELEASE_ASSERT(NS_IsMainThread());
|
|
|
|
if (NS_FAILED(rvCopy)) {
|
2017-10-26 05:14:01 +00:00
|
|
|
Preferences::HandleDirty();
|
2017-10-05 23:06:47 +00:00
|
|
|
}
|
|
|
|
}));
|
2017-06-14 17:37:28 +00:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsCOMPtr<nsIFile> mFile;
|
2016-01-01 12:39:02 +00:00
|
|
|
};
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
struct CacheData
|
|
|
|
{
|
|
|
|
void* mCacheLocation;
|
2011-05-28 23:42:57 +00:00
|
|
|
union {
|
2017-10-05 23:06:47 +00:00
|
|
|
bool mDefaultValueBool;
|
|
|
|
int32_t mDefaultValueInt;
|
|
|
|
uint32_t mDefaultValueUint;
|
|
|
|
float mDefaultValueFloat;
|
2011-05-28 23:42:57 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-09-06 05:17:05 +00:00
|
|
|
// gCacheDataDesc holds information about prefs startup. It's being used for
|
|
|
|
// diagnosing prefs startup problems in bug 1276488.
|
|
|
|
static const char* gCacheDataDesc = "untouched";
|
2017-10-05 23:06:47 +00:00
|
|
|
static nsTArray<nsAutoPtr<CacheData>>* gCacheData = nullptr;
|
2011-05-28 23:42:57 +00:00
|
|
|
|
2014-06-24 13:49:56 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
static bool
|
|
|
|
HaveExistingCacheFor(void* aPtr)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (gCacheData) {
|
|
|
|
for (size_t i = 0, count = gCacheData->Length(); i < count; ++i) {
|
2017-10-05 23:06:47 +00:00
|
|
|
if ((*gCacheData)[i]->mCacheLocation == aPtr) {
|
2014-06-24 13:49:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-10-05 23:06:47 +00:00
|
|
|
AssertNotAlreadyCached(const char* aPrefType, const char* aPref, void* aPtr)
|
2014-06-24 13:49:56 +00:00
|
|
|
{
|
|
|
|
if (HaveExistingCacheFor(aPtr)) {
|
2017-10-05 23:06:47 +00:00
|
|
|
fprintf_stderr(
|
|
|
|
stderr,
|
2014-06-24 13:49:56 +00:00
|
|
|
"Attempt to add a %s pref cache for preference '%s' at address '%p'"
|
|
|
|
"was made. However, a pref was already cached at this address.\n",
|
2017-10-05 23:06:47 +00:00
|
|
|
aPrefType,
|
|
|
|
aPref,
|
|
|
|
aPtr);
|
|
|
|
MOZ_ASSERT(false,
|
|
|
|
"Should not have an existing pref cache for this address");
|
2014-06-24 13:49:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-04-22 19:35:01 +00:00
|
|
|
static void
|
|
|
|
ReportToConsole(const char* aMessage, int aLine, bool aError)
|
|
|
|
{
|
|
|
|
nsPrintfCString message("** Preference parsing %s (line %d) = %s **\n",
|
2017-10-05 23:06:47 +00:00
|
|
|
(aError ? "error" : "warning"),
|
|
|
|
aLine,
|
|
|
|
aMessage);
|
2016-04-22 19:35:01 +00:00
|
|
|
nsPrefBranch::ReportToConsole(NS_ConvertUTF8toUTF16(message.get()));
|
|
|
|
}
|
|
|
|
|
2017-11-16 04:55:58 +00:00
|
|
|
struct PrefsSizes
|
2012-10-11 15:36:21 +00:00
|
|
|
{
|
2017-11-16 04:55:58 +00:00
|
|
|
PrefsSizes()
|
|
|
|
: mHashTable(0)
|
|
|
|
, mStringValues(0)
|
|
|
|
, mCacheData(0)
|
|
|
|
, mRootBranches(0)
|
|
|
|
, mPrefNameArena(0)
|
|
|
|
, mCallbacks(0)
|
|
|
|
, mMisc(0)
|
|
|
|
{
|
2012-10-11 15:36:21 +00:00
|
|
|
}
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2017-11-16 04:55:58 +00:00
|
|
|
size_t mHashTable;
|
|
|
|
size_t mStringValues;
|
|
|
|
size_t mCacheData;
|
|
|
|
size_t mRootBranches;
|
|
|
|
size_t mPrefNameArena;
|
|
|
|
size_t mCallbacks;
|
|
|
|
size_t mMisc;
|
|
|
|
};
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2017-11-16 04:55:58 +00:00
|
|
|
// Although this is a member of Preferences, it measures sPreferences and
|
|
|
|
// several other global structures.
|
|
|
|
/* static */ void
|
|
|
|
Preferences::AddSizeOfIncludingThis(MallocSizeOf aMallocSizeOf,
|
|
|
|
PrefsSizes& aSizes)
|
|
|
|
{
|
|
|
|
if (!sPreferences) {
|
|
|
|
return;
|
2016-02-03 06:30:54 +00:00
|
|
|
}
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2017-11-16 04:55:58 +00:00
|
|
|
aSizes.mMisc += aMallocSizeOf(sPreferences.get());
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2017-11-16 04:55:58 +00:00
|
|
|
aSizes.mRootBranches +=
|
|
|
|
static_cast<nsPrefBranch*>(sPreferences->mRootBranch.get())
|
|
|
|
->SizeOfIncludingThis(aMallocSizeOf) +
|
|
|
|
static_cast<nsPrefBranch*>(sPreferences->mDefaultRootBranch.get())
|
|
|
|
->SizeOfIncludingThis(aMallocSizeOf);
|
2012-10-11 15:36:21 +00:00
|
|
|
}
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class PreferenceServiceReporter final : public nsIMemoryReporter
|
2013-01-18 00:45:11 +00:00
|
|
|
{
|
2014-06-30 22:11:48 +00:00
|
|
|
~PreferenceServiceReporter() {}
|
|
|
|
|
2013-01-18 00:45:11 +00:00
|
|
|
public:
|
2013-12-08 05:39:47 +00:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIMEMORYREPORTER
|
2013-11-06 03:58:20 +00:00
|
|
|
|
2013-09-11 18:20:22 +00:00
|
|
|
protected:
|
|
|
|
static const uint32_t kSuspectReferentCount = 1000;
|
2013-01-18 00:45:11 +00:00
|
|
|
};
|
2012-10-11 15:36:21 +00:00
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(PreferenceServiceReporter, nsIMemoryReporter)
|
2013-12-08 05:39:47 +00:00
|
|
|
|
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(PreferenceServiceMallocSizeOf)
|
|
|
|
|
2013-09-11 18:20:22 +00:00
|
|
|
NS_IMETHODIMP
|
2016-08-24 05:23:45 +00:00
|
|
|
PreferenceServiceReporter::CollectReports(
|
2017-10-05 23:06:47 +00:00
|
|
|
nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData,
|
|
|
|
bool aAnonymize)
|
2013-09-11 18:20:22 +00:00
|
|
|
{
|
2017-11-16 04:55:58 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
MallocSizeOf mallocSizeOf = PreferenceServiceMallocSizeOf;
|
|
|
|
PrefsSizes sizes;
|
|
|
|
|
|
|
|
Preferences::AddSizeOfIncludingThis(mallocSizeOf, sizes);
|
|
|
|
|
|
|
|
if (gHashTable) {
|
|
|
|
sizes.mHashTable += gHashTable->ShallowSizeOfIncludingThis(mallocSizeOf);
|
|
|
|
for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
auto entry = static_cast<PrefHashEntry*>(iter.Get());
|
|
|
|
sizes.mStringValues += entry->SizeOfExcludingThis(mallocSizeOf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gCacheData) {
|
|
|
|
sizes.mCacheData += gCacheData->ShallowSizeOfIncludingThis(mallocSizeOf);
|
|
|
|
for (uint32_t i = 0, count = gCacheData->Length(); i < count; ++i) {
|
|
|
|
sizes.mCacheData += mallocSizeOf((*gCacheData)[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sizes.mPrefNameArena += gPrefNameArena.SizeOfExcludingThis(mallocSizeOf);
|
|
|
|
|
|
|
|
for (CallbackNode* node = gFirstCallback; node; node = node->mNext) {
|
|
|
|
sizes.mCallbacks += mallocSizeOf(node);
|
|
|
|
sizes.mCallbacks += mallocSizeOf(node->mDomain);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT("explicit/preferences/hash-table",
|
|
|
|
KIND_HEAP,
|
|
|
|
UNITS_BYTES,
|
|
|
|
sizes.mHashTable,
|
|
|
|
"Memory used by libpref's hash table.");
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT("explicit/preferences/string-values",
|
|
|
|
KIND_HEAP,
|
|
|
|
UNITS_BYTES,
|
|
|
|
sizes.mStringValues,
|
|
|
|
"Memory used by libpref's string pref values.");
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT("explicit/preferences/cache-data",
|
|
|
|
KIND_HEAP,
|
|
|
|
UNITS_BYTES,
|
|
|
|
sizes.mCacheData,
|
|
|
|
"Memory used by libpref's VarCaches.");
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT("explicit/preferences/root-branches",
|
|
|
|
KIND_HEAP,
|
|
|
|
UNITS_BYTES,
|
|
|
|
sizes.mRootBranches,
|
|
|
|
"Memory used by libpref's root branches.");
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT("explicit/preferences/pref-name-arena",
|
|
|
|
KIND_HEAP,
|
|
|
|
UNITS_BYTES,
|
|
|
|
sizes.mPrefNameArena,
|
|
|
|
"Memory used by libpref's arena for pref names.");
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT("explicit/preferences/callbacks",
|
|
|
|
KIND_HEAP,
|
|
|
|
UNITS_BYTES,
|
|
|
|
sizes.mCallbacks,
|
|
|
|
"Memory used by libpref's callbacks list, including "
|
|
|
|
"pref names and prefixes.");
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT("explicit/preferences/misc",
|
2017-10-05 23:06:47 +00:00
|
|
|
KIND_HEAP,
|
|
|
|
UNITS_BYTES,
|
2017-11-16 04:55:58 +00:00
|
|
|
sizes.mMisc,
|
|
|
|
"Miscellaneous memory used by libpref.");
|
2013-09-11 18:20:22 +00:00
|
|
|
|
|
|
|
nsPrefBranch* rootBranch =
|
|
|
|
static_cast<nsPrefBranch*>(Preferences::GetRootBranch());
|
|
|
|
if (!rootBranch) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-11-23 02:52:40 +00:00
|
|
|
size_t numStrong = 0;
|
|
|
|
size_t numWeakAlive = 0;
|
|
|
|
size_t numWeakDead = 0;
|
|
|
|
nsTArray<nsCString> suspectPreferences;
|
|
|
|
// Count of the number of referents for each preference.
|
|
|
|
nsDataHashtable<nsCStringHashKey, uint32_t> prefCounter;
|
|
|
|
|
|
|
|
for (auto iter = rootBranch->mObservers.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
nsAutoPtr<PrefCallback>& callback = iter.Data();
|
|
|
|
nsPrefBranch* prefBranch = callback->GetPrefBranch();
|
2017-10-07 20:46:07 +00:00
|
|
|
const auto& pref = prefBranch->GetPrefName(callback->GetDomain().get());
|
2015-11-23 02:52:40 +00:00
|
|
|
|
|
|
|
if (callback->IsWeak()) {
|
|
|
|
nsCOMPtr<nsIObserver> callbackRef = do_QueryReferent(callback->mWeakRef);
|
|
|
|
if (callbackRef) {
|
|
|
|
numWeakAlive++;
|
|
|
|
} else {
|
|
|
|
numWeakDead++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
numStrong++;
|
|
|
|
}
|
|
|
|
|
2017-04-11 21:00:19 +00:00
|
|
|
nsDependentCString prefString(pref.get());
|
2015-11-23 02:52:40 +00:00
|
|
|
uint32_t oldCount = 0;
|
|
|
|
prefCounter.Get(prefString, &oldCount);
|
|
|
|
uint32_t currentCount = oldCount + 1;
|
|
|
|
prefCounter.Put(prefString, currentCount);
|
|
|
|
|
|
|
|
// Keep track of preferences that have a suspiciously large number of
|
|
|
|
// referents (a symptom of a leak).
|
|
|
|
if (currentCount == kSuspectReferentCount) {
|
|
|
|
suspectPreferences.AppendElement(prefString);
|
|
|
|
}
|
|
|
|
}
|
2013-09-11 18:20:22 +00:00
|
|
|
|
2015-11-23 02:52:40 +00:00
|
|
|
for (uint32_t i = 0; i < suspectPreferences.Length(); i++) {
|
|
|
|
nsCString& suspect = suspectPreferences[i];
|
2013-09-11 18:20:22 +00:00
|
|
|
uint32_t totalReferentCount = 0;
|
2015-11-23 02:52:40 +00:00
|
|
|
prefCounter.Get(suspect, &totalReferentCount);
|
2013-09-11 18:20:22 +00:00
|
|
|
|
|
|
|
nsPrintfCString suspectPath("preference-service-suspect/"
|
2017-10-05 23:06:47 +00:00
|
|
|
"referent(pref=%s)",
|
|
|
|
suspect.get());
|
2013-09-11 18:20:22 +00:00
|
|
|
|
2016-08-24 05:23:45 +00:00
|
|
|
aHandleReport->Callback(
|
|
|
|
/* process = */ EmptyCString(),
|
2017-10-05 23:06:47 +00:00
|
|
|
suspectPath,
|
|
|
|
KIND_OTHER,
|
|
|
|
UNITS_COUNT,
|
|
|
|
totalReferentCount,
|
2016-08-24 05:23:45 +00:00
|
|
|
NS_LITERAL_CSTRING(
|
|
|
|
"A preference with a suspiciously large number referents (symptom of a "
|
|
|
|
"leak)."),
|
|
|
|
aData);
|
2013-09-11 18:20:22 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 05:23:45 +00:00
|
|
|
MOZ_COLLECT_REPORT(
|
2017-10-05 23:06:47 +00:00
|
|
|
"preference-service/referent/strong",
|
|
|
|
KIND_OTHER,
|
|
|
|
UNITS_COUNT,
|
2016-08-24 05:23:45 +00:00
|
|
|
numStrong,
|
|
|
|
"The number of strong referents held by the preference service.");
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT(
|
2017-10-05 23:06:47 +00:00
|
|
|
"preference-service/referent/weak/alive",
|
|
|
|
KIND_OTHER,
|
|
|
|
UNITS_COUNT,
|
2016-08-24 05:23:45 +00:00
|
|
|
numWeakAlive,
|
|
|
|
"The number of weak referents held by the preference service that are "
|
|
|
|
"still alive.");
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT(
|
2017-10-05 23:06:47 +00:00
|
|
|
"preference-service/referent/weak/dead",
|
|
|
|
KIND_OTHER,
|
|
|
|
UNITS_COUNT,
|
2016-08-24 05:23:45 +00:00
|
|
|
numWeakDead,
|
|
|
|
"The number of weak referents held by the preference service that are "
|
|
|
|
"dead.");
|
2013-09-11 18:20:22 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-14 06:43:25 +00:00
|
|
|
namespace {
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2016-04-26 00:23:21 +00:00
|
|
|
class AddPreferencesMemoryReporterRunnable : public Runnable
|
2013-01-14 06:43:25 +00:00
|
|
|
{
|
2017-06-12 19:34:10 +00:00
|
|
|
public:
|
2017-10-05 23:06:47 +00:00
|
|
|
AddPreferencesMemoryReporterRunnable()
|
|
|
|
: Runnable("AddPreferencesMemoryReporterRunnable")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-08-08 02:18:10 +00:00
|
|
|
NS_IMETHOD Run() override
|
2013-01-14 06:43:25 +00:00
|
|
|
{
|
2013-11-07 05:35:30 +00:00
|
|
|
return RegisterStrongMemoryReporter(new PreferenceServiceReporter());
|
2013-01-14 06:43:25 +00:00
|
|
|
}
|
|
|
|
};
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace
|
2013-01-14 06:43:25 +00:00
|
|
|
|
2017-11-09 06:59:51 +00:00
|
|
|
static InfallibleTArray<Preferences::PrefSetting>* gInitPrefs;
|
|
|
|
|
2017-10-17 04:08:42 +00:00
|
|
|
/* static */ already_AddRefed<Preferences>
|
2011-06-12 02:30:15 +00:00
|
|
|
Preferences::GetInstanceForService()
|
2011-05-23 14:54:03 +00:00
|
|
|
{
|
|
|
|
if (sPreferences) {
|
2017-10-17 04:08:42 +00:00
|
|
|
return do_AddRef(sPreferences);
|
2011-05-23 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
2017-09-06 05:17:05 +00:00
|
|
|
if (sShutdown) {
|
|
|
|
gCacheDataDesc = "shutting down in GetInstanceForService()";
|
|
|
|
return nullptr;
|
|
|
|
}
|
2011-06-03 06:01:02 +00:00
|
|
|
|
2011-05-23 14:54:03 +00:00
|
|
|
sPreferences = new Preferences();
|
2017-11-09 06:59:51 +00:00
|
|
|
|
|
|
|
MOZ_ASSERT(!gHashTable);
|
|
|
|
gHashTable = new PLDHashTable(
|
|
|
|
&pref_HashTableOps, sizeof(PrefHashEntry), PREF_HASHTABLE_INITIAL_LENGTH);
|
|
|
|
|
2017-11-14 08:06:02 +00:00
|
|
|
Result<Ok, const char*> res = InitInitialObjects();
|
2017-09-06 05:17:05 +00:00
|
|
|
if (res.isErr()) {
|
2017-11-01 02:55:28 +00:00
|
|
|
sPreferences = nullptr;
|
2017-09-06 05:17:05 +00:00
|
|
|
gCacheDataDesc = res.unwrapErr();
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2011-05-23 14:54:03 +00:00
|
|
|
}
|
2011-05-28 23:42:57 +00:00
|
|
|
|
2017-11-12 22:19:55 +00:00
|
|
|
if (!XRE_IsParentProcess()) {
|
2017-11-09 06:59:51 +00:00
|
|
|
MOZ_ASSERT(gInitPrefs);
|
|
|
|
for (unsigned int i = 0; i < gInitPrefs->Length(); i++) {
|
|
|
|
Preferences::SetPreference(gInitPrefs->ElementAt(i));
|
|
|
|
}
|
|
|
|
delete gInitPrefs;
|
|
|
|
gInitPrefs = nullptr;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Check if there is a deployment configuration file. If so, set up the
|
|
|
|
// pref config machinery, which will actually read the file.
|
|
|
|
nsAutoCString lockFileName;
|
2017-11-14 08:06:02 +00:00
|
|
|
nsresult rv = Preferences::GetCString(
|
|
|
|
"general.config.filename", lockFileName, PrefValueKind::User);
|
2017-11-09 06:59:51 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
NS_CreateServicesFromCategory(
|
|
|
|
"pref-config-startup",
|
|
|
|
static_cast<nsISupports*>(static_cast<void*>(sPreferences)),
|
|
|
|
"pref-config-startup");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (!observerService) {
|
|
|
|
sPreferences = nullptr;
|
|
|
|
gCacheDataDesc = "GetObserverService() failed (1)";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
observerService->AddObserver(
|
|
|
|
sPreferences, "profile-before-change-telemetry", true);
|
|
|
|
rv =
|
|
|
|
observerService->AddObserver(sPreferences, "profile-before-change", true);
|
|
|
|
|
|
|
|
observerService->AddObserver(
|
|
|
|
sPreferences, "suspend_process_notification", true);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
sPreferences = nullptr;
|
|
|
|
gCacheDataDesc = "AddObserver(\"profile-before-change\") failed";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
gCacheData = new nsTArray<nsAutoPtr<CacheData>>();
|
2017-09-06 05:17:05 +00:00
|
|
|
gCacheDataDesc = "set by GetInstanceForService()";
|
2011-05-28 23:42:57 +00:00
|
|
|
|
2013-01-14 06:45:04 +00:00
|
|
|
// Preferences::GetInstanceForService() can be called from GetService(), and
|
2013-11-07 05:35:30 +00:00
|
|
|
// RegisterStrongMemoryReporter calls GetService(nsIMemoryReporter). To
|
|
|
|
// avoid a potential recursive GetService() call, we can't register the
|
|
|
|
// memory reporter here; instead, do it off a runnable.
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<AddPreferencesMemoryReporterRunnable> runnable =
|
2013-01-14 06:43:25 +00:00
|
|
|
new AddPreferencesMemoryReporterRunnable();
|
|
|
|
NS_DispatchToMainThread(runnable);
|
2012-10-11 15:36:21 +00:00
|
|
|
|
2017-10-17 04:08:42 +00:00
|
|
|
return do_AddRef(sPreferences);
|
2011-11-06 19:27:56 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ bool
|
2016-06-27 06:33:14 +00:00
|
|
|
Preferences::IsServiceAvailable()
|
|
|
|
{
|
|
|
|
return !!sPreferences;
|
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ bool
|
2011-11-06 19:27:56 +00:00
|
|
|
Preferences::InitStaticMembers()
|
|
|
|
{
|
2017-04-11 13:39:32 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread() || mozilla::ServoStyleSet::IsInServoTraversal());
|
2014-03-06 01:58:01 +00:00
|
|
|
|
2017-11-12 22:19:49 +00:00
|
|
|
if (MOZ_LIKELY(sPreferences)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sShutdown) {
|
2017-04-11 13:39:32 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2011-11-06 19:27:56 +00:00
|
|
|
nsCOMPtr<nsIPrefService> prefService =
|
|
|
|
do_GetService(NS_PREFSERVICE_CONTRACTID);
|
|
|
|
}
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
return sPreferences != nullptr;
|
2011-05-23 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ void
|
2011-05-23 14:54:03 +00:00
|
|
|
Preferences::Shutdown()
|
|
|
|
{
|
2011-11-06 19:27:56 +00:00
|
|
|
if (!sShutdown) {
|
2011-10-17 14:59:28 +00:00
|
|
|
sShutdown = true; // Don't create the singleton instance after here.
|
2017-11-01 02:55:28 +00:00
|
|
|
sPreferences = nullptr;
|
2011-06-03 06:01:02 +00:00
|
|
|
}
|
2011-05-23 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
2003-05-02 04:00:19 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2001-03-22 23:34:41 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
//
|
|
|
|
// Constructor/Destructor
|
|
|
|
//
|
2001-03-22 23:34:41 +00:00
|
|
|
|
2017-11-01 02:41:14 +00:00
|
|
|
Preferences::Preferences()
|
2017-11-14 08:06:02 +00:00
|
|
|
: mRootBranch(new nsPrefBranch("", PrefValueKind::User))
|
|
|
|
, mDefaultRootBranch(new nsPrefBranch("", PrefValueKind::Default))
|
2017-11-01 02:41:14 +00:00
|
|
|
{
|
|
|
|
}
|
2001-03-22 23:34:41 +00:00
|
|
|
|
2011-05-23 14:54:02 +00:00
|
|
|
Preferences::~Preferences()
|
2001-03-22 23:34:41 +00:00
|
|
|
{
|
2017-11-01 02:55:28 +00:00
|
|
|
MOZ_ASSERT(!sPreferences);
|
2011-06-03 06:01:02 +00:00
|
|
|
|
|
|
|
delete gCacheData;
|
2012-07-30 14:20:58 +00:00
|
|
|
gCacheData = nullptr;
|
2011-06-03 06:01:02 +00:00
|
|
|
|
2017-11-09 06:56:29 +00:00
|
|
|
NS_ASSERTION(!gCallbacksInProgress,
|
|
|
|
"~Preferences was called while gCallbacksInProgress is true!");
|
|
|
|
|
|
|
|
CallbackNode* node = gFirstCallback;
|
|
|
|
while (node) {
|
|
|
|
CallbackNode* next_node = node->mNext;
|
|
|
|
free(const_cast<char*>(node->mDomain));
|
|
|
|
free(node);
|
|
|
|
node = next_node;
|
|
|
|
}
|
|
|
|
gLastPriorityNode = gFirstCallback = nullptr;
|
|
|
|
|
2017-11-09 06:58:35 +00:00
|
|
|
delete gHashTable;
|
|
|
|
gHashTable = nullptr;
|
|
|
|
gPrefNameArena.Clear();
|
2001-03-22 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
//
|
|
|
|
// nsISupports Implementation
|
|
|
|
//
|
2001-03-29 00:48:30 +00:00
|
|
|
|
2013-07-19 02:23:44 +00:00
|
|
|
NS_IMPL_ADDREF(Preferences)
|
|
|
|
NS_IMPL_RELEASE(Preferences)
|
2001-03-29 00:48:30 +00:00
|
|
|
|
2011-05-23 14:54:02 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN(Preferences)
|
2017-10-05 23:06:47 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIPrefService)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIPrefService)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIPrefBranch)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
2001-03-29 00:48:30 +00:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
//
|
|
|
|
// nsIPrefService Implementation
|
|
|
|
//
|
2001-03-29 00:48:30 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ void
|
|
|
|
Preferences::SetInitPreferences(nsTArray<PrefSetting>* aPrefs)
|
|
|
|
{
|
2017-02-05 05:52:38 +00:00
|
|
|
gInitPrefs = new InfallibleTArray<PrefSetting>(mozilla::Move(*aPrefs));
|
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ void
|
2017-07-20 20:19:39 +00:00
|
|
|
Preferences::InitializeUserPrefs()
|
2011-11-08 05:20:42 +00:00
|
|
|
{
|
2017-07-20 20:19:39 +00:00
|
|
|
MOZ_ASSERT(!sPreferences->mCurrentFile, "Should only initialize prefs once");
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// Prefs which are set before we initialize the profile are silently
|
|
|
|
// discarded. This is stupid, but there are various tests which depend on
|
|
|
|
// this behavior.
|
2011-11-08 05:20:42 +00:00
|
|
|
sPreferences->ResetUserPrefs();
|
2017-06-21 19:26:10 +00:00
|
|
|
|
2017-07-20 20:19:39 +00:00
|
|
|
nsCOMPtr<nsIFile> prefsFile = sPreferences->ReadSavedPrefs();
|
|
|
|
sPreferences->ReadUserOverridePrefs();
|
|
|
|
|
|
|
|
sPreferences->mDirty = false;
|
2017-06-21 19:26:10 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// Don't set mCurrentFile until we're done so that dirty flags work properly.
|
2017-07-20 20:19:39 +00:00
|
|
|
sPreferences->mCurrentFile = prefsFile.forget();
|
2017-06-21 19:26:10 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// Migrate the old prerelease telemetry pref.
|
2017-06-21 19:26:10 +00:00
|
|
|
if (!Preferences::GetBool(kOldTelemetryPref, true)) {
|
2017-11-14 08:06:02 +00:00
|
|
|
Preferences::SetBoolInAnyProcess(kTelemetryPref, false);
|
2017-06-21 19:26:10 +00:00
|
|
|
Preferences::ClearUser(kOldTelemetryPref);
|
|
|
|
}
|
|
|
|
|
|
|
|
sPreferences->NotifyServiceObservers(NS_PREFSERVICE_READ_TOPIC_ID);
|
2011-11-08 05:20:42 +00:00
|
|
|
}
|
|
|
|
|
2011-05-23 14:54:02 +00:00
|
|
|
NS_IMETHODIMP
|
2017-10-05 23:06:47 +00:00
|
|
|
Preferences::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const char16_t* someData)
|
2001-03-22 23:34:41 +00:00
|
|
|
{
|
2016-01-01 12:39:02 +00:00
|
|
|
if (MOZ_UNLIKELY(!XRE_IsParentProcess())) {
|
2010-09-09 03:38:12 +00:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2016-01-01 12:39:02 +00:00
|
|
|
}
|
2010-09-09 03:38:12 +00:00
|
|
|
|
2001-03-22 23:34:41 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2001-10-19 20:52:59 +00:00
|
|
|
if (!nsCRT::strcmp(aTopic, "profile-before-change")) {
|
2017-06-21 15:17:01 +00:00
|
|
|
// Normally prefs aren't written after this point, and so we kick off
|
|
|
|
// an asynchronous pref save so that I/O can be done in parallel with
|
|
|
|
// other shutdown.
|
2017-06-14 17:38:08 +00:00
|
|
|
if (AllowOffMainThreadSave()) {
|
2017-06-21 15:17:01 +00:00
|
|
|
SavePrefFile(nullptr);
|
2017-06-14 17:38:08 +00:00
|
|
|
}
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2017-06-21 15:17:01 +00:00
|
|
|
} else if (!nsCRT::strcmp(aTopic, "profile-before-change-telemetry")) {
|
|
|
|
// It's possible that a profile-before-change observer after ours
|
|
|
|
// set a pref. A blocking save here re-saves if necessary and also waits
|
|
|
|
// for any pending saves to complete.
|
|
|
|
SavePrefFileBlocking();
|
|
|
|
MOZ_ASSERT(!mDirty, "Preferences should not be dirty");
|
|
|
|
mProfileShutdown = true;
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2007-05-02 20:13:33 +00:00
|
|
|
} else if (!nsCRT::strcmp(aTopic, "reload-default-prefs")) {
|
|
|
|
// Reload the default prefs from file.
|
2017-11-14 08:06:02 +00:00
|
|
|
Unused << InitInitialObjects();
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2013-05-16 18:57:26 +00:00
|
|
|
} else if (!nsCRT::strcmp(aTopic, "suspend_process_notification")) {
|
|
|
|
// Our process is being suspended. The OS may wake our process later,
|
|
|
|
// or it may kill the process. In case our process is going to be killed
|
|
|
|
// from the suspended state, we save preferences before suspending.
|
2017-06-14 17:38:08 +00:00
|
|
|
rv = SavePrefFileBlocking();
|
2001-03-22 23:34:41 +00:00
|
|
|
}
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2001-03-22 23:34:41 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-05-23 14:54:02 +00:00
|
|
|
NS_IMETHODIMP
|
2017-10-05 23:06:47 +00:00
|
|
|
Preferences::ReadUserPrefsFromFile(nsIFile* aFile)
|
2001-03-22 23:34:41 +00:00
|
|
|
{
|
2017-11-12 22:19:55 +00:00
|
|
|
ENSURE_MAIN_PROCESS("Preferences::ReadUserPrefsFromFile", "all prefs");
|
2010-05-26 19:30:03 +00:00
|
|
|
|
2017-06-21 19:26:10 +00:00
|
|
|
if (!aFile) {
|
|
|
|
NS_ERROR("ReadUserPrefsFromFile requires a parameter");
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2001-03-22 23:34:41 +00:00
|
|
|
}
|
2012-03-23 01:13:20 +00:00
|
|
|
|
2017-06-21 19:26:10 +00:00
|
|
|
return openPrefFile(aFile);
|
2001-03-22 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
2011-05-23 14:54:02 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Preferences::ResetPrefs()
|
2001-03-22 23:34:41 +00:00
|
|
|
{
|
2017-11-12 22:19:55 +00:00
|
|
|
ENSURE_MAIN_PROCESS("Preferences::ResetPrefs", "all prefs");
|
2010-05-26 19:30:03 +00:00
|
|
|
|
2002-04-16 18:48:19 +00:00
|
|
|
NotifyServiceObservers(NS_PREFSERVICE_RESET_TOPIC_ID);
|
2001-09-05 18:22:10 +00:00
|
|
|
|
2017-11-09 06:58:35 +00:00
|
|
|
gHashTable->ClearAndPrepareForLength(PREF_HASHTABLE_INITIAL_LENGTH);
|
|
|
|
gPrefNameArena.Clear();
|
2001-09-05 18:22:10 +00:00
|
|
|
|
2017-11-14 08:06:02 +00:00
|
|
|
return InitInitialObjects().isOk() ? NS_OK : NS_ERROR_FAILURE;
|
2001-03-22 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
2011-05-23 14:54:02 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Preferences::ResetUserPrefs()
|
2001-03-22 23:34:41 +00:00
|
|
|
{
|
2017-11-12 22:19:55 +00:00
|
|
|
ENSURE_MAIN_PROCESS("Preferences::ResetUserPrefs", "all prefs");
|
2010-05-26 19:30:03 +00:00
|
|
|
|
2001-03-22 23:34:41 +00:00
|
|
|
PREF_ClearAllUserPrefs();
|
2016-01-01 12:39:02 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Preferences::AllowOffMainThreadSave()
|
|
|
|
{
|
2017-10-05 23:06:47 +00:00
|
|
|
// Put in a preference that allows us to disable off main thread preference
|
|
|
|
// file save.
|
2017-06-14 17:37:28 +00:00
|
|
|
if (sAllowOMTPrefWrite < 0) {
|
|
|
|
bool value = false;
|
|
|
|
Preferences::GetBool("preferences.allow.omt-write", &value);
|
|
|
|
sAllowOMTPrefWrite = value ? 1 : 0;
|
|
|
|
}
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2017-06-14 17:37:28 +00:00
|
|
|
return !!sAllowOMTPrefWrite;
|
2016-01-01 12:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Preferences::SavePrefFileBlocking()
|
|
|
|
{
|
2017-06-14 17:37:28 +00:00
|
|
|
if (mDirty) {
|
|
|
|
return SavePrefFileInternal(nullptr, SaveMethod::Blocking);
|
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// If we weren't dirty to start, SavePrefFileInternal will early exit so
|
|
|
|
// there is no guarantee that we don't have oustanding async saves in the
|
|
|
|
// pipe. Since the contract of SavePrefFileOnMainThread is that the file on
|
|
|
|
// disk matches the preferences, we have to make sure those requests are
|
|
|
|
// completed.
|
2017-06-14 17:37:28 +00:00
|
|
|
|
|
|
|
if (AllowOffMainThreadSave()) {
|
|
|
|
PreferencesWriter::Flush();
|
|
|
|
}
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2017-06-14 17:37:28 +00:00
|
|
|
return NS_OK;
|
2016-01-01 12:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Preferences::SavePrefFileAsynchronous()
|
|
|
|
{
|
|
|
|
return SavePrefFileInternal(nullptr, SaveMethod::Asynchronous);
|
2001-03-22 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
2011-05-23 14:54:02 +00:00
|
|
|
NS_IMETHODIMP
|
2017-10-05 23:06:47 +00:00
|
|
|
Preferences::SavePrefFile(nsIFile* aFile)
|
2001-03-22 23:34:41 +00:00
|
|
|
{
|
2017-10-05 23:06:47 +00:00
|
|
|
// This is the method accessible from service API. Make it off main thread.
|
2016-01-01 12:39:02 +00:00
|
|
|
return SavePrefFileInternal(aFile, SaveMethod::Asynchronous);
|
2001-03-22 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 08:06:02 +00:00
|
|
|
/* static */ nsresult
|
|
|
|
Preferences::SetValueFromDom(const char* aPrefName,
|
|
|
|
const dom::PrefValue& aValue,
|
|
|
|
PrefValueKind aKind)
|
|
|
|
{
|
|
|
|
switch (aValue.type()) {
|
|
|
|
case dom::PrefValue::TnsCString:
|
2017-11-14 08:06:02 +00:00
|
|
|
return Preferences::SetCStringInAnyProcess(
|
|
|
|
aPrefName, aValue.get_nsCString(), aKind);
|
2017-11-14 08:06:02 +00:00
|
|
|
|
|
|
|
case dom::PrefValue::Tint32_t:
|
2017-11-14 08:06:02 +00:00
|
|
|
return Preferences::SetIntInAnyProcess(
|
|
|
|
aPrefName, aValue.get_int32_t(), aKind);
|
2017-11-14 08:06:02 +00:00
|
|
|
|
|
|
|
case dom::PrefValue::Tbool:
|
2017-11-14 08:06:02 +00:00
|
|
|
return Preferences::SetBoolInAnyProcess(
|
|
|
|
aPrefName, aValue.get_bool(), aKind);
|
2017-11-14 08:06:02 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-02 14:51:39 +00:00
|
|
|
void
|
2012-08-22 20:00:21 +00:00
|
|
|
Preferences::SetPreference(const PrefSetting& aPref)
|
2010-09-27 21:23:35 +00:00
|
|
|
{
|
2017-10-31 21:59:30 +00:00
|
|
|
const char* prefName = aPref.name().get();
|
|
|
|
const dom::MaybePrefValue& defaultValue = aPref.defaultValue();
|
|
|
|
const dom::MaybePrefValue& userValue = aPref.userValue();
|
|
|
|
|
|
|
|
if (defaultValue.type() == dom::MaybePrefValue::TPrefValue) {
|
2017-11-14 08:06:02 +00:00
|
|
|
nsresult rv = SetValueFromDom(
|
2017-11-12 22:20:42 +00:00
|
|
|
prefName, defaultValue.get_PrefValue(), PrefValueKind::Default);
|
2017-10-31 21:59:30 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userValue.type() == dom::MaybePrefValue::TPrefValue) {
|
2017-11-14 08:06:02 +00:00
|
|
|
SetValueFromDom(prefName, userValue.get_PrefValue(), PrefValueKind::User);
|
2017-10-31 21:59:30 +00:00
|
|
|
} else {
|
|
|
|
PREF_ClearUserPref(prefName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: we should never try to clear a default value, that doesn't
|
|
|
|
// make sense
|
2010-09-27 21:23:35 +00:00
|
|
|
}
|
|
|
|
|
2011-11-02 14:51:39 +00:00
|
|
|
void
|
2012-08-22 20:00:21 +00:00
|
|
|
Preferences::GetPreference(PrefSetting* aPref)
|
2011-01-06 04:54:47 +00:00
|
|
|
{
|
2017-10-05 23:06:47 +00:00
|
|
|
PrefHashEntry* entry = pref_HashTableLookup(aPref->name().get());
|
|
|
|
if (!entry) {
|
2012-08-22 20:00:21 +00:00
|
|
|
return;
|
2017-10-05 23:06:47 +00:00
|
|
|
}
|
2010-09-28 17:02:37 +00:00
|
|
|
|
2016-05-17 14:55:49 +00:00
|
|
|
if (pref_EntryHasAdvisablySizedValues(entry)) {
|
|
|
|
pref_GetPrefFromEntry(entry, aPref);
|
|
|
|
}
|
2010-09-28 17:02:37 +00:00
|
|
|
}
|
|
|
|
|
2011-11-02 14:51:39 +00:00
|
|
|
void
|
2012-08-22 20:00:21 +00:00
|
|
|
Preferences::GetPreferences(InfallibleTArray<PrefSetting>* aPrefs)
|
2010-09-28 17:02:37 +00:00
|
|
|
{
|
2015-05-18 07:52:08 +00:00
|
|
|
aPrefs->SetCapacity(gHashTable->Capacity());
|
2015-06-19 00:58:24 +00:00
|
|
|
for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
auto entry = static_cast<PrefHashEntry*>(iter.Get());
|
2016-05-17 14:55:49 +00:00
|
|
|
|
|
|
|
if (!pref_EntryHasAdvisablySizedValues(entry)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
dom::PrefSetting* pref = aPrefs->AppendElement();
|
2015-06-19 00:58:24 +00:00
|
|
|
pref_GetPrefFromEntry(entry, pref);
|
|
|
|
}
|
2010-09-28 17:02:37 +00:00
|
|
|
}
|
|
|
|
|
2017-02-05 05:52:38 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
2017-10-26 05:14:00 +00:00
|
|
|
Preferences::SetInitPhase(pref_initPhase aPhase)
|
2017-02-05 05:52:38 +00:00
|
|
|
{
|
2017-10-26 05:14:00 +00:00
|
|
|
gPhase = aPhase;
|
2017-02-05 05:52:38 +00:00
|
|
|
}
|
2017-03-16 21:43:17 +00:00
|
|
|
|
|
|
|
pref_initPhase
|
|
|
|
Preferences::InitPhase()
|
|
|
|
{
|
2017-10-26 05:14:00 +00:00
|
|
|
return gPhase;
|
2017-03-16 21:43:17 +00:00
|
|
|
}
|
2017-02-05 05:52:38 +00:00
|
|
|
#endif
|
|
|
|
|
2011-05-23 14:54:02 +00:00
|
|
|
NS_IMETHODIMP
|
2017-10-05 23:06:47 +00:00
|
|
|
Preferences::GetBranch(const char* aPrefRoot, nsIPrefBranch** aRetVal)
|
2001-03-22 23:34:41 +00:00
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
if ((nullptr != aPrefRoot) && (*aPrefRoot != '\0')) {
|
2017-10-05 23:06:47 +00:00
|
|
|
// TODO: Cache this stuff and allow consumers to share branches (hold weak
|
|
|
|
// references, I think).
|
2017-11-14 08:06:02 +00:00
|
|
|
RefPtr<nsPrefBranch> prefBranch =
|
|
|
|
new nsPrefBranch(aPrefRoot, PrefValueKind::User);
|
2017-10-05 23:06:47 +00:00
|
|
|
prefBranch.forget(aRetVal);
|
2001-04-18 05:18:47 +00:00
|
|
|
} else {
|
2017-10-05 23:06:47 +00:00
|
|
|
// Special case: caching the default root.
|
2017-11-01 02:41:14 +00:00
|
|
|
nsCOMPtr<nsIPrefBranch> root(sPreferences->mRootBranch);
|
2017-10-05 23:06:47 +00:00
|
|
|
root.forget(aRetVal);
|
2001-04-18 05:18:47 +00:00
|
|
|
}
|
2017-10-05 23:06:47 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2001-03-22 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
2011-05-23 14:54:02 +00:00
|
|
|
NS_IMETHODIMP
|
2017-10-05 23:06:47 +00:00
|
|
|
Preferences::GetDefaultBranch(const char* aPrefRoot, nsIPrefBranch** aRetVal)
|
2001-03-22 23:34:41 +00:00
|
|
|
{
|
2011-06-22 06:39:10 +00:00
|
|
|
if (!aPrefRoot || !aPrefRoot[0]) {
|
2017-11-01 02:41:14 +00:00
|
|
|
nsCOMPtr<nsIPrefBranch> root(sPreferences->mDefaultRootBranch);
|
2017-10-05 23:06:47 +00:00
|
|
|
root.forget(aRetVal);
|
2014-03-17 17:00:17 +00:00
|
|
|
return NS_OK;
|
2011-06-22 06:39:10 +00:00
|
|
|
}
|
2001-03-22 23:34:41 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// TODO: Cache this stuff and allow consumers to share branches (hold weak
|
|
|
|
// references, I think).
|
2017-11-14 08:06:02 +00:00
|
|
|
RefPtr<nsPrefBranch> prefBranch =
|
|
|
|
new nsPrefBranch(aPrefRoot, PrefValueKind::Default);
|
2017-10-05 23:06:47 +00:00
|
|
|
if (!prefBranch) {
|
2003-12-21 06:17:26 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2017-10-05 23:06:47 +00:00
|
|
|
}
|
2001-03-22 23:34:41 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
prefBranch.forget(aRetVal);
|
2014-03-17 17:00:17 +00:00
|
|
|
return NS_OK;
|
2001-03-22 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
2014-09-04 13:52:28 +00:00
|
|
|
NS_IMETHODIMP
|
2017-10-05 23:06:47 +00:00
|
|
|
Preferences::GetDirty(bool* aRetVal)
|
|
|
|
{
|
|
|
|
*aRetVal = mDirty;
|
2014-09-04 13:52:28 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-03-22 23:34:41 +00:00
|
|
|
|
2011-05-23 14:54:02 +00:00
|
|
|
nsresult
|
2017-10-05 23:06:47 +00:00
|
|
|
Preferences::NotifyServiceObservers(const char* aTopic)
|
2001-09-05 18:22:10 +00:00
|
|
|
{
|
2017-07-06 12:00:35 +00:00
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
2017-10-05 23:06:47 +00:00
|
|
|
if (!observerService) {
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 16:59:13 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2017-10-05 23:06:47 +00:00
|
|
|
}
|
2001-09-05 18:22:10 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
auto subject = static_cast<nsIPrefService*>(this);
|
2012-07-30 14:20:58 +00:00
|
|
|
observerService->NotifyObservers(subject, aTopic, nullptr);
|
2017-07-06 12:00:35 +00:00
|
|
|
|
2001-09-05 18:22:10 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:19:39 +00:00
|
|
|
already_AddRefed<nsIFile>
|
|
|
|
Preferences::ReadSavedPrefs()
|
2001-03-29 00:48:30 +00:00
|
|
|
{
|
2017-06-21 19:26:10 +00:00
|
|
|
nsCOMPtr<nsIFile> file;
|
2017-10-05 23:06:47 +00:00
|
|
|
nsresult rv =
|
|
|
|
NS_GetSpecialDirectory(NS_APP_PREFS_50_FILE, getter_AddRefs(file));
|
2017-06-21 19:26:10 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2017-07-20 20:19:39 +00:00
|
|
|
return nullptr;
|
2017-06-21 19:26:10 +00:00
|
|
|
}
|
2017-06-21 19:26:10 +00:00
|
|
|
|
2017-06-21 19:26:10 +00:00
|
|
|
rv = openPrefFile(file);
|
|
|
|
if (rv == NS_ERROR_FILE_NOT_FOUND) {
|
2017-10-05 23:06:47 +00:00
|
|
|
// This is a normal case for new users.
|
|
|
|
Telemetry::ScalarSet(
|
|
|
|
Telemetry::ScalarID::PREFERENCES_CREATED_NEW_USER_PREFS_FILE, true);
|
2017-06-21 19:26:10 +00:00
|
|
|
rv = NS_OK;
|
|
|
|
} else if (NS_FAILED(rv)) {
|
|
|
|
// Save a backup copy of the current (invalid) prefs file, since all prefs
|
|
|
|
// from the error line to the end of the file will be lost (bug 361102).
|
|
|
|
// TODO we should notify the user about it (bug 523725).
|
2017-10-05 23:06:47 +00:00
|
|
|
Telemetry::ScalarSet(
|
|
|
|
Telemetry::ScalarID::PREFERENCES_PREFS_FILE_WAS_INVALID, true);
|
2017-06-21 19:26:10 +00:00
|
|
|
MakeBackupPrefFile(file);
|
2001-03-29 00:48:30 +00:00
|
|
|
}
|
2016-01-01 12:39:02 +00:00
|
|
|
|
2017-07-20 20:19:39 +00:00
|
|
|
return file.forget();
|
2001-03-29 00:48:30 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 19:26:10 +00:00
|
|
|
void
|
2017-07-20 20:19:39 +00:00
|
|
|
Preferences::ReadUserOverridePrefs()
|
2001-03-29 00:48:30 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFile> aFile;
|
2017-10-05 23:06:47 +00:00
|
|
|
nsresult rv =
|
|
|
|
NS_GetSpecialDirectory(NS_APP_PREFS_50_DIR, getter_AddRefs(aFile));
|
2017-06-21 19:26:10 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aFile->AppendNative(NS_LITERAL_CSTRING("user.js"));
|
|
|
|
rv = openPrefFile(aFile);
|
|
|
|
if (rv != NS_ERROR_FILE_NOT_FOUND) {
|
2017-10-05 23:06:47 +00:00
|
|
|
// If the file exists and was at least partially read, record that in
|
|
|
|
// telemetry as it may be a sign of pref injection.
|
2017-06-21 19:26:10 +00:00
|
|
|
Telemetry::ScalarSet(Telemetry::ScalarID::PREFERENCES_READ_USER_JS, true);
|
2001-03-29 00:48:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-23 14:54:02 +00:00
|
|
|
nsresult
|
2017-10-05 23:06:47 +00:00
|
|
|
Preferences::MakeBackupPrefFile(nsIFile* aFile)
|
2006-11-28 14:11:40 +00:00
|
|
|
{
|
|
|
|
// Example: this copies "prefs.js" to "Invalidprefs.js" in the same directory.
|
2009-11-03 19:05:31 +00:00
|
|
|
// "Invalidprefs.js" is removed if it exists, prior to making the copy.
|
2006-11-28 14:11:40 +00:00
|
|
|
nsAutoString newFilename;
|
|
|
|
nsresult rv = aFile->GetLeafName(newFilename);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2017-09-08 01:08:18 +00:00
|
|
|
newFilename.InsertLiteral(u"Invalid", 0);
|
2009-11-03 19:05:31 +00:00
|
|
|
nsCOMPtr<nsIFile> newFile;
|
|
|
|
rv = aFile->GetParent(getter_AddRefs(newFile));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2009-11-03 19:05:31 +00:00
|
|
|
rv = newFile->Append(newFilename);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool exists = false;
|
2009-11-03 19:05:31 +00:00
|
|
|
newFile->Exists(&exists);
|
|
|
|
if (exists) {
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = newFile->Remove(false);
|
2009-11-03 19:05:31 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = aFile->CopyTo(nullptr, newFilename);
|
2006-11-28 14:11:40 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2006-11-28 14:11:40 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-05-23 14:54:02 +00:00
|
|
|
nsresult
|
2017-10-05 23:06:47 +00:00
|
|
|
Preferences::SavePrefFileInternal(nsIFile* aFile, SaveMethod aSaveMethod)
|
2003-05-02 04:00:19 +00:00
|
|
|
{
|
2017-11-12 22:19:55 +00:00
|
|
|
ENSURE_MAIN_PROCESS("Preferences::SavePrefFileInternal", "all prefs");
|
2016-01-01 12:39:02 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// We allow different behavior here when aFile argument is not null, but it
|
|
|
|
// happens to be the same as the current file. It is not clear that we
|
|
|
|
// should, but it does give us a "force" save on the unmodified pref file
|
|
|
|
// (see the original bug 160377 when we added this.)
|
2017-05-16 17:10:40 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
if (nullptr == aFile) {
|
2017-06-15 18:15:25 +00:00
|
|
|
mSavePending = false;
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// Off main thread writing only if allowed.
|
2016-01-01 12:39:02 +00:00
|
|
|
if (!AllowOffMainThreadSave()) {
|
|
|
|
aSaveMethod = SaveMethod::Blocking;
|
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// The mDirty flag tells us if we should write to mCurrentFile. We only
|
|
|
|
// check this flag when the caller wants to write to the default.
|
2016-07-21 19:46:15 +00:00
|
|
|
if (!mDirty) {
|
2003-05-02 04:00:19 +00:00
|
|
|
return NS_OK;
|
2016-07-21 19:46:15 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// Check for profile shutdown after mDirty because the runnables from
|
2017-10-26 05:14:01 +00:00
|
|
|
// HandleDirty() can still be pending.
|
2017-06-21 15:17:01 +00:00
|
|
|
if (mProfileShutdown) {
|
|
|
|
NS_WARNING("Cannot save pref file after profile shutdown.");
|
|
|
|
return NS_ERROR_ILLEGAL_DURING_SHUTDOWN;
|
|
|
|
}
|
|
|
|
|
2003-05-02 04:00:19 +00:00
|
|
|
// It's possible that we never got a prefs file.
|
|
|
|
nsresult rv = NS_OK;
|
2017-05-16 17:10:40 +00:00
|
|
|
if (mCurrentFile) {
|
2016-01-01 12:39:02 +00:00
|
|
|
rv = WritePrefFile(mCurrentFile, aSaveMethod);
|
2017-05-16 17:10:40 +00:00
|
|
|
}
|
2003-05-02 04:00:19 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// If we succeeded writing to mCurrentFile, reset the dirty flag.
|
2017-05-16 17:10:40 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mDirty = false;
|
|
|
|
}
|
2003-05-02 04:00:19 +00:00
|
|
|
return rv;
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2003-05-02 04:00:19 +00:00
|
|
|
} else {
|
2017-10-05 23:06:47 +00:00
|
|
|
// We only allow off main thread writes on mCurrentFile.
|
2016-01-01 12:39:02 +00:00
|
|
|
return WritePrefFile(aFile, SaveMethod::Blocking);
|
2003-05-02 04:00:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-23 14:54:02 +00:00
|
|
|
nsresult
|
2017-06-14 17:37:28 +00:00
|
|
|
Preferences::WritePrefFile(nsIFile* aFile, SaveMethod aSaveMethod)
|
2002-04-16 18:48:19 +00:00
|
|
|
{
|
2016-01-01 12:39:02 +00:00
|
|
|
if (!gHashTable) {
|
2002-04-16 18:48:19 +00:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2016-01-01 12:39:02 +00:00
|
|
|
}
|
2002-04-16 18:48:19 +00:00
|
|
|
|
Bug 1375392 - Tweak the PROFILER_LABEL* macros. r=mstange.
This patch makes the following changes to the macros.
- Removes PROFILER_LABEL_FUNC. It's only suitable for use in functions outside
classes, due to PROFILER_FUNCTION_NAME not getting class names, and it was
mostly misused.
- Removes PROFILER_FUNCTION_NAME. It's no longer used, and __func__ is
universally available now anyway.
- Combines the first two string literal arguments of PROFILER_LABEL and
PROFILER_LABEL_DYNAMIC into a single argument. There was no good reason for
them to be separate, and it forced a '::' in the label, which isn't always
appropriate. Also, the meaning of the "name_space" argument was interpreted
in an interesting variety of ways.
- Adds an "AUTO_" prefix to PROFILER_LABEL and PROFILER_LABEL_DYNAMIC, to make
it clearer they construct RAII objects rather than just being function calls.
(I myself have screwed up the scoping because of this in the past.)
- Fills in the 'js::ProfileEntry::Category::' qualifier within the macro, so
the caller doesn't need to. This makes a *lot* more of the uses fit onto a
single line.
The patch also makes the following changes to the macro uses (beyond those
required by the changes described above).
- Fixes a bunch of labels that had gotten out of sync with the name of the
class and/or function that encloses them.
- Removes a useless PROFILER_LABEL use within a trivial scope in
EventStateManager::DispatchMouseOrPointerEvent(). It clearly wasn't serving
any useful purpose. It also serves as extra evidence that the AUTO_ prefix is
a good idea.
- Tweaks DecodePool::SyncRunIf{Preferred,Possible} so that the labelling is
done within them, instead of at their callsites, because that's a more
standard way of doing things.
--HG--
extra : rebase_source : 318d1bc6fc1425a94aacbf489dd46e4f83211de4
2017-06-22 07:08:53 +00:00
|
|
|
AUTO_PROFILER_LABEL("Preferences::WritePrefFile", OTHER);
|
2017-05-03 20:18:30 +00:00
|
|
|
|
2017-06-14 17:37:28 +00:00
|
|
|
if (AllowOffMainThreadSave()) {
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
2017-06-15 21:33:20 +00:00
|
|
|
mozilla::UniquePtr<PrefSaveData> prefs =
|
2017-10-26 05:26:24 +00:00
|
|
|
MakeUnique<PrefSaveData>(pref_savePrefs());
|
2017-06-14 17:37:28 +00:00
|
|
|
|
|
|
|
// Put the newly constructed preference data into sPendingWriteData
|
|
|
|
// for the next request to pick up
|
2017-06-15 21:33:20 +00:00
|
|
|
prefs.reset(PreferencesWriter::sPendingWriteData.exchange(prefs.release()));
|
2017-06-14 17:37:28 +00:00
|
|
|
if (prefs) {
|
|
|
|
// There was a previous request that hasn't been processed,
|
2017-06-15 21:33:20 +00:00
|
|
|
// and this is the data it had.
|
2017-06-14 17:37:28 +00:00
|
|
|
return rv;
|
2017-10-05 23:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// There were no previous requests. Dispatch one since sPendingWriteData has
|
|
|
|
// the up to date information.
|
|
|
|
nsCOMPtr<nsIEventTarget> target =
|
|
|
|
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
bool async = aSaveMethod == SaveMethod::Asynchronous;
|
|
|
|
if (async) {
|
|
|
|
rv = target->Dispatch(new PWRunnable(aFile),
|
|
|
|
nsIEventTarget::DISPATCH_NORMAL);
|
|
|
|
} else {
|
|
|
|
// Note that we don't get the nsresult return value here.
|
|
|
|
SyncRunnable::DispatchToThread(target, new PWRunnable(aFile), true);
|
2017-06-14 17:37:28 +00:00
|
|
|
}
|
2017-10-05 23:06:47 +00:00
|
|
|
return rv;
|
2017-06-14 17:37:28 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// If we can't get the thread for writing, for whatever reason, do the main
|
|
|
|
// thread write after making some noise.
|
|
|
|
MOZ_ASSERT(false, "failed to get the target thread for OMT pref write");
|
2017-06-14 17:37:28 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// This will do a main thread write. It is safe to do it this way because
|
|
|
|
// AllowOffMainThreadSave() returns a consistent value for the lifetime of
|
|
|
|
// the parent process.
|
2017-10-26 05:26:24 +00:00
|
|
|
PrefSaveData prefsData = pref_savePrefs();
|
2017-06-15 21:33:20 +00:00
|
|
|
return PreferencesWriter::Write(aFile, prefsData);
|
2002-04-16 18:48:19 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
static nsresult
|
|
|
|
openPrefFile(nsIFile* aFile)
|
2009-09-13 15:36:23 +00:00
|
|
|
{
|
2003-09-23 17:57:22 +00:00
|
|
|
PrefParseState ps;
|
2016-04-22 19:35:01 +00:00
|
|
|
PREF_InitParseState(&ps, PREF_ReaderCallback, ReportToConsole, nullptr);
|
2017-10-05 23:06:47 +00:00
|
|
|
auto cleanup = MakeScopeExit([&]() { PREF_FinalizeParseState(&ps); });
|
2008-04-02 06:41:23 +00:00
|
|
|
|
2017-09-01 00:02:09 +00:00
|
|
|
nsCString data;
|
|
|
|
MOZ_TRY_VAR(data, URLPreloader::ReadFile(aFile));
|
|
|
|
if (!PREF_ParseBuf(&ps, data.get(), data.Length())) {
|
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
2008-01-30 23:39:42 +00:00
|
|
|
}
|
2008-04-02 06:41:23 +00:00
|
|
|
|
2017-09-01 00:02:09 +00:00
|
|
|
return NS_OK;
|
2001-03-22 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
//
|
|
|
|
// Some stuff that gets called from Pref_Init()
|
|
|
|
//
|
2001-03-22 23:34:41 +00:00
|
|
|
|
2003-11-03 16:46:13 +00:00
|
|
|
static int
|
2017-10-05 23:06:47 +00:00
|
|
|
pref_CompareFileNames(nsIFile* aFile1, nsIFile* aFile2, void* /* unused */)
|
2001-03-22 23:34:41 +00:00
|
|
|
{
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString filename1, filename2;
|
2003-11-03 16:46:13 +00:00
|
|
|
aFile1->GetNativeLeafName(filename1);
|
|
|
|
aFile2->GetNativeLeafName(filename2);
|
2001-03-22 23:34:41 +00:00
|
|
|
|
2003-11-06 20:11:01 +00:00
|
|
|
return Compare(filename2, filename1);
|
2003-11-03 16:46:13 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// Load default pref files from a directory. The files in the directory are
|
|
|
|
// sorted reverse-alphabetically; a set of "special file names" may be
|
|
|
|
// specified which are loaded after all the others.
|
2003-11-03 16:46:13 +00:00
|
|
|
static nsresult
|
2017-10-05 23:06:47 +00:00
|
|
|
pref_LoadPrefsInDir(nsIFile* aDir,
|
|
|
|
char const* const* aSpecialFiles,
|
|
|
|
uint32_t aSpecialFilesCount)
|
2003-11-03 16:46:13 +00:00
|
|
|
{
|
|
|
|
nsresult rv, rv2;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool hasMoreElements;
|
2003-11-03 16:46:13 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> dirIterator;
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// This may fail in some normal cases, such as embedders who do not use a
|
|
|
|
// GRE.
|
2003-11-03 16:46:13 +00:00
|
|
|
rv = aDir->GetDirectoryEntries(getter_AddRefs(dirIterator));
|
2005-03-06 18:47:02 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2017-10-05 23:06:47 +00:00
|
|
|
// If the directory doesn't exist, then we have no reason to complain. We
|
2005-03-06 18:47:02 +00:00
|
|
|
// loaded everything (and nothing) successfully.
|
2017-10-05 23:06:47 +00:00
|
|
|
if (rv == NS_ERROR_FILE_NOT_FOUND ||
|
|
|
|
rv == NS_ERROR_FILE_TARGET_DOES_NOT_EXIST) {
|
2005-03-06 18:47:02 +00:00
|
|
|
rv = NS_OK;
|
2017-10-05 23:06:47 +00:00
|
|
|
}
|
2005-03-06 18:47:02 +00:00
|
|
|
return rv;
|
|
|
|
}
|
2003-11-03 16:46:13 +00:00
|
|
|
|
|
|
|
rv = dirIterator->HasMoreElements(&hasMoreElements);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMArray<nsIFile> prefFiles(INITIAL_PREF_FILES);
|
|
|
|
nsCOMArray<nsIFile> specialFiles(aSpecialFilesCount);
|
|
|
|
nsCOMPtr<nsIFile> prefFile;
|
|
|
|
|
|
|
|
while (hasMoreElements && NS_SUCCEEDED(rv)) {
|
2014-03-15 19:00:17 +00:00
|
|
|
nsCOMPtr<nsISupports> supports;
|
|
|
|
rv = dirIterator->GetNext(getter_AddRefs(supports));
|
|
|
|
prefFile = do_QueryInterface(supports);
|
2003-11-03 16:46:13 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
nsAutoCString leafName;
|
2003-11-03 16:46:13 +00:00
|
|
|
prefFile->GetNativeLeafName(leafName);
|
2017-10-05 23:06:47 +00:00
|
|
|
NS_ASSERTION(
|
|
|
|
!leafName.IsEmpty(),
|
|
|
|
"Failure in default prefs: directory enumerator returned empty file?");
|
2003-11-03 16:46:13 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// Skip non-js files.
|
|
|
|
if (StringEndsWith(leafName,
|
|
|
|
NS_LITERAL_CSTRING(".js"),
|
2005-05-14 00:30:08 +00:00
|
|
|
nsCaseInsensitiveCStringComparator())) {
|
2011-09-29 06:19:26 +00:00
|
|
|
bool shouldParse = true;
|
2017-10-05 23:06:47 +00:00
|
|
|
|
|
|
|
// Separate out special files.
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < aSpecialFilesCount; ++i) {
|
2003-11-03 16:46:13 +00:00
|
|
|
if (leafName.Equals(nsDependentCString(aSpecialFiles[i]))) {
|
2011-10-17 14:59:28 +00:00
|
|
|
shouldParse = false;
|
2017-10-05 23:06:47 +00:00
|
|
|
// Special files should be processed in order. We put them into the
|
|
|
|
// array by index, which can make the array sparse.
|
2003-11-03 16:46:13 +00:00
|
|
|
specialFiles.ReplaceObjectAt(prefFile, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldParse) {
|
|
|
|
prefFiles.AppendObject(prefFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = dirIterator->HasMoreElements(&hasMoreElements);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prefFiles.Count() + specialFiles.Count() == 0) {
|
|
|
|
NS_WARNING("No default pref files found.");
|
2001-03-22 23:34:41 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2003-11-03 16:46:13 +00:00
|
|
|
rv = NS_SUCCESS_FILE_DIRECTORY_EMPTY;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
prefFiles.Sort(pref_CompareFileNames, nullptr);
|
2017-07-06 12:00:35 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t arrayCount = prefFiles.Count();
|
|
|
|
uint32_t i;
|
2003-11-03 16:46:13 +00:00
|
|
|
for (i = 0; i < arrayCount; ++i) {
|
|
|
|
rv2 = openPrefFile(prefFiles[i]);
|
|
|
|
if (NS_FAILED(rv2)) {
|
|
|
|
NS_ERROR("Default pref file not parsed successfully.");
|
|
|
|
rv = rv2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
arrayCount = specialFiles.Count();
|
|
|
|
for (i = 0; i < arrayCount; ++i) {
|
2017-10-05 23:06:47 +00:00
|
|
|
// This may be a sparse array; test before parsing.
|
2003-11-03 16:46:13 +00:00
|
|
|
nsIFile* file = specialFiles[i];
|
|
|
|
if (file) {
|
|
|
|
rv2 = openPrefFile(file);
|
|
|
|
if (NS_FAILED(rv2)) {
|
|
|
|
NS_ERROR("Special default pref file not parsed successfully.");
|
|
|
|
rv = rv2;
|
2001-03-22 23:34:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-11-03 16:46:13 +00:00
|
|
|
|
|
|
|
return rv;
|
2001-03-22 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
static nsresult
|
|
|
|
pref_LoadPrefsInDirList(const char* aListId)
|
2007-09-01 02:16:47 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2017-10-05 23:06:47 +00:00
|
|
|
nsCOMPtr<nsIProperties> dirSvc(
|
|
|
|
do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv));
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-02-13 18:57:28 +00:00
|
|
|
return rv;
|
2017-10-05 23:06:47 +00:00
|
|
|
}
|
2007-09-01 02:16:47 +00:00
|
|
|
|
2012-02-13 18:57:28 +00:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> list;
|
2017-10-05 23:06:47 +00:00
|
|
|
dirSvc->Get(aListId, NS_GET_IID(nsISimpleEnumerator), getter_AddRefs(list));
|
|
|
|
if (!list) {
|
2012-02-13 18:57:28 +00:00
|
|
|
return NS_OK;
|
2017-10-05 23:06:47 +00:00
|
|
|
}
|
2012-02-13 18:57:28 +00:00
|
|
|
|
|
|
|
bool hasMore;
|
|
|
|
while (NS_SUCCEEDED(list->HasMoreElements(&hasMore)) && hasMore) {
|
|
|
|
nsCOMPtr<nsISupports> elem;
|
|
|
|
list->GetNext(getter_AddRefs(elem));
|
2017-10-05 23:06:47 +00:00
|
|
|
if (!elem) {
|
2012-02-13 18:57:28 +00:00
|
|
|
continue;
|
2017-10-05 23:06:47 +00:00
|
|
|
}
|
2012-02-13 18:57:28 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> path = do_QueryInterface(elem);
|
2017-10-05 23:06:47 +00:00
|
|
|
if (!path) {
|
2012-02-13 18:57:28 +00:00
|
|
|
continue;
|
2017-10-05 23:06:47 +00:00
|
|
|
}
|
2012-02-13 18:57:28 +00:00
|
|
|
|
|
|
|
// Do we care if a file provided by this process fails to load?
|
2017-10-31 05:34:57 +00:00
|
|
|
pref_LoadPrefsInDir(path, nullptr, 0);
|
2007-09-01 02:16:47 +00:00
|
|
|
}
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2007-09-01 02:16:47 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-11-03 16:46:13 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
static nsresult
|
|
|
|
pref_ReadPrefFromJar(nsZipArchive* aJarReader, const char* aName)
|
2010-06-02 21:49:33 +00:00
|
|
|
{
|
2017-09-01 00:02:09 +00:00
|
|
|
nsCString manifest;
|
2017-10-05 23:06:47 +00:00
|
|
|
MOZ_TRY_VAR(manifest,
|
|
|
|
URLPreloader::ReadZip(aJarReader, nsDependentCString(aName)));
|
2010-06-02 21:49:33 +00:00
|
|
|
|
|
|
|
PrefParseState ps;
|
2016-04-22 19:35:01 +00:00
|
|
|
PREF_InitParseState(&ps, PREF_ReaderCallback, ReportToConsole, nullptr);
|
2017-09-01 00:02:09 +00:00
|
|
|
PREF_ParseBuf(&ps, manifest.get(), manifest.Length());
|
2010-06-02 21:49:33 +00:00
|
|
|
PREF_FinalizeParseState(&ps);
|
|
|
|
|
2012-07-30 09:24:36 +00:00
|
|
|
return NS_OK;
|
2010-06-02 21:49:33 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// Initialize default preference JavaScript buffers from appropriate TEXT
|
|
|
|
// resources.
|
2017-11-14 08:06:02 +00:00
|
|
|
/* static */ Result<Ok, const char*>
|
|
|
|
Preferences::InitInitialObjects()
|
2010-06-02 21:49:33 +00:00
|
|
|
{
|
2017-10-05 23:06:47 +00:00
|
|
|
// In the omni.jar case, we load the following prefs:
|
2011-02-25 11:53:36 +00:00
|
|
|
// - jar:$gre/omni.jar!/greprefs.js
|
|
|
|
// - jar:$gre/omni.jar!/defaults/pref/*.js
|
2017-10-05 23:06:47 +00:00
|
|
|
//
|
|
|
|
// In the non-omni.jar case, we load:
|
2011-02-25 11:53:36 +00:00
|
|
|
// - $gre/greprefs.js
|
|
|
|
//
|
2011-07-28 07:04:08 +00:00
|
|
|
// In both cases, we also load:
|
2011-02-25 11:53:36 +00:00
|
|
|
// - $gre/defaults/pref/*.js
|
2017-10-05 23:06:47 +00:00
|
|
|
//
|
2011-07-28 07:04:08 +00:00
|
|
|
// This is kept for bug 591866 (channel-prefs.js should not be in omni.jar)
|
2017-10-05 23:06:47 +00:00
|
|
|
// in the `$app == $gre` case; we load all files instead of channel-prefs.js
|
|
|
|
// only to have the same behaviour as `$app != $gre`, where this is required
|
|
|
|
// as a supported location for GRE preferences.
|
2011-02-25 11:53:36 +00:00
|
|
|
//
|
2017-10-05 23:06:47 +00:00
|
|
|
// When `$app != $gre`, we additionally load, in the omni.jar case:
|
2011-02-25 11:53:36 +00:00
|
|
|
// - jar:$app/omni.jar!/defaults/preferences/*.js
|
|
|
|
// - $app/defaults/preferences/*.js
|
2017-10-05 23:06:47 +00:00
|
|
|
//
|
|
|
|
// and in the non-omni.jar case:
|
2011-02-25 11:53:36 +00:00
|
|
|
// - $app/defaults/preferences/*.js
|
2017-10-05 23:06:47 +00:00
|
|
|
//
|
|
|
|
// When `$app == $gre`, we additionally load, in the omni.jar case:
|
2012-04-19 07:39:52 +00:00
|
|
|
// - jar:$gre/omni.jar!/defaults/preferences/*.js
|
2017-10-05 23:06:47 +00:00
|
|
|
//
|
|
|
|
// Thus, in the omni.jar case, we always load app-specific default
|
|
|
|
// preferences from omni.jar, whether or not `$app == $gre`.
|
2010-06-02 21:49:33 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsZipFind* findPtr;
|
2011-02-25 11:53:36 +00:00
|
|
|
nsAutoPtr<nsZipFind> find;
|
2010-11-10 08:20:52 +00:00
|
|
|
nsTArray<nsCString> prefEntries;
|
2017-10-05 23:06:47 +00:00
|
|
|
const char* entryName;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t entryNameLen;
|
2010-11-10 08:20:52 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
RefPtr<nsZipArchive> jarReader =
|
|
|
|
mozilla::Omnijar::GetReader(mozilla::Omnijar::GRE);
|
2011-02-25 11:53:36 +00:00
|
|
|
if (jarReader) {
|
2017-10-05 23:06:47 +00:00
|
|
|
// Load jar:$gre/omni.jar!/greprefs.js.
|
2011-02-25 11:53:36 +00:00
|
|
|
rv = pref_ReadPrefFromJar(jarReader, "greprefs.js");
|
2017-09-06 05:17:05 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, Err("pref_ReadPrefFromJar() failed"));
|
2011-02-25 11:53:36 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// Load jar:$gre/omni.jar!/defaults/pref/*.js.
|
2011-02-25 11:53:36 +00:00
|
|
|
rv = jarReader->FindInit("defaults/pref/*.js$", &findPtr);
|
2017-09-06 05:17:05 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, Err("jarReader->FindInit() failed"));
|
2011-02-25 11:53:36 +00:00
|
|
|
|
2011-02-25 11:53:36 +00:00
|
|
|
find = findPtr;
|
|
|
|
while (NS_SUCCEEDED(find->FindNext(&entryName, &entryNameLen))) {
|
2011-12-09 08:35:41 +00:00
|
|
|
prefEntries.AppendElement(Substring(entryName, entryNameLen));
|
2011-02-25 11:53:36 +00:00
|
|
|
}
|
2011-05-11 01:27:51 +00:00
|
|
|
|
2011-02-25 11:53:36 +00:00
|
|
|
prefEntries.Sort();
|
2017-10-05 23:06:47 +00:00
|
|
|
for (uint32_t i = prefEntries.Length(); i--;) {
|
2011-02-25 11:53:36 +00:00
|
|
|
rv = pref_ReadPrefFromJar(jarReader, prefEntries[i].get());
|
2017-10-05 23:06:47 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2011-02-25 11:53:36 +00:00
|
|
|
NS_WARNING("Error parsing preferences.");
|
2017-10-05 23:06:47 +00:00
|
|
|
}
|
2011-02-25 11:53:36 +00:00
|
|
|
}
|
2017-10-05 23:06:47 +00:00
|
|
|
|
2011-02-25 11:53:36 +00:00
|
|
|
} else {
|
2017-10-05 23:06:47 +00:00
|
|
|
// Load $gre/greprefs.js.
|
2011-02-25 11:53:36 +00:00
|
|
|
nsCOMPtr<nsIFile> greprefsFile;
|
|
|
|
rv = NS_GetSpecialDirectory(NS_GRE_DIR, getter_AddRefs(greprefsFile));
|
2017-09-06 05:17:05 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, Err("NS_GetSpecialDirectory(NS_GRE_DIR) failed"));
|
2011-05-11 01:27:51 +00:00
|
|
|
|
2011-02-25 11:53:36 +00:00
|
|
|
rv = greprefsFile->AppendNative(NS_LITERAL_CSTRING("greprefs.js"));
|
2017-09-06 05:17:05 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, Err("greprefsFile->AppendNative() failed"));
|
2011-02-25 11:53:36 +00:00
|
|
|
|
|
|
|
rv = openPrefFile(greprefsFile);
|
2017-10-05 23:06:47 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Error parsing GRE default preferences. Is this an old-style "
|
|
|
|
"embedding app?");
|
|
|
|
}
|
2011-02-25 11:53:36 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// Load $gre/defaults/pref/*.js.
|
2011-07-28 07:04:08 +00:00
|
|
|
nsCOMPtr<nsIFile> defaultPrefDir;
|
2017-10-05 23:06:47 +00:00
|
|
|
rv = NS_GetSpecialDirectory(NS_APP_PREF_DEFAULTS_50_DIR,
|
|
|
|
getter_AddRefs(defaultPrefDir));
|
2017-09-06 05:17:05 +00:00
|
|
|
NS_ENSURE_SUCCESS(
|
|
|
|
rv, Err("NS_GetSpecialDirectory(NS_APP_PREF_DEFAULTS_50_DIR) failed"));
|
2011-02-08 18:54:52 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// These pref file names should not be used: we process them after all other
|
|
|
|
// application pref files for backwards compatibility.
|
2011-07-28 07:04:08 +00:00
|
|
|
static const char* specialFiles[] = {
|
2011-05-10 18:24:48 +00:00
|
|
|
#if defined(XP_MACOSX)
|
2011-07-28 07:04:08 +00:00
|
|
|
"macprefs.js"
|
2011-02-08 18:54:52 +00:00
|
|
|
#elif defined(XP_WIN)
|
2011-07-28 07:04:08 +00:00
|
|
|
"winpref.js"
|
2011-02-08 18:54:52 +00:00
|
|
|
#elif defined(XP_UNIX)
|
2011-07-28 07:04:08 +00:00
|
|
|
"unix.js"
|
2015-12-18 13:02:50 +00:00
|
|
|
#if defined(_AIX)
|
2017-10-05 23:06:47 +00:00
|
|
|
,
|
|
|
|
"aix.js"
|
2011-02-08 18:54:52 +00:00
|
|
|
#endif
|
2011-02-25 11:53:36 +00:00
|
|
|
#elif defined(XP_BEOS)
|
2011-07-28 07:04:08 +00:00
|
|
|
"beos.js"
|
2011-02-08 18:54:52 +00:00
|
|
|
#endif
|
2011-07-28 07:04:08 +00:00
|
|
|
};
|
2011-02-08 18:54:52 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
rv = pref_LoadPrefsInDir(
|
|
|
|
defaultPrefDir, specialFiles, ArrayLength(specialFiles));
|
|
|
|
if (NS_FAILED(rv)) {
|
2011-07-28 07:04:08 +00:00
|
|
|
NS_WARNING("Error parsing application default preferences.");
|
2017-10-05 23:06:47 +00:00
|
|
|
}
|
2011-02-25 11:53:36 +00:00
|
|
|
|
|
|
|
// Load jar:$app/omni.jar!/defaults/preferences/*.js
|
2012-04-19 07:39:52 +00:00
|
|
|
// or jar:$gre/omni.jar!/defaults/preferences/*.js.
|
2017-10-05 23:06:47 +00:00
|
|
|
RefPtr<nsZipArchive> appJarReader =
|
|
|
|
mozilla::Omnijar::GetReader(mozilla::Omnijar::APP);
|
|
|
|
|
|
|
|
// GetReader(mozilla::Omnijar::APP) returns null when `$app == $gre`, in
|
|
|
|
// which case we look for app-specific default preferences in $gre.
|
|
|
|
if (!appJarReader) {
|
2012-04-19 07:39:52 +00:00
|
|
|
appJarReader = mozilla::Omnijar::GetReader(mozilla::Omnijar::GRE);
|
2017-10-05 23:06:47 +00:00
|
|
|
}
|
|
|
|
|
2011-02-25 11:53:36 +00:00
|
|
|
if (appJarReader) {
|
|
|
|
rv = appJarReader->FindInit("defaults/preferences/*.js$", &findPtr);
|
2017-09-06 05:17:05 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, Err("appJarReader->FindInit() failed"));
|
2011-02-25 11:53:36 +00:00
|
|
|
find = findPtr;
|
|
|
|
prefEntries.Clear();
|
|
|
|
while (NS_SUCCEEDED(find->FindNext(&entryName, &entryNameLen))) {
|
2011-12-09 08:35:41 +00:00
|
|
|
prefEntries.AppendElement(Substring(entryName, entryNameLen));
|
2011-02-25 11:53:36 +00:00
|
|
|
}
|
|
|
|
prefEntries.Sort();
|
2017-10-05 23:06:47 +00:00
|
|
|
for (uint32_t i = prefEntries.Length(); i--;) {
|
2011-02-25 11:53:36 +00:00
|
|
|
rv = pref_ReadPrefFromJar(appJarReader, prefEntries[i].get());
|
2017-10-05 23:06:47 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2011-02-25 11:53:36 +00:00
|
|
|
NS_WARNING("Error parsing preferences.");
|
2017-10-05 23:06:47 +00:00
|
|
|
}
|
2011-02-25 11:53:36 +00:00
|
|
|
}
|
2011-02-08 18:54:52 +00:00
|
|
|
}
|
|
|
|
|
2007-09-01 02:16:47 +00:00
|
|
|
rv = pref_LoadPrefsInDirList(NS_APP_PREFS_DEFAULTS_DIR_LIST);
|
2017-09-06 05:17:05 +00:00
|
|
|
NS_ENSURE_SUCCESS(
|
|
|
|
rv, Err("pref_LoadPrefsInDirList(NS_APP_PREFS_DEFAULTS_DIR_LIST) failed"));
|
2001-03-22 23:34:41 +00:00
|
|
|
|
2017-10-13 20:17:18 +00:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2017-10-05 23:06:47 +00:00
|
|
|
// Set up the correct default for toolkit.telemetry.enabled. If this build
|
|
|
|
// has MOZ_TELEMETRY_ON_BY_DEFAULT *or* we're on the beta channel, telemetry
|
|
|
|
// is on by default, otherwise not. This is necessary so that beta users who
|
|
|
|
// are testing final release builds don't flipflop defaults.
|
2017-11-14 08:06:01 +00:00
|
|
|
if (Preferences::GetType(kTelemetryPref, PrefValueKind::Default) ==
|
2017-10-05 23:06:47 +00:00
|
|
|
nsIPrefBranch::PREF_INVALID) {
|
2014-04-03 18:55:42 +00:00
|
|
|
bool prerelease = false;
|
|
|
|
#ifdef MOZ_TELEMETRY_ON_BY_DEFAULT
|
|
|
|
prerelease = true;
|
|
|
|
#else
|
2017-07-31 04:28:48 +00:00
|
|
|
nsAutoCString prefValue;
|
2017-11-14 08:06:01 +00:00
|
|
|
Preferences::GetCString(kChannelPref, prefValue, PrefValueKind::Default);
|
2017-07-31 04:28:48 +00:00
|
|
|
if (prefValue.EqualsLiteral("beta")) {
|
2014-04-03 18:55:42 +00:00
|
|
|
prerelease = true;
|
|
|
|
}
|
|
|
|
#endif
|
2017-11-14 08:06:02 +00:00
|
|
|
Preferences::SetBoolInAnyProcess(
|
|
|
|
kTelemetryPref, prerelease, PrefValueKind::Default);
|
2014-04-03 18:55:42 +00:00
|
|
|
}
|
2017-10-13 20:17:18 +00:00
|
|
|
#else
|
|
|
|
// For platforms with Unified Telemetry (here meaning not-Android),
|
|
|
|
// toolkit.telemetry.enabled determines whether we send "extended" data.
|
2017-10-31 07:53:32 +00:00
|
|
|
// We only want extended data from pre-release channels due to size. We
|
|
|
|
// also want it to be recorded for local developer builds (non-official builds
|
|
|
|
// on the "default" channel).
|
|
|
|
bool developerBuild = false;
|
|
|
|
#ifndef MOZILLA_OFFICIAL
|
|
|
|
developerBuild = !strcmp(NS_STRINGIFY(MOZ_UPDATE_CHANNEL), "default");
|
|
|
|
#endif
|
|
|
|
|
2017-10-13 20:17:18 +00:00
|
|
|
if (!strcmp(NS_STRINGIFY(MOZ_UPDATE_CHANNEL), "nightly") ||
|
|
|
|
!strcmp(NS_STRINGIFY(MOZ_UPDATE_CHANNEL), "aurora") ||
|
2017-11-02 06:13:47 +00:00
|
|
|
!strcmp(NS_STRINGIFY(MOZ_UPDATE_CHANNEL), "beta") || developerBuild) {
|
2017-11-14 08:06:02 +00:00
|
|
|
Preferences::SetBoolInAnyProcess(
|
|
|
|
kTelemetryPref, true, PrefValueKind::Default);
|
2017-10-13 20:17:18 +00:00
|
|
|
} else {
|
2017-11-14 08:06:02 +00:00
|
|
|
Preferences::SetBoolInAnyProcess(
|
|
|
|
kTelemetryPref, false, PrefValueKind::Default);
|
2017-10-13 20:17:18 +00:00
|
|
|
}
|
|
|
|
PREF_LockPref(kTelemetryPref, true);
|
|
|
|
#endif // MOZ_WIDGET_ANDROID
|
2014-04-03 18:55:42 +00:00
|
|
|
|
2007-09-01 02:16:47 +00:00
|
|
|
NS_CreateServicesFromCategory(NS_PREFSERVICE_APPDEFAULTS_TOPIC_ID,
|
2017-10-05 23:06:47 +00:00
|
|
|
nullptr,
|
|
|
|
NS_PREFSERVICE_APPDEFAULTS_TOPIC_ID);
|
2004-06-14 05:55:13 +00:00
|
|
|
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 16:59:13 +00:00
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
2017-09-06 05:17:05 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, Err("GetObserverService() failed (2)"));
|
2004-06-14 05:55:13 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
observerService->NotifyObservers(
|
|
|
|
nullptr, NS_PREFSERVICE_APPDEFAULTS_TOPIC_ID, nullptr);
|
2007-09-01 02:16:47 +00:00
|
|
|
|
2017-09-06 05:17:05 +00:00
|
|
|
return Ok();
|
2001-03-22 23:34:41 +00:00
|
|
|
}
|
2011-05-23 14:54:02 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// Static utilities
|
|
|
|
//----------------------------------------------------------------------------
|
2011-05-23 14:54:03 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2017-11-14 08:06:01 +00:00
|
|
|
Preferences::GetBool(const char* aPrefName, bool* aResult, PrefValueKind aKind)
|
2011-05-23 14:54:03 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aResult, "aResult must not be NULL");
|
|
|
|
NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
|
2017-11-14 08:06:02 +00:00
|
|
|
|
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(aPrefName);
|
|
|
|
if (!pref || !pref->mPrefFlags.IsTypeBool()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aKind == PrefValueKind::Default || pref->mPrefFlags.IsLocked() ||
|
|
|
|
!pref->mPrefFlags.HasUserValue()) {
|
|
|
|
|
|
|
|
// Do we have a default?
|
|
|
|
if (!pref->mPrefFlags.HasDefault()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
*aResult = pref->mDefaultPref.mBoolVal;
|
|
|
|
} else {
|
|
|
|
*aResult = pref->mUserPref.mBoolVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2011-05-23 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2017-11-14 08:06:01 +00:00
|
|
|
Preferences::GetInt(const char* aPrefName,
|
|
|
|
int32_t* aResult,
|
|
|
|
PrefValueKind aKind)
|
2011-05-23 14:54:03 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aResult, "aResult must not be NULL");
|
|
|
|
NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
|
2017-11-14 08:06:02 +00:00
|
|
|
|
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(aPrefName);
|
|
|
|
if (!pref || !pref->mPrefFlags.IsTypeInt()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aKind == PrefValueKind::Default || pref->mPrefFlags.IsLocked() ||
|
|
|
|
!pref->mPrefFlags.HasUserValue()) {
|
|
|
|
|
|
|
|
// Do we have a default?
|
|
|
|
if (!pref->mPrefFlags.HasDefault()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
*aResult = pref->mDefaultPref.mIntVal;
|
|
|
|
} else {
|
|
|
|
*aResult = pref->mUserPref.mIntVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2011-05-23 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2017-11-14 08:06:01 +00:00
|
|
|
Preferences::GetFloat(const char* aPrefName,
|
|
|
|
float* aResult,
|
|
|
|
PrefValueKind aKind)
|
2012-09-26 20:53:37 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aResult, "aResult must not be NULL");
|
2017-11-14 08:06:02 +00:00
|
|
|
|
2012-09-26 20:53:37 +00:00
|
|
|
nsAutoCString result;
|
2017-11-14 08:06:02 +00:00
|
|
|
nsresult rv = Preferences::GetCString(aPrefName, result, aKind);
|
2012-09-26 20:53:37 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*aResult = result.ToFloat(&rv);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2017-11-14 08:06:01 +00:00
|
|
|
Preferences::GetCString(const char* aPrefName,
|
|
|
|
nsACString& aResult,
|
|
|
|
PrefValueKind aKind)
|
2011-05-23 14:54:03 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
|
2017-11-14 08:06:02 +00:00
|
|
|
|
|
|
|
aResult.SetIsVoid(true);
|
|
|
|
|
|
|
|
PrefHashEntry* pref = pref_HashTableLookup(aPrefName);
|
|
|
|
if (!pref || !pref->mPrefFlags.IsTypeString()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* stringVal = nullptr;
|
|
|
|
if (aKind == PrefValueKind::Default || pref->mPrefFlags.IsLocked() ||
|
|
|
|
!pref->mPrefFlags.HasUserValue()) {
|
|
|
|
|
|
|
|
// Do we have a default?
|
|
|
|
if (!pref->mPrefFlags.HasDefault()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
stringVal = pref->mDefaultPref.mStringVal;
|
|
|
|
} else {
|
|
|
|
stringVal = pref->mUserPref.mStringVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stringVal) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
aResult = stringVal;
|
|
|
|
return NS_OK;
|
2011-05-23 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2017-11-14 08:06:01 +00:00
|
|
|
Preferences::GetString(const char* aPrefName,
|
|
|
|
nsAString& aResult,
|
|
|
|
PrefValueKind aKind)
|
2011-05-23 14:54:03 +00:00
|
|
|
{
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString result;
|
2017-11-14 08:06:02 +00:00
|
|
|
nsresult rv = Preferences::GetCString(aPrefName, result, aKind);
|
2011-05-23 14:54:03 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2017-07-31 00:52:51 +00:00
|
|
|
CopyUTF8toUTF16(result, aResult);
|
2011-05-23 14:54:03 +00:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2017-11-14 08:06:01 +00:00
|
|
|
Preferences::GetLocalizedCString(const char* aPrefName,
|
|
|
|
nsACString& aResult,
|
|
|
|
PrefValueKind aKind)
|
2011-05-28 07:03:00 +00:00
|
|
|
{
|
|
|
|
nsAutoString result;
|
2017-11-14 08:06:01 +00:00
|
|
|
nsresult rv = GetLocalizedString(aPrefName, result);
|
2011-05-28 07:03:00 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2017-07-31 00:52:51 +00:00
|
|
|
CopyUTF16toUTF8(result, aResult);
|
2011-05-28 07:03:00 +00:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2017-11-14 08:06:01 +00:00
|
|
|
Preferences::GetLocalizedString(const char* aPrefName,
|
|
|
|
nsAString& aResult,
|
|
|
|
PrefValueKind aKind)
|
2011-05-23 14:54:03 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
|
|
|
|
nsCOMPtr<nsIPrefLocalizedString> prefLocalString;
|
2017-11-14 08:06:01 +00:00
|
|
|
nsresult rv =
|
|
|
|
GetRootBranch(aKind)->GetComplexValue(aPrefName,
|
|
|
|
NS_GET_IID(nsIPrefLocalizedString),
|
|
|
|
getter_AddRefs(prefLocalString));
|
2011-05-23 14:54:03 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2011-05-28 07:03:00 +00:00
|
|
|
NS_ASSERTION(prefLocalString, "Succeeded but the result is NULL");
|
Bug 1409635 (part 2) - Fix up nsIPrefLocalizedString. r=froydnj.
nsIPrefLocalizedString is meant to be a wrapper for nsISupportsString,
basically identical but with a different identifier. But it's not a
sub-interface of nsISupportsString. Instead it (almost) duplicates
nsISupportsString's internals.
Specifically, nsISupportsString has `attribute AString data`, resulting in
these C++ methods:
> NS_IMETHOD GetData(nsAString& aData)
> NS_IMETHOD SetData(const nsAString& aData)
nsIPrefLocalizedString has `attribute wstring data`, resulting in these C++
methods:
> NS_IMETHOD GetData(char16_t** aData)
> NS_IMETHOD SetData(const char16_t* aData)
Then we have nsPrefLocalizedString, the concrete subclass of
nsIPrefLocalizedString. It implements the AString methods via
NS_FORWARD_NSISUPPORTSSTRING, which forwards to mUnicodeString. It also
implements the wstring methods explicitly, and they just call the AString
methods. It's all a bit of a mess.
(Both interfaces also have `wstring toString()`. The forwarding works more
smoothly for that method.)
This patch changes nsIPrefLocalizedString so it is a trivial sub-interface of
nsISupportsString. This change eliminates the need for the wstring methods, so
the patch removes them as well. The net result is we have less code, and fewer
conversions between C strings and Gecko strings. The patch also merges the
nsISupportsString and nsIPrefLocalizedString cases in
nsPrefBranch::SetComplexValue(), because they are now identical. (The
nsISupportsString and nsIPrefLocalizedString cases in
nsPrefBranch::GetComplexValue() remain distinct; indeed, that's the whole
reason for having them as separate interfaces.)
2017-10-18 05:51:56 +00:00
|
|
|
prefLocalString->GetData(aResult);
|
2011-05-23 14:54:03 +00:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2017-11-14 08:06:01 +00:00
|
|
|
Preferences::GetComplex(const char* aPrefName,
|
|
|
|
const nsIID& aType,
|
|
|
|
void** aResult,
|
|
|
|
PrefValueKind aKind)
|
2011-06-12 02:30:15 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
|
2017-11-14 08:06:01 +00:00
|
|
|
return GetRootBranch(aKind)->GetComplexValue(aPrefName, aType, aResult);
|
2011-06-12 02:30:15 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2017-11-14 08:06:02 +00:00
|
|
|
Preferences::SetCStringInAnyProcess(const char* aPrefName,
|
|
|
|
const nsACString& aValue,
|
|
|
|
PrefValueKind aKind)
|
2011-05-23 14:54:03 +00:00
|
|
|
{
|
2011-06-03 06:01:02 +00:00
|
|
|
NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
|
2017-11-14 08:06:02 +00:00
|
|
|
|
|
|
|
if (aValue.Length() > MAX_PREF_LENGTH) {
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's ok to stash a pointer to the temporary PromiseFlatCString's chars in
|
|
|
|
// pref because pref_SetPref() duplicates those chars.
|
|
|
|
PrefValue prefValue;
|
|
|
|
const nsCString& flat = PromiseFlatCString(aValue);
|
|
|
|
prefValue.mStringVal = flat.get();
|
|
|
|
return pref_SetPref(aPrefName,
|
|
|
|
prefValue,
|
|
|
|
PrefType::String,
|
|
|
|
aKind == PrefValueKind::Default ? kPrefSetDefault : 0);
|
2011-05-23 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2017-11-12 22:20:45 +00:00
|
|
|
Preferences::SetCString(const char* aPrefName,
|
|
|
|
const nsACString& aValue,
|
|
|
|
PrefValueKind aKind)
|
2011-05-23 14:54:03 +00:00
|
|
|
{
|
2017-11-12 22:20:45 +00:00
|
|
|
ENSURE_MAIN_PROCESS("SetCString", aPrefName);
|
2017-11-14 08:06:02 +00:00
|
|
|
return SetCStringInAnyProcess(aPrefName, aValue, aKind);
|
2011-05-23 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2017-11-14 08:06:02 +00:00
|
|
|
Preferences::SetBoolInAnyProcess(const char* aPrefName,
|
|
|
|
bool aValue,
|
|
|
|
PrefValueKind aKind)
|
2011-05-23 14:54:03 +00:00
|
|
|
{
|
2011-11-06 19:27:56 +00:00
|
|
|
NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
|
2011-05-23 14:54:03 +00:00
|
|
|
|
2017-11-14 08:06:02 +00:00
|
|
|
PrefValue prefValue;
|
|
|
|
prefValue.mBoolVal = aValue;
|
|
|
|
return pref_SetPref(aPrefName,
|
|
|
|
prefValue,
|
|
|
|
PrefType::Bool,
|
|
|
|
aKind == PrefValueKind::Default ? kPrefSetDefault : 0);
|
2011-05-23 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2017-11-12 22:20:45 +00:00
|
|
|
Preferences::SetBool(const char* aPrefName, bool aValue, PrefValueKind aKind)
|
2011-05-23 14:54:03 +00:00
|
|
|
{
|
2017-11-12 22:20:45 +00:00
|
|
|
ENSURE_MAIN_PROCESS("SetBool", aPrefName);
|
2017-11-14 08:06:02 +00:00
|
|
|
return SetBoolInAnyProcess(aPrefName, aValue, aKind);
|
2011-05-23 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2017-11-14 08:06:02 +00:00
|
|
|
Preferences::SetIntInAnyProcess(const char* aPrefName,
|
|
|
|
int32_t aValue,
|
|
|
|
PrefValueKind aKind)
|
2011-05-23 14:54:03 +00:00
|
|
|
{
|
2011-06-03 06:01:02 +00:00
|
|
|
NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
|
2017-11-14 08:06:02 +00:00
|
|
|
|
|
|
|
PrefValue prefValue;
|
|
|
|
prefValue.mIntVal = aValue;
|
|
|
|
return pref_SetPref(aPrefName,
|
|
|
|
prefValue,
|
|
|
|
PrefType::Int,
|
|
|
|
aKind == PrefValueKind::Default ? kPrefSetDefault : 0);
|
2011-05-23 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2017-11-14 08:06:02 +00:00
|
|
|
Preferences::SetInt(const char* aPrefName, int32_t aValue, PrefValueKind aKind)
|
2014-07-11 12:25:12 +00:00
|
|
|
{
|
2017-11-14 08:06:02 +00:00
|
|
|
ENSURE_MAIN_PROCESS("SetInt", aPrefName);
|
|
|
|
return SetIntInAnyProcess(aPrefName, aValue, aKind);
|
2014-07-11 12:25:12 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2017-11-12 22:20:45 +00:00
|
|
|
Preferences::SetComplex(const char* aPrefName,
|
2017-10-05 23:06:47 +00:00
|
|
|
const nsIID& aType,
|
2017-11-12 22:20:45 +00:00
|
|
|
nsISupports* aValue,
|
|
|
|
PrefValueKind aKind)
|
2011-06-12 02:30:15 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
|
2017-11-14 08:06:01 +00:00
|
|
|
return GetRootBranch(aKind)->SetComplexValue(aPrefName, aType, aValue);
|
2011-06-12 02:30:15 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2011-05-23 14:54:03 +00:00
|
|
|
Preferences::ClearUser(const char* aPref)
|
|
|
|
{
|
2017-11-12 22:19:55 +00:00
|
|
|
ENSURE_MAIN_PROCESS("ClearUser", aPref);
|
2011-06-03 06:01:02 +00:00
|
|
|
NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
|
2011-11-06 19:27:56 +00:00
|
|
|
return PREF_ClearUserPref(aPref);
|
2011-05-23 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ bool
|
2011-06-03 06:01:02 +00:00
|
|
|
Preferences::HasUserValue(const char* aPref)
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_ENSURE_TRUE(InitStaticMembers(), false);
|
2011-11-06 19:27:56 +00:00
|
|
|
return PREF_HasUserPref(aPref);
|
2011-06-03 06:01:02 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ int32_t
|
2017-11-14 08:06:01 +00:00
|
|
|
Preferences::GetType(const char* aPrefName, PrefValueKind aKind)
|
2012-04-04 04:06:27 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(InitStaticMembers(), nsIPrefBranch::PREF_INVALID);
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t result;
|
2017-11-14 08:06:01 +00:00
|
|
|
return NS_SUCCEEDED(GetRootBranch(aKind)->GetPrefType(aPrefName, &result))
|
2017-10-05 23:06:47 +00:00
|
|
|
? result
|
|
|
|
: nsIPrefBranch::PREF_INVALID;
|
2012-04-04 04:06:27 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
|
|
|
Preferences::AddStrongObserver(nsIObserver* aObserver, const char* aPref)
|
2011-05-23 14:54:03 +00:00
|
|
|
{
|
2017-09-08 21:36:54 +00:00
|
|
|
MOZ_ASSERT(aObserver);
|
2011-05-23 14:54:03 +00:00
|
|
|
NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
|
2017-11-01 02:41:14 +00:00
|
|
|
return sPreferences->mRootBranch->AddObserver(aPref, aObserver, false);
|
2011-05-23 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
|
|
|
Preferences::AddWeakObserver(nsIObserver* aObserver, const char* aPref)
|
2011-05-23 14:54:03 +00:00
|
|
|
{
|
2017-09-08 21:36:54 +00:00
|
|
|
MOZ_ASSERT(aObserver);
|
2011-05-23 14:54:03 +00:00
|
|
|
NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
|
2017-11-01 02:41:14 +00:00
|
|
|
return sPreferences->mRootBranch->AddObserver(aPref, aObserver, true);
|
2011-05-23 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
|
|
|
Preferences::RemoveObserver(nsIObserver* aObserver, const char* aPref)
|
2011-05-23 14:54:03 +00:00
|
|
|
{
|
2017-09-08 21:36:54 +00:00
|
|
|
MOZ_ASSERT(aObserver);
|
2017-11-12 22:19:49 +00:00
|
|
|
if (sShutdown) {
|
|
|
|
MOZ_ASSERT(!sPreferences);
|
2011-06-03 06:01:02 +00:00
|
|
|
return NS_OK; // Observers have been released automatically.
|
|
|
|
}
|
|
|
|
NS_ENSURE_TRUE(sPreferences, NS_ERROR_NOT_AVAILABLE);
|
2017-11-01 02:41:14 +00:00
|
|
|
return sPreferences->mRootBranch->RemoveObserver(aPref, aObserver);
|
2011-05-23 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
|
|
|
Preferences::AddStrongObservers(nsIObserver* aObserver, const char** aPrefs)
|
2011-05-28 23:39:27 +00:00
|
|
|
{
|
2017-09-08 21:36:54 +00:00
|
|
|
MOZ_ASSERT(aObserver);
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; aPrefs[i]; i++) {
|
2011-05-28 23:39:27 +00:00
|
|
|
nsresult rv = AddStrongObserver(aObserver, aPrefs[i]);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
|
|
|
Preferences::AddWeakObservers(nsIObserver* aObserver, const char** aPrefs)
|
2011-05-28 23:39:27 +00:00
|
|
|
{
|
2017-09-08 21:36:54 +00:00
|
|
|
MOZ_ASSERT(aObserver);
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; aPrefs[i]; i++) {
|
2011-05-28 23:39:27 +00:00
|
|
|
nsresult rv = AddWeakObserver(aObserver, aPrefs[i]);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
|
|
|
Preferences::RemoveObservers(nsIObserver* aObserver, const char** aPrefs)
|
2011-05-28 23:39:27 +00:00
|
|
|
{
|
2017-09-08 21:36:54 +00:00
|
|
|
MOZ_ASSERT(aObserver);
|
2017-11-12 22:19:49 +00:00
|
|
|
if (sShutdown) {
|
|
|
|
MOZ_ASSERT(!sPreferences);
|
2011-06-03 06:01:02 +00:00
|
|
|
return NS_OK; // Observers have been released automatically.
|
|
|
|
}
|
|
|
|
NS_ENSURE_TRUE(sPreferences, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; aPrefs[i]; i++) {
|
2011-05-28 23:39:27 +00:00
|
|
|
nsresult rv = RemoveObserver(aObserver, aPrefs[i]);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-11-02 06:20:36 +00:00
|
|
|
// RegisterVarCacheCallback uses high priority callbacks to ensure that cache
|
|
|
|
// observers are called prior to ordinary pref observers. Doing this ensures
|
|
|
|
// that ordinary observers will never get stale values from cache variables.
|
2017-11-02 06:13:47 +00:00
|
|
|
static void
|
2017-11-02 06:20:36 +00:00
|
|
|
RegisterVarCacheCallback(PrefChangedFunc aCallback,
|
2017-11-02 06:13:47 +00:00
|
|
|
const char* aPref,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(Preferences::IsServiceAvailable());
|
|
|
|
|
Bug 1414188 - Remove ValueObserver. r=glandium.
libpref has callbacks. You can register a callback function against a
particular pref (or pref prefix), and it'll be called when any matching pref
changes.
This is implemented in two layers. The lower layer is the list of CallbackNode
objects, pointed to by gFirstCallback.
The upper layer involves gObserverTable, which is a hash table of ValueObserver
objects. It is used for callbacks registered via
Preferences::RegisterCallback() and Preferences::Add*VarCache(), but not for
observers registered with Preferences::Add{Weak,Strong}Observer(). If multiple
callbacks with identical prefnames, callback functions and MatchKinds occur,
they are commoned up into a single ValueObserver, which is then wrapped by a
single CallbackNode. (The callbacks within a single ValueObserver can have
different void* aClosure args; ValueObserver keeps those in an nsTArray.)
Note also that gObserverTable is very inelegant, with duplication of data
between the keys and the values due it being a
nsRefPtrHashtable<ValueObserverHashKey, ValueObserver> and ValueObserver being
a subclass of ValueObserverHashKey(!)
This extra layer might make sense if there were a lot of commoning up
happening, but there's not. Across all process kinds there is an average of
between 1.2 and 1.7 closures per ValueObserver. The vast majority have 1
closure, and there are a handful that have multiple; the highest number I've
seen is 71.
(Note that this two-layer design probably seemed more natural back when libpref
was spread across multiple files.)
This patch removes the ValueObserver layer. The only tricky part is that there
is a notion of a MatchKind -- ExactMatch or PrefixMatch -- which exists in the
ValueObserverLayer but not in the CallbackNode layer. So the patch moves the
MatchKind into the CallbackNode layer, which is straightforward.
On Linux64 this reduces memory usage by about 40 KiB in the parent process and
about 30 KiB in each content processes.
The performance implications are minor.
- The list of CallbackNodes is a bit longer, so when a pref is changed we must
check more nodes. But we avoid possibly doing prefname matching twice.
- Callback registration is much faster, just a simple list prepend, without any
hash table insertion required.
- Callback unregistration is probably not much different. There is a longer
list to traverse, but no hash table removal.
- Pref lookup is by far the hottest libpref operation, and it's unchanged.
For perf to be genuinely affected would require (a) a *lot* more
almost-duplicate callbacks that would have been commoned up, and (b) frequent
changing of the pref those callbacks are observing. This seems highly unlikely.
MozReview-Commit-ID: 6xo4xmytOf3
--HG--
extra : rebase_source : cf9884a8a40b2cc3b00ab165ae5ab4ec3383f72d
2017-11-02 09:38:34 +00:00
|
|
|
PREF_RegisterCallback(
|
|
|
|
aPref, aCallback, aClosure, Preferences::ExactMatch, /* isPriority */ true);
|
2017-11-02 06:13:47 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2011-05-28 23:42:57 +00:00
|
|
|
Preferences::RegisterCallback(PrefChangedFunc aCallback,
|
|
|
|
const char* aPref,
|
2016-05-04 00:26:04 +00:00
|
|
|
void* aClosure,
|
|
|
|
MatchKind aMatchKind)
|
2011-05-28 23:42:57 +00:00
|
|
|
{
|
2017-09-08 21:36:54 +00:00
|
|
|
MOZ_ASSERT(aCallback);
|
2011-05-28 23:42:57 +00:00
|
|
|
NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
Bug 1414188 - Remove ValueObserver. r=glandium.
libpref has callbacks. You can register a callback function against a
particular pref (or pref prefix), and it'll be called when any matching pref
changes.
This is implemented in two layers. The lower layer is the list of CallbackNode
objects, pointed to by gFirstCallback.
The upper layer involves gObserverTable, which is a hash table of ValueObserver
objects. It is used for callbacks registered via
Preferences::RegisterCallback() and Preferences::Add*VarCache(), but not for
observers registered with Preferences::Add{Weak,Strong}Observer(). If multiple
callbacks with identical prefnames, callback functions and MatchKinds occur,
they are commoned up into a single ValueObserver, which is then wrapped by a
single CallbackNode. (The callbacks within a single ValueObserver can have
different void* aClosure args; ValueObserver keeps those in an nsTArray.)
Note also that gObserverTable is very inelegant, with duplication of data
between the keys and the values due it being a
nsRefPtrHashtable<ValueObserverHashKey, ValueObserver> and ValueObserver being
a subclass of ValueObserverHashKey(!)
This extra layer might make sense if there were a lot of commoning up
happening, but there's not. Across all process kinds there is an average of
between 1.2 and 1.7 closures per ValueObserver. The vast majority have 1
closure, and there are a handful that have multiple; the highest number I've
seen is 71.
(Note that this two-layer design probably seemed more natural back when libpref
was spread across multiple files.)
This patch removes the ValueObserver layer. The only tricky part is that there
is a notion of a MatchKind -- ExactMatch or PrefixMatch -- which exists in the
ValueObserverLayer but not in the CallbackNode layer. So the patch moves the
MatchKind into the CallbackNode layer, which is straightforward.
On Linux64 this reduces memory usage by about 40 KiB in the parent process and
about 30 KiB in each content processes.
The performance implications are minor.
- The list of CallbackNodes is a bit longer, so when a pref is changed we must
check more nodes. But we avoid possibly doing prefname matching twice.
- Callback registration is much faster, just a simple list prepend, without any
hash table insertion required.
- Callback unregistration is probably not much different. There is a longer
list to traverse, but no hash table removal.
- Pref lookup is by far the hottest libpref operation, and it's unchanged.
For perf to be genuinely affected would require (a) a *lot* more
almost-duplicate callbacks that would have been commoned up, and (b) frequent
changing of the pref those callbacks are observing. This seems highly unlikely.
MozReview-Commit-ID: 6xo4xmytOf3
--HG--
extra : rebase_source : cf9884a8a40b2cc3b00ab165ae5ab4ec3383f72d
2017-11-02 09:38:34 +00:00
|
|
|
PREF_RegisterCallback(
|
|
|
|
aPref, aCallback, aClosure, aMatchKind, /* isPriority */ false);
|
|
|
|
|
2012-05-18 17:30:49 +00:00
|
|
|
return NS_OK;
|
2011-05-28 23:42:57 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2013-02-20 19:49:49 +00:00
|
|
|
Preferences::RegisterCallbackAndCall(PrefChangedFunc aCallback,
|
|
|
|
const char* aPref,
|
2016-05-04 00:26:04 +00:00
|
|
|
void* aClosure,
|
|
|
|
MatchKind aMatchKind)
|
2013-02-20 19:49:49 +00:00
|
|
|
{
|
2017-09-08 21:36:54 +00:00
|
|
|
MOZ_ASSERT(aCallback);
|
2017-02-22 03:17:52 +00:00
|
|
|
WATCHING_PREF_RAII();
|
2016-05-04 00:26:04 +00:00
|
|
|
nsresult rv = RegisterCallback(aCallback, aPref, aClosure, aMatchKind);
|
2013-02-20 19:49:49 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
(*aCallback)(aPref, aClosure);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2011-05-28 23:42:57 +00:00
|
|
|
Preferences::UnregisterCallback(PrefChangedFunc aCallback,
|
|
|
|
const char* aPref,
|
2016-05-04 00:26:04 +00:00
|
|
|
void* aClosure,
|
|
|
|
MatchKind aMatchKind)
|
2011-05-28 23:42:57 +00:00
|
|
|
{
|
2017-09-08 21:36:54 +00:00
|
|
|
MOZ_ASSERT(aCallback);
|
2017-11-12 22:19:49 +00:00
|
|
|
if (sShutdown) {
|
|
|
|
MOZ_ASSERT(!sPreferences);
|
2011-06-03 06:01:02 +00:00
|
|
|
return NS_OK; // Observers have been released automatically.
|
|
|
|
}
|
|
|
|
NS_ENSURE_TRUE(sPreferences, NS_ERROR_NOT_AVAILABLE);
|
2011-05-28 23:42:57 +00:00
|
|
|
|
Bug 1414188 - Remove ValueObserver. r=glandium.
libpref has callbacks. You can register a callback function against a
particular pref (or pref prefix), and it'll be called when any matching pref
changes.
This is implemented in two layers. The lower layer is the list of CallbackNode
objects, pointed to by gFirstCallback.
The upper layer involves gObserverTable, which is a hash table of ValueObserver
objects. It is used for callbacks registered via
Preferences::RegisterCallback() and Preferences::Add*VarCache(), but not for
observers registered with Preferences::Add{Weak,Strong}Observer(). If multiple
callbacks with identical prefnames, callback functions and MatchKinds occur,
they are commoned up into a single ValueObserver, which is then wrapped by a
single CallbackNode. (The callbacks within a single ValueObserver can have
different void* aClosure args; ValueObserver keeps those in an nsTArray.)
Note also that gObserverTable is very inelegant, with duplication of data
between the keys and the values due it being a
nsRefPtrHashtable<ValueObserverHashKey, ValueObserver> and ValueObserver being
a subclass of ValueObserverHashKey(!)
This extra layer might make sense if there were a lot of commoning up
happening, but there's not. Across all process kinds there is an average of
between 1.2 and 1.7 closures per ValueObserver. The vast majority have 1
closure, and there are a handful that have multiple; the highest number I've
seen is 71.
(Note that this two-layer design probably seemed more natural back when libpref
was spread across multiple files.)
This patch removes the ValueObserver layer. The only tricky part is that there
is a notion of a MatchKind -- ExactMatch or PrefixMatch -- which exists in the
ValueObserverLayer but not in the CallbackNode layer. So the patch moves the
MatchKind into the CallbackNode layer, which is straightforward.
On Linux64 this reduces memory usage by about 40 KiB in the parent process and
about 30 KiB in each content processes.
The performance implications are minor.
- The list of CallbackNodes is a bit longer, so when a pref is changed we must
check more nodes. But we avoid possibly doing prefname matching twice.
- Callback registration is much faster, just a simple list prepend, without any
hash table insertion required.
- Callback unregistration is probably not much different. There is a longer
list to traverse, but no hash table removal.
- Pref lookup is by far the hottest libpref operation, and it's unchanged.
For perf to be genuinely affected would require (a) a *lot* more
almost-duplicate callbacks that would have been commoned up, and (b) frequent
changing of the pref those callbacks are observing. This seems highly unlikely.
MozReview-Commit-ID: 6xo4xmytOf3
--HG--
extra : rebase_source : cf9884a8a40b2cc3b00ab165ae5ab4ec3383f72d
2017-11-02 09:38:34 +00:00
|
|
|
return PREF_UnregisterCallback(aPref, aCallback, aClosure, aMatchKind);
|
2011-05-28 23:42:57 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
static void
|
|
|
|
BoolVarChanged(const char* aPref, void* aClosure)
|
2011-05-28 23:42:57 +00:00
|
|
|
{
|
|
|
|
CacheData* cache = static_cast<CacheData*>(aClosure);
|
2017-10-05 23:06:47 +00:00
|
|
|
*static_cast<bool*>(cache->mCacheLocation) =
|
|
|
|
Preferences::GetBool(aPref, cache->mDefaultValueBool);
|
2011-05-28 23:42:57 +00:00
|
|
|
}
|
|
|
|
|
2017-09-06 05:17:05 +00:00
|
|
|
static void
|
|
|
|
CacheDataAppendElement(CacheData* aData)
|
|
|
|
{
|
|
|
|
if (!gCacheData) {
|
|
|
|
MOZ_CRASH_UNSAFE_PRINTF("!gCacheData: %s", gCacheDataDesc);
|
|
|
|
}
|
|
|
|
gCacheData->AppendElement(aData);
|
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
|
|
|
Preferences::AddBoolVarCache(bool* aCache, const char* aPref, bool aDefault)
|
2011-05-28 23:42:57 +00:00
|
|
|
{
|
2017-02-22 03:17:52 +00:00
|
|
|
WATCHING_PREF_RAII();
|
2011-05-28 23:42:57 +00:00
|
|
|
NS_ASSERTION(aCache, "aCache must not be NULL");
|
2014-06-24 13:49:56 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
AssertNotAlreadyCached("bool", aPref, aCache);
|
|
|
|
#endif
|
2011-05-28 23:42:57 +00:00
|
|
|
*aCache = GetBool(aPref, aDefault);
|
|
|
|
CacheData* data = new CacheData();
|
2017-10-05 23:06:47 +00:00
|
|
|
data->mCacheLocation = aCache;
|
|
|
|
data->mDefaultValueBool = aDefault;
|
2017-09-06 05:17:05 +00:00
|
|
|
CacheDataAppendElement(data);
|
2017-11-02 06:20:36 +00:00
|
|
|
RegisterVarCacheCallback(BoolVarChanged, aPref, data);
|
2017-04-11 15:17:52 +00:00
|
|
|
return NS_OK;
|
2011-05-28 23:42:57 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
static void
|
|
|
|
IntVarChanged(const char* aPref, void* aClosure)
|
2011-05-28 23:42:57 +00:00
|
|
|
{
|
|
|
|
CacheData* cache = static_cast<CacheData*>(aClosure);
|
2017-10-05 23:06:47 +00:00
|
|
|
*static_cast<int32_t*>(cache->mCacheLocation) =
|
|
|
|
Preferences::GetInt(aPref, cache->mDefaultValueInt);
|
2011-05-28 23:42:57 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
Preferences::AddIntVarCache(int32_t* aCache,
|
2011-05-28 23:42:57 +00:00
|
|
|
const char* aPref,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aDefault)
|
2011-05-28 23:42:57 +00:00
|
|
|
{
|
2017-02-22 03:17:52 +00:00
|
|
|
WATCHING_PREF_RAII();
|
2011-05-28 23:42:57 +00:00
|
|
|
NS_ASSERTION(aCache, "aCache must not be NULL");
|
2014-06-24 13:49:56 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
AssertNotAlreadyCached("int", aPref, aCache);
|
|
|
|
#endif
|
2011-05-28 23:42:57 +00:00
|
|
|
*aCache = Preferences::GetInt(aPref, aDefault);
|
|
|
|
CacheData* data = new CacheData();
|
2017-10-05 23:06:47 +00:00
|
|
|
data->mCacheLocation = aCache;
|
|
|
|
data->mDefaultValueInt = aDefault;
|
2017-09-06 05:17:05 +00:00
|
|
|
CacheDataAppendElement(data);
|
2017-11-02 06:20:36 +00:00
|
|
|
RegisterVarCacheCallback(IntVarChanged, aPref, data);
|
2017-04-11 15:17:52 +00:00
|
|
|
return NS_OK;
|
2011-05-28 23:42:57 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
static void
|
|
|
|
UintVarChanged(const char* aPref, void* aClosure)
|
2011-05-28 23:42:57 +00:00
|
|
|
{
|
|
|
|
CacheData* cache = static_cast<CacheData*>(aClosure);
|
2017-10-05 23:06:47 +00:00
|
|
|
*static_cast<uint32_t*>(cache->mCacheLocation) =
|
|
|
|
Preferences::GetUint(aPref, cache->mDefaultValueUint);
|
2011-05-28 23:42:57 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
Preferences::AddUintVarCache(uint32_t* aCache,
|
2011-05-28 23:42:57 +00:00
|
|
|
const char* aPref,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aDefault)
|
2011-05-28 23:42:57 +00:00
|
|
|
{
|
2017-02-22 03:17:52 +00:00
|
|
|
WATCHING_PREF_RAII();
|
2011-05-28 23:42:57 +00:00
|
|
|
NS_ASSERTION(aCache, "aCache must not be NULL");
|
2014-06-24 13:49:56 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
AssertNotAlreadyCached("uint", aPref, aCache);
|
|
|
|
#endif
|
2011-05-28 23:42:57 +00:00
|
|
|
*aCache = Preferences::GetUint(aPref, aDefault);
|
|
|
|
CacheData* data = new CacheData();
|
2017-10-05 23:06:47 +00:00
|
|
|
data->mCacheLocation = aCache;
|
|
|
|
data->mDefaultValueUint = aDefault;
|
2017-09-06 05:17:05 +00:00
|
|
|
CacheDataAppendElement(data);
|
2017-11-02 06:20:36 +00:00
|
|
|
RegisterVarCacheCallback(UintVarChanged, aPref, data);
|
2017-04-11 15:17:52 +00:00
|
|
|
return NS_OK;
|
2011-05-28 23:42:57 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
template<MemoryOrdering Order>
|
|
|
|
static void
|
|
|
|
AtomicUintVarChanged(const char* aPref, void* aClosure)
|
2016-03-20 09:08:57 +00:00
|
|
|
{
|
|
|
|
CacheData* cache = static_cast<CacheData*>(aClosure);
|
2017-10-05 23:06:47 +00:00
|
|
|
*static_cast<Atomic<uint32_t, Order>*>(cache->mCacheLocation) =
|
|
|
|
Preferences::GetUint(aPref, cache->mDefaultValueUint);
|
2016-03-20 09:08:57 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
template<MemoryOrdering Order>
|
|
|
|
/* static */ nsresult
|
2016-03-20 09:08:57 +00:00
|
|
|
Preferences::AddAtomicUintVarCache(Atomic<uint32_t, Order>* aCache,
|
|
|
|
const char* aPref,
|
|
|
|
uint32_t aDefault)
|
|
|
|
{
|
2017-02-22 03:17:52 +00:00
|
|
|
WATCHING_PREF_RAII();
|
2016-03-20 09:08:57 +00:00
|
|
|
NS_ASSERTION(aCache, "aCache must not be NULL");
|
|
|
|
#ifdef DEBUG
|
|
|
|
AssertNotAlreadyCached("uint", aPref, aCache);
|
|
|
|
#endif
|
|
|
|
*aCache = Preferences::GetUint(aPref, aDefault);
|
|
|
|
CacheData* data = new CacheData();
|
2017-10-05 23:06:47 +00:00
|
|
|
data->mCacheLocation = aCache;
|
|
|
|
data->mDefaultValueUint = aDefault;
|
2017-09-06 05:17:05 +00:00
|
|
|
CacheDataAppendElement(data);
|
2017-11-02 06:20:36 +00:00
|
|
|
RegisterVarCacheCallback(AtomicUintVarChanged<Order>, aPref, data);
|
2017-04-11 15:17:52 +00:00
|
|
|
return NS_OK;
|
2016-03-20 09:08:57 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
// Since the definition of this template function is not in a header file, we
|
|
|
|
// need to explicitly specify the instantiations that are required. Currently
|
|
|
|
// only the order=Relaxed variant is needed.
|
|
|
|
template nsresult
|
|
|
|
Preferences::AddAtomicUintVarCache(Atomic<uint32_t, Relaxed>*,
|
|
|
|
const char*,
|
|
|
|
uint32_t);
|
2016-03-20 09:08:57 +00:00
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
static void
|
|
|
|
FloatVarChanged(const char* aPref, void* aClosure)
|
2013-01-10 06:10:52 +00:00
|
|
|
{
|
|
|
|
CacheData* cache = static_cast<CacheData*>(aClosure);
|
2017-10-05 23:06:47 +00:00
|
|
|
*static_cast<float*>(cache->mCacheLocation) =
|
|
|
|
Preferences::GetFloat(aPref, cache->mDefaultValueFloat);
|
2013-01-10 06:10:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:06:47 +00:00
|
|
|
/* static */ nsresult
|
|
|
|
Preferences::AddFloatVarCache(float* aCache, const char* aPref, float aDefault)
|
2013-01-10 06:10:52 +00:00
|
|
|
{
|
2017-02-22 03:17:52 +00:00
|
|
|
WATCHING_PREF_RAII();
|
2013-01-10 06:10:52 +00:00
|
|
|
NS_ASSERTION(aCache, "aCache must not be NULL");
|
2014-06-24 13:49:56 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
AssertNotAlreadyCached("float", aPref, aCache);
|
|
|
|
#endif
|
2013-01-10 06:10:52 +00:00
|
|
|
*aCache = Preferences::GetFloat(aPref, aDefault);
|
|
|
|
CacheData* data = new CacheData();
|
2017-10-05 23:06:47 +00:00
|
|
|
data->mCacheLocation = aCache;
|
|
|
|
data->mDefaultValueFloat = aDefault;
|
2017-09-06 05:17:05 +00:00
|
|
|
CacheDataAppendElement(data);
|
2017-11-02 06:20:36 +00:00
|
|
|
RegisterVarCacheCallback(FloatVarChanged, aPref, data);
|
2017-04-11 15:17:52 +00:00
|
|
|
return NS_OK;
|
2013-01-10 06:10:52 +00:00
|
|
|
}
|
|
|
|
|
2011-05-23 14:54:02 +00:00
|
|
|
} // namespace mozilla
|
2013-12-03 23:18:09 +00:00
|
|
|
|
|
|
|
#undef ENSURE_MAIN_PROCESS
|
2017-10-11 08:03:23 +00:00
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
// Module and factory stuff
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(Preferences,
|
|
|
|
Preferences::GetInstanceForService)
|
|
|
|
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsPrefLocalizedString, Init)
|
|
|
|
NS_GENERIC_FACTORY_CONSTRUCTOR(nsRelativeFilePref)
|
|
|
|
|
|
|
|
static NS_DEFINE_CID(kPrefServiceCID, NS_PREFSERVICE_CID);
|
|
|
|
static NS_DEFINE_CID(kPrefLocalizedStringCID, NS_PREFLOCALIZEDSTRING_CID);
|
|
|
|
static NS_DEFINE_CID(kRelativeFilePrefCID, NS_RELATIVEFILEPREF_CID);
|
|
|
|
|
|
|
|
static mozilla::Module::CIDEntry kPrefCIDs[] = {
|
|
|
|
{ &kPrefServiceCID, true, nullptr, PreferencesConstructor },
|
|
|
|
{ &kPrefLocalizedStringCID,
|
|
|
|
false,
|
|
|
|
nullptr,
|
|
|
|
nsPrefLocalizedStringConstructor },
|
|
|
|
{ &kRelativeFilePrefCID, false, nullptr, nsRelativeFilePrefConstructor },
|
|
|
|
{ nullptr }
|
|
|
|
};
|
|
|
|
|
|
|
|
static mozilla::Module::ContractIDEntry kPrefContracts[] = {
|
|
|
|
{ NS_PREFSERVICE_CONTRACTID, &kPrefServiceCID },
|
|
|
|
{ NS_PREFLOCALIZEDSTRING_CONTRACTID, &kPrefLocalizedStringCID },
|
|
|
|
{ NS_RELATIVEFILEPREF_CONTRACTID, &kRelativeFilePrefCID },
|
|
|
|
{ nullptr }
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
UnloadPrefsModule()
|
|
|
|
{
|
|
|
|
Preferences::Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
static const mozilla::Module kPrefModule = { mozilla::Module::kVersion,
|
|
|
|
kPrefCIDs,
|
|
|
|
kPrefContracts,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
UnloadPrefsModule };
|
|
|
|
|
|
|
|
NSMODULE_DEFN(nsPrefModule) = &kPrefModule;
|