Window.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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/ScrollOptions.h>
  23. #include <LibWeb/HTML/StructuredSerializeOptions.h>
  24. #include <LibWeb/HTML/UniversalGlobalScope.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. // https://html.spec.whatwg.org/multipage/webappapis.html#specifier-resolution-record
  41. // A specifier resolution record is a struct. It has the following items:
  42. struct SpecifierResolution {
  43. // A serialized base URL
  44. // A string-or-null that represents the base URL of the specifier, when one exists.
  45. Optional<String> serialized_base_url;
  46. // A specifier
  47. // A string representing the specifier.
  48. String specifier;
  49. // A specifier as a URL
  50. // A URL-or-null that represents the URL in case of a URL-like module specifier.
  51. //
  52. // Spec-Note: Implementations can replace specifier as a URL with a boolean that indicates
  53. // that the specifier is either bare or URL-like that is special.
  54. bool specifier_is_null_or_url_like_that_is_special { false };
  55. };
  56. class Window final
  57. : public DOM::EventTarget
  58. , public GlobalEventHandlers
  59. , public WindowEventHandlers
  60. , public WindowOrWorkerGlobalScopeMixin
  61. , public UniversalGlobalScopeMixin
  62. , public Bindings::WindowGlobalMixin {
  63. WEB_PLATFORM_OBJECT(Window, DOM::EventTarget);
  64. GC_DECLARE_ALLOCATOR(Window);
  65. public:
  66. [[nodiscard]] static GC::Ref<Window> create(JS::Realm&);
  67. ~Window();
  68. using UniversalGlobalScopeMixin::atob;
  69. using UniversalGlobalScopeMixin::btoa;
  70. using UniversalGlobalScopeMixin::queue_microtask;
  71. using UniversalGlobalScopeMixin::structured_clone;
  72. using WindowOrWorkerGlobalScopeMixin::clear_interval;
  73. using WindowOrWorkerGlobalScopeMixin::clear_timeout;
  74. using WindowOrWorkerGlobalScopeMixin::create_image_bitmap;
  75. using WindowOrWorkerGlobalScopeMixin::fetch;
  76. using WindowOrWorkerGlobalScopeMixin::report_error;
  77. using WindowOrWorkerGlobalScopeMixin::set_interval;
  78. using WindowOrWorkerGlobalScopeMixin::set_timeout;
  79. // ^DOM::EventTarget
  80. virtual bool dispatch_event(DOM::Event&) override;
  81. // ^WindowOrWorkerGlobalScopeMixin
  82. virtual DOM::EventTarget& this_impl() override { return *this; }
  83. virtual DOM::EventTarget const& this_impl() const override { return *this; }
  84. // ^JS::Object
  85. virtual JS::ThrowCompletionOr<bool> internal_set_prototype_of(JS::Object* prototype) override;
  86. Page& page();
  87. Page const& page() const;
  88. // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
  89. DOM::Document const& associated_document() const { return *m_associated_document; }
  90. DOM::Document& associated_document() { return *m_associated_document; }
  91. void set_associated_document(DOM::Document&);
  92. // https://html.spec.whatwg.org/multipage/window-object.html#window-bc
  93. BrowsingContext const* browsing_context() const;
  94. BrowsingContext* browsing_context();
  95. GC::Ptr<Navigable> navigable() const;
  96. ImportMap& import_map() { return m_import_map; }
  97. ImportMap const& import_map() const { return m_import_map; }
  98. void set_import_map(ImportMap const& import_map) { m_import_map = import_map; }
  99. void append_resolved_module(SpecifierResolution resolution) { m_resolved_module_set.append(move(resolution)); }
  100. Vector<SpecifierResolution> const& resolved_module_set() const { return m_resolved_module_set; }
  101. WebIDL::ExceptionOr<GC::Ptr<WindowProxy>> window_open_steps(StringView url, StringView target, StringView features);
  102. struct OpenedWindow {
  103. GC::Ptr<Navigable> navigable;
  104. TokenizedFeature::NoOpener no_opener { TokenizedFeature::NoOpener::No };
  105. Navigable::WindowType window_type { Navigable::WindowType::ExistingOrNone };
  106. };
  107. WebIDL::ExceptionOr<OpenedWindow> window_open_steps_internal(StringView url, StringView target, StringView features);
  108. DOM::Event* current_event() { return m_current_event.ptr(); }
  109. DOM::Event const* current_event() const { return m_current_event.ptr(); }
  110. void set_current_event(DOM::Event* event);
  111. Optional<CSS::MediaFeatureValue> query_media_feature(CSS::MediaFeatureID) const;
  112. void fire_a_page_transition_event(FlyString const& event_name, bool persisted);
  113. WebIDL::ExceptionOr<GC::Ref<Storage>> local_storage();
  114. WebIDL::ExceptionOr<GC::Ref<Storage>> session_storage();
  115. void start_an_idle_period();
  116. // https://html.spec.whatwg.org/multipage/interaction.html#sticky-activation
  117. bool has_sticky_activation() const;
  118. // https://html.spec.whatwg.org/multipage/interaction.html#transient-activation
  119. bool has_transient_activation() const;
  120. // https://html.spec.whatwg.org/multipage/interaction.html#history-action-activation
  121. bool has_history_action_activation() const;
  122. WebIDL::ExceptionOr<void> initialize_web_interfaces(Badge<WindowEnvironmentSettingsObject>);
  123. Vector<GC::Ref<Plugin>> pdf_viewer_plugin_objects();
  124. Vector<GC::Ref<MimeType>> pdf_viewer_mime_type_objects();
  125. CrossOriginPropertyDescriptorMap const& cross_origin_property_descriptor_map() const { return m_cross_origin_property_descriptor_map; }
  126. CrossOriginPropertyDescriptorMap& cross_origin_property_descriptor_map() { return m_cross_origin_property_descriptor_map; }
  127. // JS API functions
  128. GC::Ref<WindowProxy> window() const;
  129. GC::Ref<WindowProxy> self() const;
  130. GC::Ref<DOM::Document const> document() const;
  131. String name() const;
  132. void set_name(String const&);
  133. String status() const;
  134. void close();
  135. bool closed() const;
  136. void set_status(String const&);
  137. [[nodiscard]] GC::Ref<Location> location();
  138. GC::Ref<History> history() const;
  139. GC::Ref<Navigation> navigation();
  140. void stop();
  141. void focus();
  142. void blur();
  143. GC::Ref<WindowProxy> frames() const;
  144. u32 length();
  145. GC::Ptr<WindowProxy const> top() const;
  146. GC::Ptr<WindowProxy const> opener() const;
  147. WebIDL::ExceptionOr<void> set_opener(JS::Value);
  148. GC::Ptr<WindowProxy const> parent() const;
  149. GC::Ptr<DOM::Element const> frame_element() const;
  150. WebIDL::ExceptionOr<GC::Ptr<WindowProxy>> open(Optional<String> const& url, Optional<String> const& target, Optional<String> const& features);
  151. [[nodiscard]] GC::Ref<Navigator> navigator();
  152. [[nodiscard]] GC::Ref<CloseWatcherManager> close_watcher_manager();
  153. void alert(String const& message = {});
  154. bool confirm(Optional<String> const& message);
  155. Optional<String> prompt(Optional<String> const& message, Optional<String> const& default_);
  156. WebIDL::ExceptionOr<void> post_message(JS::Value message, String const&, Vector<GC::Root<JS::Object>> const&);
  157. WebIDL::ExceptionOr<void> post_message(JS::Value message, WindowPostMessageOptions const&);
  158. Variant<GC::Root<DOM::Event>, JS::Value> event() const;
  159. [[nodiscard]] GC::Ref<CSS::CSSStyleDeclaration> get_computed_style(DOM::Element&, Optional<String> const& pseudo_element) const;
  160. WebIDL::ExceptionOr<GC::Ref<CSS::MediaQueryList>> match_media(String const& query);
  161. [[nodiscard]] GC::Ref<CSS::Screen> screen();
  162. [[nodiscard]] GC::Ptr<CSS::VisualViewport> visual_viewport();
  163. i32 inner_width() const;
  164. i32 inner_height() const;
  165. void move_to(long, long) const;
  166. void move_by(long, long) const;
  167. void resize_to(long, long) const;
  168. void resize_by(long, long) const;
  169. double scroll_x() const;
  170. double scroll_y() const;
  171. void scroll(ScrollToOptions const&);
  172. void scroll(double x, double y);
  173. void scroll_by(ScrollToOptions);
  174. void scroll_by(double x, double y);
  175. i32 screen_x() const;
  176. i32 screen_y() const;
  177. i32 outer_width() const;
  178. i32 outer_height() const;
  179. double device_pixel_ratio() const;
  180. AnimationFrameCallbackDriver& animation_frame_callback_driver();
  181. bool has_animation_frame_callbacks();
  182. WebIDL::UnsignedLong request_animation_frame(GC::Ref<WebIDL::CallbackType>);
  183. void cancel_animation_frame(WebIDL::UnsignedLong handle);
  184. u32 request_idle_callback(WebIDL::CallbackType&, RequestIdleCallback::IdleRequestOptions const&);
  185. void cancel_idle_callback(u32 handle);
  186. GC::Ptr<Selection::Selection> get_selection() const;
  187. void capture_events();
  188. void release_events();
  189. [[nodiscard]] GC::Ref<CustomElementRegistry> custom_elements();
  190. HighResolutionTime::DOMHighResTimeStamp last_activation_timestamp() const { return m_last_activation_timestamp; }
  191. void set_last_activation_timestamp(HighResolutionTime::DOMHighResTimeStamp timestamp) { m_last_activation_timestamp = timestamp; }
  192. void consume_user_activation();
  193. HighResolutionTime::DOMHighResTimeStamp last_history_action_activation_timestamp() const { return m_last_history_action_activation_timestamp; }
  194. void set_last_history_action_activation_timestamp(HighResolutionTime::DOMHighResTimeStamp timestamp) { m_last_history_action_activation_timestamp = timestamp; }
  195. void consume_history_action_user_activation();
  196. static void set_inspector_object_exposed(bool);
  197. static void set_internals_object_exposed(bool);
  198. [[nodiscard]] OrderedHashMap<FlyString, GC::Ref<Navigable>> document_tree_child_navigable_target_name_property_set();
  199. [[nodiscard]] Vector<FlyString> supported_property_names() const override;
  200. [[nodiscard]] JS::Value named_item_value(FlyString const&) const override;
  201. bool find(String const& string);
  202. private:
  203. explicit Window(JS::Realm&);
  204. virtual void visit_edges(Cell::Visitor&) override;
  205. virtual void finalize() override;
  206. // ^HTML::GlobalEventHandlers
  207. virtual GC::Ptr<DOM::EventTarget> global_event_handlers_to_event_target(FlyString const&) override { return *this; }
  208. // ^HTML::WindowEventHandlers
  209. virtual GC::Ptr<DOM::EventTarget> window_event_handlers_to_event_target() override { return *this; }
  210. void invoke_idle_callbacks();
  211. struct [[nodiscard]] NamedObjects {
  212. Vector<GC::Ref<Navigable>> navigables;
  213. Vector<GC::Ref<DOM::Element>> elements;
  214. };
  215. NamedObjects named_objects(StringView name);
  216. WebIDL::ExceptionOr<void> window_post_message_steps(JS::Value, WindowPostMessageOptions const&);
  217. // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
  218. GC::Ptr<DOM::Document> m_associated_document;
  219. GC::Ptr<DOM::Event> m_current_event;
  220. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-global-import-map
  221. // A global object has an import map, initially an empty import map.
  222. ImportMap m_import_map;
  223. // https://html.spec.whatwg.org/multipage/webappapis.html#resolved-module-set
  224. // A global object has a resolved module set, a set of specifier resolution records, initially empty.
  225. //
  226. // Spec-Note: The resolved module set ensures that module specifier resolution returns the same result when called
  227. // multiple times with the same (referrer, specifier) pair. It does that by ensuring that import map rules
  228. // that impact the specifier in its referrer's scope cannot be defined after its initial resolution. For
  229. // now, only Window global objects have their module set data structures modified from the initial empty one.
  230. Vector<SpecifierResolution> m_resolved_module_set;
  231. GC::Ptr<CSS::Screen> m_screen;
  232. GC::Ptr<Navigator> m_navigator;
  233. GC::Ptr<Location> m_location;
  234. GC::Ptr<CloseWatcherManager> m_close_watcher_manager;
  235. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#window-navigation-api
  236. GC::Ptr<Navigation> m_navigation;
  237. // https://html.spec.whatwg.org/multipage/custom-elements.html#custom-elements-api
  238. // Each Window object has an associated custom element registry (a CustomElementRegistry object).
  239. // It is set to a new CustomElementRegistry object when the Window object is created.
  240. GC::Ptr<CustomElementRegistry> m_custom_element_registry;
  241. GC::Ptr<AnimationFrameCallbackDriver> m_animation_frame_callback_driver;
  242. // https://w3c.github.io/requestidlecallback/#dfn-list-of-idle-request-callbacks
  243. Vector<NonnullRefPtr<IdleCallback>> m_idle_request_callbacks;
  244. // https://w3c.github.io/requestidlecallback/#dfn-list-of-runnable-idle-callbacks
  245. Vector<NonnullRefPtr<IdleCallback>> m_runnable_idle_callbacks;
  246. // https://w3c.github.io/requestidlecallback/#dfn-idle-callback-identifier
  247. u32 m_idle_callback_identifier = 0;
  248. // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-plugin-objects
  249. Vector<GC::Ref<Plugin>> m_pdf_viewer_plugin_objects;
  250. // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-mime-type-objects
  251. Vector<GC::Ref<MimeType>> m_pdf_viewer_mime_type_objects;
  252. // [[CrossOriginPropertyDescriptorMap]], https://html.spec.whatwg.org/multipage/browsers.html#crossoriginpropertydescriptormap
  253. CrossOriginPropertyDescriptorMap m_cross_origin_property_descriptor_map;
  254. // https://html.spec.whatwg.org/multipage/interaction.html#user-activation-data-model
  255. HighResolutionTime::DOMHighResTimeStamp m_last_activation_timestamp { AK::Infinity<double> };
  256. // https://html.spec.whatwg.org/multipage/interaction.html#last-history-action-activation-timestamp
  257. HighResolutionTime::DOMHighResTimeStamp m_last_history_action_activation_timestamp { AK::Infinity<double> };
  258. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-window-status
  259. // When the Window object is created, the attribute must be set to the empty string. It does not do anything else.
  260. String m_status;
  261. };
  262. void run_animation_frame_callbacks(DOM::Document&, double now);
  263. }