ladybird/Userland/Libraries/LibWeb/HTML/MessageEvent.cpp
Andreas Kling cc4b3cbacc
Some checks are pending
CI / Lagom (false, FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, macos-14, macOS, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (true, NO_FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (macos-14, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Push notes / build (push) Waiting to run
Meta: Update my e-mail address everywhere
2024-10-04 13:19:50 +02:00

105 lines
3.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/Array.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/MessageEventPrototype.h>
#include <LibWeb/HTML/MessageEvent.h>
#include <LibWeb/HTML/MessagePort.h>
namespace Web::HTML {
JS_DEFINE_ALLOCATOR(MessageEvent);
JS::NonnullGCPtr<MessageEvent> MessageEvent::create(JS::Realm& realm, FlyString const& event_name, MessageEventInit const& event_init)
{
return realm.heap().allocate<MessageEvent>(realm, realm, event_name, event_init);
}
WebIDL::ExceptionOr<JS::NonnullGCPtr<MessageEvent>> MessageEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, MessageEventInit const& event_init)
{
return create(realm, event_name, event_init);
}
MessageEvent::MessageEvent(JS::Realm& realm, FlyString const& event_name, MessageEventInit const& event_init)
: DOM::Event(realm, event_name, event_init)
, m_data(event_init.data)
, m_origin(event_init.origin)
, m_last_event_id(event_init.last_event_id)
, m_source(event_init.source)
{
m_ports.ensure_capacity(event_init.ports.size());
for (auto const& port : event_init.ports) {
VERIFY(port);
m_ports.unchecked_append(static_cast<JS::Object&>(*port));
}
}
MessageEvent::~MessageEvent() = default;
void MessageEvent::initialize(JS::Realm& realm)
{
Base::initialize(realm);
WEB_SET_PROTOTYPE_FOR_INTERFACE(MessageEvent);
}
void MessageEvent::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_data);
visitor.visit(m_ports_array);
visitor.visit(m_ports);
}
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>>();
}
JS::NonnullGCPtr<JS::Object> MessageEvent::ports() const
{
if (!m_ports_array) {
Vector<JS::Value> port_vector;
for (auto const& port : m_ports) {
port_vector.append(port);
}
m_ports_array = JS::Array::create_from(realm(), port_vector);
MUST(m_ports_array->set_integrity_level(IntegrityLevel::Frozen));
}
return *m_ports_array;
}
// 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 thiss 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));
}
}
}