2022-02-06 19:12:57 -07:00
/*
* Copyright ( c ) 2022 , Andrew Kaster < akaster @ serenityos . org >
2024-11-25 14:30:12 +00:00
* Copyright ( c ) 2025 , Luke Wilde < luke @ ladybird . org >
2022-02-06 19:12:57 -07:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
# pragma once
# include <AK/Optional.h>
# include <AK/RefCounted.h>
2023-11-22 09:57:22 -07:00
# include <LibCore/Socket.h>
2024-03-18 16:22:27 +13:00
# include <LibURL/URL.h>
2022-02-06 19:12:57 -07:00
# include <LibWeb/DOM/EventTarget.h>
# include <LibWeb/Forward.h>
2024-05-23 13:57:08 +01:00
# include <LibWeb/HTML/Scripting/Fetching.h>
2024-11-08 16:35:16 +13:00
# include <LibWeb/HTML/UniversalGlobalScope.h>
2023-03-05 23:58:47 +00:00
# include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
2022-02-06 19:12:57 -07:00
# include <LibWeb/HTML/WorkerLocation.h>
# include <LibWeb/HTML/WorkerNavigator.h>
2022-09-25 17:03:42 +01:00
# include <LibWeb/WebIDL/ExceptionOr.h>
2022-02-06 19:12:57 -07:00
2024-07-09 15:54:22 -06:00
# define ENUMERATE_WORKER_GLOBAL_SCOPE_EVENT_HANDLERS(E) \
E ( onerror , HTML : : EventNames : : error ) \
E ( onlanguagechange , HTML : : EventNames : : languagechange ) \
E ( ononline , HTML : : EventNames : : online ) \
E ( onoffline , HTML : : EventNames : : offline ) \
E ( onrejectionhandled , HTML : : EventNames : : rejectionhandled ) \
E ( onunhandledrejection , HTML : : EventNames : : unhandledrejection )
2022-02-06 19:12:57 -07:00
namespace Web : : HTML {
// https://html.spec.whatwg.org/multipage/workers.html#the-workerglobalscope-common-interface
// WorkerGlobalScope is the base class of each real WorkerGlobalScope that will be created when the
// user agent runs the run a worker algorithm.
2023-03-05 23:58:47 +00:00
class WorkerGlobalScope
: public DOM : : EventTarget
2024-11-08 16:35:16 +13:00
, public WindowOrWorkerGlobalScopeMixin
, public UniversalGlobalScopeMixin {
2022-08-28 13:42:07 +02:00
WEB_PLATFORM_OBJECT ( WorkerGlobalScope , DOM : : EventTarget ) ;
2024-11-15 04:01:23 +13:00
GC_DECLARE_ALLOCATOR ( WorkerGlobalScope ) ;
2022-02-06 19:12:57 -07:00
2022-08-28 13:42:07 +02:00
public :
2022-02-06 19:12:57 -07:00
virtual ~ WorkerGlobalScope ( ) override ;
2023-03-05 23:58:47 +00:00
// ^WindowOrWorkerGlobalScopeMixin
2024-12-03 02:37:33 +13:00
virtual DOM : : EventTarget & this_impl ( ) override { return * this ; }
virtual DOM : : EventTarget const & this_impl ( ) const override { return * this ; }
2023-03-05 23:58:47 +00:00
2024-11-08 16:35:16 +13:00
using UniversalGlobalScopeMixin : : atob ;
using UniversalGlobalScopeMixin : : btoa ;
using UniversalGlobalScopeMixin : : queue_microtask ;
using UniversalGlobalScopeMixin : : structured_clone ;
2023-11-08 11:47:41 -07:00
using WindowOrWorkerGlobalScopeMixin : : clear_interval ;
using WindowOrWorkerGlobalScopeMixin : : clear_timeout ;
2024-04-04 21:48:58 -04:00
using WindowOrWorkerGlobalScopeMixin : : create_image_bitmap ;
2023-11-08 11:47:41 -07:00
using WindowOrWorkerGlobalScopeMixin : : fetch ;
2024-03-30 10:04:31 +00:00
using WindowOrWorkerGlobalScopeMixin : : performance ;
2023-11-08 11:47:41 -07:00
using WindowOrWorkerGlobalScopeMixin : : set_interval ;
using WindowOrWorkerGlobalScopeMixin : : set_timeout ;
2022-02-06 19:12:57 -07:00
// Following methods are from the WorkerGlobalScope IDL definition
// https://html.spec.whatwg.org/multipage/workers.html#the-workerglobalscope-common-interface
// https://html.spec.whatwg.org/multipage/workers.html#dom-workerglobalscope-self
2024-11-15 04:01:23 +13:00
GC : : Ref < WorkerGlobalScope const > self ( ) const { return * this ; }
2022-02-06 19:12:57 -07:00
2024-11-15 04:01:23 +13:00
GC : : Ref < WorkerLocation > location ( ) const ;
GC : : Ref < WorkerNavigator > navigator ( ) const ;
2024-05-23 13:57:08 +01:00
WebIDL : : ExceptionOr < void > import_scripts ( Vector < String > const & urls , PerformTheFetchHook = nullptr ) ;
2022-02-06 19:12:57 -07:00
# undef __ENUMERATE
2022-09-24 16:02:41 +01:00
# define __ENUMERATE(attribute_name, event_name) \
void set_ # # attribute_name ( WebIDL : : CallbackType * ) ; \
WebIDL : : CallbackType * attribute_name ( ) ;
2022-02-06 19:12:57 -07:00
ENUMERATE_WORKER_GLOBAL_SCOPE_EVENT_HANDLERS ( __ENUMERATE )
# undef __ENUMERATE
2024-11-15 04:01:23 +13:00
GC : : Ref < CSS : : FontFaceSet > fonts ( ) ;
2024-05-07 14:49:31 -06:00
2022-02-06 19:12:57 -07:00
// Non-IDL public methods
2024-03-18 16:22:27 +13:00
URL : : URL const & url ( ) const { return m_url . value ( ) ; }
void set_url ( URL : : URL const & url ) { m_url = url ; }
2022-02-06 19:12:57 -07:00
2025-05-18 14:04:29 -06:00
String const & name ( ) const { return m_name ; }
void set_name ( String name ) { m_name = move ( name ) ; }
Bindings : : WorkerType type ( ) const { return m_type ; }
void set_type ( Bindings : : WorkerType type ) { m_type = type ; }
2022-02-06 19:12:57 -07:00
// Spec note: While the WorkerLocation object is created after the WorkerGlobalScope object,
// this is not problematic as it cannot be observed from script.
2024-11-15 04:01:23 +13:00
void set_location ( GC : : Ref < WorkerLocation > loc ) { m_location = move ( loc ) ; }
2022-02-06 19:12:57 -07:00
2024-11-15 04:01:23 +13:00
void set_internal_port ( GC : : Ref < MessagePort > port ) ;
2023-11-22 09:57:22 -07:00
2024-07-09 15:54:22 -06:00
void initialize_web_interfaces ( Badge < WorkerEnvironmentSettingsObject > ) { initialize_web_interfaces_impl ( ) ; }
2023-11-08 11:47:41 -07:00
2023-12-20 13:47:01 -07:00
Web : : Page * page ( ) { return m_page . ptr ( ) ; }
2023-11-08 11:47:41 -07:00
2024-11-25 14:30:12 +00:00
GC : : Ref < PolicyContainer > policy_container ( ) const ;
2024-05-23 11:46:41 +01:00
2024-07-09 03:02:22 -06:00
bool is_closing ( ) const { return m_closing ; }
2024-11-25 17:01:26 +00:00
void initialize_policy_container ( GC : : Ref < Fetch : : Infrastructure : : Response const > response , GC : : Ref < EnvironmentSettingsObject > environment ) ;
[ [ nodiscard ] ] ContentSecurityPolicy : : Directives : : Directive : : Result run_csp_initialization ( ) const ;
2022-02-06 19:12:57 -07:00
protected :
2024-11-15 04:01:23 +13:00
explicit WorkerGlobalScope ( JS : : Realm & , GC : : Ref < Web : : Page > ) ;
2022-02-06 19:12:57 -07:00
2024-07-09 15:54:22 -06:00
virtual void initialize_web_interfaces_impl ( ) ;
void close_a_worker ( ) ;
virtual void finalize ( ) override ;
2024-11-15 04:01:23 +13:00
GC : : Ptr < MessagePort > m_internal_port ;
2024-07-09 15:54:22 -06:00
2022-02-06 19:12:57 -07:00
private :
2025-04-18 10:21:36 +02:00
virtual bool is_window_or_worker_global_scope_mixin ( ) const final { return true ; }
2022-09-04 14:30:38 +02:00
virtual void visit_edges ( Cell : : Visitor & ) override ;
2024-11-15 04:01:23 +13:00
GC : : Ptr < WorkerLocation > m_location ;
GC : : Ptr < WorkerNavigator > m_navigator ;
2022-02-06 19:12:57 -07:00
2024-11-15 04:01:23 +13:00
GC : : Ref < Web : : Page > m_page ;
2023-11-22 09:57:22 -07:00
2022-02-06 19:12:57 -07:00
// https://html.spec.whatwg.org/multipage/workers.html#concept-WorkerGlobalScope-owner-set
2025-05-18 14:04:29 -06:00
// FIXME: 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.
2022-02-06 19:12:57 -07:00
// Note: It is a set, instead of a single owner, to accommodate SharedWorkerGlobalScope objects.
// https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-type
// A WorkerGlobalScope object has an associated type ("classic" or "module"). It is set during creation.
2025-05-18 14:04:29 -06:00
Bindings : : WorkerType m_type { Bindings : : WorkerType : : Classic } ;
2022-02-06 19:12:57 -07:00
// https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-url
// A WorkerGlobalScope object has an associated url (null or a URL). It is initially null.
2024-03-18 16:22:27 +13:00
Optional < URL : : URL > m_url ;
2022-02-06 19:12:57 -07:00
// https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-name
// A WorkerGlobalScope object has an associated name (a string). It is set during creation.
// Note: The name can have different semantics for each subclass of WorkerGlobalScope.
// For DedicatedWorkerGlobalScope instances, it is simply a developer-supplied name, useful mostly for debugging purposes.
// For SharedWorkerGlobalScope instances, it allows obtaining a reference to a common shared worker via the SharedWorker() constructor.
// For ServiceWorkerGlobalScope objects, it doesn't make sense (and as such isn't exposed through the JavaScript API at all).
2025-05-18 14:04:29 -06:00
String m_name ;
2022-02-06 19:12:57 -07:00
// https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-policy-container
// A WorkerGlobalScope object has an associated policy container (a policy container). It is initially a new policy container.
2024-11-25 14:30:12 +00:00
mutable GC : : Ptr < PolicyContainer > m_policy_container ;
2022-02-06 19:12:57 -07:00
// https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-embedder-policy
// A WorkerGlobalScope object has an associated embedder policy (an embedder policy).
2025-05-18 14:04:29 -06:00
// FIXME: Should be removed, https://github.com/whatwg/html/issues/11316
2022-02-06 19:12:57 -07:00
// https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-module-map
2025-05-18 14:04:29 -06:00
// FIXME: A WorkerGlobalScope object has an associated module map. It is a module map, initially empty.
2022-02-06 19:12:57 -07:00
// https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-cross-origin-isolated-capability
bool m_cross_origin_isolated_capability { false } ;
2024-05-07 14:49:31 -06:00
2024-07-09 03:02:22 -06:00
// https://html.spec.whatwg.org/multipage/workers.html#dom-workerglobalscope-closing
bool m_closing { false } ;
2024-05-07 14:49:31 -06:00
// https://drafts.csswg.org/css-font-loading/#font-source
2024-11-15 04:01:23 +13:00
GC : : Ptr < CSS : : FontFaceSet > m_fonts ;
2022-02-06 19:12:57 -07:00
} ;
}