2018-09-18 12:36:38 +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 https://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "WorkletImpl.h"
|
|
|
|
|
|
|
|
#include "Worklet.h"
|
|
|
|
#include "WorkletThread.h"
|
|
|
|
|
|
|
|
#include "mozilla/BasePrincipal.h"
|
|
|
|
#include "mozilla/dom/RegisterWorkletBindings.h"
|
2019-04-01 19:54:58 +03:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2018-09-18 12:36:38 +03:00
|
|
|
#include "mozilla/dom/WorkletBinding.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// WorkletLoadInfo
|
|
|
|
|
|
|
|
WorkletLoadInfo::WorkletLoadInfo(nsPIDOMWindowInner* aWindow,
|
|
|
|
nsIPrincipal* aPrincipal)
|
|
|
|
: mInnerWindowID(aWindow->WindowID()),
|
|
|
|
mOriginAttributes(BasePrincipal::Cast(aPrincipal)->OriginAttributesRef()),
|
|
|
|
mPrincipal(aPrincipal) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
nsPIDOMWindowOuter* outerWindow = aWindow->GetOuterWindow();
|
|
|
|
if (outerWindow) {
|
|
|
|
mOuterWindowID = outerWindow->WindowID();
|
|
|
|
} else {
|
|
|
|
mOuterWindowID = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkletLoadInfo::~WorkletLoadInfo() {
|
2018-09-18 12:34:11 +03:00
|
|
|
MOZ_ASSERT(!mPrincipal || NS_IsMainThread());
|
2018-09-18 12:36:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// WorkletImpl
|
|
|
|
|
|
|
|
WorkletImpl::WorkletImpl(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal)
|
2018-11-29 02:47:01 +03:00
|
|
|
: mWorkletLoadInfo(aWindow, aPrincipal), mTerminated(false) {}
|
2018-09-18 12:36:38 +03:00
|
|
|
|
2019-04-01 19:54:58 +03:00
|
|
|
WorkletImpl::~WorkletImpl() { MOZ_ASSERT(!mGlobalScope); }
|
2018-09-18 12:36:38 +03:00
|
|
|
|
|
|
|
JSObject* WorkletImpl::WrapWorklet(JSContext* aCx, dom::Worklet* aWorklet,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2018-09-20 14:34:01 +03:00
|
|
|
return dom::Worklet_Binding::Wrap(aCx, aWorklet, aGivenProto);
|
2018-09-18 12:36:38 +03:00
|
|
|
}
|
|
|
|
|
2019-04-01 19:54:58 +03:00
|
|
|
dom::WorkletGlobalScope* WorkletImpl::GetGlobalScope() {
|
2018-09-18 12:36:38 +03:00
|
|
|
dom::WorkletThread::AssertIsOnWorkletThread();
|
|
|
|
|
2019-04-01 19:54:58 +03:00
|
|
|
if (mGlobalScope) {
|
|
|
|
return mGlobalScope;
|
|
|
|
}
|
|
|
|
|
|
|
|
dom::AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
|
|
|
JSContext* cx = jsapi.cx();
|
2018-09-18 12:36:38 +03:00
|
|
|
|
2019-04-01 19:54:58 +03:00
|
|
|
mGlobalScope = ConstructGlobalScope();
|
2018-09-18 12:36:38 +03:00
|
|
|
|
2019-04-01 19:54:58 +03:00
|
|
|
JS::Rooted<JSObject*> global(cx);
|
|
|
|
NS_ENSURE_TRUE(mGlobalScope->WrapGlobalObject(cx, &global), nullptr);
|
|
|
|
|
|
|
|
JSAutoRealm ar(cx, global);
|
2018-09-18 12:36:38 +03:00
|
|
|
|
|
|
|
// Init Web IDL bindings
|
2019-04-01 19:54:58 +03:00
|
|
|
if (!dom::RegisterWorkletBindings(cx, global)) {
|
2018-09-18 12:36:38 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-04-01 19:54:58 +03:00
|
|
|
JS_FireOnNewGlobalObject(cx, global);
|
2018-09-18 12:36:38 +03:00
|
|
|
|
2019-04-01 19:54:58 +03:00
|
|
|
return mGlobalScope;
|
2018-09-18 12:36:38 +03:00
|
|
|
}
|
|
|
|
|
2018-11-29 02:45:04 +03:00
|
|
|
void WorkletImpl::NotifyWorkletFinished() {
|
2018-09-18 12:36:38 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2019-04-01 19:54:58 +03:00
|
|
|
if (mTerminated) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release global scope on its thread.
|
|
|
|
SendControlMessage(NS_NewRunnableFunction(
|
|
|
|
"WorkletImpl::NotifyWorkletFinished",
|
|
|
|
[self = RefPtr<WorkletImpl>(this)]() { self->mGlobalScope = nullptr; }));
|
|
|
|
|
2018-11-29 02:47:01 +03:00
|
|
|
mTerminated = true;
|
2018-11-29 02:45:04 +03:00
|
|
|
if (mWorkletThread) {
|
|
|
|
mWorkletThread->Terminate();
|
|
|
|
mWorkletThread = nullptr;
|
|
|
|
}
|
2018-09-18 12:36:38 +03:00
|
|
|
mWorkletLoadInfo.mPrincipal = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-29 02:47:01 +03:00
|
|
|
nsresult WorkletImpl::SendControlMessage(
|
|
|
|
already_AddRefed<nsIRunnable> aRunnable) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2018-09-18 12:29:15 +03:00
|
|
|
// TODO: bug 1492011 re ConsoleWorkletRunnable.
|
2018-11-29 02:47:01 +03:00
|
|
|
if (mTerminated) {
|
|
|
|
return NS_ERROR_ILLEGAL_DURING_SHUTDOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mWorkletThread) {
|
|
|
|
// Thread creation. FIXME: this will change.
|
2019-04-01 19:51:18 +03:00
|
|
|
mWorkletThread = dom::WorkletThread::Create(this);
|
2018-11-29 02:47:01 +03:00
|
|
|
if (!mWorkletThread) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 12:29:15 +03:00
|
|
|
return mWorkletThread->DispatchRunnable(std::move(aRunnable));
|
|
|
|
}
|
|
|
|
|
2018-09-18 12:36:38 +03:00
|
|
|
} // namespace mozilla
|