2017-06-20 18:53:21 +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 "Fetch.h"
|
|
|
|
#include "FetchConsumer.h"
|
|
|
|
|
2018-01-31 10:25:30 +03:00
|
|
|
#include "mozilla/dom/WorkerCommon.h"
|
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
|
|
|
#include "mozilla/dom/WorkerRunnable.h"
|
|
|
|
#include "mozilla/dom/WorkerScope.h"
|
2018-01-12 20:16:04 +03:00
|
|
|
#include "mozilla/ipc/PBackgroundSharedTypes.h"
|
2017-06-20 18:53:21 +03:00
|
|
|
#include "nsIInputStreamPump.h"
|
2017-06-20 18:53:21 +03:00
|
|
|
#include "nsProxyRelease.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
template <class Derived>
|
2018-01-31 10:23:44 +03:00
|
|
|
class FetchBodyWorkerHolder final : public WorkerHolder
|
2017-06-20 18:53:21 +03:00
|
|
|
{
|
2017-06-20 18:53:21 +03:00
|
|
|
RefPtr<FetchBodyConsumer<Derived>> mConsumer;
|
2017-06-20 18:53:21 +03:00
|
|
|
bool mWasNotified;
|
|
|
|
|
|
|
|
public:
|
2017-06-20 18:53:21 +03:00
|
|
|
explicit FetchBodyWorkerHolder(FetchBodyConsumer<Derived>* aConsumer)
|
2018-01-31 10:23:44 +03:00
|
|
|
: WorkerHolder("FetchBodyWorkerHolder")
|
2017-11-15 09:58:38 +03:00
|
|
|
, mConsumer(aConsumer)
|
2017-06-20 18:53:21 +03:00
|
|
|
, mWasNotified(false)
|
|
|
|
{
|
2017-06-20 18:53:21 +03:00
|
|
|
MOZ_ASSERT(aConsumer);
|
2017-06-20 18:53:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
~FetchBodyWorkerHolder() = default;
|
|
|
|
|
2018-01-31 10:23:44 +03:00
|
|
|
bool Notify(WorkerStatus aStatus) override
|
2017-06-20 18:53:21 +03:00
|
|
|
{
|
2018-01-31 10:23:44 +03:00
|
|
|
MOZ_ASSERT(aStatus > Running);
|
2017-06-20 18:53:21 +03:00
|
|
|
if (!mWasNotified) {
|
|
|
|
mWasNotified = true;
|
2017-09-04 18:06:47 +03:00
|
|
|
mConsumer->ShutDownMainThreadConsuming();
|
2017-06-20 18:53:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
template <class Derived>
|
|
|
|
class BeginConsumeBodyRunnable final : public Runnable
|
|
|
|
{
|
|
|
|
RefPtr<FetchBodyConsumer<Derived>> mFetchBodyConsumer;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit BeginConsumeBodyRunnable(FetchBodyConsumer<Derived>* aConsumer)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("BeginConsumeBodyRunnable")
|
|
|
|
, mFetchBodyConsumer(aConsumer)
|
2017-06-20 18:53:21 +03:00
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override
|
|
|
|
{
|
|
|
|
mFetchBodyConsumer->BeginConsumeBodyMainThread();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called on successfully reading the complete stream.
|
|
|
|
*/
|
|
|
|
template <class Derived>
|
|
|
|
class ContinueConsumeBodyRunnable final : public MainThreadWorkerRunnable
|
|
|
|
{
|
|
|
|
RefPtr<FetchBodyConsumer<Derived>> mFetchBodyConsumer;
|
|
|
|
nsresult mStatus;
|
|
|
|
uint32_t mLength;
|
|
|
|
uint8_t* mResult;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ContinueConsumeBodyRunnable(FetchBodyConsumer<Derived>* aFetchBodyConsumer,
|
|
|
|
nsresult aStatus, uint32_t aLength,
|
|
|
|
uint8_t* aResult)
|
|
|
|
: MainThreadWorkerRunnable(aFetchBodyConsumer->GetWorkerPrivate())
|
|
|
|
, mFetchBodyConsumer(aFetchBodyConsumer)
|
|
|
|
, mStatus(aStatus)
|
|
|
|
, mLength(aLength)
|
|
|
|
, mResult(aResult)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
|
|
{
|
|
|
|
mFetchBodyConsumer->ContinueConsumeBody(mStatus, mLength, mResult);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-05 21:55:08 +03:00
|
|
|
// ControlRunnable used to complete the releasing of resources on the worker
|
|
|
|
// thread when already shutting down.
|
|
|
|
template <class Derived>
|
|
|
|
class ContinueConsumeBodyControlRunnable final : public MainThreadWorkerControlRunnable
|
|
|
|
{
|
|
|
|
RefPtr<FetchBodyConsumer<Derived>> mFetchBodyConsumer;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ContinueConsumeBodyControlRunnable(FetchBodyConsumer<Derived>* aFetchBodyConsumer,
|
|
|
|
uint8_t* aResult)
|
|
|
|
: MainThreadWorkerControlRunnable(aFetchBodyConsumer->GetWorkerPrivate())
|
|
|
|
, mFetchBodyConsumer(aFetchBodyConsumer)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
free(aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
|
|
{
|
|
|
|
mFetchBodyConsumer->ContinueConsumeBody(NS_BINDING_ABORTED, 0, nullptr,
|
|
|
|
true /* shutting down */);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
template <class Derived>
|
|
|
|
class FailConsumeBodyWorkerRunnable : public MainThreadWorkerControlRunnable
|
|
|
|
{
|
|
|
|
RefPtr<FetchBodyConsumer<Derived>> mBodyConsumer;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit FailConsumeBodyWorkerRunnable(FetchBodyConsumer<Derived>* aBodyConsumer)
|
|
|
|
: MainThreadWorkerControlRunnable(aBodyConsumer->GetWorkerPrivate())
|
|
|
|
, mBodyConsumer(aBodyConsumer)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
|
|
{
|
|
|
|
mBodyConsumer->ContinueConsumeBody(NS_ERROR_FAILURE, 0, nullptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In case of failure to create a stream pump or dispatch stream completion to
|
|
|
|
* worker, ensure we cleanup properly. Thread agnostic.
|
|
|
|
*/
|
|
|
|
template <class Derived>
|
|
|
|
class MOZ_STACK_CLASS AutoFailConsumeBody final
|
|
|
|
{
|
|
|
|
RefPtr<FetchBodyConsumer<Derived>> mBodyConsumer;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit AutoFailConsumeBody(FetchBodyConsumer<Derived>* aBodyConsumer)
|
|
|
|
: mBodyConsumer(aBodyConsumer)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~AutoFailConsumeBody()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (mBodyConsumer) {
|
|
|
|
if (mBodyConsumer->GetWorkerPrivate()) {
|
|
|
|
RefPtr<FailConsumeBodyWorkerRunnable<Derived>> r =
|
|
|
|
new FailConsumeBodyWorkerRunnable<Derived>(mBodyConsumer);
|
|
|
|
if (!r->Dispatch()) {
|
|
|
|
MOZ_CRASH("We are going to leak");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mBodyConsumer->ContinueConsumeBody(NS_ERROR_FAILURE, 0, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DontFail()
|
|
|
|
{
|
|
|
|
mBodyConsumer = nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called on successfully reading the complete stream for Blob.
|
|
|
|
*/
|
|
|
|
template <class Derived>
|
|
|
|
class ContinueConsumeBlobBodyRunnable final : public MainThreadWorkerRunnable
|
|
|
|
{
|
|
|
|
RefPtr<FetchBodyConsumer<Derived>> mFetchBodyConsumer;
|
|
|
|
RefPtr<BlobImpl> mBlobImpl;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ContinueConsumeBlobBodyRunnable(FetchBodyConsumer<Derived>* aFetchBodyConsumer,
|
|
|
|
BlobImpl* aBlobImpl)
|
|
|
|
: MainThreadWorkerRunnable(aFetchBodyConsumer->GetWorkerPrivate())
|
|
|
|
, mFetchBodyConsumer(aFetchBodyConsumer)
|
|
|
|
, mBlobImpl(aBlobImpl)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mBlobImpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
|
|
{
|
|
|
|
mFetchBodyConsumer->ContinueConsumeBlobBody(mBlobImpl);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-05 21:55:08 +03:00
|
|
|
// ControlRunnable used to complete the releasing of resources on the worker
|
|
|
|
// thread when already shutting down.
|
|
|
|
template <class Derived>
|
|
|
|
class ContinueConsumeBlobBodyControlRunnable final
|
|
|
|
: public MainThreadWorkerControlRunnable
|
|
|
|
{
|
|
|
|
RefPtr<FetchBodyConsumer<Derived>> mFetchBodyConsumer;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ContinueConsumeBlobBodyControlRunnable(FetchBodyConsumer<Derived>* aFetchBodyConsumer)
|
|
|
|
: MainThreadWorkerControlRunnable(aFetchBodyConsumer->GetWorkerPrivate())
|
|
|
|
, mFetchBodyConsumer(aFetchBodyConsumer)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
|
|
{
|
|
|
|
mFetchBodyConsumer->ContinueConsumeBlobBody(nullptr,
|
|
|
|
true /* shutting down */);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
template <class Derived>
|
|
|
|
class ConsumeBodyDoneObserver : public nsIStreamLoaderObserver
|
|
|
|
, public MutableBlobStorageCallback
|
|
|
|
{
|
|
|
|
RefPtr<FetchBodyConsumer<Derived>> mFetchBodyConsumer;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
explicit ConsumeBodyDoneObserver(FetchBodyConsumer<Derived>* aFetchBodyConsumer)
|
|
|
|
: mFetchBodyConsumer(aFetchBodyConsumer)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
OnStreamComplete(nsIStreamLoader* aLoader,
|
|
|
|
nsISupports* aCtxt,
|
|
|
|
nsresult aStatus,
|
|
|
|
uint32_t aResultLength,
|
|
|
|
const uint8_t* aResult) override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2017-07-10 23:07:44 +03:00
|
|
|
// The loading is completed. Let's nullify the pump before continuing the
|
|
|
|
// consuming of the body.
|
|
|
|
mFetchBodyConsumer->NullifyConsumeBodyPump();
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
uint8_t* nonconstResult = const_cast<uint8_t*>(aResult);
|
2018-02-05 21:55:08 +03:00
|
|
|
if (!mFetchBodyConsumer->GetWorkerPrivate()) {
|
|
|
|
mFetchBodyConsumer->ContinueConsumeBody(aStatus, aResultLength,
|
|
|
|
nonconstResult);
|
|
|
|
// FetchBody is responsible for data.
|
|
|
|
return NS_SUCCESS_ADOPTED_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-06-20 18:53:21 +03:00
|
|
|
RefPtr<ContinueConsumeBodyRunnable<Derived>> r =
|
|
|
|
new ContinueConsumeBodyRunnable<Derived>(mFetchBodyConsumer,
|
|
|
|
aStatus,
|
|
|
|
aResultLength,
|
|
|
|
nonconstResult);
|
2018-02-05 21:55:08 +03:00
|
|
|
if (r->Dispatch()) {
|
|
|
|
// FetchBody is responsible for data.
|
|
|
|
return NS_SUCCESS_ADOPTED_DATA;
|
2017-06-20 18:53:21 +03:00
|
|
|
}
|
2018-02-05 21:55:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The worker is shutting down. Let's use a control runnable to complete the
|
|
|
|
// shutting down procedure.
|
|
|
|
|
|
|
|
RefPtr<ContinueConsumeBodyControlRunnable<Derived>> r =
|
|
|
|
new ContinueConsumeBodyControlRunnable<Derived>(mFetchBodyConsumer,
|
|
|
|
nonconstResult);
|
|
|
|
if (NS_WARN_IF(!r->Dispatch())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2017-06-20 18:53:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// FetchBody is responsible for data.
|
|
|
|
return NS_SUCCESS_ADOPTED_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void BlobStoreCompleted(MutableBlobStorage* aBlobStorage,
|
|
|
|
Blob* aBlob,
|
|
|
|
nsresult aRv) override
|
|
|
|
{
|
|
|
|
// On error.
|
|
|
|
if (NS_FAILED(aRv)) {
|
|
|
|
OnStreamComplete(nullptr, nullptr, aRv, 0, nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-04 18:06:47 +03:00
|
|
|
// The loading is completed. Let's nullify the pump before continuing the
|
|
|
|
// consuming of the body.
|
|
|
|
mFetchBodyConsumer->NullifyConsumeBodyPump();
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
MOZ_ASSERT(aBlob);
|
|
|
|
|
2018-02-05 21:55:08 +03:00
|
|
|
if (!mFetchBodyConsumer->GetWorkerPrivate()) {
|
|
|
|
mFetchBodyConsumer->ContinueConsumeBlobBody(aBlob->Impl());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-06-20 18:53:21 +03:00
|
|
|
RefPtr<ContinueConsumeBlobBodyRunnable<Derived>> r =
|
|
|
|
new ContinueConsumeBlobBodyRunnable<Derived>(mFetchBodyConsumer,
|
|
|
|
aBlob->Impl());
|
|
|
|
|
2018-02-05 21:55:08 +03:00
|
|
|
if (r->Dispatch()) {
|
2017-06-20 18:53:21 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-02-05 21:55:08 +03:00
|
|
|
|
|
|
|
// The worker is shutting down. Let's use a control runnable to complete the
|
|
|
|
// shutting down procedure.
|
|
|
|
|
|
|
|
RefPtr<ContinueConsumeBlobBodyControlRunnable<Derived>> r =
|
|
|
|
new ContinueConsumeBlobBodyControlRunnable<Derived>(mFetchBodyConsumer);
|
|
|
|
|
|
|
|
Unused << NS_WARN_IF(!r->Dispatch());
|
2017-06-20 18:53:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~ConsumeBodyDoneObserver()
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
NS_IMPL_ADDREF(ConsumeBodyDoneObserver<Derived>)
|
|
|
|
template <class Derived>
|
|
|
|
NS_IMPL_RELEASE(ConsumeBodyDoneObserver<Derived>)
|
|
|
|
template <class Derived>
|
|
|
|
NS_INTERFACE_MAP_BEGIN(ConsumeBodyDoneObserver<Derived>)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStreamLoaderObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIStreamLoaderObserver)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
} // anonymous
|
|
|
|
|
|
|
|
template <class Derived>
|
2017-06-20 18:53:21 +03:00
|
|
|
/* static */ already_AddRefed<Promise>
|
|
|
|
FetchBodyConsumer<Derived>::Create(nsIGlobalObject* aGlobal,
|
|
|
|
nsIEventTarget* aMainThreadEventTarget,
|
|
|
|
FetchBody<Derived>* aBody,
|
2017-08-29 12:31:06 +03:00
|
|
|
AbortSignal* aSignal,
|
2017-06-20 18:53:21 +03:00
|
|
|
FetchConsumeType aType,
|
|
|
|
ErrorResult& aRv)
|
2017-06-20 18:53:21 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aBody);
|
2017-06-20 18:53:21 +03:00
|
|
|
MOZ_ASSERT(aMainThreadEventTarget);
|
2017-06-20 18:53:21 +03:00
|
|
|
|
2017-09-04 18:06:18 +03:00
|
|
|
nsCOMPtr<nsIInputStream> bodyStream;
|
|
|
|
aBody->DerivedClass()->GetBody(getter_AddRefs(bodyStream));
|
|
|
|
if (!bodyStream) {
|
|
|
|
aRv = NS_NewCStringInputStream(getter_AddRefs(bodyStream), EmptyCString());
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
RefPtr<Promise> promise = Promise::Create(aGlobal, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-06-20 18:53:21 +03:00
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
WorkerPrivate* workerPrivate = nullptr;
|
2017-06-20 18:53:21 +03:00
|
|
|
if (!NS_IsMainThread()) {
|
2017-06-20 18:53:21 +03:00
|
|
|
workerPrivate = GetCurrentThreadWorkerPrivate();
|
2017-06-20 18:53:21 +03:00
|
|
|
MOZ_ASSERT(workerPrivate);
|
2017-06-20 18:53:21 +03:00
|
|
|
}
|
2017-06-20 18:53:21 +03:00
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
RefPtr<FetchBodyConsumer<Derived>> consumer =
|
2017-06-22 14:16:32 +03:00
|
|
|
new FetchBodyConsumer<Derived>(aMainThreadEventTarget, aGlobal,
|
2017-09-04 18:06:18 +03:00
|
|
|
workerPrivate, aBody, bodyStream, promise,
|
|
|
|
aType);
|
2017-06-20 18:53:21 +03:00
|
|
|
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
2017-09-04 18:06:31 +03:00
|
|
|
if (NS_WARN_IF(!consumer->RegisterWorkerHolder())) {
|
2017-06-20 18:53:21 +03:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2017-06-20 18:53:21 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-06-22 14:16:32 +03:00
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (NS_WARN_IF(!os)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
aRv = os->AddObserver(consumer, DOM_WINDOW_DESTROYED_TOPIC, true);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
aRv = os->AddObserver(consumer, DOM_WINDOW_FROZEN_TOPIC, true);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-06-20 18:53:21 +03:00
|
|
|
}
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r = new BeginConsumeBodyRunnable<Derived>(consumer);
|
|
|
|
aRv = aMainThreadEventTarget->Dispatch(r.forget(), NS_DISPATCH_NORMAL);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
if (aSignal) {
|
|
|
|
consumer->Follow(aSignal);
|
|
|
|
}
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
return promise.forget();
|
2017-06-20 18:53:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
2017-06-20 18:53:21 +03:00
|
|
|
FetchBodyConsumer<Derived>::ReleaseObject()
|
2017-06-20 18:53:21 +03:00
|
|
|
{
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2017-06-22 14:16:32 +03:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (os) {
|
|
|
|
os->RemoveObserver(this, DOM_WINDOW_DESTROYED_TOPIC);
|
|
|
|
os->RemoveObserver(this, DOM_WINDOW_FROZEN_TOPIC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mGlobal = nullptr;
|
2017-06-20 18:53:21 +03:00
|
|
|
mWorkerHolder = nullptr;
|
2017-09-04 18:06:18 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2017-06-20 18:53:21 +03:00
|
|
|
mBody = nullptr;
|
2017-09-04 18:06:18 +03:00
|
|
|
#endif
|
2017-08-29 12:31:06 +03:00
|
|
|
|
|
|
|
Unfollow();
|
2017-06-20 18:53:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
2017-06-20 18:53:21 +03:00
|
|
|
FetchBodyConsumer<Derived>::FetchBodyConsumer(nsIEventTarget* aMainThreadEventTarget,
|
2017-06-22 14:16:32 +03:00
|
|
|
nsIGlobalObject* aGlobalObject,
|
2017-06-20 18:53:21 +03:00
|
|
|
WorkerPrivate* aWorkerPrivate,
|
|
|
|
FetchBody<Derived>* aBody,
|
2017-09-04 18:06:18 +03:00
|
|
|
nsIInputStream* aBodyStream,
|
2017-06-20 18:53:21 +03:00
|
|
|
Promise* aPromise,
|
|
|
|
FetchConsumeType aType)
|
2017-06-20 18:53:21 +03:00
|
|
|
: mTargetThread(NS_GetCurrentThread())
|
2017-06-20 18:53:21 +03:00
|
|
|
, mMainThreadEventTarget(aMainThreadEventTarget)
|
2017-09-04 18:06:18 +03:00
|
|
|
#ifdef DEBUG
|
2017-06-20 18:53:21 +03:00
|
|
|
, mBody(aBody)
|
2017-09-04 18:06:18 +03:00
|
|
|
#endif
|
|
|
|
, mBodyStream(aBodyStream)
|
|
|
|
, mBlobStorageType(MutableBlobStorage::eOnlyInMemory)
|
2017-06-22 14:16:32 +03:00
|
|
|
, mGlobal(aGlobalObject)
|
2017-06-20 18:53:21 +03:00
|
|
|
, mWorkerPrivate(aWorkerPrivate)
|
|
|
|
, mConsumeType(aType)
|
|
|
|
, mConsumePromise(aPromise)
|
2017-06-28 21:28:42 +03:00
|
|
|
, mBodyConsumed(false)
|
2017-07-10 23:07:44 +03:00
|
|
|
, mShuttingDown(false)
|
2017-06-20 18:53:21 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aMainThreadEventTarget);
|
|
|
|
MOZ_ASSERT(aBody);
|
2017-09-04 18:06:18 +03:00
|
|
|
MOZ_ASSERT(aBodyStream);
|
2017-06-20 18:53:21 +03:00
|
|
|
MOZ_ASSERT(aPromise);
|
2017-09-04 18:06:18 +03:00
|
|
|
|
|
|
|
const mozilla::UniquePtr<mozilla::ipc::PrincipalInfo>& principalInfo =
|
|
|
|
aBody->DerivedClass()->GetPrincipalInfo();
|
|
|
|
// We support temporary file for blobs only if the principal is known and
|
|
|
|
// it's system or content not in private Browsing.
|
|
|
|
if (principalInfo &&
|
|
|
|
(principalInfo->type() == mozilla::ipc::PrincipalInfo::TSystemPrincipalInfo ||
|
|
|
|
(principalInfo->type() == mozilla::ipc::PrincipalInfo::TContentPrincipalInfo &&
|
|
|
|
principalInfo->get_ContentPrincipalInfo().attrs().mPrivateBrowsingId == 0))) {
|
|
|
|
mBlobStorageType = MutableBlobStorage::eCouldBeInTemporaryFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBodyMimeType = aBody->MimeType();
|
2017-06-20 18:53:21 +03:00
|
|
|
}
|
2017-06-20 18:53:21 +03:00
|
|
|
|
|
|
|
template <class Derived>
|
2017-06-20 18:53:21 +03:00
|
|
|
FetchBodyConsumer<Derived>::~FetchBodyConsumer()
|
2017-06-20 18:53:21 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
2017-06-20 18:53:21 +03:00
|
|
|
FetchBodyConsumer<Derived>::AssertIsOnTargetThread() const
|
2017-06-20 18:53:21 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == mTargetThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
bool
|
2017-09-04 18:06:31 +03:00
|
|
|
FetchBodyConsumer<Derived>::RegisterWorkerHolder()
|
2017-06-20 18:53:21 +03:00
|
|
|
{
|
2017-09-04 18:06:31 +03:00
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2017-06-20 18:53:21 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(!mWorkerHolder);
|
|
|
|
mWorkerHolder.reset(new FetchBodyWorkerHolder<Derived>(this));
|
|
|
|
|
2017-09-04 18:06:31 +03:00
|
|
|
if (!mWorkerHolder->HoldWorker(mWorkerPrivate, Closing)) {
|
2017-06-20 18:53:21 +03:00
|
|
|
NS_WARNING("Failed to add workerHolder");
|
|
|
|
mWorkerHolder = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
/*
|
|
|
|
* BeginConsumeBodyMainThread() will automatically reject the consume promise
|
|
|
|
* and clean up on any failures, so there is no need for callers to do so,
|
|
|
|
* reflected in a lack of error return code.
|
|
|
|
*/
|
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
FetchBodyConsumer<Derived>::BeginConsumeBodyMainThread()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2017-09-04 18:06:47 +03:00
|
|
|
AutoFailConsumeBody<Derived> autoReject(this);
|
|
|
|
|
2017-07-10 23:07:44 +03:00
|
|
|
if (mShuttingDown) {
|
2017-09-04 18:06:47 +03:00
|
|
|
// We haven't started yet, but we have been terminated. AutoFailConsumeBody
|
|
|
|
// will dispatch a runnable to release resources.
|
2017-07-10 23:07:44 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
nsCOMPtr<nsIInputStreamPump> pump;
|
2017-09-04 18:06:18 +03:00
|
|
|
nsresult rv = NS_NewInputStreamPump(getter_AddRefs(pump),
|
2017-11-22 13:18:57 +03:00
|
|
|
mBodyStream.forget(), 0, 0, false,
|
2017-09-04 18:06:18 +03:00
|
|
|
mMainThreadEventTarget);
|
2017-06-20 18:53:21 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ConsumeBodyDoneObserver<Derived>> p =
|
|
|
|
new ConsumeBodyDoneObserver<Derived>(this);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamListener> listener;
|
|
|
|
if (mConsumeType == CONSUME_BLOB) {
|
2017-09-04 18:06:18 +03:00
|
|
|
listener = new MutableBlobStreamListener(mBlobStorageType, nullptr,
|
|
|
|
mBodyMimeType, p,
|
|
|
|
mMainThreadEventTarget);
|
2017-06-20 18:53:21 +03:00
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIStreamLoader> loader;
|
|
|
|
rv = NS_NewStreamLoader(getter_AddRefs(loader), p);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
listener = loader;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = pump->AsyncRead(listener, nullptr);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that everything succeeded, we can assign the pump to a pointer that
|
2017-07-10 23:07:44 +03:00
|
|
|
// stays alive for the lifetime of the FetchConsumer.
|
|
|
|
mConsumeBodyPump = pump;
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
// It is ok for retargeting to fail and reads to happen on the main thread.
|
|
|
|
autoReject.DontFail();
|
|
|
|
|
|
|
|
// Try to retarget, otherwise fall back to main thread.
|
|
|
|
nsCOMPtr<nsIThreadRetargetableRequest> rr = do_QueryInterface(pump);
|
|
|
|
if (rr) {
|
|
|
|
nsCOMPtr<nsIEventTarget> sts = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
|
|
|
|
rv = rr->RetargetDeliveryTo(sts);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
NS_WARNING("Retargeting failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
FetchBodyConsumer<Derived>::ContinueConsumeBody(nsresult aStatus,
|
|
|
|
uint32_t aResultLength,
|
2018-02-05 21:55:08 +03:00
|
|
|
uint8_t* aResult,
|
|
|
|
bool aShuttingDown)
|
2017-06-20 18:53:21 +03:00
|
|
|
{
|
|
|
|
AssertIsOnTargetThread();
|
2017-06-28 21:28:42 +03:00
|
|
|
|
|
|
|
if (mBodyConsumed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mBodyConsumed = true;
|
2017-06-20 18:53:21 +03:00
|
|
|
|
2017-07-10 23:07:44 +03:00
|
|
|
// Just a precaution to ensure ContinueConsumeBody is not called out of
|
|
|
|
// sync with a body read.
|
|
|
|
MOZ_ASSERT(mBody->BodyUsed());
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
auto autoFree = mozilla::MakeScopeExit([&] {
|
|
|
|
free(aResult);
|
|
|
|
});
|
|
|
|
|
|
|
|
MOZ_ASSERT(mConsumePromise);
|
|
|
|
RefPtr<Promise> localPromise = mConsumePromise.forget();
|
|
|
|
|
|
|
|
RefPtr<FetchBodyConsumer<Derived>> self = this;
|
|
|
|
auto autoReleaseObject = mozilla::MakeScopeExit([&] {
|
|
|
|
self->ReleaseObject();
|
|
|
|
});
|
|
|
|
|
2018-02-05 21:55:08 +03:00
|
|
|
if (aShuttingDown) {
|
|
|
|
// If shutting down, we don't want to resolve any promise.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(aStatus))) {
|
|
|
|
localPromise->MaybeReject(NS_ERROR_DOM_ABORT_ERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't warn here since we warned above.
|
|
|
|
if (NS_FAILED(aStatus)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finish successfully consuming body according to type.
|
|
|
|
MOZ_ASSERT(aResult);
|
|
|
|
|
|
|
|
AutoJSAPI jsapi;
|
2017-09-04 18:06:18 +03:00
|
|
|
if (!jsapi.Init(mGlobal)) {
|
2017-06-20 18:53:21 +03:00
|
|
|
localPromise->MaybeReject(NS_ERROR_UNEXPECTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
ErrorResult error;
|
|
|
|
|
|
|
|
switch (mConsumeType) {
|
|
|
|
case CONSUME_ARRAYBUFFER: {
|
|
|
|
JS::Rooted<JSObject*> arrayBuffer(cx);
|
|
|
|
BodyUtil::ConsumeArrayBuffer(cx, &arrayBuffer, aResultLength, aResult,
|
|
|
|
error);
|
|
|
|
|
|
|
|
if (!error.Failed()) {
|
|
|
|
JS::Rooted<JS::Value> val(cx);
|
|
|
|
val.setObjectOrNull(arrayBuffer);
|
|
|
|
|
|
|
|
localPromise->MaybeResolve(cx, val);
|
|
|
|
// ArrayBuffer takes over ownership.
|
|
|
|
aResult = nullptr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CONSUME_BLOB: {
|
|
|
|
MOZ_CRASH("This should not happen.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CONSUME_FORMDATA: {
|
|
|
|
nsCString data;
|
|
|
|
data.Adopt(reinterpret_cast<char*>(aResult), aResultLength);
|
|
|
|
aResult = nullptr;
|
|
|
|
|
|
|
|
RefPtr<dom::FormData> fd =
|
2017-09-04 18:06:18 +03:00
|
|
|
BodyUtil::ConsumeFormData(mGlobal, mBodyMimeType, data, error);
|
2017-06-20 18:53:21 +03:00
|
|
|
if (!error.Failed()) {
|
|
|
|
localPromise->MaybeResolve(fd);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CONSUME_TEXT:
|
|
|
|
// fall through handles early exit.
|
|
|
|
case CONSUME_JSON: {
|
|
|
|
nsString decoded;
|
|
|
|
if (NS_SUCCEEDED(BodyUtil::ConsumeText(aResultLength, aResult, decoded))) {
|
|
|
|
if (mConsumeType == CONSUME_TEXT) {
|
|
|
|
localPromise->MaybeResolve(decoded);
|
|
|
|
} else {
|
|
|
|
JS::Rooted<JS::Value> json(cx);
|
|
|
|
BodyUtil::ConsumeJson(cx, &json, decoded, error);
|
|
|
|
if (!error.Failed()) {
|
|
|
|
localPromise->MaybeResolve(cx, json);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unexpected consume body type");
|
|
|
|
}
|
|
|
|
|
|
|
|
error.WouldReportJSException();
|
|
|
|
if (error.Failed()) {
|
|
|
|
localPromise->MaybeReject(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
2018-02-05 21:55:08 +03:00
|
|
|
FetchBodyConsumer<Derived>::ContinueConsumeBlobBody(BlobImpl* aBlobImpl,
|
|
|
|
bool aShuttingDown)
|
2017-06-20 18:53:21 +03:00
|
|
|
{
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
MOZ_ASSERT(mConsumeType == CONSUME_BLOB);
|
2017-06-28 21:28:42 +03:00
|
|
|
|
|
|
|
if (mBodyConsumed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mBodyConsumed = true;
|
2017-06-20 18:53:21 +03:00
|
|
|
|
2017-07-10 23:07:44 +03:00
|
|
|
// Just a precaution to ensure ContinueConsumeBody is not called out of
|
|
|
|
// sync with a body read.
|
|
|
|
MOZ_ASSERT(mBody->BodyUsed());
|
|
|
|
|
2018-02-05 21:55:08 +03:00
|
|
|
if (!aShuttingDown) {
|
|
|
|
MOZ_ASSERT(mConsumePromise);
|
|
|
|
RefPtr<Promise> localPromise = mConsumePromise.forget();
|
2017-06-20 18:53:21 +03:00
|
|
|
|
2018-02-05 21:55:08 +03:00
|
|
|
RefPtr<dom::Blob> blob = dom::Blob::Create(mGlobal, aBlobImpl);
|
|
|
|
MOZ_ASSERT(blob);
|
2017-06-20 18:53:21 +03:00
|
|
|
|
2018-02-05 21:55:08 +03:00
|
|
|
localPromise->MaybeResolve(blob);
|
|
|
|
}
|
2017-06-20 18:53:21 +03:00
|
|
|
|
|
|
|
ReleaseObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
2017-07-10 23:07:44 +03:00
|
|
|
FetchBodyConsumer<Derived>::ShutDownMainThreadConsuming()
|
2017-06-20 18:53:21 +03:00
|
|
|
{
|
2017-07-10 23:07:44 +03:00
|
|
|
if (!NS_IsMainThread()) {
|
2017-09-04 18:06:47 +03:00
|
|
|
RefPtr<FetchBodyConsumer<Derived>> self = this;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
|
|
|
"FetchBodyConsumer::ShutDownMainThreadConsuming",
|
|
|
|
[self] () { self->ShutDownMainThreadConsuming(); });
|
2017-07-10 23:07:44 +03:00
|
|
|
|
2017-09-04 18:06:47 +03:00
|
|
|
mMainThreadEventTarget->Dispatch(r.forget(), NS_DISPATCH_NORMAL);
|
2017-07-10 23:07:44 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need this because maybe, mConsumeBodyPump has not been created yet. We
|
|
|
|
// must be sure that we don't try to do it.
|
|
|
|
mShuttingDown = true;
|
|
|
|
|
|
|
|
if (mConsumeBodyPump) {
|
|
|
|
mConsumeBodyPump->Cancel(NS_BINDING_ABORTED);
|
|
|
|
mConsumeBodyPump = nullptr;
|
|
|
|
}
|
2017-06-20 18:53:21 +03:00
|
|
|
}
|
|
|
|
|
2017-06-22 14:16:32 +03:00
|
|
|
template <class Derived>
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FetchBodyConsumer<Derived>::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const char16_t* aData)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
MOZ_ASSERT((strcmp(aTopic, DOM_WINDOW_FROZEN_TOPIC) == 0) ||
|
|
|
|
(strcmp(aTopic, DOM_WINDOW_DESTROYED_TOPIC) == 0));
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(mGlobal);
|
|
|
|
if (SameCOMIdentity(aSubject, window)) {
|
|
|
|
ContinueConsumeBody(NS_BINDING_ABORTED, 0, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
template <class Derived>
|
|
|
|
void
|
2017-08-29 12:31:07 +03:00
|
|
|
FetchBodyConsumer<Derived>::Abort()
|
2017-08-29 12:31:06 +03:00
|
|
|
{
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
ContinueConsumeBody(NS_ERROR_DOM_ABORT_ERR, 0, nullptr);
|
|
|
|
}
|
|
|
|
|
2017-06-22 14:16:32 +03:00
|
|
|
template <class Derived>
|
|
|
|
NS_IMPL_ADDREF(FetchBodyConsumer<Derived>)
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
NS_IMPL_RELEASE(FetchBodyConsumer<Derived>)
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
NS_IMPL_QUERY_INTERFACE(FetchBodyConsumer<Derived>,
|
|
|
|
nsIObserver,
|
|
|
|
nsISupportsWeakReference)
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|