Window.h 10 KB

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