2022-08-08 17:29:40 -03:00
|
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
|
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
|
2024-07-09 15:43:51 -03:00
|
|
|
|
* Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
|
2022-08-08 17:29:40 -03:00
|
|
|
|
*
|
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2023-12-12 17:28:38 -03:00
|
|
|
|
#include <LibJS/Runtime/Array.h>
|
2022-09-25 19:38:21 -03:00
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2024-04-26 21:09:58 -03:00
|
|
|
|
#include <LibWeb/Bindings/MessageEventPrototype.h>
|
2022-08-08 17:29:40 -03:00
|
|
|
|
#include <LibWeb/HTML/MessageEvent.h>
|
2024-05-15 16:58:02 -03:00
|
|
|
|
#include <LibWeb/HTML/MessagePort.h>
|
2022-08-08 17:29:40 -03:00
|
|
|
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
|
GC_DEFINE_ALLOCATOR(MessageEvent);
|
2023-11-19 15:47:52 -03:00
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
|
GC::Ref<MessageEvent> MessageEvent::create(JS::Realm& realm, FlyString const& event_name, MessageEventInit const& event_init)
|
2022-08-08 17:29:40 -03:00
|
|
|
|
{
|
2024-11-13 13:50:17 -03:00
|
|
|
|
return realm.create<MessageEvent>(realm, event_name, event_init);
|
2022-08-08 17:29:40 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
|
WebIDL::ExceptionOr<GC::Ref<MessageEvent>> MessageEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, MessageEventInit const& event_init)
|
2022-09-25 19:38:21 -03:00
|
|
|
|
{
|
|
|
|
|
|
return create(realm, event_name, event_init);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-05 06:33:56 -03:00
|
|
|
|
MessageEvent::MessageEvent(JS::Realm& realm, FlyString const& event_name, MessageEventInit const& event_init)
|
2023-04-06 11:12:33 -03:00
|
|
|
|
: DOM::Event(realm, event_name, event_init)
|
2022-08-08 17:29:40 -03:00
|
|
|
|
, m_data(event_init.data)
|
|
|
|
|
|
, m_origin(event_init.origin)
|
|
|
|
|
|
, m_last_event_id(event_init.last_event_id)
|
2023-11-06 17:11:58 -03:00
|
|
|
|
, m_source(event_init.source)
|
2022-08-08 17:29:40 -03:00
|
|
|
|
{
|
2024-01-24 04:56:59 -03:00
|
|
|
|
m_ports.ensure_capacity(event_init.ports.size());
|
2024-05-15 16:58:02 -03:00
|
|
|
|
for (auto const& port : event_init.ports) {
|
2024-01-24 04:56:59 -03:00
|
|
|
|
VERIFY(port);
|
2024-05-15 16:58:02 -03:00
|
|
|
|
m_ports.unchecked_append(static_cast<JS::Object&>(*port));
|
2024-01-24 04:56:59 -03:00
|
|
|
|
}
|
2022-08-08 17:29:40 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MessageEvent::~MessageEvent() = default;
|
|
|
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
|
void MessageEvent::initialize(JS::Realm& realm)
|
2023-01-10 08:28:20 -03:00
|
|
|
|
{
|
2023-08-07 03:41:28 -03:00
|
|
|
|
Base::initialize(realm);
|
2024-03-16 09:13:08 -03:00
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(MessageEvent);
|
2023-01-10 08:28:20 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-08 17:29:40 -03:00
|
|
|
|
void MessageEvent::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
|
{
|
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
|
visitor.visit(m_data);
|
2024-01-24 04:56:59 -03:00
|
|
|
|
visitor.visit(m_ports_array);
|
2024-04-15 08:58:21 -03:00
|
|
|
|
visitor.visit(m_ports);
|
2022-08-08 17:29:40 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
|
Variant<GC::Root<WindowProxy>, GC::Root<MessagePort>, Empty> MessageEvent::source() const
|
2023-11-06 17:11:58 -03:00
|
|
|
|
{
|
|
|
|
|
|
if (!m_source.has_value())
|
|
|
|
|
|
return Empty {};
|
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
|
return m_source.value().downcast<GC::Root<WindowProxy>, GC::Root<MessagePort>>();
|
2023-11-06 17:11:58 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
|
GC::Ref<JS::Object> MessageEvent::ports() const
|
2023-12-12 17:28:38 -03:00
|
|
|
|
{
|
|
|
|
|
|
if (!m_ports_array) {
|
2024-12-26 10:32:52 -03:00
|
|
|
|
GC::RootVector<JS::Value> port_vector(heap());
|
2024-10-31 13:35:14 -03:00
|
|
|
|
for (auto const& port : m_ports)
|
2023-12-18 18:01:39 -03:00
|
|
|
|
port_vector.append(port);
|
2024-10-31 13:35:14 -03:00
|
|
|
|
|
2023-12-12 17:28:38 -03: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 15:43:51 -03:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/comms.html#dom-messageevent-initmessageevent
|
2024-11-14 12:01:23 -03:00
|
|
|
|
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<GC::Root<MessagePort>> const& ports)
|
2024-07-09 15:43:51 -03:00
|
|
|
|
{
|
|
|
|
|
|
// 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 17:29:40 -03:00
|
|
|
|
}
|