Window.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Badge.h>
  9. #include <AK/IDAllocator.h>
  10. #include <AK/NonnullRefPtrVector.h>
  11. #include <AK/RefPtr.h>
  12. #include <AK/TypeCasts.h>
  13. #include <AK/URL.h>
  14. #include <LibJS/Heap/Heap.h>
  15. #include <LibWeb/Bindings/Intrinsics.h>
  16. #include <LibWeb/DOM/EventTarget.h>
  17. #include <LibWeb/Forward.h>
  18. #include <LibWeb/HTML/AnimationFrameCallbackDriver.h>
  19. #include <LibWeb/HTML/CrossOrigin/CrossOriginPropertyDescriptorMap.h>
  20. #include <LibWeb/HTML/GlobalEventHandlers.h>
  21. #include <LibWeb/HTML/Scripting/ImportMap.h>
  22. #include <LibWeb/HTML/WindowEventHandlers.h>
  23. namespace Web::HTML {
  24. class IdleCallback;
  25. // https://html.spec.whatwg.org/#timerhandler
  26. using TimerHandler = Variant<JS::Handle<WebIDL::CallbackType>, DeprecatedString>;
  27. class Window final
  28. : public DOM::EventTarget
  29. , public HTML::GlobalEventHandlers
  30. , public HTML::WindowEventHandlers {
  31. WEB_PLATFORM_OBJECT(Window, DOM::EventTarget);
  32. public:
  33. static JS::NonnullGCPtr<Window> create(JS::Realm&);
  34. ~Window();
  35. virtual bool dispatch_event(DOM::Event&) override;
  36. Page* page();
  37. Page const* page() const;
  38. // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
  39. DOM::Document const& associated_document() const { return *m_associated_document; }
  40. DOM::Document& associated_document() { return *m_associated_document; }
  41. void set_associated_document(DOM::Document&);
  42. // https://html.spec.whatwg.org/multipage/window-object.html#window-bc
  43. HTML::BrowsingContext const* browsing_context() const;
  44. HTML::BrowsingContext* browsing_context();
  45. JS::ThrowCompletionOr<size_t> document_tree_child_browsing_context_count() const;
  46. ImportMap const& import_map() const { return m_import_map; }
  47. bool import_maps_allowed() const { return m_import_maps_allowed; }
  48. void set_import_maps_allowed(bool import_maps_allowed) { m_import_maps_allowed = import_maps_allowed; }
  49. WebIDL::ExceptionOr<JS::GCPtr<HTML::WindowProxy>> open_impl(StringView url, StringView target, StringView features);
  50. void alert_impl(DeprecatedString const&);
  51. bool confirm_impl(DeprecatedString const&);
  52. DeprecatedString prompt_impl(DeprecatedString const&, DeprecatedString const&);
  53. i32 request_animation_frame_impl(WebIDL::CallbackType& js_callback);
  54. void cancel_animation_frame_impl(i32);
  55. bool has_animation_frame_callbacks() const { return m_animation_frame_callback_driver.has_callbacks(); }
  56. i32 set_timeout_impl(TimerHandler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
  57. i32 set_interval_impl(TimerHandler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
  58. void clear_timeout_impl(i32);
  59. void clear_interval_impl(i32);
  60. void queue_microtask_impl(WebIDL::CallbackType& callback);
  61. int inner_width() const;
  62. int inner_height() const;
  63. void did_set_location_href(Badge<HTML::Location>, AK::URL const& new_href);
  64. void did_call_location_reload(Badge<HTML::Location>);
  65. void did_call_location_replace(Badge<HTML::Location>, DeprecatedString url);
  66. void deallocate_timer_id(Badge<Timer>, i32);
  67. HighResolutionTime::Performance& performance();
  68. Crypto::Crypto& crypto() { return *m_crypto; }
  69. CSS::Screen& screen();
  70. DOM::Event* current_event() { return m_current_event.ptr(); }
  71. DOM::Event const* current_event() const { return m_current_event.ptr(); }
  72. void set_current_event(DOM::Event* event);
  73. CSS::CSSStyleDeclaration* get_computed_style_impl(DOM::Element&) const;
  74. JS::NonnullGCPtr<CSS::MediaQueryList> match_media_impl(DeprecatedString);
  75. Optional<CSS::MediaFeatureValue> query_media_feature(CSS::MediaFeatureID) const;
  76. float scroll_x() const;
  77. float scroll_y() const;
  78. void fire_a_page_transition_event(DeprecatedFlyString const& event_name, bool persisted);
  79. float device_pixel_ratio() const;
  80. int screen_x() const;
  81. int screen_y() const;
  82. int outer_width() const;
  83. int outer_height() const;
  84. JS::NonnullGCPtr<HTML::Storage> local_storage();
  85. JS::NonnullGCPtr<HTML::Storage> session_storage();
  86. // https://html.spec.whatwg.org/multipage/browsers.html#dom-parent
  87. WindowProxy* parent();
  88. WebIDL::ExceptionOr<void> post_message_impl(JS::Value, DeprecatedString const& target_origin);
  89. WebIDL::ExceptionOr<JS::Value> structured_clone_impl(JS::VM& vm, JS::Value);
  90. DeprecatedString name() const;
  91. void set_name(DeprecatedString const&);
  92. void start_an_idle_period();
  93. u32 request_idle_callback_impl(WebIDL::CallbackType& callback);
  94. void cancel_idle_callback_impl(u32);
  95. AnimationFrameCallbackDriver& animation_frame_callback_driver() { return m_animation_frame_callback_driver; }
  96. // https://html.spec.whatwg.org/multipage/interaction.html#transient-activation
  97. bool has_transient_activation() const;
  98. void initialize_web_interfaces(Badge<WindowEnvironmentSettingsObject>);
  99. private:
  100. explicit Window(JS::Realm&);
  101. virtual void visit_edges(Cell::Visitor&) override;
  102. // ^HTML::GlobalEventHandlers
  103. virtual DOM::EventTarget& global_event_handlers_to_event_target(DeprecatedFlyString const&) override { return *this; }
  104. // ^HTML::WindowEventHandlers
  105. virtual DOM::EventTarget& window_event_handlers_to_event_target() override { return *this; }
  106. enum class Repeat {
  107. Yes,
  108. No,
  109. };
  110. i32 run_timer_initialization_steps(TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments, Repeat repeat, Optional<i32> previous_id = {});
  111. void invoke_idle_callbacks();
  112. // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
  113. JS::GCPtr<DOM::Document> m_associated_document;
  114. JS::GCPtr<DOM::Event> m_current_event;
  115. IDAllocator m_timer_id_allocator;
  116. HashMap<int, JS::NonnullGCPtr<Timer>> m_timers;
  117. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-window-import-map
  118. ImportMap m_import_map;
  119. // https://html.spec.whatwg.org/multipage/webappapis.html#import-maps-allowed
  120. bool m_import_maps_allowed { true };
  121. JS::GCPtr<HighResolutionTime::Performance> m_performance;
  122. JS::GCPtr<Crypto::Crypto> m_crypto;
  123. JS::GCPtr<CSS::Screen> m_screen;
  124. JS::GCPtr<HTML::Navigator> m_navigator;
  125. AnimationFrameCallbackDriver m_animation_frame_callback_driver;
  126. // https://w3c.github.io/requestidlecallback/#dfn-list-of-idle-request-callbacks
  127. NonnullRefPtrVector<IdleCallback> m_idle_request_callbacks;
  128. // https://w3c.github.io/requestidlecallback/#dfn-list-of-runnable-idle-callbacks
  129. NonnullRefPtrVector<IdleCallback> m_runnable_idle_callbacks;
  130. // https://w3c.github.io/requestidlecallback/#dfn-idle-callback-identifier
  131. u32 m_idle_callback_identifier = 0;
  132. public:
  133. HTML::Origin origin() const;
  134. HTML::Location* location() { return m_location; }
  135. HTML::Location const* location() const { return m_location; }
  136. virtual JS::ThrowCompletionOr<bool> internal_set_prototype_of(JS::Object* prototype) override;
  137. CrossOriginPropertyDescriptorMap const& cross_origin_property_descriptor_map() const { return m_cross_origin_property_descriptor_map; }
  138. CrossOriginPropertyDescriptorMap& cross_origin_property_descriptor_map() { return m_cross_origin_property_descriptor_map; }
  139. private:
  140. JS_DECLARE_NATIVE_FUNCTION(length_getter);
  141. JS_DECLARE_NATIVE_FUNCTION(top_getter);
  142. JS_DECLARE_NATIVE_FUNCTION(document_getter);
  143. JS_DECLARE_NATIVE_FUNCTION(frame_element_getter);
  144. JS_DECLARE_NATIVE_FUNCTION(location_getter);
  145. JS_DECLARE_NATIVE_FUNCTION(location_setter);
  146. JS_DECLARE_NATIVE_FUNCTION(name_getter);
  147. JS_DECLARE_NATIVE_FUNCTION(name_setter);
  148. JS_DECLARE_NATIVE_FUNCTION(performance_getter);
  149. JS_DECLARE_NATIVE_FUNCTION(performance_setter);
  150. JS_DECLARE_NATIVE_FUNCTION(history_getter);
  151. JS_DECLARE_NATIVE_FUNCTION(screen_getter);
  152. JS_DECLARE_NATIVE_FUNCTION(event_getter);
  153. JS_DECLARE_NATIVE_FUNCTION(event_setter);
  154. JS_DECLARE_NATIVE_FUNCTION(inner_width_getter);
  155. JS_DECLARE_NATIVE_FUNCTION(inner_height_getter);
  156. JS_DECLARE_NATIVE_FUNCTION(window_getter);
  157. JS_DECLARE_NATIVE_FUNCTION(frames_getter);
  158. JS_DECLARE_NATIVE_FUNCTION(self_getter);
  159. JS_DECLARE_NATIVE_FUNCTION(parent_getter);
  160. JS_DECLARE_NATIVE_FUNCTION(device_pixel_ratio_getter);
  161. JS_DECLARE_NATIVE_FUNCTION(scroll_x_getter);
  162. JS_DECLARE_NATIVE_FUNCTION(scroll_y_getter);
  163. JS_DECLARE_NATIVE_FUNCTION(scroll);
  164. JS_DECLARE_NATIVE_FUNCTION(scroll_by);
  165. JS_DECLARE_NATIVE_FUNCTION(screen_x_getter);
  166. JS_DECLARE_NATIVE_FUNCTION(screen_y_getter);
  167. JS_DECLARE_NATIVE_FUNCTION(screen_left_getter);
  168. JS_DECLARE_NATIVE_FUNCTION(screen_top_getter);
  169. JS_DECLARE_NATIVE_FUNCTION(outer_width_getter);
  170. JS_DECLARE_NATIVE_FUNCTION(outer_height_getter);
  171. JS_DECLARE_NATIVE_FUNCTION(post_message);
  172. JS_DECLARE_NATIVE_FUNCTION(structured_clone);
  173. JS_DECLARE_NATIVE_FUNCTION(local_storage_getter);
  174. JS_DECLARE_NATIVE_FUNCTION(session_storage_getter);
  175. JS_DECLARE_NATIVE_FUNCTION(origin_getter);
  176. JS_DECLARE_NATIVE_FUNCTION(is_secure_context_getter);
  177. JS_DECLARE_NATIVE_FUNCTION(open);
  178. JS_DECLARE_NATIVE_FUNCTION(alert);
  179. JS_DECLARE_NATIVE_FUNCTION(confirm);
  180. JS_DECLARE_NATIVE_FUNCTION(prompt);
  181. JS_DECLARE_NATIVE_FUNCTION(set_interval);
  182. JS_DECLARE_NATIVE_FUNCTION(set_timeout);
  183. JS_DECLARE_NATIVE_FUNCTION(clear_interval);
  184. JS_DECLARE_NATIVE_FUNCTION(clear_timeout);
  185. JS_DECLARE_NATIVE_FUNCTION(request_animation_frame);
  186. JS_DECLARE_NATIVE_FUNCTION(cancel_animation_frame);
  187. JS_DECLARE_NATIVE_FUNCTION(atob);
  188. JS_DECLARE_NATIVE_FUNCTION(btoa);
  189. JS_DECLARE_NATIVE_FUNCTION(focus);
  190. JS_DECLARE_NATIVE_FUNCTION(get_computed_style);
  191. JS_DECLARE_NATIVE_FUNCTION(match_media);
  192. JS_DECLARE_NATIVE_FUNCTION(get_selection);
  193. JS_DECLARE_NATIVE_FUNCTION(queue_microtask);
  194. JS_DECLARE_NATIVE_FUNCTION(request_idle_callback);
  195. JS_DECLARE_NATIVE_FUNCTION(cancel_idle_callback);
  196. JS_DECLARE_NATIVE_FUNCTION(crypto_getter);
  197. #define __ENUMERATE(attribute, event_name) \
  198. JS_DECLARE_NATIVE_FUNCTION(attribute##_getter); \
  199. JS_DECLARE_NATIVE_FUNCTION(attribute##_setter);
  200. ENUMERATE_GLOBAL_EVENT_HANDLERS(__ENUMERATE);
  201. ENUMERATE_WINDOW_EVENT_HANDLERS(__ENUMERATE);
  202. #undef __ENUMERATE
  203. HTML::Location* m_location { nullptr };
  204. // [[CrossOriginPropertyDescriptorMap]], https://html.spec.whatwg.org/multipage/browsers.html#crossoriginpropertydescriptormap
  205. CrossOriginPropertyDescriptorMap m_cross_origin_property_descriptor_map;
  206. };
  207. void run_animation_frame_callbacks(DOM::Document&, double now);
  208. }