Window.cpp 9.8 KB

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