Window.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <LibGUI/MessageBox.h>
  28. #include <LibJS/Interpreter.h>
  29. #include <LibJS/Runtime/Function.h>
  30. #include <LibJS/Runtime/MarkedValueList.h>
  31. #include <LibWeb/DOM/Document.h>
  32. #include <LibWeb/DOM/Timer.h>
  33. #include <LibWeb/DOM/Window.h>
  34. #include <LibWeb/Page/Frame.h>
  35. #include <LibWeb/PageView.h>
  36. namespace Web::DOM {
  37. NonnullRefPtr<Window> Window::create_with_document(Document& document)
  38. {
  39. return adopt(*new Window(document));
  40. }
  41. Window::Window(Document& document)
  42. : m_document(document)
  43. {
  44. }
  45. Window::~Window()
  46. {
  47. }
  48. void Window::set_wrapper(Badge<Bindings::WindowObject>, Bindings::WindowObject& wrapper)
  49. {
  50. m_wrapper = wrapper.make_weak_ptr();
  51. }
  52. void Window::alert(const String& message)
  53. {
  54. GUI::MessageBox::show(nullptr, message, "Alert", GUI::MessageBox::Type::Information);
  55. }
  56. bool Window::confirm(const String& message)
  57. {
  58. auto confirm_result = GUI::MessageBox::show(nullptr, message, "Confirm", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OKCancel);
  59. return confirm_result == GUI::Dialog::ExecResult::ExecOK;
  60. }
  61. i32 Window::set_interval(JS::Function& callback, i32 interval)
  62. {
  63. auto timer = Timer::create_interval(*this, interval, callback);
  64. m_timers.set(timer->id(), timer);
  65. return timer->id();
  66. }
  67. i32 Window::set_timeout(JS::Function& callback, i32 interval)
  68. {
  69. auto timer = Timer::create_timeout(*this, interval, callback);
  70. m_timers.set(timer->id(), timer);
  71. return timer->id();
  72. }
  73. void Window::timer_did_fire(Badge<Timer>, Timer& timer)
  74. {
  75. // We should not be here if there's no JS wrapper for the Window object.
  76. ASSERT(wrapper());
  77. // NOTE: This protector pointer keeps the timer alive until the end of this function no matter what.
  78. NonnullRefPtr protector(timer);
  79. if (timer.type() == Timer::Type::Timeout) {
  80. m_timers.remove(timer.id());
  81. }
  82. auto& interpreter = wrapper()->interpreter();
  83. interpreter.call(timer.callback(), wrapper());
  84. }
  85. i32 Window::allocate_timer_id(Badge<Timer>)
  86. {
  87. return m_timer_id_allocator.allocate();
  88. }
  89. void Window::clear_timeout(i32 timer_id)
  90. {
  91. m_timers.remove(timer_id);
  92. }
  93. void Window::clear_interval(i32 timer_id)
  94. {
  95. m_timers.remove(timer_id);
  96. }
  97. i32 Window::request_animation_frame(JS::Function& callback)
  98. {
  99. // FIXME: This is extremely fake!
  100. static double fake_timestamp = 0;
  101. i32 link_id = GUI::DisplayLink::register_callback([handle = make_handle(&callback)](i32 link_id) {
  102. auto& function = const_cast<JS::Function&>(static_cast<const JS::Function&>(*handle.cell()));
  103. auto& interpreter = function.interpreter();
  104. JS::MarkedValueList arguments(interpreter.heap());
  105. arguments.append(JS::Value(fake_timestamp));
  106. fake_timestamp += 10;
  107. interpreter.call(function, {}, move(arguments));
  108. GUI::DisplayLink::unregister_callback(link_id);
  109. });
  110. // FIXME: Don't hand out raw DisplayLink ID's to JavaScript!
  111. return link_id;
  112. }
  113. void Window::cancel_animation_frame(i32 id)
  114. {
  115. // FIXME: We should not be passing untrusted numbers to DisplayLink::unregister_callback()!
  116. GUI::DisplayLink::unregister_callback(id);
  117. }
  118. void Window::did_set_location_href(Badge<Bindings::LocationObject>, const String& new_href)
  119. {
  120. auto* frame = document().frame();
  121. if (!frame)
  122. return;
  123. frame->loader().load(new_href, FrameLoader::Type::Navigation);
  124. }
  125. void Window::did_call_location_reload(Badge<Bindings::LocationObject>)
  126. {
  127. auto* frame = document().frame();
  128. if (!frame)
  129. return;
  130. frame->loader().load(document().url(), FrameLoader::Type::Reload);
  131. }
  132. }