diff --git a/dom/base/nsDocument.cpp b/dom/base/nsDocument.cpp index c16680e4453f..dd695bbb99c4 100644 --- a/dom/base/nsDocument.cpp +++ b/dom/base/nsDocument.cpp @@ -282,6 +282,7 @@ #include "nsHTMLTags.h" #include "NodeUbiReporting.h" #include "nsICookieService.h" +#include "mozilla/net/RequestContextService.h" using namespace mozilla; using namespace mozilla::dom; @@ -2340,7 +2341,7 @@ nsIDocument::ResetToURI(nsIURI* aURI, // Inform the associated request context about this load start so // any of its internal load progress flags gets reset. nsCOMPtr rcsvc = - do_GetService("@mozilla.org/network/request-context-service;1"); + mozilla::net::RequestContextService::GetOrCreate(); if (rcsvc) { nsCOMPtr rc; rcsvc->GetRequestContextFromLoadGroup(aLoadGroup, getter_AddRefs(rc)); diff --git a/netwerk/base/RequestContextService.cpp b/netwerk/base/RequestContextService.cpp index 9cdb2f071d67..a0ae99b615e5 100644 --- a/netwerk/base/RequestContextService.cpp +++ b/netwerk/base/RequestContextService.cpp @@ -15,8 +15,10 @@ #include "RequestContextService.h" #include "mozilla/Atomics.h" +#include "mozilla/ClearOnShutdown.h" #include "mozilla/Logging.h" #include "mozilla/Services.h" +#include "mozilla/StaticPtr.h" #include "mozilla/TimeStamp.h" #include "mozilla/net/NeckoChild.h" @@ -32,6 +34,8 @@ LazyLogModule gRequestContextLog("RequestContext"); #undef LOG #define LOG(args) MOZ_LOG(gRequestContextLog, LogLevel::Info, args) +static StaticRefPtr gSingleton; + // This is used to prevent adding tail pending requests after shutdown static bool sShutdown = false; @@ -518,19 +522,23 @@ RequestContextService::Shutdown() sShutdown = true; } -/* static */ nsresult -RequestContextService::Create(nsISupports *aOuter, const nsIID& aIID, void **aResult) +/* static */ already_AddRefed +RequestContextService::GetOrCreate() { MOZ_ASSERT(NS_IsMainThread()); - if (aOuter != nullptr) { - return NS_ERROR_NO_AGGREGATION; + + RefPtr svc; + if (gSingleton) { + svc = gSingleton; + } else { + svc = new RequestContextService(); + nsresult rv = svc->Init(); + NS_ENSURE_SUCCESS(rv, nullptr); + gSingleton = svc; + ClearOnShutdown(&gSingleton); } - RefPtr svc = new RequestContextService(); - nsresult rv = svc->Init(); - NS_ENSURE_SUCCESS(rv, rv); - - return svc->QueryInterface(aIID, aResult); + return svc.forget(); } NS_IMETHODIMP diff --git a/netwerk/base/RequestContextService.h b/netwerk/base/RequestContextService.h index 87918ab1e78f..b619bb7993c7 100644 --- a/netwerk/base/RequestContextService.h +++ b/netwerk/base/RequestContextService.h @@ -24,14 +24,14 @@ public: NS_DECL_NSIREQUESTCONTEXTSERVICE NS_DECL_NSIOBSERVER + static already_AddRefed GetOrCreate(); + +private: RequestContextService(); + virtual ~RequestContextService(); nsresult Init(); void Shutdown(); - static nsresult Create(nsISupports *outer, const nsIID& iid, void **result); - -private: - virtual ~RequestContextService(); static RequestContextService *sSelf; diff --git a/netwerk/base/moz.build b/netwerk/base/moz.build index 7b4f41ad0763..a45b08ee9202 100644 --- a/netwerk/base/moz.build +++ b/netwerk/base/moz.build @@ -177,6 +177,7 @@ EXPORTS.mozilla.net += [ 'Predictor.h', 'RedirectChannelRegistrar.h', 'ReferrerPolicy.h', + 'RequestContextService.h', 'SimpleChannelParent.h', 'TCPFastOpen.h', ] diff --git a/netwerk/base/nsLoadGroup.cpp b/netwerk/base/nsLoadGroup.cpp index 75d8c9570f65..ae1502a29285 100644 --- a/netwerk/base/nsLoadGroup.cpp +++ b/netwerk/base/nsLoadGroup.cpp @@ -21,6 +21,7 @@ #include "nsIRequestContext.h" #include "CacheObserver.h" #include "MainThreadUtils.h" +#include "RequestContextService.h" #include "mozilla/Unused.h" namespace mozilla { @@ -1056,7 +1057,7 @@ nsresult nsLoadGroup::MergeDefaultLoadFlags(nsIRequest *aRequest, nsresult nsLoadGroup::Init() { - mRequestContextService = do_GetService("@mozilla.org/network/request-context-service;1"); + mRequestContextService = RequestContextService::GetOrCreate(); if (mRequestContextService) { Unused << mRequestContextService->NewRequestContext(getter_AddRefs(mRequestContext)); } diff --git a/netwerk/build/nsNetCID.h b/netwerk/build/nsNetCID.h index 5d6838045a96..cc2fb5177706 100644 --- a/netwerk/build/nsNetCID.h +++ b/netwerk/build/nsNetCID.h @@ -485,17 +485,6 @@ { 0x92, 0x05, 0xc3, 0x09, 0xce, 0xb2, 0xd6, 0x41 } \ } -// service implementing nsIRequestContextService -#define NS_REQUESTCONTEXTSERVICE_CONTRACTID \ - "@mozilla.org/network/request-context-service;1" -#define NS_REQUESTCONTEXTSERVICE_CID \ -{ /* d5499fa7-7ba8-49ff-9e30-1858b99ace69 */ \ - 0xd5499fa7, \ - 0x7ba8, \ - 0x49ff, \ - {0x93, 0x30, 0x18, 0x58, 0xb9, 0x9a, 0xce, 0x69} \ -} - /****************************************************************************** * netwerk/cache/ classes */ diff --git a/netwerk/build/nsNetModule.cpp b/netwerk/build/nsNetModule.cpp index 4399a536489a..d0878344b316 100644 --- a/netwerk/build/nsNetModule.cpp +++ b/netwerk/build/nsNetModule.cpp @@ -148,10 +148,6 @@ namespace net { } // namespace net } // namespace mozilla -#include "RequestContextService.h" -typedef mozilla::net::RequestContextService RequestContextService; -NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(RequestContextService, Init) - /////////////////////////////////////////////////////////////////////////////// extern nsresult @@ -754,7 +750,6 @@ NS_DEFINE_NAMED_CID(NS_CACHE_STORAGE_SERVICE_CID); NS_DEFINE_NAMED_CID(NS_NSILOADCONTEXTINFOFACTORY_CID); NS_DEFINE_NAMED_CID(NS_NETWORKPREDICTOR_CID); NS_DEFINE_NAMED_CID(NS_CAPTIVEPORTAL_CID); -NS_DEFINE_NAMED_CID(NS_REQUESTCONTEXTSERVICE_CID); #ifdef BUILD_NETWORK_INFO_SERVICE NS_DEFINE_NAMED_CID(NETWORKINFOSERVICE_CID); #endif // BUILD_NETWORK_INFO_SERVICE @@ -872,7 +867,6 @@ static const mozilla::Module::CIDEntry kNeckoCIDs[] = { { &kNS_NSILOADCONTEXTINFOFACTORY_CID, false, nullptr, LoadContextInfoFactoryConstructor }, { &kNS_NETWORKPREDICTOR_CID, false, nullptr, mozilla::net::Predictor::Create }, { &kNS_CAPTIVEPORTAL_CID, false, nullptr, mozilla::net::nsICaptivePortalServiceConstructor }, - { &kNS_REQUESTCONTEXTSERVICE_CID, false, nullptr, RequestContextServiceConstructor }, #ifdef BUILD_NETWORK_INFO_SERVICE { &kNETWORKINFOSERVICE_CID, false, nullptr, nsNetworkInfoServiceConstructor }, #endif @@ -990,7 +984,6 @@ static const mozilla::Module::ContractIDEntry kNeckoContracts[] = { { NS_NSILOADCONTEXTINFOFACTORY_CONTRACTID, &kNS_NSILOADCONTEXTINFOFACTORY_CID }, { NS_NETWORKPREDICTOR_CONTRACTID, &kNS_NETWORKPREDICTOR_CID }, { NS_CAPTIVEPORTAL_CONTRACTID, &kNS_CAPTIVEPORTAL_CID }, - { NS_REQUESTCONTEXTSERVICE_CONTRACTID, &kNS_REQUESTCONTEXTSERVICE_CID }, #ifdef BUILD_NETWORK_INFO_SERVICE { NETWORKINFOSERVICE_CONTRACT_ID, &kNETWORKINFOSERVICE_CID }, #endif diff --git a/netwerk/ipc/NeckoParent.cpp b/netwerk/ipc/NeckoParent.cpp index 1e5254dafd00..1d97630965e3 100644 --- a/netwerk/ipc/NeckoParent.cpp +++ b/netwerk/ipc/NeckoParent.cpp @@ -26,6 +26,7 @@ #include "mozilla/net/DNSRequestParent.h" #include "mozilla/net/ChannelDiverterParent.h" #include "mozilla/net/IPCTransportProvider.h" +#include "mozilla/net/RequestContextService.h" #include "mozilla/net/TrackingDummyChannelParent.h" #ifdef MOZ_WEBRTC #include "mozilla/net/StunAddrsRequestParent.h" @@ -898,7 +899,7 @@ mozilla::ipc::IPCResult NeckoParent::RecvRequestContextLoadBegin(const uint64_t& rcid) { nsCOMPtr rcsvc = - do_GetService("@mozilla.org/network/request-context-service;1"); + RequestContextService::GetOrCreate(); if (!rcsvc) { return IPC_OK(); } @@ -915,7 +916,7 @@ mozilla::ipc::IPCResult NeckoParent::RecvRequestContextAfterDOMContentLoaded(const uint64_t& rcid) { nsCOMPtr rcsvc = - do_GetService("@mozilla.org/network/request-context-service;1"); + RequestContextService::GetOrCreate(); if (!rcsvc) { return IPC_OK(); } @@ -932,7 +933,7 @@ mozilla::ipc::IPCResult NeckoParent::RecvRemoveRequestContext(const uint64_t& rcid) { nsCOMPtr rcsvc = - do_GetService("@mozilla.org/network/request-context-service;1"); + RequestContextService::GetOrCreate(); if (!rcsvc) { return IPC_OK(); } diff --git a/netwerk/protocol/http/nsHttpHandler.cpp b/netwerk/protocol/http/nsHttpHandler.cpp index 319e4968a855..7db464841c26 100644 --- a/netwerk/protocol/http/nsHttpHandler.cpp +++ b/netwerk/protocol/http/nsHttpHandler.cpp @@ -61,6 +61,7 @@ #include "mozilla/net/NeckoChild.h" #include "mozilla/net/NeckoParent.h" +#include "mozilla/net/RequestContextService.h" #include "mozilla/ipc/URIUtils.h" #include "mozilla/Telemetry.h" #include "mozilla/Unused.h" @@ -510,8 +511,7 @@ nsHttpHandler::Init() rv = InitConnectionMgr(); if (NS_FAILED(rv)) return rv; - mRequestContextService = - do_GetService("@mozilla.org/network/request-context-service;1"); + mRequestContextService = RequestContextService::GetOrCreate(); #if defined(ANDROID) mProductSub.AssignLiteral(MOZILLA_UAVERSION);