WindowOrWorkerGlobalScope.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2023, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/FlyString.h>
  9. #include <AK/Forward.h>
  10. #include <AK/HashMap.h>
  11. #include <AK/IDAllocator.h>
  12. #include <AK/Variant.h>
  13. #include <LibWeb/Bindings/PlatformObject.h>
  14. #include <LibWeb/Fetch/Request.h>
  15. #include <LibWeb/Forward.h>
  16. #include <LibWeb/HTML/ImageBitmap.h>
  17. #include <LibWeb/PerformanceTimeline/PerformanceEntry.h>
  18. #include <LibWeb/PerformanceTimeline/PerformanceEntryTuple.h>
  19. namespace Web::HTML {
  20. // https://html.spec.whatwg.org/#timerhandler
  21. using TimerHandler = Variant<JS::NonnullGCPtr<WebIDL::CallbackType>, String>;
  22. // https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
  23. class WindowOrWorkerGlobalScopeMixin {
  24. public:
  25. virtual ~WindowOrWorkerGlobalScopeMixin();
  26. virtual Bindings::PlatformObject& this_impl() = 0;
  27. virtual Bindings::PlatformObject const& this_impl() const = 0;
  28. // JS API functions
  29. WebIDL::ExceptionOr<String> origin() const;
  30. bool is_secure_context() const;
  31. bool cross_origin_isolated() const;
  32. WebIDL::ExceptionOr<String> btoa(String const& data) const;
  33. WebIDL::ExceptionOr<String> atob(String const& data) const;
  34. void queue_microtask(WebIDL::CallbackType&);
  35. JS::NonnullGCPtr<JS::Promise> create_image_bitmap(ImageBitmapSource image, Optional<ImageBitmapOptions> options = {}) const;
  36. JS::NonnullGCPtr<JS::Promise> create_image_bitmap(ImageBitmapSource image, WebIDL::Long sx, WebIDL::Long sy, WebIDL::Long sw, WebIDL::Long sh, Optional<ImageBitmapOptions> options = {}) const;
  37. WebIDL::ExceptionOr<JS::Value> structured_clone(JS::Value, StructuredSerializeOptions const&) const;
  38. JS::NonnullGCPtr<JS::Promise> fetch(Fetch::RequestInfo const&, Fetch::RequestInit const&) const;
  39. i32 set_timeout(TimerHandler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
  40. i32 set_interval(TimerHandler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
  41. void clear_timeout(i32);
  42. void clear_interval(i32);
  43. void clear_map_of_active_timers();
  44. PerformanceTimeline::PerformanceEntryTuple& relevant_performance_entry_tuple(FlyString const& entry_type);
  45. void queue_performance_entry(JS::NonnullGCPtr<PerformanceTimeline::PerformanceEntry> new_entry);
  46. void clear_performance_entry_buffer(Badge<HighResolutionTime::Performance>, FlyString const& entry_type);
  47. void remove_entries_from_performance_entry_buffer(Badge<HighResolutionTime::Performance>, FlyString const& entry_type, String entry_name);
  48. ErrorOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> filter_buffer_map_by_name_and_type(Optional<String> name, Optional<String> type) const;
  49. void register_performance_observer(Badge<PerformanceTimeline::PerformanceObserver>, JS::NonnullGCPtr<PerformanceTimeline::PerformanceObserver>);
  50. void unregister_performance_observer(Badge<PerformanceTimeline::PerformanceObserver>, JS::NonnullGCPtr<PerformanceTimeline::PerformanceObserver>);
  51. bool has_registered_performance_observer(JS::NonnullGCPtr<PerformanceTimeline::PerformanceObserver>);
  52. void queue_the_performance_observer_task();
  53. void register_event_source(Badge<EventSource>, JS::NonnullGCPtr<EventSource>);
  54. void unregister_event_source(Badge<EventSource>, JS::NonnullGCPtr<EventSource>);
  55. void forcibly_close_all_event_sources();
  56. void run_steps_after_a_timeout(i32 timeout, Function<void()> completion_step);
  57. [[nodiscard]] JS::NonnullGCPtr<HighResolutionTime::Performance> performance();
  58. JS::NonnullGCPtr<JS::Object> supported_entry_types() const;
  59. JS::NonnullGCPtr<IndexedDB::IDBFactory> indexed_db();
  60. void report_error(JS::Value e);
  61. void report_an_exception(JS::Value const& e);
  62. [[nodiscard]] JS::NonnullGCPtr<Crypto::Crypto> crypto();
  63. protected:
  64. void initialize(JS::Realm&);
  65. void visit_edges(JS::Cell::Visitor&);
  66. void finalize();
  67. private:
  68. enum class Repeat {
  69. Yes,
  70. No,
  71. };
  72. i32 run_timer_initialization_steps(TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments, Repeat repeat, Optional<i32> previous_id = {});
  73. void run_steps_after_a_timeout_impl(i32 timeout, Function<void()> completion_step, Optional<i32> timer_key = {});
  74. JS::NonnullGCPtr<JS::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;
  75. IDAllocator m_timer_id_allocator;
  76. HashMap<int, JS::NonnullGCPtr<Timer>> m_timers;
  77. // https://www.w3.org/TR/performance-timeline/#performance-timeline
  78. // Each global object has:
  79. // - a performance observer task queued flag
  80. bool m_performance_observer_task_queued { false };
  81. // - a list of registered performance observer objects that is initially empty
  82. OrderedHashTable<JS::NonnullGCPtr<PerformanceTimeline::PerformanceObserver>> m_registered_performance_observer_objects;
  83. // https://www.w3.org/TR/performance-timeline/#dfn-performance-entry-buffer-map
  84. // 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:
  85. // NOTE: See the PerformanceEntryTuple struct above for the map's value tuple.
  86. OrderedHashMap<FlyString, PerformanceTimeline::PerformanceEntryTuple> m_performance_entry_buffer_map;
  87. HashTable<JS::NonnullGCPtr<EventSource>> m_registered_event_sources;
  88. JS::GCPtr<HighResolutionTime::Performance> m_performance;
  89. JS::GCPtr<IndexedDB::IDBFactory> m_indexed_db;
  90. mutable JS::GCPtr<JS::Object> m_supported_entry_types_array;
  91. JS::GCPtr<Crypto::Crypto> m_crypto;
  92. bool m_error_reporting_mode { false };
  93. };
  94. }