Skip to content

Commit

Permalink
Bug 1943262 - Refactor instantiation of nsEffectiveTLDService r=cooki…
Browse files Browse the repository at this point in the history
…e-reviewers,anti-tracking-reviewers,permissions-reviewers,emz

This ensures that the instantiation of the service always returns the
same singleton. I also changed calls to nsEffectiveTLDService::GetInstance
to use mozilla::components::EffectiveTLD::Service() instead.

Differential Revision: https://phabricator.services.mozilla.com/D235288
  • Loading branch information
valenting committed Jan 28, 2025
1 parent 59a420a commit b8adff1
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 71 deletions.
15 changes: 10 additions & 5 deletions caps/BasePrincipal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
#include "mozilla/dom/StorageUtils.h"
#include "mozilla/JSONStringWriteFuncs.h"
#include "mozilla/JSONWriter.h"
#include "nsIEffectiveTLDService.h"
#include "nsIURL.h"
#include "nsEffectiveTLDService.h"
#include "nsIURIMutator.h"
#include "mozilla/StaticPrefs_permissions.h"
#include "nsIURIMutator.h"
Expand Down Expand Up @@ -538,7 +538,7 @@ BasePrincipal::EqualsForPermission(nsIPrincipal* aOther, bool aExactHost,
}

nsCOMPtr<nsIEffectiveTLDService> tldService =
do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
mozilla::components::EffectiveTLD::Service();
if (!tldService) {
NS_ERROR("Should have a tld service!");
return NS_ERROR_FAILURE;
Expand Down Expand Up @@ -1416,8 +1416,8 @@ BasePrincipal::GetLocalStorageQuotaKey(nsACString& aKey) {
rv = url->GetDirectory(baseDomain);
NS_ENSURE_SUCCESS(rv, rv);
} else {
nsCOMPtr<nsIEffectiveTLDService> eTLDService(
do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID, &rv));
nsCOMPtr<nsIEffectiveTLDService> eTLDService =
mozilla::components::EffectiveTLD::Service(&rv);
NS_ENSURE_SUCCESS(rv, rv);

nsAutoCString eTLDplusOne;
Expand Down Expand Up @@ -1459,7 +1459,12 @@ BasePrincipal::GetNextSubDomainPrincipal(
}

nsCString subDomain;
rv = nsEffectiveTLDService::GetInstance()->GetNextSubDomain(host, subDomain);
nsCOMPtr<nsIEffectiveTLDService> eTLDService =
mozilla::components::EffectiveTLD::Service();
if (!eTLDService) {
return NS_OK;
}
rv = eTLDService->GetNextSubDomain(host, subDomain);

if (NS_FAILED(rv) || subDomain.IsEmpty()) {
return NS_OK;
Expand Down
11 changes: 5 additions & 6 deletions dom/base/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,6 @@
#include "nsDeviceContext.h"
#include "nsDocShell.h"
#include "nsDocShellLoadTypes.h"
#include "nsEffectiveTLDService.h"
#include "nsError.h"
#include "nsEscape.h"
#include "nsFocusManager.h"
Expand Down Expand Up @@ -2061,7 +2060,7 @@ void Document::RecordPageLoadEventTelemetry(
}

nsCOMPtr<nsIEffectiveTLDService> tldService =
do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
mozilla::components::EffectiveTLD::Service();
if (tldService && mReferrerInfo &&
(docshell->GetLoadType() & nsIDocShell::LOAD_CMD_NORMAL)) {
nsAutoCString currentBaseDomain, referrerBaseDomain;
Expand Down Expand Up @@ -6786,7 +6785,7 @@ void Document::SetCookie(const nsAString& aCookieString, ErrorResult& aRv) {
}

nsCOMPtr<nsIEffectiveTLDService> tldService =
do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
mozilla::components::EffectiveTLD::Service();
if (!tldService) {
return;
}
Expand Down Expand Up @@ -9363,7 +9362,7 @@ bool Document::IsValidDomain(nsIURI* aOrigHost, nsIURI* aNewURI) {
// We're golden if the new domain is the current page's base domain or a
// subdomain of it.
nsCOMPtr<nsIEffectiveTLDService> tldService =
do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
mozilla::components::EffectiveTLD::Service();
if (!tldService) {
return false;
}
Expand Down Expand Up @@ -18810,8 +18809,8 @@ already_AddRefed<Promise> Document::RequestStorageAccessUnderSite(
// Get a grant for the storage access permission that will be set
// when this is completed in the embedding context
nsCString serializedSite;
RefPtr<nsEffectiveTLDService> etld =
nsEffectiveTLDService::GetInstance();
nsCOMPtr<nsIEffectiveTLDService> etld =
mozilla::components::EffectiveTLD::Service();
if (!etld) {
return StorageAccessAPIHelper::
StorageAccessPermissionGrantPromise::CreateAndReject(
Expand Down
3 changes: 1 addition & 2 deletions dom/base/ThirdPartyUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include "mozilla/dom/WindowGlobalParent.h"
#include "nsCOMPtr.h"
#include "nsDebug.h"
#include "nsEffectiveTLDService.h"
#include "nsError.h"
#include "nsGlobalWindowOuter.h"
#include "nsIChannel.h"
Expand Down Expand Up @@ -79,7 +78,7 @@ nsresult ThirdPartyUtil::Init() {
gService = this;
mozilla::ClearOnShutdown(&gService);

mTLDService = nsEffectiveTLDService::GetInstance();
mTLDService = mozilla::components::EffectiveTLD::Service();
return mTLDService ? NS_OK : NS_ERROR_FAILURE;
}

Expand Down
3 changes: 2 additions & 1 deletion dom/base/ThirdPartyUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "ErrorList.h"
#include "mozIThirdPartyUtil.h"
#include "mozilla/RefPtr.h"
#include "nsIEffectiveTLDService.h"
#include "nsISupports.h"
#include "nsString.h"

Expand Down Expand Up @@ -49,7 +50,7 @@ class ThirdPartyUtil final : public mozIThirdPartyUtil {
nsresult IsThirdPartyInternal(const nsCString& aFirstDomain,
nsIURI* aSecondURI, bool* aResult);

RefPtr<nsEffectiveTLDService> mTLDService;
RefPtr<nsIEffectiveTLDService> mTLDService;
};

#endif
5 changes: 3 additions & 2 deletions dom/credentialmanagement/identity/IdentityCredential.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#include "mozilla/ExpandedPrincipal.h"
#include "mozilla/IdentityCredentialRequestManager.h"
#include "mozilla/NullPrincipal.h"
#include "nsEffectiveTLDService.h"
#include "nsICredentialChooserService.h"
#include "nsIEffectiveTLDService.h"
#include "nsIGlobalObject.h"
#include "nsIIdentityCredentialPromptService.h"
#include "nsIIdentityCredentialStorageService.h"
Expand Down Expand Up @@ -1200,7 +1200,8 @@ IdentityCredential::CheckRootManifest(nsIPrincipal* aPrincipal,
if (NS_WARN_IF(NS_FAILED(rv))) {
return IdentityCredential::ValidationPromise::CreateAndReject(rv, __func__);
}
RefPtr<nsEffectiveTLDService> etld = nsEffectiveTLDService::GetInstance();
RefPtr<nsIEffectiveTLDService> etld =
mozilla::components::EffectiveTLD::Service();
if (!etld) {
return IdentityCredential::ValidationPromise::CreateAndReject(
NS_ERROR_SERVICE_NOT_AVAILABLE, __func__);
Expand Down
29 changes: 19 additions & 10 deletions extensions/permissions/PermissionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#endif
#include "mozilla/BasePrincipal.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/Components.h"
#include "mozilla/ContentPrincipal.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/dom/CanonicalBrowsingContext.h"
Expand All @@ -36,8 +37,8 @@
#include "nsContentUtils.h"
#include "nsCRT.h"
#include "nsDebug.h"
#include "nsEffectiveTLDService.h"
#include "nsIConsoleService.h"
#include "nsIEffectiveTLDService.h"
#include "nsIUserIdleService.h"
#include "nsIInputStream.h"
#include "nsINavHistoryService.h"
Expand Down Expand Up @@ -250,7 +251,8 @@ nsresult GetOriginFromPrincipal(nsIPrincipal* aPrincipal, bool aForceStripOA,
nsresult GetSiteFromPrincipal(nsIPrincipal* aPrincipal, bool aForceStripOA,
nsACString& aSite) {
nsCOMPtr<nsIURI> uri = aPrincipal->GetURI();
nsEffectiveTLDService* etld = nsEffectiveTLDService::GetInstance();
nsCOMPtr<nsIEffectiveTLDService> etld =
mozilla::components::EffectiveTLD::Service();
NS_ENSURE_TRUE(etld, NS_ERROR_FAILURE);
NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
nsresult rv = etld->GetSite(uri, aSite);
Expand Down Expand Up @@ -321,8 +323,9 @@ nsresult GetPrincipal(nsIURI* aURI, nsIPrincipal** aPrincipal) {

nsCString GetNextSubDomainForHost(const nsACString& aHost) {
nsCString subDomain;
nsresult rv =
nsEffectiveTLDService::GetInstance()->GetNextSubDomain(aHost, subDomain);
nsCOMPtr<nsIEffectiveTLDService> etld =
mozilla::components::EffectiveTLD::Service();
nsresult rv = etld->GetNextSubDomain(aHost, subDomain);
// We can fail if there is no more subdomain or if the host can't have a
// subdomain.
if (NS_FAILED(rv)) {
Expand Down Expand Up @@ -416,8 +419,9 @@ nsresult UpgradeHostToOriginAndInsert(

// Get the eTLD+1 of the domain
nsAutoCString eTLD1;
rv = nsEffectiveTLDService::GetInstance()->GetBaseDomainFromHost(aHost, 0,
eTLD1);
nsCOMPtr<nsIEffectiveTLDService> etld =
mozilla::components::EffectiveTLD::Service();
rv = etld->GetBaseDomainFromHost(aHost, 0, eTLD1);

if (NS_FAILED(rv)) {
// If the lookup on the tldService for the base domain for the host
Expand Down Expand Up @@ -1334,6 +1338,8 @@ nsresult PermissionManager::TryInitDB(bool aRemoveFile,
id = idStmt->AsInt32(0) + 1;
}

nsCOMPtr<nsIEffectiveTLDService> etld =
mozilla::components::EffectiveTLD::Service();
while (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
MigrationEntry entry;

Expand All @@ -1344,8 +1350,7 @@ nsresult PermissionManager::TryInitDB(bool aRemoveFile,
}

nsAutoCString eTLD1;
rv = nsEffectiveTLDService::GetInstance()->GetBaseDomainFromHost(
entry.mHost, 0, eTLD1);
rv = etld->GetBaseDomainFromHost(entry.mHost, 0, eTLD1);
if (NS_SUCCEEDED(rv)) {
// We only care about entries which the tldService can't
// handle
Expand Down Expand Up @@ -1458,6 +1463,8 @@ nsresult PermissionManager::TryInitDB(bool aRemoveFile,

nsTHashSet<nsCStringHashKey> deduplicationSet;
bool hasResult;
nsCOMPtr<nsIEffectiveTLDService> etld =
mozilla::components::EffectiveTLD::Service();
while (NS_SUCCEEDED(selectStmt->ExecuteStep(&hasResult)) && hasResult) {
int64_t id;
rv = selectStmt->GetInt64(0, &id);
Expand All @@ -1477,7 +1484,7 @@ nsresult PermissionManager::TryInitDB(bool aRemoveFile,
continue;
}
nsCString site;
rv = nsEffectiveTLDService::GetInstance()->GetSite(uri, site);
rv = etld->GetSite(uri, site);
if (NS_WARN_IF(NS_FAILED(rv))) {
continue;
}
Expand Down Expand Up @@ -3559,7 +3566,9 @@ nsresult PermissionManager::GetKeyForOrigin(const nsACString& aOrigin,
nsresult rv = NS_NewURI(getter_AddRefs(uri), aKey);
if (!NS_WARN_IF(NS_FAILED(rv))) {
nsCString site;
rv = nsEffectiveTLDService::GetInstance()->GetSite(uri, site);
nsCOMPtr<nsIEffectiveTLDService> etld =
mozilla::components::EffectiveTLD::Service();
rv = etld->GetSite(uri, site);
if (!NS_WARN_IF(NS_FAILED(rv))) {
aKey = site;
}
Expand Down
4 changes: 2 additions & 2 deletions netwerk/build/components.conf
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ Classes = [
'contract_ids': ['@mozilla.org/network/effective-tld-service;1'],
'interfaces': ['nsIEffectiveTLDService'],
'singleton': True,
'type': 'nsEffectiveTLDService',
'type': 'nsIEffectiveTLDService',
'constructor': 'nsEffectiveTLDService::GetXPCOMSingleton',
'headers': ['/netwerk/dns/nsEffectiveTLDService.h'],
'init_method': 'Init',
},
{
'cid': '{be9a53ae-c7e9-11d3-8cda-0060b0fc14a3}',
Expand Down
9 changes: 5 additions & 4 deletions netwerk/cookie/ThirdPartyCookieBlockingExceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "mozilla/ErrorNames.h"
#include "mozilla/Logging.h"

#include "nsIEffectiveTLDService.h"
#include "nsIChannel.h"

namespace mozilla {
Expand Down Expand Up @@ -100,8 +101,8 @@ bool ThirdPartyCookieBlockingExceptions::CheckExceptionForURIs(
return false;
}

RefPtr<nsEffectiveTLDService> eTLDService =
nsEffectiveTLDService::GetInstance();
nsCOMPtr<nsIEffectiveTLDService> eTLDService =
mozilla::components::EffectiveTLD::Service();
NS_ENSURE_TRUE(eTLDService, false);

nsAutoCString thirdPartySite;
Expand Down Expand Up @@ -130,8 +131,8 @@ bool ThirdPartyCookieBlockingExceptions::CheckExceptionForChannel(
return false;
}

RefPtr<nsEffectiveTLDService> eTLDService =
nsEffectiveTLDService::GetInstance();
nsCOMPtr<nsIEffectiveTLDService> eTLDService =
mozilla::components::EffectiveTLD::Service();
NS_ENSURE_TRUE(eTLDService, false);

nsCOMPtr<nsIURI> uri;
Expand Down
26 changes: 11 additions & 15 deletions netwerk/dns/nsEffectiveTLDService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "mozilla/ArrayUtils.h"
#include "mozilla/Components.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/HashFunctions.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/ResultExtensions.h"
Expand Down Expand Up @@ -41,7 +42,7 @@ NS_IMPL_ISUPPORTS(nsEffectiveTLDService, nsIEffectiveTLDService,

// ----------------------------------------------------------------------

static nsEffectiveTLDService* gService = nullptr;
static StaticRefPtr<nsEffectiveTLDService> gService;

nsEffectiveTLDService::nsEffectiveTLDService() : mGraph(etld_dafsa::kDafsa) {}

Expand All @@ -52,34 +53,29 @@ nsresult nsEffectiveTLDService::Init() {
return NS_ERROR_ALREADY_INITIALIZED;
}

gService = this;
RegisterWeakMemoryReporter(this);

return NS_OK;
}

nsEffectiveTLDService::~nsEffectiveTLDService() {
UnregisterWeakMemoryReporter(this);
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1786310#c15
if (gService == this) {
gService = nullptr;
}
}

// static
nsEffectiveTLDService* nsEffectiveTLDService::GetInstance() {
already_AddRefed<nsIEffectiveTLDService>
nsEffectiveTLDService::GetXPCOMSingleton() {
if (gService) {
return gService;
return do_AddRef(gService);
}
nsCOMPtr<nsIEffectiveTLDService> tldService;
tldService = mozilla::components::EffectiveTLD::Service();
if (!tldService) {
RefPtr<nsEffectiveTLDService> instance = new nsEffectiveTLDService();
nsresult rv = instance->Init();
if (NS_FAILED(rv)) {
return nullptr;
}
MOZ_ASSERT(
gService,
"gService must have been initialized in nsEffectiveTLDService::Init");
return gService;
gService = instance;
ClearOnShutdown(&gService);
return instance.forget();
}

MOZ_DEFINE_MALLOC_SIZE_OF(EffectiveTLDServiceMallocSizeOf)
Expand Down
2 changes: 1 addition & 1 deletion netwerk/dns/nsEffectiveTLDService.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class nsEffectiveTLDService final : public nsIEffectiveTLDService,
nsEffectiveTLDService();
nsresult Init();

static nsEffectiveTLDService* GetInstance();
static already_AddRefed<nsIEffectiveTLDService> GetXPCOMSingleton();

size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf);

Expand Down
24 changes: 13 additions & 11 deletions netwerk/test/browser/browser_bug1535877.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
"use strict";

add_task(_ => {
try {
Cc["@mozilla.org/network/effective-tld-service;1"].createInstance(
Ci.nsISupports
);
} catch (e) {
is(
e.result,
Cr.NS_ERROR_XPC_CI_RETURNED_FAILURE,
"Component creation as an instance fails with expected code"
);
}
// Check that create instance and the service getter all return the same object.
let etld = Cc["@mozilla.org/network/effective-tld-service;1"].createInstance(
Ci.nsIEffectiveTLDService
);

Assert.equal(etld, Services.eTLD);

// eslint-disable-next-line mozilla/use-services
let etld2 = Cc["@mozilla.org/network/effective-tld-service;1"].getService(
Ci.nsIEffectiveTLDService
);

Assert.equal(etld, etld2);
});
Loading

0 comments on commit b8adff1

Please sign in to comment.