Window.h 11 KB

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