Window.h 11 KB

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