Window.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. 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. HTML::BrowsingContext const* browsing_context() const;
  68. HTML::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<HTML::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. i32 set_timeout_impl(TimerHandler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
  76. i32 set_interval_impl(TimerHandler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
  77. void clear_timeout_impl(i32);
  78. void clear_interval_impl(i32);
  79. void did_set_location_href(Badge<HTML::Location>, AK::URL const& new_href);
  80. void did_call_location_reload(Badge<HTML::Location>);
  81. void did_call_location_replace(Badge<HTML::Location>, DeprecatedString url);
  82. void deallocate_timer_id(Badge<Timer>, i32);
  83. DOM::Event* current_event() { return m_current_event.ptr(); }
  84. DOM::Event const* current_event() const { return m_current_event.ptr(); }
  85. void set_current_event(DOM::Event* event);
  86. Optional<CSS::MediaFeatureValue> query_media_feature(CSS::MediaFeatureID) const;
  87. void fire_a_page_transition_event(DeprecatedFlyString const& event_name, bool persisted);
  88. JS::NonnullGCPtr<HTML::Storage> local_storage();
  89. JS::NonnullGCPtr<HTML::Storage> session_storage();
  90. void start_an_idle_period();
  91. AnimationFrameCallbackDriver& animation_frame_callback_driver() { return m_animation_frame_callback_driver; }
  92. // https://html.spec.whatwg.org/multipage/interaction.html#transient-activation
  93. bool has_transient_activation() const;
  94. WebIDL::ExceptionOr<void> initialize_web_interfaces(Badge<WindowEnvironmentSettingsObject>);
  95. Vector<JS::NonnullGCPtr<Plugin>> pdf_viewer_plugin_objects();
  96. Vector<JS::NonnullGCPtr<MimeType>> pdf_viewer_mime_type_objects();
  97. // JS API functions
  98. JS::NonnullGCPtr<WindowProxy> window() const;
  99. JS::NonnullGCPtr<WindowProxy> self() const;
  100. JS::NonnullGCPtr<DOM::Document const> document() const;
  101. String name() const;
  102. void set_name(String const&);
  103. JS::NonnullGCPtr<Location> location() const;
  104. JS::NonnullGCPtr<History> history() const;
  105. void focus();
  106. JS::NonnullGCPtr<WindowProxy> frames() const;
  107. u32 length() const;
  108. JS::GCPtr<WindowProxy const> top() const;
  109. JS::GCPtr<WindowProxy const> parent() const;
  110. JS::GCPtr<DOM::Element const> frame_element() const;
  111. WebIDL::ExceptionOr<JS::GCPtr<HTML::WindowProxy>> open(Optional<String> const& url, Optional<String> const& target, Optional<String> const& features);
  112. JS::NonnullGCPtr<Navigator> navigator() const;
  113. void alert(String const& message = {});
  114. bool confirm(Optional<String> const& message);
  115. Optional<String> prompt(Optional<String> const& message, Optional<String> const& default_);
  116. void post_message(JS::Value message, String const&);
  117. Variant<JS::Handle<DOM::Event>, JS::Value> event() const;
  118. WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::CSSStyleDeclaration>> get_computed_style(DOM::Element&, Optional<String> const& pseudo_element) const;
  119. WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::MediaQueryList>> match_media(String const& query);
  120. WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::Screen>> screen();
  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. private:
  142. explicit Window(JS::Realm&);
  143. virtual void visit_edges(Cell::Visitor&) override;
  144. // ^HTML::GlobalEventHandlers
  145. virtual DOM::EventTarget& global_event_handlers_to_event_target(DeprecatedFlyString const&) override { return *this; }
  146. // ^HTML::WindowEventHandlers
  147. virtual DOM::EventTarget& window_event_handlers_to_event_target() override { return *this; }
  148. enum class Repeat {
  149. Yes,
  150. No,
  151. };
  152. i32 run_timer_initialization_steps(TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments, Repeat repeat, Optional<i32> previous_id = {});
  153. void invoke_idle_callbacks();
  154. // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
  155. JS::GCPtr<DOM::Document> m_associated_document;
  156. JS::GCPtr<DOM::Event> m_current_event;
  157. IDAllocator m_timer_id_allocator;
  158. HashMap<int, JS::NonnullGCPtr<Timer>> m_timers;
  159. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-window-import-map
  160. ImportMap m_import_map;
  161. // https://html.spec.whatwg.org/multipage/webappapis.html#import-maps-allowed
  162. bool m_import_maps_allowed { true };
  163. JS::GCPtr<HighResolutionTime::Performance> m_performance;
  164. JS::GCPtr<Crypto::Crypto> m_crypto;
  165. JS::GCPtr<CSS::Screen> m_screen;
  166. JS::GCPtr<HTML::Navigator> m_navigator;
  167. AnimationFrameCallbackDriver m_animation_frame_callback_driver;
  168. // https://w3c.github.io/requestidlecallback/#dfn-list-of-idle-request-callbacks
  169. Vector<NonnullRefPtr<IdleCallback>> m_idle_request_callbacks;
  170. // https://w3c.github.io/requestidlecallback/#dfn-list-of-runnable-idle-callbacks
  171. Vector<NonnullRefPtr<IdleCallback>> m_runnable_idle_callbacks;
  172. // https://w3c.github.io/requestidlecallback/#dfn-idle-callback-identifier
  173. u32 m_idle_callback_identifier = 0;
  174. // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-plugin-objects
  175. Vector<JS::NonnullGCPtr<Plugin>> m_pdf_viewer_plugin_objects;
  176. // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-mime-type-objects
  177. Vector<JS::NonnullGCPtr<MimeType>> m_pdf_viewer_mime_type_objects;
  178. public:
  179. virtual JS::ThrowCompletionOr<bool> internal_set_prototype_of(JS::Object* prototype) override;
  180. CrossOriginPropertyDescriptorMap const& cross_origin_property_descriptor_map() const { return m_cross_origin_property_descriptor_map; }
  181. CrossOriginPropertyDescriptorMap& cross_origin_property_descriptor_map() { return m_cross_origin_property_descriptor_map; }
  182. private:
  183. JS_DECLARE_NATIVE_FUNCTION(location_setter);
  184. JS_DECLARE_NATIVE_FUNCTION(set_interval);
  185. JS_DECLARE_NATIVE_FUNCTION(set_timeout);
  186. JS_DECLARE_NATIVE_FUNCTION(clear_interval);
  187. JS_DECLARE_NATIVE_FUNCTION(clear_timeout);
  188. HTML::Location* m_location { nullptr };
  189. // [[CrossOriginPropertyDescriptorMap]], https://html.spec.whatwg.org/multipage/browsers.html#crossoriginpropertydescriptormap
  190. CrossOriginPropertyDescriptorMap m_cross_origin_property_descriptor_map;
  191. };
  192. void run_animation_frame_callbacks(DOM::Document&, double now);
  193. }