Serenity Operating System
1/*
2 * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/Optional.h>
10#include <AK/RefCounted.h>
11#include <AK/URL.h>
12#include <LibWeb/DOM/EventTarget.h>
13#include <LibWeb/Forward.h>
14#include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
15#include <LibWeb/HTML/WorkerLocation.h>
16#include <LibWeb/HTML/WorkerNavigator.h>
17#include <LibWeb/WebIDL/ExceptionOr.h>
18
19#define ENUMERATE_WORKER_GLOBAL_SCOPE_EVENT_HANDLERS(E) \
20 E(onerror, HTML::EventNames::error) \
21 E(onlanguagechange, HTML::EventNames::languagechange) \
22 E(ononline, HTML::EventNames::online) \
23 E(onoffline, HTML::EventNames::offline) \
24 E(onrejectionhandled, HTML::EventNames::rejectionhandled) \
25 E(onunhandledrejection, HTML::EventNames::unhandledrejection)
26
27namespace Web::HTML {
28
29// https://html.spec.whatwg.org/multipage/workers.html#the-workerglobalscope-common-interface
30// WorkerGlobalScope is the base class of each real WorkerGlobalScope that will be created when the
31// user agent runs the run a worker algorithm.
32class WorkerGlobalScope
33 : public DOM::EventTarget
34 , public WindowOrWorkerGlobalScopeMixin {
35 WEB_PLATFORM_OBJECT(WorkerGlobalScope, DOM::EventTarget);
36
37public:
38 virtual ~WorkerGlobalScope() override;
39
40 // ^WindowOrWorkerGlobalScopeMixin
41 virtual Bindings::PlatformObject& this_impl() override { return *this; }
42 virtual Bindings::PlatformObject const& this_impl() const override { return *this; }
43
44 // Following methods are from the WorkerGlobalScope IDL definition
45 // https://html.spec.whatwg.org/multipage/workers.html#the-workerglobalscope-common-interface
46
47 // https://html.spec.whatwg.org/multipage/workers.html#dom-workerglobalscope-self
48 JS::NonnullGCPtr<WorkerGlobalScope const> self() const { return *this; }
49
50 JS::NonnullGCPtr<WorkerLocation> location() const;
51 JS::NonnullGCPtr<WorkerNavigator> navigator() const;
52 WebIDL::ExceptionOr<void> import_scripts(Vector<String> urls);
53
54#undef __ENUMERATE
55#define __ENUMERATE(attribute_name, event_name) \
56 void set_##attribute_name(WebIDL::CallbackType*); \
57 WebIDL::CallbackType* attribute_name();
58 ENUMERATE_WORKER_GLOBAL_SCOPE_EVENT_HANDLERS(__ENUMERATE)
59#undef __ENUMERATE
60
61 // Non-IDL public methods
62
63 AK::URL const& url() const { return m_url.value(); }
64 void set_url(AK::URL const& url) { m_url = url; }
65
66 // Spec note: While the WorkerLocation object is created after the WorkerGlobalScope object,
67 // this is not problematic as it cannot be observed from script.
68 void set_location(JS::NonnullGCPtr<WorkerLocation> loc) { m_location = move(loc); }
69
70protected:
71 explicit WorkerGlobalScope(JS::Realm&);
72
73private:
74 virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
75
76 virtual void visit_edges(Cell::Visitor&) override;
77
78 JS::GCPtr<WorkerLocation> m_location;
79
80 JS::GCPtr<WorkerNavigator> m_navigator;
81
82 // FIXME: Add all these internal slots
83
84 // https://html.spec.whatwg.org/multipage/workers.html#concept-WorkerGlobalScope-owner-set
85 // A WorkerGlobalScope object has an associated owner set (a set of Document and WorkerGlobalScope objects). It is initially empty and populated when the worker is created or obtained.
86 // Note: It is a set, instead of a single owner, to accommodate SharedWorkerGlobalScope objects.
87
88 // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-type
89 // A WorkerGlobalScope object has an associated type ("classic" or "module"). It is set during creation.
90
91 // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-url
92 // A WorkerGlobalScope object has an associated url (null or a URL). It is initially null.
93 Optional<AK::URL> m_url;
94
95 // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-name
96 // A WorkerGlobalScope object has an associated name (a string). It is set during creation.
97 // Note: The name can have different semantics for each subclass of WorkerGlobalScope.
98 // For DedicatedWorkerGlobalScope instances, it is simply a developer-supplied name, useful mostly for debugging purposes.
99 // For SharedWorkerGlobalScope instances, it allows obtaining a reference to a common shared worker via the SharedWorker() constructor.
100 // For ServiceWorkerGlobalScope objects, it doesn't make sense (and as such isn't exposed through the JavaScript API at all).
101
102 // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-policy-container
103 // A WorkerGlobalScope object has an associated policy container (a policy container). It is initially a new policy container.
104
105 // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-embedder-policy
106 // A WorkerGlobalScope object has an associated embedder policy (an embedder policy).
107
108 // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-module-map
109 // A WorkerGlobalScope object has an associated module map. It is a module map, initially empty.
110
111 // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-cross-origin-isolated-capability
112 bool m_cross_origin_isolated_capability { false };
113};
114
115}