Bug 1641826 - P1 - PartiallySeekableInputStream does not return error and executes callback on AsyncWait/AsyncLengthWait if stream is closed r=baku,necko-reviewers,mayhemer
authorssengupta <ssengupta@mozilla.com>
Tue, 02 Jun 2020 19:04:20 +0000
changeset 533606 84380136974d2c2973799d692fa0ba8f61d0619c
parent 533605 39e6ee02e1ddddeac0ad12651b756479997f4381
child 533607 282fdf095683392a7f0a7ca9cacd652a26cd4725
push id37474
push userabutkovits@mozilla.com
push dateWed, 03 Jun 2020 09:29:05 +0000
treeherdermozilla-central@bf162b065e1f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku, necko-reviewers, mayhemer
bugs1641826
milestone79.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 1641826 - P1 - PartiallySeekableInputStream does not return error and executes callback on AsyncWait/AsyncLengthWait if stream is closed r=baku,necko-reviewers,mayhemer Differential Revision: https://phabricator.services.mozilla.com/D77409
netwerk/base/PartiallySeekableInputStream.cpp
--- a/netwerk/base/PartiallySeekableInputStream.cpp
+++ b/netwerk/base/PartiallySeekableInputStream.cpp
@@ -230,31 +230,41 @@ PartiallySeekableInputStream::CloseWithS
 }
 
 NS_IMETHODIMP
 PartiallySeekableInputStream::AsyncWait(nsIInputStreamCallback* aCallback,
                                         uint32_t aFlags,
                                         uint32_t aRequestedCount,
                                         nsIEventTarget* aEventTarget) {
   if (mClosed) {
-    return NS_BASE_STREAM_CLOSED;
+    if (aCallback) {
+      if (aEventTarget) {
+        nsCOMPtr<nsIInputStreamCallback> callable = NS_NewInputStreamReadyEvent(
+            "PartiallySeekableInputStream::OnInputStreamReady", aCallback,
+            aEventTarget);
+        callable->OnInputStreamReady(this);
+      } else {
+        aCallback->OnInputStreamReady(this);
+      }
+    }
+
+    return NS_OK;
   }
 
-  NS_ENSURE_STATE(mWeakAsyncInputStream);
-
-  nsCOMPtr<nsIInputStreamCallback> callback = aCallback ? this : nullptr;
   {
     MutexAutoLock lock(mMutex);
     if (mAsyncWaitCallback && aCallback) {
       return NS_ERROR_FAILURE;
     }
 
     mAsyncWaitCallback = aCallback;
   }
 
+  NS_ENSURE_STATE(mWeakAsyncInputStream);
+  nsCOMPtr<nsIInputStreamCallback> callback = aCallback ? this : nullptr;
   return mWeakAsyncInputStream->AsyncWait(callback, aFlags, aRequestedCount,
                                           aEventTarget);
 }
 
 // nsIInputStreamCallback
 
 NS_IMETHODIMP
 PartiallySeekableInputStream::OnInputStreamReady(nsIAsyncInputStream* aStream) {
@@ -374,16 +384,32 @@ PartiallySeekableInputStream::Length(int
   return mWeakInputStreamLength->Length(aLength);
 }
 
 // nsIAsyncInputStreamLength
 
 NS_IMETHODIMP
 PartiallySeekableInputStream::AsyncLengthWait(
     nsIInputStreamLengthCallback* aCallback, nsIEventTarget* aEventTarget) {
+  if (mClosed) {
+    if (aCallback) {
+      const RefPtr<PartiallySeekableInputStream> self = this;
+      const nsCOMPtr<nsIInputStreamLengthCallback> callback = aCallback;
+      nsCOMPtr<nsIRunnable> runnable = NS_NewRunnableFunction(
+          "PartiallySeekableInputStream::OnInputStreamLengthReady",
+          [self, callback] { callback->OnInputStreamLengthReady(self, -1); });
+      if (aEventTarget) {
+        aEventTarget->Dispatch(runnable, NS_DISPATCH_NORMAL);
+      } else {
+        runnable->Run();
+      }
+    }
+    return NS_OK;
+  }
+
   NS_ENSURE_STATE(mWeakAsyncInputStreamLength);
 
   nsCOMPtr<nsIInputStreamLengthCallback> callback = aCallback ? this : nullptr;
   {
     MutexAutoLock lock(mMutex);
     mAsyncInputStreamLengthCallback = aCallback;
   }