| 
									
										
										
										
											2021-09-19 22:12:31 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2021, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2023-12-19 15:28:56 -07:00
										 |  |  |  * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-09-19 22:12:31 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/RefCounted.h>
 | 
					
						
							|  |  |  | #include <AK/Weakable.h>
 | 
					
						
							| 
									
										
										
										
											2023-12-18 17:24:31 -07:00
										 |  |  | #include <LibCore/Socket.h>
 | 
					
						
							| 
									
										
										
										
											2024-10-05 15:33:34 +13:00
										 |  |  | #include <LibIPC/File.h>
 | 
					
						
							| 
									
										
										
										
											2024-10-22 15:47:33 -06:00
										 |  |  | #include <LibIPC/Transport.h>
 | 
					
						
							| 
									
										
										
										
											2023-12-07 15:52:08 -07:00
										 |  |  | #include <LibWeb/Bindings/Transferable.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-19 22:12:31 +02:00
										 |  |  | #include <LibWeb/DOM/EventTarget.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Forward.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::HTML { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define ENUMERATE_MESSAGE_PORT_EVENT_HANDLERS(E) \
 | 
					
						
							|  |  |  |     E(onmessage, HTML::EventNames::message)      \ | 
					
						
							|  |  |  |     E(onmessageerror, HTML::EventNames::messageerror) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/web-messaging.html#message-ports
 | 
					
						
							| 
									
										
										
										
											2023-12-07 15:52:08 -07:00
										 |  |  | class MessagePort final : public DOM::EventTarget | 
					
						
							|  |  |  |     , public Bindings::Transferable { | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     WEB_PLATFORM_OBJECT(MessagePort, DOM::EventTarget); | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC_DECLARE_ALLOCATOR(MessagePort); | 
					
						
							| 
									
										
										
										
											2021-09-19 22:12:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     [[nodiscard]] static GC::Ref<MessagePort> create(JS::Realm&); | 
					
						
							| 
									
										
										
										
											2021-09-19 22:12:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 07:59:04 -04:00
										 |  |  |     static void for_each_message_port(Function<void(MessagePort&)>); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-19 22:12:31 +02:00
										 |  |  |     virtual ~MessagePort() override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // https://html.spec.whatwg.org/multipage/web-messaging.html#entangle
 | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  |     void entangle_with(MessagePort&); | 
					
						
							| 
									
										
										
										
											2021-09-19 22:12:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 07:59:04 -04:00
										 |  |  |     void disentangle(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-19 22:12:31 +02:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-postmessage
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     WebIDL::ExceptionOr<void> post_message(JS::Value message, Vector<GC::Root<JS::Object>> const& transfer); | 
					
						
							| 
									
										
										
										
											2023-12-18 17:24:31 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-postmessage-options
 | 
					
						
							|  |  |  |     WebIDL::ExceptionOr<void> post_message(JS::Value message, StructuredSerializeOptions const& options); | 
					
						
							| 
									
										
										
										
											2021-09-19 22:12:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     void start(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void close(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #undef __ENUMERATE
 | 
					
						
							| 
									
										
										
										
											2022-09-24 16:02:41 +01:00
										 |  |  | #define __ENUMERATE(attribute_name, event_name)       \
 | 
					
						
							|  |  |  |     void set_##attribute_name(WebIDL::CallbackType*); \ | 
					
						
							|  |  |  |     WebIDL::CallbackType* attribute_name(); | 
					
						
							| 
									
										
										
										
											2021-09-19 22:12:31 +02:00
										 |  |  |     ENUMERATE_MESSAGE_PORT_EVENT_HANDLERS(__ENUMERATE) | 
					
						
							|  |  |  | #undef __ENUMERATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-07 15:52:08 -07:00
										 |  |  |     // ^Transferable
 | 
					
						
							|  |  |  |     virtual WebIDL::ExceptionOr<void> transfer_steps(HTML::TransferDataHolder&) override; | 
					
						
							| 
									
										
										
										
											2023-12-18 17:24:31 -07:00
										 |  |  |     virtual WebIDL::ExceptionOr<void> transfer_receiving_steps(HTML::TransferDataHolder&) override; | 
					
						
							| 
									
										
										
										
											2023-12-07 15:52:08 -07:00
										 |  |  |     virtual HTML::TransferType primary_interface() const override { return HTML::TransferType::MessagePort; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     void set_worker_event_target(GC::Ref<DOM::EventTarget>); | 
					
						
							| 
									
										
										
										
											2023-12-20 13:47:01 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-19 22:12:31 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2022-09-25 16:38:21 -06:00
										 |  |  |     explicit MessagePort(JS::Realm&); | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |     virtual void initialize(JS::Realm&) override; | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     virtual void visit_edges(Cell::Visitor&) override; | 
					
						
							| 
									
										
										
										
											2021-09-19 22:12:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-22 15:47:33 -06:00
										 |  |  |     bool is_entangled() const; | 
					
						
							| 
									
										
										
										
											2021-09-19 22:12:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     WebIDL::ExceptionOr<void> message_port_post_message_steps(GC::Ptr<MessagePort> target_port, JS::Value message, StructuredSerializeOptions const& options); | 
					
						
							| 
									
										
										
										
											2023-12-19 15:28:56 -07:00
										 |  |  |     void post_message_task_steps(SerializedTransferRecord&); | 
					
						
							|  |  |  |     void post_port_message(SerializedTransferRecord); | 
					
						
							| 
									
										
										
										
											2024-10-22 15:47:33 -06:00
										 |  |  |     ErrorOr<void> send_message_on_transport(SerializedTransferRecord const&); | 
					
						
							|  |  |  |     void read_from_transport(); | 
					
						
							| 
									
										
										
										
											2023-12-18 17:24:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-17 16:46:24 -06:00
										 |  |  |     enum class ParseDecision { | 
					
						
							|  |  |  |         NotEnoughData, | 
					
						
							|  |  |  |         ParseNextMessage, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     ErrorOr<ParseDecision> parse_message(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-19 22:12:31 +02:00
										 |  |  |     // The HTML spec implies(!) that this is MessagePort.[[RemotePort]]
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC::Ptr<MessagePort> m_remote_port; | 
					
						
							| 
									
										
										
										
											2021-09-19 22:12:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://html.spec.whatwg.org/multipage/web-messaging.html#has-been-shipped
 | 
					
						
							|  |  |  |     bool m_has_been_shipped { false }; | 
					
						
							| 
									
										
										
										
											2023-12-18 17:24:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-22 15:47:33 -06:00
										 |  |  |     Optional<IPC::Transport> m_transport; | 
					
						
							| 
									
										
										
										
											2023-12-19 15:28:56 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     enum class SocketState : u8 { | 
					
						
							|  |  |  |         Header, | 
					
						
							|  |  |  |         Data, | 
					
						
							|  |  |  |         Error, | 
					
						
							|  |  |  |     } m_socket_state { SocketState::Header }; | 
					
						
							|  |  |  |     size_t m_socket_incoming_message_size { 0 }; | 
					
						
							| 
									
										
										
										
											2024-04-17 16:46:24 -06:00
										 |  |  |     Queue<IPC::File> m_unprocessed_fds; | 
					
						
							|  |  |  |     Vector<u8> m_buffered_data; | 
					
						
							| 
									
										
										
										
											2023-12-20 13:47:01 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC::Ptr<DOM::EventTarget> m_worker_event_target; | 
					
						
							| 
									
										
										
										
											2021-09-19 22:12:31 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |