1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
7 #include "WorkletImpl.h"
10 #include "WorkletThread.h"
12 #include "mozilla/BasePrincipal.h"
13 #include "mozilla/NullPrincipal.h"
14 #include "mozilla/dom/DocGroup.h"
15 #include "mozilla/dom/RegisterWorkletBindings.h"
16 #include "mozilla/dom/ScriptSettings.h"
17 #include "mozilla/dom/WorkletBinding.h"
18 #include "mozilla/dom/WorkletGlobalScope.h"
19 #include "nsGlobalWindowInner.h"
23 // ---------------------------------------------------------------------------
26 WorkletLoadInfo::WorkletLoadInfo(nsPIDOMWindowInner
* aWindow
)
27 : mInnerWindowID(aWindow
->WindowID()) {
28 MOZ_ASSERT(NS_IsMainThread());
29 nsPIDOMWindowOuter
* outerWindow
= aWindow
->GetOuterWindow();
31 mOuterWindowID
= outerWindow
->WindowID();
37 // ---------------------------------------------------------------------------
40 WorkletImpl::WorkletImpl(nsPIDOMWindowInner
* aWindow
, nsIPrincipal
* aPrincipal
)
41 : mPrincipal(NullPrincipal::CreateWithInheritedAttributes(aPrincipal
)),
42 mWorkletLoadInfo(aWindow
),
44 mFinishedOnExecutionThread(false),
45 mTrials(OriginTrials::FromWindow(nsGlobalWindowInner::Cast(aWindow
))) {
47 NS_FAILED(ipc::PrincipalToPrincipalInfo(mPrincipal
, &mPrincipalInfo
)));
49 if (aWindow
->GetDocGroup()) {
50 mAgentClusterId
.emplace(aWindow
->GetDocGroup()->AgentClusterId());
53 mSharedMemoryAllowed
=
54 nsGlobalWindowInner::Cast(aWindow
)->IsSharedMemoryAllowed();
57 WorkletImpl::~WorkletImpl() {
58 MOZ_ASSERT(!mGlobalScope
);
59 MOZ_ASSERT(!mPrincipal
|| NS_IsMainThread());
62 JSObject
* WorkletImpl::WrapWorklet(JSContext
* aCx
, dom::Worklet
* aWorklet
,
63 JS::Handle
<JSObject
*> aGivenProto
) {
64 MOZ_ASSERT(NS_IsMainThread());
65 return dom::Worklet_Binding::Wrap(aCx
, aWorklet
, aGivenProto
);
68 dom::WorkletGlobalScope
* WorkletImpl::GetGlobalScope() {
69 dom::WorkletThread::AssertIsOnWorkletThread();
74 if (mFinishedOnExecutionThread
) {
80 JSContext
* cx
= jsapi
.cx();
82 mGlobalScope
= ConstructGlobalScope();
84 JS::Rooted
<JSObject
*> global(cx
);
85 NS_ENSURE_TRUE(mGlobalScope
->WrapGlobalObject(cx
, &global
), nullptr);
87 JSAutoRealm
ar(cx
, global
);
89 // Init Web IDL bindings
90 if (!dom::RegisterWorkletBindings(cx
, global
)) {
94 JS_FireOnNewGlobalObject(cx
, global
);
99 void WorkletImpl::NotifyWorkletFinished() {
100 MOZ_ASSERT(NS_IsMainThread());
106 // Release global scope on its thread.
108 NS_NewRunnableFunction("WorkletImpl::NotifyWorkletFinished",
109 [self
= RefPtr
<WorkletImpl
>(this)]() {
110 self
->mFinishedOnExecutionThread
= true;
111 self
->mGlobalScope
= nullptr;
115 if (mWorkletThread
) {
116 mWorkletThread
->Terminate();
117 mWorkletThread
= nullptr;
119 mPrincipal
= nullptr;
122 nsresult
WorkletImpl::SendControlMessage(
123 already_AddRefed
<nsIRunnable
> aRunnable
) {
124 MOZ_ASSERT(NS_IsMainThread());
125 RefPtr
<nsIRunnable
> runnable
= std::move(aRunnable
);
127 // TODO: bug 1492011 re ConsoleWorkletRunnable.
129 return NS_ERROR_ILLEGAL_DURING_SHUTDOWN
;
132 if (!mWorkletThread
) {
133 // Thread creation. FIXME: this will change.
134 mWorkletThread
= dom::WorkletThread::Create(this);
135 if (!mWorkletThread
) {
136 return NS_ERROR_ILLEGAL_DURING_SHUTDOWN
;
140 return mWorkletThread
->DispatchRunnable(runnable
.forget());
143 } // namespace mozilla