Window.h 6.1 KB

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