Window.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (c) 2020-2023, 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/RefPtr.h>
  10. #include <AK/TypeCasts.h>
  11. #include <AK/URL.h>
  12. #include <LibJS/Heap/Heap.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/Plugin.h>
  22. #include <LibWeb/HTML/Scripting/ImportMap.h>
  23. #include <LibWeb/HTML/ScrollOptions.h>
  24. #include <LibWeb/HTML/WindowEventHandlers.h>
  25. #include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
  26. #include <LibWeb/RequestIdleCallback/IdleRequest.h>
  27. namespace Web::HTML {
  28. class IdleCallback;
  29. // https://w3c.github.io/csswg-drafts/cssom-view/#dictdef-scrolltooptions
  30. struct ScrollToOptions : public ScrollOptions {
  31. Optional<double> left;
  32. Optional<double> top;
  33. };
  34. class Window final
  35. : public DOM::EventTarget
  36. , public GlobalEventHandlers
  37. , public WindowEventHandlers
  38. , public WindowOrWorkerGlobalScopeMixin
  39. , public Bindings::WindowGlobalMixin {
  40. WEB_PLATFORM_OBJECT(Window, DOM::EventTarget);
  41. public:
  42. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Window>> create(JS::Realm&);
  43. ~Window();
  44. using WindowOrWorkerGlobalScopeMixin::atob;
  45. using WindowOrWorkerGlobalScopeMixin::btoa;
  46. using WindowOrWorkerGlobalScopeMixin::clear_interval;
  47. using WindowOrWorkerGlobalScopeMixin::clear_timeout;
  48. using WindowOrWorkerGlobalScopeMixin::fetch;
  49. using WindowOrWorkerGlobalScopeMixin::queue_microtask;
  50. using WindowOrWorkerGlobalScopeMixin::set_interval;
  51. using WindowOrWorkerGlobalScopeMixin::set_timeout;
  52. using WindowOrWorkerGlobalScopeMixin::structured_clone;
  53. // ^DOM::EventTarget
  54. virtual bool dispatch_event(DOM::Event&) override;
  55. // ^WindowOrWorkerGlobalScopeMixin
  56. virtual Bindings::PlatformObject& this_impl() override { return *this; }
  57. virtual Bindings::PlatformObject const& this_impl() const override { return *this; }
  58. // ^JS::Object
  59. virtual JS::ThrowCompletionOr<bool> internal_set_prototype_of(JS::Object* prototype) override;
  60. Page* page();
  61. Page const* page() const;
  62. // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
  63. DOM::Document const& associated_document() const { return *m_associated_document; }
  64. DOM::Document& associated_document() { return *m_associated_document; }
  65. void set_associated_document(DOM::Document&);
  66. // https://html.spec.whatwg.org/multipage/window-object.html#window-bc
  67. BrowsingContext const* browsing_context() const;
  68. BrowsingContext* browsing_context();
  69. size_t document_tree_child_browsing_context_count() const;
  70. ImportMap const& import_map() const { return m_import_map; }
  71. bool import_maps_allowed() const { return m_import_maps_allowed; }
  72. void set_import_maps_allowed(bool import_maps_allowed) { m_import_maps_allowed = import_maps_allowed; }
  73. WebIDL::ExceptionOr<JS::GCPtr<WindowProxy>> open_impl(StringView url, StringView target, StringView features);
  74. bool has_animation_frame_callbacks() const { return m_animation_frame_callback_driver.has_callbacks(); }
  75. void did_set_location_href(Badge<Location>, AK::URL const& new_href);
  76. void did_call_location_reload(Badge<Location>);
  77. void did_call_location_replace(Badge<Location>, DeprecatedString url);
  78. DOM::Event* current_event() { return m_current_event.ptr(); }
  79. DOM::Event const* current_event() const { return m_current_event.ptr(); }
  80. void set_current_event(DOM::Event* event);
  81. Optional<CSS::MediaFeatureValue> query_media_feature(CSS::MediaFeatureID) const;
  82. void fire_a_page_transition_event(FlyString const& event_name, bool persisted);
  83. WebIDL::ExceptionOr<JS::NonnullGCPtr<Storage>> local_storage();
  84. WebIDL::ExceptionOr<JS::NonnullGCPtr<Storage>> session_storage();
  85. void start_an_idle_period();
  86. AnimationFrameCallbackDriver& animation_frame_callback_driver() { return m_animation_frame_callback_driver; }
  87. // https://html.spec.whatwg.org/multipage/interaction.html#transient-activation
  88. bool has_transient_activation() const;
  89. WebIDL::ExceptionOr<void> initialize_web_interfaces(Badge<WindowEnvironmentSettingsObject>);
  90. Vector<JS::NonnullGCPtr<Plugin>> pdf_viewer_plugin_objects();
  91. Vector<JS::NonnullGCPtr<MimeType>> pdf_viewer_mime_type_objects();
  92. CrossOriginPropertyDescriptorMap const& cross_origin_property_descriptor_map() const { return m_cross_origin_property_descriptor_map; }
  93. CrossOriginPropertyDescriptorMap& cross_origin_property_descriptor_map() { return m_cross_origin_property_descriptor_map; }
  94. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::CallbackType>> count_queuing_strategy_size_function();
  95. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::CallbackType>> byte_length_queuing_strategy_size_function();
  96. // JS API functions
  97. JS::NonnullGCPtr<WindowProxy> window() const;
  98. JS::NonnullGCPtr<WindowProxy> self() const;
  99. JS::NonnullGCPtr<DOM::Document const> document() const;
  100. String name() const;
  101. void set_name(String const&);
  102. WebIDL::ExceptionOr<JS::NonnullGCPtr<Location>> location();
  103. JS::NonnullGCPtr<History> history() const;
  104. void focus();
  105. JS::NonnullGCPtr<WindowProxy> frames() const;
  106. u32 length() const;
  107. JS::GCPtr<WindowProxy const> top() const;
  108. JS::GCPtr<WindowProxy const> parent() const;
  109. JS::GCPtr<DOM::Element const> frame_element() const;
  110. WebIDL::ExceptionOr<JS::GCPtr<WindowProxy>> open(Optional<String> const& url, Optional<String> const& target, Optional<String> const& features);
  111. WebIDL::ExceptionOr<JS::NonnullGCPtr<Navigator>> navigator();
  112. void alert(String const& message = {});
  113. bool confirm(Optional<String> const& message);
  114. Optional<String> prompt(Optional<String> const& message, Optional<String> const& default_);
  115. void post_message(JS::Value message, String const&);
  116. Variant<JS::Handle<DOM::Event>, JS::Value> event() const;
  117. WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::CSSStyleDeclaration>> get_computed_style(DOM::Element&, Optional<String> const& pseudo_element) const;
  118. WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::MediaQueryList>> match_media(String const& query);
  119. WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::Screen>> screen();
  120. WebIDL::ExceptionOr<JS::GCPtr<CSS::VisualViewport>> visual_viewport();
  121. i32 inner_width() const;
  122. i32 inner_height() const;
  123. double scroll_x() const;
  124. double scroll_y() const;
  125. void scroll(ScrollToOptions const&);
  126. void scroll(double x, double y);
  127. void scroll_by(ScrollToOptions);
  128. void scroll_by(double x, double y);
  129. i32 screen_x() const;
  130. i32 screen_y() const;
  131. i32 outer_width() const;
  132. i32 outer_height() const;
  133. double device_pixel_ratio() const;
  134. i32 request_animation_frame(WebIDL::CallbackType&);
  135. void cancel_animation_frame(i32 handle);
  136. u32 request_idle_callback(WebIDL::CallbackType&, RequestIdleCallback::IdleRequestOptions const&);
  137. void cancel_idle_callback(u32 handle);
  138. JS::GCPtr<Selection::Selection> get_selection() const;
  139. WebIDL::ExceptionOr<JS::NonnullGCPtr<HighResolutionTime::Performance>> performance();
  140. WebIDL::ExceptionOr<JS::NonnullGCPtr<Crypto::Crypto>> crypto();
  141. WebIDL::ExceptionOr<JS::NonnullGCPtr<CustomElementRegistry>> custom_elements();
  142. HighResolutionTime::DOMHighResTimeStamp get_last_activation_timestamp() const { return m_last_activation_timestamp; }
  143. void set_last_activation_timestamp(HighResolutionTime::DOMHighResTimeStamp timestamp) { m_last_activation_timestamp = timestamp; }
  144. static void set_internals_object_exposed(bool);
  145. private:
  146. explicit Window(JS::Realm&);
  147. virtual void visit_edges(Cell::Visitor&) override;
  148. // ^HTML::GlobalEventHandlers
  149. virtual DOM::EventTarget& global_event_handlers_to_event_target(FlyString const&) override { return *this; }
  150. // ^HTML::WindowEventHandlers
  151. virtual DOM::EventTarget& window_event_handlers_to_event_target() override { return *this; }
  152. void invoke_idle_callbacks();
  153. // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
  154. JS::GCPtr<DOM::Document> m_associated_document;
  155. JS::GCPtr<DOM::Event> m_current_event;
  156. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-window-import-map
  157. ImportMap m_import_map;
  158. // https://html.spec.whatwg.org/multipage/webappapis.html#import-maps-allowed
  159. bool m_import_maps_allowed { true };
  160. JS::GCPtr<HighResolutionTime::Performance> m_performance;
  161. JS::GCPtr<Crypto::Crypto> m_crypto;
  162. JS::GCPtr<CSS::Screen> m_screen;
  163. JS::GCPtr<Navigator> m_navigator;
  164. JS::GCPtr<Location> m_location;
  165. // https://html.spec.whatwg.org/multipage/custom-elements.html#custom-elements-api
  166. // Each Window object is associated with a unique instance of a CustomElementRegistry object, allocated when the Window object is created.
  167. JS::GCPtr<CustomElementRegistry> m_custom_element_registry;
  168. AnimationFrameCallbackDriver m_animation_frame_callback_driver;
  169. // https://w3c.github.io/requestidlecallback/#dfn-list-of-idle-request-callbacks
  170. Vector<NonnullRefPtr<IdleCallback>> m_idle_request_callbacks;
  171. // https://w3c.github.io/requestidlecallback/#dfn-list-of-runnable-idle-callbacks
  172. Vector<NonnullRefPtr<IdleCallback>> m_runnable_idle_callbacks;
  173. // https://w3c.github.io/requestidlecallback/#dfn-idle-callback-identifier
  174. u32 m_idle_callback_identifier = 0;
  175. // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-plugin-objects
  176. Vector<JS::NonnullGCPtr<Plugin>> m_pdf_viewer_plugin_objects;
  177. // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-mime-type-objects
  178. Vector<JS::NonnullGCPtr<MimeType>> m_pdf_viewer_mime_type_objects;
  179. // [[CrossOriginPropertyDescriptorMap]], https://html.spec.whatwg.org/multipage/browsers.html#crossoriginpropertydescriptormap
  180. CrossOriginPropertyDescriptorMap m_cross_origin_property_descriptor_map;
  181. // https://html.spec.whatwg.org/multipage/interaction.html#user-activation-data-model
  182. HighResolutionTime::DOMHighResTimeStamp m_last_activation_timestamp { NumericLimits<double>::max() };
  183. // https://streams.spec.whatwg.org/#count-queuing-strategy-size-function
  184. JS::GCPtr<WebIDL::CallbackType> m_count_queuing_strategy_size_function;
  185. // https://streams.spec.whatwg.org/#byte-length-queuing-strategy-size-function
  186. JS::GCPtr<WebIDL::CallbackType> m_byte_length_queuing_strategy_size_function;
  187. };
  188. void run_animation_frame_callbacks(DOM::Document&, double now);
  189. }