Serenity Operating System
1/*
2 * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibWeb/Bindings/Intrinsics.h>
8#include <LibWeb/DOM/EventDispatcher.h>
9#include <LibWeb/HTML/EventHandler.h>
10#include <LibWeb/HTML/EventLoop/EventLoop.h>
11#include <LibWeb/HTML/EventNames.h>
12#include <LibWeb/HTML/MessageEvent.h>
13#include <LibWeb/HTML/MessagePort.h>
14
15namespace Web::HTML {
16
17WebIDL::ExceptionOr<JS::NonnullGCPtr<MessagePort>> MessagePort::create(JS::Realm& realm)
18{
19 return MUST_OR_THROW_OOM(realm.heap().allocate<MessagePort>(realm, realm));
20}
21
22MessagePort::MessagePort(JS::Realm& realm)
23 : DOM::EventTarget(realm)
24{
25}
26
27MessagePort::~MessagePort() = default;
28
29JS::ThrowCompletionOr<void> MessagePort::initialize(JS::Realm& realm)
30{
31 MUST_OR_THROW_OOM(Base::initialize(realm));
32 set_prototype(&Bindings::ensure_web_prototype<Bindings::MessagePortPrototype>(realm, "MessagePort"));
33
34 return {};
35}
36
37void MessagePort::visit_edges(Cell::Visitor& visitor)
38{
39 Base::visit_edges(visitor);
40 visitor.visit(m_remote_port.ptr());
41}
42
43void MessagePort::disentangle()
44{
45 m_remote_port->m_remote_port = nullptr;
46 m_remote_port = nullptr;
47}
48
49// https://html.spec.whatwg.org/multipage/web-messaging.html#entangle
50void MessagePort::entangle_with(MessagePort& remote_port)
51{
52 if (m_remote_port.ptr() == &remote_port)
53 return;
54
55 // 1. If one of the ports is already entangled, then disentangle it and the port that it was entangled with.
56 if (is_entangled())
57 disentangle();
58 if (remote_port.is_entangled())
59 remote_port.disentangle();
60
61 // 2. Associate the two ports to be entangled, so that they form the two parts of a new channel.
62 // (There is no MessageChannel object that represents this channel.)
63 remote_port.m_remote_port = this;
64 m_remote_port = &remote_port;
65}
66
67// https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-postmessage
68void MessagePort::post_message(JS::Value message)
69{
70 // 1. Let targetPort be the port with which this MessagePort is entangled, if any; otherwise let it be null.
71 auto* target_port = m_remote_port.ptr();
72
73 // FIXME: 2. Let options be «[ "transfer" → transfer ]».
74
75 // 3. Run the message port post message steps providing targetPort, message and options.
76
77 // https://html.spec.whatwg.org/multipage/web-messaging.html#message-port-post-message-steps
78
79 // FIXME: 1. Let transfer be options["transfer"].
80
81 // FIXME: 2. If transfer contains this MessagePort, then throw a "DataCloneError" DOMException.
82
83 // 3. Let doomed be false.
84 bool doomed = false;
85
86 // FIXME: 4. If targetPort is not null and transfer contains targetPort, then set doomed to true and optionally report to a developer console that the target port was posted to itself, causing the communication channel to be lost.
87
88 // FIXME: 5. Let serializeWithTransferResult be StructuredSerializeWithTransfer(message, transfer). Rethrow any exceptions.
89
90 // 6. If targetPort is null, or if doomed is true, then return.
91 if (!target_port || doomed)
92 return;
93
94 // FIXME: 7. Add a task that runs the following steps to the port message queue of targetPort:
95
96 // FIXME: This is an ad-hoc hack implementation instead, since we don't currently
97 // have serialization and deserialization of messages.
98 main_thread_event_loop().task_queue().add(HTML::Task::create(HTML::Task::Source::PostedMessage, nullptr, [target_port, message] {
99 MessageEventInit event_init {};
100 event_init.data = message;
101 event_init.origin = "<origin>"_string.release_value_but_fixme_should_propagate_errors();
102 target_port->dispatch_event(MessageEvent::create(target_port->realm(), String::from_deprecated_string(HTML::EventNames::message).release_value_but_fixme_should_propagate_errors(), event_init).release_value_but_fixme_should_propagate_errors());
103 }));
104}
105
106void MessagePort::start()
107{
108 // FIXME: Message ports are supposed to be disabled by default.
109}
110
111// https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-close
112void MessagePort::close()
113{
114 // 1. Set this MessagePort object's [[Detached]] internal slot value to true.
115 m_detached = true;
116
117 // 2. If this MessagePort object is entangled, disentangle it.
118 if (is_entangled())
119 disentangle();
120}
121
122#undef __ENUMERATE
123#define __ENUMERATE(attribute_name, event_name) \
124 void MessagePort::set_##attribute_name(WebIDL::CallbackType* value) \
125 { \
126 set_event_handler_attribute(event_name, value); \
127 } \
128 WebIDL::CallbackType* MessagePort::attribute_name() \
129 { \
130 return event_handler_attribute(event_name); \
131 }
132ENUMERATE_MESSAGE_PORT_EVENT_HANDLERS(__ENUMERATE)
133#undef __ENUMERATE
134
135}