2023-03-05 23:58:47 +00:00
/*
* Copyright ( c ) 2023 , Linus Groh < linusg @ serenityos . org >
2023-03-22 19:12:57 +00:00
* Copyright ( c ) 2023 , Luke Wilde < lukew @ serenityos . org >
2023-03-05 23:58:47 +00:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
# pragma once
2023-03-22 19:12:57 +00:00
# include <AK/FlyString.h>
2023-03-05 23:58:47 +00:00
# include <AK/Forward.h>
2023-03-14 06:59:23 -04:00
# include <AK/HashMap.h>
# include <AK/IDAllocator.h>
# include <AK/Variant.h>
2023-03-05 23:58:47 +00:00
# include <LibWeb/Bindings/PlatformObject.h>
2025-07-19 19:35:33 -07:00
# include <LibWeb/Export.h>
2023-03-07 18:15:52 +00:00
# include <LibWeb/Fetch/Request.h>
2023-03-05 23:58:47 +00:00
# include <LibWeb/Forward.h>
2024-04-04 21:48:58 -04:00
# include <LibWeb/HTML/ImageBitmap.h>
2023-03-22 19:12:57 +00:00
# include <LibWeb/PerformanceTimeline/PerformanceEntry.h>
# include <LibWeb/PerformanceTimeline/PerformanceEntryTuple.h>
2025-06-05 17:50:08 +02:00
# include <LibWeb/ServiceWorker/CacheStorage.h>
2025-02-25 12:41:52 +00:00
# include <LibWeb/WebSockets/WebSocket.h>
2023-03-05 23:58:47 +00:00
namespace Web : : HTML {
2025-02-04 13:01:46 +01:00
// https://html.spec.whatwg.org/multipage/webappapis.html#timerhandler
2024-11-15 04:01:23 +13:00
using TimerHandler = Variant < GC : : Ref < WebIDL : : CallbackType > , String > ;
2023-03-14 06:59:23 -04:00
2023-03-05 23:58:47 +00:00
// https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
2025-07-19 19:35:33 -07:00
class WEB_API WindowOrWorkerGlobalScopeMixin {
2023-03-05 23:58:47 +00:00
public :
virtual ~ WindowOrWorkerGlobalScopeMixin ( ) ;
2024-12-03 02:37:33 +13:00
virtual DOM : : EventTarget & this_impl ( ) = 0 ;
virtual DOM : : EventTarget const & this_impl ( ) const = 0 ;
2023-03-05 23:58:47 +00:00
// JS API functions
2024-11-23 20:10:34 +13:00
String origin ( ) const ;
2023-03-05 23:58:47 +00:00
bool is_secure_context ( ) const ;
bool cross_origin_isolated ( ) const ;
2024-11-15 04:01:23 +13:00
GC : : Ref < WebIDL : : Promise > create_image_bitmap ( ImageBitmapSource image , Optional < ImageBitmapOptions > options = { } ) const ;
GC : : Ref < WebIDL : : Promise > create_image_bitmap ( ImageBitmapSource image , WebIDL : : Long sx , WebIDL : : Long sy , WebIDL : : Long sw , WebIDL : : Long sh , Optional < ImageBitmapOptions > options = { } ) const ;
GC : : Ref < WebIDL : : Promise > fetch ( Fetch : : RequestInfo const & , Fetch : : RequestInit const & ) const ;
2023-03-14 06:59:23 -04:00
2024-12-26 14:32:52 +01:00
i32 set_timeout ( TimerHandler , i32 timeout , GC : : RootVector < JS : : Value > arguments ) ;
i32 set_interval ( TimerHandler , i32 timeout , GC : : RootVector < JS : : Value > arguments ) ;
2023-03-14 06:59:23 -04:00
void clear_timeout ( i32 ) ;
void clear_interval ( i32 ) ;
2023-09-26 11:44:56 +02:00
void clear_map_of_active_timers ( ) ;
2023-03-14 06:59:23 -04:00
2025-02-26 14:58:17 +00:00
enum class CheckIfPerformanceBufferIsFull {
No ,
Yes ,
} ;
2023-03-23 17:07:52 +00:00
PerformanceTimeline : : PerformanceEntryTuple & relevant_performance_entry_tuple ( FlyString const & entry_type ) ;
2024-11-15 04:01:23 +13:00
void queue_performance_entry ( GC : : Ref < PerformanceTimeline : : PerformanceEntry > new_entry ) ;
2025-02-26 14:58:17 +00:00
void add_performance_entry ( GC : : Ref < PerformanceTimeline : : PerformanceEntry > new_entry , CheckIfPerformanceBufferIsFull check_if_performance_buffer_is_full = CheckIfPerformanceBufferIsFull : : No ) ;
2023-03-23 17:07:52 +00:00
void clear_performance_entry_buffer ( Badge < HighResolutionTime : : Performance > , FlyString const & entry_type ) ;
void remove_entries_from_performance_entry_buffer ( Badge < HighResolutionTime : : Performance > , FlyString const & entry_type , String entry_name ) ;
2024-11-15 04:01:23 +13:00
ErrorOr < Vector < GC : : Root < PerformanceTimeline : : PerformanceEntry > > > filter_buffer_map_by_name_and_type ( Optional < String > name , Optional < String > type ) const ;
2023-03-22 19:12:57 +00:00
2024-11-15 04:01:23 +13:00
void register_performance_observer ( Badge < PerformanceTimeline : : PerformanceObserver > , GC : : Ref < PerformanceTimeline : : PerformanceObserver > ) ;
void unregister_performance_observer ( Badge < PerformanceTimeline : : PerformanceObserver > , GC : : Ref < PerformanceTimeline : : PerformanceObserver > ) ;
bool has_registered_performance_observer ( GC : : Ref < PerformanceTimeline : : PerformanceObserver > ) ;
2023-08-25 01:26:47 +01:00
void queue_the_performance_observer_task ( ) ;
2025-02-26 15:51:05 +00:00
void set_resource_timing_buffer_size_limit ( Badge < HighResolutionTime : : Performance > , u32 value ) { m_resource_timing_buffer_size_limit = value ; }
void add_resource_timing_entry ( Badge < ResourceTiming : : PerformanceResourceTiming > , GC : : Ref < ResourceTiming : : PerformanceResourceTiming > entry ) ;
2024-11-15 04:01:23 +13:00
void register_event_source ( Badge < EventSource > , GC : : Ref < EventSource > ) ;
void unregister_event_source ( Badge < EventSource > , GC : : Ref < EventSource > ) ;
2024-04-05 13:33:41 -04:00
void forcibly_close_all_event_sources ( ) ;
2025-02-25 12:41:52 +00:00
void register_web_socket ( Badge < WebSockets : : WebSocket > , GC : : Ref < WebSockets : : WebSocket > ) ;
void unregister_web_socket ( Badge < WebSockets : : WebSocket > , GC : : Ref < WebSockets : : WebSocket > ) ;
enum class AffectedAnyWebSockets {
No ,
Yes ,
} ;
AffectedAnyWebSockets make_disappear_all_web_sockets ( ) ;
2024-02-26 17:52:32 +00:00
void run_steps_after_a_timeout ( i32 timeout , Function < void ( ) > completion_step ) ;
2024-11-15 04:01:23 +13:00
[ [ nodiscard ] ] GC : : Ref < HighResolutionTime : : Performance > performance ( ) ;
2024-03-30 10:04:31 +00:00
2024-11-15 04:01:23 +13:00
GC : : Ref < JS : : Object > supported_entry_types ( ) const ;
2024-04-01 14:03:57 +01:00
2024-11-15 04:01:23 +13:00
GC : : Ref < IndexedDB : : IDBFactory > indexed_db ( ) ;
2024-05-19 17:58:06 +12:00
2024-07-04 00:28:49 +09:00
void report_error ( JS : : Value e ) ;
2025-01-12 18:57:16 +13:00
enum class OmitError {
Yes ,
No ,
} ;
void report_an_exception ( JS : : Value exception , OmitError = OmitError : : No ) ;
2024-07-04 00:28:49 +09:00
2024-11-15 04:01:23 +13:00
[ [ nodiscard ] ] GC : : Ref < Crypto : : Crypto > crypto ( ) ;
2024-09-17 16:44:42 +01:00
2025-06-05 17:50:08 +02:00
[ [ nodiscard ] ] GC : : Ref < ServiceWorker : : CacheStorage > caches ( ) ;
2025-05-31 02:24:26 +02:00
[ [ nodiscard ] ] GC : : Ref < TrustedTypes : : TrustedTypePolicyFactory > trusted_types ( ) ;
2023-03-14 06:59:23 -04:00
protected :
2023-08-07 08:41:28 +02:00
void initialize ( JS : : Realm & ) ;
2023-03-14 06:59:23 -04:00
void visit_edges ( JS : : Cell : : Visitor & ) ;
2024-01-03 10:10:47 +01:00
void finalize ( ) ;
2023-03-14 06:59:23 -04:00
private :
enum class Repeat {
Yes ,
No ,
} ;
2024-12-26 14:32:52 +01:00
i32 run_timer_initialization_steps ( TimerHandler handler , i32 timeout , GC : : RootVector < JS : : Value > arguments , Repeat repeat , Optional < i32 > previous_id = { } ) ;
2024-02-26 17:52:32 +00:00
void run_steps_after_a_timeout_impl ( i32 timeout , Function < void ( ) > completion_step , Optional < i32 > timer_key = { } ) ;
2023-03-14 06:59:23 -04:00
2024-11-15 04:01:23 +13:00
GC : : Ref < WebIDL : : Promise > create_image_bitmap_impl ( ImageBitmapSource & image , Optional < WebIDL : : Long > sx , Optional < WebIDL : : Long > sy , Optional < WebIDL : : Long > sw , Optional < WebIDL : : Long > sh , Optional < ImageBitmapOptions > & options ) const ;
2024-04-04 21:48:58 -04:00
2025-02-26 15:51:05 +00:00
size_t resource_timing_buffer_current_size ( ) ;
bool can_add_resource_timing_entry ( ) ;
void fire_resource_timing_buffer_full_event ( ) ;
void copy_resource_timing_secondary_buffer ( ) ;
2023-03-14 06:59:23 -04:00
IDAllocator m_timer_id_allocator ;
2024-11-15 04:01:23 +13:00
HashMap < int , GC : : Ref < Timer > > m_timers ;
2023-03-22 19:12:57 +00:00
// https://www.w3.org/TR/performance-timeline/#performance-timeline
// Each global object has:
2023-08-25 01:26:47 +01:00
// - a performance observer task queued flag
bool m_performance_observer_task_queued { false } ;
// - a list of registered performance observer objects that is initially empty
2024-11-15 04:01:23 +13:00
OrderedHashTable < GC : : Ref < PerformanceTimeline : : PerformanceObserver > > m_registered_performance_observer_objects ;
2023-03-22 19:12:57 +00:00
// https://www.w3.org/TR/performance-timeline/#dfn-performance-entry-buffer-map
// a performance entry buffer map map, keyed on a DOMString, representing the entry type to which the buffer belongs. The map's value is the following tuple:
// NOTE: See the PerformanceEntryTuple struct above for the map's value tuple.
OrderedHashMap < FlyString , PerformanceTimeline : : PerformanceEntryTuple > m_performance_entry_buffer_map ;
2024-03-30 10:04:31 +00:00
2024-11-15 04:01:23 +13:00
HashTable < GC : : Ref < EventSource > > m_registered_event_sources ;
2024-04-05 13:33:41 -04:00
2024-11-15 04:01:23 +13:00
GC : : Ptr < HighResolutionTime : : Performance > m_performance ;
2024-04-01 14:03:57 +01:00
2024-11-15 04:01:23 +13:00
GC : : Ptr < IndexedDB : : IDBFactory > m_indexed_db ;
2024-05-19 17:58:06 +12:00
2024-11-15 04:01:23 +13:00
mutable GC : : Ptr < JS : : Object > m_supported_entry_types_array ;
2024-07-04 00:28:49 +09:00
2024-11-15 04:01:23 +13:00
GC : : Ptr < Crypto : : Crypto > m_crypto ;
2024-09-17 16:44:42 +01:00
2025-06-05 17:50:08 +02:00
GC : : Ptr < ServiceWorker : : CacheStorage > m_cache_storage ;
2025-05-31 02:24:26 +02:00
GC : : Ptr < TrustedTypes : : TrustedTypePolicyFactory > m_trusted_type_policy_factory ;
2024-07-04 00:28:49 +09:00
bool m_error_reporting_mode { false } ;
2025-02-25 12:41:52 +00:00
WebSockets : : WebSocket : : List m_registered_web_sockets ;
2025-02-26 15:51:05 +00:00
// https://w3c.github.io/resource-timing/#sec-extensions-performance-interface
// Each ECMAScript global environment has:
// https://w3c.github.io/resource-timing/#dfn-resource-timing-buffer-size-limit
// A resource timing buffer size limit which should initially be 250 or greater.
// The recommended minimum number of PerformanceResourceTiming objects is 250, though this may be changed by the
// user agent. setResourceTimingBufferSize can be called to request a change to this limit.
u32 m_resource_timing_buffer_size_limit { 250 } ;
// https://w3c.github.io/resource-timing/#dfn-resource-timing-buffer-full-event-pending-flag
// A resource timing buffer full event pending flag which is initially false.
bool m_resource_timing_buffer_full_event_pending { false } ;
// https://w3c.github.io/resource-timing/#dfn-resource-timing-secondary-buffer-current-size
// A resource timing secondary buffer current size which is initially 0.
// https://w3c.github.io/resource-timing/#dfn-resource-timing-secondary-buffer
// A resource timing secondary buffer to store PerformanceResourceTiming objects that is initially empty.
Vector < GC : : Ref < ResourceTiming : : PerformanceResourceTiming > > m_resource_timing_secondary_buffer ;
2023-03-05 23:58:47 +00:00
} ;
}