Serenity Operating System
1/*
2 * Copyright (c) 2021-2022, 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/Document.h>
9#include <LibWeb/HTML/MessageChannel.h>
10#include <LibWeb/HTML/MessagePort.h>
11
12namespace Web::HTML {
13
14WebIDL::ExceptionOr<JS::NonnullGCPtr<MessageChannel>> MessageChannel::construct_impl(JS::Realm& realm)
15{
16 return MUST_OR_THROW_OOM(realm.heap().allocate<MessageChannel>(realm, realm));
17}
18
19MessageChannel::MessageChannel(JS::Realm& realm)
20 : PlatformObject(realm)
21{
22 // 1. Set this's port 1 to a new MessagePort in this's relevant Realm.
23 m_port1 = MessagePort::create(realm).release_value_but_fixme_should_propagate_errors();
24
25 // 2. Set this's port 2 to a new MessagePort in this's relevant Realm.
26 m_port2 = MessagePort::create(realm).release_value_but_fixme_should_propagate_errors();
27
28 // 3. Entangle this's port 1 and this's port 2.
29 m_port1->entangle_with(*m_port2);
30}
31
32MessageChannel::~MessageChannel() = default;
33
34void MessageChannel::visit_edges(Cell::Visitor& visitor)
35{
36 Base::visit_edges(visitor);
37 visitor.visit(m_port1.ptr());
38 visitor.visit(m_port2.ptr());
39}
40
41JS::ThrowCompletionOr<void> MessageChannel::initialize(JS::Realm& realm)
42{
43 MUST_OR_THROW_OOM(Base::initialize(realm));
44 set_prototype(&Bindings::ensure_web_prototype<Bindings::MessageChannelPrototype>(realm, "MessageChannel"));
45
46 return {};
47}
48
49MessagePort* MessageChannel::port1()
50{
51 return m_port1;
52}
53
54MessagePort* MessageChannel::port2()
55{
56 return m_port2;
57}
58
59MessagePort const* MessageChannel::port1() const
60{
61 return m_port1;
62}
63
64MessagePort const* MessageChannel::port2() const
65{
66 return m_port2;
67}
68
69}