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"
|
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"
|
|
|
|
#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"
|
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"
|
2015-09-17 15:10:42 +03:00
|
|
|
#include "FetchUtil.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
|
|
|
{}
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
private:
|
|
|
|
~MainThreadFetchResolver();
|
|
|
|
};
|
|
|
|
|
|
|
|
class MainThreadFetchRunnable : public nsRunnable
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Run()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> proxy = mResolver->mPromiseProxy;
|
2015-09-03 01:04:17 +03:00
|
|
|
MutexAutoLock lock(proxy->Lock());
|
|
|
|
if (proxy->CleanedUp()) {
|
|
|
|
NS_WARNING("Aborting Fetch because worker already shut down");
|
2014-12-10 11:51:59 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-03 01:04:17 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal = proxy->GetWorkerPrivate()->GetPrincipal();
|
|
|
|
MOZ_ASSERT(principal);
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = proxy->GetWorkerPrivate()->GetLoadGroup();
|
|
|
|
MOZ_ASSERT(loadGroup);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<FetchDriver> fetch = new FetchDriver(mRequest, principal, loadGroup);
|
2014-10-06 22:01:20 +04:00
|
|
|
nsresult rv = fetch->Fetch(mResolver);
|
|
|
|
// Right now we only support async fetch, which should never directly fail.
|
2014-10-21 00:25:56 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-10-06 22:01:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
jsapi.Init(aGlobal);
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
|
|
|
|
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);
|
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) {
|
|
|
|
NS_WARNING("Could not add WorkerFetchResolver feature to worker");
|
|
|
|
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);
|
2015-09-03 01:04:17 +03:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_DispatchToMainThread(run)));
|
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
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class WorkerFetchResponseRunnable final : public WorkerRunnable
|
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)
|
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadModifyBusyCount)
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class WorkerFetchResponseEndRunnable final : public WorkerRunnable
|
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
|
|
|
public:
|
2015-09-03 01:04:17 +03:00
|
|
|
WorkerFetchResponseEndRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
WorkerFetchResolver* aResolver)
|
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadModifyBusyCount)
|
2014-12-10 11:51:59 +03:00
|
|
|
, mResolver(aResolver)
|
|
|
|
{
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2015-09-03 01:04:17 +03:00
|
|
|
mResolver->mPromiseProxy->CleanUp(aCx);
|
2014-12-10 11:51:59 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
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
|
|
|
|
2015-09-03 01:04:17 +03:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
|
|
|
if (!r->Dispatch(jsapi.cx())) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerFetchResponseEndRunnable> r =
|
2015-09-03 01:04:17 +03:00
|
|
|
new WorkerFetchResponseEndRunnable(mPromiseProxy->GetWorkerPrivate(), this);
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2015-09-03 01:04:17 +03:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
|
|
|
if (!r->Dispatch(jsapi.cx())) {
|
|
|
|
NS_WARNING("Could not dispatch fetch response end");
|
2014-12-10 11:51:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
namespace {
|
|
|
|
nsresult
|
|
|
|
ExtractFromArrayBuffer(const ArrayBuffer& aBuffer,
|
|
|
|
nsIInputStream** aStream)
|
|
|
|
{
|
|
|
|
aBuffer.ComputeLengthAndData();
|
|
|
|
//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,
|
|
|
|
nsIInputStream** aStream)
|
|
|
|
{
|
|
|
|
aBuffer.ComputeLengthAndData();
|
|
|
|
//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
|
2015-05-12 15:09:51 +03:00
|
|
|
ExtractFromBlob(const Blob& aBlob, nsIInputStream** aStream,
|
2014-10-09 21:52:10 +04:00
|
|
|
nsCString& aContentType)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BlobImpl> impl = aBlob.Impl();
|
2015-05-19 17:36:37 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
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);
|
|
|
|
aContentType = NS_ConvertUTF16toUTF8(type);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-17 18:32:04 +03:00
|
|
|
nsresult
|
2016-01-07 22:30:36 +03:00
|
|
|
ExtractFromFormData(FormData& aFormData, nsIInputStream** aStream,
|
2015-03-17 18:32:04 +03:00
|
|
|
nsCString& aContentType)
|
|
|
|
{
|
|
|
|
uint64_t unusedContentLength;
|
|
|
|
nsAutoCString unusedCharset;
|
|
|
|
return aFormData.GetSendInfo(aStream, &unusedContentLength,
|
|
|
|
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,
|
|
|
|
nsCString& aContentType)
|
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");
|
|
|
|
|
|
|
|
return NS_NewCStringInputStream(aStream, encoded);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ExtractFromURLSearchParams(const URLSearchParams& aParams,
|
|
|
|
nsIInputStream** aStream,
|
|
|
|
nsCString& aContentType)
|
|
|
|
{
|
|
|
|
nsAutoString serialized;
|
|
|
|
aParams.Stringify(serialized);
|
|
|
|
aContentType = NS_LITERAL_CSTRING("application/x-www-form-urlencoded;charset=UTF-8");
|
|
|
|
return NS_NewStringInputStream(aStream, serialized);
|
|
|
|
}
|
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,
|
|
|
|
nsCString& aContentType)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aStream);
|
|
|
|
|
|
|
|
if (aBodyInit.IsArrayBuffer()) {
|
|
|
|
const ArrayBuffer& buf = aBodyInit.GetAsArrayBuffer();
|
2014-09-27 03:41:15 +04:00
|
|
|
return ExtractFromArrayBuffer(buf, aStream);
|
2014-09-24 09:03:20 +04:00
|
|
|
} else if (aBodyInit.IsArrayBufferView()) {
|
|
|
|
const ArrayBufferView& buf = aBodyInit.GetAsArrayBufferView();
|
2014-09-27 03:41:15 +04:00
|
|
|
return ExtractFromArrayBufferView(buf, aStream);
|
2014-10-09 21:52:10 +04:00
|
|
|
} else if (aBodyInit.IsBlob()) {
|
2015-05-12 15:09:51 +03:00
|
|
|
const Blob& blob = aBodyInit.GetAsBlob();
|
2014-10-09 21:52:10 +04:00
|
|
|
return ExtractFromBlob(blob, aStream, aContentType);
|
2015-03-17 18:32:04 +03:00
|
|
|
} else if (aBodyInit.IsFormData()) {
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData& form = aBodyInit.GetAsFormData();
|
2015-03-17 18:32:04 +03:00
|
|
|
return ExtractFromFormData(form, aStream, aContentType);
|
2014-11-20 14:58:00 +03:00
|
|
|
} else 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());
|
|
|
|
return ExtractFromUSVString(str, aStream, aContentType);
|
2014-09-27 03:41:15 +04:00
|
|
|
} else if (aBodyInit.IsURLSearchParams()) {
|
|
|
|
URLSearchParams& params = aBodyInit.GetAsURLSearchParams();
|
|
|
|
return ExtractFromURLSearchParams(params, aStream, aContentType);
|
|
|
|
}
|
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,
|
|
|
|
nsCString& aContentType)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aStream);
|
|
|
|
|
|
|
|
if (aBodyInit.IsArrayBuffer()) {
|
|
|
|
const ArrayBuffer& buf = aBodyInit.GetAsArrayBuffer();
|
|
|
|
return ExtractFromArrayBuffer(buf, aStream);
|
|
|
|
} else if (aBodyInit.IsArrayBufferView()) {
|
|
|
|
const ArrayBufferView& buf = aBodyInit.GetAsArrayBufferView();
|
|
|
|
return ExtractFromArrayBufferView(buf, aStream);
|
2014-10-09 21:52:10 +04:00
|
|
|
} else if (aBodyInit.IsBlob()) {
|
2015-05-12 15:09:51 +03:00
|
|
|
const Blob& blob = aBodyInit.GetAsBlob();
|
2014-10-09 21:52:10 +04:00
|
|
|
return ExtractFromBlob(blob, aStream, aContentType);
|
2015-03-17 18:32:04 +03:00
|
|
|
} else if (aBodyInit.IsFormData()) {
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData& form = aBodyInit.GetAsFormData();
|
2015-03-17 18:32:04 +03:00
|
|
|
return ExtractFromFormData(form, aStream, aContentType);
|
2014-11-20 14:58:00 +03:00
|
|
|
} else 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());
|
|
|
|
return ExtractFromUSVString(str, aStream, aContentType);
|
2014-09-27 03:41:15 +04:00
|
|
|
} else if (aBodyInit.IsURLSearchParams()) {
|
|
|
|
URLSearchParams& params = aBodyInit.GetAsURLSearchParams();
|
|
|
|
return ExtractFromURLSearchParams(params, aStream, aContentType);
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
2015-03-21 19:28:04 +03:00
|
|
|
class ContinueConsumeBodyRunnable final : public WorkerRunnable
|
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)
|
|
|
|
: WorkerRunnable(aFetchBody->mWorkerPrivate, WorkerThreadModifyBusyCount)
|
|
|
|
, 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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
AutoSafeJSContext cx;
|
|
|
|
if (!r->Dispatch(cx)) {
|
|
|
|
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
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
AutoSafeJSContext cx;
|
2015-10-01 02:11:04 +03:00
|
|
|
if (!r->Dispatch(cx)) {
|
|
|
|
// XXXcatalinb: The worker is shutting down, the pump will be canceled
|
|
|
|
// by FetchBodyFeature::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
2015-03-21 19:28:04 +03:00
|
|
|
class BeginConsumeBodyRunnable final : public nsRunnable
|
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)
|
|
|
|
: WorkerMainThreadRunnable(aBody->mWorkerPrivate)
|
|
|
|
, 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>
|
2015-03-21 19:28:04 +03:00
|
|
|
class FetchBodyFeature final : public workers::WorkerFeature
|
2014-12-10 11:51:59 +03:00
|
|
|
{
|
|
|
|
// This is addrefed before the feature is created, and is released in ContinueConsumeBody()
|
|
|
|
// so we can hold a rawptr.
|
|
|
|
FetchBody<Derived>* mBody;
|
2015-10-01 02:11:04 +03:00
|
|
|
bool mWasNotified;
|
2014-12-10 11:51:59 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
explicit FetchBodyFeature(FetchBody<Derived>* aBody)
|
|
|
|
: mBody(aBody)
|
2015-10-01 02:11:04 +03:00
|
|
|
, mWasNotified(false)
|
2014-12-10 11:51:59 +03:00
|
|
|
{ }
|
|
|
|
|
|
|
|
~FetchBodyFeature()
|
|
|
|
{ }
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
bool Notify(JSContext* aCx, 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()
|
|
|
|
: mFeature(nullptr)
|
|
|
|
, mBodyUsed(false)
|
|
|
|
, mReadDone(false)
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
// May fail on worker if RegisterFeature() fails. In that case, it will release
|
|
|
|
// the object before returning false.
|
|
|
|
template <class Derived>
|
|
|
|
bool
|
|
|
|
FetchBody<Derived>::AddRefObject()
|
|
|
|
{
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
DerivedClass()->AddRef();
|
|
|
|
|
|
|
|
if (mWorkerPrivate && !mFeature) {
|
|
|
|
if (!RegisterFeature()) {
|
|
|
|
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();
|
|
|
|
|
|
|
|
if (mWorkerPrivate && mFeature) {
|
|
|
|
UnregisterFeature();
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
DerivedClass()->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
bool
|
|
|
|
FetchBody<Derived>::RegisterFeature()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(!mFeature);
|
|
|
|
mFeature = new FetchBodyFeature<Derived>(this);
|
|
|
|
|
|
|
|
if (!mWorkerPrivate->AddFeature(mWorkerPrivate->GetJSContext(), mFeature)) {
|
|
|
|
NS_WARNING("Failed to add feature");
|
|
|
|
mFeature = nullptr;
|
|
|
|
return false;
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
FetchBody<Derived>::UnregisterFeature()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(mFeature);
|
|
|
|
|
|
|
|
mWorkerPrivate->RemoveFeature(mWorkerPrivate->GetJSContext(), mFeature);
|
|
|
|
mFeature = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
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(!mFeature);
|
|
|
|
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);
|
|
|
|
nsresult rv = NS_DispatchToMainThread(r);
|
|
|
|
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);
|
2014-12-10 11:51:59 +03:00
|
|
|
nsCOMPtr<nsIStreamLoader> loader;
|
|
|
|
rv = NS_NewStreamLoader(getter_AddRefs(loader), p);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
rv = pump->AsyncRead(loader, nullptr);
|
|
|
|
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);
|
|
|
|
MOZ_ASSERT_IF(mWorkerPrivate, mFeature);
|
|
|
|
mReadDone = true;
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Derived> kungfuDeathGrip = 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;
|
|
|
|
jsapi.Init(DerivedClass()->GetParentObject());
|
|
|
|
JSContext* cx = jsapi.cx();
|
2014-12-10 11:51:59 +03:00
|
|
|
|
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);
|
2015-09-17 15:10:42 +03:00
|
|
|
FetchUtil::ConsumeArrayBuffer(cx, &arrayBuffer, aResultLength, aResult,
|
|
|
|
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: {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<dom::Blob> blob = FetchUtil::ConsumeBlob(
|
2015-09-17 15:10:42 +03:00
|
|
|
DerivedClass()->GetParentObject(), NS_ConvertUTF8toUTF16(mMimeType),
|
|
|
|
aResultLength, aResult, error);
|
|
|
|
if (!error.Failed()) {
|
|
|
|
localPromise->MaybeResolve(blob);
|
|
|
|
// File takes over ownership.
|
|
|
|
autoFree.Reset();
|
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-04-04 08:55:15 +03:00
|
|
|
case CONSUME_FORMDATA: {
|
|
|
|
nsCString data;
|
|
|
|
data.Adopt(reinterpret_cast<char*>(aResult), aResultLength);
|
|
|
|
autoFree.Reset();
|
|
|
|
|
2016-01-07 22:30:36 +03:00
|
|
|
RefPtr<dom::FormData> fd = FetchUtil::ConsumeFormData(
|
2015-09-17 15:10:42 +03:00
|
|
|
DerivedClass()->GetParentObject(),
|
|
|
|
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;
|
|
|
|
if (NS_SUCCEEDED(FetchUtil::ConsumeText(aResultLength, aResult, decoded))) {
|
|
|
|
if (mConsumeType == CONSUME_TEXT) {
|
|
|
|
localPromise->MaybeResolve(decoded);
|
|
|
|
} else {
|
|
|
|
JS::Rooted<JS::Value> json(cx);
|
|
|
|
FetchUtil::ConsumeJson(cx, &json, decoded, error);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2014-09-27 03:41:15 +04:00
|
|
|
nsTArray<nsCString> contentTypeValues;
|
2014-10-02 21:59:20 +04:00
|
|
|
MOZ_ASSERT(DerivedClass()->GetInternalHeaders());
|
2015-02-03 23:59:32 +03:00
|
|
|
DerivedClass()->GetInternalHeaders()->GetAll(NS_LITERAL_CSTRING("Content-Type"),
|
|
|
|
contentTypeValues, result);
|
|
|
|
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.
|
|
|
|
if (contentTypeValues.Length() == 1) {
|
|
|
|
mMimeType = contentTypeValues[0];
|
|
|
|
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
|