Serenity Operating System
at master 148 lines 5.2 kB view raw
1/* 2 * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <AK/DeprecatedString.h> 8#include <LibWeb/HTML/WorkerGlobalScope.h> 9#include <LibWeb/HTML/WorkerLocation.h> 10 11namespace Web::HTML { 12 13// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-href 14WebIDL::ExceptionOr<String> WorkerLocation::href() const 15{ 16 auto& vm = realm().vm(); 17 // The href getter steps are to return this's WorkerGlobalScope object's url, serialized. 18 return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_global_scope.url().serialize())); 19} 20 21// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-origin 22WebIDL::ExceptionOr<String> WorkerLocation::origin() const 23{ 24 auto& vm = realm().vm(); 25 // The origin getter steps are to return the serialization of this's WorkerGlobalScope object's url's origin. 26 return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_global_scope.url().serialize_origin())); 27} 28 29// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-protocol 30WebIDL::ExceptionOr<String> WorkerLocation::protocol() const 31{ 32 auto& vm = realm().vm(); 33 // The protocol getter steps are to return this's WorkerGlobalScope object's url's scheme, followed by ":". 34 return TRY_OR_THROW_OOM(vm, String::formatted("{}:", m_global_scope.url().scheme().view())); 35} 36 37// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-host 38WebIDL::ExceptionOr<String> WorkerLocation::host() const 39{ 40 auto& vm = realm().vm(); 41 42 // The host getter steps are: 43 // 1. Let url be this's WorkerGlobalScope object's url. 44 auto const& url = m_global_scope.url(); 45 46 // 2. If url's host is null, return the empty string. 47 if (url.host().is_empty()) 48 return String {}; 49 50 // 3. If url's port is null, return url's host, serialized. 51 if (!url.port().has_value()) 52 return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(url.host())); 53 54 // 4. Return url's host, serialized, followed by ":" and url's port, serialized. 55 return TRY_OR_THROW_OOM(vm, String::formatted("{}:{}", url.host().view(), url.port().value())); 56} 57 58// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-hostname 59WebIDL::ExceptionOr<String> WorkerLocation::hostname() const 60{ 61 auto& vm = realm().vm(); 62 63 // The hostname getter steps are: 64 // 1. Let host be this's WorkerGlobalScope object's url's host. 65 auto const& host = m_global_scope.url().host(); 66 67 // 2. If host is null, return the empty string. 68 if (host.is_empty()) 69 return String {}; 70 71 // 3. Return host, serialized. 72 return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(host)); 73} 74 75// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-port 76WebIDL::ExceptionOr<String> WorkerLocation::port() const 77{ 78 auto& vm = realm().vm(); 79 80 // The port getter steps are: 81 // 1. Let port be this's WorkerGlobalScope object's url's port. 82 auto const& port = m_global_scope.url().port(); 83 84 // 2. If port is null, return the empty string. 85 if (!port.has_value()) 86 return String {}; 87 // 3. Return port, serialized. 88 return TRY_OR_THROW_OOM(vm, String::number(port.value())); 89} 90 91// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-pathname 92WebIDL::ExceptionOr<String> WorkerLocation::pathname() const 93{ 94 auto& vm = realm().vm(); 95 // The pathname getter steps are to return the result of URL path serializing this's WorkerGlobalScope object's url. 96 return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_global_scope.url().path())); 97} 98 99// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-search 100WebIDL::ExceptionOr<String> WorkerLocation::search() const 101{ 102 auto& vm = realm().vm(); 103 104 // The search getter steps are: 105 // 1. Let query be this's WorkerGlobalScope object's url's query. 106 auto const& query = m_global_scope.url().query(); 107 108 // 2. If query is either null or the empty string, return the empty string. 109 if (query.is_empty()) 110 return String {}; 111 112 // 3. Return "?", followed by query. 113 return TRY_OR_THROW_OOM(vm, String::formatted("?{}", query.view())); 114} 115 116// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-hash 117WebIDL::ExceptionOr<String> WorkerLocation::hash() const 118{ 119 auto& vm = realm().vm(); 120 121 // The hash getter steps are: 122 // 1. Let fragment be this's WorkerGlobalScope object's url's fragment. 123 auto const& fragment = m_global_scope.url().fragment(); 124 125 // 2. If fragment is either null or the empty string, return the empty string. 126 if (fragment.is_empty()) 127 return String {}; 128 129 // 3. Return "#", followed by fragment. 130 return TRY_OR_THROW_OOM(vm, String::formatted("#{}", fragment.view())); 131} 132 133WorkerLocation::WorkerLocation(WorkerGlobalScope& global_scope) 134 : PlatformObject(global_scope.realm()) 135 , m_global_scope(global_scope) 136{ 137 // FIXME: Set prototype once we can get to worker scope prototypes. 138} 139 140WorkerLocation::~WorkerLocation() = default; 141 142void WorkerLocation::visit_edges(Cell::Visitor& visitor) 143{ 144 Base::visit_edges(visitor); 145 visitor.visit(m_global_scope); 146} 147 148}