2016-11-06 11:53:34 +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: */
|
|
|
|
/* 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 "Worklet.h"
|
2018-04-12 06:14:48 +03:00
|
|
|
#include "WorkletThread.h"
|
2016-12-17 00:21:51 +03:00
|
|
|
#include "AudioWorkletGlobalScope.h"
|
|
|
|
#include "PaintWorkletGlobalScope.h"
|
|
|
|
|
2016-11-06 11:53:34 +03:00
|
|
|
#include "mozilla/dom/WorkletBinding.h"
|
|
|
|
#include "mozilla/dom/BlobBinding.h"
|
|
|
|
#include "mozilla/dom/Fetch.h"
|
|
|
|
#include "mozilla/dom/PromiseNativeHandler.h"
|
2016-11-06 11:54:29 +03:00
|
|
|
#include "mozilla/dom/RegisterWorkletBindings.h"
|
2016-11-06 11:53:34 +03:00
|
|
|
#include "mozilla/dom/Response.h"
|
2016-11-06 11:54:29 +03:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2017-05-08 09:24:22 +03:00
|
|
|
#include "mozilla/dom/ScriptLoader.h"
|
2017-06-20 18:53:21 +03:00
|
|
|
#include "nsIInputStreamPump.h"
|
2016-11-06 11:54:29 +03:00
|
|
|
#include "nsIThreadRetargetableRequest.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "xpcprivate.h"
|
2016-11-06 11:53:34 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2018-04-12 06:14:48 +03:00
|
|
|
class ExecutionRunnable final : public Runnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ExecutionRunnable(WorkletFetchHandler* aHandler, Worklet::WorkletType aType,
|
|
|
|
char16_t* aScriptBuffer, size_t aScriptLength)
|
|
|
|
: Runnable("Worklet::ExecutionRunnable")
|
|
|
|
, mHandler(aHandler)
|
|
|
|
, mWorkletType(aType)
|
|
|
|
, mBuffer(aScriptBuffer, aScriptLength,
|
|
|
|
JS::SourceBufferHolder::GiveOwnership)
|
|
|
|
, mResult(NS_ERROR_FAILURE)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void
|
|
|
|
RunOnWorkletThread();
|
|
|
|
|
|
|
|
void
|
|
|
|
RunOnMainThread();
|
|
|
|
|
|
|
|
RefPtr<WorkletFetchHandler> mHandler;
|
|
|
|
Worklet::WorkletType mWorkletType;
|
|
|
|
JS::SourceBufferHolder mBuffer;
|
|
|
|
nsresult mResult;
|
|
|
|
};
|
|
|
|
|
2016-11-06 11:55:20 +03:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// WorkletFetchHandler
|
2016-11-06 11:53:34 +03:00
|
|
|
|
2018-04-12 06:14:48 +03:00
|
|
|
class WorkletFetchHandler final : public PromiseNativeHandler
|
|
|
|
, public nsIStreamLoaderObserver
|
2016-11-06 11:53:34 +03:00
|
|
|
{
|
|
|
|
public:
|
2018-04-12 06:14:48 +03:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2016-11-06 11:53:34 +03:00
|
|
|
|
|
|
|
static already_AddRefed<Promise>
|
2017-02-01 23:43:37 +03:00
|
|
|
Fetch(Worklet* aWorklet, const nsAString& aModuleURL, CallerType aCallerType,
|
|
|
|
ErrorResult& aRv)
|
2016-11-06 11:53:34 +03:00
|
|
|
{
|
2016-11-06 11:55:20 +03:00
|
|
|
MOZ_ASSERT(aWorklet);
|
2018-04-12 06:14:48 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-11-06 11:55:20 +03:00
|
|
|
|
2016-11-06 11:54:52 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global =
|
|
|
|
do_QueryInterface(aWorklet->GetParentObject());
|
|
|
|
MOZ_ASSERT(global);
|
|
|
|
|
|
|
|
RefPtr<Promise> promise = Promise::Create(global, aRv);
|
2016-11-06 11:53:34 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-06 11:55:20 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = aWorklet->GetParentObject();
|
|
|
|
MOZ_ASSERT(window);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc;
|
|
|
|
doc = window->GetExtantDoc();
|
|
|
|
if (!doc) {
|
|
|
|
promise->MaybeReject(NS_ERROR_FAILURE);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> baseURI = doc->GetBaseURI();
|
|
|
|
nsCOMPtr<nsIURI> resolvedURI;
|
2018-04-12 06:14:48 +03:00
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(resolvedURI), aModuleURL, nullptr,
|
|
|
|
baseURI);
|
2016-11-06 11:55:20 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
promise->MaybeReject(rv);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString spec;
|
|
|
|
rv = resolvedURI->GetSpec(spec);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
promise->MaybeReject(rv);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maybe we already have an handler for this URI
|
|
|
|
{
|
|
|
|
WorkletFetchHandler* handler = aWorklet->GetImportFetchHandler(spec);
|
|
|
|
if (handler) {
|
|
|
|
handler->AddPromise(promise);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-06 11:53:34 +03:00
|
|
|
RequestOrUSVString request;
|
|
|
|
request.SetAsUSVString().Rebind(aModuleURL.Data(), aModuleURL.Length());
|
|
|
|
|
|
|
|
RequestInit init;
|
|
|
|
|
2017-02-01 23:43:37 +03:00
|
|
|
RefPtr<Promise> fetchPromise =
|
|
|
|
FetchRequest(global, request, init, aCallerType, aRv);
|
2016-11-06 11:53:34 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
promise->MaybeReject(aRv);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<WorkletFetchHandler> handler =
|
2016-11-06 11:54:29 +03:00
|
|
|
new WorkletFetchHandler(aWorklet, aModuleURL, promise);
|
2016-11-06 11:53:34 +03:00
|
|
|
fetchPromise->AppendNativeHandler(handler);
|
|
|
|
|
2016-11-06 11:55:20 +03:00
|
|
|
aWorklet->AddImportFetchHandler(spec, handler);
|
2016-11-06 11:53:34 +03:00
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override
|
|
|
|
{
|
2018-04-12 06:14:48 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2016-11-06 11:53:34 +03:00
|
|
|
if (!aValue.isObject()) {
|
2016-11-06 11:55:20 +03:00
|
|
|
RejectPromises(NS_ERROR_FAILURE);
|
2016-11-06 11:53:34 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Response> response;
|
|
|
|
nsresult rv = UNWRAP_OBJECT(Response, &aValue.toObject(), response);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2016-11-06 11:55:20 +03:00
|
|
|
RejectPromises(NS_ERROR_FAILURE);
|
2016-11-06 11:53:34 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!response->Ok()) {
|
2016-11-06 11:55:20 +03:00
|
|
|
RejectPromises(NS_ERROR_DOM_NETWORK_ERR);
|
2016-11-06 11:53:34 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-06 11:54:29 +03:00
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
|
|
|
response->GetBody(getter_AddRefs(inputStream));
|
|
|
|
if (!inputStream) {
|
2016-11-06 11:55:20 +03:00
|
|
|
RejectPromises(NS_ERROR_DOM_NETWORK_ERR);
|
2016-11-06 11:54:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStreamPump> pump;
|
2017-11-22 13:18:57 +03:00
|
|
|
rv = NS_NewInputStreamPump(getter_AddRefs(pump), inputStream.forget());
|
2016-11-06 11:54:29 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2016-11-06 11:55:20 +03:00
|
|
|
RejectPromises(rv);
|
2016-11-06 11:54:29 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-11-06 11:53:34 +03:00
|
|
|
|
2016-11-06 11:54:29 +03:00
|
|
|
nsCOMPtr<nsIStreamLoader> loader;
|
|
|
|
rv = NS_NewStreamLoader(getter_AddRefs(loader), this);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2016-11-06 11:55:20 +03:00
|
|
|
RejectPromises(rv);
|
2016-11-06 11:54:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = pump->AsyncRead(loader, nullptr);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2016-11-06 11:55:20 +03:00
|
|
|
RejectPromises(rv);
|
2016-11-06 11:54:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThreadRetargetableRequest> rr = do_QueryInterface(pump);
|
|
|
|
if (rr) {
|
|
|
|
nsCOMPtr<nsIEventTarget> sts =
|
|
|
|
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
|
|
|
|
rv = rr->RetargetDeliveryTo(sts);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the nsIInputStreamPump to a IO thread.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
OnStreamComplete(nsIStreamLoader* aLoader, nsISupports* aContext,
|
|
|
|
nsresult aStatus, uint32_t aStringLen,
|
|
|
|
const uint8_t* aString) override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (NS_FAILED(aStatus)) {
|
2016-11-06 11:55:20 +03:00
|
|
|
RejectPromises(aStatus);
|
2016-11-06 11:54:29 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
char16_t* scriptTextBuf;
|
|
|
|
size_t scriptTextLength;
|
|
|
|
nsresult rv =
|
2017-05-08 09:24:22 +03:00
|
|
|
ScriptLoader::ConvertToUTF16(nullptr, aString, aStringLen,
|
|
|
|
NS_LITERAL_STRING("UTF-8"), nullptr,
|
|
|
|
scriptTextBuf, scriptTextLength);
|
2016-11-06 11:54:29 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2016-11-06 11:55:20 +03:00
|
|
|
RejectPromises(rv);
|
2016-11-06 11:54:29 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Moving the ownership of the buffer
|
2018-04-12 06:14:48 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
new ExecutionRunnable(this, mWorklet->Type(), scriptTextBuf,
|
|
|
|
scriptTextLength);
|
2016-11-06 11:54:29 +03:00
|
|
|
|
2018-04-12 06:14:48 +03:00
|
|
|
RefPtr<WorkletThread> thread = mWorklet->GetOrCreateThread();
|
|
|
|
if (!thread) {
|
|
|
|
RejectPromises(NS_ERROR_FAILURE);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-01-20 16:39:39 +03:00
|
|
|
|
2018-04-12 06:14:48 +03:00
|
|
|
if (NS_FAILED(thread->DispatchRunnable(runnable.forget()))) {
|
|
|
|
RejectPromises(NS_ERROR_FAILURE);
|
2016-11-06 11:54:29 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2016-11-06 11:53:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override
|
|
|
|
{
|
2018-04-12 06:14:48 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-11-06 11:55:20 +03:00
|
|
|
RejectPromises(NS_ERROR_DOM_NETWORK_ERR);
|
2016-11-06 11:53:34 +03:00
|
|
|
}
|
|
|
|
|
2018-04-12 06:14:48 +03:00
|
|
|
const nsString& URL() const
|
|
|
|
{
|
|
|
|
return mURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ExecutionFailed(nsresult aRv)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
RejectPromises(aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ExecutionSucceeded()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
ResolvePromises();
|
|
|
|
}
|
|
|
|
|
2016-11-06 11:53:34 +03:00
|
|
|
private:
|
2016-11-06 11:54:29 +03:00
|
|
|
WorkletFetchHandler(Worklet* aWorklet, const nsAString& aURL,
|
|
|
|
Promise* aPromise)
|
2016-11-06 11:53:34 +03:00
|
|
|
: mWorklet(aWorklet)
|
2016-11-06 11:55:20 +03:00
|
|
|
, mStatus(ePending)
|
|
|
|
, mErrorStatus(NS_OK)
|
2016-11-06 11:54:29 +03:00
|
|
|
, mURL(aURL)
|
2016-11-06 11:55:20 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorklet);
|
|
|
|
MOZ_ASSERT(aPromise);
|
2018-04-12 06:14:48 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-11-06 11:55:20 +03:00
|
|
|
|
|
|
|
mPromises.AppendElement(aPromise);
|
|
|
|
}
|
2016-11-06 11:53:34 +03:00
|
|
|
|
|
|
|
~WorkletFetchHandler()
|
|
|
|
{}
|
|
|
|
|
2016-11-06 11:55:20 +03:00
|
|
|
void
|
|
|
|
AddPromise(Promise* aPromise)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aPromise);
|
2018-04-12 06:14:48 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-11-06 11:55:20 +03:00
|
|
|
|
|
|
|
switch (mStatus) {
|
|
|
|
case ePending:
|
|
|
|
mPromises.AppendElement(aPromise);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case eRejected:
|
|
|
|
MOZ_ASSERT(NS_FAILED(mErrorStatus));
|
|
|
|
aPromise->MaybeReject(mErrorStatus);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case eResolved:
|
|
|
|
aPromise->MaybeResolveWithUndefined();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RejectPromises(nsresult aResult)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mStatus == ePending);
|
|
|
|
MOZ_ASSERT(NS_FAILED(aResult));
|
2018-04-12 06:14:48 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-11-06 11:55:20 +03:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mPromises.Length(); ++i) {
|
|
|
|
mPromises[i]->MaybeReject(aResult);
|
|
|
|
}
|
|
|
|
mPromises.Clear();
|
|
|
|
|
|
|
|
mStatus = eRejected;
|
|
|
|
mErrorStatus = aResult;
|
|
|
|
mWorklet = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ResolvePromises()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mStatus == ePending);
|
2018-04-12 06:14:48 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-11-06 11:55:20 +03:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mPromises.Length(); ++i) {
|
|
|
|
mPromises[i]->MaybeResolveWithUndefined();
|
|
|
|
}
|
|
|
|
mPromises.Clear();
|
|
|
|
|
|
|
|
mStatus = eResolved;
|
|
|
|
mWorklet = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-06 11:53:34 +03:00
|
|
|
RefPtr<Worklet> mWorklet;
|
2016-11-06 11:55:20 +03:00
|
|
|
nsTArray<RefPtr<Promise>> mPromises;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ePending,
|
|
|
|
eRejected,
|
|
|
|
eResolved
|
|
|
|
} mStatus;
|
|
|
|
|
|
|
|
nsresult mErrorStatus;
|
2016-11-06 11:54:29 +03:00
|
|
|
|
|
|
|
nsString mURL;
|
2016-11-06 11:53:34 +03:00
|
|
|
};
|
|
|
|
|
2016-11-06 11:54:29 +03:00
|
|
|
NS_IMPL_ISUPPORTS(WorkletFetchHandler, nsIStreamLoaderObserver)
|
2016-11-06 11:53:34 +03:00
|
|
|
|
2018-04-12 06:14:48 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ExecutionRunnable::Run()
|
|
|
|
{
|
|
|
|
if (WorkletThread::IsOnWorkletThread()) {
|
|
|
|
RunOnWorkletThread();
|
|
|
|
return NS_DispatchToMainThread(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
RunOnMainThread();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ExecutionRunnable::RunOnWorkletThread()
|
|
|
|
{
|
|
|
|
WorkletThread::AssertIsOnWorkletThread();
|
|
|
|
|
|
|
|
WorkletThread* workletThread = WorkletThread::Get();
|
|
|
|
MOZ_ASSERT(workletThread);
|
|
|
|
|
|
|
|
JSContext* cx = workletThread->GetJSContext();
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
|
|
|
|
|
|
|
RefPtr<WorkletGlobalScope> globalScope =
|
|
|
|
Worklet::CreateGlobalScope(jsapi.cx(), mWorkletType);
|
|
|
|
MOZ_ASSERT(globalScope);
|
|
|
|
|
|
|
|
AutoEntryScript aes(globalScope, "Worklet");
|
|
|
|
cx = aes.cx();
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> globalObj(cx, globalScope->GetGlobalJSObject());
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 url(mHandler->URL());
|
|
|
|
|
|
|
|
JS::CompileOptions compileOptions(cx);
|
|
|
|
compileOptions.setIntroductionType("Worklet");
|
|
|
|
compileOptions.setFileAndLine(url.get(), 0);
|
|
|
|
compileOptions.setIsRunOnce(true);
|
|
|
|
compileOptions.setNoScriptRval(true);
|
|
|
|
|
|
|
|
JSAutoCompartment comp(cx, globalObj);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> unused(cx);
|
|
|
|
if (!JS::Evaluate(cx, compileOptions, mBuffer, &unused)) {
|
|
|
|
ErrorResult error;
|
|
|
|
error.MightThrowJSException();
|
|
|
|
error.StealExceptionFromJSContext(cx);
|
|
|
|
mResult = error.StealNSResult();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All done.
|
|
|
|
mResult = NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ExecutionRunnable::RunOnMainThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (NS_FAILED(mResult)) {
|
|
|
|
mHandler->ExecutionFailed(mResult);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mHandler->ExecutionSucceeded();
|
|
|
|
}
|
|
|
|
|
2016-11-06 11:55:20 +03:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Worklet
|
2016-11-06 11:53:34 +03:00
|
|
|
|
2018-04-12 06:14:48 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(Worklet)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Worklet)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow)
|
|
|
|
tmp->TerminateThread();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Worklet)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(Worklet)
|
|
|
|
|
2016-11-06 11:53:34 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Worklet)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Worklet)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Worklet)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2016-12-17 00:21:51 +03:00
|
|
|
Worklet::Worklet(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal,
|
|
|
|
WorkletType aWorkletType)
|
2016-11-06 11:54:52 +03:00
|
|
|
: mWindow(aWindow)
|
2016-11-06 11:54:29 +03:00
|
|
|
, mPrincipal(aPrincipal)
|
2016-12-17 00:21:51 +03:00
|
|
|
, mWorkletType(aWorkletType)
|
2016-11-06 11:54:52 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
MOZ_ASSERT(aPrincipal);
|
2018-04-12 06:14:48 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-11-06 11:56:03 +03:00
|
|
|
|
|
|
|
#ifdef RELEASE_OR_BETA
|
|
|
|
MOZ_CRASH("This code should not go to release/beta yet!");
|
|
|
|
#endif
|
2016-11-06 11:54:52 +03:00
|
|
|
}
|
2016-11-06 11:54:29 +03:00
|
|
|
|
|
|
|
Worklet::~Worklet()
|
2018-04-12 06:14:48 +03:00
|
|
|
{
|
|
|
|
TerminateThread();
|
|
|
|
}
|
2016-11-06 11:54:29 +03:00
|
|
|
|
2016-11-06 11:53:34 +03:00
|
|
|
JSObject*
|
|
|
|
Worklet::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
|
|
{
|
2018-04-12 06:14:48 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-11-06 11:53:34 +03:00
|
|
|
return WorkletBinding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2017-02-01 23:43:37 +03:00
|
|
|
Worklet::Import(const nsAString& aModuleURL, CallerType aCallerType,
|
|
|
|
ErrorResult& aRv)
|
2016-11-06 11:53:34 +03:00
|
|
|
{
|
2018-04-12 06:14:48 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-02-01 23:43:37 +03:00
|
|
|
return WorkletFetchHandler::Fetch(this, aModuleURL, aCallerType, aRv);
|
2016-11-06 11:53:34 +03:00
|
|
|
}
|
|
|
|
|
2018-04-12 06:14:48 +03:00
|
|
|
/* static */ already_AddRefed<WorkletGlobalScope>
|
|
|
|
Worklet::CreateGlobalScope(JSContext* aCx, WorkletType aWorkletType)
|
2016-11-06 11:54:29 +03:00
|
|
|
{
|
2018-04-12 06:14:48 +03:00
|
|
|
WorkletThread::AssertIsOnWorkletThread();
|
2016-11-06 11:54:29 +03:00
|
|
|
|
2018-04-12 06:14:48 +03:00
|
|
|
RefPtr<WorkletGlobalScope> scope;
|
2016-11-06 11:54:29 +03:00
|
|
|
|
2018-04-12 06:14:48 +03:00
|
|
|
switch (aWorkletType) {
|
|
|
|
case eAudioWorklet:
|
|
|
|
scope = new AudioWorkletGlobalScope();
|
|
|
|
break;
|
|
|
|
case ePaintWorklet:
|
|
|
|
scope = new PaintWorkletGlobalScope();
|
|
|
|
break;
|
|
|
|
}
|
2016-11-06 11:54:29 +03:00
|
|
|
|
2018-04-12 06:14:48 +03:00
|
|
|
JS::Rooted<JSObject*> global(aCx);
|
|
|
|
NS_ENSURE_TRUE(scope->WrapGlobalObject(aCx, &global), nullptr);
|
|
|
|
|
|
|
|
JSAutoCompartment ac(aCx, global);
|
2016-11-06 11:54:29 +03:00
|
|
|
|
2018-04-12 06:14:48 +03:00
|
|
|
// Init Web IDL bindings
|
|
|
|
if (!RegisterWorkletBindings(aCx, global)) {
|
|
|
|
return nullptr;
|
2016-11-06 11:54:29 +03:00
|
|
|
}
|
|
|
|
|
2018-04-12 06:14:48 +03:00
|
|
|
JS_FireOnNewGlobalObject(aCx, global);
|
|
|
|
|
|
|
|
return scope.forget();
|
2016-11-06 11:54:29 +03:00
|
|
|
}
|
|
|
|
|
2016-11-06 11:55:20 +03:00
|
|
|
WorkletFetchHandler*
|
|
|
|
Worklet::GetImportFetchHandler(const nsACString& aURI)
|
|
|
|
{
|
2018-04-12 06:14:48 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-11-06 11:55:20 +03:00
|
|
|
return mImportHandlers.GetWeak(aURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Worklet::AddImportFetchHandler(const nsACString& aURI,
|
|
|
|
WorkletFetchHandler* aHandler)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aHandler);
|
|
|
|
MOZ_ASSERT(!mImportHandlers.GetWeak(aURI));
|
2018-04-12 06:14:48 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-11-06 11:55:20 +03:00
|
|
|
|
|
|
|
mImportHandlers.Put(aURI, aHandler);
|
|
|
|
}
|
|
|
|
|
2018-04-12 06:14:48 +03:00
|
|
|
WorkletThread*
|
|
|
|
Worklet::GetOrCreateThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!mWorkletThread) {
|
|
|
|
// Thread creation. FIXME: this will change.
|
|
|
|
mWorkletThread = WorkletThread::Create();
|
|
|
|
}
|
|
|
|
|
|
|
|
return mWorkletThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Worklet::TerminateThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!mWorkletThread) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mWorkletThread->Terminate();
|
|
|
|
mWorkletThread = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-06 11:53:34 +03:00
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|