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