Bug 1501404 - Part 1: Remove the XPCOM registration for RequestContextService r=valentin
authorEhsan Akhgari <ehsan@mozilla.com>
Tue, 23 Oct 2018 20:07:17 +0000
changeset 490999 81f3d1e42395241f97ff12aa489ce7ee6153b5c5
parent 490998 9d7dcb18cd7296847201c8cf8fcfa2674ebe2450
child 491000 8711f0c4d51ee1d411eb273c0252417316707746
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersvalentin
bugs1501404
milestone65.0a1
Bug 1501404 - Part 1: Remove the XPCOM registration for RequestContextService r=valentin Differential Revision: https://phabricator.services.mozilla.com/D9570
dom/base/nsDocument.cpp
netwerk/base/RequestContextService.cpp
netwerk/base/RequestContextService.h
netwerk/base/moz.build
netwerk/base/nsLoadGroup.cpp
netwerk/build/nsNetCID.h
netwerk/build/nsNetModule.cpp
netwerk/ipc/NeckoParent.cpp
netwerk/protocol/http/nsHttpHandler.cpp
--- a/dom/base/nsDocument.cpp
+++ b/dom/base/nsDocument.cpp
@@ -277,16 +277,17 @@
 #include "nsIURIMutator.h"
 #include "mozilla/DocumentStyleRootIterator.h"
 #include "mozilla/PendingFullscreenEvent.h"
 #include "mozilla/RestyleManager.h"
 #include "mozilla/ClearOnShutdown.h"
 #include "nsHTMLTags.h"
 #include "NodeUbiReporting.h"
 #include "nsICookieService.h"
+#include "mozilla/net/RequestContextService.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
 typedef nsTArray<Link*> LinkArray;
 
 static LazyLogModule gDocumentLeakPRLog("DocumentLeak");
 static LazyLogModule gCspPRLog("CSP");
@@ -2335,17 +2336,17 @@ nsIDocument::ResetToURI(nsIURI* aURI,
 
     // XXXbz what does "just fine" mean exactly?  And given that there
     // is no nsDocShell::SetDocument, what is this talking about?
 
     if (IsContentDocument()) {
       // Inform the associated request context about this load start so
       // any of its internal load progress flags gets reset.
       nsCOMPtr<nsIRequestContextService> rcsvc =
-        do_GetService("@mozilla.org/network/request-context-service;1");
+        mozilla::net::RequestContextService::GetOrCreate();
       if (rcsvc) {
         nsCOMPtr<nsIRequestContext> rc;
         rcsvc->GetRequestContextFromLoadGroup(aLoadGroup, getter_AddRefs(rc));
         if (rc) {
           rc->BeginLoad();
         }
       }
     }
--- a/netwerk/base/RequestContextService.cpp
+++ b/netwerk/base/RequestContextService.cpp
@@ -10,33 +10,37 @@
 #include "nsIDocumentLoader.h"
 #include "nsIObserverService.h"
 #include "nsIXULRuntime.h"
 #include "nsServiceManagerUtils.h"
 #include "nsThreadUtils.h"
 #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"
 #include "mozilla/net/NeckoCommon.h"
 #include "mozilla/net/PSpdyPush.h"
 
 #include "../protocol/http/nsHttpHandler.h"
 
 namespace mozilla {
 namespace net {
 
 LazyLogModule gRequestContextLog("RequestContext");
 #undef LOG
 #define LOG(args) MOZ_LOG(gRequestContextLog, LogLevel::Info, args)
 
+static StaticRefPtr<RequestContextService> gSingleton;
+
 // This is used to prevent adding tail pending requests after shutdown
 static bool sShutdown = false;
 
 // nsIRequestContext
 class RequestContext final : public nsIRequestContext
                            , public nsITimerCallback
 {
 public:
@@ -513,29 +517,33 @@ RequestContextService::Shutdown()
   // shutdown.
   for (auto iter = mTable.Iter(); !iter.Done(); iter.Next()) {
     iter.Data()->CancelTailPendingRequests(NS_ERROR_ABORT);
   }
   mTable.Clear();
   sShutdown = true;
 }
 
-/* static */ nsresult
-RequestContextService::Create(nsISupports *aOuter, const nsIID& aIID, void **aResult)
+/* static */ already_AddRefed<nsIRequestContextService>
+RequestContextService::GetOrCreate()
 {
   MOZ_ASSERT(NS_IsMainThread());
-  if (aOuter != nullptr) {
-    return NS_ERROR_NO_AGGREGATION;
+
+  RefPtr<RequestContextService> svc;
+  if (gSingleton) {
+    svc = gSingleton;
+  } else {
+    svc = new RequestContextService();
+    nsresult rv = svc->Init();
+    NS_ENSURE_SUCCESS(rv, nullptr);
+    gSingleton = svc;
+    ClearOnShutdown(&gSingleton);
   }
 
-  RefPtr<RequestContextService> svc = new RequestContextService();
-  nsresult rv = svc->Init();
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  return svc->QueryInterface(aIID, aResult);
+  return svc.forget();
 }
 
 NS_IMETHODIMP
 RequestContextService::GetRequestContext(const uint64_t rcID, nsIRequestContext **rc)
 {
   MOZ_ASSERT(NS_IsMainThread());
   NS_ENSURE_ARG_POINTER(rc);
   *rc = nullptr;
--- a/netwerk/base/RequestContextService.h
+++ b/netwerk/base/RequestContextService.h
@@ -19,24 +19,24 @@ class RequestContextService final
   : public nsIRequestContextService
   , public nsIObserver
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIREQUESTCONTEXTSERVICE
   NS_DECL_NSIOBSERVER
 
+  static already_AddRefed<nsIRequestContextService> 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;
 
   nsInterfaceHashtable<nsUint64HashKey, nsIRequestContext> mTable;
   uint32_t mRCIDNamespace;
   uint32_t mNextRCID;
 };
 
--- a/netwerk/base/moz.build
+++ b/netwerk/base/moz.build
@@ -172,16 +172,17 @@ EXPORTS.mozilla.net += [
     'Dashboard.h',
     'DashboardTypes.h',
     'IOActivityMonitor.h',
     'MemoryDownloader.h',
     'PartiallySeekableInputStream.h',
     'Predictor.h',
     'RedirectChannelRegistrar.h',
     'ReferrerPolicy.h',
+    'RequestContextService.h',
     'SimpleChannelParent.h',
     'TCPFastOpen.h',
 ]
 
 UNIFIED_SOURCES += [
     'ArrayBufferInputStream.cpp',
     'BackgroundFileSaver.cpp',
     'CaptivePortalService.cpp',
--- a/netwerk/base/nsLoadGroup.cpp
+++ b/netwerk/base/nsLoadGroup.cpp
@@ -16,16 +16,17 @@
 #include "nsTArray.h"
 #include "mozilla/Telemetry.h"
 #include "nsITimedChannel.h"
 #include "nsIInterfaceRequestor.h"
 #include "nsIRequestObserver.h"
 #include "nsIRequestContext.h"
 #include "CacheObserver.h"
 #include "MainThreadUtils.h"
+#include "RequestContextService.h"
 #include "mozilla/Unused.h"
 
 namespace mozilla {
 namespace net {
 
 //
 // Log module for nsILoadGroup logging...
 //
@@ -1051,17 +1052,17 @@ nsresult nsLoadGroup::MergeDefaultLoadFl
         rv = aRequest->SetLoadFlags(flags);
     }
     outFlags = flags;
     return rv;
 }
 
 nsresult nsLoadGroup::Init()
 {
-    mRequestContextService = do_GetService("@mozilla.org/network/request-context-service;1");
+    mRequestContextService = RequestContextService::GetOrCreate();
     if (mRequestContextService) {
         Unused << mRequestContextService->NewRequestContext(getter_AddRefs(mRequestContext));
     }
 
     return NS_OK;
 }
 
 } // namespace net
--- a/netwerk/build/nsNetCID.h
+++ b/netwerk/build/nsNetCID.h
@@ -480,27 +480,16 @@
 #define NS_CAPTIVEPORTAL_CID \
 { /* bdbe0555-fc3d-4f7b-9205-c309ceb2d641 */ \
     0xbdbe0555, \
     0xfc3d, \
     0x4f7b, \
   { 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
  */
 
 // service implementing nsICacheService.
 #define NS_CACHESERVICE_CONTRACTID \
     "@mozilla.org/network/cache-service;1"
 #define NS_CACHESERVICE_CID                          \
--- a/netwerk/build/nsNetModule.cpp
+++ b/netwerk/build/nsNetModule.cpp
@@ -143,20 +143,16 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(LoadConte
 #include "mozilla/net/CaptivePortalService.h"
 namespace mozilla {
 namespace net {
   NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsICaptivePortalService,
     CaptivePortalService::GetSingleton)
 } // namespace net
 } // namespace mozilla
 
-#include "RequestContextService.h"
-typedef mozilla::net::RequestContextService RequestContextService;
-NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(RequestContextService, Init)
-
 ///////////////////////////////////////////////////////////////////////////////
 
 extern nsresult
 net_NewIncrementalDownload(nsISupports *, const nsIID &, void **);
 
 #define NS_INCREMENTALDOWNLOAD_CID \
 { /* a62af1ba-79b3-4896-8aaf-b148bfce4280 */         \
     0xa62af1ba,                                      \
@@ -749,17 +745,16 @@ NS_DEFINE_NAMED_CID(NS_NETWORK_LINK_SERV
 #elif defined(XP_LINUX)
 NS_DEFINE_NAMED_CID(NS_NETWORK_LINK_SERVICE_CID);
 #endif
 NS_DEFINE_NAMED_CID(NS_SERIALIZATION_HELPER_CID);
 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
 
 static const mozilla::Module::CIDEntry kNeckoCIDs[] = {
     // clang-format off
     { &kNS_IOSERVICE_CID, false, nullptr, nsIOServiceConstructor },
     { &kNS_STREAMTRANSPORTSERVICE_CID, false, nullptr, nsStreamTransportServiceConstructor },
@@ -867,17 +862,16 @@ static const mozilla::Module::CIDEntry k
 #elif defined(XP_LINUX)
     { &kNS_NETWORK_LINK_SERVICE_CID, false, nullptr, nsNotifyAddrListenerConstructor },
 #endif
     { &kNS_SERIALIZATION_HELPER_CID, false, nullptr, nsSerializationHelperConstructor },
     { &kNS_CACHE_STORAGE_SERVICE_CID, false, nullptr, CacheStorageServiceConstructor },
     { &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
     { nullptr }
     // clang-format on
 };
 
 static const mozilla::Module::ContractIDEntry kNeckoContracts[] = {
@@ -985,17 +979,16 @@ static const mozilla::Module::ContractID
     { NS_NETWORK_LINK_SERVICE_CONTRACTID, &kNS_NETWORK_LINK_SERVICE_CID },
 #endif
     { NS_SERIALIZATION_HELPER_CONTRACTID, &kNS_SERIALIZATION_HELPER_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 },
     { 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
     { nullptr }
     // clang-format on
 };
 
 static const mozilla::Module kNeckoModule = {
--- a/netwerk/ipc/NeckoParent.cpp
+++ b/netwerk/ipc/NeckoParent.cpp
@@ -21,16 +21,17 @@
 #include "mozilla/net/DataChannelParent.h"
 #include "mozilla/net/SimpleChannelParent.h"
 #include "mozilla/net/AltDataOutputStreamParent.h"
 #include "mozilla/Unused.h"
 #include "mozilla/net/FileChannelParent.h"
 #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"
 #endif
 #include "mozilla/dom/ChromeUtils.h"
 #include "mozilla/dom/ContentParent.h"
 #include "mozilla/dom/TabContext.h"
 #include "mozilla/dom/TabParent.h"
@@ -893,51 +894,51 @@ NeckoParent::RecvPredReset()
   predictor->Reset();
   return IPC_OK();
 }
 
 mozilla::ipc::IPCResult
 NeckoParent::RecvRequestContextLoadBegin(const uint64_t& rcid)
 {
   nsCOMPtr<nsIRequestContextService> rcsvc =
-    do_GetService("@mozilla.org/network/request-context-service;1");
+    RequestContextService::GetOrCreate();
   if (!rcsvc) {
     return IPC_OK();
   }
   nsCOMPtr<nsIRequestContext> rc;
   rcsvc->GetRequestContext(rcid, getter_AddRefs(rc));
   if (rc) {
     rc->BeginLoad();
   }
 
   return IPC_OK();
 }
 
 mozilla::ipc::IPCResult
 NeckoParent::RecvRequestContextAfterDOMContentLoaded(const uint64_t& rcid)
 {
   nsCOMPtr<nsIRequestContextService> rcsvc =
-    do_GetService("@mozilla.org/network/request-context-service;1");
+    RequestContextService::GetOrCreate();
   if (!rcsvc) {
     return IPC_OK();
   }
   nsCOMPtr<nsIRequestContext> rc;
   rcsvc->GetRequestContext(rcid, getter_AddRefs(rc));
   if (rc) {
     rc->DOMContentLoaded();
   }
 
   return IPC_OK();
 }
 
 mozilla::ipc::IPCResult
 NeckoParent::RecvRemoveRequestContext(const uint64_t& rcid)
 {
   nsCOMPtr<nsIRequestContextService> rcsvc =
-    do_GetService("@mozilla.org/network/request-context-service;1");
+    RequestContextService::GetOrCreate();
   if (!rcsvc) {
     return IPC_OK();
   }
 
   rcsvc->RemoveRequestContext(rcid);
 
   return IPC_OK();
 }
--- a/netwerk/protocol/http/nsHttpHandler.cpp
+++ b/netwerk/protocol/http/nsHttpHandler.cpp
@@ -56,16 +56,17 @@
 #include "nsISupportsPrimitives.h"
 #include "nsIXULRuntime.h"
 #include "nsCharSeparatedTokenizer.h"
 #include "nsRFPService.h"
 #include "rust-helper/src/helper.h"
 
 #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"
 #include "mozilla/BasePrincipal.h"
 
 #include "mozilla/dom/ContentParent.h"
 #include "mozilla/dom/Navigator.h"
 
@@ -505,18 +506,17 @@ nsHttpHandler::Init()
     }
 
     mSessionStartTime = NowInSeconds();
     mHandlerActive = true;
 
     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);
 #else
     mProductSub.AssignLiteral(LEGACY_UA_GECKO_TRAIL);
 #endif
 
 #if DEBUG