Window.h 10 KB

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