| 
									
										
										
										
											2022-08-08 22:29:40 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 22:29:40 +02:00
										 |  |  | } |