2017-08-11 04:04:54 +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: */
|
|
|
|
/* 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 "FetchStream.h"
|
2017-08-29 12:31:06 +03:00
|
|
|
#include "mozilla/dom/DOMError.h"
|
2017-08-11 04:04:54 +03:00
|
|
|
#include "nsITransport.h"
|
|
|
|
#include "nsIStreamTransportService.h"
|
|
|
|
#include "nsProxyRelease.h"
|
2017-08-11 04:04:55 +03:00
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
#include "Workers.h"
|
2017-08-11 04:04:54 +03:00
|
|
|
|
|
|
|
#define FETCH_STREAM_FLAG 0
|
|
|
|
|
|
|
|
static NS_DEFINE_CID(kStreamTransportServiceCID,
|
|
|
|
NS_STREAMTRANSPORTSERVICE_CID);
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
using namespace workers;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class FetchStreamWorkerHolder final : public WorkerHolder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit FetchStreamWorkerHolder(FetchStream* aStream)
|
|
|
|
: WorkerHolder(WorkerHolder::Behavior::AllowIdleShutdownStart)
|
|
|
|
, mStream(aStream)
|
|
|
|
, mWasNotified(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool Notify(Status aStatus) override
|
|
|
|
{
|
|
|
|
if (!mWasNotified) {
|
|
|
|
mWasNotified = true;
|
|
|
|
mStream->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerPrivate* GetWorkerPrivate() const
|
|
|
|
{
|
|
|
|
return mWorkerPrivate;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<FetchStream> mStream;
|
|
|
|
bool mWasNotified;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FetchStreamWorkerHolderShutdown final : public WorkerControlRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FetchStreamWorkerHolderShutdown(WorkerPrivate* aWorkerPrivate,
|
|
|
|
UniquePtr<WorkerHolder>&& aHolder,
|
2017-08-11 04:04:55 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject>&& aGlobal,
|
|
|
|
RefPtr<FetchStreamHolder>&& aStreamHolder)
|
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount)
|
2017-08-11 04:04:55 +03:00
|
|
|
, mHolder(Move(aHolder))
|
|
|
|
, mGlobal(Move(aGlobal))
|
2017-08-11 04:04:55 +03:00
|
|
|
, mStreamHolder(Move(aStreamHolder))
|
2017-08-11 04:04:55 +03:00
|
|
|
{}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
|
|
{
|
|
|
|
mHolder = nullptr;
|
|
|
|
mGlobal = nullptr;
|
2017-08-11 04:04:55 +03:00
|
|
|
|
|
|
|
mStreamHolder->NullifyStream();
|
|
|
|
mStreamHolder = nullptr;
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
// This runnable starts from a JS Thread. We need to disable a couple of
|
|
|
|
// assertions overring the following methods.
|
|
|
|
|
|
|
|
bool
|
|
|
|
PreDispatch(WorkerPrivate* aWorkerPrivate) override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PostDispatch(WorkerPrivate* aWorkerPrivate, bool aDispatchResult) override
|
|
|
|
{}
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
private:
|
|
|
|
UniquePtr<WorkerHolder> mHolder;
|
|
|
|
nsCOMPtr<nsIGlobalObject> mGlobal;
|
2017-08-11 04:04:55 +03:00
|
|
|
RefPtr<FetchStreamHolder> mStreamHolder;
|
2017-08-11 04:04:55 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(FetchStream, nsIInputStreamCallback, nsIObserver,
|
|
|
|
nsISupportsWeakReference)
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2017-08-11 04:04:56 +03:00
|
|
|
/* static */ void
|
2017-08-11 04:04:55 +03:00
|
|
|
FetchStream::Create(JSContext* aCx, FetchStreamHolder* aStreamHolder,
|
|
|
|
nsIGlobalObject* aGlobal, nsIInputStream* aInputStream,
|
2017-08-11 04:04:56 +03:00
|
|
|
JS::MutableHandle<JSObject*> aStream, ErrorResult& aRv)
|
2017-08-11 04:04:54 +03:00
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aCx);
|
2017-08-11 04:04:55 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aStreamHolder);
|
2017-08-11 04:04:54 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aInputStream);
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
RefPtr<FetchStream> stream =
|
|
|
|
new FetchStream(aGlobal, aStreamHolder, aInputStream);
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (NS_WARN_IF(!os)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2017-08-11 04:04:56 +03:00
|
|
|
return;
|
2017-08-11 04:04:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
aRv = os->AddObserver(stream, DOM_WINDOW_DESTROYED_TOPIC, true);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2017-08-11 04:04:56 +03:00
|
|
|
return;
|
2017-08-11 04:04:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx);
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
|
|
|
|
UniquePtr<FetchStreamWorkerHolder> holder(
|
|
|
|
new FetchStreamWorkerHolder(stream));
|
|
|
|
if (NS_WARN_IF(!holder->HoldWorker(workerPrivate, Closing))) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2017-08-11 04:04:56 +03:00
|
|
|
return;
|
2017-08-11 04:04:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note, this will create a ref-cycle between the holder and the stream.
|
|
|
|
// The cycle is broken when the stream is closed or the worker begins
|
|
|
|
// shutting down.
|
|
|
|
stream->mWorkerHolder = Move(holder);
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
if (!JS::HasReadableStreamCallbacks(aCx)) {
|
|
|
|
JS::SetReadableStreamCallbacks(aCx,
|
|
|
|
&FetchStream::RequestDataCallback,
|
|
|
|
&FetchStream::WriteIntoReadRequestCallback,
|
|
|
|
&FetchStream::CancelCallback,
|
|
|
|
&FetchStream::ClosedCallback,
|
|
|
|
&FetchStream::ErroredCallback,
|
|
|
|
&FetchStream::FinalizeCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> body(aCx,
|
|
|
|
JS::NewReadableExternalSourceStreamObject(aCx, stream, FETCH_STREAM_FLAG));
|
|
|
|
if (!body) {
|
|
|
|
aRv.StealExceptionFromJSContext(aCx);
|
2017-08-11 04:04:56 +03:00
|
|
|
return;
|
2017-08-11 04:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
stream->mReadableStream = body;
|
|
|
|
|
2017-08-11 04:04:56 +03:00
|
|
|
// This will be released in FetchStream::FinalizeCallback(). We are
|
|
|
|
// guaranteed the jsapi will call FinalizeCallback when ReadableStream
|
|
|
|
// js object is finalized.
|
2017-08-11 04:04:54 +03:00
|
|
|
NS_ADDREF(stream.get());
|
2017-08-11 04:04:56 +03:00
|
|
|
|
|
|
|
aStream.set(body);
|
2017-08-11 04:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
FetchStream::RequestDataCallback(JSContext* aCx,
|
|
|
|
JS::HandleObject aStream,
|
|
|
|
void* aUnderlyingSource,
|
|
|
|
uint8_t aFlags,
|
|
|
|
size_t aDesiredSize)
|
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aUnderlyingSource);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aFlags == FETCH_STREAM_FLAG);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(JS::ReadableStreamIsDisturbed(aStream));
|
|
|
|
|
|
|
|
RefPtr<FetchStream> stream = static_cast<FetchStream*>(aUnderlyingSource);
|
|
|
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(stream->mState == eWaiting ||
|
|
|
|
stream->mState == eChecking);
|
|
|
|
|
|
|
|
if (stream->mState == eChecking) {
|
|
|
|
// If we are looking for more data, there is nothing else we should do:
|
|
|
|
// let's move this checking operation in a reading.
|
|
|
|
MOZ_ASSERT(stream->mInputStream);
|
|
|
|
stream->mState = eReading;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->mState = eReading;
|
|
|
|
|
|
|
|
if (!stream->mInputStream) {
|
|
|
|
// This is the first use of the stream. Let's convert the
|
|
|
|
// mOriginalInputStream into an nsIAsyncInputStream.
|
|
|
|
MOZ_ASSERT(stream->mOriginalInputStream);
|
|
|
|
|
|
|
|
bool nonBlocking = false;
|
|
|
|
nsresult rv = stream->mOriginalInputStream->IsNonBlocking(&nonBlocking);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
stream->ErrorPropagation(aCx, aStream, rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> asyncStream =
|
|
|
|
do_QueryInterface(stream->mOriginalInputStream);
|
|
|
|
if (!nonBlocking || !asyncStream) {
|
|
|
|
nsCOMPtr<nsIStreamTransportService> sts =
|
|
|
|
do_GetService(kStreamTransportServiceCID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
stream->ErrorPropagation(aCx, aStream, rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsITransport> transport;
|
|
|
|
rv = sts->CreateInputTransport(stream->mOriginalInputStream,
|
|
|
|
/* aStartOffset */ 0,
|
|
|
|
/* aReadLimit */ -1,
|
|
|
|
/* aCloseWhenDone */ true,
|
|
|
|
getter_AddRefs(transport));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
stream->ErrorPropagation(aCx, aStream, rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> wrapper;
|
|
|
|
rv = transport->OpenInputStream(/* aFlags */ 0,
|
|
|
|
/* aSegmentSize */ 0,
|
|
|
|
/* aSegmentCount */ 0,
|
|
|
|
getter_AddRefs(wrapper));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
stream->ErrorPropagation(aCx, aStream, rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
asyncStream = do_QueryInterface(wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->mInputStream = asyncStream;
|
|
|
|
stream->mOriginalInputStream = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(stream->mInputStream);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!stream->mOriginalInputStream);
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
stream->mInputStream->AsyncWait(stream, 0, 0,
|
2017-08-11 04:04:55 +03:00
|
|
|
stream->mOwningEventTarget);
|
2017-08-11 04:04:54 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
stream->ErrorPropagation(aCx, aStream, rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All good.
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
FetchStream::WriteIntoReadRequestCallback(JSContext* aCx,
|
|
|
|
JS::HandleObject aStream,
|
|
|
|
void* aUnderlyingSource,
|
|
|
|
uint8_t aFlags, void* aBuffer,
|
|
|
|
size_t aLength, size_t* aByteWritten)
|
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aUnderlyingSource);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aFlags == FETCH_STREAM_FLAG);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aBuffer);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aByteWritten);
|
|
|
|
|
|
|
|
RefPtr<FetchStream> stream = static_cast<FetchStream*>(aUnderlyingSource);
|
|
|
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(stream->mInputStream);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(stream->mState == eWriting);
|
|
|
|
stream->mState = eChecking;
|
|
|
|
|
|
|
|
uint32_t written;
|
|
|
|
nsresult rv =
|
|
|
|
stream->mInputStream->Read(static_cast<char*>(aBuffer), aLength, &written);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
stream->ErrorPropagation(aCx, aStream, rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aByteWritten = written;
|
|
|
|
|
|
|
|
if (written == 0) {
|
2017-08-11 04:04:55 +03:00
|
|
|
stream->CloseAndReleaseObjects(aCx, aStream);
|
2017-08-11 04:04:54 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = stream->mInputStream->AsyncWait(stream, 0, 0,
|
2017-08-11 04:04:55 +03:00
|
|
|
stream->mOwningEventTarget);
|
2017-08-11 04:04:54 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
stream->ErrorPropagation(aCx, aStream, rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All good.
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ JS::Value
|
|
|
|
FetchStream::CancelCallback(JSContext* aCx, JS::HandleObject aStream,
|
|
|
|
void* aUnderlyingSource, uint8_t aFlags,
|
|
|
|
JS::HandleValue aReason)
|
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aUnderlyingSource);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aFlags == FETCH_STREAM_FLAG);
|
|
|
|
|
2017-08-11 04:04:56 +03:00
|
|
|
// This is safe because we created an extra reference in FetchStream::Create()
|
|
|
|
// that won't be released until FetchStream::FinalizeCallback() is called.
|
|
|
|
// We are guaranteed that won't happen until the js ReadableStream object
|
|
|
|
// is finalized.
|
|
|
|
FetchStream* stream = static_cast<FetchStream*>(aUnderlyingSource);
|
2017-08-11 04:04:54 +03:00
|
|
|
|
|
|
|
if (stream->mInputStream) {
|
|
|
|
stream->mInputStream->CloseWithStatus(NS_BASE_STREAM_CLOSED);
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
stream->ReleaseObjects();
|
2017-08-11 04:04:54 +03:00
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
FetchStream::ClosedCallback(JSContext* aCx, JS::HandleObject aStream,
|
|
|
|
void* aUnderlyingSource, uint8_t aFlags)
|
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aUnderlyingSource);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aFlags == FETCH_STREAM_FLAG);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
FetchStream::ErroredCallback(JSContext* aCx, JS::HandleObject aStream,
|
|
|
|
void* aUnderlyingSource, uint8_t aFlags,
|
|
|
|
JS::HandleValue aReason)
|
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aUnderlyingSource);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aFlags == FETCH_STREAM_FLAG);
|
2017-08-29 12:31:06 +03:00
|
|
|
|
|
|
|
// This is safe because we created an extra reference in FetchStream::Create()
|
|
|
|
// that won't be released until FetchStream::FinalizeCallback() is called.
|
|
|
|
// We are guaranteed that won't happen until the js ReadableStream object
|
|
|
|
// is finalized.
|
|
|
|
FetchStream* stream = static_cast<FetchStream*>(aUnderlyingSource);
|
|
|
|
|
|
|
|
if (stream->mInputStream) {
|
|
|
|
stream->mInputStream->CloseWithStatus(NS_BASE_STREAM_CLOSED);
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->ReleaseObjects();
|
2017-08-11 04:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FetchStream::FinalizeCallback(void* aUnderlyingSource, uint8_t aFlags)
|
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aUnderlyingSource);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aFlags == FETCH_STREAM_FLAG);
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
// This can be called in any thread.
|
|
|
|
|
2017-08-11 04:04:56 +03:00
|
|
|
// This takes ownership of the ref created in FetchStream::Create().
|
2017-08-11 04:04:54 +03:00
|
|
|
RefPtr<FetchStream> stream =
|
|
|
|
dont_AddRef(static_cast<FetchStream*>(aUnderlyingSource));
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
stream->ReleaseObjects();
|
2017-08-11 04:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
FetchStream::FetchStream(nsIGlobalObject* aGlobal,
|
2017-08-11 04:04:55 +03:00
|
|
|
FetchStreamHolder* aStreamHolder,
|
2017-08-11 04:04:54 +03:00
|
|
|
nsIInputStream* aInputStream)
|
|
|
|
: mState(eWaiting)
|
|
|
|
, mGlobal(aGlobal)
|
2017-08-11 04:04:55 +03:00
|
|
|
, mStreamHolder(aStreamHolder)
|
|
|
|
, mOwningEventTarget(aGlobal->EventTargetFor(TaskCategory::Other))
|
2017-08-11 04:04:54 +03:00
|
|
|
, mOriginalInputStream(aInputStream)
|
|
|
|
, mReadableStream(nullptr)
|
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aInputStream);
|
2017-08-11 04:04:55 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aStreamHolder);
|
2017-08-11 04:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
FetchStream::~FetchStream()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FetchStream::ErrorPropagation(JSContext* aCx, JS::HandleObject aStream,
|
|
|
|
nsresult aError)
|
|
|
|
{
|
|
|
|
// Nothing to do.
|
|
|
|
if (mState == eClosed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's close the stream.
|
|
|
|
if (aError == NS_BASE_STREAM_CLOSED) {
|
2017-08-11 04:04:55 +03:00
|
|
|
CloseAndReleaseObjects(aCx, aStream);
|
2017-08-11 04:04:54 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(mGlobal);
|
|
|
|
|
|
|
|
// Let's use a generic error.
|
|
|
|
RefPtr<DOMError> error = new DOMError(window, NS_ERROR_DOM_TYPE_ERR);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> errorValue(aCx);
|
|
|
|
if (ToJSValue(aCx, error, &errorValue)) {
|
|
|
|
JS::ReadableStreamError(aCx, aStream, errorValue);
|
|
|
|
}
|
2017-08-11 04:04:55 +03:00
|
|
|
|
|
|
|
ReleaseObjects();
|
2017-08-11 04:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FetchStream::OnInputStreamReady(nsIAsyncInputStream* aStream)
|
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aStream);
|
|
|
|
|
|
|
|
// Already closed. We have nothing else to do here.
|
|
|
|
if (mState == eClosed) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mInputStream);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState == eReading || mState == eChecking);
|
|
|
|
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(mGlobal))) {
|
|
|
|
// Without JSContext we are not able to close the stream or to propagate the
|
|
|
|
// error.
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
JS::Rooted<JSObject*> stream(cx, mReadableStream);
|
|
|
|
|
|
|
|
uint64_t size = 0;
|
|
|
|
nsresult rv = mInputStream->Available(&size);
|
|
|
|
if (NS_SUCCEEDED(rv) && size == 0) {
|
|
|
|
// In theory this should not happen. If size is 0, the stream should be
|
|
|
|
// considered closed.
|
|
|
|
rv = NS_BASE_STREAM_CLOSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No warning for stream closed.
|
|
|
|
if (rv == NS_BASE_STREAM_CLOSED || NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
ErrorPropagation(cx, stream, rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This extra checking is completed. Let's wait for the next read request.
|
|
|
|
if (mState == eChecking) {
|
|
|
|
mState = eWaiting;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mState = eWriting;
|
|
|
|
JS::ReadableStreamUpdateDataAvailableFromSource(cx, stream, size);
|
|
|
|
|
|
|
|
// The WriteInto callback changes mState to eChecking.
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState == eChecking);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
/* static */ nsresult
|
|
|
|
FetchStream::RetrieveInputStream(void* aUnderlyingReadableStreamSource,
|
|
|
|
nsIInputStream** aInputStream)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aUnderlyingReadableStreamSource);
|
|
|
|
MOZ_ASSERT(aInputStream);
|
|
|
|
|
|
|
|
RefPtr<FetchStream> stream =
|
|
|
|
static_cast<FetchStream*>(aUnderlyingReadableStreamSource);
|
|
|
|
|
|
|
|
// if mOriginalInputStream is null, the reading already started. We don't want
|
|
|
|
// to expose the internal inputStream.
|
|
|
|
if (NS_WARN_IF(!stream->mOriginalInputStream)) {
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream = stream->mOriginalInputStream;
|
|
|
|
inputStream.forget(aInputStream);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
void
|
|
|
|
FetchStream::Close()
|
|
|
|
{
|
|
|
|
if (mState == eClosed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(mGlobal))) {
|
2017-08-11 04:04:55 +03:00
|
|
|
ReleaseObjects();
|
2017-08-11 04:04:55 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
JS::Rooted<JSObject*> stream(cx, mReadableStream);
|
2017-08-11 04:04:55 +03:00
|
|
|
CloseAndReleaseObjects(cx, stream);
|
2017-08-11 04:04:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-08-11 04:04:55 +03:00
|
|
|
FetchStream::CloseAndReleaseObjects(JSContext* aCx, JS::HandleObject aStream)
|
2017-08-11 04:04:55 +03:00
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState != eClosed);
|
2017-08-11 04:04:55 +03:00
|
|
|
|
|
|
|
ReleaseObjects();
|
|
|
|
|
|
|
|
if (JS::ReadableStreamIsReadable(aStream)) {
|
|
|
|
JS::ReadableStreamClose(aCx, aStream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FetchStream::ReleaseObjects()
|
|
|
|
{
|
|
|
|
if (mState == eClosed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
mState = eClosed;
|
|
|
|
|
|
|
|
if (mWorkerHolder) {
|
|
|
|
RefPtr<FetchStreamWorkerHolderShutdown> r =
|
|
|
|
new FetchStreamWorkerHolderShutdown(
|
|
|
|
static_cast<FetchStreamWorkerHolder*>(mWorkerHolder.get())->GetWorkerPrivate(),
|
2017-08-11 04:04:55 +03:00
|
|
|
Move(mWorkerHolder), Move(mGlobal), Move(mStreamHolder));
|
2017-08-11 04:04:55 +03:00
|
|
|
r->Dispatch();
|
|
|
|
} else {
|
|
|
|
RefPtr<FetchStream> self = this;
|
|
|
|
RefPtr<Runnable> r = NS_NewRunnableFunction(
|
2017-08-11 04:04:55 +03:00
|
|
|
"FetchStream::ReleaseObjects",
|
2017-08-11 04:04:55 +03:00
|
|
|
[self] () {
|
2017-08-11 04:04:55 +03:00
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->RemoveObserver(self, DOM_WINDOW_DESTROYED_TOPIC);
|
2017-08-11 04:04:55 +03:00
|
|
|
}
|
|
|
|
self->mGlobal = nullptr;
|
2017-08-11 04:04:55 +03:00
|
|
|
|
|
|
|
self->mStreamHolder->NullifyStream();
|
|
|
|
self->mStreamHolder = nullptr;
|
2017-08-11 04:04:55 +03:00
|
|
|
});
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
mOwningEventTarget->Dispatch(r.forget());
|
2017-08-11 04:04:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIObserver
|
|
|
|
// -----------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FetchStream::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
MOZ_ASSERT(strcmp(aTopic, DOM_WINDOW_DESTROYED_TOPIC) == 0);
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(mGlobal);
|
|
|
|
if (SameCOMIdentity(aSubject, window)) {
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|