Window.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/DisplayLink.h>
  7. #include <LibJS/Runtime/FunctionObject.h>
  8. #include <LibWeb/CSS/ResolvedCSSStyleDeclaration.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/DOM/Event.h>
  11. #include <LibWeb/DOM/EventDispatcher.h>
  12. #include <LibWeb/DOM/Timer.h>
  13. #include <LibWeb/DOM/Window.h>
  14. #include <LibWeb/HighResolutionTime/Performance.h>
  15. #include <LibWeb/Layout/InitialContainingBlock.h>
  16. #include <LibWeb/Page/BrowsingContext.h>
  17. #include <LibWeb/Page/Page.h>
  18. namespace Web::DOM {
  19. class RequestAnimationFrameCallback : public RefCounted<RequestAnimationFrameCallback> {
  20. public:
  21. explicit RequestAnimationFrameCallback(i32 id, Function<void(i32)> handler)
  22. : m_id(id)
  23. , m_handler(move(handler))
  24. {
  25. }
  26. ~RequestAnimationFrameCallback() { }
  27. i32 id() const { return m_id; }
  28. bool is_cancelled() const { return !m_handler; }
  29. void cancel() { m_handler = nullptr; }
  30. void invoke() { m_handler(m_id); }
  31. private:
  32. i32 m_id { 0 };
  33. Function<void(i32)> m_handler;
  34. };
  35. struct RequestAnimationFrameDriver {
  36. RequestAnimationFrameDriver()
  37. {
  38. m_timer = Core::Timer::create_single_shot(16, [this] {
  39. auto taken_callbacks = move(m_callbacks);
  40. for (auto& it : taken_callbacks) {
  41. if (!it.value->is_cancelled())
  42. it.value->invoke();
  43. }
  44. });
  45. }
  46. NonnullRefPtr<RequestAnimationFrameCallback> add(Function<void(i32)> handler)
  47. {
  48. auto id = m_id_allocator.allocate();
  49. auto callback = adopt_ref(*new RequestAnimationFrameCallback { id, move(handler) });
  50. m_callbacks.set(id, callback);
  51. if (!m_timer->is_active())
  52. m_timer->start();
  53. return callback;
  54. }
  55. bool remove(i32 id)
  56. {
  57. auto it = m_callbacks.find(id);
  58. if (it == m_callbacks.end())
  59. return false;
  60. m_callbacks.remove(it);
  61. m_id_allocator.deallocate(id);
  62. return true;
  63. }
  64. private:
  65. HashMap<i32, NonnullRefPtr<RequestAnimationFrameCallback>> m_callbacks;
  66. IDAllocator m_id_allocator;
  67. RefPtr<Core::Timer> m_timer;
  68. };
  69. static RequestAnimationFrameDriver& request_animation_frame_driver()
  70. {
  71. static RequestAnimationFrameDriver driver;
  72. return driver;
  73. }
  74. NonnullRefPtr<Window> Window::create_with_document(Document& document)
  75. {
  76. return adopt_ref(*new Window(document));
  77. }
  78. Window::Window(Document& document)
  79. : EventTarget(static_cast<Bindings::ScriptExecutionContext&>(document))
  80. , m_associated_document(document)
  81. , m_performance(make<HighResolutionTime::Performance>(*this))
  82. , m_screen(CSS::Screen::create(*this))
  83. {
  84. }
  85. Window::~Window()
  86. {
  87. }
  88. void Window::set_wrapper(Badge<Bindings::WindowObject>, Bindings::WindowObject& wrapper)
  89. {
  90. m_wrapper = wrapper.make_weak_ptr();
  91. }
  92. void Window::alert(String const& message)
  93. {
  94. if (auto* page = this->page())
  95. page->client().page_did_request_alert(message);
  96. }
  97. bool Window::confirm(String const& message)
  98. {
  99. if (auto* page = this->page())
  100. return page->client().page_did_request_confirm(message);
  101. return false;
  102. }
  103. String Window::prompt(String const& message, String const& default_)
  104. {
  105. if (auto* page = this->page())
  106. return page->client().page_did_request_prompt(message, default_);
  107. return {};
  108. }
  109. i32 Window::set_interval(JS::FunctionObject& callback, i32 interval)
  110. {
  111. auto timer = Timer::create_interval(*this, interval, callback);
  112. m_timers.set(timer->id(), timer);
  113. return timer->id();
  114. }
  115. i32 Window::set_timeout(JS::FunctionObject& callback, i32 interval)
  116. {
  117. auto timer = Timer::create_timeout(*this, interval, callback);
  118. m_timers.set(timer->id(), timer);
  119. return timer->id();
  120. }
  121. void Window::timer_did_fire(Badge<Timer>, Timer& timer)
  122. {
  123. // We should not be here if there's no JS wrapper for the Window object.
  124. VERIFY(wrapper());
  125. auto& vm = wrapper()->vm();
  126. // NOTE: This protector pointer keeps the timer alive until the end of this function no matter what.
  127. NonnullRefPtr protector(timer);
  128. if (timer.type() == Timer::Type::Timeout) {
  129. m_timers.remove(timer.id());
  130. }
  131. [[maybe_unused]] auto rc = vm.call(timer.callback(), wrapper());
  132. if (vm.exception())
  133. vm.clear_exception();
  134. }
  135. i32 Window::allocate_timer_id(Badge<Timer>)
  136. {
  137. return m_timer_id_allocator.allocate();
  138. }
  139. void Window::deallocate_timer_id(Badge<Timer>, i32 id)
  140. {
  141. m_timer_id_allocator.deallocate(id);
  142. }
  143. void Window::clear_timeout(i32 timer_id)
  144. {
  145. m_timers.remove(timer_id);
  146. }
  147. void Window::clear_interval(i32 timer_id)
  148. {
  149. m_timers.remove(timer_id);
  150. }
  151. i32 Window::request_animation_frame(JS::FunctionObject& js_callback)
  152. {
  153. auto callback = request_animation_frame_driver().add([this, handle = JS::make_handle(&js_callback)](i32 id) mutable {
  154. auto& function = *handle.cell();
  155. auto& vm = function.vm();
  156. (void)vm.call(function, JS::js_undefined(), JS::Value((double)Core::DateTime::now().timestamp()));
  157. if (vm.exception())
  158. vm.clear_exception();
  159. m_request_animation_frame_callbacks.remove(id);
  160. });
  161. m_request_animation_frame_callbacks.set(callback->id(), callback);
  162. return callback->id();
  163. }
  164. void Window::cancel_animation_frame(i32 id)
  165. {
  166. auto it = m_request_animation_frame_callbacks.find(id);
  167. if (it == m_request_animation_frame_callbacks.end())
  168. return;
  169. it->value->cancel();
  170. m_request_animation_frame_callbacks.remove(it);
  171. }
  172. void Window::did_set_location_href(Badge<Bindings::LocationObject>, AK::URL const& new_href)
  173. {
  174. auto* frame = associated_document().browsing_context();
  175. if (!frame)
  176. return;
  177. frame->loader().load(new_href, FrameLoader::Type::Navigation);
  178. }
  179. void Window::did_call_location_reload(Badge<Bindings::LocationObject>)
  180. {
  181. auto* frame = associated_document().browsing_context();
  182. if (!frame)
  183. return;
  184. frame->loader().load(associated_document().url(), FrameLoader::Type::Reload);
  185. }
  186. bool Window::dispatch_event(NonnullRefPtr<Event> event)
  187. {
  188. return EventDispatcher::dispatch(*this, event, true);
  189. }
  190. JS::Object* Window::create_wrapper(JS::GlobalObject& global_object)
  191. {
  192. return &global_object;
  193. }
  194. int Window::inner_width() const
  195. {
  196. if (!associated_document().layout_node())
  197. return 0;
  198. return associated_document().layout_node()->width();
  199. }
  200. int Window::inner_height() const
  201. {
  202. if (!associated_document().layout_node())
  203. return 0;
  204. return associated_document().layout_node()->height();
  205. }
  206. Page* Window::page()
  207. {
  208. return associated_document().page();
  209. }
  210. Page const* Window::page() const
  211. {
  212. return associated_document().page();
  213. }
  214. NonnullRefPtr<CSS::CSSStyleDeclaration> Window::get_computed_style(DOM::Element& element) const
  215. {
  216. return CSS::ResolvedCSSStyleDeclaration::create(element);
  217. }
  218. NonnullRefPtr<CSS::MediaQueryList> Window::match_media(String media)
  219. {
  220. return CSS::MediaQueryList::create(associated_document(), move(media));
  221. }
  222. // https://www.w3.org/TR/cssom-view/#dom-window-scrollx
  223. float Window::scroll_x() const
  224. {
  225. if (auto* page = this->page())
  226. return page->top_level_browsing_context().viewport_scroll_offset().x();
  227. return 0;
  228. }
  229. // https://www.w3.org/TR/cssom-view/#dom-window-scrolly
  230. float Window::scroll_y() const
  231. {
  232. if (auto* page = this->page())
  233. return page->top_level_browsing_context().viewport_scroll_offset().y();
  234. return 0;
  235. }
  236. }