Window.h 13 KB

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