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>
|
2026-02-16 11:28:08 -03:00
|
|
|
|
* Copyright (c) 2026, Sam Atkins <sam@ladybird.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>
|
2026-04-18 05:54:06 -03:00
|
|
|
|
#include <LibWeb/Bindings/MessageEvent.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>
|
2026-02-16 11:28:08 -03:00
|
|
|
|
#include <LibWeb/HTML/WindowProxy.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
|
|
|
|
|
2026-05-01 14:01:23 -03:00
|
|
|
|
GC::Ref<MessageEvent> MessageEvent::create(JS::Realm& realm, FlyString const& event_name, Bindings::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
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-01 14:01:23 -03:00
|
|
|
|
GC::Ref<MessageEvent> MessageEvent::create(JS::Realm& realm, FlyString const& event_name, Bindings::MessageEventInit const& event_init, URL::Origin const& origin)
|
|
|
|
|
|
{
|
|
|
|
|
|
return realm.create<MessageEvent>(realm, event_name, event_init, origin);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
WebIDL::ExceptionOr<GC::Ref<MessageEvent>> MessageEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, Bindings::MessageEventInit const& event_init)
|
2022-09-25 19:38:21 -03:00
|
|
|
|
{
|
|
|
|
|
|
return create(realm, event_name, event_init);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-17 11:57:03 -03:00
|
|
|
|
MessageEvent::MessageEventSourceInternal MessageEvent::to_message_event_source_internal(NullableMessageEventSource const& source)
|
2026-02-16 11:28:08 -03:00
|
|
|
|
{
|
2026-02-17 11:57:03 -03:00
|
|
|
|
return source.visit(
|
|
|
|
|
|
[](Empty) -> MessageEventSourceInternal { return Empty {}; },
|
|
|
|
|
|
[](auto const& root) -> MessageEventSourceInternal { return GC::Ref { *root }; });
|
2026-02-16 11:28:08 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-01 14:01:23 -03:00
|
|
|
|
MessageEvent::MessageEvent(JS::Realm& realm, FlyString const& event_name, Bindings::MessageEventInit const& event_init)
|
|
|
|
|
|
: MessageEvent(realm, event_name, event_init, String { event_init.origin })
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MessageEvent::MessageEvent(JS::Realm& realm, FlyString const& event_name, Bindings::MessageEventInit const& event_init, URL::Origin const& origin)
|
|
|
|
|
|
: MessageEvent(realm, event_name, event_init, Variant<URL::Origin, String, Empty> { origin })
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MessageEvent::MessageEvent(JS::Realm& realm, FlyString const& event_name, Bindings::MessageEventInit const& event_init, Variant<URL::Origin, String, Empty> origin)
|
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)
|
2026-05-01 14:01:23 -03:00
|
|
|
|
, m_origin(move(origin))
|
2022-08-08 17:29:40 -03:00
|
|
|
|
, m_last_event_id(event_init.last_event_id)
|
2026-02-16 11:28:08 -03:00
|
|
|
|
, m_source(to_message_event_source_internal(event_init.source))
|
2026-05-01 14:01:23 -03:00
|
|
|
|
|
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
|
|
|
|
{
|
2024-03-16 09:13:08 -03:00
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(MessageEvent);
|
2025-04-20 11:22:57 -03:00
|
|
|
|
Base::initialize(realm);
|
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);
|
2026-04-14 14:03:06 -03:00
|
|
|
|
visitor.visit(m_source);
|
2022-08-08 17:29:40 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-17 02:55:56 -03:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/comms.html#dom-messageevent-origin
|
|
|
|
|
|
String MessageEvent::origin() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_origin.visit(
|
|
|
|
|
|
// 1. If this's origin is an origin, then return the serialization of this's origin.
|
|
|
|
|
|
[](URL::Origin const& origin) {
|
|
|
|
|
|
return origin.serialize();
|
|
|
|
|
|
},
|
|
|
|
|
|
// 2. If this's origin is null, then return the empty string.
|
|
|
|
|
|
[](Empty) {
|
|
|
|
|
|
return String {};
|
|
|
|
|
|
},
|
|
|
|
|
|
// 3. Return this's origin.
|
|
|
|
|
|
[](String const& origin) {
|
|
|
|
|
|
return origin;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-17 11:57:03 -03:00
|
|
|
|
NullableMessageEventSource MessageEvent::source() const
|
2023-11-06 17:11:58 -03:00
|
|
|
|
{
|
2026-02-16 11:28:08 -03:00
|
|
|
|
return m_source.visit(
|
2026-02-17 11:57:03 -03:00
|
|
|
|
[](Empty) -> NullableMessageEventSource { return Empty {}; },
|
|
|
|
|
|
[](auto const& ref) -> NullableMessageEventSource { return GC::Root { *ref }; });
|
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
|
2026-02-17 11:57:03 -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, NullableMessageEventSource 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;
|
2026-02-16 11:28:08 -03:00
|
|
|
|
m_source = to_message_event_source_internal(source);
|
2026-01-05 19:11:54 -03:00
|
|
|
|
|
|
|
|
|
|
m_ports_array = nullptr;
|
2024-07-09 15:43:51 -03:00
|
|
|
|
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));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-07 10:19:54 -03:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/comms.html#the-messageevent-interface:extract-an-origin
|
|
|
|
|
|
Optional<URL::Origin> MessageEvent::extract_an_origin() const
|
|
|
|
|
|
{
|
2026-03-17 02:55:56 -03:00
|
|
|
|
// Objects implementing the MessageEvent interface's extract an origin steps are to return this's origin if it is an origin; otherwise null.
|
|
|
|
|
|
return m_origin.visit(
|
|
|
|
|
|
[](URL::Origin const& origin) -> Optional<URL::Origin> {
|
|
|
|
|
|
return origin;
|
|
|
|
|
|
},
|
|
|
|
|
|
[](auto const&) -> Optional<URL::Origin> {
|
|
|
|
|
|
return {};
|
|
|
|
|
|
});
|
2025-12-07 10:19:54 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-08 17:29:40 -03:00
|
|
|
|
}
|