| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2022, Ben Abraham <ben.d.abraham@gmail.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-23 13:28:01 +01:00
										 |  |  | #include <AK/Debug.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-28 14:20:06 +01:00
										 |  |  | #include <LibJS/Runtime/ConsoleObject.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | #include <LibJS/Runtime/Realm.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/MainThreadVM.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/Scripting/Environments.h>
 | 
					
						
							| 
									
										
										
										
											2023-11-22 09:57:22 -07:00
										 |  |  | #include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | #include <LibWeb/HTML/Worker.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-25 17:03:42 +01:00
										 |  |  | #include <LibWeb/WebIDL/ExceptionOr.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::HTML { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-19 19:47:52 +01:00
										 |  |  | JS_DEFINE_ALLOCATOR(Worker); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | // https://html.spec.whatwg.org/multipage/workers.html#dedicated-workers-and-the-worker-interface
 | 
					
						
							| 
									
										
										
										
											2023-12-20 13:47:01 -07:00
										 |  |  | Worker::Worker(String const& script_url, WorkerOptions const& options, DOM::Document& document) | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     : DOM::EventTarget(document.realm()) | 
					
						
							|  |  |  |     , m_script_url(script_url) | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  |     , m_options(options) | 
					
						
							|  |  |  |     , m_document(&document) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-10 06:28:20 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  | void Worker::initialize(JS::Realm& realm) | 
					
						
							| 
									
										
										
										
											2023-01-10 06:28:20 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |     Base::initialize(realm); | 
					
						
							| 
									
										
										
										
											2023-11-22 12:55:21 +13:00
										 |  |  |     set_prototype(&Bindings::ensure_web_prototype<Bindings::WorkerPrototype>(realm, "Worker"_fly_string)); | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Worker::visit_edges(Cell::Visitor& visitor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::visit_edges(visitor); | 
					
						
							| 
									
										
										
										
											2023-03-21 10:08:44 -07:00
										 |  |  |     visitor.visit(m_document); | 
					
						
							|  |  |  |     visitor.visit(m_outside_port); | 
					
						
							| 
									
										
										
										
											2023-11-23 09:02:00 -07:00
										 |  |  |     visitor.visit(m_agent); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/workers.html#dom-worker
 | 
					
						
							| 
									
										
										
										
											2023-12-20 13:47:01 -07:00
										 |  |  | WebIDL::ExceptionOr<JS::NonnullGCPtr<Worker>> Worker::create(String const& script_url, WorkerOptions const& options, DOM::Document& document) | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     dbgln_if(WEB_WORKER_DEBUG, "WebWorker: Creating worker with script_url = {}", script_url); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Returns a new Worker object. scriptURL will be fetched and executed in the background,
 | 
					
						
							|  |  |  |     // creating a new global environment for which worker represents the communication channel.
 | 
					
						
							|  |  |  |     // options can be used to define the name of that global environment via the name option,
 | 
					
						
							|  |  |  |     // primarily for debugging purposes. It can also ensure this new global environment supports
 | 
					
						
							|  |  |  |     // JavaScript modules (specify type: "module"), and if that is specified, can also be used
 | 
					
						
							|  |  |  |     // to specify how scriptURL is fetched through the credentials option.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // FIXME: 1. The user agent may throw a "SecurityError" DOMException if the request violates
 | 
					
						
							|  |  |  |     // a policy decision (e.g. if the user agent is configured to not allow the page to start dedicated workers).
 | 
					
						
							|  |  |  |     // Technically not a fixme if our policy is not to throw errors :^)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 2. Let outside settings be the current settings object.
 | 
					
						
							| 
									
										
										
										
											2024-03-05 09:42:10 -07:00
										 |  |  |     auto& outside_settings = current_settings_object(); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 3. Parse the scriptURL argument relative to outside settings.
 | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  |     auto url = document.parse_url(script_url.to_byte_string()); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 4. If this fails, throw a "SyntaxError" DOMException.
 | 
					
						
							|  |  |  |     if (!url.is_valid()) { | 
					
						
							|  |  |  |         dbgln_if(WEB_WORKER_DEBUG, "WebWorker: Invalid URL loaded '{}'.", script_url); | 
					
						
							| 
									
										
										
										
											2023-09-06 16:03:01 +12:00
										 |  |  |         return WebIDL::SyntaxError::create(document.realm(), "url is not valid"_fly_string); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 5. Let worker URL be the resulting URL record.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 6. Let worker be a new Worker object.
 | 
					
						
							| 
									
										
										
										
											2023-08-13 13:05:26 +02:00
										 |  |  |     auto worker = document.heap().allocate<Worker>(document.realm(), script_url, options, document); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 7. Let outside port be a new MessagePort in outside settings's Realm.
 | 
					
						
							| 
									
										
										
										
											2023-08-13 13:05:26 +02:00
										 |  |  |     auto outside_port = MessagePort::create(outside_settings.realm()); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 8. Associate the outside port with worker
 | 
					
						
							|  |  |  |     worker->m_outside_port = outside_port; | 
					
						
							| 
									
										
										
										
											2023-12-20 13:47:01 -07:00
										 |  |  |     worker->m_outside_port->set_worker_event_target(worker); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 9. Run this step in parallel:
 | 
					
						
							|  |  |  |     //    1. Run a worker given worker, worker URL, outside settings, outside port, and options.
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     worker->run_a_worker(url, outside_settings, *outside_port, options); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 10. Return worker
 | 
					
						
							| 
									
										
										
										
											2022-12-14 17:40:33 +00:00
										 |  |  |     return worker; | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/workers.html#run-a-worker
 | 
					
						
							| 
									
										
										
										
											2024-02-11 20:15:39 +13:00
										 |  |  | void Worker::run_a_worker(URL& url, EnvironmentSettingsObject& outside_settings, JS::GCPtr<MessagePort> port, WorkerOptions const& options) | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     // 1. Let is shared be true if worker is a SharedWorker object, and false otherwise.
 | 
					
						
							|  |  |  |     // FIXME: SharedWorker support
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 2. Let owner be the relevant owner to add given outside settings.
 | 
					
						
							|  |  |  |     // FIXME: Support WorkerGlobalScope options
 | 
					
						
							|  |  |  |     if (!is<HTML::WindowEnvironmentSettingsObject>(outside_settings)) | 
					
						
							|  |  |  |         TODO(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 3. Let parent worker global scope be null.
 | 
					
						
							|  |  |  |     // 4. If owner is a WorkerGlobalScope object (i.e., we are creating a nested dedicated worker),
 | 
					
						
							|  |  |  |     //    then set parent worker global scope to owner.
 | 
					
						
							|  |  |  |     // FIXME: Support for nested workers.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 5. Let unsafeWorkerCreationTime be the unsafe shared current time.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 6. Let agent be the result of obtaining a dedicated/shared worker agent given outside settings
 | 
					
						
							|  |  |  |     // and is shared. Run the rest of these steps in that agent.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-08 11:47:41 -07:00
										 |  |  |     // Note: This spawns a new process to act as the 'agent' for the worker.
 | 
					
						
							| 
									
										
										
										
											2024-03-05 09:42:10 -07:00
										 |  |  |     m_agent = heap().allocate<WorkerAgent>(outside_settings.realm(), url, options, port, outside_settings); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/workers.html#dom-worker-terminate
 | 
					
						
							| 
									
										
										
										
											2022-09-25 17:03:42 +01:00
										 |  |  | WebIDL::ExceptionOr<void> Worker::terminate() | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     dbgln_if(WEB_WORKER_DEBUG, "WebWorker: Terminate"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/workers.html#dom-worker-postmessage
 | 
					
						
							| 
									
										
										
										
											2023-12-20 13:47:01 -07:00
										 |  |  | WebIDL::ExceptionOr<void> Worker::post_message(JS::Value message, StructuredSerializeOptions const& options) | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-09 08:49:02 +02:00
										 |  |  |     dbgln_if(WEB_WORKER_DEBUG, "WebWorker: Post Message: {}", message.to_string_without_side_effects()); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-20 13:47:01 -07:00
										 |  |  |     // The postMessage(message, transfer) and postMessage(message, options) methods on Worker objects act as if,
 | 
					
						
							|  |  |  |     // when invoked, they immediately invoked the respective postMessage(message, transfer) and
 | 
					
						
							|  |  |  |     // postMessage(message, options) on the port, with the same arguments, and returned the same return value.
 | 
					
						
							| 
									
										
										
										
											2023-11-22 09:57:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-20 13:47:01 -07:00
										 |  |  |     return m_outside_port->post_message(message, options); | 
					
						
							| 
									
										
										
										
											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 Worker::set_##attribute_name(WebIDL::CallbackType* value) \ | 
					
						
							|  |  |  |     {                                                              \ | 
					
						
							|  |  |  |         set_event_handler_attribute(event_name, move(value));      \ | 
					
						
							|  |  |  |     }                                                              \ | 
					
						
							|  |  |  |     WebIDL::CallbackType* Worker::attribute_name()                 \ | 
					
						
							|  |  |  |     {                                                              \ | 
					
						
							|  |  |  |         return event_handler_attribute(event_name);                \ | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | ENUMERATE_WORKER_EVENT_HANDLERS(__ENUMERATE) | 
					
						
							|  |  |  | #undef __ENUMERATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // namespace Web::HTML
 |