Bug 1435943 - Tweak Preferences::GetType() and nsPrefBranch::GetPrefType(). r=glandium
This patch rearranges these functions so that nsPrefBranch::GetPrefType() calls
into Preferences::GetType(), because all other nsPrefBranch methods depend on
Preferences methods.
The patch also removes the `aKind` argument from GetType(), because it has no
effect -- a pref only has one type, regardless of whether it has a default
value, a user value, or both.
MozReview-Commit-ID: J3vxFPaP8S3
--- a/modules/libpref/Preferences.cpp
+++ b/modules/libpref/Preferences.cpp
@@ -1408,37 +1408,17 @@ nsPrefBranch::GetRoot(nsACString& aRoot)
}
NS_IMETHODIMP
nsPrefBranch::GetPrefType(const char* aPrefName, int32_t* aRetVal)
{
NS_ENSURE_ARG(aPrefName);
const PrefName& prefName = GetPrefName(aPrefName);
- Pref* pref;
- if (gHashTable && (pref = pref_HashTableLookup(prefName.get()))) {
- switch (pref->Type()) {
- case PrefType::String:
- *aRetVal = PREF_STRING;
- break;
-
- case PrefType::Int:
- *aRetVal = PREF_INT;
- break;
-
- case PrefType::Bool:
- *aRetVal = PREF_BOOL;
- break;
-
- default:
- MOZ_CRASH();
- }
- } else {
- *aRetVal = PREF_INVALID;
- }
+ *aRetVal = Preferences::GetType(prefName.get());
return NS_OK;
}
NS_IMETHODIMP
nsPrefBranch::GetBoolPrefWithDefault(const char* aPrefName,
bool aDefaultValue,
uint8_t aArgc,
bool* aRetVal)
@@ -3818,18 +3798,17 @@ Preferences::InitInitialObjects()
}
}
#ifdef MOZ_WIDGET_ANDROID
// Set up the correct default for toolkit.telemetry.enabled. If this build
// has MOZ_TELEMETRY_ON_BY_DEFAULT *or* we're on the beta channel, telemetry
// is on by default, otherwise not. This is necessary so that beta users who
// are testing final release builds don't flipflop defaults.
- if (Preferences::GetType(kTelemetryPref, PrefValueKind::Default) ==
- nsIPrefBranch::PREF_INVALID) {
+ if (Preferences::GetType(kTelemetryPref) == nsIPrefBranch::PREF_INVALID) {
bool prerelease = false;
#ifdef MOZ_TELEMETRY_ON_BY_DEFAULT
prerelease = true;
#else
nsAutoCString prefValue;
Preferences::GetCString(kChannelPref, prefValue, PrefValueKind::Default);
if (prefValue.EqualsLiteral("beta")) {
prerelease = true;
@@ -4161,23 +4140,38 @@ Preferences::HasUserValue(const char* aP
{
NS_ENSURE_TRUE(InitStaticMembers(), false);
Pref* pref = pref_HashTableLookup(aPrefName);
return pref && pref->HasUserValue();
}
/* static */ int32_t
-Preferences::GetType(const char* aPrefName, PrefValueKind aKind)
+Preferences::GetType(const char* aPrefName)
{
NS_ENSURE_TRUE(InitStaticMembers(), nsIPrefBranch::PREF_INVALID);
- int32_t result;
- return NS_SUCCEEDED(GetRootBranch(aKind)->GetPrefType(aPrefName, &result))
- ? result
- : nsIPrefBranch::PREF_INVALID;
+
+ Pref* pref;
+ if (!gHashTable || !(pref = pref_HashTableLookup(aPrefName))) {
+ return PREF_INVALID;
+ }
+
+ switch (pref->Type()) {
+ case PrefType::String:
+ return PREF_STRING;
+
+ case PrefType::Int:
+ return PREF_INT;
+
+ case PrefType::Bool:
+ return PREF_BOOL;
+
+ default:
+ MOZ_CRASH();
+ }
}
/* static */ nsresult
Preferences::AddStrongObserver(nsIObserver* aObserver, const char* aPref)
{
MOZ_ASSERT(aObserver);
NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
return sPreferences->mRootBranch->AddObserver(aPref, aObserver, false);
--- a/modules/libpref/Preferences.h
+++ b/modules/libpref/Preferences.h
@@ -87,18 +87,17 @@ public:
static nsIPrefBranch* GetRootBranch(PrefValueKind aKind = PrefValueKind::User)
{
NS_ENSURE_TRUE(InitStaticMembers(), nullptr);
return (aKind == PrefValueKind::Default) ? sPreferences->mDefaultRootBranch
: sPreferences->mRootBranch;
}
// Gets the type of the pref.
- static int32_t GetType(const char* aPrefName,
- PrefValueKind aKind = PrefValueKind::User);
+ static int32_t GetType(const char* aPrefName);
// Fallible value getters.
static nsresult GetBool(const char* aPrefName,
bool* aResult,
PrefValueKind aKind = PrefValueKind::User);
static nsresult GetInt(const char* aPrefName,
int32_t* aResult,
PrefValueKind aKind = PrefValueKind::User);