Serenity Operating System
1/*
2 * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/RefCounted.h>
10#include <AK/Weakable.h>
11#include <LibWeb/DOM/EventTarget.h>
12#include <LibWeb/Forward.h>
13
14namespace Web::HTML {
15
16#define ENUMERATE_MESSAGE_PORT_EVENT_HANDLERS(E) \
17 E(onmessage, HTML::EventNames::message) \
18 E(onmessageerror, HTML::EventNames::messageerror)
19
20// https://html.spec.whatwg.org/multipage/web-messaging.html#structuredserializeoptions
21struct StructuredSerializeOptions {
22 Vector<JS::Handle<JS::Object>> transfer;
23};
24
25// https://html.spec.whatwg.org/multipage/web-messaging.html#message-ports
26class MessagePort final : public DOM::EventTarget {
27 WEB_PLATFORM_OBJECT(MessagePort, DOM::EventTarget);
28
29public:
30 static WebIDL::ExceptionOr<JS::NonnullGCPtr<MessagePort>> create(JS::Realm&);
31
32 virtual ~MessagePort() override;
33
34 // https://html.spec.whatwg.org/multipage/web-messaging.html#entangle
35 void entangle_with(MessagePort&);
36
37 // https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-postmessage
38 void post_message(JS::Value);
39
40 void start();
41
42 void close();
43
44#undef __ENUMERATE
45#define __ENUMERATE(attribute_name, event_name) \
46 void set_##attribute_name(WebIDL::CallbackType*); \
47 WebIDL::CallbackType* attribute_name();
48 ENUMERATE_MESSAGE_PORT_EVENT_HANDLERS(__ENUMERATE)
49#undef __ENUMERATE
50
51private:
52 explicit MessagePort(JS::Realm&);
53
54 virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
55 virtual void visit_edges(Cell::Visitor&) override;
56
57 bool is_entangled() const { return m_remote_port; }
58 void disentangle();
59
60 // The HTML spec implies(!) that this is MessagePort.[[RemotePort]]
61 JS::GCPtr<MessagePort> m_remote_port;
62
63 // https://html.spec.whatwg.org/multipage/web-messaging.html#has-been-shipped
64 bool m_has_been_shipped { false };
65
66 // This is TransferableObject.[[Detached]]
67 bool m_detached { false };
68};
69
70}