Bug 1419382 - Moving ownership of nsIInputStream when using netUtil functions - part 3 - NS_NewInputStreamPump, r=smaug
authorAndrea Marchesini <amarchesini@mozilla.com>
Wed, 22 Nov 2017 11:18:57 +0100
changeset 701909 53f7c44ea8555b5e95be448f4d952953a4ab3208
parent 701908 6b49a0395cb47a3a18c6837aa49d5b622c953f1d
child 701910 6abf8547541108b5773fdf89dcedb40c76c5404e
push id90308
push userbmo:lhansen@mozilla.com
push dateWed, 22 Nov 2017 12:45:04 +0000
reviewerssmaug
bugs1419382
milestone59.0a1
Bug 1419382 - Moving ownership of nsIInputStream when using netUtil functions - part 3 - NS_NewInputStreamPump, r=smaug
dom/fetch/FetchConsumer.cpp
dom/fetch/FetchConsumer.h
dom/workers/ScriptLoader.cpp
dom/workers/ServiceWorkerScriptCache.cpp
dom/worklet/Worklet.cpp
modules/libjar/nsJARChannel.cpp
modules/libjar/zipwriter/nsZipWriter.cpp
netwerk/base/nsNetUtil.cpp
netwerk/base/nsNetUtil.h
netwerk/protocol/res/ExtensionProtocolHandler.cpp
netwerk/protocol/wyciwyg/nsWyciwygChannel.cpp
netwerk/protocol/wyciwyg/nsWyciwygChannel.h
--- a/dom/fetch/FetchConsumer.cpp
+++ b/dom/fetch/FetchConsumer.cpp
@@ -468,17 +468,17 @@ FetchBodyConsumer<Derived>::BeginConsume
   if (mShuttingDown) {
     // We haven't started yet, but we have been terminated. AutoFailConsumeBody
     // will dispatch a runnable to release resources.
     return;
   }
 
   nsCOMPtr<nsIInputStreamPump> pump;
   nsresult rv = NS_NewInputStreamPump(getter_AddRefs(pump),
-                                      mBodyStream, 0, 0, false,
+                                      mBodyStream.forget(), 0, 0, false,
                                       mMainThreadEventTarget);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return;
   }
 
   RefPtr<ConsumeBodyDoneObserver<Derived>> p =
    new ConsumeBodyDoneObserver<Derived>(this);
 
--- a/dom/fetch/FetchConsumer.h
+++ b/dom/fetch/FetchConsumer.h
@@ -98,17 +98,19 @@ private:
   nsCOMPtr<nsIThread> mTargetThread;
   nsCOMPtr<nsIEventTarget> mMainThreadEventTarget;
 
 #ifdef DEBUG
   // This is used only to check if the body has been correctly consumed.
   RefPtr<FetchBody<Derived>> mBody;
 #endif
 
+  // This is nullified when the consuming of the body starts.
   nsCOMPtr<nsIInputStream> mBodyStream;
+
   MutableBlobStorage::MutableBlobStorageType mBlobStorageType;
   nsCString mBodyMimeType;
 
   // Set when consuming the body is attempted on a worker.
   // Unset when consumption is done/aborted.
   // This WorkerHolder keeps alive the consumer via a cycle.
   UniquePtr<workers::WorkerHolder> mWorkerHolder;
 
--- a/dom/workers/ScriptLoader.cpp
+++ b/dom/workers/ScriptLoader.cpp
@@ -1734,17 +1734,17 @@ CacheScriptLoader::ResolvedCallback(JSCo
                                      Move(mPrincipalInfo), mCSPHeaderValue,
                                      mCSPReportOnlyHeaderValue,
                                      mReferrerPolicyHeaderValue);
     return;
   }
 
   MOZ_ASSERT(!mPump);
   rv = NS_NewInputStreamPump(getter_AddRefs(mPump),
-                             inputStream,
+                             inputStream.forget(),
                              0, /* default segsize */
                              0, /* default segcount */
                              false, /* default closeWhenDone */
                              mMainThreadEventTarget);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     Fail(rv);
     return;
   }
--- a/dom/workers/ServiceWorkerScriptCache.cpp
+++ b/dom/workers/ServiceWorkerScriptCache.cpp
@@ -1155,17 +1155,17 @@ CompareCache::ManageValueResult(JSContex
   MOZ_ASSERT(response->Ok());
 
   nsCOMPtr<nsIInputStream> inputStream;
   response->GetBody(getter_AddRefs(inputStream));
   MOZ_ASSERT(inputStream);
 
   MOZ_ASSERT(!mPump);
   rv = NS_NewInputStreamPump(getter_AddRefs(mPump),
-                             inputStream,
+                             inputStream.forget(),
                              0, /* default segsize */
                              0, /* default segcount */
                              false, /* default closeWhenDone */
                              SystemGroup::EventTargetFor(TaskCategory::Other));
   if (NS_WARN_IF(NS_FAILED(rv))) {
     Finish(rv, false);
     return;
   }
--- a/dom/worklet/Worklet.cpp
+++ b/dom/worklet/Worklet.cpp
@@ -125,17 +125,17 @@ public:
     nsCOMPtr<nsIInputStream> inputStream;
     response->GetBody(getter_AddRefs(inputStream));
     if (!inputStream) {
       RejectPromises(NS_ERROR_DOM_NETWORK_ERR);
       return;
     }
 
     nsCOMPtr<nsIInputStreamPump> pump;
-    rv = NS_NewInputStreamPump(getter_AddRefs(pump), inputStream);
+    rv = NS_NewInputStreamPump(getter_AddRefs(pump), inputStream.forget());
     if (NS_WARN_IF(NS_FAILED(rv))) {
       RejectPromises(rv);
       return;
     }
 
     nsCOMPtr<nsIStreamLoader> loader;
     rv = NS_NewStreamLoader(getter_AddRefs(loader), this);
     if (NS_WARN_IF(NS_FAILED(rv))) {
--- a/modules/libjar/nsJARChannel.cpp
+++ b/modules/libjar/nsJARChannel.cpp
@@ -389,17 +389,17 @@ nsJARChannel::OpenLocalFile()
     // Local files are always considered safe.
     mIsUnsafe = false;
 
     RefPtr<nsJARInputThunk> input;
     nsresult rv = CreateJarInput(gJarHandler->JarCache(),
                                  getter_AddRefs(input));
     if (NS_SUCCEEDED(rv)) {
         // Create input stream pump and call AsyncRead as a block.
-        rv = NS_NewInputStreamPump(getter_AddRefs(mPump), input);
+        rv = NS_NewInputStreamPump(getter_AddRefs(mPump), input.forget());
         if (NS_SUCCEEDED(rv))
             rv = mPump->AsyncRead(this, nullptr);
     }
 
     return rv;
 }
 
 void
@@ -1065,17 +1065,17 @@ nsJARChannel::OnDownloadComplete(MemoryD
 
     if (NS_SUCCEEDED(status)) {
         mTempMem = Move(aData);
 
         RefPtr<nsJARInputThunk> input;
         rv = CreateJarInput(nullptr, getter_AddRefs(input));
         if (NS_SUCCEEDED(rv)) {
             // create input stream pump
-            rv = NS_NewInputStreamPump(getter_AddRefs(mPump), input);
+            rv = NS_NewInputStreamPump(getter_AddRefs(mPump), input.forget());
             if (NS_SUCCEEDED(rv))
                 rv = mPump->AsyncRead(this, nullptr);
         }
         status = rv;
     }
 
     if (NS_FAILED(status)) {
         NotifyError(status);
--- a/modules/libjar/zipwriter/nsZipWriter.cpp
+++ b/modules/libjar/zipwriter/nsZipWriter.cpp
@@ -983,18 +983,19 @@ inline nsresult nsZipWriter::BeginProces
 
         RefPtr<nsZipDataStream> stream = new nsZipDataStream();
         NS_ENSURE_TRUE(stream, NS_ERROR_OUT_OF_MEMORY);
         rv = stream->Init(this, mStream, header, aItem->mCompression);
         NS_ENSURE_SUCCESS(rv, rv);
 
         if (aItem->mStream) {
             nsCOMPtr<nsIInputStreamPump> pump;
-            rv = NS_NewInputStreamPump(getter_AddRefs(pump), aItem->mStream,
-                                       0, 0, true);
+            nsCOMPtr<nsIInputStream> tmpStream = aItem->mStream;
+            rv = NS_NewInputStreamPump(getter_AddRefs(pump),
+                                       tmpStream.forget(), 0, 0, true);
             NS_ENSURE_SUCCESS(rv, rv);
 
             rv = pump->AsyncRead(stream, nullptr);
             NS_ENSURE_SUCCESS(rv, rv);
         }
         else {
             rv = NS_MaybeOpenChannelUsingAsyncOpen2(aItem->mChannel, stream);
             NS_ENSURE_SUCCESS(rv, rv);
@@ -1012,17 +1013,19 @@ inline nsresult nsZipWriter::BeginProces
 inline nsresult nsZipWriter::BeginProcessingRemoval(int32_t aPos)
 {
     // Open the zip file for reading
     nsCOMPtr<nsIInputStream> inputStream;
     nsresult rv = NS_NewLocalFileInputStream(getter_AddRefs(inputStream),
                                              mFile);
     NS_ENSURE_SUCCESS(rv, rv);
     nsCOMPtr<nsIInputStreamPump> pump;
-    rv = NS_NewInputStreamPump(getter_AddRefs(pump), inputStream, 0, 0, true);
+    nsCOMPtr<nsIInputStream> tmpStream = inputStream;
+    rv = NS_NewInputStreamPump(getter_AddRefs(pump), tmpStream.forget(), 0, 0,
+                               true);
     if (NS_FAILED(rv)) {
         inputStream->Close();
         return rv;
     }
     nsCOMPtr<nsIStreamListener> listener;
     rv = NS_NewSimpleStreamListener(getter_AddRefs(listener), mStream, this);
     if (NS_FAILED(rv)) {
         inputStream->Close();
--- a/netwerk/base/nsNetUtil.cpp
+++ b/netwerk/base/nsNetUtil.cpp
@@ -710,32 +710,34 @@ NS_NewInputStreamChannel(nsIChannel     
                                           aLoadingPrincipal,
                                           nullptr, // aTriggeringPrincipal
                                           aSecurityFlags,
                                           aContentPolicyType,
                                           aIsSrcdocChannel);
 }
 
 nsresult
-NS_NewInputStreamPump(nsIInputStreamPump **result,
-                      nsIInputStream      *stream,
-                      uint32_t             segsize /* = 0 */,
-                      uint32_t             segcount /* = 0 */,
-                      bool                 closeWhenDone /* = false */,
-                      nsIEventTarget      *mainThreadTarget /* = nullptr */)
+NS_NewInputStreamPump(nsIInputStreamPump** aResult,
+                      already_AddRefed<nsIInputStream> aStream,
+                      uint32_t aSegsize /* = 0 */,
+                      uint32_t aSegcount /* = 0 */,
+                      bool aCloseWhenDone /* = false */,
+                      nsIEventTarget* aMainThreadTarget /* = nullptr */)
 {
+    nsCOMPtr<nsIInputStream> stream = Move(aStream);
+
     nsresult rv;
     nsCOMPtr<nsIInputStreamPump> pump =
         do_CreateInstance(NS_INPUTSTREAMPUMP_CONTRACTID, &rv);
     if (NS_SUCCEEDED(rv)) {
-        rv = pump->Init(stream, segsize, segcount, closeWhenDone,
-                        mainThreadTarget);
+        rv = pump->Init(stream, aSegsize, aSegcount, aCloseWhenDone,
+                        aMainThreadTarget);
         if (NS_SUCCEEDED(rv)) {
-            *result = nullptr;
-            pump.swap(*result);
+            *aResult = nullptr;
+            pump.swap(*aResult);
         }
     }
     return rv;
 }
 
 nsresult
 NS_NewAsyncStreamCopier(nsIAsyncStreamCopier **result,
                         nsIInputStream        *source,
--- a/netwerk/base/nsNetUtil.h
+++ b/netwerk/base/nsNetUtil.h
@@ -296,22 +296,23 @@ nsresult NS_NewInputStreamChannel(nsICha
                                   nsIURI             *aUri,
                                   const nsAString    &aData,
                                   const nsACString   &aContentType,
                                   nsIPrincipal       *aLoadingPrincipal,
                                   nsSecurityFlags     aSecurityFlags,
                                   nsContentPolicyType aContentPolicyType,
                                   bool                aIsSrcdocChannel = false);
 
-nsresult NS_NewInputStreamPump(nsIInputStreamPump **result,
-                               nsIInputStream      *stream,
-                               uint32_t             segsize = 0,
-                               uint32_t             segcount = 0,
-                               bool                 closeWhenDone = false,
-                               nsIEventTarget      *mainThreadTarget = nullptr);
+nsresult
+NS_NewInputStreamPump(nsIInputStreamPump** aResult,
+                      already_AddRefed<nsIInputStream> aStream,
+                      uint32_t aSegsize = 0,
+                      uint32_t aSegcount = 0,
+                      bool aCloseWhenDone = false,
+                      nsIEventTarget *aMainThreadTarget = nullptr);
 
 // NOTE: you will need to specify whether or not your streams are buffered
 // (i.e., do they implement ReadSegments/WriteSegments).  the default
 // assumption of TRUE for both streams might not be right for you!
 nsresult NS_NewAsyncStreamCopier(nsIAsyncStreamCopier **result,
                                  nsIInputStream        *source,
                                  nsIOutputStream       *sink,
                                  nsIEventTarget        *target,
--- a/netwerk/protocol/res/ExtensionProtocolHandler.cpp
+++ b/netwerk/protocol/res/ExtensionProtocolHandler.cpp
@@ -114,17 +114,17 @@ class ExtensionStreamGetter : public Ref
       }
     }
 
     // Get an input stream or file descriptor from the parent asynchronously.
     Result<Ok, nsresult> GetAsync(nsIStreamListener* aListener,
                                   nsIChannel* aChannel);
 
     // Handle an input stream being returned from the parent
-    void OnStream(nsIInputStream* aStream);
+    void OnStream(already_AddRefed<nsIInputStream> aStream);
 
     // Handle file descriptor being returned from the parent
     void OnFD(const FileDescriptor& aFD);
 
     MOZ_DECLARE_REFCOUNTED_TYPENAME(ExtensionStreamGetter)
 
   private:
     nsCOMPtr<nsIURI> mURI;
@@ -239,17 +239,17 @@ ExtensionStreamGetter::GetAsync(nsIStrea
   gNeckoChild->SendGetExtensionStream(uri)->Then(
     mMainThreadEventTarget,
     __func__,
     [self] (const OptionalIPCStream& stream) {
       nsCOMPtr<nsIInputStream> inputStream;
       if (stream.type() == OptionalIPCStream::OptionalIPCStream::TIPCStream) {
         inputStream = ipc::DeserializeIPCStream(stream);
       }
-      self->OnStream(inputStream);
+      self->OnStream(inputStream.forget());
     },
     [self] (const mozilla::ipc::ResponseRejectReason) {
       self->OnStream(nullptr);
     }
   );
   return Ok();
 }
 
@@ -263,37 +263,39 @@ CancelRequest(nsIStreamListener* aListen
 
   aListener->OnStartRequest(aChannel, nullptr);
   aListener->OnStopRequest(aChannel, nullptr, aResult);
   aChannel->Cancel(NS_BINDING_ABORTED);
 }
 
 // Handle an input stream sent from the parent.
 void
-ExtensionStreamGetter::OnStream(nsIInputStream* aStream)
+ExtensionStreamGetter::OnStream(already_AddRefed<nsIInputStream> aStream)
 {
   MOZ_ASSERT(IsNeckoChild());
   MOZ_ASSERT(mListener);
   MOZ_ASSERT(mMainThreadEventTarget);
 
+  nsCOMPtr<nsIInputStream> stream = Move(aStream);
+
   // We must keep an owning reference to the listener
   // until we pass it on to AsyncRead.
   nsCOMPtr<nsIStreamListener> listener = mListener.forget();
 
   MOZ_ASSERT(mChannel);
 
-  if (!aStream) {
+  if (!stream) {
     // The parent didn't send us back a stream.
     CancelRequest(listener, mChannel, NS_ERROR_FILE_ACCESS_DENIED);
     return;
   }
 
   nsCOMPtr<nsIInputStreamPump> pump;
-  nsresult rv = NS_NewInputStreamPump(getter_AddRefs(pump), aStream, 0, 0,
-                                      false, mMainThreadEventTarget);
+  nsresult rv = NS_NewInputStreamPump(getter_AddRefs(pump), stream.forget(),
+                                      0, 0, false, mMainThreadEventTarget);
   if (NS_FAILED(rv)) {
     CancelRequest(listener, mChannel, rv);
     return;
   }
 
   rv = pump->AsyncRead(listener, nullptr);
   if (NS_FAILED(rv)) {
     CancelRequest(listener, mChannel, rv);
--- a/netwerk/protocol/wyciwyg/nsWyciwygChannel.cpp
+++ b/netwerk/protocol/wyciwyg/nsWyciwygChannel.cpp
@@ -468,17 +468,16 @@ nsWyciwygChannel::WriteToCacheEntry(cons
 
 
 NS_IMETHODIMP
 nsWyciwygChannel::CloseCacheEntry(nsresult reason)
 {
   if (mCacheEntry) {
     LOG(("nsWyciwygChannel::CloseCacheEntry [this=%p ]", this));
     mCacheOutputStream = nullptr;
-    mCacheInputStream = nullptr;
 
     if (NS_FAILED(reason)) {
       mCacheEntry->AsyncDoom(nullptr);
     }
 
     mCacheEntry = nullptr;
   }
   return NS_OK;
@@ -764,22 +763,23 @@ nsWyciwygChannel::ReadFromCache()
 
   nsAutoCString tmpStr;
   rv = mCacheEntry->GetMetaDataElement("inhibit-persistent-caching",
                                        getter_Copies(tmpStr));
   if (NS_SUCCEEDED(rv) && tmpStr.EqualsLiteral("1"))
     mLoadFlags |= INHIBIT_PERSISTENT_CACHING;
 
   // Get a transport to the cached data...
-  rv = mCacheEntry->OpenInputStream(0, getter_AddRefs(mCacheInputStream));
+  nsCOMPtr<nsIInputStream> inputStream;
+  rv = mCacheEntry->OpenInputStream(0, getter_AddRefs(inputStream));
   if (NS_FAILED(rv))
     return rv;
-  NS_ENSURE_TRUE(mCacheInputStream, NS_ERROR_UNEXPECTED);
+  NS_ENSURE_TRUE(inputStream, NS_ERROR_UNEXPECTED);
 
-  rv = NS_NewInputStreamPump(getter_AddRefs(mPump), mCacheInputStream);
+  rv = NS_NewInputStreamPump(getter_AddRefs(mPump), inputStream.forget());
   if (NS_FAILED(rv)) return rv;
 
   // Pump the cache data downstream
   return mPump->AsyncRead(this, nullptr);
 }
 
 void
 nsWyciwygChannel::WriteCharsetAndSourceToCache(int32_t aSource,
--- a/netwerk/protocol/wyciwyg/nsWyciwygChannel.h
+++ b/netwerk/protocol/wyciwyg/nsWyciwygChannel.h
@@ -91,17 +91,16 @@ protected:
     nsCOMPtr<nsISupports>               mListenerContext;
 
     // reuse as much of this channel implementation as we can
     nsCOMPtr<nsIInputStreamPump>        mPump;
 
     // Cache related stuff
     nsCOMPtr<nsICacheEntry>             mCacheEntry;
     nsCOMPtr<nsIOutputStream>           mCacheOutputStream;
-    nsCOMPtr<nsIInputStream>            mCacheInputStream;
 
     bool                                mNeedToSetSecurityInfo;
     nsCOMPtr<nsISupports>               mSecurityInfo;
 };
 
 /**
  * Casting nsWyciwygChannel to nsISupports is ambiguous.
  * This method handles that.