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"
|
|
|
|
|
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-27 03:41:15 +04:00
|
|
|
#include "nsIUnicodeDecoder.h"
|
2014-09-24 09:03:20 +04:00
|
|
|
#include "nsIUnicodeEncoder.h"
|
|
|
|
|
2015-04-04 08:55:15 +03:00
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2014-09-27 03:41:15 +04:00
|
|
|
#include "nsDOMString.h"
|
|
|
|
#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"
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2016-02-28 00:54:11 +03:00
|
|
|
#include "mozilla/dom/BodyUtil.h"
|
2014-09-24 09:03:20 +04:00
|
|
|
#include "mozilla/dom/EncodingUtils.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
|
|
|
|
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;
|
|
|
|
|
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;
|
|
|
|
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;
|
2014-10-06 22:01:20 +04:00
|
|
|
public:
|
2015-09-03 01:04:17 +03:00
|
|
|
// Returns null if worker is shutting down.
|
|
|
|
static already_AddRefed<WorkerFetchResolver>
|
|
|
|
Create(workers::WorkerPrivate* aWorkerPrivate, Promise* aPromise)
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
2015-09-03 01:04:17 +03:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> proxy = PromiseWorkerProxy::Create(aWorkerPrivate, aPromise);
|
2015-09-03 01:04:17 +03:00
|
|
|
if (!proxy) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerFetchResolver> r = new WorkerFetchResolver(proxy);
|
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
|
|
|
|
|
|
|
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
|
2015-03-21 19:28:04 +03:00
|
|
|
OnResponseEnd() override;
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2015-09-03 01:04:17 +03:00
|
|
|
private:
|
|
|
|
explicit WorkerFetchResolver(PromiseWorkerProxy* aProxy)
|
|
|
|
: mPromiseProxy(aProxy)
|
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;
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2016-09-07 05:20:23 +03:00
|
|
|
nsCOMPtr<nsIDocument> mDocument;
|
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
NS_DECL_OWNINGTHREAD
|
|
|
|
public:
|
2014-10-11 01:28:35 +04:00
|
|
|
explicit MainThreadFetchResolver(Promise* aPromise);
|
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
|
|
|
|
2016-09-07 05:20:23 +03:00
|
|
|
void SetDocument(nsIDocument* aDocument)
|
|
|
|
{
|
|
|
|
mDocument = aDocument;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnResponseEnd() override
|
|
|
|
{
|
|
|
|
FlushConsoleReport();
|
|
|
|
}
|
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
private:
|
|
|
|
~MainThreadFetchResolver();
|
2016-09-07 05:20:23 +03:00
|
|
|
|
|
|
|
void FlushConsoleReport() override
|
|
|
|
{
|
|
|
|
mReporter->FlushConsoleReports(mDocument);
|
|
|
|
}
|
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)
|
2015-09-03 01:04:17 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = proxy->GetWorkerPrivate()->GetPrincipal();
|
|
|
|
MOZ_ASSERT(principal);
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = proxy->GetWorkerPrivate()->GetLoadGroup();
|
|
|
|
MOZ_ASSERT(loadGroup);
|
|
|
|
fetch = new FetchDriver(mRequest, principal, loadGroup);
|
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
|
|
|
|
|
|
|
// ...but release it before calling Fetch, because mResolver's callback can
|
|
|
|
// be called synchronously and they want the mutex, too.
|
2016-10-26 10:07:25 +03:00
|
|
|
return fetch->Fetch(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,
|
2014-10-06 22:01:20 +04:00
|
|
|
const RequestInit& aInit, ErrorResult& aRv)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> p = Promise::Create(aGlobal, aRv);
|
2014-10-06 22:01:20 +04:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-04-08 00:13:09 +03:00
|
|
|
// Double check that we have chrome privileges if the Request's content
|
|
|
|
// policy type has been overridden. Note, we must do this before
|
|
|
|
// entering the global below. Otherwise the IsCallerChrome() will
|
|
|
|
// always fail.
|
|
|
|
MOZ_ASSERT_IF(aInput.IsRequest() &&
|
|
|
|
aInput.GetAsRequest().IsContentPolicyTypeOverridden(),
|
|
|
|
nsContentUtils::IsCallerChrome());
|
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
if (window) {
|
|
|
|
doc = window->GetExtantDoc();
|
|
|
|
if (!doc) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
principal = doc->NodePrincipal();
|
|
|
|
loadGroup = doc->GetDocumentLoadGroup();
|
|
|
|
} 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);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MainThreadFetchResolver> resolver = new MainThreadFetchResolver(p);
|
|
|
|
RefPtr<FetchDriver> fetch = new FetchDriver(r, principal, loadGroup);
|
2014-10-15 03:36:58 +04:00
|
|
|
fetch->SetDocument(doc);
|
2016-09-07 05:20:23 +03:00
|
|
|
resolver->SetDocument(doc);
|
2014-10-06 22:01:20 +04:00
|
|
|
aRv = fetch->Fetch(resolver);
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerFetchResolver> resolver = WorkerFetchResolver::Create(worker, p);
|
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
|
|
|
|
2015-10-18 08:24:48 +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();
|
|
|
|
}
|
|
|
|
|
|
|
|
MainThreadFetchResolver::MainThreadFetchResolver(Promise* aPromise)
|
|
|
|
: mPromise(aPromise)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
nsCOMPtr<nsIGlobalObject> go = mPromise->GetParentObject();
|
|
|
|
mResponse = new Response(go, aResponse);
|
|
|
|
mPromise->MaybeResolve(mResponse);
|
|
|
|
} else {
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
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 {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-05 23:02:27 +03:00
|
|
|
class WorkerFetchResponseEndBase
|
|
|
|
{
|
|
|
|
RefPtr<PromiseWorkerProxy> mPromiseProxy;
|
|
|
|
public:
|
|
|
|
explicit WorkerFetchResponseEndBase(PromiseWorkerProxy* aPromiseProxy)
|
|
|
|
: mPromiseProxy(aPromiseProxy)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mPromiseProxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerRunInternal(WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
mPromiseProxy->CleanUp();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
{
|
2014-12-10 11:51:59 +03:00
|
|
|
public:
|
2016-05-05 23:02:27 +03:00
|
|
|
explicit WorkerFetchResponseEndRunnable(PromiseWorkerProxy* aPromiseProxy)
|
2016-06-08 08:12:05 +03:00
|
|
|
: MainThreadWorkerRunnable(aPromiseProxy->GetWorkerPrivate())
|
2016-05-05 23:02:27 +03:00
|
|
|
, WorkerFetchResponseEndBase(aPromiseProxy)
|
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
|
|
|
{
|
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:
|
|
|
|
explicit WorkerFetchResponseEndControlRunnable(PromiseWorkerProxy* aPromiseProxy)
|
2016-05-19 21:39:13 +03:00
|
|
|
: MainThreadWorkerControlRunnable(aPromiseProxy->GetWorkerPrivate())
|
2016-05-05 23:02:27 +03:00
|
|
|
, WorkerFetchResponseEndBase(aPromiseProxy)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
void
|
|
|
|
WorkerFetchResolver::OnResponseEnd()
|
|
|
|
{
|
|
|
|
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 =
|
2016-05-05 23:02:27 +03:00
|
|
|
new WorkerFetchResponseEndRunnable(mPromiseProxy);
|
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 =
|
|
|
|
new WorkerFetchResponseEndControlRunnable(mPromiseProxy);
|
|
|
|
// 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) {
|
|
|
|
mReporter->FlushConsoleReports((nsIDocument*)nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (worker->IsServiceWorker()) {
|
|
|
|
// Flush to service worker
|
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
if (!swm) {
|
|
|
|
mReporter->FlushConsoleReports((nsIDocument*)nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
swm->FlushReportsToAllClients(worker->WorkerName(), mReporter);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (worker->IsSharedWorker()) {
|
|
|
|
// Flush to shared worker
|
|
|
|
worker->FlushReportsToSharedWorkers(mReporter);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush to dedicated worker
|
|
|
|
mReporter->FlushConsoleReports(worker->GetDocument());
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
namespace {
|
|
|
|
nsresult
|
|
|
|
ExtractFromArrayBuffer(const ArrayBuffer& aBuffer,
|
2016-06-02 00:02:29 +03:00
|
|
|
nsIInputStream** aStream,
|
|
|
|
uint64_t& aContentLength)
|
2014-09-27 03:41:15 +04:00
|
|
|
{
|
|
|
|
aBuffer.ComputeLengthAndData();
|
2016-06-02 00:02:29 +03:00
|
|
|
aContentLength = aBuffer.Length();
|
2014-09-27 03:41:15 +04:00
|
|
|
//XXXnsm reinterpret_cast<> is used in DOMParser, should be ok.
|
|
|
|
return NS_NewByteInputStream(aStream,
|
|
|
|
reinterpret_cast<char*>(aBuffer.Data()),
|
|
|
|
aBuffer.Length(), NS_ASSIGNMENT_COPY);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ExtractFromArrayBufferView(const ArrayBufferView& aBuffer,
|
2016-06-02 00:02:29 +03:00
|
|
|
nsIInputStream** aStream,
|
|
|
|
uint64_t& aContentLength)
|
2014-09-27 03:41:15 +04:00
|
|
|
{
|
|
|
|
aBuffer.ComputeLengthAndData();
|
2016-06-02 00:02:29 +03:00
|
|
|
aContentLength = aBuffer.Length();
|
2014-09-27 03:41:15 +04:00
|
|
|
//XXXnsm reinterpret_cast<> is used in DOMParser, should be ok.
|
|
|
|
return NS_NewByteInputStream(aStream,
|
|
|
|
reinterpret_cast<char*>(aBuffer.Data()),
|
|
|
|
aBuffer.Length(), NS_ASSIGNMENT_COPY);
|
|
|
|
}
|
|
|
|
|
2014-10-09 21:52:10 +04:00
|
|
|
nsresult
|
2016-06-02 00:02:29 +03:00
|
|
|
ExtractFromBlob(const Blob& aBlob,
|
|
|
|
nsIInputStream** aStream,
|
|
|
|
nsCString& aContentType,
|
|
|
|
uint64_t& aContentLength)
|
2014-10-09 21:52:10 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BlobImpl> impl = aBlob.Impl();
|
2015-05-19 17:36:37 +03:00
|
|
|
ErrorResult rv;
|
2016-06-02 00:02:29 +03:00
|
|
|
aContentLength = impl->GetSize(rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
|
|
|
|
2015-05-19 17:36:37 +03:00
|
|
|
impl->GetInternalStream(aStream, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
2014-10-09 21:52:10 +04:00
|
|
|
}
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
nsAutoString type;
|
2014-10-09 21:52:10 +04:00
|
|
|
impl->GetType(type);
|
2016-11-28 23:58:02 +03:00
|
|
|
if (!type.IsEmpty()) {
|
|
|
|
CopyUTF16toUTF8(type, aContentType);
|
|
|
|
}
|
2014-10-09 21:52:10 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-17 18:32:04 +03:00
|
|
|
nsresult
|
2016-06-02 00:02:29 +03:00
|
|
|
ExtractFromFormData(FormData& aFormData,
|
|
|
|
nsIInputStream** aStream,
|
|
|
|
nsCString& aContentType,
|
|
|
|
uint64_t& aContentLength)
|
2015-03-17 18:32:04 +03:00
|
|
|
{
|
|
|
|
nsAutoCString unusedCharset;
|
2016-06-02 00:02:29 +03:00
|
|
|
return aFormData.GetSendInfo(aStream, &aContentLength,
|
2015-03-17 18:32:04 +03:00
|
|
|
aContentType, unusedCharset);
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
nsresult
|
2014-11-20 14:58:00 +03:00
|
|
|
ExtractFromUSVString(const nsString& aStr,
|
2014-12-10 11:51:59 +03:00
|
|
|
nsIInputStream** aStream,
|
2016-06-02 00:02:29 +03:00
|
|
|
nsCString& aContentType,
|
|
|
|
uint64_t& aContentLength)
|
2014-09-27 03:41:15 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIUnicodeEncoder> encoder = EncodingUtils::EncoderForEncoding("UTF-8");
|
|
|
|
if (!encoder) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t destBufferLen;
|
|
|
|
nsresult rv = encoder->GetMaxLength(aStr.get(), aStr.Length(), &destBufferLen);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString encoded;
|
2015-01-28 12:00:40 +03:00
|
|
|
if (!encoded.SetCapacity(destBufferLen, fallible)) {
|
2014-09-27 03:41:15 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* destBuffer = encoded.BeginWriting();
|
|
|
|
int32_t srcLen = (int32_t) aStr.Length();
|
|
|
|
int32_t outLen = destBufferLen;
|
|
|
|
rv = encoder->Convert(aStr.get(), &srcLen, destBuffer, &outLen);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(outLen <= destBufferLen);
|
|
|
|
encoded.SetLength(outLen);
|
|
|
|
|
|
|
|
aContentType = NS_LITERAL_CSTRING("text/plain;charset=UTF-8");
|
2016-06-02 00:02:29 +03:00
|
|
|
aContentLength = outLen;
|
2014-09-27 03:41:15 +04:00
|
|
|
|
|
|
|
return NS_NewCStringInputStream(aStream, encoded);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ExtractFromURLSearchParams(const URLSearchParams& aParams,
|
|
|
|
nsIInputStream** aStream,
|
2016-06-02 00:02:29 +03:00
|
|
|
nsCString& aContentType,
|
|
|
|
uint64_t& aContentLength)
|
2014-09-27 03:41:15 +04:00
|
|
|
{
|
|
|
|
nsAutoString serialized;
|
|
|
|
aParams.Stringify(serialized);
|
|
|
|
aContentType = NS_LITERAL_CSTRING("application/x-www-form-urlencoded;charset=UTF-8");
|
2016-06-02 00:02:29 +03:00
|
|
|
aContentLength = serialized.Length();
|
2016-04-12 11:33:53 +03:00
|
|
|
return NS_NewCStringInputStream(aStream, NS_ConvertUTF16toUTF8(serialized));
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2014-09-27 03:41:15 +04:00
|
|
|
|
2014-09-24 09:03:20 +04:00
|
|
|
nsresult
|
2015-03-17 18:32:04 +03:00
|
|
|
ExtractByteStreamFromBody(const OwningArrayBufferOrArrayBufferViewOrBlobOrFormDataOrUSVStringOrURLSearchParams& aBodyInit,
|
2014-09-24 09:03:20 +04:00
|
|
|
nsIInputStream** aStream,
|
2016-06-02 00:02:29 +03:00
|
|
|
nsCString& aContentType,
|
|
|
|
uint64_t& aContentLength)
|
2014-09-24 09:03:20 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aStream);
|
2016-11-28 23:58:02 +03:00
|
|
|
aContentType.SetIsVoid(true);
|
2014-09-24 09:03:20 +04:00
|
|
|
|
|
|
|
if (aBodyInit.IsArrayBuffer()) {
|
|
|
|
const ArrayBuffer& buf = aBodyInit.GetAsArrayBuffer();
|
2016-06-02 00:02:29 +03:00
|
|
|
return ExtractFromArrayBuffer(buf, aStream, aContentLength);
|
|
|
|
}
|
|
|
|
if (aBodyInit.IsArrayBufferView()) {
|
2014-09-24 09:03:20 +04:00
|
|
|
const ArrayBufferView& buf = aBodyInit.GetAsArrayBufferView();
|
2016-06-02 00:02:29 +03:00
|
|
|
return ExtractFromArrayBufferView(buf, aStream, aContentLength);
|
|
|
|
}
|
|
|
|
if (aBodyInit.IsBlob()) {
|
2015-05-12 15:09:51 +03:00
|
|
|
const Blob& blob = aBodyInit.GetAsBlob();
|
2016-06-02 00:02:29 +03:00
|
|
|
return ExtractFromBlob(blob, aStream, aContentType, aContentLength);
|
|
|
|
}
|
|
|
|
if (aBodyInit.IsFormData()) {
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData& form = aBodyInit.GetAsFormData();
|
2016-06-02 00:02:29 +03:00
|
|
|
return ExtractFromFormData(form, aStream, aContentType, aContentLength);
|
|
|
|
}
|
|
|
|
if (aBodyInit.IsUSVString()) {
|
2014-09-27 03:41:15 +04:00
|
|
|
nsAutoString str;
|
2014-11-20 14:58:00 +03:00
|
|
|
str.Assign(aBodyInit.GetAsUSVString());
|
2016-06-02 00:02:29 +03:00
|
|
|
return ExtractFromUSVString(str, aStream, aContentType, aContentLength);
|
|
|
|
}
|
|
|
|
if (aBodyInit.IsURLSearchParams()) {
|
2014-09-27 03:41:15 +04:00
|
|
|
URLSearchParams& params = aBodyInit.GetAsURLSearchParams();
|
2016-06-02 00:02:29 +03:00
|
|
|
return ExtractFromURLSearchParams(params, aStream, aContentType, aContentLength);
|
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
|
2015-03-17 18:32:04 +03:00
|
|
|
ExtractByteStreamFromBody(const ArrayBufferOrArrayBufferViewOrBlobOrFormDataOrUSVStringOrURLSearchParams& aBodyInit,
|
2014-09-27 03:41:15 +04:00
|
|
|
nsIInputStream** aStream,
|
2016-06-02 00:02:29 +03:00
|
|
|
nsCString& aContentType,
|
|
|
|
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);
|
2016-11-28 23:58:02 +03:00
|
|
|
aContentType.SetIsVoid(true);
|
2014-09-27 03:41:15 +04:00
|
|
|
|
|
|
|
if (aBodyInit.IsArrayBuffer()) {
|
|
|
|
const ArrayBuffer& buf = aBodyInit.GetAsArrayBuffer();
|
2016-06-02 00:02:29 +03:00
|
|
|
return ExtractFromArrayBuffer(buf, aStream, aContentLength);
|
|
|
|
}
|
|
|
|
if (aBodyInit.IsArrayBufferView()) {
|
2014-09-27 03:41:15 +04:00
|
|
|
const ArrayBufferView& buf = aBodyInit.GetAsArrayBufferView();
|
2016-06-02 00:02:29 +03:00
|
|
|
return ExtractFromArrayBufferView(buf, aStream, aContentLength);
|
|
|
|
}
|
|
|
|
if (aBodyInit.IsBlob()) {
|
2015-05-12 15:09:51 +03:00
|
|
|
const Blob& blob = aBodyInit.GetAsBlob();
|
2016-06-02 00:02:29 +03:00
|
|
|
return ExtractFromBlob(blob, aStream, aContentType, aContentLength);
|
|
|
|
}
|
|
|
|
if (aBodyInit.IsFormData()) {
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData& form = aBodyInit.GetAsFormData();
|
2016-06-02 00:02:29 +03:00
|
|
|
return ExtractFromFormData(form, aStream, aContentType, aContentLength);
|
|
|
|
}
|
|
|
|
if (aBodyInit.IsUSVString()) {
|
2014-09-27 03:41:15 +04:00
|
|
|
nsAutoString str;
|
2014-11-20 14:58:00 +03:00
|
|
|
str.Assign(aBodyInit.GetAsUSVString());
|
2016-06-02 00:02:29 +03:00
|
|
|
return ExtractFromUSVString(str, aStream, aContentType, aContentLength);
|
|
|
|
}
|
|
|
|
if (aBodyInit.IsURLSearchParams()) {
|
2014-09-27 03:41:15 +04:00
|
|
|
URLSearchParams& params = aBodyInit.GetAsURLSearchParams();
|
2016-06-02 00:02:29 +03:00
|
|
|
return ExtractFromURLSearchParams(params, aStream, aContentType, aContentLength);
|
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
|
|
|
{
|
|
|
|
// This has been addrefed before this runnable is dispatched,
|
|
|
|
// released in WorkerRun().
|
|
|
|
FetchBody<Derived>* mFetchBody;
|
|
|
|
nsresult mStatus;
|
|
|
|
uint32_t mLength;
|
|
|
|
uint8_t* mResult;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ContinueConsumeBodyRunnable(FetchBody<Derived>* aFetchBody, nsresult aStatus,
|
|
|
|
uint32_t aLength, uint8_t* aResult)
|
2016-06-08 08:12:05 +03:00
|
|
|
: MainThreadWorkerRunnable(aFetchBody->mWorkerPrivate)
|
2014-12-10 11:51:59 +03:00
|
|
|
, mFetchBody(aFetchBody)
|
|
|
|
, 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
|
|
|
{
|
|
|
|
mFetchBody->ContinueConsumeBody(mStatus, mLength, mResult);
|
|
|
|
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
|
|
|
|
{
|
|
|
|
// This has been addrefed before this runnable is dispatched,
|
|
|
|
// released in WorkerRun().
|
|
|
|
FetchBody<Derived>* mFetchBody;
|
|
|
|
RefPtr<BlobImpl> mBlobImpl;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ContinueConsumeBlobBodyRunnable(FetchBody<Derived>* aFetchBody,
|
|
|
|
BlobImpl* aBlobImpl)
|
|
|
|
: MainThreadWorkerRunnable(aFetchBody->mWorkerPrivate)
|
|
|
|
, mFetchBody(aFetchBody)
|
|
|
|
, mBlobImpl(aBlobImpl)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mBlobImpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
|
|
{
|
|
|
|
mFetchBody->ContinueConsumeBlobBody(mBlobImpl);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
// OnStreamComplete always adopts the buffer, utility class to release it in
|
|
|
|
// a couple of places.
|
2015-03-21 19:28:04 +03:00
|
|
|
class MOZ_STACK_CLASS AutoFreeBuffer final {
|
2014-12-10 11:51:59 +03:00
|
|
|
uint8_t* mBuffer;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit AutoFreeBuffer(uint8_t* aBuffer)
|
|
|
|
: mBuffer(aBuffer)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~AutoFreeBuffer()
|
|
|
|
{
|
2015-02-19 07:51:06 +03:00
|
|
|
free(mBuffer);
|
2014-12-10 11:51:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Reset()
|
|
|
|
{
|
|
|
|
mBuffer= nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
class FailConsumeBodyWorkerRunnable : public MainThreadWorkerControlRunnable
|
|
|
|
{
|
|
|
|
FetchBody<Derived>* mBody;
|
|
|
|
public:
|
|
|
|
explicit FailConsumeBodyWorkerRunnable(FetchBody<Derived>* aBody)
|
|
|
|
: MainThreadWorkerControlRunnable(aBody->mWorkerPrivate)
|
|
|
|
, mBody(aBody)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
|
|
|
mBody->ContinueConsumeBody(NS_ERROR_FAILURE, 0, nullptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In case of failure to create a stream pump or dispatch stream completion to
|
|
|
|
* worker, ensure we cleanup properly. Thread agnostic.
|
|
|
|
*/
|
|
|
|
template <class Derived>
|
2015-03-21 19:28:04 +03:00
|
|
|
class MOZ_STACK_CLASS AutoFailConsumeBody final
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
|
|
|
FetchBody<Derived>* mBody;
|
|
|
|
public:
|
|
|
|
explicit AutoFailConsumeBody(FetchBody<Derived>* aBody)
|
|
|
|
: mBody(aBody)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
~AutoFailConsumeBody()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
if (mBody) {
|
|
|
|
if (mBody->mWorkerPrivate) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<FailConsumeBodyWorkerRunnable<Derived>> r =
|
2014-12-10 11:51:59 +03:00
|
|
|
new FailConsumeBodyWorkerRunnable<Derived>(mBody);
|
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 {
|
|
|
|
mBody->ContinueConsumeBody(NS_ERROR_FAILURE, 0, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DontFail()
|
|
|
|
{
|
|
|
|
mBody = nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
FetchBody<Derived>* mFetchBody;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
explicit ConsumeBodyDoneObserver(FetchBody<Derived>* aFetchBody)
|
|
|
|
: mFetchBody(aFetchBody)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (mFetchBody->mWorkerPrivate) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ContinueConsumeBodyRunnable<Derived>> r =
|
2014-12-10 11:51:59 +03:00
|
|
|
new ContinueConsumeBodyRunnable<Derived>(mFetchBody,
|
|
|
|
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 {
|
|
|
|
mFetchBody->ContinueConsumeBody(aStatus, aResultLength, nonconstResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
if (mFetchBody->mWorkerPrivate) {
|
|
|
|
RefPtr<ContinueConsumeBlobBodyRunnable<Derived>> r =
|
|
|
|
new ContinueConsumeBlobBodyRunnable<Derived>(mFetchBody, aBlob->Impl());
|
|
|
|
|
|
|
|
if (!r->Dispatch()) {
|
|
|
|
NS_WARNING("Could not dispatch ConsumeBlobBodyRunnable");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mFetchBody->ContinueConsumeBlobBody(aBlob->Impl());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
FetchBody<Derived>* mFetchBody;
|
|
|
|
public:
|
|
|
|
explicit BeginConsumeBodyRunnable(FetchBody<Derived>* aBody)
|
|
|
|
: mFetchBody(aBody)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD
|
2015-03-21 19:28:04 +03:00
|
|
|
Run() override
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
|
|
|
mFetchBody->BeginConsumeBodyMainThread();
|
|
|
|
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
|
|
|
{
|
|
|
|
FetchBody<Derived>* mBody;
|
|
|
|
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)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
2015-03-21 19:28:04 +03:00
|
|
|
MainThreadRun() override
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
|
|
|
mBody->CancelPump();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2014-12-10 11:51:59 +03:00
|
|
|
|
|
|
|
template <class Derived>
|
2016-06-23 11:53:14 +03:00
|
|
|
class FetchBodyWorkerHolder final : public workers::WorkerHolder
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
2016-06-23 11:53:14 +03:00
|
|
|
// This is addrefed before the workerHolder is created, and is released in
|
|
|
|
// ContinueConsumeBody() so we can hold a rawptr.
|
2014-12-10 11:51:59 +03:00
|
|
|
FetchBody<Derived>* mBody;
|
2015-10-01 02:11:04 +03:00
|
|
|
bool mWasNotified;
|
2014-12-10 11:51:59 +03:00
|
|
|
|
|
|
|
public:
|
2016-06-23 11:53:14 +03:00
|
|
|
explicit FetchBodyWorkerHolder(FetchBody<Derived>* aBody)
|
2014-12-10 11:51:59 +03:00
|
|
|
: mBody(aBody)
|
2015-10-01 02:11:04 +03:00
|
|
|
, mWasNotified(false)
|
2014-12-10 11:51:59 +03:00
|
|
|
{ }
|
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
~FetchBodyWorkerHolder()
|
2014-12-10 11:51:59 +03:00
|
|
|
{ }
|
|
|
|
|
2016-03-28 20:28:14 +03:00
|
|
|
bool Notify(workers::Status aStatus) override
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aStatus > workers::Running);
|
2015-10-01 02:11:04 +03:00
|
|
|
if (!mWasNotified) {
|
|
|
|
mWasNotified = true;
|
|
|
|
mBody->ContinueConsumeBody(NS_BINDING_ABORTED, 0, nullptr);
|
|
|
|
}
|
2014-12-10 11:51:59 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
FetchBody<Derived>::FetchBody()
|
2016-06-23 11:53:14 +03:00
|
|
|
: mWorkerHolder(nullptr)
|
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
|
|
|
{
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
mWorkerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
} else {
|
|
|
|
mWorkerPrivate = nullptr;
|
|
|
|
}
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
2014-12-10 11:51:59 +03:00
|
|
|
|
|
|
|
template
|
|
|
|
FetchBody<Request>::FetchBody();
|
|
|
|
|
|
|
|
template
|
|
|
|
FetchBody<Response>::FetchBody();
|
|
|
|
|
2015-02-19 04:34:29 +03:00
|
|
|
template <class Derived>
|
|
|
|
FetchBody<Derived>::~FetchBody()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
// Returns true if addref succeeded.
|
|
|
|
// Always succeeds on main thread.
|
2016-06-23 11:53:14 +03:00
|
|
|
// May fail on worker if RegisterWorkerHolder() fails. In that case, it will
|
|
|
|
// release the object before returning false.
|
2014-12-10 11:51:59 +03:00
|
|
|
template <class Derived>
|
|
|
|
bool
|
|
|
|
FetchBody<Derived>::AddRefObject()
|
|
|
|
{
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
DerivedClass()->AddRef();
|
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
if (mWorkerPrivate && !mWorkerHolder) {
|
|
|
|
if (!RegisterWorkerHolder()) {
|
2014-12-10 11:51:59 +03:00
|
|
|
ReleaseObject();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
2014-12-10 11:51:59 +03:00
|
|
|
void
|
|
|
|
FetchBody<Derived>::ReleaseObject()
|
2014-09-27 03:41:15 +04:00
|
|
|
{
|
2014-12-10 11:51:59 +03:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
if (mWorkerPrivate && mWorkerHolder) {
|
|
|
|
UnregisterWorkerHolder();
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
DerivedClass()->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
bool
|
2016-06-23 11:53:14 +03:00
|
|
|
FetchBody<Derived>::RegisterWorkerHolder()
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2016-06-23 11:53:14 +03:00
|
|
|
MOZ_ASSERT(!mWorkerHolder);
|
|
|
|
mWorkerHolder = new FetchBodyWorkerHolder<Derived>(this);
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2016-08-18 17:11:04 +03:00
|
|
|
if (!mWorkerHolder->HoldWorker(mWorkerPrivate, Closing)) {
|
2016-06-23 11:53:14 +03:00
|
|
|
NS_WARNING("Failed to add workerHolder");
|
|
|
|
mWorkerHolder = nullptr;
|
2014-12-10 11:51:59 +03:00
|
|
|
return false;
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
2016-06-23 11:53:14 +03:00
|
|
|
FetchBody<Derived>::UnregisterWorkerHolder()
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2016-06-23 11:53:14 +03:00
|
|
|
MOZ_ASSERT(mWorkerHolder);
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
mWorkerHolder->ReleaseWorker();
|
|
|
|
mWorkerHolder = nullptr;
|
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();
|
2016-06-23 11:53:14 +03:00
|
|
|
MOZ_ASSERT(!mWorkerHolder);
|
2014-12-10 11:51:59 +03:00
|
|
|
MOZ_ASSERT(mConsumePromise);
|
|
|
|
|
|
|
|
// The FetchBody is not thread-safe refcounted. We addref it here and release
|
|
|
|
// it once the stream read is finished.
|
|
|
|
if (!AddRefObject()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-12-24 08:47:17 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r = new BeginConsumeBodyRunnable<Derived>(this);
|
2016-09-14 06:14:02 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (mWorkerPrivate) {
|
|
|
|
rv = mWorkerPrivate->DispatchToMainThread(r.forget());
|
|
|
|
} else {
|
|
|
|
rv = NS_DispatchToMainThread(r.forget());
|
|
|
|
}
|
2014-12-24 08:47:17 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
ReleaseObject();
|
|
|
|
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
|
|
|
|
FetchBody<Derived>::BeginConsumeBodyMainThread()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
AutoFailConsumeBody<Derived> autoReject(DerivedClass());
|
|
|
|
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),
|
|
|
|
stream);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-24 09:03:20 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ConsumeBodyDoneObserver<Derived>> p = new ConsumeBodyDoneObserver<Derived>(this);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
listener = new MutableBlobStreamListener(type, nullptr, mMimeType, p);
|
|
|
|
} 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.
|
|
|
|
mConsumeBodyPump = new nsMainThreadPtrHolder<nsIInputStreamPump>(pump);
|
|
|
|
// 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
|
|
|
|
FetchBody<Derived>::ContinueConsumeBody(nsresult aStatus, uint32_t aResultLength, uint8_t* aResult)
|
|
|
|
{
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
// Just a precaution to ensure ContinueConsumeBody is not called out of
|
|
|
|
// sync with a body read.
|
|
|
|
MOZ_ASSERT(mBodyUsed);
|
|
|
|
MOZ_ASSERT(!mReadDone);
|
2016-06-23 11:53:14 +03:00
|
|
|
MOZ_ASSERT_IF(mWorkerPrivate, mWorkerHolder);
|
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
|
|
|
|
|
|
|
AutoFreeBuffer autoFree(aResult);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mConsumePromise);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> localPromise = mConsumePromise.forget();
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2016-08-24 21:12:09 +03:00
|
|
|
RefPtr<Derived> derivedClass = DerivedClass();
|
2014-12-10 11:51:59 +03:00
|
|
|
ReleaseObject();
|
|
|
|
|
|
|
|
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 =
|
2014-12-10 11:51:59 +03:00
|
|
|
new CancelPumpRunnable<Derived>(this);
|
2015-11-24 08:04:21 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
r->Dispatch(rv);
|
|
|
|
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;
|
2016-08-24 21:12:09 +03:00
|
|
|
if (!jsapi.Init(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,
|
2015-09-17 15:10:42 +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.
|
|
|
|
autoFree.Reset();
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
autoFree.Reset();
|
|
|
|
|
2016-02-28 00:54:11 +03:00
|
|
|
RefPtr<dom::FormData> fd = BodyUtil::ConsumeFormData(
|
2016-08-24 21:12:09 +03:00
|
|
|
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
|
|
|
|
FetchBody<Derived>::ContinueConsumeBlobBody(BlobImpl* aBlobImpl)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
MOZ_ASSERT_IF(mWorkerPrivate, mWorkerHolder);
|
|
|
|
#ifdef DEBUG
|
|
|
|
mReadDone = true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
MOZ_ASSERT(mConsumePromise);
|
|
|
|
RefPtr<Promise> localPromise = mConsumePromise.forget();
|
|
|
|
|
|
|
|
RefPtr<Derived> derivedClass = DerivedClass();
|
|
|
|
ReleaseObject();
|
|
|
|
|
|
|
|
// 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 =
|
|
|
|
dom::Blob::Create(derivedClass->GetParentObject(), aBlobImpl);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
mConsumeType = aType;
|
|
|
|
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();
|
|
|
|
|
|
|
|
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
|