Bug 1501404 - Part 5: Remove the XPCOM registration for nsSocketProviderService r=valentin
authorEhsan Akhgari <ehsan@mozilla.com>
Tue, 23 Oct 2018 20:07:25 +0000
changeset 491003 79795a6721ccdafcaf4f68898240568047cdd18f
parent 491002 361c06422c19ac2b90748c54b76ec6e8d7053960
child 491004 1766ed669623dd83d791586c65a65a5a7d1097c0
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersvalentin
bugs1501404
milestone65.0a1
Bug 1501404 - Part 5: Remove the XPCOM registration for nsSocketProviderService r=valentin Depends on D9573 Differential Revision: https://phabricator.services.mozilla.com/D9574
netwerk/base/nsSocketTransport2.cpp
netwerk/build/nsNetCID.h
netwerk/build/nsNetModule.cpp
netwerk/protocol/http/TunnelUtils.cpp
netwerk/protocol/http/nsHttpHandler.cpp
netwerk/socket/moz.build
netwerk/socket/nsSocketProviderService.cpp
netwerk/socket/nsSocketProviderService.h
--- a/netwerk/base/nsSocketTransport2.cpp
+++ b/netwerk/base/nsSocketTransport2.cpp
@@ -20,17 +20,17 @@
 #include "nsCOMPtr.h"
 #include "plstr.h"
 #include "prerr.h"
 #include "IOActivityMonitor.h"
 #include "NSSErrorsService.h"
 #include "mozilla/dom/ToJSValue.h"
 #include "mozilla/net/NeckoChild.h"
 #include "nsThreadUtils.h"
-#include "nsISocketProviderService.h"
+#include "nsSocketProviderService.h"
 #include "nsISocketProvider.h"
 #include "nsISSLSocketControl.h"
 #include "nsIPipe.h"
 #include "nsIClassInfoImpl.h"
 #include "nsURLHelper.h"
 #include "nsIDNSService.h"
 #include "nsIDNSRecord.h"
 #include "nsIDNSByTypeRecord.h"
@@ -58,17 +58,16 @@
 
 #define SUCCESSFUL_CONNECTING_TO_IPV4_ADDRESS 0
 #define UNSUCCESSFUL_CONNECTING_TO_IPV4_ADDRESS 1
 #define SUCCESSFUL_CONNECTING_TO_IPV6_ADDRESS 2
 #define UNSUCCESSFUL_CONNECTING_TO_IPV6_ADDRESS 3
 
 //-----------------------------------------------------------------------------
 
-static NS_DEFINE_CID(kSocketProviderServiceCID, NS_SOCKETPROVIDERSERVICE_CID);
 static NS_DEFINE_CID(kDNSServiceCID, NS_DNSSERVICE_CID);
 
 //-----------------------------------------------------------------------------
 
 namespace mozilla {
 namespace net {
 
 class nsSocketEvent : public Runnable
@@ -881,18 +880,17 @@ nsSocketTransport::Init(const char **typ
     mTypeCount = typeCount + (proxyType != nullptr);
     if (!mTypeCount)
         return NS_OK;
 
     // if we have socket types, then the socket provider service had
     // better exist!
     nsresult rv;
     nsCOMPtr<nsISocketProviderService> spserv =
-        do_GetService(kSocketProviderServiceCID, &rv);
-    if (NS_FAILED(rv)) return rv;
+        nsSocketProviderService::GetOrCreate();
 
     mTypes = (char **) malloc(mTypeCount * sizeof(char *));
     if (!mTypes)
         return NS_ERROR_OUT_OF_MEMORY;
 
     // now verify that each socket type has a registered socket provider.
     for (uint32_t i = 0, type = 0; i < mTypeCount; ++i) {
         // store socket types
@@ -1206,18 +1204,17 @@ nsSocketTransport::BuildSocket(PRFileDes
 #if defined(XP_UNIX)
         MOZ_ASSERT(!mNetAddrIsSet || mNetAddr.raw.family != AF_LOCAL,
                    "Unix domain sockets can't be used with socket types");
 #endif
 
         fd = nullptr;
 
         nsCOMPtr<nsISocketProviderService> spserv =
-            do_GetService(kSocketProviderServiceCID, &rv);
-        if (NS_FAILED(rv)) return rv;
+            nsSocketProviderService::GetOrCreate();
 
         // by setting host to mOriginHost, instead of mHost we send the
         // SocketProvider (e.g. PSM) the origin hostname but can still do DNS
         // on an explicit alternate service host name
         const char *host       = mOriginHost.get();
         int32_t     port       = (int32_t) mOriginPort;
         nsCOMPtr<nsIProxyInfo> proxyInfo = mProxyInfo;
         uint32_t    controlFlags = 0;
--- a/netwerk/build/nsNetCID.h
+++ b/netwerk/build/nsNetCID.h
@@ -748,26 +748,16 @@
 
 #define NS_MIMEHEADERPARAM_CONTRACTID "@mozilla.org/network/mime-hdrparam;1"
 
 
 /******************************************************************************
  * netwerk/socket classes
  */
 
-#define NS_SOCKETPROVIDERSERVICE_CONTRACTID \
-    "@mozilla.org/network/socket-provider-service;1"
-#define NS_SOCKETPROVIDERSERVICE_CID                   \
-{ /* ed394ba0-5472-11d3-bbc8-0000861d1237 */           \
-    0xed394ba0,                                        \
-    0x5472,                                            \
-    0x11d3,                                            \
-    { 0xbb, 0xc8, 0x00, 0x00, 0x86, 0x1d, 0x12, 0x37 } \
-}
-
 #define NS_SOCKSSOCKETPROVIDER_CID                     \
 { /* 8dbe7246-1dd2-11b2-9b8f-b9a849e4403a */           \
     0x8dbe7246,                                        \
     0x1dd2,                                            \
     0x11b2,                                            \
     { 0x9b, 0x8f, 0xb9, 0xa8, 0x49, 0xe4, 0x40, 0x3a } \
 }
 
--- a/netwerk/build/nsNetModule.cpp
+++ b/netwerk/build/nsNetModule.cpp
@@ -10,17 +10,16 @@
 #include "base/basictypes.h"
 
 #include "nsCOMPtr.h"
 #include "nsIClassInfoImpl.h"
 #include "mozilla/ModuleUtils.h"
 #include "nsIComponentManager.h"
 #include "nsIServiceManager.h"
 #include "nsICategoryManager.h"
-#include "nsSocketProviderService.h"
 #include "nscore.h"
 #include "nsSimpleURI.h"
 #include "nsSimpleNestedURI.h"
 #include "nsLoadGroup.h"
 #include "nsStreamLoader.h"
 #include "nsIncrementalStreamLoader.h"
 #include "nsFileStreams.h"
 #include "nsBufferedStreams.h"
@@ -641,17 +640,16 @@ static void nsNetShutdown()
 }
 
 NS_DEFINE_NAMED_CID(NS_IOSERVICE_CID);
 NS_DEFINE_NAMED_CID(NS_STREAMTRANSPORTSERVICE_CID);
 NS_DEFINE_NAMED_CID(NS_SOCKETTRANSPORTSERVICE_CID);
 NS_DEFINE_NAMED_CID(NS_SERVERSOCKET_CID);
 NS_DEFINE_NAMED_CID(NS_TLSSERVERSOCKET_CID);
 NS_DEFINE_NAMED_CID(NS_UDPSOCKET_CID);
-NS_DEFINE_NAMED_CID(NS_SOCKETPROVIDERSERVICE_CID);
 NS_DEFINE_NAMED_CID(NS_DNSSERVICE_CID);
 NS_DEFINE_NAMED_CID(NS_IDNSERVICE_CID);
 NS_DEFINE_NAMED_CID(NS_EFFECTIVETLDSERVICE_CID);
 NS_DEFINE_NAMED_CID(NS_SIMPLEURI_CID);
 NS_DEFINE_NAMED_CID(NS_SIMPLEURIMUTATOR_CID);
 NS_DEFINE_NAMED_CID(NS_SIMPLENESTEDURI_CID);
 NS_DEFINE_NAMED_CID(NS_SIMPLENESTEDURIMUTATOR_CID);
 NS_DEFINE_NAMED_CID(NS_ASYNCSTREAMCOPIER_CID);
@@ -753,17 +751,16 @@ NS_DEFINE_NAMED_CID(NETWORKINFOSERVICE_C
 static const mozilla::Module::CIDEntry kNeckoCIDs[] = {
     // clang-format off
     { &kNS_IOSERVICE_CID, false, nullptr, nsIOServiceConstructor },
     { &kNS_STREAMTRANSPORTSERVICE_CID, false, nullptr, nsStreamTransportServiceConstructor },
     { &kNS_SOCKETTRANSPORTSERVICE_CID, false, nullptr, nsSocketTransportServiceConstructor },
     { &kNS_SERVERSOCKET_CID, false, nullptr, nsServerSocketConstructor },
     { &kNS_TLSSERVERSOCKET_CID, false, nullptr, TLSServerSocketConstructor },
     { &kNS_UDPSOCKET_CID, false, nullptr, nsUDPSocketConstructor },
-    { &kNS_SOCKETPROVIDERSERVICE_CID, false, nullptr, nsSocketProviderService::Create },
     { &kNS_DNSSERVICE_CID, false, nullptr, nsIDNSServiceConstructor },
     { &kNS_IDNSERVICE_CID, false, nullptr, nsIDNServiceConstructor },
     { &kNS_EFFECTIVETLDSERVICE_CID, false, nullptr, nsEffectiveTLDServiceConstructor },
     { &kNS_SIMPLEURI_CID, false, nullptr, nsSimpleURIMutatorConstructor }, // do_CreateInstance returns mutator
     { &kNS_SIMPLEURIMUTATOR_CID, false, nullptr, nsSimpleURIMutatorConstructor },
     { &kNS_SIMPLENESTEDURI_CID, false, nullptr, nsSimpleNestedURIMutatorConstructor }, // do_CreateInstance returns mutator
     { &kNS_SIMPLENESTEDURIMUTATOR_CID, false, nullptr, nsSimpleNestedURIMutatorConstructor },
     { &kNS_ASYNCSTREAMCOPIER_CID, false, nullptr, nsAsyncStreamCopierConstructor },
@@ -873,17 +870,16 @@ static const mozilla::Module::ContractID
     // clang-format off
     { NS_IOSERVICE_CONTRACTID, &kNS_IOSERVICE_CID },
     { NS_NETUTIL_CONTRACTID, &kNS_IOSERVICE_CID },
     { NS_STREAMTRANSPORTSERVICE_CONTRACTID, &kNS_STREAMTRANSPORTSERVICE_CID },
     { NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &kNS_SOCKETTRANSPORTSERVICE_CID },
     { NS_SERVERSOCKET_CONTRACTID, &kNS_SERVERSOCKET_CID },
     { NS_TLSSERVERSOCKET_CONTRACTID, &kNS_TLSSERVERSOCKET_CID },
     { NS_UDPSOCKET_CONTRACTID, &kNS_UDPSOCKET_CID },
-    { NS_SOCKETPROVIDERSERVICE_CONTRACTID, &kNS_SOCKETPROVIDERSERVICE_CID },
     { NS_DNSSERVICE_CONTRACTID, &kNS_DNSSERVICE_CID },
     { NS_IDNSERVICE_CONTRACTID, &kNS_IDNSERVICE_CID },
     { NS_EFFECTIVETLDSERVICE_CONTRACTID, &kNS_EFFECTIVETLDSERVICE_CID },
     { NS_SIMPLEURIMUTATOR_CONTRACTID, &kNS_SIMPLEURIMUTATOR_CID },
     { NS_ASYNCSTREAMCOPIER_CONTRACTID, &kNS_ASYNCSTREAMCOPIER_CID },
     { NS_INPUTSTREAMPUMP_CONTRACTID, &kNS_INPUTSTREAMPUMP_CID },
     { NS_INPUTSTREAMCHANNEL_CONTRACTID, &kNS_INPUTSTREAMCHANNEL_CID },
     { NS_STREAMLOADER_CONTRACTID, &kNS_STREAMLOADER_CID },
--- a/netwerk/protocol/http/TunnelUtils.cpp
+++ b/netwerk/protocol/http/TunnelUtils.cpp
@@ -8,17 +8,17 @@
 #include "HttpLog.h"
 
 #include "Http2Session.h"
 #include "nsHttp.h"
 #include "nsHttpHandler.h"
 #include "nsHttpRequestHead.h"
 #include "TCPFastOpen.h"
 #include "nsISocketProvider.h"
-#include "nsISocketProviderService.h"
+#include "nsSocketProviderService.h"
 #include "nsISSLSocketControl.h"
 #include "nsISocketTransport.h"
 #include "nsISupportsPriority.h"
 #include "nsNetAddr.h"
 #include "prerror.h"
 #include "prio.h"
 #include "TunnelUtils.h"
 #include "nsNetCID.h"
@@ -48,17 +48,17 @@ TLSFilterTransaction::TLSFilterTransacti
   , mReadSegmentBlocked(false)
   , mNudgeCounter(0)
 {
   MOZ_ASSERT(OnSocketThread(), "not on socket thread");
   LOG(("TLSFilterTransaction ctor %p\n", this));
 
   nsCOMPtr<nsISocketProvider> provider;
   nsCOMPtr<nsISocketProviderService> spserv =
-    do_GetService(NS_SOCKETPROVIDERSERVICE_CONTRACTID);
+    nsSocketProviderService::GetOrCreate();
 
   if (spserv) {
     spserv->GetSocketProvider("ssl", getter_AddRefs(provider));
   }
 
   // Install an NSPR layer to handle getpeername() with a failure. This is kind
   // of silly, but the default one used by the pipe asserts when called and the
   // nss code calls it to see if we are connected to a real socket or not.
--- a/netwerk/protocol/http/nsHttpHandler.cpp
+++ b/netwerk/protocol/http/nsHttpHandler.cpp
@@ -19,17 +19,17 @@
 #include "nsINetworkProperties.h"
 #include "nsIHttpChannel.h"
 #include "nsIStandardURL.h"
 #include "LoadContextInfo.h"
 #include "nsCategoryManagerUtils.h"
 #include "nsIPrefService.h"
 #include "nsIPrefBranch.h"
 #include "nsIPrefLocalizedString.h"
-#include "nsISocketProviderService.h"
+#include "nsSocketProviderService.h"
 #include "nsISocketProvider.h"
 #include "nsPrintfCString.h"
 #include "nsCOMPtr.h"
 #include "nsNetCID.h"
 #include "mozilla/ClearOnShutdown.h"
 #include "mozilla/Printf.h"
 #include "mozilla/Sprintf.h"
 #include "nsAsyncRedirectVerifyHelper.h"
@@ -1438,18 +1438,18 @@ nsHttpHandler::PrefsChanged(const char *
     if (PREF_CHANGED(HTTP_PREF("default-socket-type"))) {
         nsAutoCString sval;
         rv = Preferences::GetCString(HTTP_PREF("default-socket-type"), sval);
         if (NS_SUCCEEDED(rv)) {
             if (sval.IsEmpty())
                 mDefaultSocketType.SetIsVoid(true);
             else {
                 // verify that this socket type is actually valid
-                nsCOMPtr<nsISocketProviderService> sps(
-                        do_GetService(NS_SOCKETPROVIDERSERVICE_CONTRACTID));
+                nsCOMPtr<nsISocketProviderService> sps =
+                  nsSocketProviderService::GetOrCreate();
                 if (sps) {
                     nsCOMPtr<nsISocketProvider> sp;
                     rv = sps->GetSocketProvider(sval.get(), getter_AddRefs(sp));
                     if (NS_SUCCEEDED(rv)) {
                         // OK, this looks like a valid socket provider.
                         mDefaultSocketType.Assign(sval);
                     }
                 }
--- a/netwerk/socket/moz.build
+++ b/netwerk/socket/moz.build
@@ -13,16 +13,20 @@ XPIDL_SOURCES += [
 ]
 
 XPIDL_MODULE = 'necko_socket'
 
 LOCAL_INCLUDES += [
     '/netwerk/base',
 ]
 
+EXPORTS += [
+    'nsSocketProviderService.h',
+]
+
 UNIFIED_SOURCES += [
     'nsSocketProviderService.cpp',
     'nsSOCKSIOLayer.cpp',
     'nsSOCKSSocketProvider.cpp',
     'nsUDPSocketProvider.cpp',
 ]
 
 if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'windows':
--- a/netwerk/socket/nsSocketProviderService.cpp
+++ b/netwerk/socket/nsSocketProviderService.cpp
@@ -3,29 +3,43 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsString.h"
 #include "nsIServiceManager.h"
 #include "nsISocketProvider.h"
 #include "nsSocketProviderService.h"
 #include "nsError.h"
+#include "nsSSLSocketProvider.h"
+#include "nsTLSSocketProvider.h"
+#include "nsUDPSocketProvider.h"
+#include "mozilla/ClearOnShutdown.h"
+
+mozilla::StaticRefPtr<nsSocketProviderService> nsSocketProviderService::gSingleton;
 
 ////////////////////////////////////////////////////////////////////////////////
 
-nsresult
-nsSocketProviderService::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
+already_AddRefed<nsISocketProviderService>
+nsSocketProviderService::GetOrCreate()
 {
-  nsresult rv;
-  nsCOMPtr<nsISocketProviderService> inst = new nsSocketProviderService();
-  if (!inst)
-    rv = NS_ERROR_OUT_OF_MEMORY;
-  else
-    rv = inst->QueryInterface(aIID, aResult);
-  return rv;
+  RefPtr<nsSocketProviderService> inst;
+  if (gSingleton) {
+    inst = gSingleton;
+  } else {
+    inst = new nsSocketProviderService();
+    gSingleton = inst;
+    if (NS_IsMainThread()) {
+      mozilla::ClearOnShutdown(&gSingleton);
+    } else {
+      NS_DispatchToMainThread(NS_NewRunnableFunction(
+        "net::nsSocketProviderService::GetOrCreate",
+        []() -> void { mozilla::ClearOnShutdown(&gSingleton); }));
+    }
+  }
+  return inst.forget();
 }
 
 NS_IMPL_ISUPPORTS(nsSocketProviderService, nsISocketProviderService)
 
 ////////////////////////////////////////////////////////////////////////////////
 
 NS_IMETHODIMP
 nsSocketProviderService::GetSocketProvider(const char         *type,
--- a/netwerk/socket/nsSocketProviderService.h
+++ b/netwerk/socket/nsSocketProviderService.h
@@ -2,23 +2,26 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef nsSocketProviderService_h__
 #define nsSocketProviderService_h__
 
 #include "nsISocketProviderService.h"
+#include "mozilla/StaticPtr.h"
 
 class nsSocketProviderService : public nsISocketProviderService
 {
+  nsSocketProviderService() = default;
   virtual ~nsSocketProviderService() = default;
 
 public:
   NS_DECL_THREADSAFE_ISUPPORTS
   NS_DECL_NSISOCKETPROVIDERSERVICE
 
-  nsSocketProviderService() = default;
+  static already_AddRefed<nsISocketProviderService> GetOrCreate();
 
-  static nsresult Create(nsISupports *, REFNSIID aIID, void **aResult);
+private:
+  static mozilla::StaticRefPtr<nsSocketProviderService> gSingleton;
 };
 
 #endif /* nsSocketProviderService_h__ */