Bug 1159378 - Part 1: Enable nsIStreamLoader to optionally notify an nsIRequestObserver; r=mcmanus
authorEhsan Akhgari <ehsan@mozilla.com>
Mon, 04 May 2015 10:17:47 -0400
changeset 274264 47947bd5ebed09603734c6177c0eda96016352a5
parent 274263 1a39f8cf4130b5cc951d951a648168e7881e56f5
child 274265 c532540057ddc282cb886502df2f31940acfc857
push id863
push userraliiev@mozilla.com
push dateMon, 03 Aug 2015 13:22:43 +0000
treeherdermozilla-release@f6321b14228d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmcmanus
bugs1159378
milestone40.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1159378 - Part 1: Enable nsIStreamLoader to optionally notify an nsIRequestObserver; r=mcmanus
netwerk/base/nsIStreamLoader.idl
netwerk/base/nsNetUtil.h
netwerk/base/nsPACMan.cpp
netwerk/base/nsStreamLoader.cpp
netwerk/base/nsStreamLoader.h
--- a/netwerk/base/nsIStreamLoader.idl
+++ b/netwerk/base/nsIStreamLoader.idl
@@ -44,26 +44,30 @@ interface nsIStreamLoaderObserver : nsIS
  * To use this interface, first call init() with a nsIStreamLoaderObserver
  * that will be notified when the data has been loaded. Then call asyncOpen()
  * on the channel with the nsIStreamLoader as the listener. The context
  * argument in the asyncOpen() call will be passed to the onStreamComplete()
  * callback.
  *
  * XXX define behaviour for sizes >4 GB
  */
-[scriptable, uuid(8ea7e890-8211-11d9-8bde-f66bad1e3f3a)]
+[scriptable, uuid(323bcff1-7513-4e1f-a541-1c9213c2ed1b)]
 interface nsIStreamLoader : nsIStreamListener
 {
     /**
      * Initialize this stream loader, and start loading the data.
      *
-     * @param aObserver
+     * @param aStreamObserver
      *        An observer that will be notified when the data is complete.
+     * @param aRequestObserver
+     *        An optional observer that will be notified when the request
+     *        has started or stopped.
      */
-    void init(in nsIStreamLoaderObserver aObserver);
+    void init(in nsIStreamLoaderObserver aStreamObserver,
+              [optional] in nsIRequestObserver aRequestObserver);
 
     /**
      * Gets the number of bytes read so far.
      */
     readonly attribute unsigned long numBytesRead;
 
     /**
      * Gets the request that loaded this file.
--- a/netwerk/base/nsNetUtil.h
+++ b/netwerk/base/nsNetUtil.h
@@ -855,23 +855,24 @@ NS_NewDownloader(nsIStreamListener   **r
             downloader.forget(result);
         }
     }
     return rv;
 }
 
 inline nsresult
 NS_NewStreamLoader(nsIStreamLoader        **result,
-                   nsIStreamLoaderObserver *observer)
+                   nsIStreamLoaderObserver *observer,
+                   nsIRequestObserver      *requestObserver = nullptr)
 {
     nsresult rv;
     nsCOMPtr<nsIStreamLoader> loader =
         do_CreateInstance(NS_STREAMLOADER_CONTRACTID, &rv);
     if (NS_SUCCEEDED(rv)) {
-        rv = loader->Init(observer);
+        rv = loader->Init(observer, requestObserver);
         if (NS_SUCCEEDED(rv)) {
             *result = nullptr;
             loader.swap(*result);
         }
     }
     return rv;
 }
 
--- a/netwerk/base/nsPACMan.cpp
+++ b/netwerk/base/nsPACMan.cpp
@@ -434,17 +434,17 @@ nsPACMan::StartLoading()
   mLoadPending = false;
 
   // CancelExistingLoad was called...
   if (!mLoader) {
     PostCancelPendingQ(NS_ERROR_ABORT);
     return;
   }
 
-  if (NS_SUCCEEDED(mLoader->Init(this))) {
+  if (NS_SUCCEEDED(mLoader->Init(this, nullptr))) {
     // Always hit the origin server when loading PAC.
     nsCOMPtr<nsIIOService> ios = do_GetIOService();
     if (ios) {
       nsCOMPtr<nsIChannel> channel;
       nsCOMPtr<nsIURI> pacURI;
       NS_NewURI(getter_AddRefs(pacURI), mPACURISpec);
 
       // NOTE: This results in GetProxyForURI being called
--- a/netwerk/base/nsStreamLoader.cpp
+++ b/netwerk/base/nsStreamLoader.cpp
@@ -16,20 +16,22 @@ nsStreamLoader::nsStreamLoader()
 {
 }
 
 nsStreamLoader::~nsStreamLoader()
 {
 }
 
 NS_IMETHODIMP
-nsStreamLoader::Init(nsIStreamLoaderObserver* observer)
+nsStreamLoader::Init(nsIStreamLoaderObserver* aStreamObserver,
+                     nsIRequestObserver* aRequestObserver)
 {
-  NS_ENSURE_ARG_POINTER(observer);
-  mObserver = observer;
+  NS_ENSURE_ARG_POINTER(aStreamObserver);
+  mObserver = aStreamObserver;
+  mRequestObserver = aRequestObserver;
   return NS_OK;
 }
 
 nsresult
 nsStreamLoader::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
 {
   if (aOuter) return NS_ERROR_NO_AGGREGATION;
 
@@ -75,16 +77,19 @@ nsStreamLoader::OnStartRequest(nsIReques
       }
       // preallocate buffer
       if (!mData.initCapacity(contentLength)) {
         return NS_ERROR_OUT_OF_MEMORY;
       }
     }
   }
   mContext = ctxt;
+  if (mRequestObserver) {
+    mRequestObserver->OnStartRequest(request, ctxt);
+  }
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsStreamLoader::OnStopRequest(nsIRequest* request, nsISupports *ctxt,
                               nsresult aStatus)
 {
   PROFILER_LABEL("nsStreamLoader", "OnStopRequest",
@@ -103,16 +108,22 @@ nsStreamLoader::OnStopRequest(nsIRequest
       mData.replaceRawBuffer(elems, length);
     }
     // done.. cleanup
     ReleaseData();
     mRequest = 0;
     mObserver = 0;
     mContext = 0;
   }
+
+  if (mRequestObserver) {
+    mRequestObserver->OnStopRequest(request, ctxt, aStatus);
+    mRequestObserver = nullptr;
+  }
+
   return NS_OK;
 }
 
 NS_METHOD
 nsStreamLoader::WriteSegmentFun(nsIInputStream *inStr,
                                 void *closure,
                                 const char *fromSegment,
                                 uint32_t toOffset,
--- a/netwerk/base/nsStreamLoader.h
+++ b/netwerk/base/nsStreamLoader.h
@@ -37,15 +37,16 @@ protected:
 
   // Utility method to free mData, if present, and update other state to
   // reflect that no data has been allocated.
   void ReleaseData();
 
   nsCOMPtr<nsIStreamLoaderObserver> mObserver;
   nsCOMPtr<nsISupports>             mContext;  // the observer's context
   nsCOMPtr<nsIRequest>              mRequest;
+  nsCOMPtr<nsIRequestObserver>      mRequestObserver;
 
   // Buffer to accumulate incoming data. We preallocate if contentSize is
   // available.
   mozilla::Vector<uint8_t, 0> mData;
 };
 
 #endif // nsStreamLoader_h__