Window.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/ComputedCSSStyleDeclaration.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. NonnullRefPtr<Window> Window::create_with_document(Document& document)
  20. {
  21. return adopt_ref(*new Window(document));
  22. }
  23. Window::Window(Document& document)
  24. : EventTarget(static_cast<Bindings::ScriptExecutionContext&>(document))
  25. , m_associated_document(document)
  26. , m_performance(make<HighResolutionTime::Performance>(*this))
  27. , m_screen(CSS::Screen::create(*this))
  28. {
  29. }
  30. Window::~Window()
  31. {
  32. }
  33. void Window::set_wrapper(Badge<Bindings::WindowObject>, Bindings::WindowObject& wrapper)
  34. {
  35. m_wrapper = wrapper.make_weak_ptr();
  36. }
  37. void Window::alert(String const& message)
  38. {
  39. if (auto* page = this->page())
  40. page->client().page_did_request_alert(message);
  41. }
  42. bool Window::confirm(String const& message)
  43. {
  44. if (auto* page = this->page())
  45. return page->client().page_did_request_confirm(message);
  46. return false;
  47. }
  48. String Window::prompt(String const& message, String const& default_)
  49. {
  50. if (auto* page = this->page())
  51. return page->client().page_did_request_prompt(message, default_);
  52. return {};
  53. }
  54. i32 Window::set_interval(JS::FunctionObject& callback, i32 interval)
  55. {
  56. auto timer = Timer::create_interval(*this, interval, callback);
  57. m_timers.set(timer->id(), timer);
  58. return timer->id();
  59. }
  60. i32 Window::set_timeout(JS::FunctionObject& callback, i32 interval)
  61. {
  62. auto timer = Timer::create_timeout(*this, interval, callback);
  63. m_timers.set(timer->id(), timer);
  64. return timer->id();
  65. }
  66. void Window::timer_did_fire(Badge<Timer>, Timer& timer)
  67. {
  68. // We should not be here if there's no JS wrapper for the Window object.
  69. VERIFY(wrapper());
  70. auto& vm = wrapper()->vm();
  71. // NOTE: This protector pointer keeps the timer alive until the end of this function no matter what.
  72. NonnullRefPtr protector(timer);
  73. if (timer.type() == Timer::Type::Timeout) {
  74. m_timers.remove(timer.id());
  75. }
  76. [[maybe_unused]] auto rc = vm.call(timer.callback(), wrapper());
  77. if (vm.exception())
  78. vm.clear_exception();
  79. }
  80. i32 Window::allocate_timer_id(Badge<Timer>)
  81. {
  82. return m_timer_id_allocator.allocate();
  83. }
  84. void Window::deallocate_timer_id(Badge<Timer>, i32 id)
  85. {
  86. m_timer_id_allocator.deallocate(id);
  87. }
  88. void Window::clear_timeout(i32 timer_id)
  89. {
  90. m_timers.remove(timer_id);
  91. }
  92. void Window::clear_interval(i32 timer_id)
  93. {
  94. m_timers.remove(timer_id);
  95. }
  96. i32 Window::request_animation_frame(JS::FunctionObject& callback)
  97. {
  98. // FIXME: This is extremely fake!
  99. static double fake_timestamp = 0;
  100. i32 link_id = GUI::DisplayLink::register_callback([handle = make_handle(&callback)](i32 link_id) {
  101. auto& function = const_cast<JS::FunctionObject&>(static_cast<JS::FunctionObject const&>(*handle.cell()));
  102. auto& vm = function.vm();
  103. fake_timestamp += 10;
  104. [[maybe_unused]] auto rc = vm.call(function, JS::js_undefined(), JS::Value(fake_timestamp));
  105. if (vm.exception())
  106. vm.clear_exception();
  107. GUI::DisplayLink::unregister_callback(link_id);
  108. });
  109. // FIXME: Don't hand out raw DisplayLink ID's to JavaScript!
  110. return link_id;
  111. }
  112. void Window::cancel_animation_frame(i32 id)
  113. {
  114. // FIXME: We should not be passing untrusted numbers to DisplayLink::unregister_callback()!
  115. GUI::DisplayLink::unregister_callback(id);
  116. }
  117. void Window::did_set_location_href(Badge<Bindings::LocationObject>, URL const& new_href)
  118. {
  119. auto* frame = associated_document().browsing_context();
  120. if (!frame)
  121. return;
  122. frame->loader().load(new_href, FrameLoader::Type::Navigation);
  123. }
  124. void Window::did_call_location_reload(Badge<Bindings::LocationObject>)
  125. {
  126. auto* frame = associated_document().browsing_context();
  127. if (!frame)
  128. return;
  129. frame->loader().load(associated_document().url(), FrameLoader::Type::Reload);
  130. }
  131. bool Window::dispatch_event(NonnullRefPtr<Event> event)
  132. {
  133. return EventDispatcher::dispatch(*this, event, true);
  134. }
  135. JS::Object* Window::create_wrapper(JS::GlobalObject& global_object)
  136. {
  137. return &global_object;
  138. }
  139. int Window::inner_width() const
  140. {
  141. if (!associated_document().layout_node())
  142. return 0;
  143. return associated_document().layout_node()->width();
  144. }
  145. int Window::inner_height() const
  146. {
  147. if (!associated_document().layout_node())
  148. return 0;
  149. return associated_document().layout_node()->height();
  150. }
  151. Page* Window::page()
  152. {
  153. return associated_document().page();
  154. }
  155. Page const* Window::page() const
  156. {
  157. return associated_document().page();
  158. }
  159. NonnullRefPtr<CSS::CSSStyleDeclaration> Window::get_computed_style(DOM::Element& element) const
  160. {
  161. return CSS::ComputedCSSStyleDeclaration::create(element);
  162. }
  163. NonnullRefPtr<CSS::MediaQueryList> Window::match_media(String media)
  164. {
  165. return CSS::MediaQueryList::create(associated_document(), move(media));
  166. }
  167. }