WindowOrWorkerGlobalScope.h 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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/MessagePort.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. WebIDL::ExceptionOr<JS::Value> structured_clone(JS::Value, StructuredSerializeOptions const&) const;
  36. JS::NonnullGCPtr<JS::Promise> fetch(Fetch::RequestInfo const&, Fetch::RequestInit const&) const;
  37. i32 set_timeout(TimerHandler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
  38. i32 set_interval(TimerHandler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
  39. void clear_timeout(i32);
  40. void clear_interval(i32);
  41. void clear_map_of_active_timers();
  42. PerformanceTimeline::PerformanceEntryTuple& relevant_performance_entry_tuple(FlyString const& entry_type);
  43. void queue_performance_entry(JS::NonnullGCPtr<PerformanceTimeline::PerformanceEntry> new_entry);
  44. void clear_performance_entry_buffer(Badge<HighResolutionTime::Performance>, FlyString const& entry_type);
  45. void remove_entries_from_performance_entry_buffer(Badge<HighResolutionTime::Performance>, FlyString const& entry_type, String entry_name);
  46. ErrorOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> filter_buffer_map_by_name_and_type(Optional<String> name, Optional<String> type) const;
  47. void register_performance_observer(Badge<PerformanceTimeline::PerformanceObserver>, JS::NonnullGCPtr<PerformanceTimeline::PerformanceObserver>);
  48. void unregister_performance_observer(Badge<PerformanceTimeline::PerformanceObserver>, JS::NonnullGCPtr<PerformanceTimeline::PerformanceObserver>);
  49. bool has_registered_performance_observer(JS::NonnullGCPtr<PerformanceTimeline::PerformanceObserver>);
  50. void queue_the_performance_observer_task();
  51. protected:
  52. void initialize(JS::Realm&);
  53. void visit_edges(JS::Cell::Visitor&);
  54. private:
  55. enum class Repeat {
  56. Yes,
  57. No,
  58. };
  59. i32 run_timer_initialization_steps(TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments, Repeat repeat, Optional<i32> previous_id = {});
  60. IDAllocator m_timer_id_allocator;
  61. HashMap<int, JS::NonnullGCPtr<Timer>> m_timers;
  62. // https://www.w3.org/TR/performance-timeline/#performance-timeline
  63. // Each global object has:
  64. // - a performance observer task queued flag
  65. bool m_performance_observer_task_queued { false };
  66. // - a list of registered performance observer objects that is initially empty
  67. OrderedHashTable<JS::NonnullGCPtr<PerformanceTimeline::PerformanceObserver>> m_registered_performance_observer_objects;
  68. // https://www.w3.org/TR/performance-timeline/#dfn-performance-entry-buffer-map
  69. // 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:
  70. // NOTE: See the PerformanceEntryTuple struct above for the map's value tuple.
  71. OrderedHashMap<FlyString, PerformanceTimeline::PerformanceEntryTuple> m_performance_entry_buffer_map;
  72. };
  73. }