ladybird/Userland/Libraries/LibWeb/HTML/MessagePort.h
Andreas Kling 6f433c8656 LibWeb+LibJS: Make the EventTarget hierarchy (incl. DOM) GC-allocated
This is a monster patch that turns all EventTargets into GC-allocated
PlatformObjects. Their C++ wrapper classes are removed, and the LibJS
garbage collector is now responsible for their lifetimes.

There's a fair amount of hacks and band-aids in this patch, and we'll
have a lot of cleanup to do after this.
2022-09-06 00:27:09 +02:00

67 lines
1.8 KiB
C++

/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/RefCounted.h>
#include <AK/Weakable.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/Forward.h>
namespace Web::HTML {
#define ENUMERATE_MESSAGE_PORT_EVENT_HANDLERS(E) \
E(onmessage, HTML::EventNames::message) \
E(onmessageerror, HTML::EventNames::messageerror)
// https://html.spec.whatwg.org/multipage/web-messaging.html#message-ports
class MessagePort final : public DOM::EventTarget {
WEB_PLATFORM_OBJECT(MessagePort, DOM::EventTarget);
public:
static JS::NonnullGCPtr<MessagePort> create(HTML::Window&);
virtual ~MessagePort() override;
// https://html.spec.whatwg.org/multipage/web-messaging.html#entangle
void entangle_with(MessagePort&);
// https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-postmessage
void post_message(JS::Value);
void start();
void close();
#undef __ENUMERATE
#define __ENUMERATE(attribute_name, event_name) \
void set_##attribute_name(Bindings::CallbackType*); \
Bindings::CallbackType* attribute_name();
ENUMERATE_MESSAGE_PORT_EVENT_HANDLERS(__ENUMERATE)
#undef __ENUMERATE
private:
explicit MessagePort(HTML::Window&);
virtual void visit_edges(Cell::Visitor&) override;
bool is_entangled() const { return m_remote_port; }
void disentangle();
// The HTML spec implies(!) that this is MessagePort.[[RemotePort]]
JS::GCPtr<MessagePort> m_remote_port;
// https://html.spec.whatwg.org/multipage/web-messaging.html#has-been-shipped
bool m_has_been_shipped { false };
// This is TransferableObject.[[Detached]]
bool m_detached { false };
};
}
WRAPPER_HACK(MessagePort, Web::HTML)