Window.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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/HTML/EventLoop/EventLoop.h>
  15. #include <LibWeb/HTML/PageTransitionEvent.h>
  16. #include <LibWeb/HighResolutionTime/Performance.h>
  17. #include <LibWeb/Layout/InitialContainingBlock.h>
  18. #include <LibWeb/Page/BrowsingContext.h>
  19. #include <LibWeb/Page/Page.h>
  20. namespace Web::DOM {
  21. class RequestAnimationFrameCallback : public RefCounted<RequestAnimationFrameCallback> {
  22. public:
  23. explicit RequestAnimationFrameCallback(i32 id, Function<void(i32)> handler)
  24. : m_id(id)
  25. , m_handler(move(handler))
  26. {
  27. }
  28. ~RequestAnimationFrameCallback() { }
  29. i32 id() const { return m_id; }
  30. bool is_cancelled() const { return !m_handler; }
  31. void cancel() { m_handler = nullptr; }
  32. void invoke() { m_handler(m_id); }
  33. private:
  34. i32 m_id { 0 };
  35. Function<void(i32)> m_handler;
  36. };
  37. struct RequestAnimationFrameDriver {
  38. RequestAnimationFrameDriver()
  39. {
  40. m_timer = Core::Timer::create_single_shot(16, [this] {
  41. auto taken_callbacks = move(m_callbacks);
  42. for (auto& it : taken_callbacks) {
  43. if (!it.value->is_cancelled())
  44. it.value->invoke();
  45. }
  46. });
  47. }
  48. NonnullRefPtr<RequestAnimationFrameCallback> add(Function<void(i32)> handler)
  49. {
  50. auto id = m_id_allocator.allocate();
  51. auto callback = adopt_ref(*new RequestAnimationFrameCallback { id, move(handler) });
  52. m_callbacks.set(id, callback);
  53. if (!m_timer->is_active())
  54. m_timer->start();
  55. return callback;
  56. }
  57. bool remove(i32 id)
  58. {
  59. auto it = m_callbacks.find(id);
  60. if (it == m_callbacks.end())
  61. return false;
  62. m_callbacks.remove(it);
  63. m_id_allocator.deallocate(id);
  64. return true;
  65. }
  66. private:
  67. HashMap<i32, NonnullRefPtr<RequestAnimationFrameCallback>> m_callbacks;
  68. IDAllocator m_id_allocator;
  69. RefPtr<Core::Timer> m_timer;
  70. };
  71. static RequestAnimationFrameDriver& request_animation_frame_driver()
  72. {
  73. static RequestAnimationFrameDriver driver;
  74. return driver;
  75. }
  76. NonnullRefPtr<Window> Window::create_with_document(Document& document)
  77. {
  78. return adopt_ref(*new Window(document));
  79. }
  80. Window::Window(Document& document)
  81. : EventTarget(static_cast<Bindings::ScriptExecutionContext&>(document))
  82. , m_associated_document(document)
  83. , m_performance(make<HighResolutionTime::Performance>(*this))
  84. , m_screen(CSS::Screen::create(*this))
  85. {
  86. }
  87. Window::~Window()
  88. {
  89. }
  90. void Window::set_wrapper(Badge<Bindings::WindowObject>, Bindings::WindowObject& wrapper)
  91. {
  92. m_wrapper = wrapper.make_weak_ptr();
  93. }
  94. void Window::alert(String const& message)
  95. {
  96. if (auto* page = this->page())
  97. page->client().page_did_request_alert(message);
  98. }
  99. bool Window::confirm(String const& message)
  100. {
  101. if (auto* page = this->page())
  102. return page->client().page_did_request_confirm(message);
  103. return false;
  104. }
  105. String Window::prompt(String const& message, String const& default_)
  106. {
  107. if (auto* page = this->page())
  108. return page->client().page_did_request_prompt(message, default_);
  109. return {};
  110. }
  111. i32 Window::set_interval(JS::FunctionObject& callback, i32 interval)
  112. {
  113. auto timer = Timer::create_interval(*this, interval, callback);
  114. m_timers.set(timer->id(), timer);
  115. return timer->id();
  116. }
  117. i32 Window::set_timeout(JS::FunctionObject& callback, i32 interval)
  118. {
  119. auto timer = Timer::create_timeout(*this, interval, callback);
  120. m_timers.set(timer->id(), timer);
  121. return timer->id();
  122. }
  123. void Window::timer_did_fire(Badge<Timer>, Timer& timer)
  124. {
  125. // We should not be here if there's no JS wrapper for the Window object.
  126. VERIFY(wrapper());
  127. auto& vm = wrapper()->vm();
  128. // NOTE: This protector pointer keeps the timer alive until the end of this function no matter what.
  129. NonnullRefPtr protector(timer);
  130. if (timer.type() == Timer::Type::Timeout) {
  131. m_timers.remove(timer.id());
  132. }
  133. [[maybe_unused]] auto rc = vm.call(timer.callback(), wrapper());
  134. if (vm.exception())
  135. vm.clear_exception();
  136. }
  137. i32 Window::allocate_timer_id(Badge<Timer>)
  138. {
  139. return m_timer_id_allocator.allocate();
  140. }
  141. void Window::deallocate_timer_id(Badge<Timer>, i32 id)
  142. {
  143. m_timer_id_allocator.deallocate(id);
  144. }
  145. void Window::clear_timeout(i32 timer_id)
  146. {
  147. m_timers.remove(timer_id);
  148. }
  149. void Window::clear_interval(i32 timer_id)
  150. {
  151. m_timers.remove(timer_id);
  152. }
  153. i32 Window::request_animation_frame(JS::FunctionObject& js_callback)
  154. {
  155. auto callback = request_animation_frame_driver().add([this, handle = JS::make_handle(&js_callback)](i32 id) mutable {
  156. auto& function = *handle.cell();
  157. auto& vm = function.vm();
  158. (void)vm.call(function, JS::js_undefined(), JS::Value((double)Core::DateTime::now().timestamp()));
  159. if (vm.exception())
  160. vm.clear_exception();
  161. m_request_animation_frame_callbacks.remove(id);
  162. });
  163. m_request_animation_frame_callbacks.set(callback->id(), callback);
  164. return callback->id();
  165. }
  166. void Window::cancel_animation_frame(i32 id)
  167. {
  168. auto it = m_request_animation_frame_callbacks.find(id);
  169. if (it == m_request_animation_frame_callbacks.end())
  170. return;
  171. it->value->cancel();
  172. m_request_animation_frame_callbacks.remove(it);
  173. }
  174. void Window::did_set_location_href(Badge<Bindings::LocationObject>, AK::URL const& new_href)
  175. {
  176. auto* frame = associated_document().browsing_context();
  177. if (!frame)
  178. return;
  179. frame->loader().load(new_href, FrameLoader::Type::Navigation);
  180. }
  181. void Window::did_call_location_reload(Badge<Bindings::LocationObject>)
  182. {
  183. auto* frame = associated_document().browsing_context();
  184. if (!frame)
  185. return;
  186. frame->loader().load(associated_document().url(), FrameLoader::Type::Reload);
  187. }
  188. bool Window::dispatch_event(NonnullRefPtr<Event> event)
  189. {
  190. return EventDispatcher::dispatch(*this, event, true);
  191. }
  192. JS::Object* Window::create_wrapper(JS::GlobalObject& global_object)
  193. {
  194. return &global_object;
  195. }
  196. int Window::inner_width() const
  197. {
  198. if (!associated_document().layout_node())
  199. return 0;
  200. return associated_document().layout_node()->width();
  201. }
  202. int Window::inner_height() const
  203. {
  204. if (!associated_document().layout_node())
  205. return 0;
  206. return associated_document().layout_node()->height();
  207. }
  208. Page* Window::page()
  209. {
  210. return associated_document().page();
  211. }
  212. Page const* Window::page() const
  213. {
  214. return associated_document().page();
  215. }
  216. NonnullRefPtr<CSS::CSSStyleDeclaration> Window::get_computed_style(DOM::Element& element) const
  217. {
  218. return CSS::ResolvedCSSStyleDeclaration::create(element);
  219. }
  220. NonnullRefPtr<CSS::MediaQueryList> Window::match_media(String media)
  221. {
  222. return CSS::MediaQueryList::create(associated_document(), move(media));
  223. }
  224. // https://www.w3.org/TR/cssom-view/#dom-window-scrollx
  225. float Window::scroll_x() const
  226. {
  227. if (auto* page = this->page())
  228. return page->top_level_browsing_context().viewport_scroll_offset().x();
  229. return 0;
  230. }
  231. // https://www.w3.org/TR/cssom-view/#dom-window-scrolly
  232. float Window::scroll_y() const
  233. {
  234. if (auto* page = this->page())
  235. return page->top_level_browsing_context().viewport_scroll_offset().y();
  236. return 0;
  237. }
  238. // https://html.spec.whatwg.org/#fire-a-page-transition-event
  239. void Window::fire_a_page_transition_event(FlyString event_name, bool persisted)
  240. {
  241. // To fire a page transition event named eventName at a Window window with a boolean persisted,
  242. // fire an event named eventName at window, using PageTransitionEvent,
  243. // with the persisted attribute initialized to persisted,
  244. auto event = HTML::PageTransitionEvent::create(move(event_name), persisted);
  245. // ...the cancelable attribute intialized to true,
  246. event->set_cancelable(true);
  247. // the bubbles attribute initialized to true,
  248. event->set_bubbles(true);
  249. // and legacy target override flag set.
  250. dispatch_event(move(event));
  251. }
  252. // https://html.spec.whatwg.org/#dom-queuemicrotask
  253. void Window::queue_microtask(JS::FunctionObject& callback)
  254. {
  255. // The queueMicrotask(callback) method must queue a microtask to invoke callback,
  256. HTML::queue_a_microtask(associated_document(), [&callback, handle = JS::make_handle(&callback)]() {
  257. auto& vm = callback.vm();
  258. [[maybe_unused]] auto rc = vm.call(callback, JS::js_null());
  259. // FIXME: ...and if callback throws an exception, report the exception.
  260. if (vm.exception())
  261. vm.clear_exception();
  262. });
  263. }
  264. float Window::device_pixel_ratio() const
  265. {
  266. // FIXME: Return 2.0f if we're in HiDPI mode!
  267. return 1.0f;
  268. }
  269. // https://drafts.csswg.org/cssom-view/#dom-window-screenx
  270. int Window::screen_x() const
  271. {
  272. // The screenX and screenLeft attributes must return the x-coordinate, relative to the origin of the Web-exposed screen area,
  273. // of the left of the client window as number of CSS pixels, or zero if there is no such thing.
  274. return 0;
  275. }
  276. // https://drafts.csswg.org/cssom-view/#dom-window-screeny
  277. int Window::screen_y() const
  278. {
  279. // The screenY and screenTop attributes must return the y-coordinate, relative to the origin of the screen of the Web-exposed screen area,
  280. // of the top of the client window as number of CSS pixels, or zero if there is no such thing.
  281. return 0;
  282. }
  283. }