2022-08-08 22:29:40 +02:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
2024-07-09 19:43:51 +01:00
|
|
|
|
* Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
|
2022-08-08 22:29:40 +02:00
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2023-12-12 13:28:38 -07:00
|
|
|
|
#include <LibJS/Runtime/Array.h>
|
2022-09-25 16:38:21 -06:00
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2024-04-27 12:09:58 +12:00
|
|
|
|
#include <LibWeb/Bindings/MessageEventPrototype.h>
|
2022-08-08 22:29:40 +02:00
|
|
|
|
#include <LibWeb/HTML/MessageEvent.h>
|
2024-05-15 20:58:02 +01:00
|
|
|
|
#include <LibWeb/HTML/MessagePort.h>
|
2022-08-08 22:29:40 +02:00
|
|
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
|
2023-11-19 19:47:52 +01:00
|
|
|
|
JS_DEFINE_ALLOCATOR(MessageEvent);
|
|
|
|
|
|
2023-08-13 13:05:26 +02:00
|
|
|
|
JS::NonnullGCPtr<MessageEvent> MessageEvent::create(JS::Realm& realm, FlyString const& event_name, MessageEventInit const& event_init)
|
2022-08-08 22:29:40 +02:00
|
|
|
|
{
|
2023-08-13 13:05:26 +02:00
|
|
|
|
return realm.heap().allocate<MessageEvent>(realm, realm, event_name, event_init);
|
2022-08-08 22:29:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-05 10:33:56 +01:00
|
|
|
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<MessageEvent>> MessageEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, MessageEventInit const& event_init)
|
2022-09-25 16:38:21 -06:00
|
|
|
|
{
|
|
|
|
|
return create(realm, event_name, event_init);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-05 10:33:56 +01:00
|
|
|
|
MessageEvent::MessageEvent(JS::Realm& realm, FlyString const& event_name, MessageEventInit const& event_init)
|
2023-04-06 16:12:33 +02:00
|
|
|
|
: DOM::Event(realm, event_name, event_init)
|
2022-08-08 22:29:40 +02:00
|
|
|
|
, m_data(event_init.data)
|
|
|
|
|
, m_origin(event_init.origin)
|
|
|
|
|
, m_last_event_id(event_init.last_event_id)
|
2023-11-06 20:11:58 +00:00
|
|
|
|
, m_source(event_init.source)
|
2022-08-08 22:29:40 +02:00
|
|
|
|
{
|
2024-01-24 08:56:59 +01:00
|
|
|
|
m_ports.ensure_capacity(event_init.ports.size());
|
2024-05-15 20:58:02 +01:00
|
|
|
|
for (auto const& port : event_init.ports) {
|
2024-01-24 08:56:59 +01:00
|
|
|
|
VERIFY(port);
|
2024-05-15 20:58:02 +01:00
|
|
|
|
m_ports.unchecked_append(static_cast<JS::Object&>(*port));
|
2024-01-24 08:56:59 +01:00
|
|
|
|
}
|
2022-08-08 22:29:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MessageEvent::~MessageEvent() = default;
|
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
|
void MessageEvent::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
|
{
|
2023-08-07 08:41:28 +02:00
|
|
|
|
Base::initialize(realm);
|
2024-03-16 13:13:08 +01:00
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(MessageEvent);
|
2023-01-10 06:28:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 22:29:40 +02:00
|
|
|
|
void MessageEvent::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
visitor.visit(m_data);
|
2024-01-24 08:56:59 +01:00
|
|
|
|
visitor.visit(m_ports_array);
|
2024-04-15 13:58:21 +02:00
|
|
|
|
visitor.visit(m_ports);
|
2022-08-08 22:29:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 20:11:58 +00:00
|
|
|
|
Variant<JS::Handle<WindowProxy>, JS::Handle<MessagePort>, Empty> MessageEvent::source() const
|
|
|
|
|
{
|
|
|
|
|
if (!m_source.has_value())
|
|
|
|
|
return Empty {};
|
|
|
|
|
|
|
|
|
|
return m_source.value().downcast<JS::Handle<WindowProxy>, JS::Handle<MessagePort>>();
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-12 13:28:38 -07:00
|
|
|
|
JS::NonnullGCPtr<JS::Object> MessageEvent::ports() const
|
|
|
|
|
{
|
|
|
|
|
if (!m_ports_array) {
|
|
|
|
|
Vector<JS::Value> port_vector;
|
|
|
|
|
for (auto const& port : m_ports) {
|
2023-12-18 14:01:39 -07:00
|
|
|
|
port_vector.append(port);
|
2023-12-12 13:28:38 -07:00
|
|
|
|
}
|
|
|
|
|
m_ports_array = JS::Array::create_from(realm(), port_vector);
|
|
|
|
|
MUST(m_ports_array->set_integrity_level(IntegrityLevel::Frozen));
|
|
|
|
|
}
|
|
|
|
|
return *m_ports_array;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-09 19:43:51 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/comms.html#dom-messageevent-initmessageevent
|
|
|
|
|
void MessageEvent::init_message_event(String const& type, bool bubbles, bool cancelable, JS::Value data, String const& origin, String const& last_event_id, Optional<MessageEventSource> source, Vector<JS::Handle<MessagePort>> const& ports)
|
|
|
|
|
{
|
|
|
|
|
// The initMessageEvent(type, bubbles, cancelable, data, origin, lastEventId, source, ports) method must initialize the event in a
|
|
|
|
|
// manner analogous to the similarly-named initEvent() method.
|
|
|
|
|
|
|
|
|
|
// 1. If this’s dispatch flag is set, then return.
|
|
|
|
|
if (dispatched())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// 2. Initialize this with type, bubbles, and cancelable.
|
|
|
|
|
initialize_event(type, bubbles, cancelable);
|
|
|
|
|
|
|
|
|
|
// Implementation Defined: Initialise other values.
|
|
|
|
|
m_data = data;
|
|
|
|
|
m_origin = origin;
|
|
|
|
|
m_last_event_id = last_event_id;
|
|
|
|
|
m_source = source;
|
|
|
|
|
m_ports.clear();
|
|
|
|
|
m_ports.ensure_capacity(ports.size());
|
|
|
|
|
for (auto const& port : ports) {
|
|
|
|
|
VERIFY(port);
|
|
|
|
|
m_ports.unchecked_append(static_cast<JS::Object&>(*port));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 22:29:40 +02:00
|
|
|
|
}
|