Window.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Badge.h>
  8. #include <AK/IDAllocator.h>
  9. #include <AK/RefCounted.h>
  10. #include <AK/RefPtr.h>
  11. #include <AK/Weakable.h>
  12. #include <LibWeb/Bindings/WindowObject.h>
  13. #include <LibWeb/Bindings/Wrappable.h>
  14. #include <LibWeb/CSS/MediaQueryList.h>
  15. #include <LibWeb/CSS/Screen.h>
  16. #include <LibWeb/DOM/Document.h>
  17. #include <LibWeb/DOM/Event.h>
  18. #include <LibWeb/DOM/EventTarget.h>
  19. #include <LibWeb/HTML/BrowsingContext.h>
  20. #include <LibWeb/HTML/GlobalEventHandlers.h>
  21. namespace Web::HTML {
  22. class RequestAnimationFrameCallback;
  23. class IdleCallback;
  24. class Window final
  25. : public RefCounted<Window>
  26. , public Weakable<Window>
  27. , public DOM::EventTarget
  28. , public HTML::GlobalEventHandlers {
  29. public:
  30. static NonnullRefPtr<Window> create_with_document(DOM::Document&);
  31. ~Window();
  32. using RefCounted::ref;
  33. using RefCounted::unref;
  34. virtual void ref_event_target() override { RefCounted::ref(); }
  35. virtual void unref_event_target() override { RefCounted::unref(); }
  36. virtual bool dispatch_event(NonnullRefPtr<DOM::Event>) override;
  37. virtual JS::Object* create_wrapper(JS::GlobalObject&) override;
  38. Page* page();
  39. Page const* page() const;
  40. // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
  41. DOM::Document const& associated_document() const { return *m_associated_document; }
  42. DOM::Document& associated_document() { return *m_associated_document; }
  43. // https://html.spec.whatwg.org/multipage/window-object.html#window-bc
  44. HTML::BrowsingContext const* browsing_context() const { return m_associated_document->browsing_context(); }
  45. HTML::BrowsingContext* browsing_context() { return m_associated_document->browsing_context(); }
  46. void alert(String const&);
  47. bool confirm(String const&);
  48. String prompt(String const&, String const&);
  49. i32 request_animation_frame(NonnullOwnPtr<Bindings::CallbackType> js_callback);
  50. void cancel_animation_frame(i32);
  51. bool has_animation_frame_callbacks() const { return !m_request_animation_frame_callbacks.is_empty(); }
  52. i32 set_timeout(Bindings::TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
  53. i32 set_interval(Bindings::TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
  54. void clear_timeout(i32);
  55. void clear_interval(i32);
  56. void queue_microtask(NonnullOwnPtr<Bindings::CallbackType> callback);
  57. int inner_width() const;
  58. int inner_height() const;
  59. void did_set_location_href(Badge<Bindings::LocationObject>, AK::URL const& new_href);
  60. void did_call_location_reload(Badge<Bindings::LocationObject>);
  61. void did_call_location_replace(Badge<Bindings::LocationObject>, String url);
  62. Bindings::WindowObject* wrapper() { return m_wrapper; }
  63. Bindings::WindowObject const* wrapper() const { return m_wrapper; }
  64. void set_wrapper(Badge<Bindings::WindowObject>, Bindings::WindowObject&);
  65. void deallocate_timer_id(Badge<Timer>, i32);
  66. HighResolutionTime::Performance& performance() { return *m_performance; }
  67. Crypto::Crypto& crypto() { return *m_crypto; }
  68. CSS::Screen& screen() { return *m_screen; }
  69. DOM::Event const* current_event() const { return m_current_event; }
  70. void set_current_event(DOM::Event* event) { m_current_event = event; }
  71. NonnullRefPtr<CSS::CSSStyleDeclaration> get_computed_style(DOM::Element&) const;
  72. NonnullRefPtr<CSS::MediaQueryList> match_media(String);
  73. Optional<CSS::MediaFeatureValue> query_media_feature(CSS::MediaFeatureID) const;
  74. float scroll_x() const;
  75. float scroll_y() const;
  76. void fire_a_page_transition_event(FlyString const& event_name, bool persisted);
  77. float device_pixel_ratio() const;
  78. int screen_x() const;
  79. int screen_y() const;
  80. Selection::Selection* get_selection();
  81. RefPtr<HTML::Storage> local_storage();
  82. RefPtr<HTML::Storage> session_storage();
  83. Window* parent();
  84. DOM::ExceptionOr<void> post_message(JS::Value, String const& target_origin);
  85. String name() const;
  86. void set_name(String const&);
  87. void start_an_idle_period();
  88. u32 request_idle_callback(NonnullOwnPtr<Bindings::CallbackType> callback);
  89. void cancel_idle_callback(u32);
  90. private:
  91. explicit Window(DOM::Document&);
  92. // ^HTML::GlobalEventHandlers
  93. virtual DOM::EventTarget& global_event_handlers_to_event_target() override { return *this; }
  94. enum class Repeat {
  95. Yes,
  96. No,
  97. };
  98. i32 run_timer_initialization_steps(Bindings::TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments, Repeat repeat, Optional<i32> previous_id = {});
  99. void invoke_idle_callbacks();
  100. // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
  101. WeakPtr<DOM::Document> m_associated_document;
  102. WeakPtr<Bindings::WindowObject> m_wrapper;
  103. IDAllocator m_timer_id_allocator;
  104. HashMap<int, NonnullRefPtr<Timer>> m_timers;
  105. NonnullOwnPtr<HighResolutionTime::Performance> m_performance;
  106. NonnullRefPtr<Crypto::Crypto> m_crypto;
  107. NonnullOwnPtr<CSS::Screen> m_screen;
  108. RefPtr<DOM::Event> m_current_event;
  109. HashMap<i32, NonnullRefPtr<RequestAnimationFrameCallback>> m_request_animation_frame_callbacks;
  110. // https://w3c.github.io/requestidlecallback/#dfn-list-of-idle-request-callbacks
  111. NonnullRefPtrVector<IdleCallback> m_idle_request_callbacks;
  112. // https://w3c.github.io/requestidlecallback/#dfn-list-of-runnable-idle-callbacks
  113. NonnullRefPtrVector<IdleCallback> m_runnable_idle_callbacks;
  114. // https://w3c.github.io/requestidlecallback/#dfn-idle-callback-identifier
  115. u32 m_idle_callback_identifier = 0;
  116. };
  117. void run_animation_frame_callbacks(DOM::Document&, double now);
  118. }