Bug 1879774 [wpt PR 44524] - WebKit export: Implement field-sizing support for input...
[gecko.git] / dom / workers / MessageEventRunnable.cpp
blob8edc7037cd936ceb979f7f7b359625ffd319f090
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,
18 Target aTarget)
19 : WorkerDebuggeeRunnable(aWorkerPrivate, "MessageEventRunnable", aTarget),
20 StructuredCloneHolder(CloningSupported, TransferringSupported,
21 StructuredCloneScope::SameProcess) {}
23 bool MessageEventRunnable::DispatchDOMEvent(JSContext* aCx,
24 WorkerPrivate* aWorkerPrivate,
25 DOMEventTargetHelper* aTarget,
26 bool aIsMainThread) {
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.
31 if (!parent) {
32 JS::Rooted<JSObject*> globalObject(aCx, JS::CurrentGlobalOrNull(aCx));
33 if (NS_WARN_IF(!globalObject)) {
34 return false;
37 parent = xpc::NativeGlobal(globalObject);
38 if (NS_WARN_IF(!parent)) {
39 return false;
43 MOZ_ASSERT(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);
64 return false;
67 Sequence<OwningNonNull<MessagePort>> ports;
68 if (!TakeTransferredPortsAsSequence(ports)) {
69 DispatchError(aCx, aTarget);
70 return false;
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,
76 ports);
78 event->SetTrusted(true);
80 aTarget->DispatchEvent(*event);
82 return true;
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
89 // private object.
90 if (!aWorkerPrivate->IsAcceptingEvents()) {
91 return true;
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.
105 if (NS_FAILED(
106 aWorkerPrivate->GlobalScope()->CheckCurrentGlobalCorrectness())) {
107 return true;
110 return DispatchDOMEvent(aCx, aWorkerPrivate, aWorkerPrivate->GlobalScope(),
111 false);
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