2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2018-01-31 18:45:20 +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 "NonBlockingAsyncInputStream.h"
|
|
|
|
#include "mozilla/ipc/InputStreamUtils.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace ipc;
|
|
|
|
|
2018-01-31 18:45:21 +03:00
|
|
|
class NonBlockingAsyncInputStream::AsyncWaitRunnable final
|
|
|
|
: public CancelableRunnable {
|
2018-01-31 18:45:20 +03:00
|
|
|
RefPtr<NonBlockingAsyncInputStream> mStream;
|
|
|
|
nsCOMPtr<nsIInputStreamCallback> mCallback;
|
|
|
|
|
|
|
|
public:
|
|
|
|
AsyncWaitRunnable(NonBlockingAsyncInputStream* aStream,
|
|
|
|
nsIInputStreamCallback* aCallback)
|
|
|
|
: CancelableRunnable("AsyncWaitRunnable"),
|
|
|
|
mStream(aStream),
|
|
|
|
mCallback(aCallback) {}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
2018-01-31 18:45:21 +03:00
|
|
|
mStream->RunAsyncWaitCallback(this, mCallback.forget());
|
2018-01-31 18:45:20 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2019-01-11 19:57:51 +03:00
|
|
|
|
|
|
|
nsresult Cancel() override {
|
|
|
|
mStream = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-01-31 18:45:20 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(NonBlockingAsyncInputStream);
|
|
|
|
NS_IMPL_RELEASE(NonBlockingAsyncInputStream);
|
|
|
|
|
2018-01-31 18:45:21 +03:00
|
|
|
NonBlockingAsyncInputStream::WaitClosureOnly::WaitClosureOnly(
|
|
|
|
AsyncWaitRunnable* aRunnable, nsIEventTarget* aEventTarget)
|
|
|
|
: mRunnable(aRunnable), mEventTarget(aEventTarget) {}
|
|
|
|
|
2018-01-31 18:45:20 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN(NonBlockingAsyncInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIAsyncInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsICloneableInputStream,
|
|
|
|
mWeakCloneableInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIIPCSerializableInputStream,
|
|
|
|
mWeakIPCSerializableInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsISeekableStream,
|
|
|
|
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
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsITellableStream,
|
|
|
|
mWeakTellableInputStream)
|
2018-01-31 18:45:20 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIInputStream)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2019-02-26 01:14:01 +03:00
|
|
|
/* static */
|
|
|
|
nsresult NonBlockingAsyncInputStream::Create(
|
2018-01-31 18:45:20 +03:00
|
|
|
already_AddRefed<nsIInputStream> aInputStream,
|
2018-01-31 18:45:20 +03:00
|
|
|
nsIAsyncInputStream** aResult) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aResult);
|
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
nsCOMPtr<nsIInputStream> inputStream = std::move(aInputStream);
|
2018-01-31 18:45:20 +03:00
|
|
|
|
2018-01-31 18:45:20 +03:00
|
|
|
bool nonBlocking = false;
|
2018-01-31 18:45:20 +03:00
|
|
|
nsresult rv = inputStream->IsNonBlocking(&nonBlocking);
|
2018-01-31 18:45:20 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(nonBlocking);
|
|
|
|
|
2018-02-14 20:01:57 +03:00
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
2018-01-31 18:45:20 +03:00
|
|
|
nsCOMPtr<nsIAsyncInputStream> asyncInputStream =
|
2018-01-31 18:45:20 +03:00
|
|
|
do_QueryInterface(inputStream);
|
2018-01-31 18:45:20 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!asyncInputStream);
|
2018-02-14 20:01:57 +03:00
|
|
|
#endif // MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
2018-01-31 18:45:20 +03:00
|
|
|
|
|
|
|
RefPtr<NonBlockingAsyncInputStream> stream =
|
2018-01-31 18:45:20 +03:00
|
|
|
new NonBlockingAsyncInputStream(inputStream.forget());
|
2018-01-31 18:45:20 +03:00
|
|
|
|
|
|
|
stream.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-01-31 18:45:20 +03:00
|
|
|
NonBlockingAsyncInputStream::NonBlockingAsyncInputStream(
|
|
|
|
already_AddRefed<nsIInputStream> aInputStream)
|
2018-05-30 22:15:35 +03:00
|
|
|
: mInputStream(std::move(aInputStream)),
|
2018-01-31 18:45:20 +03:00
|
|
|
mWeakCloneableInputStream(nullptr),
|
|
|
|
mWeakIPCSerializableInputStream(nullptr),
|
|
|
|
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),
|
2018-01-31 18:45:21 +03:00
|
|
|
mLock("NonBlockingAsyncInputStream::mLock"),
|
2018-01-31 18:45:20 +03:00
|
|
|
mClosed(false) {
|
|
|
|
MOZ_ASSERT(mInputStream);
|
|
|
|
|
|
|
|
nsCOMPtr<nsICloneableInputStream> cloneableStream =
|
2018-01-31 18:45:20 +03:00
|
|
|
do_QueryInterface(mInputStream);
|
|
|
|
if (cloneableStream && SameCOMIdentity(mInputStream, cloneableStream)) {
|
2018-01-31 18:45:20 +03:00
|
|
|
mWeakCloneableInputStream = cloneableStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIPCSerializableInputStream> serializableStream =
|
2018-01-31 18:45:20 +03:00
|
|
|
do_QueryInterface(mInputStream);
|
|
|
|
if (serializableStream && SameCOMIdentity(mInputStream, serializableStream)) {
|
2018-01-31 18:45:20 +03:00
|
|
|
mWeakIPCSerializableInputStream = serializableStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISeekableStream> seekableStream = do_QueryInterface(mInputStream);
|
2018-01-31 18:45:20 +03:00
|
|
|
if (seekableStream && SameCOMIdentity(mInputStream, seekableStream)) {
|
2018-01-31 18:45:20 +03:00
|
|
|
mWeakSeekableInputStream = seekableStream;
|
|
|
|
}
|
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;
|
|
|
|
}
|
2018-01-31 18:45:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NonBlockingAsyncInputStream::~NonBlockingAsyncInputStream() {}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NonBlockingAsyncInputStream::Close() {
|
2018-01-31 18:45:21 +03:00
|
|
|
RefPtr<AsyncWaitRunnable> waitClosureOnlyRunnable;
|
|
|
|
nsCOMPtr<nsIEventTarget> waitClosureOnlyEventTarget;
|
2018-01-31 18:45:20 +03:00
|
|
|
|
2018-01-31 18:45:21 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
2018-01-31 18:45:20 +03:00
|
|
|
|
2018-01-31 18:45:21 +03:00
|
|
|
if (mClosed) {
|
2018-02-09 23:15:15 +03:00
|
|
|
// Here we could return NS_BASE_STREAM_CLOSED as well, but just to avoid
|
|
|
|
// warning messages, let's make everybody happy with a NS_OK.
|
|
|
|
return NS_OK;
|
2018-01-31 18:45:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mClosed = true;
|
|
|
|
|
|
|
|
NS_ENSURE_STATE(mInputStream);
|
|
|
|
nsresult rv = mInputStream->Close();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mWaitClosureOnly.reset();
|
|
|
|
return rv;
|
|
|
|
}
|
2018-01-31 18:45:20 +03:00
|
|
|
|
2018-01-31 18:45:21 +03:00
|
|
|
// If we have a WaitClosureOnly runnable, it's time to use it.
|
|
|
|
if (mWaitClosureOnly.isSome()) {
|
2018-05-30 22:15:35 +03:00
|
|
|
waitClosureOnlyRunnable = std::move(mWaitClosureOnly->mRunnable);
|
|
|
|
waitClosureOnlyEventTarget = std::move(mWaitClosureOnly->mEventTarget);
|
2018-01-31 18:45:20 +03:00
|
|
|
|
2018-01-31 18:45:21 +03:00
|
|
|
mWaitClosureOnly.reset();
|
2018-01-31 18:45:20 +03:00
|
|
|
|
2018-01-31 18:45:21 +03:00
|
|
|
// Now we want to dispatch the asyncWaitCallback.
|
|
|
|
mAsyncWaitCallback = waitClosureOnlyRunnable;
|
|
|
|
}
|
|
|
|
}
|
2018-01-31 18:45:20 +03:00
|
|
|
|
2018-01-31 18:45:21 +03:00
|
|
|
if (waitClosureOnlyRunnable) {
|
2018-01-31 18:45:20 +03:00
|
|
|
if (waitClosureOnlyEventTarget) {
|
|
|
|
waitClosureOnlyEventTarget->Dispatch(waitClosureOnlyRunnable,
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
} else {
|
|
|
|
waitClosureOnlyRunnable->Run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIInputStream interface
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NonBlockingAsyncInputStream::Available(uint64_t* aLength) {
|
2018-02-27 10:03:00 +03:00
|
|
|
nsresult rv = mInputStream->Available(aLength);
|
2018-12-04 01:48:00 +03:00
|
|
|
// Don't issue warnings for legal condition NS_BASE_STREAM_CLOSED.
|
|
|
|
if (rv == NS_BASE_STREAM_CLOSED || NS_WARN_IF(NS_FAILED(rv))) {
|
2018-02-27 10:03:00 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing more to read. Let's close the stream now.
|
|
|
|
if (*aLength == 0) {
|
|
|
|
mInputStream->Close();
|
|
|
|
mClosed = true;
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2018-01-31 18:45:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NonBlockingAsyncInputStream::Read(char* aBuffer, uint32_t aCount,
|
|
|
|
uint32_t* aReadCount) {
|
|
|
|
return mInputStream->Read(aBuffer, aCount, aReadCount);
|
|
|
|
}
|
|
|
|
|
2018-01-31 18:45:20 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class MOZ_RAII ReadSegmentsData {
|
|
|
|
public:
|
|
|
|
ReadSegmentsData(NonBlockingAsyncInputStream* aStream,
|
|
|
|
nsWriteSegmentFun aFunc, void* aClosure)
|
|
|
|
: mStream(aStream), mFunc(aFunc), mClosure(aClosure) {}
|
|
|
|
|
|
|
|
NonBlockingAsyncInputStream* mStream;
|
|
|
|
nsWriteSegmentFun mFunc;
|
|
|
|
void* mClosure;
|
|
|
|
};
|
|
|
|
|
|
|
|
nsresult ReadSegmentsWriter(nsIInputStream* aInStream, void* aClosure,
|
|
|
|
const char* aFromSegment, uint32_t aToOffset,
|
|
|
|
uint32_t aCount, uint32_t* aWriteCount) {
|
|
|
|
ReadSegmentsData* data = static_cast<ReadSegmentsData*>(aClosure);
|
|
|
|
return data->mFunc(data->mStream, data->mClosure, aFromSegment, aToOffset,
|
|
|
|
aCount, aWriteCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-01-31 18:45:20 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
NonBlockingAsyncInputStream::ReadSegments(nsWriteSegmentFun aWriter,
|
|
|
|
void* aClosure, uint32_t aCount,
|
2018-01-31 18:45:20 +03:00
|
|
|
uint32_t* aResult) {
|
|
|
|
ReadSegmentsData data(this, aWriter, aClosure);
|
|
|
|
return mInputStream->ReadSegments(ReadSegmentsWriter, &data, aCount, aResult);
|
2018-01-31 18:45:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NonBlockingAsyncInputStream::IsNonBlocking(bool* aNonBlocking) {
|
|
|
|
*aNonBlocking = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsICloneableInputStream interface
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NonBlockingAsyncInputStream::GetCloneable(bool* aCloneable) {
|
|
|
|
NS_ENSURE_STATE(mWeakCloneableInputStream);
|
|
|
|
return mWeakCloneableInputStream->GetCloneable(aCloneable);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NonBlockingAsyncInputStream::Clone(nsIInputStream** aResult) {
|
|
|
|
NS_ENSURE_STATE(mWeakCloneableInputStream);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> clonedStream;
|
|
|
|
nsresult rv = mWeakCloneableInputStream->Clone(getter_AddRefs(clonedStream));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> asyncStream;
|
2018-01-31 18:45:20 +03:00
|
|
|
rv = Create(clonedStream.forget(), getter_AddRefs(asyncStream));
|
2018-01-31 18:45:20 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
asyncStream.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIAsyncInputStream interface
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NonBlockingAsyncInputStream::CloseWithStatus(nsresult aStatus) {
|
|
|
|
return Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NonBlockingAsyncInputStream::AsyncWait(nsIInputStreamCallback* aCallback,
|
|
|
|
uint32_t aFlags,
|
|
|
|
uint32_t aRequestedCount,
|
|
|
|
nsIEventTarget* aEventTarget) {
|
2018-01-31 18:45:21 +03:00
|
|
|
RefPtr<AsyncWaitRunnable> runnable;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
2018-01-31 18:45:20 +03:00
|
|
|
|
2018-01-31 18:45:21 +03:00
|
|
|
if (aCallback && (mWaitClosureOnly.isSome() || mAsyncWaitCallback)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2018-01-31 18:45:20 +03:00
|
|
|
|
2018-01-31 18:45:21 +03:00
|
|
|
if (!aCallback) {
|
|
|
|
// Canceling previous callbacks.
|
|
|
|
mWaitClosureOnly.reset();
|
|
|
|
mAsyncWaitCallback = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-01-31 18:45:20 +03:00
|
|
|
|
2018-01-31 18:45:21 +03:00
|
|
|
// Maybe the stream is already closed.
|
|
|
|
if (!mClosed) {
|
|
|
|
uint64_t length;
|
|
|
|
nsresult rv = mInputStream->Available(&length);
|
|
|
|
if (NS_SUCCEEDED(rv) && length == 0) {
|
|
|
|
mInputStream->Close();
|
|
|
|
mClosed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runnable = new AsyncWaitRunnable(this, aCallback);
|
|
|
|
if ((aFlags & nsIAsyncInputStream::WAIT_CLOSURE_ONLY) && !mClosed) {
|
|
|
|
mWaitClosureOnly.emplace(runnable, aEventTarget);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mAsyncWaitCallback = runnable;
|
2018-01-31 18:45:20 +03:00
|
|
|
}
|
|
|
|
|
2018-01-31 18:45:21 +03:00
|
|
|
MOZ_ASSERT(runnable);
|
|
|
|
|
2018-01-31 18:45:20 +03:00
|
|
|
if (aEventTarget) {
|
|
|
|
return aEventTarget->Dispatch(runnable.forget());
|
|
|
|
}
|
|
|
|
|
|
|
|
return runnable->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIIPCSerializableInputStream
|
|
|
|
|
|
|
|
void NonBlockingAsyncInputStream::Serialize(
|
|
|
|
mozilla::ipc::InputStreamParams& aParams,
|
2019-01-28 12:48:35 +03:00
|
|
|
FileDescriptorArray& aFileDescriptors, bool aDelayedStart,
|
2019-02-05 01:50:51 +03:00
|
|
|
uint32_t aMaxSize, uint32_t* aSizeUsed,
|
2019-02-25 23:04:49 +03:00
|
|
|
mozilla::dom::ContentChild* aManager) {
|
2019-02-05 01:50:51 +03:00
|
|
|
SerializeInternal(aParams, aFileDescriptors, aDelayedStart, aMaxSize,
|
|
|
|
aSizeUsed, aManager);
|
2019-01-28 12:48:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void NonBlockingAsyncInputStream::Serialize(
|
|
|
|
mozilla::ipc::InputStreamParams& aParams,
|
|
|
|
FileDescriptorArray& aFileDescriptors, bool aDelayedStart,
|
2019-02-05 01:50:51 +03:00
|
|
|
uint32_t aMaxSize, uint32_t* aSizeUsed,
|
2019-01-28 12:48:35 +03:00
|
|
|
mozilla::ipc::PBackgroundChild* aManager) {
|
2019-02-05 01:50:51 +03:00
|
|
|
SerializeInternal(aParams, aFileDescriptors, aDelayedStart, aMaxSize,
|
|
|
|
aSizeUsed, aManager);
|
2019-01-28 12:48:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void NonBlockingAsyncInputStream::Serialize(
|
|
|
|
mozilla::ipc::InputStreamParams& aParams,
|
|
|
|
FileDescriptorArray& aFileDescriptors, bool aDelayedStart,
|
2019-02-05 01:50:51 +03:00
|
|
|
uint32_t aMaxSize, uint32_t* aSizeUsed,
|
2019-02-25 23:04:47 +03:00
|
|
|
mozilla::dom::ContentParent* aManager) {
|
2019-02-05 01:50:51 +03:00
|
|
|
SerializeInternal(aParams, aFileDescriptors, aDelayedStart, aMaxSize,
|
|
|
|
aSizeUsed, aManager);
|
2019-01-28 12:48:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void NonBlockingAsyncInputStream::Serialize(
|
|
|
|
mozilla::ipc::InputStreamParams& aParams,
|
|
|
|
FileDescriptorArray& aFileDescriptors, bool aDelayedStart,
|
2019-02-05 01:50:51 +03:00
|
|
|
uint32_t aMaxSize, uint32_t* aSizeUsed,
|
2019-01-28 12:48:35 +03:00
|
|
|
mozilla::ipc::PBackgroundParent* 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 NonBlockingAsyncInputStream::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) {
|
2018-01-31 18:45:20 +03:00
|
|
|
MOZ_ASSERT(mWeakIPCSerializableInputStream);
|
2019-02-05 01:50:51 +03:00
|
|
|
InputStreamHelper::SerializeInputStream(mInputStream, aParams,
|
|
|
|
aFileDescriptors, aDelayedStart,
|
|
|
|
aMaxSize, aSizeUsed, aManager);
|
2018-01-31 18:45:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool NonBlockingAsyncInputStream::Deserialize(
|
|
|
|
const mozilla::ipc::InputStreamParams& aParams,
|
|
|
|
const FileDescriptorArray& aFileDescriptors) {
|
|
|
|
MOZ_CRASH("NonBlockingAsyncInputStream cannot be deserialized!");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsISeekableStream
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NonBlockingAsyncInputStream::Seek(int32_t aWhence, int64_t aOffset) {
|
|
|
|
NS_ENSURE_STATE(mWeakSeekableInputStream);
|
|
|
|
return mWeakSeekableInputStream->Seek(aWhence, aOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
NonBlockingAsyncInputStream::SetEOF() {
|
2018-01-31 18:45:20 +03:00
|
|
|
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
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2018-01-31 18:45:20 +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
|
|
|
// nsITellableStream
|
|
|
|
|
2018-01-31 18:45:20 +03:00
|
|
|
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
|
|
|
NonBlockingAsyncInputStream::Tell(int64_t* aResult) {
|
|
|
|
NS_ENSURE_STATE(mWeakTellableInputStream);
|
|
|
|
return mWeakTellableInputStream->Tell(aResult);
|
2018-01-31 18:45:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void NonBlockingAsyncInputStream::RunAsyncWaitCallback(
|
|
|
|
NonBlockingAsyncInputStream::AsyncWaitRunnable* aRunnable,
|
|
|
|
already_AddRefed<nsIInputStreamCallback> aCallback) {
|
2018-05-30 22:15:35 +03:00
|
|
|
nsCOMPtr<nsIInputStreamCallback> callback = std::move(aCallback);
|
2018-01-31 18:45:21 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
if (mAsyncWaitCallback != aRunnable) {
|
|
|
|
// The callback has been canceled in the meantime.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mAsyncWaitCallback = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback->OnInputStreamReady(this);
|
2018-01-31 18:45:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|