WindowOrWorkerGlobalScope.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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<GC::Ref<WebIDL::CallbackType>, String>;
  22. // https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
  23. class WindowOrWorkerGlobalScopeMixin {
  24. public:
  25. virtual ~WindowOrWorkerGlobalScopeMixin();
  26. virtual DOM::EventTarget& this_impl() = 0;
  27. virtual DOM::EventTarget const& this_impl() const = 0;
  28. // JS API functions
  29. String origin() const;
  30. bool is_secure_context() const;
  31. bool cross_origin_isolated() const;
  32. GC::Ref<WebIDL::Promise> create_image_bitmap(ImageBitmapSource image, Optional<ImageBitmapOptions> options = {}) const;
  33. 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;
  34. GC::Ref<WebIDL::Promise> fetch(Fetch::RequestInfo const&, Fetch::RequestInit const&) const;
  35. i32 set_timeout(TimerHandler, i32 timeout, GC::RootVector<JS::Value> arguments);
  36. i32 set_interval(TimerHandler, i32 timeout, GC::RootVector<JS::Value> arguments);
  37. void clear_timeout(i32);
  38. void clear_interval(i32);
  39. void clear_map_of_active_timers();
  40. PerformanceTimeline::PerformanceEntryTuple& relevant_performance_entry_tuple(FlyString const& entry_type);
  41. void queue_performance_entry(GC::Ref<PerformanceTimeline::PerformanceEntry> new_entry);
  42. void clear_performance_entry_buffer(Badge<HighResolutionTime::Performance>, FlyString const& entry_type);
  43. void remove_entries_from_performance_entry_buffer(Badge<HighResolutionTime::Performance>, FlyString const& entry_type, String entry_name);
  44. ErrorOr<Vector<GC::Root<PerformanceTimeline::PerformanceEntry>>> filter_buffer_map_by_name_and_type(Optional<String> name, Optional<String> type) const;
  45. void register_performance_observer(Badge<PerformanceTimeline::PerformanceObserver>, GC::Ref<PerformanceTimeline::PerformanceObserver>);
  46. void unregister_performance_observer(Badge<PerformanceTimeline::PerformanceObserver>, GC::Ref<PerformanceTimeline::PerformanceObserver>);
  47. bool has_registered_performance_observer(GC::Ref<PerformanceTimeline::PerformanceObserver>);
  48. void queue_the_performance_observer_task();
  49. void register_event_source(Badge<EventSource>, GC::Ref<EventSource>);
  50. void unregister_event_source(Badge<EventSource>, GC::Ref<EventSource>);
  51. void forcibly_close_all_event_sources();
  52. void run_steps_after_a_timeout(i32 timeout, Function<void()> completion_step);
  53. [[nodiscard]] GC::Ref<HighResolutionTime::Performance> performance();
  54. GC::Ref<JS::Object> supported_entry_types() const;
  55. GC::Ref<IndexedDB::IDBFactory> indexed_db();
  56. void report_error(JS::Value e);
  57. void report_an_exception(JS::Value const& e);
  58. [[nodiscard]] GC::Ref<Crypto::Crypto> crypto();
  59. protected:
  60. void initialize(JS::Realm&);
  61. void visit_edges(JS::Cell::Visitor&);
  62. void finalize();
  63. private:
  64. enum class Repeat {
  65. Yes,
  66. No,
  67. };
  68. i32 run_timer_initialization_steps(TimerHandler handler, i32 timeout, GC::RootVector<JS::Value> arguments, Repeat repeat, Optional<i32> previous_id = {});
  69. void run_steps_after_a_timeout_impl(i32 timeout, Function<void()> completion_step, Optional<i32> timer_key = {});
  70. 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;
  71. IDAllocator m_timer_id_allocator;
  72. HashMap<int, GC::Ref<Timer>> m_timers;
  73. // https://www.w3.org/TR/performance-timeline/#performance-timeline
  74. // Each global object has:
  75. // - a performance observer task queued flag
  76. bool m_performance_observer_task_queued { false };
  77. // - a list of registered performance observer objects that is initially empty
  78. OrderedHashTable<GC::Ref<PerformanceTimeline::PerformanceObserver>> m_registered_performance_observer_objects;
  79. // https://www.w3.org/TR/performance-timeline/#dfn-performance-entry-buffer-map
  80. // 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:
  81. // NOTE: See the PerformanceEntryTuple struct above for the map's value tuple.
  82. OrderedHashMap<FlyString, PerformanceTimeline::PerformanceEntryTuple> m_performance_entry_buffer_map;
  83. HashTable<GC::Ref<EventSource>> m_registered_event_sources;
  84. GC::Ptr<HighResolutionTime::Performance> m_performance;
  85. GC::Ptr<IndexedDB::IDBFactory> m_indexed_db;
  86. mutable GC::Ptr<JS::Object> m_supported_entry_types_array;
  87. GC::Ptr<Crypto::Crypto> m_crypto;
  88. bool m_error_reporting_mode { false };
  89. };
  90. }