2017-10-26 21:39:00 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2016-07-25 23:41:02 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "SlicedInputStream.h"
|
2017-02-22 11:41:05 +03:00
|
|
|
#include "mozilla/ipc/InputStreamUtils.h"
|
2018-05-23 08:12:35 +03:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2018-04-13 17:40:20 +03:00
|
|
|
#include "mozilla/ScopeExit.h"
|
2016-07-25 23:41:02 +03:00
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
|
2017-11-07 03:04:06 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace ipc;
|
2017-02-22 11:41:05 +03:00
|
|
|
|
2017-02-22 11:40:46 +03:00
|
|
|
NS_IMPL_ADDREF(SlicedInputStream);
|
|
|
|
NS_IMPL_RELEASE(SlicedInputStream);
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(SlicedInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsICloneableInputStream,
|
2017-02-22 11:41:05 +03:00
|
|
|
mWeakCloneableInputStream || !mInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(
|
|
|
|
nsIIPCSerializableInputStream,
|
|
|
|
mWeakIPCSerializableInputStream || !mInputStream)
|
2017-02-22 11:41:59 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsISeekableStream,
|
|
|
|
mWeakSeekableInputStream || !mInputStream)
|
Bug 1496581 - Split nsISeekableStream in 2 classes: nsISeekableStream and nsITellableStream, f=mayhemer, r=froydnj
In the current code there are 3 main issues:
1. nsFileStream is not really thread-safe. There is nothing to protect the
internal members and we see crashes.
2. nsPipeInputStream doesn't implement ::Seek() method and that caused issues
in devtools when a nsHttpChannel sends POST data using a pipe. In order to fix
this, bug 1494176 added a check in nsHttpChannel: if the stream doesn't
implement ::Seek(), let's clone it. This was an hack around nsPipeInputStream,
and it's bad.
3. When nsHttpChannel sends POST data using a file stream, nsFileStream does
I/O on main-thread because of the issue 2. Plus, ::Seek() is called on the
main-thread causing issue 1.
Note that nsPipeInputStream implements only ::Tell(), of the nsISeekableStream
methods. It doesn't implement ::Seek() and it doesn't implement ::SetEOF().
With this patch I want to fix point 2 and point 3 (and consequentially issue 1
- but we need a separate fix for it - follow up). The patch does:
1. it splits nsISeekableStream in 2 interfaces: nsITellableStream and
nsISeekableStream.
2. nsPipeInputStream implements only nsITellableStream. Doing this, we don't
need the ::Seek() check for point 2 in nsHttpChannel: a simple QI check is
enough.
3. Because we don't call ::Seek() in nsHttpChannel, nsFileStream doesn't do I/O
on the main-thread, and we don't crash doing so.
2018-10-18 14:35:35 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsITellableStream,
|
|
|
|
mWeakTellableInputStream || !mInputStream)
|
2017-04-12 14:49:08 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIAsyncInputStream,
|
|
|
|
mWeakAsyncInputStream || !mInputStream)
|
2017-04-12 14:49:08 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIInputStreamCallback,
|
|
|
|
mWeakAsyncInputStream || !mInputStream)
|
2018-05-23 08:12:35 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIInputStreamLength,
|
|
|
|
mWeakInputStreamLength || !mInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(
|
|
|
|
nsIAsyncInputStreamLength, mWeakAsyncInputStreamLength || !mInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(
|
|
|
|
nsIInputStreamLengthCallback,
|
|
|
|
mWeakAsyncInputStreamLength || !mInputStream)
|
2017-02-22 11:40:46 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIInputStream)
|
|
|
|
NS_INTERFACE_MAP_END
|
2016-07-25 23:41:02 +03:00
|
|
|
|
2017-10-05 08:38:48 +03:00
|
|
|
SlicedInputStream::SlicedInputStream(
|
|
|
|
already_AddRefed<nsIInputStream> aInputStream, uint64_t aStart,
|
2016-07-25 23:41:02 +03:00
|
|
|
uint64_t aLength)
|
2017-02-22 11:40:46 +03:00
|
|
|
: mWeakCloneableInputStream(nullptr),
|
2017-02-22 11:41:05 +03:00
|
|
|
mWeakIPCSerializableInputStream(nullptr),
|
2017-02-22 11:41:59 +03:00
|
|
|
mWeakSeekableInputStream(nullptr),
|
Bug 1496581 - Split nsISeekableStream in 2 classes: nsISeekableStream and nsITellableStream, f=mayhemer, r=froydnj
In the current code there are 3 main issues:
1. nsFileStream is not really thread-safe. There is nothing to protect the
internal members and we see crashes.
2. nsPipeInputStream doesn't implement ::Seek() method and that caused issues
in devtools when a nsHttpChannel sends POST data using a pipe. In order to fix
this, bug 1494176 added a check in nsHttpChannel: if the stream doesn't
implement ::Seek(), let's clone it. This was an hack around nsPipeInputStream,
and it's bad.
3. When nsHttpChannel sends POST data using a file stream, nsFileStream does
I/O on main-thread because of the issue 2. Plus, ::Seek() is called on the
main-thread causing issue 1.
Note that nsPipeInputStream implements only ::Tell(), of the nsISeekableStream
methods. It doesn't implement ::Seek() and it doesn't implement ::SetEOF().
With this patch I want to fix point 2 and point 3 (and consequentially issue 1
- but we need a separate fix for it - follow up). The patch does:
1. it splits nsISeekableStream in 2 interfaces: nsITellableStream and
nsISeekableStream.
2. nsPipeInputStream implements only nsITellableStream. Doing this, we don't
need the ::Seek() check for point 2 in nsHttpChannel: a simple QI check is
enough.
3. Because we don't call ::Seek() in nsHttpChannel, nsFileStream doesn't do I/O
on the main-thread, and we don't crash doing so.
2018-10-18 14:35:35 +03:00
|
|
|
mWeakTellableInputStream(nullptr),
|
2017-04-12 14:49:08 +03:00
|
|
|
mWeakAsyncInputStream(nullptr),
|
2018-05-23 08:12:35 +03:00
|
|
|
mWeakInputStreamLength(nullptr),
|
|
|
|
mWeakAsyncInputStreamLength(nullptr),
|
2016-07-25 23:41:02 +03:00
|
|
|
mStart(aStart),
|
|
|
|
mLength(aLength),
|
|
|
|
mCurPos(0),
|
|
|
|
mClosed(false),
|
2017-05-04 09:36:35 +03:00
|
|
|
mAsyncWaitFlags(0),
|
|
|
|
mAsyncWaitRequestedCount(0),
|
2018-04-10 18:33:08 +03:00
|
|
|
mMutex("SlicedInputStream::mMutex") {
|
2018-05-30 22:15:35 +03:00
|
|
|
nsCOMPtr<nsIInputStream> inputStream = std::move(aInputStream);
|
2017-10-05 08:38:48 +03:00
|
|
|
SetSourceStream(inputStream.forget());
|
2016-07-25 23:41:02 +03:00
|
|
|
}
|
|
|
|
|
2017-02-22 11:41:05 +03:00
|
|
|
SlicedInputStream::SlicedInputStream()
|
|
|
|
: mWeakCloneableInputStream(nullptr),
|
|
|
|
mWeakIPCSerializableInputStream(nullptr),
|
2017-02-22 11:41:59 +03:00
|
|
|
mWeakSeekableInputStream(nullptr),
|
Bug 1496581 - Split nsISeekableStream in 2 classes: nsISeekableStream and nsITellableStream, f=mayhemer, r=froydnj
In the current code there are 3 main issues:
1. nsFileStream is not really thread-safe. There is nothing to protect the
internal members and we see crashes.
2. nsPipeInputStream doesn't implement ::Seek() method and that caused issues
in devtools when a nsHttpChannel sends POST data using a pipe. In order to fix
this, bug 1494176 added a check in nsHttpChannel: if the stream doesn't
implement ::Seek(), let's clone it. This was an hack around nsPipeInputStream,
and it's bad.
3. When nsHttpChannel sends POST data using a file stream, nsFileStream does
I/O on main-thread because of the issue 2. Plus, ::Seek() is called on the
main-thread causing issue 1.
Note that nsPipeInputStream implements only ::Tell(), of the nsISeekableStream
methods. It doesn't implement ::Seek() and it doesn't implement ::SetEOF().
With this patch I want to fix point 2 and point 3 (and consequentially issue 1
- but we need a separate fix for it - follow up). The patch does:
1. it splits nsISeekableStream in 2 interfaces: nsITellableStream and
nsISeekableStream.
2. nsPipeInputStream implements only nsITellableStream. Doing this, we don't
need the ::Seek() check for point 2 in nsHttpChannel: a simple QI check is
enough.
3. Because we don't call ::Seek() in nsHttpChannel, nsFileStream doesn't do I/O
on the main-thread, and we don't crash doing so.
2018-10-18 14:35:35 +03:00
|
|
|
mWeakTellableInputStream(nullptr),
|
2017-04-12 14:49:08 +03:00
|
|
|
mWeakAsyncInputStream(nullptr),
|
2017-02-22 11:41:05 +03:00
|
|
|
mStart(0),
|
|
|
|
mLength(0),
|
|
|
|
mCurPos(0),
|
|
|
|
mClosed(false),
|
2017-05-04 09:36:35 +03:00
|
|
|
mAsyncWaitFlags(0),
|
|
|
|
mAsyncWaitRequestedCount(0),
|
2018-04-10 18:33:08 +03:00
|
|
|
mMutex("SlicedInputStream::mMutex") {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-25 23:41:02 +03:00
|
|
|
SlicedInputStream::~SlicedInputStream() {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-10-05 08:38:48 +03:00
|
|
|
void SlicedInputStream::SetSourceStream(
|
|
|
|
already_AddRefed<nsIInputStream> aInputStream) {
|
2017-02-22 11:41:05 +03:00
|
|
|
MOZ_ASSERT(!mInputStream);
|
2017-02-22 11:40:46 +03:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
mInputStream = std::move(aInputStream);
|
2017-02-22 11:40:46 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsICloneableInputStream> cloneableStream =
|
2017-10-05 08:38:48 +03:00
|
|
|
do_QueryInterface(mInputStream);
|
|
|
|
if (cloneableStream && SameCOMIdentity(mInputStream, cloneableStream)) {
|
2017-02-22 11:40:46 +03:00
|
|
|
mWeakCloneableInputStream = cloneableStream;
|
|
|
|
}
|
2017-02-22 11:41:05 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIIPCSerializableInputStream> serializableStream =
|
2017-10-05 08:38:48 +03:00
|
|
|
do_QueryInterface(mInputStream);
|
|
|
|
if (serializableStream && SameCOMIdentity(mInputStream, serializableStream)) {
|
2017-02-22 11:41:05 +03:00
|
|
|
mWeakIPCSerializableInputStream = serializableStream;
|
|
|
|
}
|
2017-02-22 11:41:59 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsISeekableStream> seekableStream = do_QueryInterface(mInputStream);
|
2017-10-05 08:38:48 +03:00
|
|
|
if (seekableStream && SameCOMIdentity(mInputStream, seekableStream)) {
|
2017-02-22 11:41:59 +03:00
|
|
|
mWeakSeekableInputStream = seekableStream;
|
|
|
|
}
|
2017-04-12 14:49:08 +03:00
|
|
|
|
Bug 1496581 - Split nsISeekableStream in 2 classes: nsISeekableStream and nsITellableStream, f=mayhemer, r=froydnj
In the current code there are 3 main issues:
1. nsFileStream is not really thread-safe. There is nothing to protect the
internal members and we see crashes.
2. nsPipeInputStream doesn't implement ::Seek() method and that caused issues
in devtools when a nsHttpChannel sends POST data using a pipe. In order to fix
this, bug 1494176 added a check in nsHttpChannel: if the stream doesn't
implement ::Seek(), let's clone it. This was an hack around nsPipeInputStream,
and it's bad.
3. When nsHttpChannel sends POST data using a file stream, nsFileStream does
I/O on main-thread because of the issue 2. Plus, ::Seek() is called on the
main-thread causing issue 1.
Note that nsPipeInputStream implements only ::Tell(), of the nsISeekableStream
methods. It doesn't implement ::Seek() and it doesn't implement ::SetEOF().
With this patch I want to fix point 2 and point 3 (and consequentially issue 1
- but we need a separate fix for it - follow up). The patch does:
1. it splits nsISeekableStream in 2 interfaces: nsITellableStream and
nsISeekableStream.
2. nsPipeInputStream implements only nsITellableStream. Doing this, we don't
need the ::Seek() check for point 2 in nsHttpChannel: a simple QI check is
enough.
3. Because we don't call ::Seek() in nsHttpChannel, nsFileStream doesn't do I/O
on the main-thread, and we don't crash doing so.
2018-10-18 14:35:35 +03:00
|
|
|
nsCOMPtr<nsITellableStream> tellableStream = do_QueryInterface(mInputStream);
|
|
|
|
if (tellableStream && SameCOMIdentity(mInputStream, tellableStream)) {
|
|
|
|
mWeakTellableInputStream = tellableStream;
|
|
|
|
}
|
|
|
|
|
2017-04-12 14:49:08 +03:00
|
|
|
nsCOMPtr<nsIAsyncInputStream> asyncInputStream =
|
2017-10-05 08:38:48 +03:00
|
|
|
do_QueryInterface(mInputStream);
|
|
|
|
if (asyncInputStream && SameCOMIdentity(mInputStream, asyncInputStream)) {
|
2017-04-12 14:49:08 +03:00
|
|
|
mWeakAsyncInputStream = asyncInputStream;
|
|
|
|
}
|
2018-05-23 08:12:35 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStreamLength> streamLength = do_QueryInterface(mInputStream);
|
|
|
|
if (streamLength && SameCOMIdentity(mInputStream, streamLength)) {
|
|
|
|
mWeakInputStreamLength = streamLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAsyncInputStreamLength> asyncStreamLength =
|
|
|
|
do_QueryInterface(mInputStream);
|
|
|
|
if (asyncStreamLength && SameCOMIdentity(mInputStream, asyncStreamLength)) {
|
|
|
|
mWeakAsyncInputStreamLength = asyncStreamLength;
|
|
|
|
}
|
2017-02-22 11:40:46 +03:00
|
|
|
}
|
|
|
|
|
2018-05-23 08:12:35 +03:00
|
|
|
uint64_t SlicedInputStream::AdjustRange(uint64_t aRange) {
|
|
|
|
CheckedUint64 range(aRange);
|
|
|
|
range += mCurPos;
|
|
|
|
|
|
|
|
// Let's remove extra length from the end.
|
|
|
|
if (range.isValid() && range.value() > mStart + mLength) {
|
|
|
|
aRange -= XPCOM_MIN((uint64_t)aRange, range.value() - (mStart + mLength));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's remove extra length from the begin.
|
|
|
|
if (mCurPos < mStart) {
|
|
|
|
aRange -= XPCOM_MIN((uint64_t)aRange, mStart - mCurPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return aRange;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIInputStream interface
|
|
|
|
|
2016-07-25 23:41:02 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
SlicedInputStream::Close() {
|
2017-02-22 11:41:05 +03:00
|
|
|
NS_ENSURE_STATE(mInputStream);
|
|
|
|
|
2016-07-25 23:41:02 +03:00
|
|
|
mClosed = true;
|
2017-10-05 08:38:48 +03:00
|
|
|
return mInputStream->Close();
|
2016-07-25 23:41:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SlicedInputStream::Available(uint64_t* aLength) {
|
2017-02-22 11:41:05 +03:00
|
|
|
NS_ENSURE_STATE(mInputStream);
|
|
|
|
|
2016-07-25 23:41:02 +03:00
|
|
|
if (mClosed) {
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = mInputStream->Available(aLength);
|
2017-11-06 16:22:47 +03:00
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
|
|
|
mClosed = true;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-07-25 23:41:02 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2018-05-23 08:12:35 +03:00
|
|
|
*aLength = AdjustRange(*aLength);
|
2016-07-25 23:41:02 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SlicedInputStream::Read(char* aBuffer, uint32_t aCount, uint32_t* aReadCount) {
|
2017-02-22 11:42:27 +03:00
|
|
|
*aReadCount = 0;
|
2017-02-22 11:19:50 +03:00
|
|
|
|
2016-07-25 23:41:02 +03:00
|
|
|
if (mClosed) {
|
2017-02-22 11:42:27 +03:00
|
|
|
return NS_OK;
|
2016-07-25 23:41:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mCurPos < mStart) {
|
|
|
|
nsCOMPtr<nsISeekableStream> seekableStream =
|
|
|
|
do_QueryInterface(mInputStream);
|
|
|
|
if (seekableStream) {
|
|
|
|
nsresult rv =
|
|
|
|
seekableStream->Seek(nsISeekableStream::NS_SEEK_SET, mStart);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCurPos = mStart;
|
|
|
|
} else {
|
|
|
|
char buf[4096];
|
|
|
|
while (mCurPos < mStart) {
|
2017-04-12 14:49:08 +03:00
|
|
|
uint32_t bytesRead;
|
|
|
|
uint64_t bufCount = XPCOM_MIN(mStart - mCurPos, (uint64_t)sizeof(buf));
|
|
|
|
nsresult rv = mInputStream->Read(buf, bufCount, &bytesRead);
|
2017-11-06 16:22:47 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && bytesRead == 0) {
|
|
|
|
mClosed = true;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2017-04-12 14:49:08 +03:00
|
|
|
return rv;
|
|
|
|
}
|
2016-07-25 23:41:02 +03:00
|
|
|
|
|
|
|
mCurPos += bytesRead;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's reduce aCount in case it's too big.
|
|
|
|
if (mCurPos + aCount > mStart + mLength) {
|
|
|
|
aCount = mStart + mLength - mCurPos;
|
|
|
|
}
|
|
|
|
|
2017-10-04 14:10:10 +03:00
|
|
|
// Nothing else to read.
|
|
|
|
if (!aCount) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-02-22 11:42:27 +03:00
|
|
|
nsresult rv = mInputStream->Read(aBuffer, aCount, aReadCount);
|
2017-11-06 16:22:47 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aReadCount == 0) {
|
|
|
|
mClosed = true;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2017-02-22 11:42:27 +03:00
|
|
|
return rv;
|
2016-07-25 23:41:02 +03:00
|
|
|
}
|
|
|
|
|
2017-02-22 11:42:27 +03:00
|
|
|
mCurPos += *aReadCount;
|
2016-07-25 23:41:02 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-02-22 11:42:27 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
SlicedInputStream::ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
|
|
|
|
uint32_t aCount, uint32_t* aResult) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2016-07-25 23:41:02 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
SlicedInputStream::IsNonBlocking(bool* aNonBlocking) {
|
2017-02-22 11:41:05 +03:00
|
|
|
NS_ENSURE_STATE(mInputStream);
|
2016-07-25 23:41:02 +03:00
|
|
|
return mInputStream->IsNonBlocking(aNonBlocking);
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsICloneableInputStream interface
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SlicedInputStream::GetCloneable(bool* aCloneable) {
|
2017-02-22 11:41:05 +03:00
|
|
|
NS_ENSURE_STATE(mInputStream);
|
|
|
|
NS_ENSURE_STATE(mWeakCloneableInputStream);
|
|
|
|
|
2016-07-25 23:41:02 +03:00
|
|
|
*aCloneable = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SlicedInputStream::Clone(nsIInputStream** aResult) {
|
2017-02-22 11:41:05 +03:00
|
|
|
NS_ENSURE_STATE(mInputStream);
|
|
|
|
NS_ENSURE_STATE(mWeakCloneableInputStream);
|
2017-02-22 11:19:50 +03:00
|
|
|
|
2017-02-22 11:40:46 +03:00
|
|
|
nsCOMPtr<nsIInputStream> clonedStream;
|
|
|
|
nsresult rv = mWeakCloneableInputStream->Clone(getter_AddRefs(clonedStream));
|
2016-07-25 23:41:02 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> sis =
|
2017-10-05 08:38:48 +03:00
|
|
|
new SlicedInputStream(clonedStream.forget(), mStart, mLength);
|
2016-07-25 23:41:02 +03:00
|
|
|
|
|
|
|
sis.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIAsyncInputStream interface
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SlicedInputStream::CloseWithStatus(nsresult aStatus) {
|
2017-02-22 11:41:05 +03:00
|
|
|
NS_ENSURE_STATE(mInputStream);
|
2017-04-12 14:49:08 +03:00
|
|
|
NS_ENSURE_STATE(mWeakAsyncInputStream);
|
2017-02-22 11:41:05 +03:00
|
|
|
|
2017-10-05 08:38:48 +03:00
|
|
|
mClosed = true;
|
2017-04-12 14:49:08 +03:00
|
|
|
return mWeakAsyncInputStream->CloseWithStatus(aStatus);
|
2016-07-25 23:41:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SlicedInputStream::AsyncWait(nsIInputStreamCallback* aCallback, uint32_t aFlags,
|
|
|
|
uint32_t aRequestedCount,
|
|
|
|
nsIEventTarget* aEventTarget) {
|
2017-02-22 11:41:05 +03:00
|
|
|
NS_ENSURE_STATE(mInputStream);
|
2017-04-12 14:49:08 +03:00
|
|
|
NS_ENSURE_STATE(mWeakAsyncInputStream);
|
2017-02-22 11:41:05 +03:00
|
|
|
|
2018-04-10 18:33:08 +03:00
|
|
|
nsCOMPtr<nsIInputStreamCallback> callback = aCallback ? this : nullptr;
|
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
uint32_t flags = aFlags;
|
|
|
|
uint32_t requestedCount = aRequestedCount;
|
2018-04-10 18:33:08 +03:00
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2017-04-12 14:49:08 +03:00
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
if (mAsyncWaitCallback && aCallback) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2017-04-12 14:49:08 +03:00
|
|
|
}
|
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
mAsyncWaitCallback = aCallback;
|
|
|
|
|
|
|
|
// If we haven't started retrieving data, let's see if we can seek.
|
|
|
|
// If we cannot seek, we will do consecutive reads.
|
|
|
|
if (mCurPos < mStart && mWeakSeekableInputStream) {
|
|
|
|
nsresult rv = mWeakSeekableInputStream->Seek(
|
|
|
|
nsISeekableStream::NS_SEEK_SET, mStart);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2017-04-12 14:49:08 +03:00
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
mCurPos = mStart;
|
|
|
|
}
|
2017-04-12 14:49:08 +03:00
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
mAsyncWaitFlags = aFlags;
|
|
|
|
mAsyncWaitRequestedCount = aRequestedCount;
|
|
|
|
mAsyncWaitEventTarget = aEventTarget;
|
2018-04-10 18:33:08 +03:00
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
// If we are not at the right position, let's do an asyncWait just internal.
|
|
|
|
if (mCurPos < mStart) {
|
|
|
|
flags = 0;
|
|
|
|
requestedCount = mStart - mCurPos;
|
|
|
|
}
|
2017-04-12 14:49:08 +03:00
|
|
|
}
|
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
return mWeakAsyncInputStream->AsyncWait(callback, flags, requestedCount,
|
2017-04-12 14:49:08 +03:00
|
|
|
aEventTarget);
|
2016-07-25 23:41:02 +03:00
|
|
|
}
|
2017-02-22 11:41:05 +03:00
|
|
|
|
2017-04-12 14:49:08 +03:00
|
|
|
// nsIInputStreamCallback
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SlicedInputStream::OnInputStreamReady(nsIAsyncInputStream* aStream) {
|
|
|
|
MOZ_ASSERT(mInputStream);
|
|
|
|
MOZ_ASSERT(mWeakAsyncInputStream);
|
|
|
|
MOZ_ASSERT(mWeakAsyncInputStream == aStream);
|
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
nsCOMPtr<nsIInputStreamCallback> callback;
|
|
|
|
uint32_t asyncWaitFlags = 0;
|
|
|
|
uint32_t asyncWaitRequestedCount = 0;
|
|
|
|
nsCOMPtr<nsIEventTarget> asyncWaitEventTarget;
|
2018-04-10 18:33:08 +03:00
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2017-04-12 14:49:08 +03:00
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
// We have been canceled in the meanwhile.
|
|
|
|
if (!mAsyncWaitCallback) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-11-06 16:22:47 +03:00
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
auto raii = MakeScopeExit([&] {
|
|
|
|
mMutex.AssertCurrentThreadOwns();
|
|
|
|
mAsyncWaitCallback = nullptr;
|
|
|
|
mAsyncWaitEventTarget = nullptr;
|
|
|
|
});
|
2017-04-12 14:49:08 +03:00
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
asyncWaitFlags = mAsyncWaitFlags;
|
|
|
|
asyncWaitRequestedCount = mAsyncWaitRequestedCount;
|
|
|
|
asyncWaitEventTarget = mAsyncWaitEventTarget;
|
2017-04-12 14:49:08 +03:00
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
// If at the end of this locked block, the callback is not null, it will be
|
|
|
|
// executed, otherwise, we are going to exec another AsyncWait().
|
|
|
|
callback = mAsyncWaitCallback;
|
2017-04-12 14:49:08 +03:00
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
if (mCurPos < mStart) {
|
|
|
|
char buf[4096];
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
while (mCurPos < mStart) {
|
|
|
|
uint32_t bytesRead;
|
|
|
|
uint64_t bufCount = XPCOM_MIN(mStart - mCurPos, (uint64_t)sizeof(buf));
|
|
|
|
rv = mInputStream->Read(buf, bufCount, &bytesRead);
|
|
|
|
if (NS_SUCCEEDED(rv) && bytesRead == 0) {
|
|
|
|
mClosed = true;
|
|
|
|
break;
|
|
|
|
}
|
2018-04-10 18:33:08 +03:00
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
if (rv == NS_BASE_STREAM_WOULD_BLOCK) {
|
|
|
|
asyncWaitFlags = 0;
|
|
|
|
asyncWaitRequestedCount = mStart - mCurPos;
|
|
|
|
// Here we want to exec another AsyncWait().
|
|
|
|
callback = nullptr;
|
|
|
|
break;
|
|
|
|
}
|
2018-04-10 18:33:08 +03:00
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
break;
|
|
|
|
}
|
2017-04-12 14:49:08 +03:00
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
mCurPos += bytesRead;
|
|
|
|
}
|
2017-04-12 14:49:08 +03:00
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
// Now we are ready to do the 'real' asyncWait.
|
|
|
|
if (mCurPos >= mStart) {
|
|
|
|
// We don't want to nullify the callback now, because it will be needed
|
|
|
|
// at the next ::OnInputStreamReady.
|
|
|
|
raii.release();
|
|
|
|
callback = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-12 14:49:08 +03:00
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
if (callback) {
|
|
|
|
return callback->OnInputStreamReady(this);
|
|
|
|
}
|
2017-04-12 14:49:08 +03:00
|
|
|
|
2018-04-13 17:40:20 +03:00
|
|
|
return mWeakAsyncInputStream->AsyncWait(
|
|
|
|
this, asyncWaitFlags, asyncWaitRequestedCount, asyncWaitEventTarget);
|
2017-04-12 14:49:08 +03:00
|
|
|
}
|
|
|
|
|
2017-02-22 11:41:05 +03:00
|
|
|
// nsIIPCSerializableInputStream
|
|
|
|
|
2019-12-03 16:41:58 +03:00
|
|
|
void SlicedInputStream::Serialize(
|
|
|
|
mozilla::ipc::InputStreamParams& aParams,
|
|
|
|
FileDescriptorArray& aFileDescriptors, bool aDelayedStart,
|
|
|
|
uint32_t aMaxSize, uint32_t* aSizeUsed,
|
|
|
|
mozilla::ipc::ParentToChildStreamActorManager* aManager) {
|
2019-02-05 01:50:51 +03:00
|
|
|
SerializeInternal(aParams, aFileDescriptors, aDelayedStart, aMaxSize,
|
|
|
|
aSizeUsed, aManager);
|
2019-01-28 12:48:35 +03:00
|
|
|
}
|
|
|
|
|
2019-12-03 16:41:58 +03:00
|
|
|
void SlicedInputStream::Serialize(
|
|
|
|
mozilla::ipc::InputStreamParams& aParams,
|
|
|
|
FileDescriptorArray& aFileDescriptors, bool aDelayedStart,
|
|
|
|
uint32_t aMaxSize, uint32_t* aSizeUsed,
|
|
|
|
mozilla::ipc::ChildToParentStreamActorManager* aManager) {
|
2019-02-05 01:50:51 +03:00
|
|
|
SerializeInternal(aParams, aFileDescriptors, aDelayedStart, aMaxSize,
|
|
|
|
aSizeUsed, aManager);
|
2019-01-28 12:48:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename M>
|
|
|
|
void SlicedInputStream::SerializeInternal(
|
|
|
|
mozilla::ipc::InputStreamParams& aParams,
|
2019-02-05 01:50:51 +03:00
|
|
|
FileDescriptorArray& aFileDescriptors, bool aDelayedStart,
|
|
|
|
uint32_t aMaxSize, uint32_t* aSizeUsed, M* aManager) {
|
2017-02-22 11:41:05 +03:00
|
|
|
MOZ_ASSERT(mInputStream);
|
|
|
|
MOZ_ASSERT(mWeakIPCSerializableInputStream);
|
|
|
|
|
|
|
|
SlicedInputStreamParams params;
|
2019-02-05 01:50:51 +03:00
|
|
|
InputStreamHelper::SerializeInputStream(mInputStream, params.stream(),
|
|
|
|
aFileDescriptors, aDelayedStart,
|
|
|
|
aMaxSize, aSizeUsed, aManager);
|
2017-02-22 11:41:05 +03:00
|
|
|
params.start() = mStart;
|
|
|
|
params.length() = mLength;
|
|
|
|
params.curPos() = mCurPos;
|
|
|
|
params.closed() = mClosed;
|
|
|
|
|
|
|
|
aParams = params;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SlicedInputStream::Deserialize(
|
|
|
|
const mozilla::ipc::InputStreamParams& aParams,
|
|
|
|
const FileDescriptorArray& aFileDescriptors) {
|
|
|
|
MOZ_ASSERT(!mInputStream);
|
|
|
|
MOZ_ASSERT(!mWeakIPCSerializableInputStream);
|
|
|
|
|
|
|
|
if (aParams.type() != InputStreamParams::TSlicedInputStreamParams) {
|
|
|
|
NS_ERROR("Received unknown parameters from the other process!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SlicedInputStreamParams& params = aParams.get_SlicedInputStreamParams();
|
|
|
|
|
2017-03-21 09:47:25 +03:00
|
|
|
nsCOMPtr<nsIInputStream> stream = InputStreamHelper::DeserializeInputStream(
|
|
|
|
params.stream(), aFileDescriptors);
|
2017-02-22 11:41:05 +03:00
|
|
|
if (!stream) {
|
|
|
|
NS_WARNING("Deserialize failed!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-05 08:38:48 +03:00
|
|
|
SetSourceStream(stream.forget());
|
2017-02-22 11:41:05 +03:00
|
|
|
|
|
|
|
mStart = params.start();
|
|
|
|
mLength = params.length();
|
|
|
|
mCurPos = params.curPos();
|
|
|
|
mClosed = params.closed();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-22 11:41:59 +03:00
|
|
|
// nsISeekableStream
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SlicedInputStream::Seek(int32_t aWhence, int64_t aOffset) {
|
|
|
|
NS_ENSURE_STATE(mInputStream);
|
|
|
|
NS_ENSURE_STATE(mWeakSeekableInputStream);
|
|
|
|
|
|
|
|
int64_t offset;
|
2017-03-27 12:48:06 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
2017-02-22 11:41:59 +03:00
|
|
|
switch (aWhence) {
|
|
|
|
case NS_SEEK_SET:
|
|
|
|
offset = mStart + aOffset;
|
|
|
|
break;
|
|
|
|
case NS_SEEK_CUR:
|
2017-03-27 12:48:06 +03:00
|
|
|
// mCurPos could be lower than mStart if the reading has not started yet.
|
|
|
|
offset = XPCOM_MAX(mStart, mCurPos) + aOffset;
|
2017-02-22 11:41:59 +03:00
|
|
|
break;
|
2017-03-27 12:48:06 +03:00
|
|
|
case NS_SEEK_END: {
|
|
|
|
uint64_t available;
|
|
|
|
rv = mInputStream->Available(&available);
|
2017-11-06 16:22:47 +03:00
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
|
|
|
mClosed = true;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-03-27 12:48:06 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = XPCOM_MIN(mStart + mLength, available) + aOffset;
|
2017-02-22 11:41:59 +03:00
|
|
|
break;
|
2017-03-27 12:48:06 +03:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
2017-02-22 11:41:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (offset < (int64_t)mStart || offset > (int64_t)(mStart + mLength)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2017-03-27 12:48:06 +03:00
|
|
|
rv = mWeakSeekableInputStream->Seek(NS_SEEK_SET, offset);
|
2017-02-22 11:41:59 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-03-27 12:48:06 +03:00
|
|
|
mCurPos = offset;
|
2017-02-22 11:41:59 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
Bug 1496581 - Split nsISeekableStream in 2 classes: nsISeekableStream and nsITellableStream, f=mayhemer, r=froydnj
In the current code there are 3 main issues:
1. nsFileStream is not really thread-safe. There is nothing to protect the
internal members and we see crashes.
2. nsPipeInputStream doesn't implement ::Seek() method and that caused issues
in devtools when a nsHttpChannel sends POST data using a pipe. In order to fix
this, bug 1494176 added a check in nsHttpChannel: if the stream doesn't
implement ::Seek(), let's clone it. This was an hack around nsPipeInputStream,
and it's bad.
3. When nsHttpChannel sends POST data using a file stream, nsFileStream does
I/O on main-thread because of the issue 2. Plus, ::Seek() is called on the
main-thread causing issue 1.
Note that nsPipeInputStream implements only ::Tell(), of the nsISeekableStream
methods. It doesn't implement ::Seek() and it doesn't implement ::SetEOF().
With this patch I want to fix point 2 and point 3 (and consequentially issue 1
- but we need a separate fix for it - follow up). The patch does:
1. it splits nsISeekableStream in 2 interfaces: nsITellableStream and
nsISeekableStream.
2. nsPipeInputStream implements only nsITellableStream. Doing this, we don't
need the ::Seek() check for point 2 in nsHttpChannel: a simple QI check is
enough.
3. Because we don't call ::Seek() in nsHttpChannel, nsFileStream doesn't do I/O
on the main-thread, and we don't crash doing so.
2018-10-18 14:35:35 +03:00
|
|
|
SlicedInputStream::SetEOF() {
|
2017-02-22 11:41:59 +03:00
|
|
|
NS_ENSURE_STATE(mInputStream);
|
|
|
|
NS_ENSURE_STATE(mWeakSeekableInputStream);
|
|
|
|
|
Bug 1496581 - Split nsISeekableStream in 2 classes: nsISeekableStream and nsITellableStream, f=mayhemer, r=froydnj
In the current code there are 3 main issues:
1. nsFileStream is not really thread-safe. There is nothing to protect the
internal members and we see crashes.
2. nsPipeInputStream doesn't implement ::Seek() method and that caused issues
in devtools when a nsHttpChannel sends POST data using a pipe. In order to fix
this, bug 1494176 added a check in nsHttpChannel: if the stream doesn't
implement ::Seek(), let's clone it. This was an hack around nsPipeInputStream,
and it's bad.
3. When nsHttpChannel sends POST data using a file stream, nsFileStream does
I/O on main-thread because of the issue 2. Plus, ::Seek() is called on the
main-thread causing issue 1.
Note that nsPipeInputStream implements only ::Tell(), of the nsISeekableStream
methods. It doesn't implement ::Seek() and it doesn't implement ::SetEOF().
With this patch I want to fix point 2 and point 3 (and consequentially issue 1
- but we need a separate fix for it - follow up). The patch does:
1. it splits nsISeekableStream in 2 interfaces: nsITellableStream and
nsISeekableStream.
2. nsPipeInputStream implements only nsITellableStream. Doing this, we don't
need the ::Seek() check for point 2 in nsHttpChannel: a simple QI check is
enough.
3. Because we don't call ::Seek() in nsHttpChannel, nsFileStream doesn't do I/O
on the main-thread, and we don't crash doing so.
2018-10-18 14:35:35 +03:00
|
|
|
mClosed = true;
|
|
|
|
return mWeakSeekableInputStream->SetEOF();
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsITellableStream
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SlicedInputStream::Tell(int64_t* aResult) {
|
|
|
|
NS_ENSURE_STATE(mInputStream);
|
|
|
|
NS_ENSURE_STATE(mWeakTellableInputStream);
|
|
|
|
|
2017-02-22 11:41:59 +03:00
|
|
|
int64_t tell = 0;
|
|
|
|
|
Bug 1496581 - Split nsISeekableStream in 2 classes: nsISeekableStream and nsITellableStream, f=mayhemer, r=froydnj
In the current code there are 3 main issues:
1. nsFileStream is not really thread-safe. There is nothing to protect the
internal members and we see crashes.
2. nsPipeInputStream doesn't implement ::Seek() method and that caused issues
in devtools when a nsHttpChannel sends POST data using a pipe. In order to fix
this, bug 1494176 added a check in nsHttpChannel: if the stream doesn't
implement ::Seek(), let's clone it. This was an hack around nsPipeInputStream,
and it's bad.
3. When nsHttpChannel sends POST data using a file stream, nsFileStream does
I/O on main-thread because of the issue 2. Plus, ::Seek() is called on the
main-thread causing issue 1.
Note that nsPipeInputStream implements only ::Tell(), of the nsISeekableStream
methods. It doesn't implement ::Seek() and it doesn't implement ::SetEOF().
With this patch I want to fix point 2 and point 3 (and consequentially issue 1
- but we need a separate fix for it - follow up). The patch does:
1. it splits nsISeekableStream in 2 interfaces: nsITellableStream and
nsISeekableStream.
2. nsPipeInputStream implements only nsITellableStream. Doing this, we don't
need the ::Seek() check for point 2 in nsHttpChannel: a simple QI check is
enough.
3. Because we don't call ::Seek() in nsHttpChannel, nsFileStream doesn't do I/O
on the main-thread, and we don't crash doing so.
2018-10-18 14:35:35 +03:00
|
|
|
nsresult rv = mWeakTellableInputStream->Tell(&tell);
|
2017-02-22 11:41:59 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tell < (int64_t)mStart) {
|
|
|
|
*aResult = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = tell - mStart;
|
|
|
|
if (*aResult > (int64_t)mLength) {
|
|
|
|
*aResult = mLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-05-23 08:12:35 +03:00
|
|
|
// nsIInputStreamLength
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SlicedInputStream::Length(int64_t* aLength) {
|
|
|
|
NS_ENSURE_STATE(mInputStream);
|
|
|
|
NS_ENSURE_STATE(mWeakInputStreamLength);
|
|
|
|
|
|
|
|
nsresult rv = mWeakInputStreamLength->Length(aLength);
|
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
|
|
|
mClosed = true;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*aLength == -1) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aLength = (int64_t)AdjustRange((uint64_t)*aLength);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIAsyncInputStreamLength
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SlicedInputStream::AsyncLengthWait(nsIInputStreamLengthCallback* aCallback,
|
|
|
|
nsIEventTarget* aEventTarget) {
|
|
|
|
NS_ENSURE_STATE(mInputStream);
|
|
|
|
NS_ENSURE_STATE(mWeakAsyncInputStreamLength);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStreamLengthCallback> callback = aCallback ? this : nullptr;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mAsyncWaitLengthCallback = aCallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mWeakAsyncInputStreamLength->AsyncLengthWait(callback, aEventTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIInputStreamLengthCallback
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SlicedInputStream::OnInputStreamLengthReady(nsIAsyncInputStreamLength* aStream,
|
|
|
|
int64_t aLength) {
|
|
|
|
MOZ_ASSERT(mInputStream);
|
|
|
|
MOZ_ASSERT(mWeakAsyncInputStreamLength);
|
|
|
|
MOZ_ASSERT(mWeakAsyncInputStreamLength == aStream);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStreamLengthCallback> callback;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
// We have been canceled in the meanwhile.
|
|
|
|
if (!mAsyncWaitLengthCallback) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback.swap(mAsyncWaitLengthCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aLength != -1) {
|
|
|
|
aLength = (int64_t)AdjustRange((uint64_t)aLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(callback);
|
|
|
|
return callback->OnInputStreamLengthReady(this, aLength);
|
|
|
|
}
|
|
|
|
|
2017-11-07 03:04:06 +03:00
|
|
|
} // namespace mozilla
|