| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2022, Ben Abraham <ben.d.abraham@gmail.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/RefCounted.h>
 | 
					
						
							|  |  |  | #include <AK/URLParser.h>
 | 
					
						
							|  |  |  | #include <LibJS/Interpreter.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/MainThreadVM.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Forward.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/MessageEvent.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/MessagePort.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/Scripting/ClassicScript.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/WorkerDebugConsoleClient.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Loader/ResourceLoader.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define ENUMERATE_WORKER_EVENT_HANDLERS(E)  \
 | 
					
						
							|  |  |  |     E(onmessage, HTML::EventNames::message) \ | 
					
						
							|  |  |  |     E(onmessageerror, HTML::EventNames::messageerror) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::HTML { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct WorkerOptions { | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString type { "classic" }; | 
					
						
							|  |  |  |     DeprecatedString credentials { "same-origin" }; | 
					
						
							|  |  |  |     DeprecatedString name { "" }; | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/workers.html#dedicated-workers-and-the-worker-interface
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | class Worker : public DOM::EventTarget { | 
					
						
							|  |  |  |     WEB_PLATFORM_OBJECT(Worker, DOM::EventTarget); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-01-08 19:23:00 -05:00
										 |  |  |     static WebIDL::ExceptionOr<JS::NonnullGCPtr<Worker>> create(DeprecatedFlyString const& script_url, WorkerOptions const options, DOM::Document& document); | 
					
						
							|  |  |  |     static WebIDL::ExceptionOr<JS::NonnullGCPtr<Worker>> construct_impl(JS::Realm& realm, DeprecatedFlyString const& script_url, WorkerOptions const options) | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-25 16:38:21 -06:00
										 |  |  |         auto& window = verify_cast<HTML::Window>(realm.global_object()); | 
					
						
							| 
									
										
										
										
											2022-09-21 17:45:18 +01:00
										 |  |  |         return Worker::create(script_url, options, window.associated_document()); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 17:03:42 +01:00
										 |  |  |     WebIDL::ExceptionOr<void> terminate(); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     void post_message(JS::Value message, JS::Value transfer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual ~Worker() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     MessagePort* implicit_message_port() { return m_implicit_port.ptr(); } | 
					
						
							|  |  |  |     JS::GCPtr<MessagePort> outside_message_port() { return m_outside_port; } | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #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(); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  |     ENUMERATE_WORKER_EVENT_HANDLERS(__ENUMERATE) | 
					
						
							|  |  |  | #undef __ENUMERATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							| 
									
										
										
										
											2023-01-08 19:23:00 -05:00
										 |  |  |     Worker(DeprecatedFlyString const&, const WorkerOptions, DOM::Document&); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     static HTML::EventLoop& get_vm_event_loop(JS::VM& target_vm) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return static_cast<Bindings::WebEngineCustomData*>(target_vm.custom_data())->event_loop; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-10 06:28:20 -05:00
										 |  |  |     virtual void initialize(JS::Realm&) override; | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     virtual void visit_edges(Cell::Visitor&) override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-08 19:23:00 -05:00
										 |  |  |     DeprecatedFlyString m_script_url; | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  |     WorkerOptions m_options; | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     JS::GCPtr<DOM::Document> m_document; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  |     Bindings::WebEngineCustomData m_custom_data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     NonnullRefPtr<JS::VM> m_worker_vm; | 
					
						
							|  |  |  |     NonnullOwnPtr<JS::Interpreter> m_interpreter; | 
					
						
							| 
									
										
										
										
											2022-09-24 15:39:23 -06:00
										 |  |  |     JS::GCPtr<WorkerEnvironmentSettingsObject> m_inner_settings; | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  |     JS::VM::InterpreterExecutionScope m_interpreter_scope; | 
					
						
							|  |  |  |     RefPtr<WorkerDebugConsoleClient> m_console; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     JS::NonnullGCPtr<MessagePort> m_implicit_port; | 
					
						
							|  |  |  |     JS::GCPtr<MessagePort> m_outside_port; | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-05 12:19:41 +02:00
										 |  |  |     // NOTE: These are inside the worker VM.
 | 
					
						
							|  |  |  |     JS::GCPtr<JS::Realm> m_worker_realm; | 
					
						
							|  |  |  |     JS::GCPtr<JS::Object> m_worker_scope; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 16:38:21 -06:00
										 |  |  |     void run_a_worker(AK::URL& url, EnvironmentSettingsObject& outside_settings, MessagePort& outside_port, WorkerOptions const& options); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |