Bug 1653231 - Pass const nsCString& rather than const char* where possible. r=njn

Differential Revision: https://phabricator.services.mozilla.com/D83918
This commit is contained in:
Simon Giesecke 2020-07-20 11:43:33 +00:00
parent 4af01d0e97
commit b7f75cf860

View File

@ -436,7 +436,7 @@ class PrefWrapper;
class Pref { class Pref {
public: public:
explicit Pref(const char* aName) explicit Pref(const nsACString& aName)
: mName(ArenaStrdup(aName, PrefNameArena())), : mName(ArenaStrdup(aName, PrefNameArena())),
mType(static_cast<uint32_t>(PrefType::None)), mType(static_cast<uint32_t>(PrefType::None)),
mIsSticky(false), mIsSticky(false),
@ -1441,10 +1441,11 @@ class PrefsIter {
static Pref* pref_HashTableLookup(const char* aPrefName); static Pref* pref_HashTableLookup(const char* aPrefName);
static void NotifyCallbacks(const char* aPrefName, static void NotifyCallbacks(const nsCString& aPrefName,
const PrefWrapper* aPref = nullptr); const PrefWrapper* aPref = nullptr);
static void NotifyCallbacks(const char* aPrefName, const PrefWrapper& aPref) { static void NotifyCallbacks(const nsCString& aPrefName,
const PrefWrapper& aPref) {
NotifyCallbacks(aPrefName, &aPref); NotifyCallbacks(aPrefName, &aPref);
} }
@ -1552,10 +1553,10 @@ Maybe<PrefWrapper> pref_Lookup(const char* aPrefName,
} }
static Result<Pref*, nsresult> pref_LookupForModify( static Result<Pref*, nsresult> pref_LookupForModify(
const char* aPrefName, const nsCString& aPrefName,
const std::function<bool(const PrefWrapper&)>& aCheckFn) { const std::function<bool(const PrefWrapper&)>& aCheckFn) {
Maybe<PrefWrapper> wrapper = Maybe<PrefWrapper> wrapper =
pref_Lookup(aPrefName, /* includeTypeNone */ true); pref_Lookup(aPrefName.get(), /* includeTypeNone */ true);
if (wrapper.isNothing()) { if (wrapper.isNothing()) {
return Err(NS_ERROR_INVALID_ARG); return Err(NS_ERROR_INVALID_ARG);
} }
@ -1567,7 +1568,7 @@ static Result<Pref*, nsresult> pref_LookupForModify(
} }
Pref* pref = new Pref(aPrefName); Pref* pref = new Pref(aPrefName);
if (!HashTable()->putNew(aPrefName, pref)) { if (!HashTable()->putNew(aPrefName.get(), pref)) {
delete pref; delete pref;
return Err(NS_ERROR_OUT_OF_MEMORY); return Err(NS_ERROR_OUT_OF_MEMORY);
} }
@ -1575,7 +1576,7 @@ static Result<Pref*, nsresult> pref_LookupForModify(
return pref; return pref;
} }
static nsresult pref_SetPref(const char* aPrefName, PrefType aType, static nsresult pref_SetPref(const nsCString& aPrefName, PrefType aType,
PrefValueKind aKind, PrefValue aValue, PrefValueKind aKind, PrefValue aValue,
bool aIsSticky, bool aIsLocked, bool aFromInit) { bool aIsSticky, bool aIsLocked, bool aFromInit) {
MOZ_ASSERT(XRE_IsParentProcess()); MOZ_ASSERT(XRE_IsParentProcess());
@ -1598,7 +1599,7 @@ static nsresult pref_SetPref(const char* aPrefName, PrefType aType,
} }
if (!pref) { if (!pref) {
auto p = HashTable()->lookupForAdd(aPrefName); auto p = HashTable()->lookupForAdd(aPrefName.get());
if (!p) { if (!p) {
pref = new Pref(aPrefName); pref = new Pref(aPrefName);
pref->SetType(aType); pref->SetType(aType);
@ -1624,7 +1625,7 @@ static nsresult pref_SetPref(const char* aPrefName, PrefType aType,
NS_WARNING( NS_WARNING(
nsPrintfCString("Rejected attempt to change type of pref %s's %s value " nsPrintfCString("Rejected attempt to change type of pref %s's %s value "
"from %s to %s", "from %s to %s",
aPrefName, aPrefName.get(),
(aKind == PrefValueKind::Default) ? "default" : "user", (aKind == PrefValueKind::Default) ? "default" : "user",
PrefTypeToString(pref->Type()), PrefTypeToString(aType)) PrefTypeToString(pref->Type()), PrefTypeToString(aType))
.get()); .get());
@ -1662,7 +1663,8 @@ static CallbackNode* pref_RemoveCallbackNode(CallbackNode* aNode,
return next_node; return next_node;
} }
static void NotifyCallbacks(const char* aPrefName, const PrefWrapper* aPref) { static void NotifyCallbacks(const nsCString& aPrefName,
const PrefWrapper* aPref) {
bool reentered = gCallbacksInProgress; bool reentered = gCallbacksInProgress;
gCallbackPref = aPref; gCallbackPref = aPref;
@ -1674,12 +1676,10 @@ static void NotifyCallbacks(const char* aPrefName, const PrefWrapper* aPref) {
// if we haven't reentered. // if we haven't reentered.
gCallbacksInProgress = true; gCallbacksInProgress = true;
nsDependentCString prefName(aPrefName);
for (CallbackNode* node = gFirstCallback; node; node = node->Next()) { for (CallbackNode* node = gFirstCallback; node; node = node->Next()) {
if (node->Func()) { if (node->Func()) {
if (node->Matches(prefName)) { if (node->Matches(aPrefName)) {
(node->Func())(aPrefName, node->Data()); (node->Func())(aPrefName.get(), node->Data());
} }
} }
} }
@ -1709,7 +1709,7 @@ static void NotifyCallbacks(const char* aPrefName, const PrefWrapper* aPref) {
// name. We have about 100 `once`-mirrored prefs. std::map performs a // name. We have about 100 `once`-mirrored prefs. std::map performs a
// search in O(log n), so this is fast enough. // search in O(log n), so this is fast enough.
MOZ_ASSERT(gOnceStaticPrefsAntiFootgun); MOZ_ASSERT(gOnceStaticPrefsAntiFootgun);
auto search = gOnceStaticPrefsAntiFootgun->find(aPrefName); auto search = gOnceStaticPrefsAntiFootgun->find(aPrefName.get());
if (search != gOnceStaticPrefsAntiFootgun->end()) { if (search != gOnceStaticPrefsAntiFootgun->end()) {
// Run the callback. // Run the callback.
(search->second)(); (search->second)();
@ -1781,7 +1781,8 @@ class Parser {
bool aIsLocked) { bool aIsLocked) {
MOZ_ASSERT(XRE_IsParentProcess()); MOZ_ASSERT(XRE_IsParentProcess());
sNumPrefs++; sNumPrefs++;
pref_SetPref(aPrefName, aType, aKind, aValue, aIsSticky, aIsLocked, pref_SetPref(nsDependentCString(aPrefName), aType, aKind, aValue, aIsSticky,
aIsLocked,
/* fromInit */ true); /* fromInit */ true);
} }
@ -3755,7 +3756,7 @@ Preferences::ResetUserPrefs() {
} }
for (const char* prefName : prefNames) { for (const char* prefName : prefNames) {
NotifyCallbacks(prefName); NotifyCallbacks(nsDependentCString(prefName));
} }
Preferences::HandleDirty(); Preferences::HandleDirty();
@ -3807,10 +3808,10 @@ void Preferences::SetPreference(const dom::Pref& aDomPref) {
MOZ_ASSERT(!XRE_IsParentProcess()); MOZ_ASSERT(!XRE_IsParentProcess());
NS_ENSURE_TRUE(InitStaticMembers(), (void)0); NS_ENSURE_TRUE(InitStaticMembers(), (void)0);
const char* prefName = aDomPref.name().get(); const nsCString& prefName = aDomPref.name();
Pref* pref; Pref* pref;
auto p = HashTable()->lookupForAdd(prefName); auto p = HashTable()->lookupForAdd(prefName.get());
if (!p) { if (!p) {
pref = new Pref(prefName); pref = new Pref(prefName);
if (!HashTable()->add(p, pref)) { if (!HashTable()->add(p, pref)) {
@ -3840,7 +3841,7 @@ void Preferences::SetPreference(const dom::Pref& aDomPref) {
if (gSharedMap->Has(pref->Name())) { if (gSharedMap->Has(pref->Name())) {
pref->SetType(PrefType::None); pref->SetType(PrefType::None);
} else { } else {
HashTable()->remove(prefName); HashTable()->remove(prefName.get());
} }
pref = nullptr; pref = nullptr;
} }
@ -4739,7 +4740,8 @@ nsresult Preferences::SetCString(const char* aPrefName,
PrefValue prefValue; PrefValue prefValue;
const nsCString& flat = PromiseFlatCString(aValue); const nsCString& flat = PromiseFlatCString(aValue);
prefValue.mStringVal = flat.get(); prefValue.mStringVal = flat.get();
return pref_SetPref(aPrefName, PrefType::String, aKind, prefValue, return pref_SetPref(nsDependentCString(aPrefName), PrefType::String, aKind,
prefValue,
/* isSticky */ false, /* isSticky */ false,
/* isLocked */ false, /* isLocked */ false,
/* fromInit */ false); /* fromInit */ false);
@ -4753,7 +4755,8 @@ nsresult Preferences::SetBool(const char* aPrefName, bool aValue,
PrefValue prefValue; PrefValue prefValue;
prefValue.mBoolVal = aValue; prefValue.mBoolVal = aValue;
return pref_SetPref(aPrefName, PrefType::Bool, aKind, prefValue, return pref_SetPref(nsDependentCString(aPrefName), PrefType::Bool, aKind,
prefValue,
/* isSticky */ false, /* isSticky */ false,
/* isLocked */ false, /* isLocked */ false,
/* fromInit */ false); /* fromInit */ false);
@ -4767,7 +4770,8 @@ nsresult Preferences::SetInt(const char* aPrefName, int32_t aValue,
PrefValue prefValue; PrefValue prefValue;
prefValue.mIntVal = aValue; prefValue.mIntVal = aValue;
return pref_SetPref(aPrefName, PrefType::Int, aKind, prefValue, return pref_SetPref(nsDependentCString(aPrefName), PrefType::Int, aKind,
prefValue,
/* isSticky */ false, /* isSticky */ false,
/* isLocked */ false, /* isLocked */ false,
/* fromInit */ false); /* fromInit */ false);
@ -4785,15 +4789,17 @@ nsresult Preferences::Lock(const char* aPrefName) {
ENSURE_PARENT_PROCESS("Lock", aPrefName); ENSURE_PARENT_PROCESS("Lock", aPrefName);
NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE); NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
const auto& prefName = nsDependentCString(aPrefName);
Pref* pref; Pref* pref;
MOZ_TRY_VAR(pref, MOZ_TRY_VAR(pref,
pref_LookupForModify(aPrefName, [](const PrefWrapper& aPref) { pref_LookupForModify(prefName, [](const PrefWrapper& aPref) {
return !aPref.IsLocked(); return !aPref.IsLocked();
})); }));
if (pref) { if (pref) {
pref->SetIsLocked(true); pref->SetIsLocked(true);
NotifyCallbacks(aPrefName, PrefWrapper(pref)); NotifyCallbacks(prefName, PrefWrapper(pref));
} }
return NS_OK; return NS_OK;
@ -4804,15 +4810,17 @@ nsresult Preferences::Unlock(const char* aPrefName) {
ENSURE_PARENT_PROCESS("Unlock", aPrefName); ENSURE_PARENT_PROCESS("Unlock", aPrefName);
NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE); NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
const auto& prefName = nsDependentCString(aPrefName);
Pref* pref; Pref* pref;
MOZ_TRY_VAR(pref, MOZ_TRY_VAR(pref,
pref_LookupForModify(aPrefName, [](const PrefWrapper& aPref) { pref_LookupForModify(prefName, [](const PrefWrapper& aPref) {
return aPref.IsLocked(); return aPref.IsLocked();
})); }));
if (pref) { if (pref) {
pref->SetIsLocked(false); pref->SetIsLocked(false);
NotifyCallbacks(aPrefName, PrefWrapper(pref)); NotifyCallbacks(prefName, PrefWrapper(pref));
} }
return NS_OK; return NS_OK;
@ -4831,8 +4839,9 @@ nsresult Preferences::ClearUser(const char* aPrefName) {
ENSURE_PARENT_PROCESS("ClearUser", aPrefName); ENSURE_PARENT_PROCESS("ClearUser", aPrefName);
NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE); NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
const auto& prefName = nsDependentCString{aPrefName};
auto result = pref_LookupForModify( auto result = pref_LookupForModify(
aPrefName, [](const PrefWrapper& aPref) { return aPref.HasUserValue(); }); prefName, [](const PrefWrapper& aPref) { return aPref.HasUserValue(); });
if (result.isErr()) { if (result.isErr()) {
return NS_OK; return NS_OK;
} }
@ -4847,9 +4856,9 @@ nsresult Preferences::ClearUser(const char* aPrefName) {
pref->SetType(PrefType::None); pref->SetType(PrefType::None);
} }
NotifyCallbacks(aPrefName); NotifyCallbacks(prefName);
} else { } else {
NotifyCallbacks(aPrefName, PrefWrapper(pref)); NotifyCallbacks(prefName, PrefWrapper(pref));
} }
Preferences::HandleDirty(); Preferences::HandleDirty();
@ -5204,7 +5213,7 @@ void Preferences::AddAtomicFloatVarCache(std::atomic<float>* aCache,
// The InitPref_*() functions below end in a `_<type>` suffix because they are // The InitPref_*() functions below end in a `_<type>` suffix because they are
// used by the PREF macro definition in InitAll() below. // used by the PREF macro definition in InitAll() below.
static void InitPref_bool(const char* aName, bool aDefaultValue) { static void InitPref_bool(const nsCString& aName, bool aDefaultValue) {
MOZ_ASSERT(XRE_IsParentProcess()); MOZ_ASSERT(XRE_IsParentProcess());
PrefValue value; PrefValue value;
value.mBoolVal = aDefaultValue; value.mBoolVal = aDefaultValue;
@ -5214,7 +5223,7 @@ static void InitPref_bool(const char* aName, bool aDefaultValue) {
/* fromInit */ true); /* fromInit */ true);
} }
static void InitPref_int32_t(const char* aName, int32_t aDefaultValue) { static void InitPref_int32_t(const nsCString& aName, int32_t aDefaultValue) {
MOZ_ASSERT(XRE_IsParentProcess()); MOZ_ASSERT(XRE_IsParentProcess());
PrefValue value; PrefValue value;
value.mIntVal = aDefaultValue; value.mIntVal = aDefaultValue;
@ -5224,11 +5233,11 @@ static void InitPref_int32_t(const char* aName, int32_t aDefaultValue) {
/* fromInit */ true); /* fromInit */ true);
} }
static void InitPref_uint32_t(const char* aName, uint32_t aDefaultValue) { static void InitPref_uint32_t(const nsCString& aName, uint32_t aDefaultValue) {
InitPref_int32_t(aName, int32_t(aDefaultValue)); InitPref_int32_t(aName, int32_t(aDefaultValue));
} }
static void InitPref_float(const char* aName, float aDefaultValue) { static void InitPref_float(const nsCString& aName, float aDefaultValue) {
MOZ_ASSERT(XRE_IsParentProcess()); MOZ_ASSERT(XRE_IsParentProcess());
PrefValue value; PrefValue value;
// Convert the value in a locale-independent way, including a trailing ".0" // Convert the value in a locale-independent way, including a trailing ".0"
@ -5246,7 +5255,7 @@ static void InitPref_float(const char* aName, float aDefaultValue) {
/* fromInit */ true); /* fromInit */ true);
} }
static void InitPref_String(const char* aName, const char* aDefaultValue) { static void InitPref_String(const nsCString& aName, const char* aDefaultValue) {
MOZ_ASSERT(XRE_IsParentProcess()); MOZ_ASSERT(XRE_IsParentProcess());
PrefValue value; PrefValue value;
value.mStringVal = aDefaultValue; value.mStringVal = aDefaultValue;
@ -5256,16 +5265,16 @@ static void InitPref_String(const char* aName, const char* aDefaultValue) {
/* fromInit */ true); /* fromInit */ true);
} }
static void InitPref(const char* aName, bool aDefaultValue) { static void InitPref(const nsCString& aName, bool aDefaultValue) {
InitPref_bool(aName, aDefaultValue); InitPref_bool(aName, aDefaultValue);
} }
static void InitPref(const char* aName, int32_t aDefaultValue) { static void InitPref(const nsCString& aName, int32_t aDefaultValue) {
InitPref_int32_t(aName, aDefaultValue); InitPref_int32_t(aName, aDefaultValue);
} }
static void InitPref(const char* aName, uint32_t aDefaultValue) { static void InitPref(const nsCString& aName, uint32_t aDefaultValue) {
InitPref_uint32_t(aName, aDefaultValue); InitPref_uint32_t(aName, aDefaultValue);
} }
static void InitPref(const char* aName, float aDefaultValue) { static void InitPref(const nsCString& aName, float aDefaultValue) {
InitPref_float(aName, aDefaultValue); InitPref_float(aName, aDefaultValue);
} }
@ -5275,7 +5284,7 @@ static void InitAlwaysPref(const nsCString& aName, T* aCache,
// Only called in the parent process. Set/reset the pref value and the // Only called in the parent process. Set/reset the pref value and the
// `always` mirror to the default value. // `always` mirror to the default value.
// `once` mirrors will be initialized lazily in InitOncePrefs(). // `once` mirrors will be initialized lazily in InitOncePrefs().
InitPref(aName.get(), aDefaultValue); InitPref(aName, aDefaultValue);
*aCache = aDefaultValue; *aCache = aDefaultValue;
} }
@ -5323,11 +5332,12 @@ static void InitAll() {
// prefs having int32_t and float default values. That suffix is not needed // prefs having int32_t and float default values. That suffix is not needed
// for the InitAlwaysPref() functions because they take a pointer parameter, // for the InitAlwaysPref() functions because they take a pointer parameter,
// which prevents automatic int-to-float coercion. // which prevents automatic int-to-float coercion.
#define NEVER_PREF(name, cpp_type, value) InitPref_##cpp_type(name, value); #define NEVER_PREF(name, cpp_type, value) \
InitPref_##cpp_type(name ""_ns, value);
#define ALWAYS_PREF(name, base_id, full_id, cpp_type, value) \ #define ALWAYS_PREF(name, base_id, full_id, cpp_type, value) \
InitAlwaysPref(name ""_ns, &sMirror_##full_id, value); InitAlwaysPref(name ""_ns, &sMirror_##full_id, value);
#define ONCE_PREF(name, base_id, full_id, cpp_type, value) \ #define ONCE_PREF(name, base_id, full_id, cpp_type, value) \
InitPref_##cpp_type(name, value); InitPref_##cpp_type(name ""_ns, value);
#include "mozilla/StaticPrefListAll.h" #include "mozilla/StaticPrefListAll.h"
#undef NEVER_PREF #undef NEVER_PREF
#undef ALWAYS_PREF #undef ALWAYS_PREF
@ -5397,7 +5407,7 @@ static void InitOncePrefs() {
} // namespace StaticPrefs } // namespace StaticPrefs
static MOZ_MAYBE_UNUSED void SaveOncePrefToSharedMap( static MOZ_MAYBE_UNUSED void SaveOncePrefToSharedMap(
SharedPrefMapBuilder& aBuilder, const char* aName, bool aValue) { SharedPrefMapBuilder& aBuilder, const nsACString& aName, bool aValue) {
auto oncePref = MakeUnique<Pref>(aName); auto oncePref = MakeUnique<Pref>(aName);
oncePref->SetType(PrefType::Bool); oncePref->SetType(PrefType::Bool);
oncePref->SetIsSkippedByIteration(true); oncePref->SetIsSkippedByIteration(true);
@ -5410,7 +5420,7 @@ static MOZ_MAYBE_UNUSED void SaveOncePrefToSharedMap(
} }
static MOZ_MAYBE_UNUSED void SaveOncePrefToSharedMap( static MOZ_MAYBE_UNUSED void SaveOncePrefToSharedMap(
SharedPrefMapBuilder& aBuilder, const char* aName, int32_t aValue) { SharedPrefMapBuilder& aBuilder, const nsACString& aName, int32_t aValue) {
auto oncePref = MakeUnique<Pref>(aName); auto oncePref = MakeUnique<Pref>(aName);
oncePref->SetType(PrefType::Int); oncePref->SetType(PrefType::Int);
oncePref->SetIsSkippedByIteration(true); oncePref->SetIsSkippedByIteration(true);
@ -5423,12 +5433,12 @@ static MOZ_MAYBE_UNUSED void SaveOncePrefToSharedMap(
} }
static MOZ_MAYBE_UNUSED void SaveOncePrefToSharedMap( static MOZ_MAYBE_UNUSED void SaveOncePrefToSharedMap(
SharedPrefMapBuilder& aBuilder, const char* aName, uint32_t aValue) { SharedPrefMapBuilder& aBuilder, const nsACString& aName, uint32_t aValue) {
SaveOncePrefToSharedMap(aBuilder, aName, int32_t(aValue)); SaveOncePrefToSharedMap(aBuilder, aName, int32_t(aValue));
} }
static MOZ_MAYBE_UNUSED void SaveOncePrefToSharedMap( static MOZ_MAYBE_UNUSED void SaveOncePrefToSharedMap(
SharedPrefMapBuilder& aBuilder, const char* aName, float aValue) { SharedPrefMapBuilder& aBuilder, const nsACString& aName, float aValue) {
auto oncePref = MakeUnique<Pref>(aName); auto oncePref = MakeUnique<Pref>(aName);
oncePref->SetType(PrefType::String); oncePref->SetType(PrefType::String);
oncePref->SetIsSkippedByIteration(true); oncePref->SetIsSkippedByIteration(true);
@ -5445,7 +5455,7 @@ static MOZ_MAYBE_UNUSED void SaveOncePrefToSharedMap(
oncePref->AddToMap(aBuilder); oncePref->AddToMap(aBuilder);
} }
#define ONCE_PREF_NAME(name) ("$$$" name "$$$") #define ONCE_PREF_NAME(name) "$$$" name "$$$"
namespace StaticPrefs { namespace StaticPrefs {
@ -5463,8 +5473,8 @@ static void RegisterOncePrefs(SharedPrefMapBuilder& aBuilder) {
// "$$$" prefix and suffix to the preference name. // "$$$" prefix and suffix to the preference name.
#define NEVER_PREF(name, cpp_type, value) #define NEVER_PREF(name, cpp_type, value)
#define ALWAYS_PREF(name, base_id, full_id, cpp_type, value) #define ALWAYS_PREF(name, base_id, full_id, cpp_type, value)
#define ONCE_PREF(name, base_id, full_id, cpp_type, value) \ #define ONCE_PREF(name, base_id, full_id, cpp_type, value) \
SaveOncePrefToSharedMap(aBuilder, ONCE_PREF_NAME(name), \ SaveOncePrefToSharedMap(aBuilder, ONCE_PREF_NAME(name) ""_ns, \
cpp_type(sMirror_##full_id)); cpp_type(sMirror_##full_id));
#include "mozilla/StaticPrefListAll.h" #include "mozilla/StaticPrefListAll.h"
#undef NEVER_PREF #undef NEVER_PREF