Window.h 11 KB

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