Window.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/DisplayLink.h>
  27. #include <LibJS/Runtime/Function.h>
  28. #include <LibWeb/DOM/Document.h>
  29. #include <LibWeb/DOM/Event.h>
  30. #include <LibWeb/DOM/EventDispatcher.h>
  31. #include <LibWeb/DOM/Timer.h>
  32. #include <LibWeb/DOM/Window.h>
  33. #include <LibWeb/HighResolutionTime/Performance.h>
  34. #include <LibWeb/InProcessWebView.h>
  35. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  36. #include <LibWeb/Page/Frame.h>
  37. namespace Web::DOM {
  38. NonnullRefPtr<Window> Window::create_with_document(Document& document)
  39. {
  40. return adopt(*new Window(document));
  41. }
  42. Window::Window(Document& document)
  43. : EventTarget(static_cast<Bindings::ScriptExecutionContext&>(document))
  44. , m_document(document)
  45. , m_performance(make<HighResolutionTime::Performance>(*this))
  46. {
  47. }
  48. Window::~Window()
  49. {
  50. }
  51. void Window::set_wrapper(Badge<Bindings::WindowObject>, Bindings::WindowObject& wrapper)
  52. {
  53. m_wrapper = wrapper.make_weak_ptr();
  54. }
  55. void Window::alert(const String& message)
  56. {
  57. if (auto* page = m_document.page())
  58. page->client().page_did_request_alert(message);
  59. }
  60. bool Window::confirm(const String& message)
  61. {
  62. if (auto* page = m_document.page())
  63. return page->client().page_did_request_confirm(message);
  64. return false;
  65. }
  66. String Window::prompt(const String& message, const String& default_)
  67. {
  68. if (auto* page = m_document.page())
  69. return page->client().page_did_request_prompt(message, default_);
  70. return {};
  71. }
  72. i32 Window::set_interval(JS::Function& callback, i32 interval)
  73. {
  74. auto timer = Timer::create_interval(*this, interval, callback);
  75. m_timers.set(timer->id(), timer);
  76. return timer->id();
  77. }
  78. i32 Window::set_timeout(JS::Function& callback, i32 interval)
  79. {
  80. auto timer = Timer::create_timeout(*this, interval, callback);
  81. m_timers.set(timer->id(), timer);
  82. return timer->id();
  83. }
  84. void Window::timer_did_fire(Badge<Timer>, Timer& timer)
  85. {
  86. // We should not be here if there's no JS wrapper for the Window object.
  87. VERIFY(wrapper());
  88. auto& vm = wrapper()->vm();
  89. // NOTE: This protector pointer keeps the timer alive until the end of this function no matter what.
  90. NonnullRefPtr protector(timer);
  91. if (timer.type() == Timer::Type::Timeout) {
  92. m_timers.remove(timer.id());
  93. }
  94. [[maybe_unused]] auto rc = vm.call(timer.callback(), wrapper());
  95. if (vm.exception())
  96. vm.clear_exception();
  97. vm.run_queued_promise_jobs();
  98. VERIFY(!vm.exception());
  99. }
  100. i32 Window::allocate_timer_id(Badge<Timer>)
  101. {
  102. return m_timer_id_allocator.allocate();
  103. }
  104. void Window::deallocate_timer_id(Badge<Timer>, i32 id)
  105. {
  106. m_timer_id_allocator.deallocate(id);
  107. }
  108. void Window::clear_timeout(i32 timer_id)
  109. {
  110. m_timers.remove(timer_id);
  111. }
  112. void Window::clear_interval(i32 timer_id)
  113. {
  114. m_timers.remove(timer_id);
  115. }
  116. i32 Window::request_animation_frame(JS::Function& callback)
  117. {
  118. // FIXME: This is extremely fake!
  119. static double fake_timestamp = 0;
  120. i32 link_id = GUI::DisplayLink::register_callback([handle = make_handle(&callback)](i32 link_id) {
  121. auto& function = const_cast<JS::Function&>(static_cast<const JS::Function&>(*handle.cell()));
  122. auto& vm = function.vm();
  123. fake_timestamp += 10;
  124. [[maybe_unused]] auto rc = vm.call(function, JS::js_undefined(), JS::Value(fake_timestamp));
  125. if (vm.exception())
  126. vm.clear_exception();
  127. vm.run_queued_promise_jobs();
  128. VERIFY(!vm.exception());
  129. GUI::DisplayLink::unregister_callback(link_id);
  130. });
  131. // FIXME: Don't hand out raw DisplayLink ID's to JavaScript!
  132. return link_id;
  133. }
  134. void Window::cancel_animation_frame(i32 id)
  135. {
  136. // FIXME: We should not be passing untrusted numbers to DisplayLink::unregister_callback()!
  137. GUI::DisplayLink::unregister_callback(id);
  138. }
  139. void Window::did_set_location_href(Badge<Bindings::LocationObject>, const URL& new_href)
  140. {
  141. auto* frame = document().frame();
  142. if (!frame)
  143. return;
  144. frame->loader().load(new_href, FrameLoader::Type::Navigation);
  145. }
  146. void Window::did_call_location_reload(Badge<Bindings::LocationObject>)
  147. {
  148. auto* frame = document().frame();
  149. if (!frame)
  150. return;
  151. frame->loader().load(document().url(), FrameLoader::Type::Reload);
  152. }
  153. bool Window::dispatch_event(NonnullRefPtr<Event> event)
  154. {
  155. return EventDispatcher::dispatch(*this, event, true);
  156. }
  157. JS::Object* Window::create_wrapper(JS::GlobalObject& global_object)
  158. {
  159. return &global_object;
  160. }
  161. int Window::inner_width() const
  162. {
  163. if (!document().layout_node())
  164. return 0;
  165. return document().layout_node()->width();
  166. }
  167. int Window::inner_height() const
  168. {
  169. if (!document().layout_node())
  170. return 0;
  171. return document().layout_node()->height();
  172. }
  173. }