2015-05-03 22:32:37 +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: */
|
2014-09-24 09:03:20 +04: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 "Fetch.h"
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2014-10-06 22:01:20 +04:00
|
|
|
#include "nsIGlobalObject.h"
|
2014-09-24 09:03:20 +04:00
|
|
|
|
2015-04-04 08:55:15 +03:00
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2014-09-27 03:41:15 +04:00
|
|
|
#include "nsDOMString.h"
|
2017-05-19 08:29:00 +03:00
|
|
|
#include "nsJSUtils.h"
|
2014-09-27 03:41:15 +04:00
|
|
|
#include "nsNetUtil.h"
|
2015-04-04 08:55:15 +03:00
|
|
|
#include "nsReadableUtils.h"
|
2014-09-27 03:41:15 +04:00
|
|
|
#include "nsStreamUtils.h"
|
2014-09-24 09:03:20 +04:00
|
|
|
#include "nsStringStream.h"
|
2017-06-20 18:53:20 +03:00
|
|
|
#include "nsProxyRelease.h"
|
2014-09-24 09:03:20 +04:00
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2017-02-01 23:43:37 +03:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2019-06-07 17:16:59 +03:00
|
|
|
#include "mozilla/dom/BodyConsumer.h"
|
2014-12-05 01:06:45 +03:00
|
|
|
#include "mozilla/dom/Exceptions.h"
|
2017-08-29 12:31:06 +03:00
|
|
|
#include "mozilla/dom/DOMException.h"
|
2014-10-06 22:01:20 +04:00
|
|
|
#include "mozilla/dom/FetchDriver.h"
|
2014-09-27 03:41:15 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2016-01-07 22:30:36 +03:00
|
|
|
#include "mozilla/dom/FormData.h"
|
2014-09-27 03:41:15 +04:00
|
|
|
#include "mozilla/dom/Headers.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
2015-09-03 01:04:17 +03:00
|
|
|
#include "mozilla/dom/PromiseWorkerProxy.h"
|
2018-11-20 02:18:33 +03:00
|
|
|
#include "mozilla/dom/RemoteWorkerChild.h"
|
2014-09-27 03:41:15 +04:00
|
|
|
#include "mozilla/dom/Request.h"
|
|
|
|
#include "mozilla/dom/Response.h"
|
2014-10-06 22:01:20 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2014-09-24 09:03:20 +04:00
|
|
|
#include "mozilla/dom/URLSearchParams.h"
|
2020-03-04 11:59:08 +03:00
|
|
|
#include "mozilla/net/CookieJarSettings.h"
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2017-01-24 14:13:31 +03:00
|
|
|
#include "BodyExtractor.h"
|
2018-11-06 16:48:07 +03:00
|
|
|
#include "EmptyBody.h"
|
2017-03-22 13:07:27 +03:00
|
|
|
#include "FetchObserver.h"
|
2014-12-23 18:56:19 +03:00
|
|
|
#include "InternalRequest.h"
|
2014-10-06 22:01:20 +04:00
|
|
|
#include "InternalResponse.h"
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2018-01-31 10:25:30 +03:00
|
|
|
#include "mozilla/dom/WorkerCommon.h"
|
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
2018-03-16 18:52:29 +03:00
|
|
|
#include "mozilla/dom/WorkerRef.h"
|
2018-01-31 10:25:30 +03:00
|
|
|
#include "mozilla/dom/WorkerRunnable.h"
|
|
|
|
#include "mozilla/dom/WorkerScope.h"
|
2014-09-24 09:03:20 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
namespace {
|
|
|
|
|
2018-10-11 22:18:43 +03:00
|
|
|
void AbortStream(JSContext* aCx, JS::Handle<JSObject*> aStream,
|
|
|
|
ErrorResult& aRv) {
|
2018-10-31 20:30:18 +03:00
|
|
|
aRv.MightThrowJSException();
|
|
|
|
|
2018-10-11 22:18:43 +03:00
|
|
|
bool isReadable;
|
|
|
|
if (!JS::ReadableStreamIsReadable(aCx, aStream, &isReadable)) {
|
|
|
|
aRv.StealExceptionFromJSContext(aCx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!isReadable) {
|
2017-08-29 12:31:06 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<DOMException> e = DOMException::Create(NS_ERROR_DOM_ABORT_ERR);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
|
|
|
if (!GetOrCreateDOMReflector(aCx, e, &value)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-11 22:18:43 +03:00
|
|
|
if (!JS::ReadableStreamError(aCx, aStream, value)) {
|
|
|
|
aRv.StealExceptionFromJSContext(aCx);
|
|
|
|
}
|
2017-08-29 12:31:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-08-26 15:16:21 +03:00
|
|
|
class AbortSignalMainThread final : public AbortSignalImpl {
|
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(AbortSignalMainThread)
|
|
|
|
|
|
|
|
explicit AbortSignalMainThread(bool aAborted) : AbortSignalImpl(aAborted) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~AbortSignalMainThread() = default;
|
|
|
|
};
|
|
|
|
|
2018-09-07 12:05:47 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(AbortSignalMainThread)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(AbortSignalMainThread)
|
|
|
|
tmp->Unfollow();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(AbortSignalMainThread)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFollowingSignal)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2018-08-26 15:16:21 +03:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AbortSignalMainThread)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(AbortSignalMainThread)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(AbortSignalMainThread)
|
|
|
|
|
|
|
|
// This class helps the proxying of AbortSignalImpl changes cross threads.
|
2017-08-29 12:31:07 +03:00
|
|
|
class AbortSignalProxy final : public AbortFollower {
|
2017-03-22 13:06:08 +03:00
|
|
|
// This is created and released on the main-thread.
|
2018-08-26 15:16:21 +03:00
|
|
|
RefPtr<AbortSignalImpl> mSignalImplMainThread;
|
2017-03-22 13:06:08 +03:00
|
|
|
|
2017-05-16 11:59:29 +03:00
|
|
|
// The main-thread event target for runnable dispatching.
|
|
|
|
nsCOMPtr<nsIEventTarget> mMainThreadEventTarget;
|
|
|
|
|
2018-08-26 15:16:21 +03:00
|
|
|
// This value is used only for the creation of AbortSignalImpl on the
|
2017-03-22 13:06:08 +03:00
|
|
|
// main-thread. They are not updated.
|
|
|
|
const bool mAborted;
|
|
|
|
|
2018-08-26 15:16:21 +03:00
|
|
|
// This runnable propagates changes from the AbortSignalImpl on workers to the
|
|
|
|
// AbortSignalImpl on main-thread.
|
2017-08-29 12:31:06 +03:00
|
|
|
class AbortSignalProxyRunnable final : public Runnable {
|
|
|
|
RefPtr<AbortSignalProxy> mProxy;
|
2017-03-22 13:06:08 +03:00
|
|
|
|
|
|
|
public:
|
2017-08-29 12:31:06 +03:00
|
|
|
explicit AbortSignalProxyRunnable(AbortSignalProxy* aProxy)
|
|
|
|
: Runnable("dom::AbortSignalProxy::AbortSignalProxyRunnable"),
|
2017-06-12 22:34:10 +03:00
|
|
|
mProxy(aProxy) {}
|
2017-03-22 13:06:08 +03:00
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2018-08-26 15:16:21 +03:00
|
|
|
AbortSignalImpl* signalImpl =
|
|
|
|
mProxy->GetOrCreateSignalImplForMainThread();
|
|
|
|
signalImpl->Abort();
|
2017-03-22 13:06:08 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2017-08-29 12:31:06 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AbortSignalProxy)
|
2017-03-22 13:06:08 +03:00
|
|
|
|
2018-08-26 15:16:21 +03:00
|
|
|
AbortSignalProxy(AbortSignalImpl* aSignalImpl,
|
|
|
|
nsIEventTarget* aMainThreadEventTarget)
|
2017-05-16 11:59:29 +03:00
|
|
|
: mMainThreadEventTarget(aMainThreadEventTarget),
|
2018-08-26 15:16:21 +03:00
|
|
|
mAborted(aSignalImpl->Aborted()) {
|
2017-05-16 11:59:29 +03:00
|
|
|
MOZ_ASSERT(mMainThreadEventTarget);
|
2018-08-26 15:16:21 +03:00
|
|
|
Follow(aSignalImpl);
|
2017-03-22 13:06:08 +03:00
|
|
|
}
|
|
|
|
|
2017-08-29 12:31:07 +03:00
|
|
|
void Abort() override {
|
2017-08-29 12:31:06 +03:00
|
|
|
RefPtr<AbortSignalProxyRunnable> runnable =
|
|
|
|
new AbortSignalProxyRunnable(this);
|
2017-05-16 11:59:29 +03:00
|
|
|
mMainThreadEventTarget->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
|
2017-03-22 13:06:08 +03:00
|
|
|
}
|
|
|
|
|
2018-08-26 15:16:21 +03:00
|
|
|
AbortSignalImpl* GetOrCreateSignalImplForMainThread() {
|
2017-03-22 13:06:08 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2018-08-26 15:16:21 +03:00
|
|
|
if (!mSignalImplMainThread) {
|
|
|
|
mSignalImplMainThread = new AbortSignalMainThread(mAborted);
|
2017-03-22 13:06:08 +03:00
|
|
|
}
|
2018-08-26 15:16:21 +03:00
|
|
|
return mSignalImplMainThread;
|
2017-03-22 13:06:08 +03:00
|
|
|
}
|
|
|
|
|
2018-08-26 15:16:21 +03:00
|
|
|
AbortSignalImpl* GetSignalImplForTargetThread() { return mFollowingSignal; }
|
2017-08-29 12:31:06 +03:00
|
|
|
|
2017-03-22 13:06:08 +03:00
|
|
|
void Shutdown() { Unfollow(); }
|
|
|
|
|
|
|
|
private:
|
2017-08-29 12:31:06 +03:00
|
|
|
~AbortSignalProxy() {
|
2018-08-26 15:16:21 +03:00
|
|
|
NS_ProxyRelease("AbortSignalProxy::mSignalImplMainThread",
|
|
|
|
mMainThreadEventTarget, mSignalImplMainThread.forget());
|
2017-03-22 13:06:08 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-03 01:04:17 +03:00
|
|
|
class WorkerFetchResolver final : public FetchDriverObserver {
|
2017-12-22 12:05:48 +03:00
|
|
|
// Thread-safe:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> mPromiseProxy;
|
2017-08-29 12:31:06 +03:00
|
|
|
RefPtr<AbortSignalProxy> mSignalProxy;
|
2017-12-22 12:05:48 +03:00
|
|
|
|
|
|
|
// Touched only on the worker thread.
|
2017-03-22 13:07:27 +03:00
|
|
|
RefPtr<FetchObserver> mFetchObserver;
|
2018-03-16 18:52:29 +03:00
|
|
|
RefPtr<WeakWorkerRef> mWorkerRef;
|
2019-08-28 23:21:50 +03:00
|
|
|
bool mIsShutdown;
|
2017-03-22 13:06:08 +03:00
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
public:
|
2015-09-03 01:04:17 +03:00
|
|
|
// Returns null if worker is shutting down.
|
|
|
|
static already_AddRefed<WorkerFetchResolver> Create(
|
2018-01-31 10:24:08 +03:00
|
|
|
WorkerPrivate* aWorkerPrivate, Promise* aPromise,
|
2018-08-26 15:16:21 +03:00
|
|
|
AbortSignalImpl* aSignalImpl, FetchObserver* aObserver) {
|
2015-09-03 01:04:17 +03:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
2017-03-22 13:06:08 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> proxy =
|
|
|
|
PromiseWorkerProxy::Create(aWorkerPrivate, aPromise);
|
2015-09-03 01:04:17 +03:00
|
|
|
if (!proxy) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
RefPtr<AbortSignalProxy> signalProxy;
|
2018-08-26 15:16:21 +03:00
|
|
|
if (aSignalImpl) {
|
|
|
|
signalProxy = new AbortSignalProxy(
|
|
|
|
aSignalImpl, aWorkerPrivate->MainThreadEventTarget());
|
2017-03-22 13:06:08 +03:00
|
|
|
}
|
|
|
|
|
2017-03-22 13:07:27 +03:00
|
|
|
RefPtr<WorkerFetchResolver> r =
|
|
|
|
new WorkerFetchResolver(proxy, signalProxy, aObserver);
|
2017-12-22 12:06:47 +03:00
|
|
|
|
2018-03-16 18:52:29 +03:00
|
|
|
RefPtr<WeakWorkerRef> workerRef = WeakWorkerRef::Create(
|
|
|
|
aWorkerPrivate, [r]() { r->Shutdown(r->mWorkerRef->GetPrivate()); });
|
|
|
|
if (NS_WARN_IF(!workerRef)) {
|
2017-12-22 12:06:47 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
r->mWorkerRef = std::move(workerRef);
|
2018-03-16 18:52:29 +03:00
|
|
|
|
2015-09-03 01:04:17 +03:00
|
|
|
return r.forget();
|
2014-12-10 11:51:59 +03:00
|
|
|
}
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
AbortSignalImpl* GetAbortSignalForMainThread() {
|
2017-03-22 13:06:08 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!mSignalProxy) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-08-26 15:16:21 +03:00
|
|
|
return mSignalProxy->GetOrCreateSignalImplForMainThread();
|
2017-03-22 13:06:08 +03:00
|
|
|
}
|
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
AbortSignalImpl* GetAbortSignalForTargetThread() {
|
|
|
|
mPromiseProxy->GetWorkerPrivate()->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (!mSignalProxy) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-08-26 15:16:21 +03:00
|
|
|
return mSignalProxy->GetSignalImplForTargetThread();
|
2017-08-29 12:31:06 +03:00
|
|
|
}
|
|
|
|
|
2017-12-22 12:05:48 +03:00
|
|
|
PromiseWorkerProxy* PromiseProxy() const {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mPromiseProxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
Promise* WorkerPromise(WorkerPrivate* aWorkerPrivate) const {
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
2019-08-28 23:21:50 +03:00
|
|
|
MOZ_ASSERT(!mIsShutdown);
|
2017-12-22 12:05:48 +03:00
|
|
|
|
|
|
|
return mPromiseProxy->WorkerPromise();
|
|
|
|
}
|
|
|
|
|
|
|
|
FetchObserver* GetFetchObserver(WorkerPrivate* aWorkerPrivate) const {
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
return mFetchObserver;
|
|
|
|
}
|
|
|
|
|
2015-09-14 18:05:35 +03:00
|
|
|
void OnResponseAvailableInternal(InternalResponse* aResponse) override;
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2017-03-22 13:06:08 +03:00
|
|
|
void OnResponseEnd(FetchDriverObserver::EndReason eReason) override;
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2017-10-13 05:23:16 +03:00
|
|
|
bool NeedOnDataAvailable() override;
|
|
|
|
|
2017-03-22 13:07:27 +03:00
|
|
|
void OnDataAvailable() override;
|
|
|
|
|
2017-12-22 12:05:48 +03:00
|
|
|
void Shutdown(WorkerPrivate* aWorkerPrivate) {
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2019-08-28 23:21:50 +03:00
|
|
|
mIsShutdown = true;
|
2017-12-22 12:05:48 +03:00
|
|
|
mPromiseProxy->CleanUp();
|
|
|
|
|
|
|
|
mFetchObserver = nullptr;
|
|
|
|
|
|
|
|
if (mSignalProxy) {
|
|
|
|
mSignalProxy->Shutdown();
|
|
|
|
}
|
2017-12-22 12:06:47 +03:00
|
|
|
|
2018-03-16 18:52:29 +03:00
|
|
|
mWorkerRef = nullptr;
|
2017-12-22 12:05:48 +03:00
|
|
|
}
|
|
|
|
|
2019-08-28 23:21:50 +03:00
|
|
|
bool IsShutdown(WorkerPrivate* aWorkerPrivate) const {
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
return mIsShutdown;
|
|
|
|
}
|
|
|
|
|
2015-09-03 01:04:17 +03:00
|
|
|
private:
|
2017-03-22 13:06:08 +03:00
|
|
|
WorkerFetchResolver(PromiseWorkerProxy* aProxy,
|
2017-08-29 12:31:06 +03:00
|
|
|
AbortSignalProxy* aSignalProxy, FetchObserver* aObserver)
|
2015-09-03 01:04:17 +03:00
|
|
|
: mPromiseProxy(aProxy),
|
2017-03-22 13:06:08 +03:00
|
|
|
mSignalProxy(aSignalProxy),
|
2019-08-28 23:21:50 +03:00
|
|
|
mFetchObserver(aObserver),
|
|
|
|
mIsShutdown(false) {
|
2015-09-03 01:04:17 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mPromiseProxy);
|
2014-12-10 11:51:59 +03:00
|
|
|
}
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2020-02-20 19:53:06 +03:00
|
|
|
~WorkerFetchResolver() = default;
|
2016-09-07 05:20:23 +03:00
|
|
|
|
|
|
|
virtual void FlushConsoleReport() override;
|
2014-10-06 22:01:20 +04:00
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class MainThreadFetchResolver final : public FetchDriverObserver {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> mPromise;
|
|
|
|
RefPtr<Response> mResponse;
|
2017-03-22 13:07:27 +03:00
|
|
|
RefPtr<FetchObserver> mFetchObserver;
|
2018-08-26 15:16:21 +03:00
|
|
|
RefPtr<AbortSignalImpl> mSignalImpl;
|
2017-11-15 22:53:42 +03:00
|
|
|
const bool mMozErrors;
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2017-02-06 04:19:34 +03:00
|
|
|
nsCOMPtr<nsILoadGroup> mLoadGroup;
|
2016-09-07 05:20:23 +03:00
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
NS_DECL_OWNINGTHREAD
|
|
|
|
public:
|
2017-08-29 12:31:06 +03:00
|
|
|
MainThreadFetchResolver(Promise* aPromise, FetchObserver* aObserver,
|
2018-08-26 15:16:21 +03:00
|
|
|
AbortSignalImpl* aSignalImpl, bool aMozErrors)
|
2017-03-22 13:07:27 +03:00
|
|
|
: mPromise(aPromise),
|
|
|
|
mFetchObserver(aObserver),
|
2018-08-26 15:16:21 +03:00
|
|
|
mSignalImpl(aSignalImpl),
|
2017-11-15 22:53:42 +03:00
|
|
|
mMozErrors(aMozErrors) {}
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2015-09-14 18:05:35 +03:00
|
|
|
void OnResponseAvailableInternal(InternalResponse* aResponse) override;
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2017-02-06 04:19:34 +03:00
|
|
|
void SetLoadGroup(nsILoadGroup* aLoadGroup) { mLoadGroup = aLoadGroup; }
|
2016-09-07 05:20:23 +03:00
|
|
|
|
2017-03-22 13:06:08 +03:00
|
|
|
void OnResponseEnd(FetchDriverObserver::EndReason aReason) override {
|
|
|
|
if (aReason == eAborted) {
|
|
|
|
mPromise->MaybeReject(NS_ERROR_DOM_ABORT_ERR);
|
|
|
|
}
|
|
|
|
|
2017-03-22 13:07:27 +03:00
|
|
|
mFetchObserver = nullptr;
|
|
|
|
|
2016-09-07 05:20:23 +03:00
|
|
|
FlushConsoleReport();
|
|
|
|
}
|
|
|
|
|
2017-10-13 05:23:16 +03:00
|
|
|
bool NeedOnDataAvailable() override;
|
|
|
|
|
2017-03-22 13:07:27 +03:00
|
|
|
void OnDataAvailable() override;
|
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
private:
|
|
|
|
~MainThreadFetchResolver();
|
2016-09-07 05:20:23 +03:00
|
|
|
|
|
|
|
void FlushConsoleReport() override {
|
2017-02-06 04:19:34 +03:00
|
|
|
mReporter->FlushConsoleReports(mLoadGroup);
|
2016-09-07 05:20:23 +03:00
|
|
|
}
|
2014-10-06 22:01:20 +04:00
|
|
|
};
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class MainThreadFetchRunnable : public Runnable {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerFetchResolver> mResolver;
|
2018-01-23 18:38:54 +03:00
|
|
|
const ClientInfo mClientInfo;
|
|
|
|
const Maybe<ServiceWorkerDescriptor> mController;
|
2018-10-23 09:17:13 +03:00
|
|
|
nsCOMPtr<nsICSPEventListener> mCSPEventListener;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalRequest> mRequest;
|
2019-06-15 19:46:22 +03:00
|
|
|
UniquePtr<SerializedStackHolder> mOriginStack;
|
2014-10-06 22:01:20 +04:00
|
|
|
|
|
|
|
public:
|
2015-09-03 01:04:17 +03:00
|
|
|
MainThreadFetchRunnable(WorkerFetchResolver* aResolver,
|
2018-01-23 18:38:54 +03:00
|
|
|
const ClientInfo& aClientInfo,
|
|
|
|
const Maybe<ServiceWorkerDescriptor>& aController,
|
2018-10-23 09:17:13 +03:00
|
|
|
nsICSPEventListener* aCSPEventListener,
|
2019-06-15 19:46:22 +03:00
|
|
|
InternalRequest* aRequest,
|
|
|
|
UniquePtr<SerializedStackHolder>&& aOriginStack)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("dom::MainThreadFetchRunnable"),
|
|
|
|
mResolver(aResolver),
|
2018-01-23 18:38:54 +03:00
|
|
|
mClientInfo(aClientInfo),
|
|
|
|
mController(aController),
|
2018-10-23 09:17:13 +03:00
|
|
|
mCSPEventListener(aCSPEventListener),
|
2019-06-15 19:46:22 +03:00
|
|
|
mRequest(aRequest),
|
|
|
|
mOriginStack(std::move(aOriginStack)) {
|
2015-09-03 01:04:17 +03:00
|
|
|
MOZ_ASSERT(mResolver);
|
2014-10-06 22:01:20 +04:00
|
|
|
}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
2014-10-06 22:01:20 +04:00
|
|
|
AssertIsOnMainThread();
|
2016-06-14 06:08:00 +03:00
|
|
|
RefPtr<FetchDriver> fetch;
|
2017-12-22 12:05:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> proxy = mResolver->PromiseProxy();
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2016-06-14 06:08:00 +03:00
|
|
|
{
|
|
|
|
// Acquire the proxy mutex while getting data from the WorkerPrivate...
|
|
|
|
MutexAutoLock lock(proxy->Lock());
|
|
|
|
if (proxy->CleanedUp()) {
|
|
|
|
NS_WARNING("Aborting Fetch because worker already shut down");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-05-16 13:27:01 +03:00
|
|
|
WorkerPrivate* workerPrivate = proxy->GetWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = workerPrivate->GetPrincipal();
|
2016-06-14 06:08:00 +03:00
|
|
|
MOZ_ASSERT(principal);
|
2017-05-16 13:27:01 +03:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = workerPrivate->GetLoadGroup();
|
2016-06-14 06:08:00 +03:00
|
|
|
MOZ_ASSERT(loadGroup);
|
2017-05-19 08:29:00 +03:00
|
|
|
// We don't track if a worker is spawned from a tracking script for now,
|
|
|
|
// so pass false as the last argument to FetchDriver().
|
2017-05-16 13:27:01 +03:00
|
|
|
fetch = new FetchDriver(mRequest, principal, loadGroup,
|
2018-01-24 19:17:31 +03:00
|
|
|
workerPrivate->MainThreadEventTarget(),
|
2020-03-04 11:59:08 +03:00
|
|
|
workerPrivate->CookieJarSettings(),
|
2018-01-24 19:17:31 +03:00
|
|
|
workerPrivate->GetPerformanceStorage(), false);
|
2016-09-07 05:20:23 +03:00
|
|
|
nsAutoCString spec;
|
|
|
|
if (proxy->GetWorkerPrivate()->GetBaseURI()) {
|
|
|
|
proxy->GetWorkerPrivate()->GetBaseURI()->GetAsciiSpec(spec);
|
|
|
|
}
|
|
|
|
fetch->SetWorkerScript(spec);
|
2018-01-23 18:38:54 +03:00
|
|
|
|
|
|
|
fetch->SetClientInfo(mClientInfo);
|
|
|
|
fetch->SetController(mController);
|
2018-10-23 09:17:13 +03:00
|
|
|
fetch->SetCSPEventListener(mCSPEventListener);
|
2014-10-21 00:25:56 +04:00
|
|
|
}
|
2016-06-14 06:08:00 +03:00
|
|
|
|
2019-06-15 19:46:22 +03:00
|
|
|
fetch->SetOriginStack(std::move(mOriginStack));
|
|
|
|
|
2018-08-26 15:16:21 +03:00
|
|
|
RefPtr<AbortSignalImpl> signalImpl =
|
|
|
|
mResolver->GetAbortSignalForMainThread();
|
2017-03-22 13:06:08 +03:00
|
|
|
|
2016-06-14 06:08:00 +03:00
|
|
|
// ...but release it before calling Fetch, because mResolver's callback can
|
|
|
|
// be called synchronously and they want the mutex, too.
|
2018-08-26 15:16:21 +03:00
|
|
|
return fetch->Fetch(signalImpl, mResolver);
|
2014-10-06 22:01:20 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-20 14:58:00 +03:00
|
|
|
already_AddRefed<Promise> FetchRequest(nsIGlobalObject* aGlobal,
|
|
|
|
const RequestOrUSVString& aInput,
|
2017-02-01 23:43:37 +03:00
|
|
|
const RequestInit& aInit,
|
|
|
|
CallerType aCallerType,
|
|
|
|
ErrorResult& aRv) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> p = Promise::Create(aGlobal, aRv);
|
2014-10-06 22:01:20 +04:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-05-16 13:27:01 +03:00
|
|
|
MOZ_ASSERT(aGlobal);
|
|
|
|
|
2016-04-08 00:13:09 +03:00
|
|
|
// Double check that we have chrome privileges if the Request's content
|
2017-02-01 23:43:37 +03:00
|
|
|
// policy type has been overridden.
|
2016-04-08 00:13:09 +03:00
|
|
|
MOZ_ASSERT_IF(aInput.IsRequest() &&
|
|
|
|
aInput.GetAsRequest().IsContentPolicyTypeOverridden(),
|
2017-02-01 23:43:37 +03:00
|
|
|
aCallerType == CallerType::System);
|
2016-04-08 00:13:09 +03:00
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
AutoJSAPI jsapi;
|
2016-07-14 06:18:11 +03:00
|
|
|
if (!jsapi.Init(aGlobal)) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2016-07-14 06:18:11 +03:00
|
|
|
JSContext* cx = jsapi.cx();
|
2014-10-06 22:01:20 +04:00
|
|
|
JS::Rooted<JSObject*> jsGlobal(cx, aGlobal->GetGlobalJSObject());
|
|
|
|
GlobalObject global(cx, jsGlobal);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Request> request = Request::Constructor(global, aInput, aInit, aRv);
|
2018-02-10 01:04:03 +03:00
|
|
|
if (aRv.Failed()) {
|
2014-10-06 22:01:20 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalRequest> r = request->GetInternalRequest();
|
2018-08-26 15:16:21 +03:00
|
|
|
RefPtr<AbortSignalImpl> signalImpl = request->GetSignalImpl();
|
2017-03-22 13:06:08 +03:00
|
|
|
|
2018-08-26 15:16:21 +03:00
|
|
|
if (signalImpl && signalImpl->Aborted()) {
|
2017-08-29 12:31:07 +03:00
|
|
|
// Already aborted signal rejects immediately.
|
|
|
|
aRv.Throw(NS_ERROR_DOM_ABORT_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-03-22 13:07:27 +03:00
|
|
|
RefPtr<FetchObserver> observer;
|
|
|
|
if (aInit.mObserve.WasPassed()) {
|
2018-08-26 15:16:21 +03:00
|
|
|
observer = new FetchObserver(aGlobal, signalImpl);
|
2017-03-22 13:07:27 +03:00
|
|
|
aInit.mObserve.Value().HandleEvent(*observer);
|
|
|
|
}
|
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
if (NS_IsMainThread()) {
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aGlobal);
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc;
|
2015-05-21 00:26:31 +03:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
2020-03-04 11:59:08 +03:00
|
|
|
nsCOMPtr<nsICookieJarSettings> cookieJarSettings;
|
2015-05-21 00:26:31 +03:00
|
|
|
nsIPrincipal* principal;
|
2017-05-19 08:29:00 +03:00
|
|
|
bool isTrackingFetch = false;
|
2015-05-21 00:26:31 +03:00
|
|
|
if (window) {
|
|
|
|
doc = window->GetExtantDoc();
|
|
|
|
if (!doc) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
principal = doc->NodePrincipal();
|
|
|
|
loadGroup = doc->GetDocumentLoadGroup();
|
2020-03-04 11:59:08 +03:00
|
|
|
cookieJarSettings = doc->CookieJarSettings();
|
2017-05-19 08:29:00 +03:00
|
|
|
|
2019-08-20 23:58:57 +03:00
|
|
|
isTrackingFetch = doc->IsScriptTracking(cx);
|
2015-05-21 00:26:31 +03:00
|
|
|
} else {
|
|
|
|
principal = aGlobal->PrincipalOrNull();
|
|
|
|
if (NS_WARN_IF(!principal)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-09-09 18:54:57 +03:00
|
|
|
|
2020-03-04 11:59:08 +03:00
|
|
|
cookieJarSettings = mozilla::net::CookieJarSettings::Create();
|
2019-09-09 18:54:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!loadGroup) {
|
2015-05-21 00:26:31 +03:00
|
|
|
nsresult rv = NS_NewLoadGroup(getter_AddRefs(loadGroup), principal);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-12-10 11:51:59 +03:00
|
|
|
}
|
|
|
|
|
2017-03-22 13:07:27 +03:00
|
|
|
RefPtr<MainThreadFetchResolver> resolver = new MainThreadFetchResolver(
|
2018-08-26 15:16:21 +03:00
|
|
|
p, observer, signalImpl, request->MozErrors());
|
2017-05-19 08:29:00 +03:00
|
|
|
RefPtr<FetchDriver> fetch = new FetchDriver(
|
2018-01-24 19:17:31 +03:00
|
|
|
r, principal, loadGroup, aGlobal->EventTargetFor(TaskCategory::Other),
|
2020-03-04 11:59:08 +03:00
|
|
|
cookieJarSettings, nullptr, // PerformanceStorage
|
2018-01-24 19:17:31 +03:00
|
|
|
isTrackingFetch);
|
2014-10-15 03:36:58 +04:00
|
|
|
fetch->SetDocument(doc);
|
2017-02-06 04:19:34 +03:00
|
|
|
resolver->SetLoadGroup(loadGroup);
|
2018-08-26 15:16:21 +03:00
|
|
|
aRv = fetch->Fetch(signalImpl, resolver);
|
2014-10-06 22:01:20 +04:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
2014-12-24 13:08:41 +03:00
|
|
|
|
|
|
|
if (worker->IsServiceWorker()) {
|
|
|
|
r->SetSkipServiceWorker();
|
|
|
|
}
|
|
|
|
|
2017-03-22 13:06:08 +03:00
|
|
|
RefPtr<WorkerFetchResolver> resolver =
|
2018-08-26 15:16:21 +03:00
|
|
|
WorkerFetchResolver::Create(worker, p, signalImpl, observer);
|
2015-09-03 01:04:17 +03:00
|
|
|
if (!resolver) {
|
2018-03-16 18:52:29 +03:00
|
|
|
NS_WARNING("Could not keep the worker alive.");
|
2015-09-03 01:04:17 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_ABORT_ERR);
|
|
|
|
return nullptr;
|
2014-10-06 22:01:20 +04:00
|
|
|
}
|
2015-09-03 01:04:17 +03:00
|
|
|
|
2018-05-02 16:29:26 +03:00
|
|
|
Maybe<ClientInfo> clientInfo(worker->GetClientInfo());
|
|
|
|
if (clientInfo.isNothing()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-06-15 19:46:22 +03:00
|
|
|
UniquePtr<SerializedStackHolder> stack;
|
|
|
|
if (worker->IsWatchedByDevtools()) {
|
|
|
|
stack = GetCurrentStackForNetMonitor(cx);
|
|
|
|
}
|
|
|
|
|
2017-03-22 13:06:08 +03:00
|
|
|
RefPtr<MainThreadFetchRunnable> run = new MainThreadFetchRunnable(
|
2018-05-02 16:29:26 +03:00
|
|
|
resolver, clientInfo.ref(), worker->GetController(),
|
2019-06-15 19:46:22 +03:00
|
|
|
worker->CSPEventListener(), r, std::move(stack));
|
2016-09-14 06:14:02 +03:00
|
|
|
worker->DispatchToMainThread(run.forget());
|
2014-10-06 22:01:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return p.forget();
|
|
|
|
}
|
|
|
|
|
2019-06-12 21:37:49 +03:00
|
|
|
class ResolveFetchPromise : public Runnable {
|
|
|
|
public:
|
|
|
|
ResolveFetchPromise(Promise* aPromise, Response* aResponse)
|
|
|
|
: Runnable("ResolveFetchPromise"),
|
|
|
|
mPromise(aPromise),
|
|
|
|
mResponse(aResponse) {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
mPromise->MaybeResolve(mResponse);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
RefPtr<Promise> mPromise;
|
|
|
|
RefPtr<Response> mResponse;
|
|
|
|
};
|
|
|
|
|
2015-09-14 18:05:35 +03:00
|
|
|
void MainThreadFetchResolver::OnResponseAvailableInternal(
|
|
|
|
InternalResponse* aResponse) {
|
2014-10-21 00:25:56 +04:00
|
|
|
NS_ASSERT_OWNINGTHREAD(MainThreadFetchResolver);
|
2014-10-06 22:01:20 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2015-01-15 00:43:27 +03:00
|
|
|
if (aResponse->Type() != ResponseType::Error) {
|
2017-03-22 13:07:27 +03:00
|
|
|
if (mFetchObserver) {
|
|
|
|
mFetchObserver->SetState(FetchState::Complete);
|
|
|
|
}
|
|
|
|
|
2015-01-15 00:43:27 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> go = mPromise->GetParentObject();
|
2018-08-26 15:16:21 +03:00
|
|
|
mResponse = new Response(go, aResponse, mSignalImpl);
|
2019-06-12 21:37:49 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> inner = do_QueryInterface(go);
|
2019-12-01 00:13:24 +03:00
|
|
|
BrowsingContext* bc = inner ? inner->GetBrowsingContext() : nullptr;
|
|
|
|
bc = bc ? bc->Top() : nullptr;
|
2019-12-01 00:13:54 +03:00
|
|
|
if (bc && bc->IsLoading()) {
|
2019-12-01 00:13:24 +03:00
|
|
|
bc->AddDeprioritizedLoadRunner(
|
2019-06-12 21:37:49 +03:00
|
|
|
new ResolveFetchPromise(mPromise, mResponse));
|
|
|
|
} else {
|
|
|
|
mPromise->MaybeResolve(mResponse);
|
|
|
|
}
|
2015-01-15 00:43:27 +03:00
|
|
|
} else {
|
2017-03-22 13:07:27 +03:00
|
|
|
if (mFetchObserver) {
|
|
|
|
mFetchObserver->SetState(FetchState::Errored);
|
|
|
|
}
|
|
|
|
|
2017-11-15 22:53:42 +03:00
|
|
|
if (mMozErrors) {
|
|
|
|
mPromise->MaybeReject(aResponse->GetErrorCode());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-19 16:02:39 +03:00
|
|
|
mPromise->MaybeRejectWithTypeError<MSG_FETCH_FAILED>();
|
2015-01-15 00:43:27 +03:00
|
|
|
}
|
2014-12-10 11:51:59 +03:00
|
|
|
}
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2017-10-13 05:23:16 +03:00
|
|
|
bool MainThreadFetchResolver::NeedOnDataAvailable() {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(MainThreadFetchResolver);
|
|
|
|
return !!mFetchObserver;
|
|
|
|
}
|
|
|
|
|
2017-03-22 13:07:27 +03:00
|
|
|
void MainThreadFetchResolver::OnDataAvailable() {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(MainThreadFetchResolver);
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (!mFetchObserver) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mFetchObserver->State() == FetchState::Requesting) {
|
|
|
|
mFetchObserver->SetState(FetchState::Responding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
MainThreadFetchResolver::~MainThreadFetchResolver() {
|
2014-10-21 00:25:56 +04:00
|
|
|
NS_ASSERT_OWNINGTHREAD(MainThreadFetchResolver);
|
2014-10-06 22:01:20 +04:00
|
|
|
}
|
|
|
|
|
2016-06-08 08:12:05 +03:00
|
|
|
class WorkerFetchResponseRunnable final : public MainThreadWorkerRunnable {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerFetchResolver> mResolver;
|
2014-12-10 11:51:59 +03:00
|
|
|
// Passed from main thread to worker thread after being initialized.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalResponse> mInternalResponse;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
public:
|
2015-09-03 01:04:17 +03:00
|
|
|
WorkerFetchResponseRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
WorkerFetchResolver* aResolver,
|
|
|
|
InternalResponse* aResponse)
|
2016-06-08 08:12:05 +03:00
|
|
|
: MainThreadWorkerRunnable(aWorkerPrivate),
|
2014-10-06 22:01:20 +04:00
|
|
|
mResolver(aResolver),
|
2014-12-10 11:51:59 +03:00
|
|
|
mInternalResponse(aResponse) {
|
2017-03-22 13:06:08 +03:00
|
|
|
MOZ_ASSERT(mResolver);
|
2014-10-06 22:01:20 +04:00
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
2014-10-06 22:01:20 +04:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2017-12-22 12:05:48 +03:00
|
|
|
RefPtr<Promise> promise = mResolver->WorkerPromise(aWorkerPrivate);
|
|
|
|
RefPtr<FetchObserver> fetchObserver =
|
|
|
|
mResolver->GetFetchObserver(aWorkerPrivate);
|
2015-01-15 00:43:27 +03:00
|
|
|
|
|
|
|
if (mInternalResponse->Type() != ResponseType::Error) {
|
2017-12-22 12:05:48 +03:00
|
|
|
if (fetchObserver) {
|
|
|
|
fetchObserver->SetState(FetchState::Complete);
|
2017-03-22 13:07:27 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIGlobalObject> global = aWorkerPrivate->GlobalScope();
|
2017-08-29 12:31:06 +03:00
|
|
|
RefPtr<Response> response =
|
|
|
|
new Response(global, mInternalResponse,
|
|
|
|
mResolver->GetAbortSignalForTargetThread());
|
2015-09-03 01:04:17 +03:00
|
|
|
promise->MaybeResolve(response);
|
2015-01-15 00:43:27 +03:00
|
|
|
} else {
|
2017-12-22 12:05:48 +03:00
|
|
|
if (fetchObserver) {
|
|
|
|
fetchObserver->SetState(FetchState::Errored);
|
2017-03-22 13:07:27 +03:00
|
|
|
}
|
|
|
|
|
2019-09-19 16:02:39 +03:00
|
|
|
promise->MaybeRejectWithTypeError<MSG_FETCH_FAILED>();
|
2015-01-15 00:43:27 +03:00
|
|
|
}
|
2014-10-06 22:01:20 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-22 13:07:27 +03:00
|
|
|
class WorkerDataAvailableRunnable final : public MainThreadWorkerRunnable {
|
|
|
|
RefPtr<WorkerFetchResolver> mResolver;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-03-22 13:07:27 +03:00
|
|
|
public:
|
|
|
|
WorkerDataAvailableRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
WorkerFetchResolver* aResolver)
|
|
|
|
: MainThreadWorkerRunnable(aWorkerPrivate), mResolver(aResolver) {}
|
|
|
|
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2017-12-22 12:05:48 +03:00
|
|
|
RefPtr<FetchObserver> fetchObserver =
|
|
|
|
mResolver->GetFetchObserver(aWorkerPrivate);
|
|
|
|
|
|
|
|
if (fetchObserver && fetchObserver->State() == FetchState::Requesting) {
|
|
|
|
fetchObserver->SetState(FetchState::Responding);
|
2017-03-22 13:07:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-05 23:02:27 +03:00
|
|
|
class WorkerFetchResponseEndBase {
|
2017-03-22 13:07:27 +03:00
|
|
|
protected:
|
|
|
|
RefPtr<WorkerFetchResolver> mResolver;
|
2017-03-22 13:06:08 +03:00
|
|
|
|
2016-05-05 23:02:27 +03:00
|
|
|
public:
|
2017-03-22 13:07:27 +03:00
|
|
|
explicit WorkerFetchResponseEndBase(WorkerFetchResolver* aResolver)
|
|
|
|
: mResolver(aResolver) {
|
|
|
|
MOZ_ASSERT(aResolver);
|
2016-05-05 23:02:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void WorkerRunInternal(WorkerPrivate* aWorkerPrivate) {
|
2017-12-22 12:05:48 +03:00
|
|
|
mResolver->Shutdown(aWorkerPrivate);
|
2016-05-05 23:02:27 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-08 08:12:05 +03:00
|
|
|
class WorkerFetchResponseEndRunnable final : public MainThreadWorkerRunnable,
|
2016-05-05 23:02:27 +03:00
|
|
|
public WorkerFetchResponseEndBase {
|
2017-03-22 13:07:27 +03:00
|
|
|
FetchDriverObserver::EndReason mReason;
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
public:
|
2017-03-22 13:07:27 +03:00
|
|
|
WorkerFetchResponseEndRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
WorkerFetchResolver* aResolver,
|
|
|
|
FetchDriverObserver::EndReason aReason)
|
|
|
|
: MainThreadWorkerRunnable(aWorkerPrivate),
|
|
|
|
WorkerFetchResponseEndBase(aResolver),
|
|
|
|
mReason(aReason) {}
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
2019-08-28 23:21:50 +03:00
|
|
|
if (mResolver->IsShutdown(aWorkerPrivate)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-22 13:07:27 +03:00
|
|
|
if (mReason == FetchDriverObserver::eAborted) {
|
2017-12-22 12:05:48 +03:00
|
|
|
mResolver->WorkerPromise(aWorkerPrivate)
|
|
|
|
->MaybeReject(NS_ERROR_DOM_ABORT_ERR);
|
2017-03-22 13:07:27 +03:00
|
|
|
}
|
|
|
|
|
2016-05-05 23:02:27 +03:00
|
|
|
WorkerRunInternal(aWorkerPrivate);
|
|
|
|
return true;
|
|
|
|
}
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2016-05-05 23:02:27 +03:00
|
|
|
nsresult Cancel() override {
|
|
|
|
// Execute Run anyway to make sure we cleanup our promise proxy to avoid
|
|
|
|
// leaking the worker thread
|
|
|
|
Run();
|
|
|
|
return WorkerRunnable::Cancel();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-19 21:39:13 +03:00
|
|
|
class WorkerFetchResponseEndControlRunnable final
|
|
|
|
: public MainThreadWorkerControlRunnable,
|
2016-05-05 23:02:27 +03:00
|
|
|
public WorkerFetchResponseEndBase {
|
|
|
|
public:
|
2017-03-22 13:07:27 +03:00
|
|
|
WorkerFetchResponseEndControlRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
WorkerFetchResolver* aResolver)
|
|
|
|
: MainThreadWorkerControlRunnable(aWorkerPrivate),
|
|
|
|
WorkerFetchResponseEndBase(aResolver) {}
|
2016-05-05 23:02:27 +03:00
|
|
|
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
WorkerRunInternal(aWorkerPrivate);
|
2014-12-10 11:51:59 +03:00
|
|
|
return true;
|
|
|
|
}
|
2016-05-05 23:02:27 +03:00
|
|
|
|
|
|
|
// Control runnable cancel already calls Run().
|
2014-12-10 11:51:59 +03:00
|
|
|
};
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2015-09-14 18:05:35 +03:00
|
|
|
void WorkerFetchResolver::OnResponseAvailableInternal(
|
|
|
|
InternalResponse* aResponse) {
|
2014-10-06 22:01:20 +04:00
|
|
|
AssertIsOnMainThread();
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2015-09-03 01:04:17 +03:00
|
|
|
MutexAutoLock lock(mPromiseProxy->Lock());
|
|
|
|
if (mPromiseProxy->CleanedUp()) {
|
2014-12-10 11:51:59 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerFetchResponseRunnable> r = new WorkerFetchResponseRunnable(
|
2015-09-03 01:04:17 +03:00
|
|
|
mPromiseProxy->GetWorkerPrivate(), this, aResponse);
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
if (!r->Dispatch()) {
|
2015-09-03 01:04:17 +03:00
|
|
|
NS_WARNING("Could not dispatch fetch response");
|
2014-10-06 22:01:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 05:23:16 +03:00
|
|
|
bool WorkerFetchResolver::NeedOnDataAvailable() {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MutexAutoLock lock(mPromiseProxy->Lock());
|
|
|
|
return !!mFetchObserver;
|
|
|
|
}
|
|
|
|
|
2017-03-22 13:07:27 +03:00
|
|
|
void WorkerFetchResolver::OnDataAvailable() {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
MutexAutoLock lock(mPromiseProxy->Lock());
|
|
|
|
if (mPromiseProxy->CleanedUp()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<WorkerDataAvailableRunnable> r =
|
|
|
|
new WorkerDataAvailableRunnable(mPromiseProxy->GetWorkerPrivate(), this);
|
|
|
|
Unused << r->Dispatch();
|
|
|
|
}
|
|
|
|
|
2017-03-22 13:06:08 +03:00
|
|
|
void WorkerFetchResolver::OnResponseEnd(
|
|
|
|
FetchDriverObserver::EndReason aReason) {
|
2014-12-10 11:51:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2015-09-03 01:04:17 +03:00
|
|
|
MutexAutoLock lock(mPromiseProxy->Lock());
|
|
|
|
if (mPromiseProxy->CleanedUp()) {
|
2014-12-10 11:51:59 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-07 05:20:23 +03:00
|
|
|
FlushConsoleReport();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerFetchResponseEndRunnable> r = new WorkerFetchResponseEndRunnable(
|
2017-03-22 13:07:27 +03:00
|
|
|
mPromiseProxy->GetWorkerPrivate(), this, aReason);
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
if (!r->Dispatch()) {
|
2016-05-05 23:02:27 +03:00
|
|
|
RefPtr<WorkerFetchResponseEndControlRunnable> cr =
|
2017-03-22 13:07:27 +03:00
|
|
|
new WorkerFetchResponseEndControlRunnable(
|
|
|
|
mPromiseProxy->GetWorkerPrivate(), this);
|
2016-05-05 23:02:27 +03:00
|
|
|
// This can fail if the worker thread is canceled or killed causing
|
2018-03-16 18:52:29 +03:00
|
|
|
// the PromiseWorkerProxy to give up its WorkerRef immediately,
|
2016-05-05 23:02:27 +03:00
|
|
|
// allowing the worker thread to become Dead.
|
2016-05-11 17:42:05 +03:00
|
|
|
if (!cr->Dispatch()) {
|
|
|
|
NS_WARNING("Failed to dispatch WorkerFetchResponseEndControlRunnable");
|
|
|
|
}
|
2014-12-10 11:51:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-07 05:20:23 +03:00
|
|
|
void WorkerFetchResolver::FlushConsoleReport() {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(mPromiseProxy);
|
|
|
|
|
|
|
|
if (!mReporter) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-31 10:24:08 +03:00
|
|
|
WorkerPrivate* worker = mPromiseProxy->GetWorkerPrivate();
|
2016-09-07 05:20:23 +03:00
|
|
|
if (!worker) {
|
2017-02-06 04:19:34 +03:00
|
|
|
mReporter->FlushReportsToConsole(0);
|
2016-09-07 05:20:23 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (worker->IsServiceWorker()) {
|
|
|
|
// Flush to service worker
|
2018-01-12 20:16:04 +03:00
|
|
|
mReporter->FlushReportsToConsoleForServiceWorkerScope(
|
|
|
|
worker->ServiceWorkerScope());
|
2016-09-07 05:20:23 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (worker->IsSharedWorker()) {
|
|
|
|
// Flush to shared worker
|
2018-11-20 02:18:33 +03:00
|
|
|
worker->GetRemoteWorkerController()->FlushReportsOnMainThread(mReporter);
|
2016-09-07 05:20:23 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush to dedicated worker
|
2017-02-06 04:19:34 +03:00
|
|
|
mReporter->FlushConsoleReports(worker->GetLoadGroup());
|
2016-09-07 05:20:23 +03:00
|
|
|
}
|
|
|
|
|
2017-02-09 12:37:54 +03:00
|
|
|
nsresult ExtractByteStreamFromBody(const fetch::OwningBodyInit& aBodyInit,
|
2014-09-24 09:03:20 +04:00
|
|
|
nsIInputStream** aStream,
|
2017-01-24 14:13:31 +03:00
|
|
|
nsCString& aContentTypeWithCharset,
|
2016-06-02 00:02:29 +03:00
|
|
|
uint64_t& aContentLength) {
|
2014-09-24 09:03:20 +04:00
|
|
|
MOZ_ASSERT(aStream);
|
2017-01-24 14:13:31 +03:00
|
|
|
nsAutoCString charset;
|
|
|
|
aContentTypeWithCharset.SetIsVoid(true);
|
2014-09-24 09:03:20 +04:00
|
|
|
|
|
|
|
if (aBodyInit.IsArrayBuffer()) {
|
2017-01-24 14:13:31 +03:00
|
|
|
BodyExtractor<const ArrayBuffer> body(&aBodyInit.GetAsArrayBuffer());
|
|
|
|
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
|
|
|
charset);
|
2016-06-02 00:02:29 +03:00
|
|
|
}
|
2017-01-24 14:13:31 +03:00
|
|
|
|
2016-06-02 00:02:29 +03:00
|
|
|
if (aBodyInit.IsArrayBufferView()) {
|
2017-01-24 14:13:31 +03:00
|
|
|
BodyExtractor<const ArrayBufferView> body(
|
|
|
|
&aBodyInit.GetAsArrayBufferView());
|
|
|
|
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
|
|
|
charset);
|
2016-06-02 00:02:29 +03:00
|
|
|
}
|
2017-01-24 14:13:31 +03:00
|
|
|
|
2016-06-02 00:02:29 +03:00
|
|
|
if (aBodyInit.IsBlob()) {
|
2017-01-24 14:13:31 +03:00
|
|
|
Blob& blob = aBodyInit.GetAsBlob();
|
2017-09-20 04:32:12 +03:00
|
|
|
BodyExtractor<const Blob> body(&blob);
|
2017-01-24 14:13:31 +03:00
|
|
|
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
|
|
|
charset);
|
2016-06-02 00:02:29 +03:00
|
|
|
}
|
2017-01-24 14:13:31 +03:00
|
|
|
|
2016-06-02 00:02:29 +03:00
|
|
|
if (aBodyInit.IsFormData()) {
|
2017-01-24 14:13:31 +03:00
|
|
|
FormData& formData = aBodyInit.GetAsFormData();
|
2017-09-20 04:32:12 +03:00
|
|
|
BodyExtractor<const FormData> body(&formData);
|
2017-01-24 14:13:31 +03:00
|
|
|
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
|
|
|
charset);
|
2016-06-02 00:02:29 +03:00
|
|
|
}
|
2017-01-24 14:13:31 +03:00
|
|
|
|
2016-06-02 00:02:29 +03:00
|
|
|
if (aBodyInit.IsUSVString()) {
|
2017-01-24 14:13:31 +03:00
|
|
|
BodyExtractor<const nsAString> body(&aBodyInit.GetAsUSVString());
|
|
|
|
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
|
|
|
charset);
|
2016-06-02 00:02:29 +03:00
|
|
|
}
|
2017-01-24 14:13:31 +03:00
|
|
|
|
2016-06-02 00:02:29 +03:00
|
|
|
if (aBodyInit.IsURLSearchParams()) {
|
2017-01-24 14:13:31 +03:00
|
|
|
URLSearchParams& usp = aBodyInit.GetAsURLSearchParams();
|
2017-09-20 04:32:12 +03:00
|
|
|
BodyExtractor<const URLSearchParams> body(&usp);
|
2017-01-24 14:13:31 +03:00
|
|
|
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
|
|
|
charset);
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
2014-09-24 09:03:20 +04:00
|
|
|
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Should never reach here");
|
2014-09-27 03:41:15 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-09-24 09:03:20 +04:00
|
|
|
|
2017-02-09 12:37:54 +03:00
|
|
|
nsresult ExtractByteStreamFromBody(const fetch::BodyInit& aBodyInit,
|
2014-09-27 03:41:15 +04:00
|
|
|
nsIInputStream** aStream,
|
2017-01-24 14:13:31 +03:00
|
|
|
nsCString& aContentTypeWithCharset,
|
2016-06-02 00:02:29 +03:00
|
|
|
uint64_t& aContentLength) {
|
2014-09-27 03:41:15 +04:00
|
|
|
MOZ_ASSERT(aStream);
|
2016-06-02 00:02:29 +03:00
|
|
|
MOZ_ASSERT(!*aStream);
|
2017-01-24 14:13:31 +03:00
|
|
|
|
|
|
|
nsAutoCString charset;
|
|
|
|
aContentTypeWithCharset.SetIsVoid(true);
|
2014-09-27 03:41:15 +04:00
|
|
|
|
|
|
|
if (aBodyInit.IsArrayBuffer()) {
|
2017-01-24 14:13:31 +03:00
|
|
|
BodyExtractor<const ArrayBuffer> body(&aBodyInit.GetAsArrayBuffer());
|
|
|
|
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
|
|
|
charset);
|
2016-06-02 00:02:29 +03:00
|
|
|
}
|
2017-01-24 14:13:31 +03:00
|
|
|
|
2016-06-02 00:02:29 +03:00
|
|
|
if (aBodyInit.IsArrayBufferView()) {
|
2017-01-24 14:13:31 +03:00
|
|
|
BodyExtractor<const ArrayBufferView> body(
|
|
|
|
&aBodyInit.GetAsArrayBufferView());
|
|
|
|
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
|
|
|
charset);
|
2016-06-02 00:02:29 +03:00
|
|
|
}
|
2017-01-24 14:13:31 +03:00
|
|
|
|
2016-06-02 00:02:29 +03:00
|
|
|
if (aBodyInit.IsBlob()) {
|
2017-09-20 04:32:12 +03:00
|
|
|
BodyExtractor<const Blob> body(&aBodyInit.GetAsBlob());
|
2017-01-24 14:13:31 +03:00
|
|
|
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
|
|
|
charset);
|
2016-06-02 00:02:29 +03:00
|
|
|
}
|
2017-01-24 14:13:31 +03:00
|
|
|
|
2016-06-02 00:02:29 +03:00
|
|
|
if (aBodyInit.IsFormData()) {
|
2017-09-20 04:32:12 +03:00
|
|
|
BodyExtractor<const FormData> body(&aBodyInit.GetAsFormData());
|
2017-01-24 14:13:31 +03:00
|
|
|
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
|
|
|
charset);
|
2016-06-02 00:02:29 +03:00
|
|
|
}
|
2017-01-24 14:13:31 +03:00
|
|
|
|
2016-06-02 00:02:29 +03:00
|
|
|
if (aBodyInit.IsUSVString()) {
|
2017-01-24 14:13:31 +03:00
|
|
|
BodyExtractor<const nsAString> body(&aBodyInit.GetAsUSVString());
|
|
|
|
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
|
|
|
charset);
|
2016-06-02 00:02:29 +03:00
|
|
|
}
|
2017-01-24 14:13:31 +03:00
|
|
|
|
2016-06-02 00:02:29 +03:00
|
|
|
if (aBodyInit.IsURLSearchParams()) {
|
2017-09-20 04:32:12 +03:00
|
|
|
BodyExtractor<const URLSearchParams> body(
|
|
|
|
&aBodyInit.GetAsURLSearchParams());
|
2017-01-24 14:13:31 +03:00
|
|
|
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
|
|
|
charset);
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
|
|
|
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Should never reach here");
|
2014-09-27 03:41:15 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
nsresult ExtractByteStreamFromBody(const fetch::ResponseBodyInit& aBodyInit,
|
|
|
|
nsIInputStream** aStream,
|
|
|
|
nsCString& aContentTypeWithCharset,
|
|
|
|
uint64_t& aContentLength) {
|
|
|
|
MOZ_ASSERT(aStream);
|
|
|
|
MOZ_ASSERT(!*aStream);
|
|
|
|
|
|
|
|
// ReadableStreams should be handled by
|
|
|
|
// BodyExtractorReadableStream::GetAsStream.
|
|
|
|
MOZ_ASSERT(!aBodyInit.IsReadableStream());
|
|
|
|
|
|
|
|
nsAutoCString charset;
|
|
|
|
aContentTypeWithCharset.SetIsVoid(true);
|
|
|
|
|
|
|
|
if (aBodyInit.IsArrayBuffer()) {
|
|
|
|
BodyExtractor<const ArrayBuffer> body(&aBodyInit.GetAsArrayBuffer());
|
|
|
|
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
|
|
|
charset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aBodyInit.IsArrayBufferView()) {
|
|
|
|
BodyExtractor<const ArrayBufferView> body(
|
|
|
|
&aBodyInit.GetAsArrayBufferView());
|
|
|
|
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
|
|
|
charset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aBodyInit.IsBlob()) {
|
2017-09-20 04:32:12 +03:00
|
|
|
BodyExtractor<const Blob> body(&aBodyInit.GetAsBlob());
|
2017-08-11 04:04:55 +03:00
|
|
|
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
|
|
|
charset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aBodyInit.IsFormData()) {
|
2017-09-20 04:32:12 +03:00
|
|
|
BodyExtractor<const FormData> body(&aBodyInit.GetAsFormData());
|
2017-08-11 04:04:55 +03:00
|
|
|
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
|
|
|
charset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aBodyInit.IsUSVString()) {
|
|
|
|
BodyExtractor<const nsAString> body(&aBodyInit.GetAsUSVString());
|
|
|
|
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
|
|
|
charset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aBodyInit.IsURLSearchParams()) {
|
2017-09-20 04:32:12 +03:00
|
|
|
BodyExtractor<const URLSearchParams> body(
|
|
|
|
&aBodyInit.GetAsURLSearchParams());
|
2017-08-11 04:04:55 +03:00
|
|
|
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
|
|
|
charset);
|
|
|
|
}
|
|
|
|
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Should never reach here");
|
2017-08-11 04:04:55 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
template <class Derived>
|
2017-05-16 13:12:24 +03:00
|
|
|
FetchBody<Derived>::FetchBody(nsIGlobalObject* aOwner)
|
2017-06-20 18:53:20 +03:00
|
|
|
: mOwner(aOwner),
|
2017-06-20 18:53:21 +03:00
|
|
|
mWorkerPrivate(nullptr),
|
2017-08-11 04:04:54 +03:00
|
|
|
mReadableStreamBody(nullptr),
|
2017-08-11 04:04:55 +03:00
|
|
|
mReadableStreamReader(nullptr),
|
2014-12-10 11:51:59 +03:00
|
|
|
mBodyUsed(false) {
|
2017-05-16 13:12:24 +03:00
|
|
|
MOZ_ASSERT(aOwner);
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
mWorkerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
2017-05-16 13:12:24 +03:00
|
|
|
mMainThreadEventTarget = mWorkerPrivate->MainThreadEventTarget();
|
2014-12-10 11:51:59 +03:00
|
|
|
} else {
|
2017-05-16 13:12:24 +03:00
|
|
|
mMainThreadEventTarget = aOwner->EventTargetFor(TaskCategory::Other);
|
2014-12-10 11:51:59 +03:00
|
|
|
}
|
2017-05-16 13:12:24 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(mMainThreadEventTarget);
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2017-05-16 13:12:24 +03:00
|
|
|
template FetchBody<Request>::FetchBody(nsIGlobalObject* aOwner);
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2017-05-16 13:12:24 +03:00
|
|
|
template FetchBody<Response>::FetchBody(nsIGlobalObject* aOwner);
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2015-02-19 04:34:29 +03:00
|
|
|
template <class Derived>
|
|
|
|
FetchBody<Derived>::~FetchBody() {
|
2017-08-29 12:31:06 +03:00
|
|
|
Unfollow();
|
2015-02-19 04:34:29 +03:00
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
template FetchBody<Request>::~FetchBody();
|
|
|
|
|
|
|
|
template FetchBody<Response>::~FetchBody();
|
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
template <class Derived>
|
2018-10-11 22:18:43 +03:00
|
|
|
bool FetchBody<Derived>::GetBodyUsed(ErrorResult& aRv) const {
|
2017-08-11 04:04:54 +03:00
|
|
|
if (mBodyUsed) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:30:18 +03:00
|
|
|
// If this stream is disturbed, return true.
|
2017-08-11 04:04:54 +03:00
|
|
|
if (mReadableStreamBody) {
|
2018-10-31 20:30:18 +03:00
|
|
|
aRv.MightThrowJSException();
|
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (!jsapi.Init(mOwner)) {
|
2018-10-11 22:18:43 +03:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2017-08-11 04:04:54 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
JS::Rooted<JSObject*> body(cx, mReadableStreamBody);
|
2018-10-11 22:18:43 +03:00
|
|
|
bool disturbed;
|
2018-10-31 20:30:18 +03:00
|
|
|
if (!JS::ReadableStreamIsDisturbed(cx, body, &disturbed)) {
|
2018-10-11 22:18:43 +03:00
|
|
|
aRv.StealExceptionFromJSContext(cx);
|
|
|
|
return false;
|
2017-08-11 04:04:54 +03:00
|
|
|
}
|
2018-10-11 22:18:43 +03:00
|
|
|
|
2018-10-31 20:30:18 +03:00
|
|
|
return disturbed;
|
2017-08-11 04:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-11 22:18:43 +03:00
|
|
|
template bool FetchBody<Request>::GetBodyUsed(ErrorResult&) const;
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2018-10-11 22:18:43 +03:00
|
|
|
template bool FetchBody<Response>::GetBodyUsed(ErrorResult&) const;
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
bool FetchBody<Derived>::CheckBodyUsed() const {
|
|
|
|
IgnoredErrorResult result;
|
|
|
|
bool bodyUsed = GetBodyUsed(result);
|
|
|
|
if (result.Failed()) {
|
|
|
|
// Ignore the error.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return bodyUsed;
|
|
|
|
}
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
template <class Derived>
|
2017-08-11 04:04:55 +03:00
|
|
|
void FetchBody<Derived>::SetBodyUsed(JSContext* aCx, ErrorResult& aRv) {
|
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
MOZ_ASSERT(mOwner->EventTargetFor(TaskCategory::Other)->IsOnCurrentThread());
|
|
|
|
|
|
|
|
if (mBodyUsed) {
|
|
|
|
return;
|
2014-12-10 11:51:59 +03:00
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
mBodyUsed = true;
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
// If we already have a ReadableStreamBody and it has been created by DOM, we
|
|
|
|
// have to lock it now because it can have been shared with other objects.
|
2017-08-11 04:04:54 +03:00
|
|
|
if (mReadableStreamBody) {
|
2018-10-31 20:30:18 +03:00
|
|
|
aRv.MightThrowJSException();
|
|
|
|
|
2019-03-12 17:02:13 +03:00
|
|
|
JSAutoRealm ar(aCx, mOwner->GetGlobalJSObject());
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
JS::Rooted<JSObject*> readableStreamObj(aCx, mReadableStreamBody);
|
2018-10-11 22:18:43 +03:00
|
|
|
|
|
|
|
JS::ReadableStreamMode mode;
|
|
|
|
if (!JS::ReadableStreamGetMode(aCx, readableStreamObj, &mode)) {
|
|
|
|
aRv.StealExceptionFromJSContext(aCx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == JS::ReadableStreamMode::ExternalSource) {
|
2017-08-11 04:04:55 +03:00
|
|
|
LockStream(aCx, readableStreamObj, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2017-08-11 04:04:55 +03:00
|
|
|
return;
|
2017-08-11 04:04:55 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If this is not a native ReadableStream, let's activate the
|
|
|
|
// FetchStreamReader.
|
|
|
|
MOZ_ASSERT(mFetchStreamReader);
|
|
|
|
JS::Rooted<JSObject*> reader(aCx);
|
|
|
|
mFetchStreamReader->StartConsuming(aCx, readableStreamObj, &reader, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2017-08-11 04:04:55 +03:00
|
|
|
return;
|
2017-08-11 04:04:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mReadableStreamReader = reader;
|
2017-08-11 04:04:55 +03:00
|
|
|
}
|
2017-08-11 04:04:54 +03:00
|
|
|
}
|
2017-08-11 04:04:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template void FetchBody<Request>::SetBodyUsed(JSContext* aCx, ErrorResult& aRv);
|
|
|
|
|
|
|
|
template void FetchBody<Response>::SetBodyUsed(JSContext* aCx,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
already_AddRefed<Promise> FetchBody<Derived>::ConsumeBody(
|
2019-06-07 17:16:59 +03:00
|
|
|
JSContext* aCx, BodyConsumer::ConsumeType aType, ErrorResult& aRv) {
|
2018-10-31 20:30:18 +03:00
|
|
|
aRv.MightThrowJSException();
|
|
|
|
|
2018-08-26 15:16:21 +03:00
|
|
|
RefPtr<AbortSignalImpl> signalImpl = DerivedClass()->GetSignalImpl();
|
|
|
|
if (signalImpl && signalImpl->Aborted()) {
|
2017-08-29 12:31:06 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_ABORT_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-10-11 22:18:43 +03:00
|
|
|
bool bodyUsed = GetBodyUsed(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (bodyUsed) {
|
2017-08-11 04:04:55 +03:00
|
|
|
aRv.ThrowTypeError<MSG_FETCH_BODY_CONSUMED_ERROR>();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-06 16:48:07 +03:00
|
|
|
// Null bodies are a special-case in the fetch spec. The Body mix-in can only
|
|
|
|
// be "disturbed" or "locked" if its associated "body" is non-null.
|
|
|
|
// Additionally, the Body min-in's "consume body" algorithm explicitly creates
|
|
|
|
// a fresh empty ReadableStream object in step 2. This means that `bodyUsed`
|
|
|
|
// will never return true for a null body.
|
|
|
|
//
|
|
|
|
// To this end, we create a fresh (empty) body every time a request is made
|
|
|
|
// and consume its body here, without marking this FetchBody consumed via
|
|
|
|
// SetBodyUsed.
|
|
|
|
nsCOMPtr<nsIInputStream> bodyStream;
|
|
|
|
DerivedClass()->GetBody(getter_AddRefs(bodyStream));
|
|
|
|
if (!bodyStream) {
|
|
|
|
RefPtr<EmptyBody> emptyBody = EmptyBody::Create(
|
|
|
|
DerivedClass()->GetParentObject(),
|
|
|
|
DerivedClass()->GetPrincipalInfo().get(), signalImpl, mMimeType, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return emptyBody->ConsumeBody(aCx, aType, aRv);
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
SetBodyUsed(aCx, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = DerivedClass()->GetParentObject();
|
|
|
|
|
2019-06-07 16:09:32 +03:00
|
|
|
MutableBlobStorage::MutableBlobStorageType blobStorageType =
|
|
|
|
MutableBlobStorage::eOnlyInMemory;
|
|
|
|
const mozilla::UniquePtr<mozilla::ipc::PrincipalInfo>& principalInfo =
|
|
|
|
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))) {
|
|
|
|
blobStorageType = MutableBlobStorage::eCouldBeInTemporaryFile;
|
|
|
|
}
|
|
|
|
|
2019-06-07 17:16:59 +03:00
|
|
|
RefPtr<Promise> promise = BodyConsumer::Create(
|
2019-06-07 16:09:32 +03:00
|
|
|
global, mMainThreadEventTarget, bodyStream, signalImpl, aType,
|
|
|
|
BodyBlobURISpec(), BodyLocalPath(), MimeType(), blobStorageType, aRv);
|
2014-12-10 11:51:59 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise.forget();
|
2014-09-24 09:03:20 +04:00
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
template already_AddRefed<Promise> FetchBody<Request>::ConsumeBody(
|
2019-06-07 17:16:59 +03:00
|
|
|
JSContext* aCx, BodyConsumer::ConsumeType aType, ErrorResult& aRv);
|
2014-09-27 03:41:15 +04:00
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
template already_AddRefed<Promise> FetchBody<Response>::ConsumeBody(
|
2019-06-07 17:16:59 +03:00
|
|
|
JSContext* aCx, BodyConsumer::ConsumeType aType, ErrorResult& aRv);
|
2014-09-27 03:41:15 +04:00
|
|
|
|
2018-11-06 16:48:07 +03:00
|
|
|
template already_AddRefed<Promise> FetchBody<EmptyBody>::ConsumeBody(
|
2019-06-07 17:16:59 +03:00
|
|
|
JSContext* aCx, BodyConsumer::ConsumeType aType, ErrorResult& aRv);
|
2018-11-06 16:48:07 +03:00
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
template <class Derived>
|
2015-02-03 23:59:32 +03:00
|
|
|
void FetchBody<Derived>::SetMimeType() {
|
2014-09-27 03:41:15 +04:00
|
|
|
// Extract mime type.
|
2015-02-03 23:59:32 +03:00
|
|
|
ErrorResult result;
|
2016-10-10 20:20:14 +03:00
|
|
|
nsCString contentTypeValues;
|
2014-10-02 21:59:20 +04:00
|
|
|
MOZ_ASSERT(DerivedClass()->GetInternalHeaders());
|
2016-10-10 20:20:14 +03:00
|
|
|
DerivedClass()->GetInternalHeaders()->Get(NS_LITERAL_CSTRING("Content-Type"),
|
|
|
|
contentTypeValues, result);
|
2015-02-03 23:59:32 +03:00
|
|
|
MOZ_ALWAYS_TRUE(!result.Failed());
|
2014-09-27 03:41:15 +04:00
|
|
|
|
|
|
|
// HTTP ABNF states Content-Type may have only one value.
|
|
|
|
// This is from the "parse a header value" of the fetch spec.
|
2016-10-10 20:20:14 +03:00
|
|
|
if (!contentTypeValues.IsVoid() && contentTypeValues.Find(",") == -1) {
|
2019-04-14 18:02:39 +03:00
|
|
|
// Convert from a bytestring to a UTF8 CString.
|
|
|
|
CopyLatin1toUTF8(contentTypeValues, mMimeType);
|
2014-09-27 03:41:15 +04:00
|
|
|
ToLowerCase(mMimeType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-03 23:59:32 +03:00
|
|
|
template void FetchBody<Request>::SetMimeType();
|
2014-09-27 03:41:15 +04:00
|
|
|
|
2015-02-03 23:59:32 +03:00
|
|
|
template void FetchBody<Response>::SetMimeType();
|
2015-09-17 15:10:42 +03:00
|
|
|
|
2018-11-06 16:48:07 +03:00
|
|
|
template <class Derived>
|
|
|
|
void FetchBody<Derived>::OverrideMimeType(const nsACString& aMimeType) {
|
|
|
|
mMimeType = aMimeType;
|
|
|
|
}
|
|
|
|
|
2018-10-06 20:34:24 +03:00
|
|
|
template <class Derived>
|
|
|
|
const nsACString& FetchBody<Derived>::BodyBlobURISpec() const {
|
|
|
|
return DerivedClass()->BodyBlobURISpec();
|
|
|
|
}
|
|
|
|
|
|
|
|
template const nsACString& FetchBody<Request>::BodyBlobURISpec() const;
|
|
|
|
|
|
|
|
template const nsACString& FetchBody<Response>::BodyBlobURISpec() const;
|
|
|
|
|
2018-11-06 16:48:07 +03:00
|
|
|
template const nsACString& FetchBody<EmptyBody>::BodyBlobURISpec() const;
|
|
|
|
|
2018-09-11 22:13:15 +03:00
|
|
|
template <class Derived>
|
|
|
|
const nsAString& FetchBody<Derived>::BodyLocalPath() const {
|
|
|
|
return DerivedClass()->BodyLocalPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
template const nsAString& FetchBody<Request>::BodyLocalPath() const;
|
|
|
|
|
|
|
|
template const nsAString& FetchBody<Response>::BodyLocalPath() const;
|
|
|
|
|
2018-11-06 16:48:07 +03:00
|
|
|
template const nsAString& FetchBody<EmptyBody>::BodyLocalPath() const;
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
template <class Derived>
|
2017-08-29 12:31:06 +03:00
|
|
|
void FetchBody<Derived>::SetReadableStreamBody(JSContext* aCx,
|
|
|
|
JSObject* aBody) {
|
2017-08-11 04:04:55 +03:00
|
|
|
MOZ_ASSERT(!mReadableStreamBody);
|
|
|
|
MOZ_ASSERT(aBody);
|
|
|
|
mReadableStreamBody = aBody;
|
2017-08-29 12:31:06 +03:00
|
|
|
|
2018-08-26 15:16:21 +03:00
|
|
|
RefPtr<AbortSignalImpl> signalImpl = DerivedClass()->GetSignalImpl();
|
|
|
|
if (!signalImpl) {
|
2017-08-29 12:31:06 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-26 15:16:21 +03:00
|
|
|
bool aborted = signalImpl->Aborted();
|
2017-08-29 12:31:06 +03:00
|
|
|
if (aborted) {
|
|
|
|
JS::Rooted<JSObject*> body(aCx, mReadableStreamBody);
|
2018-10-11 22:18:43 +03:00
|
|
|
IgnoredErrorResult result;
|
|
|
|
AbortStream(aCx, body, result);
|
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-29 12:31:06 +03:00
|
|
|
} else if (!IsFollowing()) {
|
2018-08-26 15:16:21 +03:00
|
|
|
Follow(signalImpl);
|
2017-08-29 12:31:06 +03:00
|
|
|
}
|
2017-08-11 04:04:55 +03:00
|
|
|
}
|
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
template void FetchBody<Request>::SetReadableStreamBody(JSContext* aCx,
|
|
|
|
JSObject* aBody);
|
2017-08-11 04:04:55 +03:00
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
template void FetchBody<Response>::SetReadableStreamBody(JSContext* aCx,
|
|
|
|
JSObject* aBody);
|
2017-08-11 04:04:55 +03:00
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
template <class Derived>
|
|
|
|
void FetchBody<Derived>::GetBody(JSContext* aCx,
|
2017-08-11 04:04:54 +03:00
|
|
|
JS::MutableHandle<JSObject*> aBodyOut,
|
|
|
|
ErrorResult& aRv) {
|
2017-08-11 04:04:55 +03:00
|
|
|
if (mReadableStreamBody) {
|
|
|
|
aBodyOut.set(mReadableStreamBody);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
|
|
|
DerivedClass()->GetBody(getter_AddRefs(inputStream));
|
|
|
|
|
|
|
|
if (!inputStream) {
|
|
|
|
aBodyOut.set(nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-07 16:09:12 +03:00
|
|
|
BodyStream::Create(aCx, this, DerivedClass()->GetParentObject(), inputStream,
|
2019-07-24 02:23:41 +03:00
|
|
|
aRv);
|
2017-08-11 04:04:55 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2019-07-24 02:23:41 +03:00
|
|
|
MOZ_ASSERT(mReadableStreamBody);
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2019-07-24 02:23:41 +03:00
|
|
|
JS::Rooted<JSObject*> body(aCx, mReadableStreamBody);
|
2019-07-13 02:43:16 +03:00
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
// If the body has been already consumed, we lock the stream.
|
2018-10-11 22:18:43 +03:00
|
|
|
bool bodyUsed = GetBodyUsed(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (bodyUsed) {
|
2017-08-11 04:04:55 +03:00
|
|
|
LockStream(aCx, body, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
2017-08-11 04:04:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-26 15:16:21 +03:00
|
|
|
RefPtr<AbortSignalImpl> signalImpl = DerivedClass()->GetSignalImpl();
|
|
|
|
if (signalImpl) {
|
|
|
|
if (signalImpl->Aborted()) {
|
2018-10-11 22:18:43 +03:00
|
|
|
AbortStream(aCx, body, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-29 12:31:06 +03:00
|
|
|
} else if (!IsFollowing()) {
|
2018-08-26 15:16:21 +03:00
|
|
|
Follow(signalImpl);
|
2017-08-29 12:31:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
aBodyOut.set(mReadableStreamBody);
|
2017-08-11 04:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template void FetchBody<Request>::GetBody(JSContext* aCx,
|
2017-08-11 04:04:54 +03:00
|
|
|
JS::MutableHandle<JSObject*> aMessage,
|
|
|
|
ErrorResult& aRv);
|
2017-08-11 04:04:54 +03:00
|
|
|
|
|
|
|
template void FetchBody<Response>::GetBody(
|
2017-08-11 04:04:54 +03:00
|
|
|
JSContext* aCx, JS::MutableHandle<JSObject*> aMessage, ErrorResult& aRv);
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
template <class Derived>
|
|
|
|
void FetchBody<Derived>::LockStream(JSContext* aCx, JS::HandleObject aStream,
|
|
|
|
ErrorResult& aRv) {
|
2018-10-31 20:30:18 +03:00
|
|
|
aRv.MightThrowJSException();
|
|
|
|
|
2018-10-11 22:18:43 +03:00
|
|
|
#if DEBUG
|
|
|
|
JS::ReadableStreamMode streamMode;
|
|
|
|
if (!JS::ReadableStreamGetMode(aCx, aStream, &streamMode)) {
|
|
|
|
aRv.StealExceptionFromJSContext(aCx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(streamMode == JS::ReadableStreamMode::ExternalSource);
|
|
|
|
#endif // DEBUG
|
2017-08-11 04:04:56 +03:00
|
|
|
|
|
|
|
// This is native stream, creating a reader will not execute any JS code.
|
2017-08-11 04:04:55 +03:00
|
|
|
JS::Rooted<JSObject*> reader(
|
|
|
|
aCx, JS::ReadableStreamGetReader(aCx, aStream,
|
|
|
|
JS::ReadableStreamReaderMode::Default));
|
|
|
|
if (!reader) {
|
|
|
|
aRv.StealExceptionFromJSContext(aCx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mReadableStreamReader = reader;
|
2017-08-11 04:04:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template void FetchBody<Request>::LockStream(JSContext* aCx,
|
|
|
|
JS::HandleObject aStream,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
|
|
|
template void FetchBody<Response>::LockStream(JSContext* aCx,
|
|
|
|
JS::HandleObject aStream,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void FetchBody<Derived>::MaybeTeeReadableStreamBody(
|
|
|
|
JSContext* aCx, JS::MutableHandle<JSObject*> aBodyOut,
|
2017-08-11 04:04:55 +03:00
|
|
|
FetchStreamReader** aStreamReader, nsIInputStream** aInputStream,
|
2017-08-11 04:04:55 +03:00
|
|
|
ErrorResult& aRv) {
|
2017-08-11 04:04:55 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aStreamReader);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aInputStream);
|
2018-10-11 22:18:43 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!CheckBodyUsed());
|
2017-08-11 04:04:55 +03:00
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
aBodyOut.set(nullptr);
|
|
|
|
*aStreamReader = nullptr;
|
|
|
|
*aInputStream = nullptr;
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
if (!mReadableStreamBody) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:30:18 +03:00
|
|
|
aRv.MightThrowJSException();
|
|
|
|
|
2019-03-12 17:02:13 +03:00
|
|
|
JSAutoRealm ar(aCx, mOwner->GetGlobalJSObject());
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
JS::Rooted<JSObject*> stream(aCx, mReadableStreamBody);
|
|
|
|
|
|
|
|
// If this is a ReadableStream with an external source, this has been
|
|
|
|
// generated by a Fetch. In this case, Fetch will be able to recreate it
|
|
|
|
// again when GetBody() is called.
|
2018-10-11 22:18:43 +03:00
|
|
|
JS::ReadableStreamMode streamMode;
|
|
|
|
if (!JS::ReadableStreamGetMode(aCx, stream, &streamMode)) {
|
|
|
|
aRv.StealExceptionFromJSContext(aCx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (streamMode == JS::ReadableStreamMode::ExternalSource) {
|
2017-08-11 04:04:55 +03:00
|
|
|
aBodyOut.set(nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> branch1(aCx);
|
|
|
|
JS::Rooted<JSObject*> branch2(aCx);
|
|
|
|
|
|
|
|
if (!JS::ReadableStreamTee(aCx, stream, &branch1, &branch2)) {
|
|
|
|
aRv.StealExceptionFromJSContext(aCx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mReadableStreamBody = branch1;
|
|
|
|
aBodyOut.set(branch2);
|
2017-08-11 04:04:55 +03:00
|
|
|
|
|
|
|
aRv = FetchStreamReader::Create(aCx, mOwner, aStreamReader, aInputStream);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-11 04:04:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template void FetchBody<Request>::MaybeTeeReadableStreamBody(
|
|
|
|
JSContext* aCx, JS::MutableHandle<JSObject*> aMessage,
|
2017-08-11 04:04:55 +03:00
|
|
|
FetchStreamReader** aStreamReader, nsIInputStream** aInputStream,
|
2017-08-11 04:04:55 +03:00
|
|
|
ErrorResult& aRv);
|
|
|
|
|
|
|
|
template void FetchBody<Response>::MaybeTeeReadableStreamBody(
|
|
|
|
JSContext* aCx, JS::MutableHandle<JSObject*> aMessage,
|
2017-08-11 04:04:55 +03:00
|
|
|
FetchStreamReader** aStreamReader, nsIInputStream** aInputStream,
|
2017-08-11 04:04:55 +03:00
|
|
|
ErrorResult& aRv);
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
template <class Derived>
|
2017-08-29 12:31:07 +03:00
|
|
|
void FetchBody<Derived>::Abort() {
|
2018-04-24 17:10:38 +03:00
|
|
|
if (!mReadableStreamBody) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-29 12:31:06 +03:00
|
|
|
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (!jsapi.Init(mOwner)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> body(cx, mReadableStreamBody);
|
2018-10-11 22:18:43 +03:00
|
|
|
IgnoredErrorResult result;
|
|
|
|
AbortStream(cx, body, result);
|
2017-08-29 12:31:06 +03:00
|
|
|
}
|
|
|
|
|
2017-08-29 12:31:07 +03:00
|
|
|
template void FetchBody<Request>::Abort();
|
2017-08-29 12:31:06 +03:00
|
|
|
|
2017-08-29 12:31:07 +03:00
|
|
|
template void FetchBody<Response>::Abort();
|
2017-08-29 12:31:06 +03:00
|
|
|
|
2014-09-24 09:03:20 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|