Window.cpp 9.5 KB

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