Window.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright (c) 2020-2024, Andreas Kling <andreas@ladybird.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/RefPtr.h>
  10. #include <AK/TypeCasts.h>
  11. #include <LibJS/Heap/Heap.h>
  12. #include <LibURL/URL.h>
  13. #include <LibWeb/Bindings/Intrinsics.h>
  14. #include <LibWeb/Bindings/WindowGlobalMixin.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/Navigable.h>
  22. #include <LibWeb/HTML/Plugin.h>
  23. #include <LibWeb/HTML/Scripting/ImportMap.h>
  24. #include <LibWeb/HTML/ScrollOptions.h>
  25. #include <LibWeb/HTML/WindowEventHandlers.h>
  26. #include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
  27. #include <LibWeb/RequestIdleCallback/IdleRequest.h>
  28. #include <LibWeb/WebIDL/Types.h>
  29. namespace Web::HTML {
  30. class IdleCallback;
  31. // https://w3c.github.io/csswg-drafts/cssom-view/#dictdef-scrolltooptions
  32. struct ScrollToOptions : public ScrollOptions {
  33. Optional<double> left;
  34. Optional<double> top;
  35. };
  36. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#windowpostmessageoptions
  37. struct WindowPostMessageOptions : public StructuredSerializeOptions {
  38. String target_origin { "/"_string };
  39. };
  40. class Window final
  41. : public DOM::EventTarget
  42. , public GlobalEventHandlers
  43. , public WindowEventHandlers
  44. , public WindowOrWorkerGlobalScopeMixin
  45. , public Bindings::WindowGlobalMixin {
  46. WEB_PLATFORM_OBJECT(Window, DOM::EventTarget);
  47. JS_DECLARE_ALLOCATOR(Window);
  48. public:
  49. [[nodiscard]] static JS::NonnullGCPtr<Window> create(JS::Realm&);
  50. ~Window();
  51. using WindowOrWorkerGlobalScopeMixin::atob;
  52. using WindowOrWorkerGlobalScopeMixin::btoa;
  53. using WindowOrWorkerGlobalScopeMixin::clear_interval;
  54. using WindowOrWorkerGlobalScopeMixin::clear_timeout;
  55. using WindowOrWorkerGlobalScopeMixin::create_image_bitmap;
  56. using WindowOrWorkerGlobalScopeMixin::fetch;
  57. using WindowOrWorkerGlobalScopeMixin::queue_microtask;
  58. using WindowOrWorkerGlobalScopeMixin::report_error;
  59. using WindowOrWorkerGlobalScopeMixin::set_interval;
  60. using WindowOrWorkerGlobalScopeMixin::set_timeout;
  61. using WindowOrWorkerGlobalScopeMixin::structured_clone;
  62. // ^DOM::EventTarget
  63. virtual bool dispatch_event(DOM::Event&) override;
  64. // ^WindowOrWorkerGlobalScopeMixin
  65. virtual Bindings::PlatformObject& this_impl() override { return *this; }
  66. virtual Bindings::PlatformObject const& this_impl() const override { return *this; }
  67. // ^JS::Object
  68. virtual JS::ThrowCompletionOr<bool> internal_set_prototype_of(JS::Object* prototype) override;
  69. Page& page();
  70. Page const& page() const;
  71. // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
  72. DOM::Document const& associated_document() const { return *m_associated_document; }
  73. DOM::Document& associated_document() { return *m_associated_document; }
  74. void set_associated_document(DOM::Document&);
  75. // https://html.spec.whatwg.org/multipage/window-object.html#window-bc
  76. BrowsingContext const* browsing_context() const;
  77. BrowsingContext* browsing_context();
  78. JS::GCPtr<Navigable> navigable() const;
  79. ImportMap const& import_map() const { return m_import_map; }
  80. void set_import_map(ImportMap const& import_map) { m_import_map = import_map; }
  81. bool import_maps_allowed() const { return m_import_maps_allowed; }
  82. void set_import_maps_allowed(bool import_maps_allowed) { m_import_maps_allowed = import_maps_allowed; }
  83. WebIDL::ExceptionOr<JS::GCPtr<WindowProxy>> open_impl(StringView url, StringView target, StringView features);
  84. bool has_animation_frame_callbacks() const { return m_animation_frame_callback_driver.has_callbacks(); }
  85. DOM::Event* current_event() { return m_current_event.ptr(); }
  86. DOM::Event const* current_event() const { return m_current_event.ptr(); }
  87. void set_current_event(DOM::Event* event);
  88. Optional<CSS::MediaFeatureValue> query_media_feature(CSS::MediaFeatureID) const;
  89. void fire_a_page_transition_event(FlyString const& event_name, bool persisted);
  90. WebIDL::ExceptionOr<JS::NonnullGCPtr<Storage>> local_storage();
  91. WebIDL::ExceptionOr<JS::NonnullGCPtr<Storage>> session_storage();
  92. void start_an_idle_period();
  93. AnimationFrameCallbackDriver& animation_frame_callback_driver() { return m_animation_frame_callback_driver; }
  94. // https://html.spec.whatwg.org/multipage/interaction.html#sticky-activation
  95. bool has_sticky_activation() const;
  96. // https://html.spec.whatwg.org/multipage/interaction.html#transient-activation
  97. bool has_transient_activation() const;
  98. // https://html.spec.whatwg.org/multipage/interaction.html#history-action-activation
  99. bool has_history_action_activation() const;
  100. WebIDL::ExceptionOr<void> initialize_web_interfaces(Badge<WindowEnvironmentSettingsObject>);
  101. Vector<JS::NonnullGCPtr<Plugin>> pdf_viewer_plugin_objects();
  102. Vector<JS::NonnullGCPtr<MimeType>> pdf_viewer_mime_type_objects();
  103. CrossOriginPropertyDescriptorMap const& cross_origin_property_descriptor_map() const { return m_cross_origin_property_descriptor_map; }
  104. CrossOriginPropertyDescriptorMap& cross_origin_property_descriptor_map() { return m_cross_origin_property_descriptor_map; }
  105. JS::NonnullGCPtr<WebIDL::CallbackType> count_queuing_strategy_size_function();
  106. JS::NonnullGCPtr<WebIDL::CallbackType> byte_length_queuing_strategy_size_function();
  107. // JS API functions
  108. JS::NonnullGCPtr<WindowProxy> window() const;
  109. JS::NonnullGCPtr<WindowProxy> self() const;
  110. JS::NonnullGCPtr<DOM::Document const> document() const;
  111. String name() const;
  112. void set_name(String const&);
  113. String status() const;
  114. void close();
  115. bool closed() const;
  116. void set_status(String const&);
  117. [[nodiscard]] JS::NonnullGCPtr<Location> location();
  118. JS::NonnullGCPtr<History> history() const;
  119. JS::NonnullGCPtr<Navigation> navigation();
  120. void focus();
  121. void blur();
  122. JS::NonnullGCPtr<WindowProxy> frames() const;
  123. u32 length();
  124. JS::GCPtr<WindowProxy const> top() const;
  125. JS::GCPtr<WindowProxy const> opener() const;
  126. WebIDL::ExceptionOr<void> set_opener(JS::Value);
  127. JS::GCPtr<WindowProxy const> parent() const;
  128. JS::GCPtr<DOM::Element const> frame_element() const;
  129. WebIDL::ExceptionOr<JS::GCPtr<WindowProxy>> open(Optional<String> const& url, Optional<String> const& target, Optional<String> const& features);
  130. [[nodiscard]] JS::NonnullGCPtr<Navigator> navigator();
  131. [[nodiscard]] JS::NonnullGCPtr<CloseWatcherManager> close_watcher_manager();
  132. void alert(String const& message = {});
  133. bool confirm(Optional<String> const& message);
  134. Optional<String> prompt(Optional<String> const& message, Optional<String> const& default_);
  135. WebIDL::ExceptionOr<void> post_message(JS::Value message, String const&, Vector<JS::Handle<JS::Object>> const&);
  136. WebIDL::ExceptionOr<void> post_message(JS::Value message, WindowPostMessageOptions const&);
  137. Variant<JS::Handle<DOM::Event>, JS::Value> event() const;
  138. [[nodiscard]] JS::NonnullGCPtr<CSS::CSSStyleDeclaration> get_computed_style(DOM::Element&, Optional<String> const& pseudo_element) const;
  139. WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::MediaQueryList>> match_media(String const& query);
  140. [[nodiscard]] JS::NonnullGCPtr<CSS::Screen> screen();
  141. [[nodiscard]] JS::GCPtr<CSS::VisualViewport> visual_viewport();
  142. i32 inner_width() const;
  143. i32 inner_height() const;
  144. void move_to(long, long) const;
  145. void move_by(long, long) const;
  146. void resize_to(long, long) const;
  147. void resize_by(long, long) const;
  148. double scroll_x() const;
  149. double scroll_y() const;
  150. void scroll(ScrollToOptions const&);
  151. void scroll(double x, double y);
  152. void scroll_by(ScrollToOptions);
  153. void scroll_by(double x, double y);
  154. i32 screen_x() const;
  155. i32 screen_y() const;
  156. i32 outer_width() const;
  157. i32 outer_height() const;
  158. double device_pixel_ratio() const;
  159. WebIDL::UnsignedLong request_animation_frame(WebIDL::CallbackType&);
  160. void cancel_animation_frame(WebIDL::UnsignedLong handle);
  161. u32 request_idle_callback(WebIDL::CallbackType&, RequestIdleCallback::IdleRequestOptions const&);
  162. void cancel_idle_callback(u32 handle);
  163. JS::GCPtr<Selection::Selection> get_selection() const;
  164. void capture_events();
  165. void release_events();
  166. [[nodiscard]] JS::NonnullGCPtr<CustomElementRegistry> custom_elements();
  167. HighResolutionTime::DOMHighResTimeStamp last_activation_timestamp() const { return m_last_activation_timestamp; }
  168. void set_last_activation_timestamp(HighResolutionTime::DOMHighResTimeStamp timestamp) { m_last_activation_timestamp = timestamp; }
  169. void consume_user_activation();
  170. HighResolutionTime::DOMHighResTimeStamp last_history_action_activation_timestamp() const { return m_last_history_action_activation_timestamp; }
  171. void set_last_history_action_activation_timestamp(HighResolutionTime::DOMHighResTimeStamp timestamp) { m_last_history_action_activation_timestamp = timestamp; }
  172. void consume_history_action_user_activation();
  173. static void set_inspector_object_exposed(bool);
  174. static void set_internals_object_exposed(bool);
  175. [[nodiscard]] OrderedHashMap<FlyString, JS::NonnullGCPtr<Navigable>> document_tree_child_navigable_target_name_property_set();
  176. [[nodiscard]] Vector<FlyString> supported_property_names() const override;
  177. [[nodiscard]] JS::Value named_item_value(FlyString const&) const override;
  178. bool find(String const& string);
  179. private:
  180. explicit Window(JS::Realm&);
  181. virtual void visit_edges(Cell::Visitor&) override;
  182. virtual void finalize() override;
  183. // ^HTML::GlobalEventHandlers
  184. virtual JS::GCPtr<DOM::EventTarget> global_event_handlers_to_event_target(FlyString const&) override { return *this; }
  185. // ^HTML::WindowEventHandlers
  186. virtual JS::GCPtr<DOM::EventTarget> window_event_handlers_to_event_target() override { return *this; }
  187. void invoke_idle_callbacks();
  188. struct [[nodiscard]] NamedObjects {
  189. Vector<JS::NonnullGCPtr<Navigable>> navigables;
  190. Vector<JS::NonnullGCPtr<DOM::Element>> elements;
  191. };
  192. NamedObjects named_objects(StringView name);
  193. WebIDL::ExceptionOr<void> window_post_message_steps(JS::Value, WindowPostMessageOptions const&);
  194. // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
  195. JS::GCPtr<DOM::Document> m_associated_document;
  196. JS::GCPtr<DOM::Event> m_current_event;
  197. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-window-import-map
  198. ImportMap m_import_map;
  199. // https://html.spec.whatwg.org/multipage/webappapis.html#import-maps-allowed
  200. bool m_import_maps_allowed { true };
  201. JS::GCPtr<CSS::Screen> m_screen;
  202. JS::GCPtr<Navigator> m_navigator;
  203. JS::GCPtr<Location> m_location;
  204. JS::GCPtr<CloseWatcherManager> m_close_watcher_manager;
  205. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#window-navigation-api
  206. JS::GCPtr<Navigation> m_navigation;
  207. // https://html.spec.whatwg.org/multipage/custom-elements.html#custom-elements-api
  208. // Each Window object is associated with a unique instance of a CustomElementRegistry object, allocated when the Window object is created.
  209. JS::GCPtr<CustomElementRegistry> m_custom_element_registry;
  210. AnimationFrameCallbackDriver m_animation_frame_callback_driver;
  211. // https://w3c.github.io/requestidlecallback/#dfn-list-of-idle-request-callbacks
  212. Vector<NonnullRefPtr<IdleCallback>> m_idle_request_callbacks;
  213. // https://w3c.github.io/requestidlecallback/#dfn-list-of-runnable-idle-callbacks
  214. Vector<NonnullRefPtr<IdleCallback>> m_runnable_idle_callbacks;
  215. // https://w3c.github.io/requestidlecallback/#dfn-idle-callback-identifier
  216. u32 m_idle_callback_identifier = 0;
  217. // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-plugin-objects
  218. Vector<JS::NonnullGCPtr<Plugin>> m_pdf_viewer_plugin_objects;
  219. // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-mime-type-objects
  220. Vector<JS::NonnullGCPtr<MimeType>> m_pdf_viewer_mime_type_objects;
  221. // [[CrossOriginPropertyDescriptorMap]], https://html.spec.whatwg.org/multipage/browsers.html#crossoriginpropertydescriptormap
  222. CrossOriginPropertyDescriptorMap m_cross_origin_property_descriptor_map;
  223. // https://html.spec.whatwg.org/multipage/interaction.html#user-activation-data-model
  224. HighResolutionTime::DOMHighResTimeStamp m_last_activation_timestamp { AK::Infinity<double> };
  225. // https://html.spec.whatwg.org/multipage/interaction.html#last-history-action-activation-timestamp
  226. HighResolutionTime::DOMHighResTimeStamp m_last_history_action_activation_timestamp { AK::Infinity<double> };
  227. // https://streams.spec.whatwg.org/#count-queuing-strategy-size-function
  228. JS::GCPtr<WebIDL::CallbackType> m_count_queuing_strategy_size_function;
  229. // https://streams.spec.whatwg.org/#byte-length-queuing-strategy-size-function
  230. JS::GCPtr<WebIDL::CallbackType> m_byte_length_queuing_strategy_size_function;
  231. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-window-status
  232. // When the Window object is created, the attribute must be set to the empty string. It does not do anything else.
  233. String m_status;
  234. };
  235. void run_animation_frame_callbacks(DOM::Document&, double now);
  236. }