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"
|
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"
|
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-20 09:27:02 +03:00
|
|
|
: 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-20 09:27:02 +03:00
|
|
|
: 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2014-12-10 11:51:59 +03:00
|
|
|
/*
|
|
|
|
* Called on successfully reading the complete stream.
|
|
|
|
*/
|
|
|
|
template <class Derived>
|
2016-06-08 08:12:05 +03:00
|
|
|
class ContinueConsumeBodyRunnable final : public MainThreadWorkerRunnable
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
2017-06-20 18:53:21 +03:00
|
|
|
RefPtr<FetchBodyConsumer<Derived>> mFetchBodyConsumer;
|
2014-12-10 11:51:59 +03:00
|
|
|
nsresult mStatus;
|
|
|
|
uint32_t mLength;
|
|
|
|
uint8_t* mResult;
|
|
|
|
|
|
|
|
public:
|
2017-06-20 18:53:21 +03:00
|
|
|
ContinueConsumeBodyRunnable(FetchBodyConsumer<Derived>* aFetchBodyConsumer,
|
2017-06-20 18:53:20 +03:00
|
|
|
nsresult aStatus, uint32_t aLength,
|
|
|
|
uint8_t* aResult)
|
2017-06-20 18:53:21 +03:00
|
|
|
: MainThreadWorkerRunnable(aFetchBodyConsumer->Body()->mWorkerPrivate)
|
|
|
|
, mFetchBodyConsumer(aFetchBodyConsumer)
|
2014-12-10 11:51:59 +03:00
|
|
|
, mStatus(aStatus)
|
|
|
|
, mLength(aLength)
|
|
|
|
, mResult(aResult)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-09-27 03:41:15 +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-06-20 18:53:21 +03:00
|
|
|
mFetchBodyConsumer->Body()->ContinueConsumeBody(mFetchBodyConsumer, mStatus,
|
|
|
|
mLength, mResult);
|
2014-12-10 11:51:59 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-26 10:07:25 +03:00
|
|
|
/*
|
|
|
|
* Called on successfully reading the complete stream for Blob.
|
|
|
|
*/
|
|
|
|
template <class Derived>
|
|
|
|
class ContinueConsumeBlobBodyRunnable final : public MainThreadWorkerRunnable
|
|
|
|
{
|
2017-06-20 18:53:21 +03:00
|
|
|
RefPtr<FetchBodyConsumer<Derived>> mFetchBodyConsumer;
|
2016-10-26 10:07:25 +03:00
|
|
|
RefPtr<BlobImpl> mBlobImpl;
|
|
|
|
|
|
|
|
public:
|
2017-06-20 18:53:21 +03:00
|
|
|
ContinueConsumeBlobBodyRunnable(FetchBodyConsumer<Derived>* aFetchBodyConsumer,
|
2016-10-26 10:07:25 +03:00
|
|
|
BlobImpl* aBlobImpl)
|
2017-06-20 18:53:21 +03:00
|
|
|
: MainThreadWorkerRunnable(aFetchBodyConsumer->Body()->mWorkerPrivate)
|
|
|
|
, mFetchBodyConsumer(aFetchBodyConsumer)
|
2016-10-26 10:07:25 +03:00
|
|
|
, mBlobImpl(aBlobImpl)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mBlobImpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
|
|
{
|
2017-06-20 18:53:21 +03:00
|
|
|
mFetchBodyConsumer->Body()->ContinueConsumeBlobBody(mFetchBodyConsumer,
|
|
|
|
mBlobImpl);
|
2016-10-26 10:07:25 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
template <class Derived>
|
|
|
|
class FailConsumeBodyWorkerRunnable : public MainThreadWorkerControlRunnable
|
|
|
|
{
|
2017-06-20 18:53:21 +03:00
|
|
|
RefPtr<FetchBodyConsumer<Derived>> mBodyConsumer;
|
2017-06-20 18:53:20 +03:00
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
public:
|
2017-06-20 18:53:21 +03:00
|
|
|
explicit FailConsumeBodyWorkerRunnable(FetchBodyConsumer<Derived>* aBodyConsumer)
|
|
|
|
: MainThreadWorkerControlRunnable(aBodyConsumer->Body()->mWorkerPrivate)
|
|
|
|
, mBodyConsumer(aBodyConsumer)
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
2017-06-20 18:53:21 +03:00
|
|
|
mBodyConsumer->Body()->ContinueConsumeBody(mBodyConsumer, NS_ERROR_FAILURE,
|
|
|
|
0, nullptr);
|
2014-12-10 11:51:59 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In case of failure to create a stream pump or dispatch stream completion to
|
|
|
|
* worker, ensure we cleanup properly. Thread agnostic.
|
|
|
|
*/
|
|
|
|
template <class Derived>
|
2015-03-21 19:28:04 +03:00
|
|
|
class MOZ_STACK_CLASS AutoFailConsumeBody final
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
2017-06-20 18:53:21 +03:00
|
|
|
RefPtr<FetchBodyConsumer<Derived>> mBodyConsumer;
|
2017-06-20 18:53:20 +03:00
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
public:
|
2017-06-20 18:53:21 +03:00
|
|
|
explicit AutoFailConsumeBody(FetchBodyConsumer<Derived>* aBodyConsumer)
|
|
|
|
: mBodyConsumer(aBodyConsumer)
|
2017-06-20 18:53:20 +03:00
|
|
|
{}
|
2014-12-10 11:51:59 +03:00
|
|
|
|
|
|
|
~AutoFailConsumeBody()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2017-06-20 18:53:20 +03:00
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
if (mBodyConsumer) {
|
|
|
|
if (mBodyConsumer->Body()->mWorkerPrivate) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<FailConsumeBodyWorkerRunnable<Derived>> r =
|
2017-06-20 18:53:21 +03:00
|
|
|
new FailConsumeBodyWorkerRunnable<Derived>(mBodyConsumer);
|
2016-02-26 23:23:12 +03:00
|
|
|
if (!r->Dispatch()) {
|
2014-12-10 11:51:59 +03:00
|
|
|
MOZ_CRASH("We are going to leak");
|
|
|
|
}
|
|
|
|
} else {
|
2017-06-20 18:53:21 +03:00
|
|
|
mBodyConsumer->Body()->ContinueConsumeBody(mBodyConsumer,
|
|
|
|
NS_ERROR_FAILURE, 0,
|
|
|
|
nullptr);
|
2014-12-10 11:51:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DontFail()
|
|
|
|
{
|
2017-06-20 18:53:21 +03:00
|
|
|
mBodyConsumer = nullptr;
|
2014-12-10 11:51:59 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
class ConsumeBodyDoneObserver : public nsIStreamLoaderObserver
|
2016-10-26 10:07:25 +03:00
|
|
|
, public MutableBlobStorageCallback
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
2017-06-20 18:53:21 +03:00
|
|
|
RefPtr<FetchBodyConsumer<Derived>> mFetchBodyConsumer;
|
2014-12-10 11:51:59 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
explicit ConsumeBodyDoneObserver(FetchBodyConsumer<Derived>* aFetchBodyConsumer)
|
|
|
|
: mFetchBodyConsumer(aFetchBodyConsumer)
|
2014-12-10 11:51:59 +03:00
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
OnStreamComplete(nsIStreamLoader* aLoader,
|
|
|
|
nsISupports* aCtxt,
|
|
|
|
nsresult aStatus,
|
|
|
|
uint32_t aResultLength,
|
2015-03-21 19:28:04 +03:00
|
|
|
const uint8_t* aResult) override
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
// If the binding requested cancel, we don't need to call
|
|
|
|
// ContinueConsumeBody, since that is the originator.
|
|
|
|
if (aStatus == NS_BINDING_ABORTED) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* nonconstResult = const_cast<uint8_t*>(aResult);
|
2017-06-20 18:53:21 +03:00
|
|
|
if (mFetchBodyConsumer->Body()->mWorkerPrivate) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ContinueConsumeBodyRunnable<Derived>> r =
|
2017-06-20 18:53:21 +03:00
|
|
|
new ContinueConsumeBodyRunnable<Derived>(mFetchBodyConsumer,
|
2017-06-20 18:53:20 +03:00
|
|
|
aStatus,
|
|
|
|
aResultLength,
|
|
|
|
nonconstResult);
|
2016-02-26 23:23:12 +03:00
|
|
|
if (!r->Dispatch()) {
|
2015-10-01 02:11:04 +03:00
|
|
|
// XXXcatalinb: The worker is shutting down, the pump will be canceled
|
2016-06-23 11:53:14 +03:00
|
|
|
// by FetchBodyWorkerHolder::Notify.
|
2014-12-10 11:51:59 +03:00
|
|
|
NS_WARNING("Could not dispatch ConsumeBodyRunnable");
|
|
|
|
// Return failure so that aResult is freed.
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
} else {
|
2017-06-20 18:53:21 +03:00
|
|
|
mFetchBodyConsumer->Body()->ContinueConsumeBody(mFetchBodyConsumer,
|
|
|
|
aStatus, aResultLength,
|
|
|
|
nonconstResult);
|
2014-12-10 11:51:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// FetchBody is responsible for data.
|
|
|
|
return NS_SUCCESS_ADOPTED_DATA;
|
|
|
|
}
|
|
|
|
|
2016-10-26 10:07:25 +03:00
|
|
|
virtual void BlobStoreCompleted(MutableBlobStorage* aBlobStorage,
|
|
|
|
Blob* aBlob,
|
|
|
|
nsresult aRv) override
|
|
|
|
{
|
|
|
|
// On error.
|
|
|
|
if (NS_FAILED(aRv)) {
|
|
|
|
OnStreamComplete(nullptr, nullptr, aRv, 0, nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aBlob);
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
if (mFetchBodyConsumer->Body()->mWorkerPrivate) {
|
2016-10-26 10:07:25 +03:00
|
|
|
RefPtr<ContinueConsumeBlobBodyRunnable<Derived>> r =
|
2017-06-20 18:53:21 +03:00
|
|
|
new ContinueConsumeBlobBodyRunnable<Derived>(mFetchBodyConsumer,
|
2017-06-20 18:53:20 +03:00
|
|
|
aBlob->Impl());
|
2016-10-26 10:07:25 +03:00
|
|
|
|
|
|
|
if (!r->Dispatch()) {
|
|
|
|
NS_WARNING("Could not dispatch ConsumeBlobBodyRunnable");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2017-06-20 18:53:21 +03:00
|
|
|
mFetchBodyConsumer->Body()->ContinueConsumeBlobBody(mFetchBodyConsumer,
|
|
|
|
aBlob->Impl());
|
2016-10-26 10:07:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
private:
|
|
|
|
virtual ~ConsumeBodyDoneObserver()
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
NS_IMPL_ADDREF(ConsumeBodyDoneObserver<Derived>)
|
|
|
|
template <class Derived>
|
|
|
|
NS_IMPL_RELEASE(ConsumeBodyDoneObserver<Derived>)
|
|
|
|
template <class Derived>
|
|
|
|
NS_INTERFACE_MAP_BEGIN(ConsumeBodyDoneObserver<Derived>)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStreamLoaderObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIStreamLoaderObserver)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
template <class Derived>
|
2016-04-26 03:23:21 +03:00
|
|
|
class BeginConsumeBodyRunnable final : public Runnable
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
2017-06-20 18:53:21 +03:00
|
|
|
RefPtr<FetchBodyConsumer<Derived>> mFetchBodyConsumer;
|
2017-06-20 18:53:20 +03:00
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
public:
|
2017-06-20 18:53:21 +03:00
|
|
|
explicit BeginConsumeBodyRunnable(FetchBodyConsumer<Derived>* aConsumer)
|
|
|
|
: mFetchBodyConsumer(aConsumer)
|
2014-12-10 11:51:59 +03:00
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD
|
2015-03-21 19:28:04 +03:00
|
|
|
Run() override
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
2017-06-20 18:53:21 +03:00
|
|
|
mFetchBodyConsumer->Body()->BeginConsumeBodyMainThread(mFetchBodyConsumer);
|
2014-12-10 11:51:59 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Derived>
|
2015-03-21 19:28:04 +03:00
|
|
|
class CancelPumpRunnable final : public WorkerMainThreadRunnable
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
2017-06-20 18:53:20 +03:00
|
|
|
// This is a sync runnable. What dispatches this runnable must keep the body
|
|
|
|
// alive.
|
2014-12-10 11:51:59 +03:00
|
|
|
FetchBody<Derived>* mBody;
|
2017-06-20 18:53:20 +03:00
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
public:
|
|
|
|
explicit CancelPumpRunnable(FetchBody<Derived>* aBody)
|
2016-05-03 10:09:47 +03:00
|
|
|
: WorkerMainThreadRunnable(aBody->mWorkerPrivate,
|
|
|
|
NS_LITERAL_CSTRING("Fetch :: Cancel Pump"))
|
2014-12-10 11:51:59 +03:00
|
|
|
, mBody(aBody)
|
2017-06-20 18:53:20 +03:00
|
|
|
{}
|
2014-12-10 11:51:59 +03:00
|
|
|
|
|
|
|
bool
|
2015-03-21 19:28:04 +03:00
|
|
|
MainThreadRun() override
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
|
|
|
mBody->CancelPump();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-20 18:53:20 +03:00
|
|
|
} // namespace
|
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)
|
2014-12-10 11:51:59 +03:00
|
|
|
, mBodyUsed(false)
|
2016-02-26 18:52:06 +03:00
|
|
|
#ifdef DEBUG
|
2014-12-10 11:51:59 +03:00
|
|
|
, mReadDone(false)
|
2016-02-26 18:52:06 +03:00
|
|
|
#endif
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
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 {
|
|
|
|
mWorkerPrivate = nullptr;
|
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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
FetchBody<Derived>::CancelPump()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(mConsumeBodyPump);
|
|
|
|
mConsumeBodyPump->Cancel(NS_BINDING_ABORTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return value is used by ConsumeBody to bubble the error code up to WebIDL so
|
|
|
|
// mConsumePromise doesn't have to be rejected on early exit.
|
|
|
|
template <class Derived>
|
|
|
|
nsresult
|
|
|
|
FetchBody<Derived>::BeginConsumeBody()
|
|
|
|
{
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
MOZ_ASSERT(mConsumePromise);
|
|
|
|
|
2017-06-20 18:53:20 +03:00
|
|
|
// The FetchBody is not thread-safe refcounted. We wrap it with a thread-safe
|
|
|
|
// object able to keep the current worker alive (if we are running in a
|
|
|
|
// worker).
|
2017-06-20 18:53:21 +03:00
|
|
|
RefPtr<FetchBodyConsumer<Derived>> consumer =
|
|
|
|
FetchBodyConsumer<Derived>::Create(this);
|
|
|
|
if (!consumer) {
|
2014-12-10 11:51:59 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r = new BeginConsumeBodyRunnable<Derived>(consumer);
|
2017-06-14 16:53:04 +03:00
|
|
|
nsresult rv = mMainThreadEventTarget->Dispatch(r.forget(), NS_DISPATCH_NORMAL);
|
2014-12-24 08:47:17 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2014-12-10 11:51:59 +03:00
|
|
|
}
|
2014-12-24 08:47:17 +03:00
|
|
|
return NS_OK;
|
2014-12-10 11:51:59 +03:00
|
|
|
}
|
2014-09-27 03:41:15 +04:00
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
/*
|
|
|
|
* BeginConsumeBodyMainThread() will automatically reject the consume promise
|
|
|
|
* and clean up on any failures, so there is no need for callers to do so,
|
|
|
|
* reflected in a lack of error return code.
|
|
|
|
*/
|
|
|
|
template <class Derived>
|
|
|
|
void
|
2017-06-20 18:53:21 +03:00
|
|
|
FetchBody<Derived>::BeginConsumeBodyMainThread(FetchBodyConsumer<Derived>* aConsumer)
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2017-06-20 18:53:20 +03:00
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
AutoFailConsumeBody<Derived> autoReject(aConsumer);
|
2017-06-20 18:53:20 +03:00
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
nsresult rv;
|
2014-09-27 03:41:15 +04:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
DerivedClass()->GetBody(getter_AddRefs(stream));
|
|
|
|
if (!stream) {
|
2015-01-08 02:50:54 +03:00
|
|
|
rv = NS_NewCStringInputStream(getter_AddRefs(stream), EmptyCString());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
nsCOMPtr<nsIInputStreamPump> pump;
|
|
|
|
rv = NS_NewInputStreamPump(getter_AddRefs(pump),
|
2017-05-16 13:12:24 +03:00
|
|
|
stream, -1, -1, 0, 0, false,
|
|
|
|
mMainThreadEventTarget);
|
2014-12-10 11:51:59 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-24 09:03:20 +04:00
|
|
|
|
2017-06-20 18:53:20 +03:00
|
|
|
RefPtr<ConsumeBodyDoneObserver<Derived>> p =
|
2017-06-20 18:53:21 +03:00
|
|
|
new ConsumeBodyDoneObserver<Derived>(aConsumer);
|
2016-10-26 10:07:25 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamListener> listener;
|
|
|
|
if (mConsumeType == CONSUME_BLOB) {
|
|
|
|
MutableBlobStorage::MutableBlobStorageType type =
|
|
|
|
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))) {
|
|
|
|
type = MutableBlobStorage::eCouldBeInTemporaryFile;
|
|
|
|
}
|
|
|
|
|
2017-05-16 13:12:24 +03:00
|
|
|
listener = new MutableBlobStreamListener(type, nullptr, mMimeType, p,
|
|
|
|
mMainThreadEventTarget);
|
2016-10-26 10:07:25 +03:00
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIStreamLoader> loader;
|
|
|
|
rv = NS_NewStreamLoader(getter_AddRefs(loader), p);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
listener = loader;
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
|
|
|
|
2016-10-26 10:07:25 +03:00
|
|
|
rv = pump->AsyncRead(listener, nullptr);
|
2014-12-10 11:51:59 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
// Now that everything succeeded, we can assign the pump to a pointer that
|
|
|
|
// stays alive for the lifetime of the FetchBody.
|
2017-05-16 13:12:24 +03:00
|
|
|
mConsumeBodyPump =
|
2017-06-14 04:27:17 +03:00
|
|
|
new nsMainThreadPtrHolder<nsIInputStreamPump>(
|
|
|
|
"FetchBody::mConsumeBodyPump", pump, mMainThreadEventTarget);
|
2014-12-10 11:51:59 +03:00
|
|
|
// It is ok for retargeting to fail and reads to happen on the main thread.
|
|
|
|
autoReject.DontFail();
|
|
|
|
|
|
|
|
// Try to retarget, otherwise fall back to main thread.
|
|
|
|
nsCOMPtr<nsIThreadRetargetableRequest> rr = do_QueryInterface(pump);
|
|
|
|
if (rr) {
|
|
|
|
nsCOMPtr<nsIEventTarget> sts = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
|
|
|
|
rv = rr->RetargetDeliveryTo(sts);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
NS_WARNING("Retargeting failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-27 03:41:15 +04:00
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
template <class Derived>
|
|
|
|
void
|
2017-06-20 18:53:21 +03:00
|
|
|
FetchBody<Derived>::ContinueConsumeBody(FetchBodyConsumer<Derived>* aBodyConsumer,
|
2017-06-20 18:53:20 +03:00
|
|
|
nsresult aStatus, uint32_t aResultLength,
|
|
|
|
uint8_t* aResult)
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
// Just a precaution to ensure ContinueConsumeBody is not called out of
|
|
|
|
// sync with a body read.
|
|
|
|
MOZ_ASSERT(mBodyUsed);
|
|
|
|
MOZ_ASSERT(!mReadDone);
|
2016-02-26 18:52:06 +03:00
|
|
|
#ifdef DEBUG
|
2014-12-10 11:51:59 +03:00
|
|
|
mReadDone = true;
|
2016-02-26 18:52:06 +03:00
|
|
|
#endif
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2017-06-18 12:47:45 +03:00
|
|
|
auto autoFree = mozilla::MakeScopeExit([&] {
|
|
|
|
free(aResult);
|
|
|
|
});
|
2014-12-10 11:51:59 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(mConsumePromise);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> localPromise = mConsumePromise.forget();
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2017-06-20 18:53:20 +03:00
|
|
|
auto autoReleaseObject = mozilla::MakeScopeExit([&] {
|
2017-06-20 18:53:21 +03:00
|
|
|
aBodyConsumer->ReleaseObject();
|
2017-06-20 18:53:20 +03:00
|
|
|
});
|
2014-12-10 11:51:59 +03:00
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(aStatus))) {
|
|
|
|
localPromise->MaybeReject(NS_ERROR_DOM_ABORT_ERR);
|
|
|
|
|
|
|
|
// If binding aborted, cancel the pump. We can't assert mConsumeBodyPump.
|
|
|
|
// In the (admittedly rare) situation that BeginConsumeBodyMainThread()
|
|
|
|
// context switches out, and the worker thread gets canceled before the
|
|
|
|
// pump is setup, mConsumeBodyPump will be null.
|
|
|
|
// We've to use the !! form since non-main thread pointer access on
|
|
|
|
// a nsMainThreadPtrHandle is not permitted.
|
|
|
|
if (aStatus == NS_BINDING_ABORTED && !!mConsumeBodyPump) {
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
CancelPump();
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
// In case of worker thread, we block the worker while the request is
|
|
|
|
// canceled on the main thread. This ensures that OnStreamComplete has
|
|
|
|
// a valid FetchBody around to call CancelPump and we don't release the
|
|
|
|
// FetchBody on the main thread.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CancelPumpRunnable<Derived>> r =
|
2017-06-20 18:53:21 +03:00
|
|
|
new CancelPumpRunnable<Derived>(aBodyConsumer->Body());
|
2015-11-24 08:04:21 +03:00
|
|
|
ErrorResult rv;
|
2017-01-05 12:05:32 +03:00
|
|
|
r->Dispatch(Terminating, rv);
|
2015-11-24 08:04:21 +03:00
|
|
|
if (rv.Failed()) {
|
2014-12-10 11:51:59 +03:00
|
|
|
NS_WARNING("Could not dispatch CancelPumpRunnable. Nothing we can do here");
|
2015-11-24 08:04:21 +03:00
|
|
|
// None of our callers are callled directly from JS, so there is no
|
|
|
|
// point in trying to propagate this failure out of here. And
|
|
|
|
// localPromise is already rejected. Just suppress the failure.
|
|
|
|
rv.SuppressException();
|
2014-12-10 11:51:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release the pump and then early exit if there was an error.
|
|
|
|
// Uses NS_ProxyRelease internally, so this is safe.
|
|
|
|
mConsumeBodyPump = nullptr;
|
|
|
|
|
|
|
|
// Don't warn here since we warned above.
|
|
|
|
if (NS_FAILED(aStatus)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finish successfully consuming body according to type.
|
|
|
|
MOZ_ASSERT(aResult);
|
|
|
|
|
2014-12-05 01:06:45 +03:00
|
|
|
AutoJSAPI jsapi;
|
2017-06-20 18:53:21 +03:00
|
|
|
if (!jsapi.Init(aBodyConsumer->Body()->DerivedClass()->GetParentObject())) {
|
2016-07-14 06:18:11 +03:00
|
|
|
localPromise->MaybeReject(NS_ERROR_UNEXPECTED);
|
|
|
|
return;
|
|
|
|
}
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2016-07-14 06:18:11 +03:00
|
|
|
JSContext* cx = jsapi.cx();
|
2015-09-17 15:10:42 +03:00
|
|
|
ErrorResult error;
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
switch (mConsumeType) {
|
2014-09-27 03:41:15 +04:00
|
|
|
case CONSUME_ARRAYBUFFER: {
|
|
|
|
JS::Rooted<JSObject*> arrayBuffer(cx);
|
2016-02-28 00:54:11 +03:00
|
|
|
BodyUtil::ConsumeArrayBuffer(cx, &arrayBuffer, aResultLength, aResult,
|
2017-06-14 16:52:46 +03:00
|
|
|
error);
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2015-09-17 15:10:42 +03:00
|
|
|
if (!error.Failed()) {
|
|
|
|
JS::Rooted<JS::Value> val(cx);
|
|
|
|
val.setObjectOrNull(arrayBuffer);
|
|
|
|
|
|
|
|
localPromise->MaybeResolve(cx, val);
|
|
|
|
// ArrayBuffer takes over ownership.
|
2017-06-18 12:47:45 +03:00
|
|
|
autoFree.release();
|
2015-09-17 15:10:42 +03:00
|
|
|
}
|
|
|
|
break;
|
2014-09-24 09:03:20 +04:00
|
|
|
}
|
2014-09-27 03:41:15 +04:00
|
|
|
case CONSUME_BLOB: {
|
2016-10-26 10:07:25 +03:00
|
|
|
MOZ_CRASH("This should not happen.");
|
2015-09-17 15:10:42 +03:00
|
|
|
break;
|
2014-09-24 09:03:20 +04:00
|
|
|
}
|
2015-04-04 08:55:15 +03:00
|
|
|
case CONSUME_FORMDATA: {
|
|
|
|
nsCString data;
|
|
|
|
data.Adopt(reinterpret_cast<char*>(aResult), aResultLength);
|
2017-06-18 12:47:45 +03:00
|
|
|
autoFree.release();
|
2015-04-04 08:55:15 +03:00
|
|
|
|
2016-02-28 00:54:11 +03:00
|
|
|
RefPtr<dom::FormData> fd = BodyUtil::ConsumeFormData(
|
2017-06-20 18:53:21 +03:00
|
|
|
aBodyConsumer->Body()->DerivedClass()->GetParentObject(),
|
2015-09-17 15:10:42 +03:00
|
|
|
mMimeType, data, error);
|
|
|
|
if (!error.Failed()) {
|
2015-04-04 08:55:15 +03:00
|
|
|
localPromise->MaybeResolve(fd);
|
|
|
|
}
|
2015-09-17 15:10:42 +03:00
|
|
|
break;
|
2015-04-04 08:55:15 +03:00
|
|
|
}
|
2014-12-10 11:51:59 +03:00
|
|
|
case CONSUME_TEXT:
|
|
|
|
// fall through handles early exit.
|
2014-09-27 03:41:15 +04:00
|
|
|
case CONSUME_JSON: {
|
2015-09-17 15:10:42 +03:00
|
|
|
nsString decoded;
|
2016-02-28 00:54:11 +03:00
|
|
|
if (NS_SUCCEEDED(BodyUtil::ConsumeText(aResultLength, aResult, decoded))) {
|
2015-09-17 15:10:42 +03:00
|
|
|
if (mConsumeType == CONSUME_TEXT) {
|
|
|
|
localPromise->MaybeResolve(decoded);
|
|
|
|
} else {
|
|
|
|
JS::Rooted<JS::Value> json(cx);
|
2016-02-28 00:54:11 +03:00
|
|
|
BodyUtil::ConsumeJson(cx, &json, decoded, error);
|
2015-09-17 15:10:42 +03:00
|
|
|
if (!error.Failed()) {
|
|
|
|
localPromise->MaybeResolve(cx, json);
|
|
|
|
}
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
2015-09-17 15:10:42 +03:00
|
|
|
};
|
|
|
|
break;
|
2014-09-24 09:03:20 +04:00
|
|
|
}
|
2015-09-17 15:10:42 +03:00
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unexpected consume body type");
|
2014-09-24 09:03:20 +04:00
|
|
|
}
|
|
|
|
|
2015-09-17 15:10:42 +03:00
|
|
|
error.WouldReportJSException();
|
|
|
|
if (error.Failed()) {
|
2015-11-21 00:29:41 +03:00
|
|
|
localPromise->MaybeReject(error);
|
2015-09-17 15:10:42 +03:00
|
|
|
}
|
2014-12-10 11:51:59 +03:00
|
|
|
}
|
|
|
|
|
2016-10-26 10:07:25 +03:00
|
|
|
template <class Derived>
|
|
|
|
void
|
2017-06-20 18:53:21 +03:00
|
|
|
FetchBody<Derived>::ContinueConsumeBlobBody(FetchBodyConsumer<Derived>* aBodyConsumer,
|
2017-06-20 18:53:20 +03:00
|
|
|
BlobImpl* aBlobImpl)
|
2016-10-26 10:07:25 +03:00
|
|
|
{
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
// Just a precaution to ensure ContinueConsumeBody is not called out of
|
|
|
|
// sync with a body read.
|
|
|
|
MOZ_ASSERT(mBodyUsed);
|
|
|
|
MOZ_ASSERT(!mReadDone);
|
|
|
|
MOZ_ASSERT(mConsumeType == CONSUME_BLOB);
|
|
|
|
#ifdef DEBUG
|
|
|
|
mReadDone = true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
MOZ_ASSERT(mConsumePromise);
|
|
|
|
RefPtr<Promise> localPromise = mConsumePromise.forget();
|
|
|
|
|
2017-06-20 18:53:20 +03:00
|
|
|
auto autoReleaseObject = mozilla::MakeScopeExit([&] {
|
2017-06-20 18:53:21 +03:00
|
|
|
aBodyConsumer->ReleaseObject();
|
2017-06-20 18:53:20 +03:00
|
|
|
});
|
2016-10-26 10:07:25 +03:00
|
|
|
|
|
|
|
// Release the pump and then early exit if there was an error.
|
|
|
|
// Uses NS_ProxyRelease internally, so this is safe.
|
|
|
|
mConsumeBodyPump = nullptr;
|
|
|
|
|
|
|
|
RefPtr<dom::Blob> blob =
|
2017-06-20 18:53:21 +03:00
|
|
|
dom::Blob::Create(aBodyConsumer->Body()->DerivedClass()->GetParentObject(),
|
2017-06-20 18:53:20 +03:00
|
|
|
aBlobImpl);
|
2016-10-26 10:07:25 +03:00
|
|
|
MOZ_ASSERT(blob);
|
|
|
|
|
|
|
|
localPromise->MaybeResolve(blob);
|
|
|
|
}
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
template <class Derived>
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
FetchBody<Derived>::ConsumeBody(ConsumeType aType, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-06-14 16:52:46 +03:00
|
|
|
mConsumeType = aType;
|
2014-12-10 11:51:59 +03:00
|
|
|
SetBodyUsed();
|
|
|
|
|
|
|
|
mConsumePromise = Promise::Create(DerivedClass()->GetParentObject(), aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
aRv = BeginConsumeBody();
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
mConsumePromise = nullptr;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> promise = mConsumePromise;
|
2014-12-10 11:51:59 +03:00
|
|
|
return promise.forget();
|
2014-09-24 09:03:20 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
template
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
FetchBody<Request>::ConsumeBody(ConsumeType aType, ErrorResult& aRv);
|
|
|
|
|
|
|
|
template
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
FetchBody<Response>::ConsumeBody(ConsumeType aType, ErrorResult& aRv);
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-09-24 09:03:20 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|