Window.h 11 KB

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