Bug 1775627 - Caching the Email WebApp domain pref. r=dimi

This patch make the pref value of the Email WebApp domain pref to be
cached in the memory. So, we won't get the pref everytime we access the
pref value. The cached pref value will be updated when the pref changes.

Differential Revision: https://phabricator.services.mozilla.com/D154085
This commit is contained in:
Tim Huang 2022-08-12 07:31:14 +00:00
parent 35814d5a15
commit 53800e2e07
2 changed files with 63 additions and 4 deletions

View File

@ -7,6 +7,7 @@
#include "UrlClassifierFeatureEmailTrackingDataCollection.h"
#include "mozilla/AntiTrackingUtils.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/ContentBlockingNotifier.h"
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/WindowGlobalParent.h"
@ -43,6 +44,18 @@ namespace {
StaticRefPtr<UrlClassifierFeatureEmailTrackingDataCollection>
gFeatureEmailTrackingDataCollection;
StaticAutoPtr<nsCString> gEmailWebAppDomainsPref;
static constexpr char kEmailWebAppDomainPrefName[] =
"privacy.trackingprotection.emailtracking.webapp.domains";
void EmailWebAppDomainPrefChangeCallback(const char* aPrefName, void*) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(!strcmp(aPrefName, kEmailWebAppDomainPrefName));
MOZ_ASSERT(gEmailWebAppDomainsPref);
Preferences::GetCString(kEmailWebAppDomainPrefName, *gEmailWebAppDomainsPref);
}
} // namespace
UrlClassifierFeatureEmailTrackingDataCollection::
@ -171,8 +184,22 @@ UrlClassifierFeatureEmailTrackingDataCollection::ProcessChannel(
RefPtr<dom::WindowGlobalParent> topWindowParent =
bc->Canonical()->GetTopWindowContext();
bool isTopEmailWebApp = topWindowParent->DocumentPrincipal()->IsURIInPrefList(
"privacy.trackingprotection.emailtracking.webapp.domains");
// Cache the email webapp domains pref value and register the callback
// function to update the cached value when the pref changes.
if (!gEmailWebAppDomainsPref) {
gEmailWebAppDomainsPref = new nsCString();
Preferences::RegisterCallbackAndCall(EmailWebAppDomainPrefChangeCallback,
kEmailWebAppDomainPrefName);
RunOnShutdown([]() {
Preferences::UnregisterCallback(EmailWebAppDomainPrefChangeCallback,
kEmailWebAppDomainPrefName);
gEmailWebAppDomainsPref = nullptr;
});
}
bool isTopEmailWebApp = topWindowParent->DocumentPrincipal()->IsURIInList(
*gEmailWebAppDomainsPref);
uint32_t flags = UrlClassifierCommon::TablesToClassificationFlags(
aList, sClassificationData,

View File

@ -12,6 +12,7 @@
#include "nsServiceManagerUtils.h"
#include "nsTArray.h"
#include "mozilla/BasePrincipal.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/HashFunctions.h"
#include "mozilla/Preferences.h"
#include "mozilla/RandomNum.h"
@ -19,6 +20,7 @@
#include "mozilla/StaticPrefs_browser.h"
#include "mozilla/StaticPrefs_privacy.h"
#include "mozilla/StaticPrefs_telemetry.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/Telemetry.h"
#include "mozilla/XorShift128PlusRNG.h"
@ -36,6 +38,22 @@ static constexpr double kRatioReportUser = 0.01;
// randomly choose 0.14% documents when the page is unload.
static constexpr double kRatioReportDocument = 0.0014;
namespace {
StaticAutoPtr<nsCString> gEmailWebAppDomainsPref;
static constexpr char kEmailWebAppDomainPrefName[] =
"privacy.trackingprotection.emailtracking.webapp.domains";
void EmailWebAppDomainPrefChangeCallback(const char* aPrefName, void*) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(!strcmp(aPrefName, kEmailWebAppDomainPrefName));
MOZ_ASSERT(gEmailWebAppDomainsPref);
Preferences::GetCString(kEmailWebAppDomainPrefName, *gEmailWebAppDomainsPref);
}
} // namespace
static bool IsReportingPerUserEnabled() {
MOZ_ASSERT(NS_IsMainThread());
@ -369,8 +387,22 @@ void ContentBlockingLog::ReportEmailTrackingLog(
}
}
bool isTopEmailWebApp = aFirstPartyPrincipal->IsURIInPrefList(
"privacy.trackingprotection.emailtracking.webapp.domains");
// Cache the email webapp domains pref value and register the callback
// function to update the cached value when the pref changes.
if (!gEmailWebAppDomainsPref) {
gEmailWebAppDomainsPref = new nsCString();
Preferences::RegisterCallbackAndCall(EmailWebAppDomainPrefChangeCallback,
kEmailWebAppDomainPrefName);
RunOnShutdown([]() {
Preferences::UnregisterCallback(EmailWebAppDomainPrefChangeCallback,
kEmailWebAppDomainPrefName);
gEmailWebAppDomainsPref = nullptr;
});
}
bool isTopEmailWebApp =
aFirstPartyPrincipal->IsURIInList(*gEmailWebAppDomainsPref);
uint32_t level1Count = level1SiteSet.Count();
uint32_t level2Count = level2SiteSet.Count();