Window.h 10 KB

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