| 
									
										
										
										
											2021-09-19 22:12:31 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/RefCounted.h>
 | 
					
						
							|  |  |  | #include <AK/Weakable.h>
 | 
					
						
							|  |  |  | #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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-07 18:21:49 +00:00
										 |  |  | // https://html.spec.whatwg.org/multipage/web-messaging.html#structuredserializeoptions
 | 
					
						
							|  |  |  | struct StructuredSerializeOptions { | 
					
						
							|  |  |  |     Vector<JS::Handle<JS::Object>> transfer; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-19 22:12:31 +02:00
										 |  |  | // https://html.spec.whatwg.org/multipage/web-messaging.html#message-ports
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | class MessagePort final : public DOM::EventTarget { | 
					
						
							|  |  |  |     WEB_PLATFORM_OBJECT(MessagePort, DOM::EventTarget); | 
					
						
							| 
									
										
										
										
											2021-09-19 22:12:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-08-13 13:05:26 +02:00
										 |  |  |     [[nodiscard]] static JS::NonnullGCPtr<MessagePort> create(JS::Realm&); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-postmessage
 | 
					
						
							|  |  |  |     void post_message(JS::Value); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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
										 |  |  | 
 | 
					
						
							|  |  |  |     bool is_entangled() const { return m_remote_port; } | 
					
						
							|  |  |  |     void disentangle(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // The HTML spec implies(!) that this is MessagePort.[[RemotePort]]
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     JS::GCPtr<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 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // This is TransferableObject.[[Detached]]
 | 
					
						
							|  |  |  |     bool m_detached { false }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |