| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:22:27 +13:00
										 |  |  | #include <LibURL/Parser.h>
 | 
					
						
							| 
									
										
										
										
											2024-04-27 12:09:58 +12:00
										 |  |  | #include <LibWeb/Bindings/WorkerLocationPrototype.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | #include <LibWeb/HTML/WorkerGlobalScope.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/WorkerLocation.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::HTML { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-19 19:47:52 +01:00
										 |  |  | JS_DEFINE_ALLOCATOR(WorkerLocation); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-href
 | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  | WebIDL::ExceptionOr<String> WorkerLocation::href() const | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  |     auto& vm = realm().vm(); | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  |     // The href getter steps are to return this's WorkerGlobalScope object's url, serialized.
 | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  |     return TRY_OR_THROW_OOM(vm, String::from_byte_string(m_global_scope->url().serialize())); | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-origin
 | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  | WebIDL::ExceptionOr<String> WorkerLocation::origin() const | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  |     auto& vm = realm().vm(); | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  |     // The origin getter steps are to return the serialization of this's WorkerGlobalScope object's url's origin.
 | 
					
						
							| 
									
										
										
										
											2024-10-05 17:03:51 +13:00
										 |  |  |     return TRY_OR_THROW_OOM(vm, String::from_byte_string(m_global_scope->url().origin().serialize())); | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-protocol
 | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  | WebIDL::ExceptionOr<String> WorkerLocation::protocol() const | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  |     auto& vm = realm().vm(); | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  |     // The protocol getter steps are to return this's WorkerGlobalScope object's url's scheme, followed by ":".
 | 
					
						
							| 
									
										
										
										
											2023-08-12 16:52:41 +12:00
										 |  |  |     return TRY_OR_THROW_OOM(vm, String::formatted("{}:", m_global_scope->url().scheme())); | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-host
 | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  | WebIDL::ExceptionOr<String> WorkerLocation::host() const | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  |     auto& vm = realm().vm(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  |     // The host getter steps are:
 | 
					
						
							|  |  |  |     // 1. Let url be this's WorkerGlobalScope object's url.
 | 
					
						
							| 
									
										
										
										
											2023-02-26 16:09:02 -07:00
										 |  |  |     auto const& url = m_global_scope->url(); | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 2. If url's host is null, return the empty string.
 | 
					
						
							| 
									
										
										
										
											2023-07-27 21:40:41 +12:00
										 |  |  |     if (url.host().has<Empty>()) | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  |         return String {}; | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 3. If url's port is null, return url's host, serialized.
 | 
					
						
							|  |  |  |     if (!url.port().has_value()) | 
					
						
							| 
									
										
										
										
											2023-07-27 21:40:41 +12:00
										 |  |  |         return TRY_OR_THROW_OOM(vm, url.serialized_host()); | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 4. Return url's host, serialized, followed by ":" and url's port, serialized.
 | 
					
						
							| 
									
										
										
										
											2023-07-27 21:40:41 +12:00
										 |  |  |     return TRY_OR_THROW_OOM(vm, String::formatted("{}:{}", TRY_OR_THROW_OOM(vm, url.serialized_host()), url.port().value())); | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-hostname
 | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  | WebIDL::ExceptionOr<String> WorkerLocation::hostname() const | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  |     auto& vm = realm().vm(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  |     // The hostname getter steps are:
 | 
					
						
							|  |  |  |     // 1. Let host be this's WorkerGlobalScope object's url's host.
 | 
					
						
							| 
									
										
										
										
											2023-02-26 16:09:02 -07:00
										 |  |  |     auto const& host = m_global_scope->url().host(); | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 2. If host is null, return the empty string.
 | 
					
						
							| 
									
										
										
										
											2023-07-27 21:40:41 +12:00
										 |  |  |     if (host.has<Empty>()) | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  |         return String {}; | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 3. Return host, serialized.
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:22:27 +13:00
										 |  |  |     return TRY_OR_THROW_OOM(vm, URL::Parser::serialize_host(host)); | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-port
 | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  | WebIDL::ExceptionOr<String> WorkerLocation::port() const | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  |     auto& vm = realm().vm(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  |     // The port getter steps are:
 | 
					
						
							|  |  |  |     // 1. Let port be this's WorkerGlobalScope object's url's port.
 | 
					
						
							| 
									
										
										
										
											2023-02-26 16:09:02 -07:00
										 |  |  |     auto const& port = m_global_scope->url().port(); | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 2. If port is null, return the empty string.
 | 
					
						
							|  |  |  |     if (!port.has_value()) | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  |         return String {}; | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  |     // 3. Return port, serialized.
 | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  |     return TRY_OR_THROW_OOM(vm, String::number(port.value())); | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-pathname
 | 
					
						
							| 
									
										
										
										
											2024-08-05 16:55:39 +12:00
										 |  |  | String WorkerLocation::pathname() const | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | { | 
					
						
							|  |  |  |     // The pathname getter steps are to return the result of URL path serializing this's WorkerGlobalScope object's url.
 | 
					
						
							| 
									
										
										
										
											2024-08-05 16:55:39 +12:00
										 |  |  |     return m_global_scope->url().serialize_path(); | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-search
 | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  | WebIDL::ExceptionOr<String> WorkerLocation::search() const | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  |     auto& vm = realm().vm(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  |     // The search getter steps are:
 | 
					
						
							|  |  |  |     // 1. Let query be this's WorkerGlobalScope object's url's query.
 | 
					
						
							| 
									
										
										
										
											2023-02-26 16:09:02 -07:00
										 |  |  |     auto const& query = m_global_scope->url().query(); | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 2. If query is either null or the empty string, return the empty string.
 | 
					
						
							| 
									
										
										
										
											2023-08-12 19:28:19 +12:00
										 |  |  |     if (!query.has_value() || query->is_empty()) | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  |         return String {}; | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 3. Return "?", followed by query.
 | 
					
						
							| 
									
										
										
										
											2023-08-12 19:28:19 +12:00
										 |  |  |     return TRY_OR_THROW_OOM(vm, String::formatted("?{}", *query)); | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-hash
 | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  | WebIDL::ExceptionOr<String> WorkerLocation::hash() const | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  |     auto& vm = realm().vm(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  |     // The hash getter steps are:
 | 
					
						
							|  |  |  |     // 1. Let fragment be this's WorkerGlobalScope object's url's fragment.
 | 
					
						
							| 
									
										
										
										
											2023-02-26 16:09:02 -07:00
										 |  |  |     auto const& fragment = m_global_scope->url().fragment(); | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 2. If fragment is either null or the empty string, return the empty string.
 | 
					
						
							| 
									
										
										
										
											2023-08-12 16:52:42 +12:00
										 |  |  |     if (!fragment.has_value() || fragment->is_empty()) | 
					
						
							| 
									
										
										
										
											2023-02-20 21:44:05 +01:00
										 |  |  |         return String {}; | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 3. Return "#", followed by fragment.
 | 
					
						
							| 
									
										
										
										
											2023-08-12 16:52:42 +12:00
										 |  |  |     return TRY_OR_THROW_OOM(vm, String::formatted("#{}", *fragment)); | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | WorkerLocation::WorkerLocation(WorkerGlobalScope& global_scope) | 
					
						
							| 
									
										
										
										
											2022-09-04 14:30:38 +02:00
										 |  |  |     : PlatformObject(global_scope.realm()) | 
					
						
							|  |  |  |     , m_global_scope(global_scope) | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-09-04 14:30:38 +02:00
										 |  |  |     // FIXME: Set prototype once we can get to worker scope prototypes.
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | WorkerLocation::~WorkerLocation() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-28 19:14:45 +01:00
										 |  |  | void WorkerLocation::initialize(JS::Realm& realm) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::initialize(realm); | 
					
						
							|  |  |  |     WEB_SET_PROTOTYPE_FOR_INTERFACE(WorkerLocation); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-04 14:30:38 +02:00
										 |  |  | void WorkerLocation::visit_edges(Cell::Visitor& visitor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::visit_edges(visitor); | 
					
						
							|  |  |  |     visitor.visit(m_global_scope); | 
					
						
							| 
									
										
										
										
											2022-02-06 19:12:57 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |