Serenity Operating System
at master 99 lines 4.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/Vector.h> 8#include <LibWeb/Bindings/ExceptionOrUtils.h> 9#include <LibWeb/Bindings/WorkerGlobalScopePrototype.h> 10#include <LibWeb/Forward.h> 11#include <LibWeb/HTML/EventHandler.h> 12#include <LibWeb/HTML/EventNames.h> 13#include <LibWeb/HTML/WorkerGlobalScope.h> 14#include <LibWeb/HTML/WorkerLocation.h> 15#include <LibWeb/HTML/WorkerNavigator.h> 16 17namespace Web::HTML { 18 19WorkerGlobalScope::WorkerGlobalScope(JS::Realm& realm) 20 : DOM::EventTarget(realm) 21 22{ 23} 24 25WorkerGlobalScope::~WorkerGlobalScope() = default; 26 27JS::ThrowCompletionOr<void> WorkerGlobalScope::initialize(JS::Realm& realm) 28{ 29 MUST_OR_THROW_OOM(Base::initialize(realm)); 30 m_navigator = TRY(Bindings::throw_dom_exception_if_needed(realm.vm(), [&]() { 31 return WorkerNavigator::create(*this); 32 })); 33 34 return {}; 35} 36 37void WorkerGlobalScope::visit_edges(Cell::Visitor& visitor) 38{ 39 Base::visit_edges(visitor); 40 visitor.visit(m_location.ptr()); 41 visitor.visit(m_navigator.ptr()); 42} 43 44// https://html.spec.whatwg.org/multipage/workers.html#importing-scripts-and-libraries 45WebIDL::ExceptionOr<void> WorkerGlobalScope::import_scripts(Vector<String> urls) 46{ 47 // The algorithm may optionally be customized by supplying custom perform the fetch hooks, 48 // which if provided will be used when invoking fetch a classic worker-imported script. 49 // NOTE: Service Workers is an example of a specification that runs this algorithm with its own options for the perform the fetch hook. 50 51 // FIXME: 1. If worker global scope's type is "module", throw a TypeError exception. 52 // FIXME: 2. Let settings object be the current settings object. 53 54 // 3. If urls is empty, return. 55 if (urls.is_empty()) 56 return {}; 57 58 // FIXME: 4. Parse each value in urls relative to settings object. If any fail, throw a "SyntaxError" DOMException. 59 // FIXME: 5. For each url in the resulting URL records, run these substeps: 60 // 1. Fetch a classic worker-imported script given url and settings object, passing along any custom perform the fetch steps provided. 61 // If this succeeds, let script be the result. Otherwise, rethrow the exception. 62 // 2. Run the classic script script, with the rethrow errors argument set to true. 63 // NOTE: script will run until it either returns, fails to parse, fails to catch an exception, 64 // or gets prematurely aborted by the terminate a worker algorithm defined above. 65 // If an exception was thrown or if the script was prematurely aborted, then abort all these steps, 66 // letting the exception or aborting continue to be processed by the calling script. 67 68 return {}; 69} 70 71// https://html.spec.whatwg.org/multipage/workers.html#dom-workerglobalscope-location 72JS::NonnullGCPtr<WorkerLocation> WorkerGlobalScope::location() const 73{ 74 // The location attribute must return the WorkerLocation object whose associated WorkerGlobalScope object is the WorkerGlobalScope object. 75 return *m_location; 76} 77 78// https://html.spec.whatwg.org/multipage/workers.html#dom-worker-navigator 79JS::NonnullGCPtr<WorkerNavigator> WorkerGlobalScope::navigator() const 80{ 81 // The navigator attribute of the WorkerGlobalScope interface must return an instance of the WorkerNavigator interface, 82 // which represents the identity and state of the user agent (the client). 83 return *m_navigator; 84} 85 86#undef __ENUMERATE 87#define __ENUMERATE(attribute_name, event_name) \ 88 void WorkerGlobalScope::set_##attribute_name(WebIDL::CallbackType* value) \ 89 { \ 90 set_event_handler_attribute(event_name, move(value)); \ 91 } \ 92 WebIDL::CallbackType* WorkerGlobalScope::attribute_name() \ 93 { \ 94 return event_handler_attribute(event_name); \ 95 } 96ENUMERATE_WORKER_GLOBAL_SCOPE_EVENT_HANDLERS(__ENUMERATE) 97#undef __ENUMERATE 98 99}