Source code
Revision control
Copy as Markdown
Other Tools
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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
// Documentation for libpref is in modules/libpref/docs/index.rst.
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "SharedPrefMap.h"
#include "base/basictypes.h"
#include "MainThreadUtils.h"
#include "mozilla/AppShutdown.h"
#include "mozilla/ArenaAllocatorExtensions.h"
#include "mozilla/ArenaAllocator.h"
#include "mozilla/Attributes.h"
#include "mozilla/Components.h"
#include "mozilla/dom/PContent.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/RemoteType.h"
#include "mozilla/dom/quota/ResultExtensions.h"
#include "mozilla/glean/LibprefMetrics.h"
#include "mozilla/HashFunctions.h"
#include "mozilla/HashTable.h"
#include "mozilla/HelperMacros.h"
#include "mozilla/Logging.h"
#include "mozilla/Maybe.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/Omnijar.h"
#include "mozilla/Preferences.h"
#include "mozilla/ProfilerLabels.h"
#include "mozilla/ProfilerMarkers.h"
#include "mozilla/ResultExtensions.h"
#include "mozilla/SchedulerGroup.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/ServoStyleSet.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "mozilla/StaticMutex.h"
#include "mozilla/StaticPrefsAll.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/SyncRunnable.h"
#include "mozilla/Try.h"
#include "mozilla/URLPreloader.h"
#include "mozilla/Variant.h"
#include "mozilla/Vector.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsCategoryManagerUtils.h"
#include "nsClassHashtable.h"
#include "nsCOMArray.h"
#include "nsCOMPtr.h"
#include "nsComponentManagerUtils.h"
#include "nsContentUtils.h"
#include "nsCRT.h"
#include "nsTHashMap.h"
#include "nsDirectoryServiceDefs.h"
#include "nsIConsoleService.h"
#include "nsIFile.h"
#include "nsIMemoryReporter.h"
#include "nsIObserver.h"
#include "nsIObserverService.h"
#include "nsIOutputStream.h"
#include "nsIPrefBranch.h"
#include "nsIPrefLocalizedString.h"
#include "nsIPrefOverrideMap.h"
#include "nsIRelativeFilePref.h"
#include "nsISafeOutputStream.h"
#include "nsISimpleEnumerator.h"
#include "nsIStringBundle.h"
#include "nsISupportsImpl.h"
#include "nsISupportsPrimitives.h"
#include "nsIZipReader.h"
#include "nsNetUtil.h"
#include "nsPrintfCString.h"
#include "nsProxyRelease.h"
#include "nsReadableUtils.h"
#include "nsRefPtrHashtable.h"
#include "nsRelativeFilePref.h"
#include "nsString.h"
#include "nsTArray.h"
#include "nsThreadUtils.h"
#include "nsTStringHasher.h"
#include "nsUTF8Utils.h"
#include "nsWeakReference.h"
#include "nsXPCOMCID.h"
#include "nsXPCOM.h"
#include "nsXULAppAPI.h"
#include "nsZipArchive.h"
#include "plbase64.h"
#include "PLDHashTable.h"
#include "prdtoa.h"
#include "prlink.h"
#include "xpcpublic.h"
#include "js/RootingAPI.h"
#ifdef MOZ_BACKGROUNDTASKS
# include "mozilla/BackgroundTasks.h"
#endif
#ifdef DEBUG
# include <map>
#endif
#ifdef MOZ_MEMORY
# include "mozmemory.h"
#endif
#ifdef XP_WIN
# include "windows.h"
#endif
#if defined(MOZ_WIDGET_GTK)
# include "mozilla/WidgetUtilsGtk.h"
#endif // defined(MOZ_WIDGET_GTK)
#ifdef MOZ_WIDGET_COCOA
# include "ChannelPrefsUtil.h"
#endif
using namespace mozilla;
using dom::Promise;
using ipc::FileDescriptor;
#ifdef DEBUG
# define ENSURE_PARENT_PROCESS(func, pref) \
do { \
if (MOZ_UNLIKELY(!XRE_IsParentProcess())) { \
nsPrintfCString msg( \
"ENSURE_PARENT_PROCESS: called %s on %s in a non-parent process", \
func, pref); \
NS_ERROR(msg.get()); \
return NS_ERROR_NOT_AVAILABLE; \
} \
} while (0)
#else // DEBUG
# define ENSURE_PARENT_PROCESS(func, pref) \
if (MOZ_UNLIKELY(!XRE_IsParentProcess())) { \
return NS_ERROR_NOT_AVAILABLE; \
}
#endif // DEBUG
// Forward declarations.
namespace mozilla::StaticPrefs {
static void InitAll();
static void StartObservingAlwaysPrefs();
static void InitOncePrefs();
static void InitStaticPrefsFromShared();
static void RegisterOncePrefs(SharedPrefMapBuilder& aBuilder);
static void ShutdownAlwaysPrefs();
} // namespace mozilla::StaticPrefs
//===========================================================================
// Low-level types and operations
//===========================================================================
typedef nsTArray<nsCString> PrefSaveData;
// 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;
// This is used for pref names and string pref values. We encode the string
// length, then a '/', then the string chars. This encoding means there are no
// special chars that are forbidden or require escaping.
static void SerializeAndAppendString(const nsCString& aChars, nsCString& aStr) {
aStr.AppendInt(uint64_t(aChars.Length()));
aStr.Append('/');
aStr.Append(aChars);
}
static const char* DeserializeString(const char* aChars, nsCString& aStr) {
const char* p = aChars;
uint32_t length = strtol(p, const_cast<char**>(&p), 10);
MOZ_ASSERT(p[0] == '/');
p++; // move past the '/'
aStr.Assign(p, length);
p += length; // move past the string itself
return p;
}
static void GetPrefsJsPreamble(nsACString& aPreamble) {
// clang-format off
aPreamble.AssignLiteral(
"// 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
"// To change a preference value, you can either:" NS_LINEBREAK
"// - modify it via the UI (e.g. via about:config in the browser); or" NS_LINEBREAK
"// - set it within a user.js file in your profile." NS_LINEBREAK
NS_LINEBREAK);
// clang-format on
}
// Keep this in sync with PrefValue in parser/src/lib.rs.
union PrefValue {
// PrefValues within Pref objects own their chars. PrefValues passed around
// as arguments don't own their chars.
const char* mStringVal;
int32_t mIntVal;
bool mBoolVal;
PrefValue() = default;
explicit PrefValue(bool aVal) : mBoolVal(aVal) {}
explicit PrefValue(int32_t aVal) : mIntVal(aVal) {}
explicit PrefValue(const char* aVal) : mStringVal(aVal) {}
bool Equals(PrefType aType, PrefValue aValue) {
switch (aType) {
case PrefType::String: {
if (mStringVal && aValue.mStringVal) {
return strcmp(mStringVal, aValue.mStringVal) == 0;
}
if (!mStringVal && !aValue.mStringVal) {
return true;
}
return false;
}
case PrefType::Int:
return mIntVal == aValue.mIntVal;
case PrefType::Bool:
return mBoolVal == aValue.mBoolVal;
default:
MOZ_CRASH("Unhandled enum value");
}
}
template <typename T>
T Get() const;
void Init(PrefType aNewType, PrefValue aNewValue) {
if (aNewType == PrefType::String) {
MOZ_ASSERT(aNewValue.mStringVal);
aNewValue.mStringVal = moz_xstrdup(aNewValue.mStringVal);
}
*this = aNewValue;
}
void Clear(PrefType aType) {
if (aType == PrefType::String) {
free(const_cast<char*>(mStringVal));
}
// Zero the entire value (regardless of type) via mStringVal.
mStringVal = nullptr;
}
void Replace(bool aHasValue, PrefType aOldType, PrefType aNewType,
PrefValue aNewValue) {
if (aHasValue) {
Clear(aOldType);
}
Init(aNewType, aNewValue);
}
void ToDomPrefValue(PrefType aType, dom::PrefValue* aDomValue) {
switch (aType) {
case PrefType::String:
*aDomValue = nsDependentCString(mStringVal);
return;
case PrefType::Int:
*aDomValue = mIntVal;
return;
case PrefType::Bool:
*aDomValue = mBoolVal;
return;
default:
MOZ_CRASH();
}
}
PrefType FromDomPrefValue(const dom::PrefValue& aDomValue) {
switch (aDomValue.type()) {
case dom::PrefValue::TnsCString:
mStringVal = aDomValue.get_nsCString().get();
return PrefType::String;
case dom::PrefValue::Tint32_t:
mIntVal = aDomValue.get_int32_t();
return PrefType::Int;
case dom::PrefValue::Tbool:
mBoolVal = aDomValue.get_bool();
return PrefType::Bool;
default:
MOZ_CRASH();
}
}
void SerializeAndAppend(PrefType aType, nsCString& aStr) {
switch (aType) {
case PrefType::Bool:
aStr.Append(mBoolVal ? 'T' : 'F');
break;
case PrefType::Int:
aStr.AppendInt(mIntVal);
break;
case PrefType::String: {
SerializeAndAppendString(nsDependentCString(mStringVal), aStr);
break;
}
case PrefType::None:
default:
MOZ_CRASH();
}
}
void ToString(PrefType aType, nsCString& aStr) {
switch (aType) {
case PrefType::Bool:
aStr.Append(mBoolVal ? "true" : "false");
break;
case PrefType::Int:
aStr.AppendInt(mIntVal);
break;
case PrefType::String: {
aStr.Append(nsDependentCString(mStringVal));
break;
}
case PrefType::None:
default:;
}
}
static const char* Deserialize(PrefType aType, const char* aStr,
Maybe<dom::PrefValue>* aDomValue) {
const char* p = aStr;
switch (aType) {
case PrefType::Bool:
if (*p == 'T') {
*aDomValue = Some(true);
} else if (*p == 'F') {
*aDomValue = Some(false);
} else {
*aDomValue = Some(false);
NS_ERROR("bad bool pref value");
}
p++;
return p;
case PrefType::Int: {
*aDomValue = Some(int32_t(strtol(p, const_cast<char**>(&p), 10)));
return p;
}
case PrefType::String: {
nsCString str;
p = DeserializeString(p, str);
*aDomValue = Some(str);
return p;
}
default:
MOZ_CRASH();
}
}
};
template <>
bool PrefValue::Get() const {
return mBoolVal;
}
template <>
int32_t PrefValue::Get() const {
return mIntVal;
}
template <>
nsDependentCString PrefValue::Get() const {
return nsDependentCString(mStringVal);
}
// Like PrefValue but strings are owned.
class OwnedPrefValue {
public:
explicit OwnedPrefValue(bool aVal) : mPrefValue(aVal) {}
explicit OwnedPrefValue(int32_t aVal) : mPrefValue(aVal) {}
explicit OwnedPrefValue(const char* aVal)
: mOwnedStr(aVal), mPrefValue(mOwnedStr.get()) {}
PrefValue GetPrefValue() { return mPrefValue; }
private:
nsCString mOwnedStr;
PrefValue mPrefValue;
};
class nsPrefOverrideMap : public nsIPrefOverrideMap {
NS_DECL_ISUPPORTS
NS_DECL_NSIPREFOVERRIDEMAP
public:
const HashMap<nsCString, Maybe<OwnedPrefValue>>& Ref() const { return mMap; }
private:
virtual ~nsPrefOverrideMap() = default;
// Map full pref name to value or Nothing if pref was not defined.
HashMap<nsCString, Maybe<OwnedPrefValue>> mMap;
};
#ifdef DEBUG
const char* PrefTypeToString(PrefType aType) {
switch (aType) {
case PrefType::None:
return "none";
case PrefType::String:
return "string";
case PrefType::Int:
return "int";
case PrefType::Bool:
return "bool";
default:
MOZ_CRASH("Unhandled enum value");
}
}
#endif
// Assign to aResult a quoted, escaped copy of aOriginal.
static void StrEscape(const char* aOriginal, nsCString& aResult) {
if (aOriginal == nullptr) {
aResult.AssignLiteral("\"\"");
return;
}
// 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;
aResult.Assign('"');
// 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;
}
}
aResult.Append('"');
}
// error case handling for parsing pref strings. Many callers do not check error
// codes, so the returned values may be used even if an error is set.
//
// This method should never return NaN, but may return +-inf if the provided
// number is too large to fit in a float.
static float ParsePrefFloat(const nsCString& aString, nsresult* aError) {
if (aString.IsEmpty()) {
*aError = NS_ERROR_ILLEGAL_VALUE;
return 0.f;
}
// PR_strtod does a locale-independent conversion.
char* stopped = nullptr;
float result = PR_strtod(aString.get(), &stopped);
// Defensively avoid potential breakage caused by returning NaN into
// unsuspecting code. AFAIK this should never happen as PR_strtod cannot
// return NaN as currently configured.
if (std::isnan(result)) {
MOZ_ASSERT_UNREACHABLE("PR_strtod shouldn't return NaN");
*aError = NS_ERROR_ILLEGAL_VALUE;
return 0.f;
}
*aError = (stopped == aString.EndReading()) ? NS_OK : NS_ERROR_ILLEGAL_VALUE;
return result;
}
struct PreferenceMarker {
static constexpr Span<const char> MarkerTypeName() {
return MakeStringSpan("Preference");
}
static void StreamJSONMarkerData(baseprofiler::SpliceableJSONWriter& aWriter,
const ProfilerString8View& aPrefName,
const Maybe<PrefValueKind>& aPrefKind,
PrefType aPrefType,
const ProfilerString8View& aPrefValue) {
aWriter.StringProperty("prefName", aPrefName);
aWriter.StringProperty("prefKind", PrefValueKindToString(aPrefKind));
aWriter.StringProperty("prefType", PrefTypeToString(aPrefType));
aWriter.StringProperty("prefValue", aPrefValue);
}
static MarkerSchema MarkerTypeDisplay() {
using MS = MarkerSchema;
MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable};
schema.AddKeyLabelFormat("prefName", "Name", MS::Format::String,
MS::PayloadFlags::Searchable);
schema.AddKeyLabelFormat("prefKind", "Kind", MS::Format::String);
schema.AddKeyLabelFormat("prefType", "Type", MS::Format::String);
schema.AddKeyLabelFormat("prefValue", "Value", MS::Format::String);
schema.SetTableLabel(
"{marker.data.prefName}: {marker.data.prefValue} "
"({marker.data.prefType})");
return schema;
}
private:
static Span<const char> PrefValueKindToString(
const Maybe<PrefValueKind>& aKind) {
if (aKind) {
return *aKind == PrefValueKind::Default ? MakeStringSpan("Default")
: MakeStringSpan("User");
}
return "Shared";
}
static Span<const char> PrefTypeToString(PrefType type) {
switch (type) {
case PrefType::None:
return "None";
case PrefType::Int:
return "Int";
case PrefType::Bool:
return "Bool";
case PrefType::String:
return "String";
default:
MOZ_ASSERT_UNREACHABLE("Unknown preference type.");
return "Unknown";
}
}
};
namespace mozilla {
struct PrefsSizes {
PrefsSizes()
: mHashTable(0),
mPrefValues(0),
mStringValues(0),
mRootBranches(0),
mPrefNameArena(0),
mCallbacksObjects(0),
mCallbacksDomains(0),
mMisc(0) {}
size_t mHashTable;
size_t mPrefValues;
size_t mStringValues;
size_t mRootBranches;
size_t mPrefNameArena;
size_t mCallbacksObjects;
size_t mCallbacksDomains;
size_t mMisc;
};
} // namespace mozilla
static StaticRefPtr<SharedPrefMap> gSharedMap;
// Arena for Pref names.
// Never access sPrefNameArena directly, always use PrefNameArena()
// because it must only be accessed on the Main Thread
typedef ArenaAllocator<4096, 1> NameArena;
static NameArena* sPrefNameArena;
static inline NameArena& PrefNameArena() {
MOZ_ASSERT(NS_IsMainThread());
if (!sPrefNameArena) {
sPrefNameArena = new NameArena();
}
return *sPrefNameArena;
}
class PrefWrapper;
// Three forward declarations for immediately below
class Pref;
static bool IsPreferenceSanitized(const Pref* const aPref);
static bool ShouldSanitizePreference(const Pref* const aPref);
// Note that this never changes in the parent process, and is only read in
// content processes.
static bool gContentProcessPrefsAreInited = false;
class Pref {
public:
explicit Pref(const nsACString& aName)
: mName(ArenaStrdup(aName, PrefNameArena()), aName.Length()),
mType(static_cast<uint32_t>(PrefType::None)),
mIsSticky(false),
mIsLocked(false),
mIsSanitized(false),
mHasDefaultValue(false),
mHasUserValue(false),
mIsSkippedByIteration(false),
mDefaultValue(),
mUserValue() {}
~Pref() {
// There's no need to free mName because it's allocated in memory owned by
// sPrefNameArena.
mDefaultValue.Clear(Type());
mUserValue.Clear(Type());
}
const char* Name() const { return mName.get(); }
const nsDependentCString& NameString() const { return mName; }
// Types.
PrefType Type() const { return static_cast<PrefType>(mType); }
void SetType(PrefType aType) { mType = static_cast<uint32_t>(aType); }
bool IsType(PrefType aType) const { return Type() == aType; }
bool IsTypeNone() const { return IsType(PrefType::None); }
bool IsTypeString() const { return IsType(PrefType::String); }
bool IsTypeInt() const { return IsType(PrefType::Int); }
bool IsTypeBool() const { return IsType(PrefType::Bool); }
// Other properties.
bool IsLocked() const { return mIsLocked; }
void SetIsLocked(bool aValue) { mIsLocked = aValue; }
bool IsSkippedByIteration() const { return mIsSkippedByIteration; }
void SetIsSkippedByIteration(bool aValue) { mIsSkippedByIteration = aValue; }
bool IsSticky() const { return mIsSticky; }
bool IsSanitized() const { return mIsSanitized; }
bool HasDefaultValue() const { return mHasDefaultValue; }
bool HasUserValue() const { return mHasUserValue; }
template <typename T>
void AddToMap(SharedPrefMapBuilder& aMap) {
// Sanitized preferences should never be added to the shared pref map
MOZ_ASSERT(!ShouldSanitizePreference(this));
aMap.Add(NameString(),
{HasDefaultValue(), HasUserValue(), IsSticky(), IsLocked(),
/* isSanitized */ false, IsSkippedByIteration()},
HasDefaultValue() ? mDefaultValue.Get<T>() : T(),
HasUserValue() ? mUserValue.Get<T>() : T());
}
void AddToMap(SharedPrefMapBuilder& aMap) {
if (IsTypeBool()) {
AddToMap<bool>(aMap);
} else if (IsTypeInt()) {
AddToMap<int32_t>(aMap);
} else if (IsTypeString()) {
AddToMap<nsDependentCString>(aMap);
} else {
MOZ_ASSERT_UNREACHABLE("Unexpected preference type");
}
}
// Other operations.
#define CHECK_SANITIZATION() \
if (IsPreferenceSanitized(this)) { \
glean::security::pref_usage_content_process.Record( \
Some(glean::security::PrefUsageContentProcessExtra{Some(Name())})); \
if (sCrashOnBlocklistedPref) { \
MOZ_CRASH_UNSAFE_PRINTF( \
"Should not access the preference '%s' in the Content Processes", \
Name()); \
} \
}
bool GetBoolValue(PrefValueKind aKind = PrefValueKind::User) const {
MOZ_ASSERT(IsTypeBool());
MOZ_ASSERT(aKind == PrefValueKind::Default ? HasDefaultValue()
: HasUserValue());
CHECK_SANITIZATION();
return aKind == PrefValueKind::Default ? mDefaultValue.mBoolVal
: mUserValue.mBoolVal;
}
int32_t GetIntValue(PrefValueKind aKind = PrefValueKind::User) const {
MOZ_ASSERT(IsTypeInt());
MOZ_ASSERT(aKind == PrefValueKind::Default ? HasDefaultValue()
: HasUserValue());
CHECK_SANITIZATION();
return aKind == PrefValueKind::Default ? mDefaultValue.mIntVal
: mUserValue.mIntVal;
}
const char* GetBareStringValue(
PrefValueKind aKind = PrefValueKind::User) const {
MOZ_ASSERT(IsTypeString());
MOZ_ASSERT(aKind == PrefValueKind::Default ? HasDefaultValue()
: HasUserValue());
CHECK_SANITIZATION();
return aKind == PrefValueKind::Default ? mDefaultValue.mStringVal
: mUserValue.mStringVal;
}
#undef CHECK_SANITIZATION
nsDependentCString GetStringValue(
PrefValueKind aKind = PrefValueKind::User) const {
return nsDependentCString(GetBareStringValue(aKind));
}
void ToDomPref(dom::Pref* aDomPref, bool aIsDestinationWebContentProcess) {
MOZ_ASSERT(XRE_IsParentProcess());
aDomPref->name() = mName;
aDomPref->isLocked() = mIsLocked;
aDomPref->isSanitized() =
aIsDestinationWebContentProcess && ShouldSanitizePreference(this);
if (mHasDefaultValue) {
aDomPref->defaultValue() = Some(dom::PrefValue());
mDefaultValue.ToDomPrefValue(Type(), &aDomPref->defaultValue().ref());
} else {
aDomPref->defaultValue() = Nothing();
}
if (mHasUserValue &&
!(aDomPref->isSanitized() && sOmitBlocklistedPrefValues)) {
aDomPref->userValue() = Some(dom::PrefValue());
mUserValue.ToDomPrefValue(Type(), &aDomPref->userValue().ref());
} else {
aDomPref->userValue() = Nothing();
}
MOZ_ASSERT(aDomPref->defaultValue().isNothing() ||
aDomPref->userValue().isNothing() ||
(mIsSanitized && sOmitBlocklistedPrefValues) ||
(aDomPref->defaultValue().ref().type() ==
aDomPref->userValue().ref().type()));
}
void FromDomPref(const dom::Pref& aDomPref, bool* aValueChanged) {
MOZ_ASSERT(!XRE_IsParentProcess());
MOZ_ASSERT(mName == aDomPref.name());
mIsLocked = aDomPref.isLocked();
mIsSanitized = aDomPref.isSanitized();
const Maybe<dom::PrefValue>& defaultValue = aDomPref.defaultValue();
bool defaultValueChanged = false;
if (defaultValue.isSome()) {
PrefValue value;
PrefType type = value.FromDomPrefValue(defaultValue.ref());
if (!ValueMatches(PrefValueKind::Default, type, value)) {
// Type() is PrefType::None if it's a newly added pref. This is ok.
mDefaultValue.Replace(mHasDefaultValue, Type(), type, value);
SetType(type);
mHasDefaultValue = true;
defaultValueChanged = true;
}
} else if (mHasDefaultValue) {
ClearDefaultValue();
defaultValueChanged = true;
}
const Maybe<dom::PrefValue>& userValue = aDomPref.userValue();
bool userValueChanged = false;
if (userValue.isSome()) {
PrefValue value;
PrefType type = value.FromDomPrefValue(userValue.ref());
if (!ValueMatches(PrefValueKind::User, type, value)) {
// Type() is PrefType::None if it's a newly added pref. This is ok.
mUserValue.Replace(mHasUserValue, Type(), type, value);
SetType(type);
mHasUserValue = true;
userValueChanged = true;
}
} else if (mHasUserValue) {
ClearUserValue();
userValueChanged = true;
}
if (userValueChanged || (defaultValueChanged && !mHasUserValue)) {
*aValueChanged = true;
}
}
void FromWrapper(PrefWrapper& aWrapper);
bool HasAdvisablySizedValues() {
MOZ_ASSERT(XRE_IsParentProcess());
if (!IsTypeString()) {
return true;
}
if (mHasDefaultValue &&
strlen(mDefaultValue.mStringVal) > MAX_ADVISABLE_PREF_LENGTH) {
return false;
}
if (mHasUserValue &&
strlen(mUserValue.mStringVal) > MAX_ADVISABLE_PREF_LENGTH) {
return false;
}
return true;
}
private:
bool ValueMatches(PrefValueKind aKind, PrefType aType, PrefValue aValue) {
return IsType(aType) &&
(aKind == PrefValueKind::Default
? mHasDefaultValue && mDefaultValue.Equals(aType, aValue)
: mHasUserValue && mUserValue.Equals(aType, aValue));
}
public:
void ClearUserValue() {
mUserValue.Clear(Type());
mHasUserValue = false;
}
void ClearDefaultValue() {
mDefaultValue.Clear(Type());
mHasDefaultValue = false;
}
nsresult SetDefaultValue(PrefType aType, PrefValue aValue, bool aIsSticky,
bool aIsLocked, bool* aValueChanged) {
// Types must always match when setting the default value.
if (!IsType(aType)) {
return NS_ERROR_UNEXPECTED;
}
// Should we set the default value? Only if the pref is not locked, and
// doing so would change the default value.
if (!IsLocked()) {
if (aIsLocked) {
SetIsLocked(true);
}
if (aIsSticky) {
mIsSticky = true;
}
if (!ValueMatches(PrefValueKind::Default, aType, aValue)) {
mDefaultValue.Replace(mHasDefaultValue, Type(), aType, aValue);
mHasDefaultValue = true;
if (!mHasUserValue) {
*aValueChanged = true;
}
// What if we change the default to be the same as the user value?
// Should we clear the user value? Currently we don't.
}
}
return NS_OK;
}
nsresult SetUserValue(PrefType aType, PrefValue aValue, bool aFromInit,
bool* aValueChanged) {
// If we have a default value, types must match when setting the user
// value.
if (mHasDefaultValue && !IsType(aType)) {
return NS_ERROR_UNEXPECTED;
}
// Should we clear the user value, if present? Only if the new user value
// matches the default value, and the pref isn't sticky, and we aren't
// force-setting it during initialization.
if (ValueMatches(PrefValueKind::Default, aType, aValue) && !mIsSticky &&
!aFromInit) {
if (mHasUserValue) {
ClearUserValue();
if (!IsLocked()) {
*aValueChanged = true;
}
}
// Otherwise, should we set the user value? Only if doing so would
// change the user value.
} else if (!ValueMatches(PrefValueKind::User, aType, aValue)) {
mUserValue.Replace(mHasUserValue, Type(), aType, aValue);
SetType(aType); // needed because we may have changed the type
mHasUserValue = true;
if (!IsLocked()) {
*aValueChanged = true;
}
}
return NS_OK;
}
// Prefs are serialized in a manner that mirrors dom::Pref. The two should be
// kept in sync. E.g. if something is added to one it should also be added to
// the other. (It would be nice to be able to use the code generated from
// IPDL for serializing dom::Pref here instead of writing by hand this
// serialization/deserialization. Unfortunately, that generated code is
// difficult to use directly, outside of the IPDL IPC code.)
//
// The grammar for the serialized prefs has the following form.
//
// <pref> = <type> <locked> <sanitized> ':' <name> ':' <value>? ':'
// <value>? '\n'
// <type> = 'B' | 'I' | 'S'
// <locked> = 'L' | '-'
// <sanitized> = 'S' | '-'
// <name> = <string-value>
// <value> = <bool-value> | <int-value> | <string-value>
// <bool-value> = 'T' | 'F'
// <int-value> = an integer literal accepted by strtol()
// <string-value> = <int-value> '/' <chars>
// <chars> = any char sequence of length dictated by the preceding
// <int-value>.
//
// No whitespace is tolerated between tokens. <type> must match the types of
// the values.
//
// The serialization is text-based, rather than binary, for the following
// reasons.
//
// - The size difference wouldn't be much different between text-based and
// binary. Most of the space is for strings (pref names and string pref
// values), which would be the same in both styles. And other differences
// would be minimal, e.g. small integers are shorter in text but long
// integers are longer in text.
//
// - Likewise, speed differences should be negligible.
//
// - It's much easier to debug a text-based serialization. E.g. you can
// print it and inspect it easily in a debugger.
//
// Examples of unlocked boolean prefs:
// - "B--:8/my.bool1:F:T\n"
// - "B--:8/my.bool2:F:\n"
// - "B--:8/my.bool3::T\n"
//
// Examples of sanitized, unlocked boolean prefs:
// - "B-S:8/my.bool1:F:T\n"
// - "B-S:8/my.bool2:F:\n"
// - "B-S:8/my.bool3::T\n"
//
// Examples of locked integer prefs:
// - "IL-:7/my.int1:0:1\n"
// - "IL-:7/my.int2:123:\n"
// - "IL-:7/my.int3::-99\n"
//
// Examples of unlocked string prefs:
// - "S--:10/my.string1:3/abc:4/wxyz\n"
// - "S--:10/my.string2:5/1.234:\n"
// - "S--:10/my.string3::7/string!\n"
void SerializeAndAppend(nsCString& aStr, bool aSanitizeUserValue) {
switch (Type()) {
case PrefType::Bool:
aStr.Append('B');
break;
case PrefType::Int:
aStr.Append('I');
break;
case PrefType::String: {
aStr.Append('S');
break;
}
case PrefType::None:
default:
MOZ_CRASH();
}
aStr.Append(mIsLocked ? 'L' : '-');
aStr.Append(aSanitizeUserValue ? 'S' : '-');
aStr.Append(':');
SerializeAndAppendString(mName, aStr);
aStr.Append(':');
if (mHasDefaultValue) {
mDefaultValue.SerializeAndAppend(Type(), aStr);
}
aStr.Append(':');
if (mHasUserValue && !(aSanitizeUserValue && sOmitBlocklistedPrefValues)) {
mUserValue.SerializeAndAppend(Type(), aStr);
}
aStr.Append('\n');
}
static const char* Deserialize(const char* aStr, dom::Pref* aDomPref) {
const char* p = aStr;
// The type.
PrefType type;
if (*p == 'B') {
type = PrefType::Bool;
} else if (*p == 'I') {
type = PrefType::Int;
} else if (*p == 'S') {
type = PrefType::String;
} else {
NS_ERROR("bad pref type");
type = PrefType::None;
}
p++; // move past the type char
// Locked?
bool isLocked;
if (*p == 'L') {
isLocked = true;
} else if (*p == '-') {
isLocked = false;
} else {
NS_ERROR("bad pref locked status");
isLocked = false;
}
p++; // move past the isLocked char
// Sanitize?
bool isSanitized;
if (*p == 'S') {
isSanitized = true;
} else if (*p == '-') {
isSanitized = false;
} else {
NS_ERROR("bad pref sanitized status");
isSanitized = false;
}
p++; // move past the isSanitized char
MOZ_ASSERT(*p == ':');
p++; // move past the ':'
// The pref name.
nsCString name;
p = DeserializeString(p, name);
MOZ_ASSERT(*p == ':');
p++; // move past the ':' preceding the default value
Maybe<dom::PrefValue> maybeDefaultValue;
if (*p != ':') {
dom::PrefValue defaultValue;
p = PrefValue::Deserialize(type, p, &maybeDefaultValue);
}
MOZ_ASSERT(*p == ':');
p++; // move past the ':' between the default and user values
Maybe<dom::PrefValue> maybeUserValue;
if (*p != '\n') {
dom::PrefValue userValue;
p = PrefValue::Deserialize(type, p, &maybeUserValue);
}
MOZ_ASSERT(*p == '\n');
p++; // move past the '\n' following the user value
*aDomPref = dom::Pref(name, isLocked, isSanitized, maybeDefaultValue,
maybeUserValue);
return p;
}
void AddSizeOfIncludingThis(MallocSizeOf aMallocSizeOf, PrefsSizes& aSizes) {
// Note: mName is allocated in sPrefNameArena, measured elsewhere.
aSizes.mPrefValues += aMallocSizeOf(this);
if (IsTypeString()) {
if (mHasDefaultValue) {
aSizes.mStringValues += aMallocSizeOf(mDefaultValue.mStringVal);
}
if (mHasUserValue) {
aSizes.mStringValues += aMallocSizeOf(mUserValue.mStringVal);
}
}
}
void RelocateName(NameArena* aArena) {
mName.Rebind(ArenaStrdup(mName.get(), *aArena), mName.Length());
}
private:
nsDependentCString mName; // allocated in sPrefNameArena
uint32_t mType : 2;
uint32_t mIsSticky : 1;
uint32_t mIsLocked : 1;
uint32_t mIsSanitized : 1;
uint32_t mHasDefaultValue : 1;
uint32_t mHasUserValue : 1;
uint32_t mIsSkippedByIteration : 1;
PrefValue mDefaultValue;
PrefValue mUserValue;
};
struct PrefHasher {
using Key = UniquePtr<Pref>;
using Lookup = const char*;
static HashNumber hash(const Lookup aLookup) { return HashString(aLookup); }
static bool match(const Key& aKey, const Lookup aLookup) {
if (!aLookup || !aKey->Name()) {
return false;
}
return strcmp(aLookup, aKey->Name()) == 0;
}
};
using PrefWrapperBase = Variant<Pref*, SharedPrefMap::Pref>;
class MOZ_STACK_CLASS PrefWrapper : public PrefWrapperBase {
using SharedPref = const SharedPrefMap::Pref;
public:
MOZ_IMPLICIT PrefWrapper(Pref* aPref) : PrefWrapperBase(AsVariant(aPref)) {}
MOZ_IMPLICIT PrefWrapper(const SharedPrefMap::Pref& aPref)
: PrefWrapperBase(AsVariant(aPref)) {}
// Types.
bool IsType(PrefType aType) const { return Type() == aType; }
bool IsTypeNone() const { return IsType(PrefType::None); }
bool IsTypeString() const { return IsType(PrefType::String); }
bool IsTypeInt() const { return IsType(PrefType::Int); }
bool IsTypeBool() const { return IsType(PrefType::Bool); }
#define FORWARD(retType, method) \
retType method() const { \
struct Matcher { \
retType operator()(const Pref* aPref) { return aPref->method(); } \
retType operator()(SharedPref& aPref) { return aPref.method(); } \
}; \
return match(Matcher()); \
}
FORWARD(bool, IsLocked)
FORWARD(bool, IsSanitized)
FORWARD(bool, IsSticky)
FORWARD(bool, HasDefaultValue)
FORWARD(bool, HasUserValue)
FORWARD(const char*, Name)
FORWARD(nsCString, NameString)
FORWARD(PrefType, Type)
#undef FORWARD
#define FORWARD(retType, method) \
retType method(PrefValueKind aKind = PrefValueKind::User) const { \
struct Matcher { \
PrefValueKind mKind; \
\
retType operator()(const Pref* aPref) { return aPref->method(mKind); } \
retType operator()(SharedPref& aPref) { return aPref.method(mKind); } \
}; \
return match(Matcher{aKind}); \
}
FORWARD(bool, GetBoolValue)
FORWARD(int32_t, GetIntValue)
FORWARD(nsCString, GetStringValue)
FORWARD(const char*, GetBareStringValue)
#undef FORWARD
PrefValue GetValue(PrefValueKind aKind = PrefValueKind::User) const {
switch (Type()) {
case PrefType::Bool:
return PrefValue{GetBoolValue(aKind)};
case PrefType::Int:
return PrefValue{GetIntValue(aKind)};
case PrefType::String:
return PrefValue{GetBareStringValue(aKind)};
case PrefType::None:
// This check will be performed in the above functions; but for NoneType
// we need to do it explicitly, then fall-through.
if (IsPreferenceSanitized(Name())) {
glean::security::pref_usage_content_process.Record(Some(
glean::security::PrefUsageContentProcessExtra{Some(Name())}));
if (sCrashOnBlocklistedPref) {
MOZ_CRASH_UNSAFE_PRINTF(
"Should not access the preference '%s' in the Content "
"Processes",
Name());
}
}
[[fallthrough]];
default:
MOZ_ASSERT_UNREACHABLE("Unexpected pref type");
return PrefValue{};
}
}
Result<PrefValueKind, nsresult> WantValueKind(PrefType aType,
PrefValueKind aKind) const {
// WantValueKind may short-circuit GetValue functions and cause them to
// return early, before this check occurs in GetFooValue()
if (this->is<Pref*>() && IsPreferenceSanitized(this->as<Pref*>())) {
glean::security::pref_usage_content_process.Record(
Some(glean::security::PrefUsageContentProcessExtra{Some(Name())}));
if (sCrashOnBlocklistedPref) {
MOZ_CRASH_UNSAFE_PRINTF(
"Should not access the preference '%s' in the Content Processes",
Name());
}
} else if (!this->is<Pref*>()) {
// While we could use Name() above, and avoid the Variant checks, it
// would less efficient than needed and we can instead do a debug-only
// assert here to limit the inefficientcy
MOZ_ASSERT(!IsPreferenceSanitized(Name()),
"We should never have a sanitized SharedPrefMap::Pref.");
}
if (Type() != aType) {
return Err(NS_ERROR_UNEXPECTED);
}
if (aKind == PrefValueKind::Default || IsLocked() || !HasUserValue()) {
if (!HasDefaultValue()) {
return Err(NS_ERROR_UNEXPECTED);
}
return PrefValueKind::Default;
}
return PrefValueKind::User;
}
nsresult GetValue(PrefValueKind aKind, bool* aResult) const {
PrefValueKind kind = MOZ_TRY(WantValueKind(PrefType::Bool, aKind));
*aResult = GetBoolValue(kind);
return NS_OK;
}
nsresult GetValue(PrefValueKind aKind, int32_t* aResult) const {
PrefValueKind kind = MOZ_TRY(WantValueKind(PrefType::Int, aKind));
*aResult = GetIntValue(kind);
return NS_OK;
}
nsresult GetValue(PrefValueKind aKind, uint32_t* aResult) const {
return GetValue(aKind, reinterpret_cast<int32_t*>(aResult));
}
nsresult GetValue(PrefValueKind aKind, float* aResult) const {
nsAutoCString result;
nsresult rv = GetValue(aKind, result);
if (NS_SUCCEEDED(rv)) {
// ParsePrefFloat() does a locale-independent conversion.
// FIXME: Other `GetValue` overloads don't clobber `aResult` on error.
*aResult = ParsePrefFloat(result, &rv);
}
return rv;
}
nsresult GetValue(PrefValueKind aKind, nsACString& aResult) const {
PrefValueKind kind = MOZ_TRY(WantValueKind(PrefType::String, aKind));
aResult = GetStringValue(kind);
return NS_OK;
}
nsresult GetValue(PrefValueKind aKind, nsACString* aResult) const {
return GetValue(aKind, *aResult);
}
// Returns false if this pref doesn't have a user value worth saving.
bool UserValueToStringForSaving(nsCString& aStr,
const nsIPrefOverrideMap* aPrefOverrideMap) {
auto getPrefValue = [&]() -> Result<PrefValue, bool> {
if (aPrefOverrideMap) {
auto& overrideMap =
static_cast<const nsPrefOverrideMap*>(aPrefOverrideMap)->Ref();
if (auto it = overrideMap.lookup(NameString())) {
if (it->value().isNothing()) {
// Don't save pref if pref was not defined before Nimbus set it.
return Err(false);
}
return it->value()->GetPrefValue();
}
}
// Use the user value.
if (!HasUserValue()) {
return Err(false);
}
return GetValue();
};
PrefValue prefValue = MOZ_TRY(getPrefValue());
// Should we save the value, if present? Only if it does not match the
// default value, or it is sticky.
if (!ValueMatches(PrefValueKind::Default, Type(), prefValue) ||
IsSticky()) {
if (IsTypeString()) {
StrEscape(prefValue.Get<nsDependentCString>().get(), aStr);
} else if (IsTypeInt()) {
aStr.AppendInt(prefValue.Get<int32_t>());
} else if (IsTypeBool()) {
aStr = prefValue.Get<bool>() ? "true" : "false";
}
return true;
}
// Do not save default prefs that haven't changed.
return false;
}
bool Matches(PrefType aType, PrefValueKind aKind, PrefValue& aValue,
bool aIsSticky, bool aIsLocked) const {
return (ValueMatches(aKind, aType, aValue) && aIsSticky == IsSticky() &&
aIsLocked == IsLocked());
}
bool ValueMatches(PrefValueKind aKind, PrefType aType,
const PrefValue& aValue) const {
if (!IsType(aType)) {
return false;
}
if (!(aKind == PrefValueKind::Default ? HasDefaultValue()
: HasUserValue())) {
return false;
}
switch (aType) {
case PrefType::Bool:
return GetBoolValue(aKind) == aValue.mBoolVal;
case PrefType::Int:
return GetIntValue(aKind) == aValue.mIntVal;
case PrefType::String:
return strcmp(GetBareStringValue(aKind), aValue.mStringVal) == 0;
default:
MOZ_ASSERT_UNREACHABLE("Unexpected preference type");
return false;
}
}
};
void Pref::FromWrapper(PrefWrapper& aWrapper) {
MOZ_ASSERT(aWrapper.is<SharedPrefMap::Pref>());
auto pref = aWrapper.as<SharedPrefMap::Pref>();
MOZ_ASSERT(IsTypeNone());
MOZ_ASSERT(mName == pref.NameString());
mType = uint32_t(pref.Type());
mIsLocked = pref.IsLocked();
mIsSanitized = pref.IsSanitized();
mIsSticky = pref.IsSticky();
mHasDefaultValue = pref.HasDefaultValue();
mHasUserValue = pref.HasUserValue();
if (mHasDefaultValue) {
mDefaultValue.Init(Type(), aWrapper.GetValue(PrefValueKind::Default));
}
if (mHasUserValue) {
mUserValue.Init(Type(), aWrapper.GetValue(PrefValueKind::User));
}
}
class CallbackNode {
public:
CallbackNode(const nsACString& aDomain, PrefChangedFunc aFunc, void* aData,
Preferences::MatchKind aMatchKind)
: mDomain(AsVariant(nsCString(aDomain))),
mFunc(aFunc),
mData(aData),
mNextAndMatchKind(aMatchKind) {}
CallbackNode(const char* const* aDomains, PrefChangedFunc aFunc, void* aData,
Preferences::MatchKind aMatchKind)
: mDomain(AsVariant(aDomains)),
mFunc(aFunc),
mData(aData),
mNextAndMatchKind(aMatchKind) {}
// mDomain is a UniquePtr<>, so any uses of Domain() should only be temporary
// borrows.
const Variant<nsCString, const char* const*>& Domain() const {
return mDomain;
}
PrefChangedFunc Func() const { return mFunc; }
void ClearFunc() { mFunc = nullptr; }
void* Data() const { return mData; }
Preferences::MatchKind MatchKind() const {
return static_cast<Preferences::MatchKind>(mNextAndMatchKind &
kMatchKindMask);
}
bool DomainIs(const nsACString& aDomain) const {
return mDomain.is<nsCString>() && mDomain.as<nsCString>() == aDomain;
}
bool DomainIs(const char* const* aPrefs) const {
return mDomain == AsVariant(aPrefs);
}
bool Matches(const nsACString& aPrefName) const {
auto match = [&](const nsACString& aStr) {
return MatchKind() == Preferences::ExactMatch
? aPrefName == aStr
: StringBeginsWith(aPrefName, aStr);
};
if (mDomain.is<nsCString>()) {
return match(mDomain.as<nsCString>());
}
for (const char* const* ptr = mDomain.as<const char* const*>(); *ptr;
ptr++) {
if (match(nsDependentCString(*ptr))) {
return true;
}
}
return false;
}
CallbackNode* Next() const {
return reinterpret_cast<CallbackNode*>(mNextAndMatchKind & kNextMask);
}
void SetNext(CallbackNode* aNext) {
uintptr_t matchKind = mNextAndMatchKind & kMatchKindMask;
mNextAndMatchKind = reinterpret_cast<uintptr_t>(aNext);
MOZ_ASSERT((mNextAndMatchKind & kMatchKindMask) == 0);
mNextAndMatchKind |= matchKind;
}
void AddSizeOfIncludingThis(MallocSizeOf aMallocSizeOf, PrefsSizes& aSizes) {
aSizes.mCallbacksObjects += aMallocSizeOf(this);
if (mDomain.is<nsCString>()) {
aSizes.mCallbacksDomains +=
mDomain.as<nsCString>().SizeOfExcludingThisIfUnshared(aMallocSizeOf);
}
}
private:
static const uintptr_t kMatchKindMask = uintptr_t(0x1);
static const uintptr_t kNextMask = ~kMatchKindMask;
Variant<nsCString, const char* const*> mDomain;
// If someone attempts to remove the node from the callback list while
// NotifyCallbacks() is running, |func| is set to nullptr. Such nodes will
// be removed at the end of NotifyCallbacks().
PrefChangedFunc mFunc;
void* mData;
// Conceptually this is two fields:
// - CallbackNode* mNext;
// - Preferences::MatchKind mMatchKind;
// They are combined into a tagged pointer to save memory.
uintptr_t mNextAndMatchKind;
};
using PrefsHashTable = HashSet<UniquePtr<Pref>, PrefHasher>;
// The main prefs hash table. Inside a function so we can assert it's only
// accessed on the main thread. (That assertion can be avoided but only do so
// with great care!)
static inline PrefsHashTable*& HashTable(bool aOffMainThread = false) {
MOZ_ASSERT(NS_IsMainThread() || ServoStyleSet::IsInServoTraversal());
static PrefsHashTable* sHashTable = nullptr;
return sHashTable;
}
#ifdef DEBUG
// This defines the type used to store our `once` mirrors checker. We can't use
// HashMap for now due to alignment restrictions when dealing with
typedef std::function<void()> AntiFootgunCallback;
struct CompareStr {
bool operator()(char const* a, char const* b) const {
return std::strcmp(a, b) < 0;
}
};
typedef std::map<const char*, AntiFootgunCallback, CompareStr> AntiFootgunMap;
static StaticAutoPtr<AntiFootgunMap> gOnceStaticPrefsAntiFootgun;
#endif
// The callback list contains all the priority callbacks followed by the
// non-priority callbacks. gLastPriorityNode records where the first part ends.
static CallbackNode* gFirstCallback = nullptr;
static CallbackNode* gLastPriorityNode = nullptr;
#ifdef DEBUG
# define ACCESS_COUNTS
#endif
#ifdef ACCESS_COUNTS
using AccessCountsHashTable = nsTHashMap<nsCStringHashKey, uint32_t>;
static StaticAutoPtr<AccessCountsHashTable> gAccessCounts;
static void AddAccessCount(const nsACString& aPrefName) {
// FIXME: Servo reads preferences from background threads in unsafe ways (bug
// 1474789), and triggers assertions here if we try to add usage count entries
// from background threads.
if (NS_IsMainThread()) {
JS::AutoSuppressGCAnalysis nogc; // Hash functions will not GC.
uint32_t& count = gAccessCounts->LookupOrInsert(aPrefName);
count++;
}
}
static void AddAccessCount(const char* aPrefName) {
AddAccessCount(nsDependentCString(aPrefName));
}
#else
[[maybe_unused]] static void AddAccessCount(const nsACString& aPrefName) {}
static void AddAccessCount(const char* aPrefName) {}
#endif
// These are only used during the call to NotifyCallbacks().
static bool gCallbacksInProgress = false;
static bool gShouldCleanupDeadNodes = false;
class PrefsHashIter {
using Iterator = decltype(HashTable()->modIter());
using ElemType = Pref*;
Iterator mIter;
public:
explicit PrefsHashIter(PrefsHashTable* aTable) : mIter(aTable->modIter()) {}
class Elem {
friend class PrefsHashIter;
PrefsHashIter& mParent;
bool mDone;
Elem(PrefsHashIter& aIter, bool aDone) : mParent(aIter), mDone(aDone) {}
Iterator& Iter() { return mParent.mIter; }
public:
Elem& operator*() { return *this; }
ElemType get() {
if (mDone) {
return nullptr;
}
return Iter().get().get();
}
ElemType get() const { return const_cast<Elem*>(this)->get(); }
ElemType operator->() { return get(); }
ElemType operator->() const { return get(); }
operator ElemType() { return get(); }
void Remove() { Iter().remove(); }
Elem& operator++() {
MOZ_ASSERT(!mDone);
Iter().next();
mDone = Iter().done();
return *this;
}
bool operator!=(Elem& other) {
return mDone != other.mDone || this->get() != other.get();
}
};
Elem begin() { return Elem(*this, mIter.done()); }
Elem end() { return Elem(*this, true); }
};
class PrefsIter {
using Iterator = decltype(HashTable()->iter());
using ElemType = PrefWrapper;
using HashElem = PrefsHashIter::Elem;
using SharedElem = SharedPrefMap::Pref;
using ElemTypeVariant = Variant<HashElem, SharedElem>;
SharedPrefMap* mSharedMap;
PrefsHashTable* mHashTable;
PrefsHashIter mIter;
ElemTypeVariant mPos;
ElemTypeVariant mEnd;
Maybe<PrefWrapper> mEntry;
public:
PrefsIter(PrefsHashTable* aHashTable, SharedPrefMap* aSharedMap)
: mSharedMap(aSharedMap),
mHashTable(aHashTable),
mIter(aHashTable),
mPos(AsVariant(mIter.begin())),
mEnd(AsVariant(mIter.end())) {
if (Done()) {
NextIterator();
}
}
private:
#define MATCH(type, ...) \
do { \
struct Matcher { \
PrefsIter& mIter; \
type operator()(HashElem& pos) { \
[[maybe_unused]] HashElem& end = mIter.mEnd.as<HashElem>(); \
__VA_ARGS__; \
} \
type operator()(SharedElem& pos) { \
[[maybe_unused]] SharedElem& end = mIter.mEnd.as<SharedElem>(); \
__VA_ARGS__; \
} \
}; \
return mPos.match(Matcher{*this}); \
} while (0);
bool Done() { MATCH(bool, return pos == end); }
PrefWrapper MakeEntry() { MATCH(PrefWrapper, return PrefWrapper(pos)); }
void NextEntry() {
mEntry.reset();
MATCH(void, ++pos);
}
#undef MATCH
bool Next() {
NextEntry();
return !Done() || NextIterator();
}
bool NextIterator() {
if (mPos.is<HashElem>() && mSharedMap) {
mPos = AsVariant(mSharedMap->begin());
mEnd = AsVariant(mSharedMap->end());
return !Done();
}
return false;
}
bool IteratingBase() { return mPos.is<SharedElem>(); }
PrefWrapper& Entry() {
MOZ_ASSERT(!Done());
if (!mEntry.isSome()) {
mEntry.emplace(MakeEntry());
}
return mEntry.ref();
}
public:
class Elem {
friend class PrefsIter;
PrefsIter& mParent;
bool mDone;
Elem(PrefsIter& aIter, bool aDone) : mParent(aIter), mDone(aDone) {
SkipDuplicates();
}
void Next() { mDone = !mParent.Next(); }
void SkipDuplicates() {
while (!mDone &&
(mParent.IteratingBase() ? mParent.mHashTable->has(ref().Name())
: ref().IsTypeNone())) {
Next();
}
}
public:
Elem& operator*() { return *this; }
ElemType& ref() { return mParent.Entry(); }
const ElemType& ref() const { return const_cast<Elem*>(this)->ref(); }
ElemType* operator->() { return &ref(); }
const ElemType* operator->() const { return &ref(); }
operator ElemType() { return ref(); }
Elem& operator++() {
MOZ_ASSERT(!mDone);
Next();
SkipDuplicates();
return *this;
}
bool operator!=(Elem& other) {
if (mDone != other.mDone) {
return true;
}
if (mDone) {
return false;
}
return &this->ref() != &other.ref();
}
};
Elem begin() { return {*this, Done()}; }
Elem end() { return {*this, true}; }
};
static Pref* pref_HashTableLookup(const char* aPrefName);
static void NotifyCallbacks(const nsCString& aPrefName,
const PrefWrapper* aPref = nullptr);
static void NotifyCallbacks(const nsCString& aPrefName,
const PrefWrapper& aPref) {
NotifyCallbacks(aPrefName, &aPref);
}
// The approximate number of preferences in the dynamic hashtable for the parent
// and content processes, respectively. These numbers are used to determine the
// initial size of the dynamic preference hashtables, and should be chosen to
// avoid rehashing during normal usage. The actual number of preferences will,
// or course, change over time, but these numbers only need to be within a
// binary order of magnitude of the actual values to remain effective.
//
// The number for the parent process should reflect the total number of
// preferences in the database, since the parent process needs to initially
// build a dynamic hashtable of the entire preference database. The number for
// the child process should reflect the number of preferences which are likely
// to change after the startup of the first content process, since content
// processes only store changed preferences on top of a snapshot of the database
// created at startup.
//
// Note: The capacity of a hashtable doubles when its length reaches an exact
// power of two. A table with an initial length of 64 is twice as large as one
// with an initial length of 63. This is important in content processes, where
// lookup speed is less critical and we pay the price of the additional overhead
// for each content process. So the initial content length should generally be
// *under* the next power-of-two larger than its expected length.
constexpr size_t kHashTableInitialLengthParent = 3000;
constexpr size_t kHashTableInitialLengthContent = 64;
static PrefSaveData pref_savePrefs(const nsIPrefOverrideMap* aPrefOverrideMap) {
MOZ_ASSERT(NS_IsMainThread());
PrefSaveData savedPrefs(HashTable()->count());
for (auto& pref : PrefsIter(HashTable(), gSharedMap)) {
nsAutoCString prefValueStr;
if (!pref->UserValueToStringForSaving(prefValueStr, aPrefOverrideMap)) {
continue;
}
nsAutoCString prefNameStr;
StrEscape(pref->Name(), prefNameStr);
nsPrintfCString str("user_pref(%s, %s);", prefNameStr.get(),
prefValueStr.get());
savedPrefs.AppendElement(str);
}
return savedPrefs;
}
static Pref* pref_HashTableLookup(const char* aPrefName) {
MOZ_ASSERT(NS_IsMainThread() || ServoStyleSet::IsInServoTraversal());
MOZ_ASSERT_IF(!XRE_IsParentProcess(), gContentProcessPrefsAreInited);
// We use readonlyThreadsafeLookup() because we often have concurrent lookups
// from multiple Stylo threads. This is safe because those threads cannot
// modify sHashTable, and the main thread is blocked while Stylo threads are
// doing these lookups.
auto p = HashTable()->readonlyThreadsafeLookup(aPrefName);
return p ? p->get() : nullptr;
}
// While notifying preference callbacks, this holds the wrapper for the
// preference being notified, in order to optimize lookups.
//
// Note: Callbacks and lookups only happen on the main thread, so this is safe
// to use without locking.
static const PrefWrapper* gCallbackPref;
Maybe<PrefWrapper> pref_SharedLookup(const char* aPrefName) {
MOZ_DIAGNOSTIC_ASSERT(gSharedMap, "gSharedMap must be initialized");
if (Maybe<SharedPrefMap::Pref> pref = gSharedMap->Get(aPrefName)) {
return Some(*pref);
}
return Nothing();
}
Maybe<PrefWrapper> pref_Lookup(const char* aPrefName,
bool aIncludeTypeNone = false) {
MOZ_ASSERT(NS_IsMainThread() || ServoStyleSet::IsInServoTraversal());
AddAccessCount(aPrefName);
if (gCallbackPref && strcmp(aPrefName, gCallbackPref->Name()) == 0) {
return Some(*gCallbackPref);
}
if (Pref* pref = pref_HashTableLookup(aPrefName)) {
if (aIncludeTypeNone || !pref->IsTypeNone() || pref->IsSanitized()) {
return Some(pref);
}
} else if (gSharedMap) {
return pref_SharedLookup(aPrefName);
}
return Nothing();
}
static Result<Pref*, nsresult> pref_LookupForModify(
const char* aPrefName,
const std::function<bool(const PrefWrapper&)>& aCheckFn) {
Maybe<PrefWrapper> wrapper =
pref_Lookup(aPrefName, /* includeTypeNone */ true);
if (wrapper.isNothing()) {
return Err(NS_ERROR_INVALID_ARG);
}
if (!aCheckFn(*wrapper)) {
return nullptr;
}
if (wrapper->is<Pref*>()) {
return wrapper->as<Pref*>();
}
Pref* pref = new Pref(nsDependentCString{aPrefName});
if (!HashTable()->putNew(aPrefName, pref)) {
delete pref;
return Err(NS_ERROR_OUT_OF_MEMORY);
}
pref->FromWrapper(*wrapper);
return pref;
}
static nsresult pref_SetPref(const nsCString& aPrefName, PrefType aType,
PrefValueKind aKind, PrefValue aValue,
bool aIsSticky, bool aIsLocked, bool aFromInit) {
MOZ_ASSERT(XRE_IsParentProcess());
MOZ_ASSERT(NS_IsMainThread());
if (AppShutdown::IsInOrBeyond(ShutdownPhase::XPCOMShutdownThreads)) {
printf(
"pref_SetPref: Attempt to write pref %s after XPCOMShutdownThreads "
"started.\n",
aPrefName.get());
if (nsContentUtils::IsInitialized()) {
xpc_DumpJSStack(true, true, false);
}
MOZ_ASSERT(false, "Late preference writes should be avoided.");
return NS_ERROR_ILLEGAL_DURING_SHUTDOWN;
}
if (!HashTable()) {
return NS_ERROR_OUT_OF_MEMORY;
}
Pref* pref = nullptr;
if (gSharedMap) {
auto result =
pref_LookupForModify(aPrefName.get(), [&](const PrefWrapper& aWrapper) {
return !aWrapper.Matches(aType, aKind, aValue, aIsSticky, aIsLocked);
});
if (result.isOk() && !(pref = result.unwrap())) {
// No changes required.
return NS_OK;
}
}
if (!pref) {
auto p = HashTable()->lookupForAdd(aPrefName.get());
if (!p) {
pref = new Pref(aPrefName);
pref->SetType(aType);
if (!HashTable()->add(p, pref)) {
delete pref;
return NS_ERROR_OUT_OF_MEMORY;
}
} else {
pref = p->get();
}
}
bool valueChanged = false;
nsresult rv;
if (aKind == PrefValueKind::Default) {
rv = pref->SetDefaultValue(aType, aValue, aIsSticky, aIsLocked,
&valueChanged);
} else {
MOZ_ASSERT(!aIsLocked); // `locked` is disallowed in user pref files
rv = pref->SetUserValue(aType, aValue, aFromInit, &valueChanged);
}
if (NS_FAILED(rv)) {
NS_WARNING(
nsPrintfCString("Rejected attempt to change type of pref %s's %s value "
"from %s to %s",
aPrefName.get(),
(aKind == PrefValueKind::Default) ? "default" : "user",
PrefTypeToString(pref->Type()), PrefTypeToString(aType))
.get());
return rv;
}
if (valueChanged) {
if (!aFromInit && profiler_thread_is_being_profiled_for_markers()) {
nsAutoCString value;
aValue.ToString(aType, value);
profiler_add_marker(
"Preference Write", baseprofiler::category::OTHER_PreferenceRead, {},
PreferenceMarker{}, aPrefName, Some(aKind), aType, value);
}
if (aKind == PrefValueKind::User) {
Preferences::HandleDirty();
}
NotifyCallbacks(aPrefName, PrefWrapper(pref));
}
return NS_OK;
}
// Removes |node| from callback list. Returns the node after the deleted one.
static CallbackNode* pref_RemoveCallbackNode(CallbackNode* aNode,
CallbackNode* aPrevNode) {
MOZ_ASSERT(!aPrevNode || aPrevNode->Next() == aNode);
MOZ_ASSERT(aPrevNode || gFirstCallback == aNode);
MOZ_ASSERT(!gCallbacksInProgress);
CallbackNode* next_node = aNode->Next();
if (aPrevNode) {
aPrevNode->SetNext(next_node);
} else {
gFirstCallback = next_node;
}
if (gLastPriorityNode == aNode) {
gLastPriorityNode = aPrevNode;
}
delete aNode;
return next_node;
}
static void NotifyCallbacks(const nsCString& aPrefName,
const PrefWrapper* aPref) {
bool reentered = gCallbacksInProgress;
gCallbackPref = aPref;
auto cleanup = MakeScopeExit([]() { gCallbackPref = nullptr; });
// 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;
for (CallbackNode* node = gFirstCallback; node; node = node->Next()) {
if (node->Func()) {
if (node->Matches(aPrefName)) {
(node->Func())(aPrefName.get(), node->Data());
}
}
}
gCallbacksInProgress = reentered;
if (gShouldCleanupDeadNodes && !gCallbacksInProgress) {
CallbackNode* prev_node = nullptr;
CallbackNode* node = gFirstCallback;
while (node) {
if (!node->Func()) {
node = pref_RemoveCallbackNode(node, prev_node);
} else {
prev_node = node;
node = node->Next();
}
}
gShouldCleanupDeadNodes = false;
}
#ifdef DEBUG
if (XRE_IsParentProcess() &&
!StaticPrefs::preferences_force_disable_check_once_policy() &&
(StaticPrefs::preferences_check_once_policy() || xpc::IsInAutomation())) {
// Check that we aren't modifying a `once`-mirrored pref using that pref
// name. We have about 100 `once`-mirrored prefs. std::map performs a
// search in O(log n), so this is fast enough.
MOZ_ASSERT(gOnceStaticPrefsAntiFootgun);
auto search = gOnceStaticPrefsAntiFootgun->find(aPrefName.get());
if (search != gOnceStaticPrefsAntiFootgun->end()) {
// Run the callback.
(search->second)();
}
}
#endif
}
//===========================================================================
// Prefs parsing
//===========================================================================
extern "C" {
// Keep this in sync with PrefFn in parser/src/lib.rs.
typedef void (*PrefsParserPrefFn)(const char* aPrefName, PrefType aType,
PrefValueKind aKind, PrefValue aValue,
bool aIsSticky, bool aIsLocked);
// Keep this in sync with ErrorFn in parser/src/lib.rs.
//
// `aMsg` is just a borrow of the string, and must be copied if it is used
// outside the lifetime of the prefs_parser_parse() call.
typedef void (*PrefsParserErrorFn)(const char* aMsg);
// Keep this in sync with prefs_parser_parse() in parser/src/lib.rs.
bool prefs_parser_parse(const char* aPath, PrefValueKind aKind,
const char* aBuf, size_t aLen,
PrefsParserPrefFn aPrefFn, PrefsParserErrorFn aErrorFn);
}
class Parser {
public:
Parser() = default;
~Parser() = default;
bool Parse(PrefValueKind aKind, const char* aPath, const nsCString& aBuf) {
MOZ_ASSERT(XRE_IsParentProcess());
return prefs_parser_parse(aPath, aKind, aBuf.get(), aBuf.Length(),
HandlePref, HandleError);
}
private:
static void HandlePref(const char* aPrefName, PrefType aType,
PrefValueKind aKind, PrefValue aValue, bool aIsSticky,
bool aIsLocked) {
MOZ_ASSERT(XRE_IsParentProcess());
pref_SetPref(nsDependentCString(aPrefName), aType, aKind, aValue, aIsSticky,
aIsLocked,
/* fromInit */ true);
}
static void HandleError(const char* aMsg) {
nsresult rv;
nsCOMPtr<nsIConsoleService> console =
do_GetService("@mozilla.org/consoleservice;1", &rv);
if (NS_SUCCEEDED(rv)) {
console->LogStringMessage(NS_ConvertUTF8toUTF16(aMsg).get());
}
#ifdef DEBUG
NS_ERROR(aMsg);
#else
printf_stderr("%s\n", aMsg);
#endif
}
};
// The following code is test code for the gtest.
static void TestParseErrorHandlePref(const char* aPrefName, PrefType aType,
PrefValueKind aKind, PrefValue aValue,
bool aIsSticky, bool aIsLocked) {}
constinit static nsCString gTestParseErrorMsgs;
static void TestParseErrorHandleError(const char* aMsg) {
gTestParseErrorMsgs.Append(aMsg);
gTestParseErrorMsgs.Append('\n');
}
// Keep this in sync with the declaration in test/gtest/Parser.cpp.
void TestParseError(PrefValueKind aKind, const char* aText,
nsCString& aErrorMsg) {
prefs_parser_parse("test", aKind, aText, strlen(aText),
TestParseErrorHandlePref, TestParseErrorHandleError);
// Copy the error messages into the outparam, then clear them from
// gTestParseErrorMsgs.
aErrorMsg.Assign(gTestParseErrorMsgs);
gTestParseErrorMsgs.Truncate();
}
//===========================================================================
// 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 = HashString(aKey->mDomain);
return AddToHash(hash, aKey->mCanonical);
}
public:
// Create a PrefCallback with a strong reference to its observer.
PrefCallback(const nsACString& 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 nsACString& 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;
}
// This is explicitly not a copy constructor.
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(const PrefCallback&) = delete;
PrefCallback(PrefCallback&&) = default;
MOZ_COUNTED_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;
}
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;
}
enum { ALLOW_MEMMOVE = true };
private:
nsCString mDomain;
nsPrefBranch* mBranch;
// Exactly one of mWeakRef and mStrongRef should be non-null.
nsWeakPtr mWeakRef;
nsCOMPtr<nsIObserver> mStrongRef;
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
nsPrefBranch(const char* aPrefRoot, PrefValueKind aKind);
nsPrefBranch() = delete;
static void NotifyObserver(const char* aNewpref, void* aData);
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
private:
using PrefName = nsCString;
virtual ~nsPrefBranch();
int32_t GetRootLength() const { return mPrefRoot.Length(); }
nsresult GetDefaultFromPropertiesFile(const char* aPrefName,
nsAString& aReturn);
// As SetCharPref, but without any check on the length of |aValue|.
nsresult SetCharPrefNoLengthCheck(const char* aPrefName,
const nsACString& aValue);
// 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 {
return GetPrefName(nsDependentCString(aPrefName));
}
PrefName GetPrefName(const nsACString& aPrefName) const;
void FreeObserverList(void);
const nsCString mPrefRoot;
PrefValueKind mKind;
bool mFreeingObserverList;
nsClassHashtable<PrefCallback, PrefCallback> mObservers;
};
class nsPrefLocalizedString final : public nsIPrefLocalizedString {
public:
nsPrefLocalizedString();
NS_DECL_ISUPPORTS
NS_FORWARD_NSISUPPORTSPRIMITIVE(mUnicodeString->)
NS_FORWARD_NSISUPPORTSSTRING(mUnicodeString->)
nsresult Init();
private:
virtual ~nsPrefLocalizedString();
nsCOMPtr<nsISupportsString> mUnicodeString;
};
//----------------------------------------------------------------------------
// nsPrefBranch
//----------------------------------------------------------------------------
nsPrefBranch::nsPrefBranch(const char* aPrefRoot, PrefValueKind aKind)
: mPrefRoot(aPrefRoot), mKind(aKind), mFreeingObserverList(false) {
nsCOMPtr<nsIObserverService> observerService = 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(); }
NS_IMPL_ISUPPORTS(nsPrefBranch, nsIPrefBranch, nsIObserver,
nsISupportsWeakReference)
NS_IMETHODIMP
nsPrefBranch::GetRoot(nsACString& aRoot) {
aRoot = mPrefRoot;
return NS_OK;
}
NS_IMETHODIMP
nsPrefBranch::GetPrefType(const char* aPrefName, int32_t* aRetVal) {
NS_ENSURE_ARG(aPrefName);
const PrefName& prefName = GetPrefName(aPrefName);
*aRetVal = Preferences::GetType(prefName.get());
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);
return Preferences::GetBool(pref.get(), aRetVal, mKind);
}
NS_IMETHODIMP
nsPrefBranch::SetBoolPref(const char* aPrefName, bool aValue) {
NS_ENSURE_ARG(aPrefName);
const PrefName& pref = GetPrefName(aPrefName);
return Preferences::SetBool(pref.get(), aValue, mKind);
}
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;
nsresult rv = GetCharPref(aPrefName, stringVal);
if (NS_SUCCEEDED(rv)) {
// ParsePrefFloat() does a locale-independent conversion.
*aRetVal = ParsePrefFloat(stringVal, &rv);
}
return rv;
}
NS_IMETHODIMP
nsPrefBranch::GetCharPrefWithDefault(const char* aPrefName,
const nsACString& aDefaultValue,
uint8_t aArgc, nsACString& aRetVal) {
nsresult rv = GetCharPref(aPrefName, aRetVal);
if (NS_FAILED(rv) && aArgc == 1) {
aRetVal = aDefaultValue;
return NS_OK;
}
return rv;
}
NS_IMETHODIMP
nsPrefBranch::GetCharPref(const char* aPrefName, nsACString& aRetVal) {
NS_ENSURE_ARG(aPrefName);
const PrefName& pref = GetPrefName(aPrefName);
return Preferences::GetCString(pref.get(), aRetVal, mKind);
}
NS_IMETHODIMP
nsPrefBranch::SetCharPref(const char* aPrefName, const nsACString& aValue) {
nsresult rv = CheckSanityOfStringLength(aPrefName, aValue);
if (NS_FAILED(rv)) {
return rv;
}
return SetCharPrefNoLengthCheck(aPrefName, aValue);
}
nsresult nsPrefBranch::SetCharPrefNoLengthCheck(const char* aPrefName,
const nsACString& aValue) {
NS_ENSURE_ARG(aPrefName);
const PrefName& pref = GetPrefName(aPrefName);
return Preferences::SetCString(pref.get(), aValue, mKind);
}
NS_IMETHODIMP
nsPrefBranch::GetStringPref(const char* aPrefName,
const nsACString& aDefaultValue, uint8_t aArgc,
nsACString& aRetVal) {
nsCString utf8String;
nsresult rv = GetCharPref(aPrefName, utf8String);
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;
}
return SetCharPrefNoLengthCheck(aPrefName, aValue);
}
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);
return Preferences::GetInt(pref.get(), aRetVal, mKind);
}
NS_IMETHODIMP
nsPrefBranch::SetIntPref(const char* aPrefName, int32_t aValue) {
NS_ENSURE_ARG(aPrefName);
const PrefName& pref = GetPrefName(aPrefName);
return Preferences::SetInt(pref.get(), aValue, mKind);
}
NS_IMETHODIMP
nsPrefBranch::GetComplexValue(const char* aPrefName, const nsIID& aType,
void** aRetVal) {
NS_ENSURE_ARG(aPrefName);
nsresult rv;
nsAutoCString utf8String;
// We have to do this one first because it's different to all the rest.
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;
if (mKind == PrefValueKind::Default) {
bNeedDefault = true;
} else {
// if there is no user (or locked) value
if (!Preferences::HasUserValue(pref.get()) &&
!Preferences::IsLocked(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)) {
theString->SetData(utf16String);
}
} else {
rv = GetCharPref(aPrefName, utf8String);
if (NS_SUCCEEDED(rv)) {
theString->SetData(NS_ConvertUTF8toUTF16(utf8String));
}
}
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
rv = GetCharPref(aPrefName, utf8String);
if (NS_FAILED(rv)) {
return rv;
}
if (aType.Equals(NS_GET_IID(nsIFile))) {
ENSURE_PARENT_PROCESS("GetComplexValue(nsIFile)", aPrefName);
MOZ_TRY(NS_NewLocalFileWithPersistentDescriptor(
utf8String, reinterpret_cast<nsIFile**>(aRetVal)));
return NS_OK;
}
if (aType.Equals(NS_GET_IID(nsIRelativeFilePref))) {
ENSURE_PARENT_PROCESS("GetComplexValue(nsIRelativeFilePref)", aPrefName);
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;
MOZ_TRY(NS_NewLocalFileWithRelativeDescriptor(
fromFile, Substring(++keyEnd, strEnd), getter_AddRefs(theFile)));
nsCOMPtr<nsIRelativeFilePref> relativePref = new nsRelativeFilePref();
(void)relativePref->SetFile(theFile);
(void)relativePref->SetRelativeToKey(key);
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.",
aLength, GetPrefName(aPrefName).get()));
rv = console->LogStringMessage(NS_ConvertUTF8toUTF16(message).get());
if (NS_FAILED(rv)) {
return rv;
}
return NS_OK;
}
NS_IMETHODIMP
nsPrefBranch::SetComplexValue(const char* aPrefName, const nsIID& aType,
nsISupports* aValue) {
ENSURE_PARENT_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)) {
rv = SetCharPrefNoLengthCheck(aPrefName, descriptorString);
}
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);
return SetCharPrefNoLengthCheck(aPrefName, descriptorString);
}
if (aType.Equals(NS_GET_IID(nsIPrefLocalizedString))) {
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;
}
rv = SetCharPrefNoLengthCheck(aPrefName,
NS_ConvertUTF16toUTF8(wideString));
}
}
return rv;
}
NS_WARNING("nsPrefBranch::SetComplexValue - Unsupported interface type");
return NS_NOINTERFACE;
}
NS_IMETHODIMP
nsPrefBranch::ClearUserPref(const char* aPrefName) {
NS_ENSURE_ARG(aPrefName);
const PrefName& pref = GetPrefName(aPrefName);
return Preferences::ClearUser(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 = Preferences::HasUserValue(pref.get());
return NS_OK;
}
NS_IMETHODIMP
nsPrefBranch::PrefHasDefaultValue(const char* aPrefName, bool* aRetVal) {
NS_ENSURE_ARG_POINTER(aRetVal);
NS_ENSURE_ARG(aPrefName);
const PrefName& pref = GetPrefName(aPrefName);
*aRetVal = Preferences::HasDefaultValue(pref.get());
return NS_OK;
}
NS_IMETHODIMP
nsPrefBranch::LockPref(const char* aPrefName) {
NS_ENSURE_ARG(aPrefName);
const PrefName& pref = GetPrefName(aPrefName);
return Preferences::Lock(pref.get());
}
NS_IMETHODIMP
nsPrefBranch::PrefIsLocked(const char* aPrefName, bool* aRetVal) {
NS_ENSURE_ARG_POINTER(aRetVal);
NS_ENSURE_ARG(aPrefName);
const PrefName& pref = GetPrefName(aPrefName);
*aRetVal = Preferences::IsLocked(pref.get());
return NS_OK;
}
NS_IMETHODIMP
nsPrefBranch::PrefIsSanitized(const char* aPrefName, bool* aRetVal) {
NS_ENSURE_ARG_POINTER(aRetVal);
NS_ENSURE_ARG(aPrefName);
const PrefName& pref = GetPrefName(aPrefName);
*aRetVal = Preferences::IsSanitized(pref.get());
return NS_OK;
}
NS_IMETHODIMP
nsPrefBranch::UnlockPref(const char* aPrefName) {
NS_ENSURE_ARG(aPrefName);
const PrefName& pref = GetPrefName(aPrefName);
return Preferences::Unlock(pref.get());
}
NS_IMETHODIMP
nsPrefBranch::DeleteBranch(const char* aStartingAt) {
ENSURE_PARENT_PROCESS("DeleteBranch", aStartingAt);
NS_ENSURE_ARG(aStartingAt);
MOZ_ASSERT(NS_IsMainThread());
if (!HashTable()) {
return NS_ERROR_NOT_INITIALIZED;
}
const PrefName& pref = GetPrefName(aStartingAt);
nsAutoCString branchName(pref.get());
// Add a trailing '.' if it doesn't already have one.
if (branchName.Length() > 1 && !StringEndsWith(branchName, "."_ns)) {
branchName += '.';
}
const nsACString& branchNameNoDot =
Substring(branchName, 0, branchName.Length() - 1);
// Collect the list of prefs to remove
AutoTArray<const char*, 32> prefNames;
for (auto& pref : PrefsIter(HashTable(), gSharedMap)) {
// Match preferences that start with branchName or equal branchNameNoDot.
// For inputs ending with "..", this matches the node without the trailing
// dot and all children with the double dot prefix.
// For other inputs, this matches both the node itself and all children.
if (StringBeginsWith(pref->NameString(), branchName) ||
pref->NameString() == branchNameNoDot) {
prefNames.AppendElement(pref->Name());
}
}
// Remove the listed preferences.
for (auto& prefName : prefNames) {
auto result = pref_LookupForModify(
prefName, [](const PrefWrapper& aPref) { return !aPref.IsTypeNone(); });
if (result.isErr()) {
// Pref was likely removed by a previously-notified callback
continue;
}
if (Pref* pref = result.unwrap()) {
pref->ClearUserValue();
pref->ClearDefaultValue();
MOZ_ASSERT(
!gSharedMap || !pref->IsSanitized() || !gSharedMap->Has(pref->Name()),
"A sanitized pref should never be in the shared pref map.");
if (!pref->IsSanitized() &&
(!gSharedMap || !gSharedMap->Has(pref->Name()))) {
HashTable()->remove(prefName);
} else {
// If there is a matching shared pref, it must be shadowed by an empty
// entry in the HashTable().
pref->SetType(PrefType::None);
}
NotifyCallbacks(nsDependentCString{prefName});
}
}
Preferences::HandleDirty();
return NS_OK;
}
NS_IMETHODIMP
nsPrefBranch::GetChildList(const char* aStartingAt,
nsTArray<nsCString>& aChildArray) {
NS_ENSURE_ARG(aStartingAt);
MOZ_ASSERT(NS_IsMainThread());
// This will contain a list of all the pref name strings. Allocated on the
// stack for speed.
AutoTArray<nsCString, 32> prefArray;
const PrefName& parent = GetPrefName(aStartingAt);
size_t parentLen = parent.Length();
for (auto& pref : PrefsIter(HashTable(), gSharedMap)) {
if (strncmp(pref->Name(), parent.get(), parentLen) == 0) {
prefArray.AppendElement(pref->NameString());
}
}
// Now that we've built up the list, run the callback on all the matching
// elements.
aChildArray.SetCapacity(prefArray.Length());
for (auto& element : prefArray) {
// 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.
aChildArray.AppendElement(Substring(element, mPrefRoot.Length()));
}
return NS_OK;
}
NS_IMETHODIMP
nsPrefBranch::AddObserverImpl(const nsACString& aDomain, nsIObserver* aObserver,
bool aHoldWeak) {
UniquePtr<PrefCallback> pCallback;
NS_ENSURE_ARG(aObserver);
const nsCString& prefName = GetPrefName(aDomain);
// 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 = MakeUnique<PrefCallback>(prefName, weakRefFactory, this);
} else {
// Construct a PrefCallback with a strong reference to the observer.
pCallback = MakeUnique<PrefCallback>(prefName, aObserver, this);
}
mObservers.WithEntryHandle(pCallback.get(), [&](auto&& p) {
if (p) {
NS_WARNING(
nsPrintfCString("Ignoring duplicate observer: %s", prefName.get())
.get());
} else {
// 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.
Preferences::RegisterCallback(NotifyObserver, prefName, pCallback.get(),
Preferences::PrefixMatch,
/* isPriority */ false);
p.Insert(std::move(pCallback));
}
});
return NS_OK;
}
NS_IMETHODIMP
nsPrefBranch::RemoveObserverImpl(const nsACString& aDomain,
nsIObserver* aObserver) {
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.
const nsCString& prefName = GetPrefName(aDomain);
PrefCallback key(prefName, aObserver, this);
mozilla::UniquePtr<PrefCallback> pCallback;
mObservers.Remove(&key, &pCallback);
if (pCallback) {
rv = Preferences::UnregisterCallback(
NotifyObserver, prefName, pCallback.get(), Preferences::PrefixMatch);
}
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();
nsDependentCString suffix(aNewPref + len);
observer->Observe(static_cast<nsIPrefBranch*>(pCallback->GetPrefBranch()),
NS_PREFBRANCH_PREFCHANGE_TOPIC_ID,
NS_ConvertASCIItoUTF16(suffix).get());
}
size_t nsPrefBranch::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
size_t n = aMallocSizeOf(this);
n += mPrefRoot.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
n += mObservers.ShallowSizeOfExcludingThis(aMallocSizeOf);
for (const auto& entry : mObservers) {
const PrefCallback* data = entry.GetWeak();
n += data->SizeOfIncludingThis(aMallocSizeOf);
}
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()) {
auto callback = iter.UserData();
Preferences::UnregisterCallback(nsPrefBranch::NotifyObserver,
callback->GetDomain(), callback,
Preferences::PrefixMatch);
iter.Remove();
}
nsCOMPtr<nsIObserverService> observerService = services::GetObserverService();
if (observerService) {
observerService->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
}
mFreeingObserverList = false;
}
void nsPrefBranch::RemoveExpiredCallback(PrefCallback* aCallback) {
MOZ_ASSERT(aCallback->IsExpired());
mObservers.Remove(aCallback);
}
nsresult nsPrefBranch::GetDefaultFromPropertiesFile(const char* aPrefName,
nsAString& aReturn) {
// The default value contains a URL to a .properties file.
nsAutoCString propertyFileURL;
nsresult rv = Preferences::GetCString(aPrefName, propertyFileURL,
PrefValueKind::Default);
if (NS_FAILED(rv)) {
return rv;
}
nsCOMPtr<nsIStringBundleService> bundleService =
components::StringBundle::Service();
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 nsACString& aPrefName) const {
if (mPrefRoot.IsEmpty()) {
return PrefName(PromiseFlatCString(aPrefName));
}
return PrefName(mPrefRoot + aPrefName);
}
//----------------------------------------------------------------------------
// nsPrefLocalizedString
//----------------------------------------------------------------------------
nsPrefLocalizedString::nsPrefLocalizedString() = default;
nsPrefLocalizedString::~nsPrefLocalizedString() = default;
NS_IMPL_ISUPPORTS(nsPrefLocalizedString, nsIPrefLocalizedString,
nsISupportsString)
nsresult nsPrefLocalizedString::Init() {
nsresult rv;
mUnicodeString = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
return rv;
}
//----------------------------------------------------------------------------
// nsPrefOverrideMap
//----------------------------------------------------------------------------
NS_IMPL_ISUPPORTS(nsPrefOverrideMap, nsIPrefOverrideMap)
NS_IMETHODIMP
nsPrefOverrideMap::AddEntry(const nsACString& aPrefName,
JS::Handle<JS::Value> aPrefValue, JSContext* aCx) {
nsCString prefName(aPrefName);
auto maybePrefWrapper = pref_Lookup(prefName.get());
if (NS_WARN_IF(!maybePrefWrapper)) {
return NS_ERROR_DOM_NOT_FOUND_ERR;
}
nsAutoCString str;
auto jsValueToPrefValue = [&]() -> Result<Maybe<OwnedPrefValue>, nsresult> {
switch (aPrefValue.type()) {
case JS::ValueType::Boolean:
if (NS_WARN_IF(!maybePrefWrapper->IsTypeBool())) {
return Err(NS_ERROR_DOM_TYPE_MISMATCH_ERR);
}
return Some(OwnedPrefValue(aPrefValue.toBoolean()));
case JS::ValueType::Int32:
if (NS_WARN_IF(!maybePrefWrapper->IsTypeInt())) {
return Err(NS_ERROR_DOM_TYPE_MISMATCH_ERR);
}
return Some(OwnedPrefValue(aPrefValue.toInt32()));
case JS::ValueType::String: {
if (NS_WARN_IF(!maybePrefWrapper->IsTypeString())) {
return Err(NS_ERROR_DOM_TYPE_MISMATCH_ERR);
}
if (NS_WARN_IF(!AssignJSString(aCx, str, aPrefValue.toString()))) {
return Err(NS_ERROR_OUT_OF_MEMORY);
}
return Some(OwnedPrefValue(str.get()));
}
case JS::ValueType::Null:
return Result<Maybe<OwnedPrefValue>, nsresult>(Nothing());
default:
NS_WARNING("Invalid type in nsPrefOverrideMap::AddEntry");
return Err(NS_ERROR_DOM_TYPE_MISMATCH_ERR);
}
};
Maybe<OwnedPrefValue> prefValue = MOZ_TRY(jsValueToPrefValue());
if (NS_WARN_IF(!mMap.put(prefName, prefValue))) {
return NS_ERROR_OUT_OF_MEMORY;
}
return NS_OK;
}
NS_IMETHODIMP
nsPrefOverrideMap::GetEntry(const nsACString& aPrefName, JSContext* aCx,
JS::MutableHandle<JS::Value> aPrefValue) {
nsCString prefName(aPrefName);
auto maybePrefWrapper = pref_Lookup(prefName.get());
if (NS_WARN_IF(!maybePrefWrapper)) {
return NS_ERROR_DOM_NOT_FOUND_ERR;
}
auto prefType = maybePrefWrapper->Type();
auto prefValueToJsValue = [&]() -> mozilla::Result<JS::Value, nsresult> {
if (auto it = mMap.lookup(prefName)) {
if (it->value().isNothing()) {
return JS::NullValue();
}
switch (prefType) {
case PrefType::Bool:
return JS::BooleanValue(it->value()->GetPrefValue().Get<bool>());
case PrefType::Int:
return JS::Int32Value(it->value()->GetPrefValue().Get<int32_t>());
case PrefType::String: {
auto str = it->value()->GetPrefValue().Get<nsDependentCString>();
return JS::StringValue(
JS_NewStringCopyN(aCx, str.get(), str.Length()));
}
default:
// Do not expect type NONE
NS_WARNING("Invalid type in nsPrefOverrideMap::GetEntry");
return Err(NS_ERROR_DOM_TYPE_MISMATCH_ERR);
}
}
return Err(NS_ERROR_ILLEGAL_VALUE);
};
auto ret = MOZ_TRY(prefValueToJsValue());
aPrefValue.set(ret);
return NS_OK;
}
//----------------------------------------------------------------------------
// 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;
}
//===========================================================================
// class Preferences and related things
//===========================================================================
namespace mozilla {
#define INITIAL_PREF_FILES 10
void Preferences::HandleDirty() {
MOZ_ASSERT(XRE_IsParentProcess());
if (!HashTable() || !sPreferences) {
return;
}
if (sPreferences->mProfileShutdown) {
NS_WARNING("Setting user pref after profile shutdown.");
return;
}
if (!sPreferences->mDirty) {
sPreferences->mDirty = true;
if (sPreferences->mCurrentFile && sPreferences->AllowOffMainThreadSave() &&
!sPreferences->mSavePending) {
sPreferences->mSavePending = true;
static const int PREF_DELAY_MS = 500;
NS_DelayedDispatchToCurrentThread(
NewRunnableMethod("Preferences::SavePrefFileAsynchronous",
sPreferences.get(),
&Preferences::SavePrefFileAsynchronous),
PREF_DELAY_MS);
}
}
}
static nsresult openPrefFile(nsIFile* aFile, PrefValueKind aKind);
static nsresult parsePrefData(const nsCString& aData, PrefValueKind aKind);
// Note: if sShutdown is true, sPreferences will be nullptr.
StaticRefPtr<Preferences> Preferences::sPreferences;
bool Preferences::sShutdown = false;
// This globally enables or disables OMT pref writing, both sync and async.
static int32_t sAllowOMTPrefWrite = -1;
// Write the preference data to a file.
class PreferencesWriter final {
public:
PreferencesWriter() = default;
static nsresult Write(nsIFile* aFile, PrefSaveData& aPrefs) {
nsCOMPtr<nsIOutputStream> outStreamSink;
nsCOMPtr<nsIOutputStream> outStream;
uint32_t writeAmount;
nsresult rv;
// Execute a "safe" save by saving through a tempfile.
rv = NS_NewSafeLocalFileOutputStream(getter_AddRefs(outStreamSink), aFile,
-1, 0600);
if (NS_FAILED(rv)) {
return rv;
}
rv = NS_NewBufferedOutputStream(getter_AddRefs(outStream),
outStreamSink.forget(), 4096);
if (NS_FAILED(rv)) {
return rv;
}
struct CharComparator {
bool LessThan(const nsCString& aA, const nsCString& aB) const {
return aA < aB;
}
bool Equals(const nsCString& aA, const nsCString& aB) const {
return aA == aB;
}
};
// Sort the preferences to make a readable file on disk.
aPrefs.Sort(CharComparator());
// Write out the file header.
nsAutoCString preamble;
::GetPrefsJsPreamble(preamble);
outStream->Write(preamble.get(), preamble.Length(), &writeAmount);
for (nsCString& pref : aPrefs) {
outStream->Write(pref.get(), pref.Length(), &writeAmount);
outStream->Write(NS_LINEBREAK, NS_LINEBREAK_LEN, &writeAmount);
}
// Tell the safe output stream to overwrite the real prefs file.
// (It'll abort if there were any errors during writing.)
nsCOMPtr<nsISafeOutputStream> safeStream = do_QueryInterface(outStream);
MOZ_ASSERT(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
return rv;
}
static void Flush() {
MOZ_DIAGNOSTIC_ASSERT(sPendingWriteCount >= 0);
// SpinEventLoopUntil is unfortunate, but ultimately it's the best thing
// we can do here given the constraint that we need to ensure that
// the preferences on disk match what we have in memory. We could
// easily perform the write here ourselves by doing exactly what
// happens in PWRunnable::Run. This would be the right thing to do
// if we're stuck here because other unrelated runnables are taking
// a long time, and the wrong thing to do if PreferencesWriter::Write
// is what takes a long time, as we would be trading a SpinEventLoopUntil
// for a synchronous disk write, wherein we could not even spin the
// event loop. Given that PWRunnable generally runs on a thread pool,
// if we're stuck here, it's likely because of PreferencesWriter::Write
// and not some other runnable. Thus, spin away.
mozilla::SpinEventLoopUntil("PreferencesWriter::Flush"_ns,
[]() { return sPendingWriteCount <= 0; });
}
// 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.
static Atomic<PrefSaveData*> sPendingWriteData;
// This is the number of writes via PWRunnables which have been dispatched
// but not yet completed. This is intended to be used by Flush to ensure
// that there are no outstanding writes left incomplete, and thus our prefs
// on disk are in sync with what we have in memory.
static Atomic<int> sPendingWriteCount;
// See PWRunnable::Run for details on why we need this lock.
static StaticMutex sWritingToFile MOZ_UNANNOTATED;
};
Atomic<PrefSaveData*> PreferencesWriter::sPendingWriteData(nullptr);
Atomic<int> PreferencesWriter::sPendingWriteCount(0);
StaticMutex PreferencesWriter::sWritingToFile;
class PWRunnable : public Runnable {
public:
explicit PWRunnable(
nsIFile* aFile,
UniquePtr<MozPromiseHolder<Preferences::WritePrefFilePromise>>
aPromiseHolder)
: Runnable("PWRunnable"),
mFile(aFile),
mPromiseHolder(std::move(aPromiseHolder)) {}
NS_IMETHOD Run() override {
// Preference writes are handled a bit strangely, in that a "newer"
// write is generally regarded as always better. For this reason,
// sPendingWriteData can be overwritten multiple times before anyone
// gets around to actually using it, minimizing writes. However,
// once we've acquired sPendingWriteData we've reached a
// "point of no return" and have to complete the write.
//
// Unfortunately, this design allows the following behaviour:
//
// 1. write1 is queued up
// 2. thread1 acquires write1
// 3. write2 is queued up
// 4. thread2 acquires write2
// 5. thread1 and thread2 concurrently clobber each other
//
// To avoid this, we use this lock to ensure that only one thread
// at a time is trying to acquire the write, and when it does,
// all other threads are prevented from acquiring writes until it
// completes the write. New writes are still allowed to be queued
// up in this time.
//
// Although it's atomic, the acquire needs to be guarded by the mutex
// to avoid reordering of writes -- we don't want an older write to
// run after a newer one. To avoid this causing too much waiting, we check
// if sPendingWriteData is already null before acquiring the mutex. If it
// is, then there's definitely no work to be done (or someone is in the
// middle of doing it for us).
//
// Note that every time a new write is queued up, a new write task is
// is also queued up, so there will always be a task that can see the newest
// write.
//
// Ideally this lock wouldn't be necessary, and the PreferencesWriter
// would be used more carefully, but it's hard to untangle all that.
nsresult rv = NS_OK;
if (PreferencesWriter::sPendingWriteData) {
StaticMutexAutoLock lock(PreferencesWriter::sWritingToFile);
// If we get a nullptr on the exchange, it means that somebody
// else has already processed the request, and we can just return.
UniquePtr<PrefSaveData> prefs(
PreferencesWriter::sPendingWriteData.exchange(nullptr));
if (prefs) {
rv = PreferencesWriter::Write(mFile, *prefs);
// 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);
SchedulerGroup::Dispatch(NS_NewRunnableFunction(
"Preferences::WriterRunnable",
[fileCopy, rvCopy, promiseHolder = std::move(mPromiseHolder)] {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (NS_FAILED(rvCopy)) {
Preferences::HandleDirty();
}
if (promiseHolder) {
promiseHolder->ResolveIfExists(true, __func__);
}
}));
}
}
// We've completed the write to the best of our abilities, whether
// we had prefs to write or another runnable got to them first. If
// PreferencesWriter::Write failed, this is still correct as the
// write is no longer outstanding, and the above HandleDirty call
// will just start the cycle again.
PreferencesWriter::sPendingWriteCount--;
return rv;
}
private:
~PWRunnable() {
if (mPromiseHolder) {
mPromiseHolder->RejectIfExists(NS_ERROR_ABORT, __func__);
}
}
protected:
nsCOMPtr<nsIFile> mFile;
UniquePtr<MozPromiseHolder<Preferences::WritePrefFilePromise>> mPromiseHolder;
};
// 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;
}
aSizes.mMisc += aMallocSizeOf(sPreferences.get());
aSizes.mRootBranches +=
static_cast<nsPrefBranch*>(sPreferences->mRootBranch.get())
->SizeOfIncludingThis(aMallocSizeOf) +
static_cast<nsPrefBranch*>(sPreferences->mDefaultRootBranch.get())
->SizeOfIncludingThis(aMallocSizeOf);
}
class PreferenceServiceReporter final : public nsIMemoryReporter {
~PreferenceServiceReporter() = default;
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIMEMORYREPORTER
protected:
static const uint32_t kSuspectReferentCount = 1000;
};
NS_IMPL_ISUPPORTS(PreferenceServiceReporter, nsIMemoryReporter)
MOZ_DEFINE_MALLOC_SIZE_OF(PreferenceServiceMallocSizeOf)
NS_IMETHODIMP
PreferenceServiceReporter::CollectReports(
nsIHandleReportCallback* aHandleReport, nsISupports* aData,
bool aAnonymize) {
MOZ_ASSERT(NS_IsMainThread());
MallocSizeOf mallocSizeOf = PreferenceServiceMallocSizeOf;
PrefsSizes sizes;
Preferences::AddSizeOfIncludingThis(mallocSizeOf, sizes);
if (HashTable()) {
sizes.mHashTable += HashTable()->shallowSizeOfIncludingThis(mallocSizeOf);
for (auto iter = HashTable()->iter(); !iter.done(); iter.next()) {
iter.get()->AddSizeOfIncludingThis(mallocSizeOf, sizes);
}
}
sizes.mPrefNameArena += PrefNameArena().SizeOfExcludingThis(mallocSizeOf);
for (CallbackNode* node = gFirstCallback; node; node = node->Next()) {
node->AddSizeOfIncludingThis(mallocSizeOf, sizes);
}
if (gSharedMap) {
sizes.mMisc += mallocSizeOf(gSharedMap);
}
#ifdef ACCESS_COUNTS
if (gAccessCounts) {
sizes.mMisc += gAccessCounts->ShallowSizeOfIncludingThis(mallocSizeOf);
}
#endif
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/pref-values", KIND_HEAP, UNITS_BYTES,
sizes.mPrefValues,
"Memory used by PrefValues hanging off the 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/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/objects", KIND_HEAP,
UNITS_BYTES, sizes.mCallbacksObjects,
"Memory used by pref callback objects.");
MOZ_COLLECT_REPORT("explicit/preferences/callbacks/domains", KIND_HEAP,
UNITS_BYTES, sizes.mCallbacksDomains,
"Memory used by pref callback domains (pref names and "
"prefixes).");
MOZ_COLLECT_REPORT("explicit/preferences/misc", KIND_HEAP, UNITS_BYTES,
sizes.mMisc, "Miscellaneous memory used by libpref.");
if (gSharedMap) {
if (XRE_IsParentProcess()) {
MOZ_COLLECT_REPORT("explicit/preferences/shared-memory-map", KIND_NONHEAP,
UNITS_BYTES, gSharedMap->MapSize(),
"The shared memory mapping used to share a "
"snapshot of preference values across processes.");
}
}
nsPrefBranch* rootBranch =
static_cast<nsPrefBranch*>(Preferences::GetRootBranch());
if (!rootBranch) {
return NS_OK;
}
size_t numStrong = 0;
size_t numWeakAlive = 0;
size_t numWeakDead = 0;
nsTArray<nsCString> suspectPreferences;
// Count of the number of referents for each preference.
nsTHashMap<nsCStringHashKey, uint32_t> prefCounter;
for (const auto& entry : rootBranch->mObservers) {
auto* callback = entry.GetWeak();
if (callback->IsWeak()) {
nsCOMPtr<nsIObserver> callbackRef = do_QueryReferent(callback->mWeakRef);
if (callbackRef) {
numWeakAlive++;
} else {
numWeakDead++;
}
} else {
numStrong++;
}
const uint32_t currentCount = prefCounter.Get(callback->GetDomain()) + 1;
prefCounter.InsertOrUpdate(callback->GetDomain(), currentCount);
// Keep track of preferences that have a suspiciously large number of
// referents (a symptom of a leak).
if (currentCount == kSuspectReferentCount) {
suspectPreferences.AppendElement(callback->GetDomain());
}
}
for (uint32_t i = 0; i < suspectPreferences.Length(); i++) {
nsCString& suspect = suspectPreferences[i];
const uint32_t totalReferentCount = prefCounter.Get(suspect);
nsPrintfCString suspectPath(
"preference-service-suspect/"
"referent(pref=%s)",
suspect.get());
aHandleReport->Callback(
/* process = */ ""_ns, suspectPath, KIND_OTHER, UNITS_COUNT,
totalReferentCount,
"A preference with a suspiciously large number "
"referents (symptom of a leak)."_ns,
aData);
}
MOZ_COLLECT_REPORT(
"preference-service/referent/strong", KIND_OTHER, UNITS_COUNT, numStrong,
"The number of strong referents held by the preference service.");
MOZ_COLLECT_REPORT(
"preference-service/referent/weak/alive", KIND_OTHER, UNITS_COUNT,
numWeakAlive,
"The number of weak referents held by the preference service that are "
"still alive.");
MOZ_COLLECT_REPORT(
"preference-service/referent/weak/dead", KIND_OTHER, UNITS_COUNT,
numWeakDead,
"The number of weak referents held by the preference service that are "
"dead.");
return NS_OK;
}
namespace {
class AddPreferencesMemoryReporterRunnable : public Runnable {
public:
AddPreferencesMemoryReporterRunnable()
: Runnable("AddPreferencesMemoryReporterRunnable") {}
NS_IMETHOD Run() override {
return RegisterStrongMemoryReporter(new PreferenceServiceReporter());
}
};
} // namespace
// A list of changed prefs sent from the parent via shared memory.
static StaticAutoPtr<nsTArray<dom::Pref>> gChangedDomPrefs;
static const char kTelemetryPref[] = "toolkit.telemetry.enabled";
#if !(defined(MOZ_WIDGET_ANDROID) && defined(MOZ_TELEMETRY_ON_BY_DEFAULT))
static const char kChannelPref[] = "app.update.channel";
#endif
#ifdef MOZ_WIDGET_ANDROID
static Maybe<bool> TelemetryPrefValue() {
// Leave it unchanged if it's already set.
// XXX: how could it already be set?
if (Preferences::GetType(kTelemetryPref) != nsIPrefBranch::PREF_INVALID) {
return Nothing();
}
// Determine 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.
# ifdef MOZ_TELEMETRY_ON_BY_DEFAULT
return Some(true);
# else
nsAutoCString channelPrefValue;
(void)Preferences::GetCString(kChannelPref, channelPrefValue,
PrefValueKind::Default);
return Some(channelPrefValue.EqualsLiteral("beta"));
# endif
}
/* static */
void Preferences::SetupTelemetryPref() {
MOZ_ASSERT(XRE_IsParentProcess());
Maybe<bool> telemetryPrefValue = TelemetryPrefValue();
if (telemetryPrefValue.isSome()) {
Preferences::SetBool(kTelemetryPref, *telemetryPrefValue,
PrefValueKind::Default);
}
}
#else // !MOZ_WIDGET_ANDROID
static bool TelemetryPrefValue() {
// For platforms with Unified Telemetry (here meaning not-Android),
// toolkit.telemetry.enabled determines whether we send "extended" data.
// We only want extended data from pre-release channels due to size.
constexpr auto channel = MOZ_STRINGIFY(MOZ_UPDATE_CHANNEL) ""_ns;
// Easy cases: Nightly, Aurora, Beta.
if (channel.EqualsLiteral("nightly") || channel.EqualsLiteral("aurora") ||
channel.EqualsLiteral("beta")) {
return true;
}
# ifndef MOZILLA_OFFICIAL
// Local developer builds: non-official builds on the "default" channel.
if (channel.EqualsLiteral("default")) {
return true;
}
# endif
// Release Candidate builds: builds that think they are release builds, but
// are shipped to beta users.
if (channel.EqualsLiteral("release")) {
nsAutoCString channelPrefValue;
(void)Preferences::GetCString(kChannelPref, channelPrefValue,
PrefValueKind::Default);
if (channelPrefValue.EqualsLiteral("beta")) {
return true;
}
}
return false;
}
/* static */
void Preferences::SetupTelemetryPref() {
MOZ_ASSERT(XRE_IsParentProcess());
Preferences::SetBool(kTelemetryPref, TelemetryPrefValue(),
PrefValueKind::Default);
Preferences::Lock(kTelemetryPref);
}
#endif // MOZ_WIDGET_ANDROID
/* static */
already_AddRefed<Preferences> Preferences::GetInstanceForService() {
if (sPreferences) {
return do_AddRef(sPreferences);
}
if (sShutdown) {
return nullptr;
}
sPreferences = new Preferences();
MOZ_ASSERT(!HashTable());
HashTable() = new PrefsHashTable(XRE_IsParentProcess()
? kHashTableInitialLengthParent
: kHashTableInitialLengthContent);
#ifdef DEBUG
gOnceStaticPrefsAntiFootgun = new AntiFootgunMap();
#endif
#ifdef ACCESS_COUNTS
MOZ_ASSERT(!gAccessCounts);
gAccessCounts = new AccessCountsHashTable();
#endif
nsresult rv = InitInitialObjects(/* isStartup */ true);
if (NS_FAILED(rv)) {
sPreferences = nullptr;
return nullptr;
}
if (!XRE_IsParentProcess()) {
MOZ_ASSERT(gChangedDomPrefs);
for (unsigned int i = 0; i < gChangedDomPrefs->Length(); i++) {
Preferences::SetPreference(gChangedDomPrefs->ElementAt(i));
}
gChangedDomPrefs = 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;
nsresult rv = Preferences::GetCString("general.config.filename",
lockFileName, PrefValueKind::User);
if (NS_SUCCEEDED(rv)) {
NS_CreateServicesFromCategory(
"pref-config-startup",
static_cast<nsISupports*>(static_cast<void*>(sPreferences)),
"pref-config-startup");
}
nsCOMPtr<nsIObserverService> observerService =
services::GetObserverService();
if (!observerService) {
sPreferences = nullptr;
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;
return nullptr;
}
}
const char* defaultPrefs = getenv("MOZ_DEFAULT_PREFS");
if (defaultPrefs) {
parsePrefData(nsCString(defaultPrefs), PrefValueKind::Default);
}
// Preferences::GetInstanceForService() can be called from GetService(), and
// 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.
RefPtr<AddPreferencesMemoryReporterRunnable> runnable =
new AddPreferencesMemoryReporterRunnable();
NS_DispatchToMainThread(runnable);
return do_AddRef(sPreferences);
}
/* static */
bool Preferences::IsServiceAvailable() { return !!sPreferences; }
/* static */
bool Preferences::InitStaticMembers() {
MOZ_ASSERT(NS_IsMainThread() || ServoStyleSet::IsInServoTraversal());
if (MOZ_LIKELY(sPreferences)) {
return true;
}
if (!sShutdown) {
MOZ_ASSERT(NS_IsMainThread());
nsCOMPtr<nsIPrefService> prefService =
do_GetService(NS_PREFSERVICE_CONTRACTID);
}
return sPreferences != nullptr;
}
/* static */
void Preferences::Shutdown() {
if (!sShutdown) {
sShutdown = true; // Don't create the singleton instance after here.
sPreferences = nullptr;
StaticPrefs::ShutdownAlwaysPrefs();
}
}
Preferences::Preferences()
: mRootBranch(new nsPrefBranch("", PrefValueKind::User)),
mDefaultRootBranch(new nsPrefBranch("", PrefValueKind::Default)) {}
Preferences::~Preferences() {
MOZ_ASSERT(!sPreferences);
MOZ_ASSERT(!gCallbacksInProgress);
CallbackNode* node = gFirstCallback;
while (node) {
CallbackNode* next_node = node->Next();
delete node;
node = next_node;
}
gLastPriorityNode = gFirstCallback = nullptr;
delete HashTable();
HashTable() = nullptr;
#ifdef DEBUG
gOnceStaticPrefsAntiFootgun = nullptr;
#endif
#ifdef ACCESS_COUNTS
gAccessCounts = nullptr;
#endif
gSharedMap = nullptr;
PrefNameArena().Clear();
}
NS_IMPL_ISUPPORTS(Preferences, nsIPrefService, nsIObserver, nsIPrefBranch,
nsISupportsWeakReference)
/* static */
void Preferences::SerializePreferences(nsCString& aStr,
bool aIsDestinationWebContentProcess) {
MOZ_RELEASE_ASSERT(InitStaticMembers());
aStr.Truncate();
for (auto iter = HashTable()->iter(); !iter.done(); iter.next()) {
Pref* pref = iter.get().get();
if (!pref->IsTypeNone() && pref->HasAdvisablySizedValues()) {
pref->SerializeAndAppend(aStr, aIsDestinationWebContentProcess &&
ShouldSanitizePreference(pref));
}
}
aStr.Append('\0');
}
/* static */
void Preferences::DeserializePreferences(const char* aStr, size_t aPrefsLen) {
MOZ_ASSERT(!XRE_IsParentProcess());
MOZ_ASSERT(!gChangedDomPrefs);
gChangedDomPrefs = new nsTArray<dom::Pref>();
const char* p = aStr;
while (*p != '\0') {
dom::Pref pref;
p = Pref::Deserialize(p, &pref);
gChangedDomPrefs->AppendElement(pref);
}
// We finished parsing on a '\0'. That should be the last char in the shared
// memory. (aPrefsLen includes the '\0'.)