Bug 1641825 - P1 - nsBufferedInputStream 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/D77433
This commit is contained in:
ssengupta 2020-06-10 06:38:44 +00:00
parent 2c6fc2f557
commit 76c2c39353

View File

@ -669,7 +669,20 @@ nsBufferedInputStream::AsyncWait(nsIInputStreamCallback* aCallback,
nsIEventTarget* aEventTarget) {
nsCOMPtr<nsIAsyncInputStream> stream = do_QueryInterface(mStream);
if (!stream) {
return NS_ERROR_FAILURE;
// Stream is probably closed. Callback, if not nullptr, can be executed
// immediately
if (!aCallback) {
return NS_OK;
}
if (aEventTarget) {
nsCOMPtr<nsIInputStreamCallback> callable = NS_NewInputStreamReadyEvent(
"nsBufferedInputStream::OnInputStreamReady", aCallback, aEventTarget);
return callable->OnInputStreamReady(this);
}
aCallback->OnInputStreamReady(this);
return NS_OK;
}
nsCOMPtr<nsIInputStreamCallback> callback = aCallback ? this : nullptr;
@ -774,7 +787,22 @@ nsBufferedInputStream::AsyncLengthWait(nsIInputStreamLengthCallback* aCallback,
nsIEventTarget* aEventTarget) {
nsCOMPtr<nsIAsyncInputStreamLength> stream = do_QueryInterface(mStream);
if (!stream) {
return NS_ERROR_FAILURE;
// Stream is probably closed. Callback, if not nullptr, can be executed
// immediately
if (aCallback) {
const RefPtr<nsBufferedInputStream> self = this;
const nsCOMPtr<nsIInputStreamLengthCallback> callback = aCallback;
nsCOMPtr<nsIRunnable> runnable = NS_NewRunnableFunction(
"nsBufferedInputStream::OnInputStreamLengthReady",
[self, callback] { callback->OnInputStreamLengthReady(self, -1); });
if (aEventTarget) {
aEventTarget->Dispatch(runnable, NS_DISPATCH_NORMAL);
} else {
runnable->Run();
}
}
return NS_OK;
}
nsCOMPtr<nsIInputStreamLengthCallback> callback = aCallback ? this : nullptr;