Bug 1777562 [wpt PR 34663] - [FedCM] Rename FederatedCredential to IdentityCredential...
[gecko.git] / dom / worklet / WorkletImpl.cpp
blobcccc1d04aee7a1933bedb78d68c922e2592c23dc
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"
9 #include "Worklet.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"
21 namespace mozilla {
23 // ---------------------------------------------------------------------------
24 // WorkletLoadInfo
26 WorkletLoadInfo::WorkletLoadInfo(nsPIDOMWindowInner* aWindow)
27 : mInnerWindowID(aWindow->WindowID()) {
28 MOZ_ASSERT(NS_IsMainThread());
29 nsPIDOMWindowOuter* outerWindow = aWindow->GetOuterWindow();
30 if (outerWindow) {
31 mOuterWindowID = outerWindow->WindowID();
32 } else {
33 mOuterWindowID = 0;
37 // ---------------------------------------------------------------------------
38 // WorkletImpl
40 WorkletImpl::WorkletImpl(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal)
41 : mPrincipal(NullPrincipal::CreateWithInheritedAttributes(aPrincipal)),
42 mWorkletLoadInfo(aWindow),
43 mTerminated(false),
44 mFinishedOnExecutionThread(false),
45 mTrials(OriginTrials::FromWindow(nsGlobalWindowInner::Cast(aWindow))) {
46 Unused << NS_WARN_IF(
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();
71 if (mGlobalScope) {
72 return mGlobalScope;
74 if (mFinishedOnExecutionThread) {
75 return nullptr;
78 dom::AutoJSAPI jsapi;
79 jsapi.Init();
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)) {
91 return nullptr;
94 JS_FireOnNewGlobalObject(cx, global);
96 return mGlobalScope;
99 void WorkletImpl::NotifyWorkletFinished() {
100 MOZ_ASSERT(NS_IsMainThread());
102 if (mTerminated) {
103 return;
106 // Release global scope on its thread.
107 SendControlMessage(
108 NS_NewRunnableFunction("WorkletImpl::NotifyWorkletFinished",
109 [self = RefPtr<WorkletImpl>(this)]() {
110 self->mFinishedOnExecutionThread = true;
111 self->mGlobalScope = nullptr;
112 }));
114 mTerminated = true;
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.
128 if (mTerminated) {
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