--- a/toolkit/components/resistfingerprinting/nsRFPService.cpp
+++ b/toolkit/components/resistfingerprinting/nsRFPService.cpp
@@ -706,16 +706,24 @@ nsRFPService::GetSpoofedUserAgent(nsACSt
uint32_t spoofedVersion = firefoxVersion - ((firefoxVersion - 4) % 7);
userAgent.Assign(nsPrintfCString(
"Mozilla/5.0 (%s; rv:%d.0) Gecko/%s Firefox/%d.0",
SPOOFED_UA_OS, spoofedVersion, LEGACY_BUILD_ID, spoofedVersion));
return rv;
}
+static const char* gCallbackPrefs[] = {
+ RESIST_FINGERPRINTING_PREF,
+ RFP_TIMER_PREF,
+ RFP_TIMER_VALUE_PREF,
+ RFP_JITTER_VALUE_PREF,
+ nullptr,
+};
+
nsresult
nsRFPService::Init()
{
MOZ_ASSERT(NS_IsMainThread());
nsresult rv;
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
@@ -724,30 +732,18 @@ nsRFPService::Init()
rv = obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
NS_ENSURE_SUCCESS(rv, rv);
#if defined(XP_WIN)
rv = obs->AddObserver(this, PROFILE_INITIALIZED_TOPIC, false);
NS_ENSURE_SUCCESS(rv, rv);
#endif
- nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
- NS_ENSURE_TRUE(prefs, NS_ERROR_NOT_AVAILABLE);
-
- rv = prefs->AddObserver(RESIST_FINGERPRINTING_PREF, this, false);
- NS_ENSURE_SUCCESS(rv, rv);
-
- rv = prefs->AddObserver(RFP_TIMER_PREF, this, false);
- NS_ENSURE_SUCCESS(rv, rv);
-
- rv = prefs->AddObserver(RFP_TIMER_VALUE_PREF, this, false);
- NS_ENSURE_SUCCESS(rv, rv);
-
- rv = prefs->AddObserver(RFP_JITTER_VALUE_PREF, this, false);
- NS_ENSURE_SUCCESS(rv, rv);
+ Preferences::RegisterCallbacks(PREF_CHANGE_METHOD(nsRFPService::PrefChanged),
+ gCallbackPrefs, this);
Preferences::AddAtomicBoolVarCache(&sPrivacyTimerPrecisionReduction,
RFP_TIMER_PREF,
true);
Preferences::AddAtomicUintVarCache(&sResolutionUSec,
RFP_TIMER_VALUE_PREF,
RFP_TIMER_VALUE_DEFAULT);
@@ -850,26 +846,19 @@ nsRFPService::StartShutdown()
StaticMutexAutoLock lock(sLock);
{
sCache = nullptr;
}
if (obs) {
obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
-
- nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
-
- if (prefs) {
- prefs->RemoveObserver(RESIST_FINGERPRINTING_PREF, this);
- prefs->RemoveObserver(RFP_TIMER_PREF, this);
- prefs->RemoveObserver(RFP_TIMER_VALUE_PREF, this);
- prefs->RemoveObserver(RFP_JITTER_VALUE_PREF, this);
- }
}
+ Preferences::UnregisterCallbacks(PREF_CHANGE_METHOD(nsRFPService::PrefChanged),
+ gCallbackPrefs, this);
}
/* static */
void
nsRFPService::MaybeCreateSpoofingKeyCodes(const KeyboardLangs aLang,
const KeyboardRegions aRegion)
{
if (!sSpoofingKeyboardCodes) {
@@ -1089,42 +1078,44 @@ nsRFPService::GetSpoofedKeyCode(const ns
if (GetSpoofedKeyCodeInfo(aDoc, aKeyboardEvent, keyCodeInfo)) {
aOut = keyCodeInfo.mKeyCode;
return true;
}
return false;
}
+void
+nsRFPService::PrefChanged(const char* aPref)
+{
+ nsDependentCString pref(aPref);
+
+ if (pref.EqualsLiteral(RFP_TIMER_PREF) ||
+ pref.EqualsLiteral(RFP_TIMER_VALUE_PREF) ||
+ pref.EqualsLiteral(RFP_JITTER_VALUE_PREF)) {
+ UpdateTimers();
+ }
+ else if (pref.EqualsLiteral(RESIST_FINGERPRINTING_PREF)) {
+ UpdateRFPPref();
+
+#if defined(XP_WIN)
+ if (!XRE_IsE10sParentProcess()) {
+ // Windows does not follow POSIX. Updates to the TZ environment variable
+ // are not reflected immediately on that platform as they are on UNIX
+ // systems without this call.
+ _tzset();
+ }
+#endif
+ }
+}
+
NS_IMETHODIMP
nsRFPService::Observe(nsISupports* aObject, const char* aTopic,
const char16_t* aMessage)
{
- if (!strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic)) {
- NS_ConvertUTF16toUTF8 pref(aMessage);
-
- if (pref.EqualsLiteral(RFP_TIMER_PREF) ||
- pref.EqualsLiteral(RFP_TIMER_VALUE_PREF) ||
- pref.EqualsLiteral(RFP_JITTER_VALUE_PREF)) {
- UpdateTimers();
- }
- else if (pref.EqualsLiteral(RESIST_FINGERPRINTING_PREF)) {
- UpdateRFPPref();
-
-#if defined(XP_WIN)
- if (!XRE_IsE10sParentProcess()) {
- // Windows does not follow POSIX. Updates to the TZ environment variable
- // are not reflected immediately on that platform as they are on UNIX
- // systems without this call.
- _tzset();
- }
-#endif
- }
- }
-
if (!strcmp(NS_XPCOM_SHUTDOWN_OBSERVER_ID, aTopic)) {
StartShutdown();
}
#if defined(XP_WIN)
else if (!strcmp(PROFILE_INITIALIZED_TOPIC, aTopic)) {
// If we're e10s, then we don't need to run this, since the child process will
// simply inherit the environment variable from the parent process, in which
// case it's unnecessary to call _tzset().
--- a/toolkit/components/resistfingerprinting/nsRFPService.h
+++ b/toolkit/components/resistfingerprinting/nsRFPService.h
@@ -258,16 +258,18 @@ private:
nsRFPService() {}
~nsRFPService() {}
void UpdateTimers();
void UpdateRFPPref();
void StartShutdown();
+ void PrefChanged(const char* aPref);
+
static void MaybeCreateSpoofingKeyCodes(const KeyboardLangs aLang,
const KeyboardRegions aRegion);
static void MaybeCreateSpoofingKeyCodesForEnUS();
static void GetKeyboardLangAndRegion(const nsAString& aLanguage,
KeyboardLangs& aLang,
KeyboardRegions& aRegion);
static bool GetSpoofedKeyCodeInfo(const nsIDocument* aDoc,