Bug 1362498 - cache some networking mozilla::services r=bagder draft
authorPatrick McManus <mcmanus@ducksong.com>
Fri, 05 May 2017 14:32:19 -0400
changeset 575477 fb92db8e59f3ea7a0dc242c17d97d9c2c8459038
parent 575104 2b6f6881a24a726364054aa69031927e2f3fc5fa
child 627922 8838c6adf52a93ad4a2daf18496d89330d016087
push id58061
push userbmo:mcmanus@ducksong.com
push dateWed, 10 May 2017 13:36:21 +0000
reviewersbagder
bugs1362498
milestone55.0a1
Bug 1362498 - cache some networking mozilla::services r=bagder MozReview-Commit-ID: G61lDIOKcm
netwerk/base/nsISocketTransportService.idl
netwerk/base/nsIStreamTransportService.idl
netwerk/base/nsPISocketTransportService.idl
netwerk/base/nsProtocolProxyService.cpp
netwerk/protocol/http/nsHttpChannel.cpp
netwerk/protocol/http/nsHttpConnectionMgr.cpp
netwerk/protocol/http/nsHttpTransaction.cpp
xpcom/build/ServiceList.h
xpcom/build/Services.cpp
--- a/netwerk/base/nsISocketTransportService.idl
+++ b/netwerk/base/nsISocketTransportService.idl
@@ -13,17 +13,17 @@ interface nsIRunnable;
 %{C++
 class nsASocketHandler;
 struct PRFileDesc;
 %}
 
 [ptr] native PRFileDescPtr(PRFileDesc);
 [ptr] native nsASocketHandlerPtr(nsASocketHandler);
 
-[scriptable, uuid(ad56b25f-e6bb-4db3-9f7b-5b7db33fd2b1)]
+[builtinclass, scriptable, uuid(ad56b25f-e6bb-4db3-9f7b-5b7db33fd2b1)]
 interface nsISocketTransportService : nsISupports 
 {
     /**
      * Creates a transport for a specified host and port.
      *
      * @param aSocketTypes
      *        array of socket type strings.  null if using default socket type.
      * @param aTypeCount
@@ -114,17 +114,17 @@ interface nsISocketTransportService : ns
      *        be attached
      *
      * NOTE: this function may only be called from an event dispatch on the
      *       socket thread.
      */
     [noscript] void notifyWhenCanAttachSocket(in nsIRunnable aEvent);
 };
 
-[scriptable, uuid(c5204623-5b58-4a16-8b2e-67c34dd02e3f)]
+[builtinclass, scriptable, uuid(c5204623-5b58-4a16-8b2e-67c34dd02e3f)]
 interface nsIRoutedSocketTransportService : nsISocketTransportService
 {
     // use this instead of createTransport when you have a transport
     // that distinguishes between origin and route (aka connection)
     nsISocketTransport createRoutedTransport([array, size_is(aTypeCount)]
                                              in string aSocketTypes,
                                              in unsigned long aTypeCount,
                                              in AUTF8String aHost, // origin
--- a/netwerk/base/nsIStreamTransportService.idl
+++ b/netwerk/base/nsIStreamTransportService.idl
@@ -10,17 +10,17 @@ interface nsIOutputStream;
 
 /**
  * This service read/writes a stream on a background thread.
  *
  * Use this service to transform any blocking stream (e.g., file stream)
  * into a fully asynchronous stream that can be read/written without 
  * blocking the main thread.
  */
-[scriptable, uuid(5e0adf7d-9785-45c3-a193-04f25a75da8f)]
+[builtinclass, scriptable, uuid(5e0adf7d-9785-45c3-a193-04f25a75da8f)]
 interface nsIStreamTransportService : nsISupports
 {
     /**
      * CreateInputTransport
      *
      * @param aStream
      *        The input stream that will be read on a background thread.
      *        This stream must implement "blocking" stream semantics.
--- a/netwerk/base/nsPISocketTransportService.idl
+++ b/netwerk/base/nsPISocketTransportService.idl
@@ -5,17 +5,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsISocketTransportService.idl"
 
 /**
  * This is a private interface used by the internals of the networking library.
  * It will never be frozen.  Do not use it in external code.
  */
-[scriptable, uuid(18f73bf1-b35b-4b7b-aa9a-11bcbdbc389c)]
+[builtinclass, scriptable, uuid(18f73bf1-b35b-4b7b-aa9a-11bcbdbc389c)]
 
 interface nsPISocketTransportService : nsIRoutedSocketTransportService
 {
   /**
    * init/shutdown routines.
    */
   void init();
   void shutdown(in bool aXpcomShutdown);
--- a/netwerk/base/nsProtocolProxyService.cpp
+++ b/netwerk/base/nsProtocolProxyService.cpp
@@ -1203,16 +1203,26 @@ nsProtocolProxyService::AsyncResolveInte
     nsProtocolInfo info;
     rv = GetProtocolInfo(uri, &info);
     if (NS_FAILED(rv))
         return rv;
 
     nsCOMPtr<nsIProxyInfo> pi;
     bool usePACThread;
 
+    // adapt to realtime changes in the system proxy service
+    if (mProxyConfig == PROXYCONFIG_SYSTEM) {
+        nsCOMPtr<nsISystemProxySettings> sp2 =
+            do_GetService(NS_SYSTEMPROXYSETTINGS_CONTRACTID);
+        if (sp2 != mSystemProxySettings) {
+            mSystemProxySettings = sp2;
+            ResetPACThread();
+        }
+    }
+
     // SystemProxySettings and PAC files can block the main thread
     // but if neither of them are in use, we can just do the work
     // right here and directly invoke the callback
 
     rv = Resolve_Internal(channel, info, flags,
                           &usePACThread, getter_AddRefs(pi));
     if (NS_FAILED(rv))
         return rv;
--- a/netwerk/protocol/http/nsHttpChannel.cpp
+++ b/netwerk/protocol/http/nsHttpChannel.cpp
@@ -135,18 +135,16 @@ static uint32_t sRCWNSmallResourceSizeKB
          (result) == NS_ERROR_OUT_OF_MEMORY)
 
 #define WRONG_RACING_RESPONSE_SOURCE(req)                                                  \
     (mRaceCacheWithNetwork &&                                                                 \
         (((mFirstResponseSource == RESPONSE_FROM_CACHE) && (req != mCachePump)) ||         \
          ((mFirstResponseSource == RESPONSE_FROM_NETWORK) && (req != mTransactionPump))))
 
 static NS_DEFINE_CID(kStreamListenerTeeCID, NS_STREAMLISTENERTEE_CID);
-static NS_DEFINE_CID(kStreamTransportServiceCID,
-                     NS_STREAMTRANSPORTSERVICE_CID);
 
 enum CacheDisposition {
     kCacheHit = 1,
     kCacheHitViaReval = 2,
     kCacheMissedViaReval = 3,
     kCacheMissed = 4
 };
 
@@ -3478,19 +3476,20 @@ nsHttpChannel::OpenCacheEntry(bool isHtt
         nsCOMPtr<nsIApplicationCacheContainer> appCacheContainer;
         GetCallback(appCacheContainer);
 
         if (appCacheContainer) {
             appCacheContainer->GetApplicationCache(getter_AddRefs(mApplicationCache));
         }
     }
 
-    nsCOMPtr<nsICacheStorageService> cacheStorageService =
-        do_GetService("@mozilla.org/netwerk/cache-storage-service;1", &rv);
-    NS_ENSURE_SUCCESS(rv, rv);
+    nsCOMPtr<nsICacheStorageService> cacheStorageService(services::GetCacheStorageService());
+    if (!cacheStorageService) {
+        return NS_ERROR_NOT_AVAILABLE;
+    }
 
     nsCOMPtr<nsICacheStorage> cacheStorage;
     nsCOMPtr<nsIURI> openURI;
     if (!mFallbackKey.IsEmpty() && mFallbackChannel) {
         // This is a fallback channel, open fallback URI instead
         rv = NS_NewURI(getter_AddRefs(openURI), mFallbackKey);
         NS_ENSURE_SUCCESS(rv, rv);
     }
@@ -4691,18 +4690,18 @@ nsHttpChannel::OpenCacheInputStream(nsIC
     }
 
     // Have the stream transport service start reading the entity on one of its
     // background threads.
 
     nsCOMPtr<nsITransport> transport;
     nsCOMPtr<nsIInputStream> wrapper;
 
-    nsCOMPtr<nsIStreamTransportService> sts =
-        do_GetService(kStreamTransportServiceCID, &rv);
+    nsCOMPtr<nsIStreamTransportService> sts(services::GetStreamTransportService());
+    rv = sts ? NS_OK : NS_ERROR_NOT_AVAILABLE;
     if (NS_SUCCEEDED(rv)) {
         rv = sts->CreateInputTransport(stream, int64_t(-1), int64_t(-1),
                                         true, getter_AddRefs(transport));
     }
     if (NS_SUCCEEDED(rv)) {
         rv = transport->OpenInputStream(0, 0, 0, getter_AddRefs(wrapper));
     }
     if (NS_SUCCEEDED(rv)) {
@@ -5233,19 +5232,20 @@ nsHttpChannel::InstallCacheListener(int6
 #endif
 
     nsCOMPtr<nsIStreamListenerTee> tee =
         do_CreateInstance(kStreamListenerTeeCID, &rv);
     if (NS_FAILED(rv)) return rv;
 
     nsCOMPtr<nsIEventTarget> cacheIOTarget;
     if (!CacheObserver::UseNewCache()) {
-        nsCOMPtr<nsICacheStorageService> serv =
-            do_GetService("@mozilla.org/netwerk/cache-storage-service;1", &rv);
-        NS_ENSURE_SUCCESS(rv, rv);
+        nsCOMPtr<nsICacheStorageService> serv(services::GetCacheStorageService());
+        if (!serv) {
+            return NS_ERROR_NOT_AVAILABLE;
+        }
 
         serv->GetIoTarget(getter_AddRefs(cacheIOTarget));
     }
 
     if (!cacheIOTarget) {
         LOG(("nsHttpChannel::InstallCacheListener sync tee %p rv=%" PRIx32
              " cacheIOTarget=%p",
              tee.get(), static_cast<uint32_t>(rv), cacheIOTarget.get()));
@@ -5987,17 +5987,17 @@ nsHttpChannel::InitLocalBlockList(const 
 {
     mLocalBlocklist = false;
 
     if (!(mLoadFlags & LOAD_CLASSIFY_URI)) {
         return false;
     }
 
     // Check to see if this principal exists on local blocklists.
-    nsCOMPtr<nsIURIClassifier> classifier = do_GetService(NS_URICLASSIFIERSERVICE_CONTRACTID);
+    nsCOMPtr<nsIURIClassifier> classifier(services::GetURIClassifier());
     RefPtr<nsChannelClassifier> channelClassifier = new nsChannelClassifier(this);
     bool tpEnabled = false;
     channelClassifier->ShouldEnableTrackingProtection(&tpEnabled);
     if (!classifier || !tpEnabled) {
         return false;
     }
 
     // We skip speculative connections by setting mLocalBlocklist only
@@ -8257,18 +8257,18 @@ nsHttpChannel::DoInvalidateCacheEntry(ns
 
     nsAutoCString key;
     if (LOG_ENABLED()) {
       aURI->GetAsciiSpec(key);
     }
 
     LOG(("DoInvalidateCacheEntry [channel=%p key=%s]", this, key.get()));
 
-    nsCOMPtr<nsICacheStorageService> cacheStorageService =
-        do_GetService("@mozilla.org/netwerk/cache-storage-service;1", &rv);
+    nsCOMPtr<nsICacheStorageService> cacheStorageService(services::GetCacheStorageService());
+    rv = cacheStorageService ? NS_OK : NS_ERROR_FAILURE;
 
     nsCOMPtr<nsICacheStorage> cacheStorage;
     if (NS_SUCCEEDED(rv)) {
         RefPtr<LoadContextInfo> info = GetLoadContextInfo(this);
         rv = cacheStorageService->DiskCacheStorage(info, false, getter_AddRefs(cacheStorage));
     }
 
     if (NS_SUCCEEDED(rv)) {
--- a/netwerk/protocol/http/nsHttpConnectionMgr.cpp
+++ b/netwerk/protocol/http/nsHttpConnectionMgr.cpp
@@ -17,16 +17,17 @@
 #include "nsHttpHandler.h"
 #include "nsIHttpChannelInternal.h"
 #include "nsNetCID.h"
 #include "nsCOMPtr.h"
 #include "nsNetUtil.h"
 #include "mozilla/net/DNS.h"
 #include "nsISocketTransport.h"
 #include "nsISSLSocketControl.h"
+#include "mozilla/Services.h"
 #include "mozilla/Telemetry.h"
 #include "mozilla/net/DashboardTypes.h"
 #include "NullHttpTransaction.h"
 #include "nsIDNSRecord.h"
 #include "nsITransport.h"
 #include "nsInterfaceRequestorAgg.h"
 #include "nsIRequestContext.h"
 #include "nsISocketTransportService.h"
@@ -128,31 +129,33 @@ nsHttpConnectionMgr::~nsHttpConnectionMg
     MOZ_ASSERT(mCoalescingHash.Count() == 0);
     if (mTimeoutTick)
         mTimeoutTick->Cancel();
 }
 
 nsresult
 nsHttpConnectionMgr::EnsureSocketThreadTarget()
 {
-    nsresult rv;
     nsCOMPtr<nsIEventTarget> sts;
-    nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
-    if (NS_SUCCEEDED(rv))
-        sts = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
+    nsCOMPtr<nsIIOService> ioService = services::GetIOService();
+    if (ioService) {
+        nsCOMPtr<nsISocketTransportService> realSTS =
+            services::GetSocketTransportService();
+        sts = do_QueryInterface(realSTS);
+    }
 
     ReentrantMonitorAutoEnter mon(mReentrantMonitor);
 
     // do nothing if already initialized or if we've shut down
     if (mSocketThreadTarget || mIsShuttingDown)
         return NS_OK;
 
     mSocketThreadTarget = sts;
 
-    return rv;
+    return sts ? NS_OK : NS_ERROR_NOT_AVAILABLE;
 }
 
 nsresult
 nsHttpConnectionMgr::Init(uint16_t maxUrgentExcessiveConns,
                           uint16_t maxConns,
                           uint16_t maxPersistConnsPerHost,
                           uint16_t maxPersistConnsPerProxy,
                           uint16_t maxRequestDelay)
@@ -3108,18 +3111,20 @@ nsHalfOpenSocket::SetupStreams(nsISocket
         if (socketTypes[typeCount]) {
             typeCount++;
         }
     }
 
     nsCOMPtr<nsISocketTransport> socketTransport;
     nsCOMPtr<nsISocketTransportService> sts;
 
-    sts = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
-    NS_ENSURE_SUCCESS(rv, rv);
+    sts = services::GetSocketTransportService();
+    if (!sts) {
+        return NS_ERROR_NOT_AVAILABLE;
+    }
 
     LOG(("nsHalfOpenSocket::SetupStreams [this=%p ent=%s] "
          "setup routed transport to origin %s:%d via %s:%d\n",
          this, ci->HashKey().get(),
          ci->Origin(), ci->OriginPort(), ci->RoutedHost(), ci->RoutedPort()));
 
     nsCOMPtr<nsIRoutedSocketTransportService> routedSTS(do_QueryInterface(sts));
     if (routedSTS) {
--- a/netwerk/protocol/http/nsHttpTransaction.cpp
+++ b/netwerk/protocol/http/nsHttpTransaction.cpp
@@ -23,17 +23,16 @@
 #include "mozilla/Tokenizer.h"
 #include "TCPFastOpenLayer.h"
 
 #include "nsISeekableStream.h"
 #include "nsMultiplexInputStream.h"
 #include "nsStringStream.h"
 
 #include "nsComponentManagerUtils.h" // do_CreateInstance
-#include "nsServiceManagerUtils.h"   // do_GetService
 #include "nsIHttpActivityObserver.h"
 #include "nsSocketTransportService2.h"
 #include "nsICancelable.h"
 #include "nsIEventTarget.h"
 #include "nsIHttpChannelInternal.h"
 #include "nsIInputStream.h"
 #include "nsIThrottledInputChannel.h"
 #include "nsITransport.h"
@@ -212,18 +211,20 @@ nsHttpTransaction::Init(uint32_t caps,
 
     MOZ_ASSERT(cinfo);
     MOZ_ASSERT(requestHead);
     MOZ_ASSERT(target);
     MOZ_ASSERT(NS_IsMainThread());
 
     mTopLevelOuterContentWindowId = topLevelOuterContentWindowId;
 
-    mActivityDistributor = do_GetService(NS_HTTPACTIVITYDISTRIBUTOR_CONTRACTID, &rv);
-    if (NS_FAILED(rv)) return rv;
+    mActivityDistributor = services::GetActivityDistributor();
+    if (!mActivityDistributor) {
+        return NS_ERROR_NOT_AVAILABLE;
+    }
 
     bool activityDistributorActive;
     rv = mActivityDistributor->GetIsActive(&activityDistributorActive);
     if (NS_SUCCEEDED(rv) && activityDistributorActive) {
         // there are some observers registered at activity distributor, gather
         // nsISupports for the channel that called Init()
         LOG(("nsHttpTransaction::Init() " \
              "mActivityDistributor is active " \
--- a/xpcom/build/ServiceList.h
+++ b/xpcom/build/ServiceList.h
@@ -28,16 +28,26 @@ MOZ_SERVICE(PermissionManager, nsIPermis
 MOZ_SERVICE(ServiceWorkerManager, nsIServiceWorkerManager,
             "@mozilla.org/serviceworkers/manager;1");
 MOZ_SERVICE(AsyncShutdown, nsIAsyncShutdownService,
             "@mozilla.org/async-shutdown-service;1")
 MOZ_SERVICE(UUIDGenerator, nsIUUIDGenerator,
             "@mozilla.org/uuid-generator;1");
 MOZ_SERVICE(GfxInfo, nsIGfxInfo,
             "@mozilla.org/gfx/info;1");
+MOZ_SERVICE(SocketTransportService, nsISocketTransportService,
+            "@mozilla.org/network/socket-transport-service;1");
+MOZ_SERVICE(StreamTransportService, nsIStreamTransportService,
+            "@mozilla.org/network/stream-transport-service;1");
+MOZ_SERVICE(CacheStorageService, nsICacheStorageService,
+            "@mozilla.org/netwerk/cache-storage-service;1");
+MOZ_SERVICE(URIClassifier, nsIURIClassifier,
+            "@mozilla.org/uriclassifierservice");
+MOZ_SERVICE(ActivityDistributor, nsIHttpActivityDistributor,
+            "@mozilla.org/network/http-activity-distributor;1");
 
 #ifdef MOZ_USE_NAMESPACE
 namespace mozilla {
 #endif
 
 MOZ_SERVICE(HistoryService, IHistory,
             "@mozilla.org/browser/history;1")
 
--- a/xpcom/build/Services.cpp
+++ b/xpcom/build/Services.cpp
@@ -17,16 +17,21 @@
 #include "nsIStringBundle.h"
 #include "nsIToolkitChromeRegistry.h"
 #include "nsIXULOverlayProvider.h"
 #include "IHistory.h"
 #include "nsIXPConnect.h"
 #include "inIDOMUtils.h"
 #include "nsIPermissionManager.h"
 #include "nsIServiceWorkerManager.h"
+#include "nsICacheStorageService.h"
+#include "nsIStreamTransportService.h"
+#include "nsISocketTransportService.h"
+#include "nsIURIClassifier.h"
+#include "nsIHttpActivityObserver.h"
 #include "nsIAsyncShutdown.h"
 #include "nsIUUIDGenerator.h"
 #include "nsIGfxInfo.h"
 
 using namespace mozilla;
 using namespace mozilla::services;
 
 /*