From 9172f9d6167b6d8afa85a218bceead485a3d296d Mon Sep 17 00:00:00 2001 From: Ehsan Akhgari Date: Wed, 22 Aug 2018 00:37:28 -0400 Subject: [PATCH] Bug 1485494 - Remove the XPCOM registration for RedirectChannelRegistrar; r=mayhemer --- netwerk/base/RedirectChannelRegistrar.cpp | 25 +++++++++++++++++++ netwerk/base/RedirectChannelRegistrar.h | 7 ++++++ netwerk/base/moz.build | 1 + netwerk/base/nsNetUtil.cpp | 8 +++--- netwerk/build/nsNetCID.h | 11 -------- netwerk/build/nsNetModule.cpp | 10 +++----- netwerk/protocol/http/HttpChannelParent.cpp | 4 +-- .../http/HttpChannelParentListener.cpp | 10 ++++---- 8 files changed, 46 insertions(+), 30 deletions(-) diff --git a/netwerk/base/RedirectChannelRegistrar.cpp b/netwerk/base/RedirectChannelRegistrar.cpp index 17e26603a915..2cbe6f92a809 100644 --- a/netwerk/base/RedirectChannelRegistrar.cpp +++ b/netwerk/base/RedirectChannelRegistrar.cpp @@ -3,10 +3,15 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "RedirectChannelRegistrar.h" +#include "mozilla/StaticPtr.h" namespace mozilla { namespace net { +namespace { +StaticRefPtr gSingleton; +} + NS_IMPL_ISUPPORTS(RedirectChannelRegistrar, nsIRedirectChannelRegistrar) RedirectChannelRegistrar::RedirectChannelRegistrar() @@ -15,6 +20,26 @@ RedirectChannelRegistrar::RedirectChannelRegistrar() , mId(1) , mLock("RedirectChannelRegistrar") { + MOZ_ASSERT(!gSingleton); +} + +// static +already_AddRefed +RedirectChannelRegistrar::GetOrCreate() +{ + MOZ_ASSERT(NS_IsMainThread()); + if (!gSingleton) { + gSingleton = new RedirectChannelRegistrar(); + } + return do_AddRef(gSingleton); +} + +// static +void +RedirectChannelRegistrar::Shutdown() +{ + MOZ_ASSERT(NS_IsMainThread()); + gSingleton = nullptr; } NS_IMETHODIMP diff --git a/netwerk/base/RedirectChannelRegistrar.h b/netwerk/base/RedirectChannelRegistrar.h index 2bf8c5a6c227..438123b5e00e 100644 --- a/netwerk/base/RedirectChannelRegistrar.h +++ b/netwerk/base/RedirectChannelRegistrar.h @@ -26,6 +26,13 @@ class RedirectChannelRegistrar final : public nsIRedirectChannelRegistrar private: ~RedirectChannelRegistrar() = default; +public: + // Singleton accessor + static already_AddRefed GetOrCreate(); + + // Cleanup the singleton instance on shutdown + static void Shutdown(); + protected: typedef nsInterfaceHashtable ChannelHashtable; diff --git a/netwerk/base/moz.build b/netwerk/base/moz.build index f15746302ea0..5e972b076929 100644 --- a/netwerk/base/moz.build +++ b/netwerk/base/moz.build @@ -176,6 +176,7 @@ EXPORTS.mozilla.net += [ 'MemoryDownloader.h', 'PartiallySeekableInputStream.h', 'Predictor.h', + 'RedirectChannelRegistrar.h', 'ReferrerPolicy.h', 'SimpleChannelParent.h', 'TCPFastOpen.h', diff --git a/netwerk/base/nsNetUtil.cpp b/netwerk/base/nsNetUtil.cpp index 98cbd6b3b412..76a8b281a624 100644 --- a/netwerk/base/nsNetUtil.cpp +++ b/netwerk/base/nsNetUtil.cpp @@ -47,7 +47,7 @@ #include "nsIPrivateBrowsingChannel.h" #include "nsIPropertyBag2.h" #include "nsIProtocolProxyService.h" -#include "nsIRedirectChannelRegistrar.h" +#include "mozilla/net/RedirectChannelRegistrar.h" #include "nsIRequestObserverProxy.h" #include "nsIScriptSecurityManager.h" #include "nsISensitiveInfoHiddenURI.h" @@ -2757,11 +2757,9 @@ NS_LinkRedirectChannels(uint32_t channelId, nsIParentChannel *parentChannel, nsIChannel **_result) { - nsresult rv; - nsCOMPtr registrar = - do_GetService("@mozilla.org/redirectchannelregistrar;1", &rv); - NS_ENSURE_SUCCESS(rv, rv); + RedirectChannelRegistrar::GetOrCreate(); + MOZ_ASSERT(registrar); return registrar->LinkChannels(channelId, parentChannel, diff --git a/netwerk/build/nsNetCID.h b/netwerk/build/nsNetCID.h index dfdad224afe3..fed218d5a04c 100644 --- a/netwerk/build/nsNetCID.h +++ b/netwerk/build/nsNetCID.h @@ -474,17 +474,6 @@ #define NS_URICLASSIFIERSERVICE_CONTRACTID \ "@mozilla.org/uriclassifierservice" -// Redirect channel registrar used for redirect to various protocols -#define NS_REDIRECTCHANNELREGISTRAR_CONTRACTID \ - "@mozilla.org/redirectchannelregistrar;1" -#define NS_REDIRECTCHANNELREGISTRAR_CID \ -{ /* {b69043a6-8929-4d60-8d17-a27e44a8393e} */ \ - 0xb69043a6, \ - 0x8929, \ - 0x4d60, \ - { 0x8d, 0x17, 0xa2, 0x7e, 0x44, 0xa8, 0x39, 0x3e } \ -} - // service implementing nsINetworkPredictor #define NS_NETWORKPREDICTOR_CONTRACTID \ "@mozilla.org/network/predictor;1" diff --git a/netwerk/build/nsNetModule.cpp b/netwerk/build/nsNetModule.cpp index 9aac5c67891b..f0e1cab622bf 100644 --- a/netwerk/build/nsNetModule.cpp +++ b/netwerk/build/nsNetModule.cpp @@ -39,6 +39,7 @@ #include "Predictor.h" #include "nsIThreadPool.h" #include "mozilla/net/NeckoChild.h" +#include "RedirectChannelRegistrar.h" #include "nsNetCID.h" @@ -131,10 +132,6 @@ NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsEffectiveTLDService, Init) #include "nsSerializationHelper.h" NS_GENERIC_FACTORY_CONSTRUCTOR(nsSerializationHelper) -#include "RedirectChannelRegistrar.h" -typedef mozilla::net::RedirectChannelRegistrar RedirectChannelRegistrar; -NS_GENERIC_FACTORY_CONSTRUCTOR(RedirectChannelRegistrar) - #include "CacheStorageService.h" typedef mozilla::net::CacheStorageService CacheStorageService; NS_GENERIC_FACTORY_CONSTRUCTOR(CacheStorageService) @@ -652,6 +649,8 @@ static void nsNetShutdown() mozilla::net::Http2CompressionCleanup(); + mozilla::net::RedirectChannelRegistrar::Shutdown(); + delete gNetSniffers; gNetSniffers = nullptr; delete gDataSniffers; @@ -768,7 +767,6 @@ NS_DEFINE_NAMED_CID(NS_NETWORK_LINK_SERVICE_CID); NS_DEFINE_NAMED_CID(NS_NETWORK_LINK_SERVICE_CID); #endif NS_DEFINE_NAMED_CID(NS_SERIALIZATION_HELPER_CID); -NS_DEFINE_NAMED_CID(NS_REDIRECTCHANNELREGISTRAR_CID); NS_DEFINE_NAMED_CID(NS_CACHE_STORAGE_SERVICE_CID); NS_DEFINE_NAMED_CID(NS_NSILOADCONTEXTINFOFACTORY_CID); NS_DEFINE_NAMED_CID(NS_NETWORKPREDICTOR_CID); @@ -893,7 +891,6 @@ static const mozilla::Module::CIDEntry kNeckoCIDs[] = { { &kNS_NETWORK_LINK_SERVICE_CID, false, nullptr, nsNotifyAddrListenerConstructor }, #endif { &kNS_SERIALIZATION_HELPER_CID, false, nullptr, nsSerializationHelperConstructor }, - { &kNS_REDIRECTCHANNELREGISTRAR_CID, false, nullptr, RedirectChannelRegistrarConstructor }, { &kNS_CACHE_STORAGE_SERVICE_CID, false, nullptr, CacheStorageServiceConstructor }, { &kNS_NSILOADCONTEXTINFOFACTORY_CID, false, nullptr, LoadContextInfoFactoryConstructor }, { &kNS_NETWORKPREDICTOR_CID, false, nullptr, mozilla::net::Predictor::Create }, @@ -1017,7 +1014,6 @@ static const mozilla::Module::ContractIDEntry kNeckoContracts[] = { { NS_NETWORK_LINK_SERVICE_CONTRACTID, &kNS_NETWORK_LINK_SERVICE_CID }, #endif { NS_SERIALIZATION_HELPER_CONTRACTID, &kNS_SERIALIZATION_HELPER_CID }, - { NS_REDIRECTCHANNELREGISTRAR_CONTRACTID, &kNS_REDIRECTCHANNELREGISTRAR_CID }, { NS_CACHE_STORAGE_SERVICE_CONTRACTID, &kNS_CACHE_STORAGE_SERVICE_CID }, { NS_CACHE_STORAGE_SERVICE_CONTRACTID2, &kNS_CACHE_STORAGE_SERVICE_CID }, { NS_NSILOADCONTEXTINFOFACTORY_CONTRACTID, &kNS_NSILOADCONTEXTINFOFACTORY_CID }, diff --git a/netwerk/protocol/http/HttpChannelParent.cpp b/netwerk/protocol/http/HttpChannelParent.cpp index 347570deb112..21b2ace5dff2 100644 --- a/netwerk/protocol/http/HttpChannelParent.cpp +++ b/netwerk/protocol/http/HttpChannelParent.cpp @@ -45,7 +45,7 @@ #include "nsCORSListenerProxy.h" #include "nsIIPCSerializableInputStream.h" #include "nsIPrompt.h" -#include "nsIRedirectChannelRegistrar.h" +#include "mozilla/net/RedirectChannelRegistrar.h" #include "nsIWindowWatcher.h" #include "nsIDocument.h" #include "nsStreamUtils.h" @@ -972,7 +972,7 @@ HttpChannelParent::RecvRedirect2Verify(const nsresult& aResult, // Wait for background channel ready on target channel nsCOMPtr redirectReg = - do_GetService(NS_REDIRECTCHANNELREGISTRAR_CONTRACTID); + RedirectChannelRegistrar::GetOrCreate(); MOZ_ASSERT(redirectReg); nsCOMPtr redirectParentChannel; diff --git a/netwerk/protocol/http/HttpChannelParentListener.cpp b/netwerk/protocol/http/HttpChannelParentListener.cpp index 7be910841fb6..0ab01cb98887 100644 --- a/netwerk/protocol/http/HttpChannelParentListener.cpp +++ b/netwerk/protocol/http/HttpChannelParentListener.cpp @@ -15,7 +15,7 @@ #include "nsIAuthPrompt.h" #include "nsIAuthPrompt2.h" #include "nsIHttpHeaderVisitor.h" -#include "nsIRedirectChannelRegistrar.h" +#include "mozilla/net/RedirectChannelRegistrar.h" #include "nsIPromptFactory.h" #include "nsIWindowWatcher.h" #include "nsQueryObject.h" @@ -179,8 +179,8 @@ HttpChannelParentListener::AsyncOnChannelRedirect( // Register the new channel and obtain id for it nsCOMPtr registrar = - do_GetService("@mozilla.org/redirectchannelregistrar;1", &rv); - NS_ENSURE_SUCCESS(rv, rv); + RedirectChannelRegistrar::GetOrCreate(); + MOZ_ASSERT(registrar); rv = registrar->RegisterChannel(newChannel, &mRedirectChannelId); NS_ENSURE_SUCCESS(rv, rv); @@ -208,8 +208,8 @@ HttpChannelParentListener::OnRedirectResult(bool succeeded) nsCOMPtr redirectChannel; if (mRedirectChannelId) { nsCOMPtr registrar = - do_GetService("@mozilla.org/redirectchannelregistrar;1", &rv); - NS_ENSURE_SUCCESS(rv, rv); + RedirectChannelRegistrar::GetOrCreate(); + MOZ_ASSERT(registrar); rv = registrar->GetParentChannel(mRedirectChannelId, getter_AddRefs(redirectChannel));