Window.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 Window final
  24. : public RefCounted<Window>
  25. , public Weakable<Window>
  26. , public DOM::EventTarget
  27. , public HTML::GlobalEventHandlers {
  28. public:
  29. static NonnullRefPtr<Window> create_with_document(DOM::Document&);
  30. ~Window();
  31. using RefCounted::ref;
  32. using RefCounted::unref;
  33. virtual void ref_event_target() override { RefCounted::ref(); }
  34. virtual void unref_event_target() override { RefCounted::unref(); }
  35. virtual bool dispatch_event(NonnullRefPtr<DOM::Event>) override;
  36. virtual JS::Object* create_wrapper(JS::GlobalObject&) override;
  37. Page* page();
  38. Page const* page() const;
  39. // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
  40. DOM::Document const& associated_document() const { return *m_associated_document; }
  41. DOM::Document& associated_document() { return *m_associated_document; }
  42. // https://html.spec.whatwg.org/multipage/window-object.html#window-bc
  43. HTML::BrowsingContext const* browsing_context() const { return m_associated_document->browsing_context(); }
  44. HTML::BrowsingContext* browsing_context() { return m_associated_document->browsing_context(); }
  45. void alert(String const&);
  46. bool confirm(String const&);
  47. String prompt(String const&, String const&);
  48. i32 request_animation_frame(NonnullOwnPtr<Bindings::CallbackType> js_callback);
  49. void cancel_animation_frame(i32);
  50. i32 set_timeout(Bindings::TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
  51. i32 set_interval(Bindings::TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
  52. void clear_timeout(i32);
  53. void clear_interval(i32);
  54. void queue_microtask(NonnullOwnPtr<Bindings::CallbackType> callback);
  55. int inner_width() const;
  56. int inner_height() const;
  57. void did_set_location_href(Badge<Bindings::LocationObject>, AK::URL const& new_href);
  58. void did_call_location_reload(Badge<Bindings::LocationObject>);
  59. void did_call_location_replace(Badge<Bindings::LocationObject>, String url);
  60. Bindings::WindowObject* wrapper() { return m_wrapper; }
  61. Bindings::WindowObject const* wrapper() const { return m_wrapper; }
  62. void set_wrapper(Badge<Bindings::WindowObject>, Bindings::WindowObject&);
  63. void deallocate_timer_id(Badge<Timer>, i32);
  64. HighResolutionTime::Performance& performance() { return *m_performance; }
  65. Crypto::Crypto& crypto() { return *m_crypto; }
  66. CSS::Screen& screen() { return *m_screen; }
  67. DOM::Event const* current_event() const { return m_current_event; }
  68. void set_current_event(DOM::Event* event) { m_current_event = event; }
  69. NonnullRefPtr<CSS::CSSStyleDeclaration> get_computed_style(DOM::Element&) const;
  70. NonnullRefPtr<CSS::MediaQueryList> match_media(String);
  71. Optional<CSS::MediaFeatureValue> query_media_feature(CSS::MediaFeatureID) const;
  72. float scroll_x() const;
  73. float scroll_y() const;
  74. void fire_a_page_transition_event(FlyString const& event_name, bool persisted);
  75. float device_pixel_ratio() const;
  76. int screen_x() const;
  77. int screen_y() const;
  78. Selection::Selection* get_selection();
  79. RefPtr<HTML::Storage> local_storage();
  80. RefPtr<HTML::Storage> session_storage();
  81. Window* parent();
  82. DOM::ExceptionOr<void> post_message(JS::Value, String const& target_origin);
  83. String name() const;
  84. void set_name(String const&);
  85. private:
  86. explicit Window(DOM::Document&);
  87. // ^HTML::GlobalEventHandlers
  88. virtual DOM::EventTarget& global_event_handlers_to_event_target() override { return *this; }
  89. enum class Repeat {
  90. Yes,
  91. No,
  92. };
  93. i32 run_timer_initialization_steps(Bindings::TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments, Repeat repeat, Optional<i32> previous_id = {});
  94. // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
  95. WeakPtr<DOM::Document> m_associated_document;
  96. WeakPtr<Bindings::WindowObject> m_wrapper;
  97. IDAllocator m_timer_id_allocator;
  98. HashMap<int, NonnullRefPtr<Timer>> m_timers;
  99. NonnullOwnPtr<HighResolutionTime::Performance> m_performance;
  100. NonnullRefPtr<Crypto::Crypto> m_crypto;
  101. NonnullOwnPtr<CSS::Screen> m_screen;
  102. RefPtr<DOM::Event> m_current_event;
  103. HashMap<i32, NonnullRefPtr<RequestAnimationFrameCallback>> m_request_animation_frame_callbacks;
  104. };
  105. void run_animation_frame_callbacks(DOM::Document&, double now);
  106. }