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 http://mozilla.org/MPL/2.0/. */
7 #include "MessageEventRunnable.h"
9 #include "mozilla/dom/MessageEvent.h"
10 #include "mozilla/dom/MessageEventBinding.h"
11 #include "mozilla/dom/RootedDictionary.h"
12 #include "nsQueryObject.h"
13 #include "WorkerScope.h"
15 namespace mozilla::dom
{
17 MessageEventRunnable::MessageEventRunnable(WorkerPrivate
* aWorkerPrivate
,
19 : WorkerDebuggeeRunnable(aWorkerPrivate
, "MessageEventRunnable", aTarget
),
20 StructuredCloneHolder(CloningSupported
, TransferringSupported
,
21 StructuredCloneScope::SameProcess
) {}
23 bool MessageEventRunnable::DispatchDOMEvent(JSContext
* aCx
,
24 WorkerPrivate
* aWorkerPrivate
,
25 DOMEventTargetHelper
* aTarget
,
27 nsCOMPtr
<nsIGlobalObject
> parent
= aTarget
->GetParentObject();
29 // For some workers without window, parent is null and we try to find it
30 // from the JS Context.
32 JS::Rooted
<JSObject
*> globalObject(aCx
, JS::CurrentGlobalOrNull(aCx
));
33 if (NS_WARN_IF(!globalObject
)) {
37 parent
= xpc::NativeGlobal(globalObject
);
38 if (NS_WARN_IF(!parent
)) {
45 JS::Rooted
<JS::Value
> messageData(aCx
);
46 IgnoredErrorResult rv
;
48 JS::CloneDataPolicy cloneDataPolicy
;
49 if (parent
->GetClientInfo().isSome() &&
50 parent
->GetClientInfo()->AgentClusterId().isSome() &&
51 parent
->GetClientInfo()->AgentClusterId()->Equals(
52 aWorkerPrivate
->AgentClusterId())) {
53 cloneDataPolicy
.allowIntraClusterClonableSharedObjects();
56 if (aWorkerPrivate
->IsSharedMemoryAllowed()) {
57 cloneDataPolicy
.allowSharedMemoryObjects();
60 Read(parent
, aCx
, &messageData
, cloneDataPolicy
, rv
);
62 if (NS_WARN_IF(rv
.Failed())) {
63 DispatchError(aCx
, aTarget
);
67 Sequence
<OwningNonNull
<MessagePort
>> ports
;
68 if (!TakeTransferredPortsAsSequence(ports
)) {
69 DispatchError(aCx
, aTarget
);
73 RefPtr
<MessageEvent
> event
= new MessageEvent(aTarget
, nullptr, nullptr);
74 event
->InitMessageEvent(nullptr, u
"message"_ns
, CanBubble::eNo
,
75 Cancelable::eNo
, messageData
, u
""_ns
, u
""_ns
, nullptr,
78 event
->SetTrusted(true);
80 aTarget
->DispatchEvent(*event
);
85 bool MessageEventRunnable::WorkerRun(JSContext
* aCx
,
86 WorkerPrivate
* aWorkerPrivate
) {
87 if (mTarget
== ParentThread
) {
88 // Don't fire this event if the JS object has been disconnected from the
90 if (!aWorkerPrivate
->IsAcceptingEvents()) {
94 aWorkerPrivate
->AssertInnerWindowIsCorrect();
96 return DispatchDOMEvent(aCx
, aWorkerPrivate
,
97 aWorkerPrivate
->ParentEventTargetRef(),
98 !aWorkerPrivate
->GetParent());
101 MOZ_ASSERT(aWorkerPrivate
== GetWorkerPrivateFromContext(aCx
));
102 MOZ_ASSERT(aWorkerPrivate
->GlobalScope());
104 // If the worker start shutting down, don't dispatch the message event.
106 aWorkerPrivate
->GlobalScope()->CheckCurrentGlobalCorrectness())) {
110 return DispatchDOMEvent(aCx
, aWorkerPrivate
, aWorkerPrivate
->GlobalScope(),
114 void MessageEventRunnable::DispatchError(JSContext
* aCx
,
115 DOMEventTargetHelper
* aTarget
) {
116 RootedDictionary
<MessageEventInit
> init(aCx
);
117 init
.mBubbles
= false;
118 init
.mCancelable
= false;
120 RefPtr
<Event
> event
=
121 MessageEvent::Constructor(aTarget
, u
"messageerror"_ns
, init
);
122 event
->SetTrusted(true);
124 aTarget
->DispatchEvent(*event
);
127 } // namespace mozilla::dom