Bug 1848694 - Remove/avoid global references to nsIIOService r=mccr8,necko-reviewers,kershaw

This patch removes the static pointer to nsIIOService in nsContentUtils,
replacing it to calls to mozilla::components::IO::Service.

It also makes nsScriptSecurityManager::sIOService a StaticRefPtr.

Differential Revision: https://phabricator.services.mozilla.com/D188714
This commit is contained in:
Valentin Gosu
2023-09-22 12:49:44 +00:00
parent 84f393f171
commit cdd5195292
7 changed files with 30 additions and 29 deletions

View File

@@ -79,7 +79,7 @@
using namespace mozilla;
using namespace mozilla::dom;
nsIIOService* nsScriptSecurityManager::sIOService = nullptr;
StaticRefPtr<nsIIOService> nsScriptSecurityManager::sIOService;
std::atomic<bool> nsScriptSecurityManager::sStrictFileOriginPolicy = true;
namespace {
@@ -1549,9 +1549,12 @@ nsScriptSecurityManager::nsScriptSecurityManager(void)
}
nsresult nsScriptSecurityManager::Init() {
nsresult rv = CallGetService(NS_IOSERVICE_CONTRACTID, &sIOService);
NS_ENSURE_SUCCESS(rv, rv);
nsresult rv;
RefPtr<nsIIOService> io = mozilla::components::IO::Service(&rv);
if (NS_FAILED(rv)) {
return rv;
}
sIOService = std::move(io);
InitPrefs();
// Create our system principal singleton
@@ -1597,7 +1600,7 @@ nsScriptSecurityManager::~nsScriptSecurityManager(void) {
}
void nsScriptSecurityManager::Shutdown() {
NS_IF_RELEASE(sIOService);
sIOService = nullptr;
BundleHelper::Shutdown();
SystemPrincipal::Shutdown();
}

View File

@@ -135,7 +135,7 @@ class nsScriptSecurityManager final : public nsIScriptSecurityManager {
static std::atomic<bool> sStrictFileOriginPolicy;
static nsIIOService* sIOService;
static mozilla::StaticRefPtr<nsIIOService> sIOService;
static nsIStringBundle* sStrBundle;
};

View File

@@ -12496,8 +12496,8 @@ void Document::MaybePreconnect(nsIURI* aOrigURI, mozilla::CORSMode aCORSMode) {
return;
}
nsCOMPtr<nsISpeculativeConnect> speculator(
do_QueryInterface(nsContentUtils::GetIOService()));
nsCOMPtr<nsISpeculativeConnect> speculator =
mozilla::components::IO::Service();
if (!speculator) {
return;
}

View File

@@ -26,6 +26,7 @@
#include "mozilla/AnimationTarget.h"
#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/CORSMode.h"
#include "mozilla/Components.h"
#include "mozilla/ComputedStyle.h"
#include "mozilla/ContentEvents.h"
#include "mozilla/DebugOnly.h"
@@ -3263,13 +3264,14 @@ nsresult Element::PostHandleEventForLinks(EventChainPostVisitor& aVisitor) {
// connection to be sure we have one ready when we open the channel.
if (nsIDocShell* shell = OwnerDoc()->GetDocShell()) {
if (nsCOMPtr<nsIURI> absURI = GetHrefURI()) {
nsCOMPtr<nsISpeculativeConnect> sc =
do_QueryInterface(nsContentUtils::GetIOService());
if (nsCOMPtr<nsISpeculativeConnect> sc =
mozilla::components::IO::Service()) {
nsCOMPtr<nsIInterfaceRequestor> ir = do_QueryInterface(shell);
sc->SpeculativeConnect(absURI, NodePrincipal(), ir, false);
}
}
}
}
} break;
case eMouseClick: {

View File

@@ -424,7 +424,6 @@ nsIXPConnect* nsContentUtils::sXPConnect;
nsIScriptSecurityManager* nsContentUtils::sSecurityManager;
nsIPrincipal* nsContentUtils::sSystemPrincipal;
nsIPrincipal* nsContentUtils::sNullSubjectPrincipal;
nsIIOService* nsContentUtils::sIOService;
nsIConsoleService* nsContentUtils::sConsoleService;
static nsTHashMap<RefPtr<nsAtom>, EventNameMapping>* sAtomEventTable;
@@ -804,13 +803,6 @@ nsresult nsContentUtils::Init() {
nullPrincipal.forget(&sNullSubjectPrincipal);
nsresult rv = CallGetService(NS_IOSERVICE_CONTRACTID, &sIOService);
if (NS_FAILED(rv)) {
// This makes life easier, but we can live without it.
sIOService = nullptr;
}
if (!InitializeEventTable()) return NS_ERROR_FAILURE;
if (!sEventListenerManagersHash) {
@@ -1880,7 +1872,6 @@ void nsContentUtils::Shutdown() {
NS_IF_RELEASE(sSecurityManager);
NS_IF_RELEASE(sSystemPrincipal);
NS_IF_RELEASE(sNullSubjectPrincipal);
NS_IF_RELEASE(sIOService);
sBidiKeyboard = nullptr;
@@ -2084,8 +2075,15 @@ bool nsContentUtils::IsAbsoluteURL(const nsACString& aURL) {
return true;
}
nsresult rv = NS_OK;
nsCOMPtr<nsIIOService> io = mozilla::components::IO::Service(&rv);
MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
if (NS_FAILED(rv)) {
return false;
}
uint32_t flags;
if (NS_SUCCEEDED(sIOService->GetProtocolFlags(scheme.get(), &flags))) {
if (NS_SUCCEEDED(io->GetProtocolFlags(scheme.get(), &flags))) {
return flags & nsIProtocolHandler::URI_NORELATIVE;
}
@@ -6255,7 +6253,7 @@ bool nsContentUtils::CheckForSubFrameDrop(nsIDragSession* aDragSession,
/* static */
bool nsContentUtils::URIIsLocalFile(nsIURI* aURI) {
bool isFile;
nsCOMPtr<nsINetUtil> util = do_QueryInterface(sIOService);
nsCOMPtr<nsINetUtil> util = mozilla::components::IO::Service();
// Important: we do NOT test the entire URI chain here!
return util &&

View File

@@ -826,8 +826,6 @@ class nsContentUtils {
// element.
static bool InProlog(nsINode* aNode);
static nsIIOService* GetIOService() { return sIOService; }
static nsIBidiKeyboard* GetBidiKeyboard();
/**
@@ -3523,8 +3521,6 @@ class nsContentUtils {
static nsIPrincipal* sSystemPrincipal;
static nsIPrincipal* sNullSubjectPrincipal;
static nsIIOService* sIOService;
static nsIConsoleService* sConsoleService;
static nsIStringBundleService* sStringBundleService;

View File

@@ -176,8 +176,10 @@ static bool CanHandleURI(nsIURI* aURI) {
return false;
}
nsIIOService* ios = nsContentUtils::GetIOService();
if (!ios) return false;
nsCOMPtr<nsIIOService> ios = mozilla::components::IO::Service();
if (!ios) {
return false;
}
nsCOMPtr<nsIProtocolHandler> handler;
ios->GetProtocolHandler(scheme.get(), getter_AddRefs(handler));