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"
|
2017-06-20 18:53:21 +03:00
|
|
|
#include "FetchConsumer.h"
|
2017-08-11 04:04:54 +03:00
|
|
|
#include "FetchStream.h"
|
2014-09-24 09:03:20 +04:00
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIGlobalObject.h"
|
2014-12-10 11:51:59 +03:00
|
|
|
#include "nsIStreamLoader.h"
|
|
|
|
#include "nsIThreadRetargetableRequest.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"
|
2016-02-28 00:54:11 +03:00
|
|
|
#include "mozilla/dom/BodyUtil.h"
|
2014-12-05 01:06:45 +03:00
|
|
|
#include "mozilla/dom/Exceptions.h"
|
2017-08-11 04:04:54 +03:00
|
|
|
#include "mozilla/dom/DOMError.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"
|
2016-10-26 10:07:25 +03:00
|
|
|
#include "mozilla/dom/MutableBlobStreamListener.h"
|
2014-09-27 03:41:15 +04:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2015-09-03 01:04:17 +03:00
|
|
|
#include "mozilla/dom/PromiseWorkerProxy.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"
|
2016-09-07 05:20:23 +03:00
|
|
|
#include "mozilla/dom/workers/ServiceWorkerManager.h"
|
2015-03-27 13:47:00 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2017-01-24 14:13:31 +03:00
|
|
|
#include "BodyExtractor.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
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
#include "WorkerRunnable.h"
|
2014-12-10 11:51:59 +03:00
|
|
|
#include "WorkerScope.h"
|
|
|
|
#include "Workers.h"
|
2014-09-24 09:03:20 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
using namespace workers;
|
|
|
|
|
2017-03-22 13:06:08 +03:00
|
|
|
// This class helps the proxying of FetchSignal changes cross threads.
|
|
|
|
class FetchSignalProxy final : public FetchSignal::Follower
|
|
|
|
{
|
|
|
|
// This is created and released on the main-thread.
|
|
|
|
RefPtr<FetchSignal> mSignalMainThread;
|
|
|
|
|
2017-05-16 11:59:29 +03:00
|
|
|
// The main-thread event target for runnable dispatching.
|
|
|
|
nsCOMPtr<nsIEventTarget> mMainThreadEventTarget;
|
|
|
|
|
2017-03-22 13:06:08 +03:00
|
|
|
// This value is used only for the creation of FetchSignal on the
|
|
|
|
// main-thread. They are not updated.
|
|
|
|
const bool mAborted;
|
|
|
|
|
|
|
|
// This runnable propagates changes from the FetchSignal on workers to the
|
|
|
|
// FetchSignal on main-thread.
|
|
|
|
class FetchSignalProxyRunnable final : public Runnable
|
|
|
|
{
|
|
|
|
RefPtr<FetchSignalProxy> mProxy;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit FetchSignalProxyRunnable(FetchSignalProxy* aProxy)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("dom::FetchSignalProxy::FetchSignalProxyRunnable")
|
|
|
|
, mProxy(aProxy)
|
2017-03-22 13:06:08 +03:00
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
FetchSignal* signal = mProxy->GetOrCreateSignalForMainThread();
|
|
|
|
signal->Abort();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FetchSignalProxy)
|
|
|
|
|
2017-05-16 11:59:29 +03:00
|
|
|
FetchSignalProxy(FetchSignal* aSignal, nsIEventTarget* aMainThreadEventTarget)
|
|
|
|
: mMainThreadEventTarget(aMainThreadEventTarget)
|
|
|
|
, mAborted(aSignal->Aborted())
|
2017-03-22 13:06:08 +03:00
|
|
|
{
|
2017-05-16 11:59:29 +03:00
|
|
|
MOZ_ASSERT(mMainThreadEventTarget);
|
2017-03-22 13:06:08 +03:00
|
|
|
Follow(aSignal);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Aborted() override
|
|
|
|
{
|
|
|
|
RefPtr<FetchSignalProxyRunnable> runnable =
|
|
|
|
new FetchSignalProxyRunnable(this);
|
2017-05-16 11:59:29 +03:00
|
|
|
mMainThreadEventTarget->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
|
2017-03-22 13:06:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
FetchSignal*
|
|
|
|
GetOrCreateSignalForMainThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!mSignalMainThread) {
|
|
|
|
mSignalMainThread = new FetchSignal(mAborted);
|
|
|
|
}
|
|
|
|
return mSignalMainThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Shutdown()
|
|
|
|
{
|
|
|
|
Unfollow();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~FetchSignalProxy()
|
|
|
|
{
|
2017-06-14 04:27:17 +03:00
|
|
|
NS_ProxyRelease(
|
|
|
|
"FetchSignalProxy::mSignalMainThread",
|
|
|
|
mMainThreadEventTarget, mSignalMainThread.forget());
|
2017-03-22 13:06:08 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-03 01:04:17 +03:00
|
|
|
class WorkerFetchResolver final : public FetchDriverObserver
|
2014-10-06 22:01:20 +04:00
|
|
|
{
|
2014-12-10 11:51:59 +03:00
|
|
|
friend class MainThreadFetchRunnable;
|
2017-03-22 13:07:27 +03:00
|
|
|
friend class WorkerDataAvailableRunnable;
|
2017-03-22 13:06:08 +03:00
|
|
|
friend class WorkerFetchResponseEndBase;
|
2014-12-10 11:51:59 +03:00
|
|
|
friend class WorkerFetchResponseEndRunnable;
|
2014-10-06 22:01:20 +04:00
|
|
|
friend class WorkerFetchResponseRunnable;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> mPromiseProxy;
|
2017-03-22 13:06:08 +03:00
|
|
|
RefPtr<FetchSignalProxy> mSignalProxy;
|
2017-03-22 13:07:27 +03:00
|
|
|
RefPtr<FetchObserver> mFetchObserver;
|
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>
|
2017-03-22 13:06:08 +03:00
|
|
|
Create(workers::WorkerPrivate* aWorkerPrivate, Promise* aPromise,
|
2017-03-22 13:07:27 +03:00
|
|
|
FetchSignal* aSignal, FetchObserver* aObserver)
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
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-03-22 13:06:08 +03:00
|
|
|
RefPtr<FetchSignalProxy> signalProxy;
|
|
|
|
if (aSignal) {
|
2017-05-16 11:59:29 +03:00
|
|
|
signalProxy =
|
|
|
|
new FetchSignalProxy(aSignal, 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);
|
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-03-22 13:06:08 +03:00
|
|
|
FetchSignal*
|
|
|
|
GetFetchSignal()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!mSignalProxy) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mSignalProxy->GetOrCreateSignalForMainThread();
|
|
|
|
}
|
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
void
|
2015-09-14 18:05:35 +03:00
|
|
|
OnResponseAvailableInternal(InternalResponse* aResponse) override;
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
void
|
2017-03-22 13:06:08 +03:00
|
|
|
OnResponseEnd(FetchDriverObserver::EndReason eReason) override;
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2017-03-22 13:07:27 +03:00
|
|
|
void
|
|
|
|
OnDataAvailable() override;
|
|
|
|
|
2015-09-03 01:04:17 +03:00
|
|
|
private:
|
2017-03-22 13:06:08 +03:00
|
|
|
WorkerFetchResolver(PromiseWorkerProxy* aProxy,
|
2017-03-22 13:07:27 +03:00
|
|
|
FetchSignalProxy* aSignalProxy,
|
|
|
|
FetchObserver* aObserver)
|
2015-09-03 01:04:17 +03:00
|
|
|
: mPromiseProxy(aProxy)
|
2017-03-22 13:06:08 +03:00
|
|
|
, mSignalProxy(aSignalProxy)
|
2017-03-22 13:07:27 +03:00
|
|
|
, mFetchObserver(aObserver)
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
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
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
~WorkerFetchResolver()
|
2015-09-03 01:04:17 +03:00
|
|
|
{}
|
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
|
2014-10-06 22:01:20 +04:00
|
|
|
{
|
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;
|
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-03-22 13:07:27 +03:00
|
|
|
MainThreadFetchResolver(Promise* aPromise, FetchObserver* aObserver)
|
|
|
|
: mPromise(aPromise)
|
|
|
|
, mFetchObserver(aObserver)
|
|
|
|
{}
|
2014-10-06 22:01:20 +04:00
|
|
|
|
|
|
|
void
|
2015-09-14 18:05:35 +03:00
|
|
|
OnResponseAvailableInternal(InternalResponse* aResponse) override;
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2017-02-06 04:19:34 +03:00
|
|
|
void SetLoadGroup(nsILoadGroup* aLoadGroup)
|
2016-09-07 05:20:23 +03:00
|
|
|
{
|
2017-02-06 04:19:34 +03:00
|
|
|
mLoadGroup = aLoadGroup;
|
2016-09-07 05:20:23 +03:00
|
|
|
}
|
|
|
|
|
2017-03-22 13:06:08 +03:00
|
|
|
void
|
|
|
|
OnResponseEnd(FetchDriverObserver::EndReason aReason) override
|
2016-09-07 05:20:23 +03:00
|
|
|
{
|
2017-03-22 13:06:08 +03:00
|
|
|
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-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
|
2014-10-06 22:01:20 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerFetchResolver> mResolver;
|
|
|
|
RefPtr<InternalRequest> mRequest;
|
2014-10-06 22:01:20 +04:00
|
|
|
|
|
|
|
public:
|
2015-09-03 01:04:17 +03:00
|
|
|
MainThreadFetchRunnable(WorkerFetchResolver* aResolver,
|
2014-10-06 22:01:20 +04:00
|
|
|
InternalRequest* aRequest)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("dom::MainThreadFetchRunnable")
|
|
|
|
, mResolver(aResolver)
|
2014-10-06 22:01:20 +04:00
|
|
|
, mRequest(aRequest)
|
|
|
|
{
|
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;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> proxy = mResolver->mPromiseProxy;
|
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,
|
|
|
|
workerPrivate->MainThreadEventTarget(), false);
|
2016-09-07 05:20:23 +03:00
|
|
|
nsAutoCString spec;
|
|
|
|
if (proxy->GetWorkerPrivate()->GetBaseURI()) {
|
|
|
|
proxy->GetWorkerPrivate()->GetBaseURI()->GetAsciiSpec(spec);
|
|
|
|
}
|
|
|
|
fetch->SetWorkerScript(spec);
|
2014-10-21 00:25:56 +04:00
|
|
|
}
|
2016-06-14 06:08:00 +03:00
|
|
|
|
2017-03-22 13:06:08 +03:00
|
|
|
RefPtr<FetchSignal> signal = mResolver->GetFetchSignal();
|
|
|
|
|
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.
|
2017-03-22 13:06:08 +03:00
|
|
|
return fetch->Fetch(signal, mResolver);
|
2014-10-06 22:01:20 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2014-11-20 14:58:00 +03:00
|
|
|
FetchRequest(nsIGlobalObject* aGlobal, const RequestOrUSVString& aInput,
|
2017-02-01 23:43:37 +03:00
|
|
|
const RequestInit& aInit, CallerType aCallerType, ErrorResult& aRv)
|
2014-10-06 22:01:20 +04:00
|
|
|
{
|
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);
|
2014-10-06 22:01:20 +04:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalRequest> r = request->GetInternalRequest();
|
2014-12-23 18:56:19 +03:00
|
|
|
|
2017-03-22 13:06:08 +03:00
|
|
|
RefPtr<FetchSignal> signal;
|
|
|
|
if (aInit.mSignal.WasPassed()) {
|
|
|
|
signal = &aInit.mSignal.Value();
|
|
|
|
// Let's FetchDriver to deal with an already aborted signal.
|
|
|
|
}
|
|
|
|
|
2017-03-22 13:07:27 +03:00
|
|
|
RefPtr<FetchObserver> observer;
|
|
|
|
if (aInit.mObserve.WasPassed()) {
|
|
|
|
observer = new FetchObserver(aGlobal, signal);
|
|
|
|
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);
|
2015-05-21 00:26:31 +03:00
|
|
|
nsCOMPtr<nsIDocument> doc;
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
|
|
|
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();
|
2017-05-19 08:29:00 +03:00
|
|
|
|
|
|
|
nsAutoCString fileNameString;
|
|
|
|
if (nsJSUtils::GetCallingLocation(cx, fileNameString)) {
|
|
|
|
isTrackingFetch = doc->IsScriptTracking(fileNameString);
|
|
|
|
}
|
2015-05-21 00:26:31 +03:00
|
|
|
} else {
|
|
|
|
principal = aGlobal->PrincipalOrNull();
|
|
|
|
if (NS_WARN_IF(!principal)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-03-27 13:47:00 +03:00
|
|
|
Telemetry::Accumulate(Telemetry::FETCH_IS_MAINTHREAD, 1);
|
|
|
|
|
2017-03-22 13:07:27 +03:00
|
|
|
RefPtr<MainThreadFetchResolver> resolver =
|
|
|
|
new MainThreadFetchResolver(p, observer);
|
2017-05-19 08:29:00 +03:00
|
|
|
RefPtr<FetchDriver> fetch =
|
2017-05-16 13:27:01 +03:00
|
|
|
new FetchDriver(r, principal, loadGroup,
|
|
|
|
aGlobal->EventTargetFor(TaskCategory::Other), isTrackingFetch);
|
2014-10-15 03:36:58 +04:00
|
|
|
fetch->SetDocument(doc);
|
2017-02-06 04:19:34 +03:00
|
|
|
resolver->SetLoadGroup(loadGroup);
|
2017-03-22 13:06:08 +03:00
|
|
|
aRv = fetch->Fetch(signal, 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
|
|
|
|
2015-03-27 13:47:00 +03:00
|
|
|
Telemetry::Accumulate(Telemetry::FETCH_IS_MAINTHREAD, 0);
|
|
|
|
|
2014-12-24 13:08:41 +03:00
|
|
|
if (worker->IsServiceWorker()) {
|
|
|
|
r->SetSkipServiceWorker();
|
|
|
|
}
|
|
|
|
|
2017-03-22 13:06:08 +03:00
|
|
|
RefPtr<WorkerFetchResolver> resolver =
|
2017-03-22 13:07:27 +03:00
|
|
|
WorkerFetchResolver::Create(worker, p, signal, observer);
|
2015-09-03 01:04:17 +03:00
|
|
|
if (!resolver) {
|
2016-06-23 11:53:14 +03:00
|
|
|
NS_WARNING("Could not add WorkerFetchResolver workerHolder to worker");
|
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
|
|
|
|
2017-03-22 13:06:08 +03:00
|
|
|
RefPtr<MainThreadFetchRunnable> run =
|
|
|
|
new MainThreadFetchRunnable(resolver, r);
|
2016-09-14 06:14:02 +03:00
|
|
|
worker->DispatchToMainThread(run.forget());
|
2014-10-06 22:01:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return p.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-14 18:05:35 +03:00
|
|
|
MainThreadFetchResolver::OnResponseAvailableInternal(InternalResponse* aResponse)
|
2014-10-06 22:01:20 +04:00
|
|
|
{
|
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();
|
|
|
|
mResponse = new Response(go, aResponse);
|
|
|
|
mPromise->MaybeResolve(mResponse);
|
|
|
|
} else {
|
2017-03-22 13:07:27 +03:00
|
|
|
if (mFetchObserver) {
|
|
|
|
mFetchObserver->SetState(FetchState::Errored);
|
|
|
|
}
|
|
|
|
|
2015-01-15 00:43:27 +03:00
|
|
|
ErrorResult result;
|
2015-10-05 19:38:14 +03:00
|
|
|
result.ThrowTypeError<MSG_FETCH_FAILED>();
|
2015-01-15 00:43:27 +03:00
|
|
|
mPromise->MaybeReject(result);
|
|
|
|
}
|
2014-12-10 11:51:59 +03:00
|
|
|
}
|
2014-10-06 22:01:20 +04:00
|
|
|
|
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
|
2014-10-06 22:01:20 +04:00
|
|
|
{
|
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;
|
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)
|
2014-10-06 22:01:20 +04:00
|
|
|
{
|
2017-03-22 13:06:08 +03:00
|
|
|
MOZ_ASSERT(mResolver);
|
2014-10-06 22:01:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2014-10-06 22:01:20 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> promise = mResolver->mPromiseProxy->WorkerPromise();
|
2015-01-15 00:43:27 +03:00
|
|
|
|
|
|
|
if (mInternalResponse->Type() != ResponseType::Error) {
|
2017-03-22 13:07:27 +03:00
|
|
|
if (mResolver->mFetchObserver) {
|
|
|
|
mResolver->mFetchObserver->SetState(FetchState::Complete);
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIGlobalObject> global = aWorkerPrivate->GlobalScope();
|
|
|
|
RefPtr<Response> response = new Response(global, mInternalResponse);
|
2015-09-03 01:04:17 +03:00
|
|
|
promise->MaybeResolve(response);
|
2015-01-15 00:43:27 +03:00
|
|
|
} else {
|
2017-03-22 13:07:27 +03:00
|
|
|
if (mResolver->mFetchObserver) {
|
|
|
|
mResolver->mFetchObserver->SetState(FetchState::Errored);
|
|
|
|
}
|
|
|
|
|
2015-01-15 00:43:27 +03:00
|
|
|
ErrorResult result;
|
2015-10-05 19:38:14 +03:00
|
|
|
result.ThrowTypeError<MSG_FETCH_FAILED>();
|
2015-01-15 00:43:27 +03:00
|
|
|
promise->MaybeReject(result);
|
|
|
|
}
|
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;
|
|
|
|
public:
|
|
|
|
WorkerDataAvailableRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
WorkerFetchResolver* aResolver)
|
|
|
|
: MainThreadWorkerRunnable(aWorkerPrivate)
|
|
|
|
, mResolver(aResolver)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mResolver->mFetchObserver &&
|
|
|
|
mResolver->mFetchObserver->State() == FetchState::Requesting) {
|
|
|
|
mResolver->mFetchObserver->SetState(FetchState::Responding);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2016-05-05 23:02:27 +03:00
|
|
|
{
|
2017-03-22 13:07:27 +03:00
|
|
|
MOZ_ASSERT(aResolver);
|
2016-05-05 23:02:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerRunInternal(WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
2017-03-22 13:06:08 +03:00
|
|
|
|
2017-03-22 13:07:27 +03:00
|
|
|
mResolver->mPromiseProxy->CleanUp();
|
2017-03-22 13:06:08 +03:00
|
|
|
|
2017-03-22 13:07:27 +03:00
|
|
|
mResolver->mFetchObserver = nullptr;
|
2017-03-22 13:06:08 +03:00
|
|
|
|
2017-03-22 13:07:27 +03:00
|
|
|
if (mResolver->mSignalProxy) {
|
|
|
|
mResolver->mSignalProxy->Shutdown();
|
|
|
|
mResolver->mSignalProxy = nullptr;
|
2017-03-22 13:06:08 +03:00
|
|
|
}
|
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
|
2014-10-06 22:01:20 +04:00
|
|
|
{
|
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-12-10 11:51:59 +03:00
|
|
|
{
|
|
|
|
}
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
2017-03-22 13:07:27 +03:00
|
|
|
if (mReason == FetchDriverObserver::eAborted) {
|
|
|
|
RefPtr<Promise> promise = mResolver->mPromiseProxy->WorkerPromise();
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_ABORT_ERR);
|
|
|
|
}
|
|
|
|
|
2016-05-05 23:02:27 +03:00
|
|
|
WorkerRunInternal(aWorkerPrivate);
|
|
|
|
return true;
|
|
|
|
}
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2016-05-11 03:38:39 +03:00
|
|
|
nsresult
|
2016-05-05 23:02:27 +03:00
|
|
|
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
|
|
|
|
|
|
|
void
|
2015-09-14 18:05:35 +03:00
|
|
|
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 =
|
2015-09-03 01:04:17 +03:00
|
|
|
new WorkerFetchResponseRunnable(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-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();
|
|
|
|
}
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
void
|
2017-03-22 13:06:08 +03:00
|
|
|
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 =
|
2017-03-22 13:07:27 +03:00
|
|
|
new WorkerFetchResponseEndRunnable(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
|
2016-06-23 11:53:14 +03:00
|
|
|
// the PromiseWorkerProxy to give up its WorkerHolder 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
workers::WorkerPrivate* worker = mPromiseProxy->GetWorkerPrivate();
|
|
|
|
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
|
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
if (!swm) {
|
2017-02-06 04:19:34 +03:00
|
|
|
mReporter->FlushReportsToConsole(0);
|
2016-09-07 05:20:23 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-25 02:56:48 +03:00
|
|
|
swm->FlushReportsToAllClients(worker->ServiceWorkerScope(), mReporter);
|
2016-09-07 05:20:23 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (worker->IsSharedWorker()) {
|
|
|
|
// Flush to shared worker
|
|
|
|
worker->FlushReportsToSharedWorkers(mReporter);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-09-24 09:03:20 +04:00
|
|
|
nsresult
|
2017-02-09 12:37:54 +03:00
|
|
|
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-01-24 14:14:00 +03:00
|
|
|
BodyExtractor<nsIXHRSendable> 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-01-24 14:14:00 +03:00
|
|
|
BodyExtractor<nsIXHRSendable> 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-01-24 14:14:00 +03:00
|
|
|
BodyExtractor<nsIXHRSendable> 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
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
NS_NOTREACHED("Should never reach here");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-09-24 09:03:20 +04:00
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
nsresult
|
2017-02-09 12:37:54 +03:00
|
|
|
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-01-24 14:14:00 +03:00
|
|
|
BodyExtractor<nsIXHRSendable> 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-01-24 14:14:00 +03:00
|
|
|
BodyExtractor<nsIXHRSendable> 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-01-24 14:14:00 +03:00
|
|
|
BodyExtractor<nsIXHRSendable> 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
|
|
|
}
|
|
|
|
|
|
|
|
NS_NOTREACHED("Should never reach here");
|
|
|
|
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)
|
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
|
|
|
|
|
|
|
template
|
2017-05-16 13:12:24 +03:00
|
|
|
FetchBody<Request>::FetchBody(nsIGlobalObject* aOwner);
|
2014-12-10 11:51:59 +03:00
|
|
|
|
|
|
|
template
|
2017-05-16 13:12:24 +03:00
|
|
|
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-11 04:04:54 +03:00
|
|
|
template <class Derived>
|
|
|
|
bool
|
|
|
|
FetchBody<Derived>::BodyUsed() const
|
|
|
|
{
|
|
|
|
if (mBodyUsed) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this object is disturbed or locked, return false.
|
|
|
|
if (mReadableStreamBody) {
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (!jsapi.Init(mOwner)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> body(cx, mReadableStreamBody);
|
|
|
|
if (JS::ReadableStreamIsDisturbed(body) ||
|
|
|
|
JS::ReadableStreamIsLocked(body)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template
|
|
|
|
bool
|
|
|
|
FetchBody<Request>::BodyUsed() const;
|
|
|
|
|
|
|
|
template
|
|
|
|
bool
|
|
|
|
FetchBody<Response>::BodyUsed() const;
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
template <class Derived>
|
|
|
|
already_AddRefed<Promise>
|
2017-08-11 04:04:54 +03:00
|
|
|
FetchBody<Derived>::ConsumeBody(JSContext* aCx, FetchConsumeType aType,
|
|
|
|
ErrorResult& aRv)
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
|
|
|
if (BodyUsed()) {
|
2015-10-05 19:38:14 +03:00
|
|
|
aRv.ThrowTypeError<MSG_FETCH_BODY_CONSUMED_ERROR>();
|
2014-12-10 11:51:59 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetBodyUsed();
|
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = DerivedClass()->GetParentObject();
|
|
|
|
|
|
|
|
// If we already created a ReadableStreamBody we have to close it now.
|
|
|
|
if (mReadableStreamBody) {
|
|
|
|
JS::Rooted<JSObject*> body(aCx, mReadableStreamBody);
|
|
|
|
JS::ReadableStreamClose(aCx, body);
|
|
|
|
}
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
RefPtr<Promise> promise =
|
2017-08-11 04:04:54 +03:00
|
|
|
FetchBodyConsumer<Derived>::Create(global, mMainThreadEventTarget, this,
|
|
|
|
aType, 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
|
|
|
}
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
template
|
|
|
|
already_AddRefed<Promise>
|
2017-08-11 04:04:54 +03:00
|
|
|
FetchBody<Request>::ConsumeBody(JSContext* aCx, FetchConsumeType aType,
|
|
|
|
ErrorResult& aRv);
|
2014-09-27 03:41:15 +04:00
|
|
|
|
|
|
|
template
|
|
|
|
already_AddRefed<Promise>
|
2017-08-11 04:04:54 +03:00
|
|
|
FetchBody<Response>::ConsumeBody(JSContext* aCx, FetchConsumeType aType,
|
|
|
|
ErrorResult& aRv);
|
2014-09-27 03:41:15 +04:00
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
2015-02-03 23:59:32 +03:00
|
|
|
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) {
|
|
|
|
mMimeType = contentTypeValues;
|
2014-09-27 03:41:15 +04:00
|
|
|
ToLowerCase(mMimeType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template
|
|
|
|
void
|
2015-02-03 23:59:32 +03:00
|
|
|
FetchBody<Request>::SetMimeType();
|
2014-09-27 03:41:15 +04:00
|
|
|
|
|
|
|
template
|
|
|
|
void
|
2015-02-03 23:59:32 +03:00
|
|
|
FetchBody<Response>::SetMimeType();
|
2015-09-17 15:10:42 +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:54 +03:00
|
|
|
{
|
2017-08-11 04:04:54 +03:00
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
|
|
|
DerivedClass()->GetBody(getter_AddRefs(inputStream));
|
|
|
|
|
|
|
|
if (!inputStream) {
|
|
|
|
aBodyOut.set(nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mReadableStreamBody) {
|
|
|
|
JS::Rooted<JSObject*> body(aCx,
|
|
|
|
FetchStream::Create(aCx,
|
|
|
|
DerivedClass()->GetParentObject(),
|
|
|
|
inputStream,
|
|
|
|
aRv));
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(body);
|
|
|
|
|
|
|
|
// If the body has been already consumed, we close the stream.
|
|
|
|
if (BodyUsed() && !JS::ReadableStreamClose(aCx, body)) {
|
|
|
|
aRv.StealExceptionFromJSContext(aCx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mReadableStreamBody = body;
|
|
|
|
}
|
|
|
|
|
|
|
|
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(JSContext* aCx,
|
2017-08-11 04:04:54 +03:00
|
|
|
JS::MutableHandle<JSObject*> aMessage,
|
|
|
|
ErrorResult& aRv);
|
2017-08-11 04:04:54 +03:00
|
|
|
|
|
|
|
|
2014-09-24 09:03:20 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|