Window.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/Bindings/WindowGlobalMixin.h>
  16. #include <LibWeb/DOM/EventTarget.h>
  17. #include <LibWeb/Forward.h>
  18. #include <LibWeb/HTML/AnimationFrameCallbackDriver.h>
  19. #include <LibWeb/HTML/CrossOrigin/CrossOriginPropertyDescriptorMap.h>
  20. #include <LibWeb/HTML/GlobalEventHandlers.h>
  21. #include <LibWeb/HTML/MimeType.h>
  22. #include <LibWeb/HTML/Plugin.h>
  23. #include <LibWeb/HTML/Scripting/ImportMap.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://html.spec.whatwg.org/#timerhandler
  30. using TimerHandler = Variant<JS::Handle<WebIDL::CallbackType>, DeprecatedString>;
  31. // https://w3c.github.io/csswg-drafts/cssom-view/#dictdef-scrolloptions
  32. struct ScrollOptions {
  33. Bindings::ScrollBehavior behavior { Bindings::ScrollBehavior::Auto };
  34. };
  35. // https://w3c.github.io/csswg-drafts/cssom-view/#dictdef-scrolltooptions
  36. struct ScrollToOptions : public ScrollOptions {
  37. Optional<double> left;
  38. Optional<double> top;
  39. };
  40. class Window final
  41. : public DOM::EventTarget
  42. , public HTML::GlobalEventHandlers
  43. , public HTML::WindowEventHandlers
  44. , public WindowOrWorkerGlobalScopeMixin
  45. , public Bindings::WindowGlobalMixin {
  46. WEB_PLATFORM_OBJECT(Window, DOM::EventTarget);
  47. public:
  48. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Window>> create(JS::Realm&);
  49. ~Window();
  50. using WindowOrWorkerGlobalScopeMixin::atob;
  51. using WindowOrWorkerGlobalScopeMixin::btoa;
  52. using WindowOrWorkerGlobalScopeMixin::fetch;
  53. using WindowOrWorkerGlobalScopeMixin::queue_microtask;
  54. using WindowOrWorkerGlobalScopeMixin::structured_clone;
  55. // ^DOM::EventTarget
  56. virtual bool dispatch_event(DOM::Event&) override;
  57. // ^WindowOrWorkerGlobalScopeMixin
  58. virtual Bindings::PlatformObject& this_impl() override { return *this; }
  59. virtual Bindings::PlatformObject const& this_impl() const override { return *this; }
  60. // ^JS::Object
  61. virtual JS::ThrowCompletionOr<bool> internal_set_prototype_of(JS::Object* prototype) override;
  62. Page* page();
  63. Page const* page() const;
  64. // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
  65. DOM::Document const& associated_document() const { return *m_associated_document; }
  66. DOM::Document& associated_document() { return *m_associated_document; }
  67. void set_associated_document(DOM::Document&);
  68. // https://html.spec.whatwg.org/multipage/window-object.html#window-bc
  69. HTML::BrowsingContext const* browsing_context() const;
  70. HTML::BrowsingContext* browsing_context();
  71. size_t document_tree_child_browsing_context_count() const;
  72. ImportMap const& import_map() const { return m_import_map; }
  73. bool import_maps_allowed() const { return m_import_maps_allowed; }
  74. void set_import_maps_allowed(bool import_maps_allowed) { m_import_maps_allowed = import_maps_allowed; }
  75. WebIDL::ExceptionOr<JS::GCPtr<HTML::WindowProxy>> open_impl(StringView url, StringView target, StringView features);
  76. bool has_animation_frame_callbacks() const { return m_animation_frame_callback_driver.has_callbacks(); }
  77. i32 set_timeout_impl(TimerHandler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
  78. i32 set_interval_impl(TimerHandler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
  79. void clear_timeout_impl(i32);
  80. void clear_interval_impl(i32);
  81. void did_set_location_href(Badge<HTML::Location>, AK::URL const& new_href);
  82. void did_call_location_reload(Badge<HTML::Location>);
  83. void did_call_location_replace(Badge<HTML::Location>, DeprecatedString url);
  84. void deallocate_timer_id(Badge<Timer>, i32);
  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(DeprecatedFlyString const& event_name, bool persisted);
  90. JS::NonnullGCPtr<HTML::Storage> local_storage();
  91. JS::NonnullGCPtr<HTML::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#transient-activation
  95. bool has_transient_activation() const;
  96. WebIDL::ExceptionOr<void> initialize_web_interfaces(Badge<WindowEnvironmentSettingsObject>);
  97. Vector<JS::NonnullGCPtr<Plugin>> pdf_viewer_plugin_objects();
  98. Vector<JS::NonnullGCPtr<MimeType>> pdf_viewer_mime_type_objects();
  99. CrossOriginPropertyDescriptorMap const& cross_origin_property_descriptor_map() const { return m_cross_origin_property_descriptor_map; }
  100. CrossOriginPropertyDescriptorMap& cross_origin_property_descriptor_map() { return m_cross_origin_property_descriptor_map; }
  101. // JS API functions
  102. JS::NonnullGCPtr<WindowProxy> window() const;
  103. JS::NonnullGCPtr<WindowProxy> self() const;
  104. JS::NonnullGCPtr<DOM::Document const> document() const;
  105. String name() const;
  106. void set_name(String const&);
  107. WebIDL::ExceptionOr<JS::NonnullGCPtr<Location>> location();
  108. JS::NonnullGCPtr<History> history() const;
  109. void focus();
  110. JS::NonnullGCPtr<WindowProxy> frames() const;
  111. u32 length() const;
  112. JS::GCPtr<WindowProxy const> top() const;
  113. JS::GCPtr<WindowProxy const> parent() const;
  114. JS::GCPtr<DOM::Element const> frame_element() const;
  115. WebIDL::ExceptionOr<JS::GCPtr<HTML::WindowProxy>> open(Optional<String> const& url, Optional<String> const& target, Optional<String> const& features);
  116. WebIDL::ExceptionOr<JS::NonnullGCPtr<Navigator>> navigator();
  117. void alert(String const& message = {});
  118. bool confirm(Optional<String> const& message);
  119. Optional<String> prompt(Optional<String> const& message, Optional<String> const& default_);
  120. void post_message(JS::Value message, String const&);
  121. Variant<JS::Handle<DOM::Event>, JS::Value> event() const;
  122. WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::CSSStyleDeclaration>> get_computed_style(DOM::Element&, Optional<String> const& pseudo_element) const;
  123. WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::MediaQueryList>> match_media(String const& query);
  124. WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::Screen>> screen();
  125. i32 inner_width() const;
  126. i32 inner_height() const;
  127. double scroll_x() const;
  128. double scroll_y() const;
  129. void scroll(ScrollToOptions const&);
  130. void scroll(double x, double y);
  131. void scroll_by(ScrollToOptions);
  132. void scroll_by(double x, double y);
  133. i32 screen_x() const;
  134. i32 screen_y() const;
  135. i32 outer_width() const;
  136. i32 outer_height() const;
  137. double device_pixel_ratio() const;
  138. i32 request_animation_frame(WebIDL::CallbackType&);
  139. void cancel_animation_frame(i32 handle);
  140. u32 request_idle_callback(WebIDL::CallbackType&, RequestIdleCallback::IdleRequestOptions const&);
  141. void cancel_idle_callback(u32 handle);
  142. JS::GCPtr<Selection::Selection> get_selection() const;
  143. WebIDL::ExceptionOr<JS::NonnullGCPtr<HighResolutionTime::Performance>> performance();
  144. WebIDL::ExceptionOr<JS::NonnullGCPtr<Crypto::Crypto>> crypto();
  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(DeprecatedFlyString const&) override { return *this; }
  150. // ^HTML::WindowEventHandlers
  151. virtual DOM::EventTarget& window_event_handlers_to_event_target() override { return *this; }
  152. enum class Repeat {
  153. Yes,
  154. No,
  155. };
  156. i32 run_timer_initialization_steps(TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments, Repeat repeat, Optional<i32> previous_id = {});
  157. void invoke_idle_callbacks();
  158. // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
  159. JS::GCPtr<DOM::Document> m_associated_document;
  160. JS::GCPtr<DOM::Event> m_current_event;
  161. IDAllocator m_timer_id_allocator;
  162. HashMap<int, JS::NonnullGCPtr<Timer>> m_timers;
  163. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-window-import-map
  164. ImportMap m_import_map;
  165. // https://html.spec.whatwg.org/multipage/webappapis.html#import-maps-allowed
  166. bool m_import_maps_allowed { true };
  167. JS::GCPtr<HighResolutionTime::Performance> m_performance;
  168. JS::GCPtr<Crypto::Crypto> m_crypto;
  169. JS::GCPtr<CSS::Screen> m_screen;
  170. JS::GCPtr<HTML::Navigator> m_navigator;
  171. JS::GCPtr<HTML::Location> m_location;
  172. AnimationFrameCallbackDriver m_animation_frame_callback_driver;
  173. // https://w3c.github.io/requestidlecallback/#dfn-list-of-idle-request-callbacks
  174. Vector<NonnullRefPtr<IdleCallback>> m_idle_request_callbacks;
  175. // https://w3c.github.io/requestidlecallback/#dfn-list-of-runnable-idle-callbacks
  176. Vector<NonnullRefPtr<IdleCallback>> m_runnable_idle_callbacks;
  177. // https://w3c.github.io/requestidlecallback/#dfn-idle-callback-identifier
  178. u32 m_idle_callback_identifier = 0;
  179. // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-plugin-objects
  180. Vector<JS::NonnullGCPtr<Plugin>> m_pdf_viewer_plugin_objects;
  181. // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-mime-type-objects
  182. Vector<JS::NonnullGCPtr<MimeType>> m_pdf_viewer_mime_type_objects;
  183. // [[CrossOriginPropertyDescriptorMap]], https://html.spec.whatwg.org/multipage/browsers.html#crossoriginpropertydescriptormap
  184. CrossOriginPropertyDescriptorMap m_cross_origin_property_descriptor_map;
  185. JS_DECLARE_NATIVE_FUNCTION(location_setter);
  186. JS_DECLARE_NATIVE_FUNCTION(set_interval);
  187. JS_DECLARE_NATIVE_FUNCTION(set_timeout);
  188. JS_DECLARE_NATIVE_FUNCTION(clear_interval);
  189. JS_DECLARE_NATIVE_FUNCTION(clear_timeout);
  190. };
  191. void run_animation_frame_callbacks(DOM::Document&, double now);
  192. }