WindowObject.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 <AK/FlyString.h>
  27. #include <AK/Function.h>
  28. #include <LibJS/Interpreter.h>
  29. #include <LibJS/Runtime/Error.h>
  30. #include <LibJS/Runtime/Function.h>
  31. #include <LibWeb/Bindings/DocumentWrapper.h>
  32. #include <LibWeb/Bindings/NavigatorObject.h>
  33. #include <LibWeb/Bindings/WindowObject.h>
  34. #include <LibWeb/Bindings/XMLHttpRequestConstructor.h>
  35. #include <LibWeb/Bindings/XMLHttpRequestPrototype.h>
  36. #include <LibWeb/DOM/Document.h>
  37. #include <LibWeb/DOM/Window.h>
  38. namespace Web {
  39. namespace Bindings {
  40. WindowObject::WindowObject(Window& impl)
  41. : m_impl(impl)
  42. {
  43. }
  44. void WindowObject::initialize()
  45. {
  46. GlobalObject::initialize();
  47. put("window", this);
  48. put_native_property("document", document_getter, document_setter);
  49. put_native_function("alert", alert);
  50. put_native_function("confirm", confirm);
  51. put_native_function("setInterval", set_interval, 1);
  52. put_native_function("setTimeout", set_timeout, 1);
  53. put_native_function("requestAnimationFrame", request_animation_frame, 1);
  54. put_native_function("cancelAnimationFrame", cancel_animation_frame, 1);
  55. put("navigator", heap().allocate<NavigatorObject>());
  56. m_xhr_prototype = heap().allocate<XMLHttpRequestPrototype>();
  57. m_xhr_constructor = heap().allocate<XMLHttpRequestConstructor>();
  58. m_xhr_constructor->put("prototype", m_xhr_prototype);
  59. put("XMLHttpRequest", m_xhr_constructor);
  60. }
  61. WindowObject::~WindowObject()
  62. {
  63. }
  64. void WindowObject::visit_children(Visitor& visitor)
  65. {
  66. GlobalObject::visit_children(visitor);
  67. visitor.visit(m_xhr_constructor);
  68. visitor.visit(m_xhr_prototype);
  69. }
  70. static Window* impl_from(JS::Interpreter& interpreter)
  71. {
  72. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  73. if (!this_object) {
  74. ASSERT_NOT_REACHED();
  75. return nullptr;
  76. }
  77. if (StringView("WindowObject") != this_object->class_name()) {
  78. interpreter.throw_exception<JS::TypeError>("That's not a WindowObject, bro.");
  79. return nullptr;
  80. }
  81. return &static_cast<WindowObject*>(this_object)->impl();
  82. }
  83. JS::Value WindowObject::alert(JS::Interpreter& interpreter)
  84. {
  85. auto* impl = impl_from(interpreter);
  86. if (!impl)
  87. return {};
  88. String message = "";
  89. if (interpreter.argument_count())
  90. message = interpreter.argument(0).to_string();
  91. impl->alert(message);
  92. return JS::js_undefined();
  93. }
  94. JS::Value WindowObject::confirm(JS::Interpreter& interpreter)
  95. {
  96. auto* impl = impl_from(interpreter);
  97. if (!impl)
  98. return {};
  99. String message = "";
  100. if (interpreter.argument_count())
  101. message = interpreter.argument(0).to_string();
  102. return JS::Value(impl->confirm(message));
  103. }
  104. JS::Value WindowObject::set_interval(JS::Interpreter& interpreter)
  105. {
  106. auto* impl = impl_from(interpreter);
  107. if (!impl)
  108. return {};
  109. auto& arguments = interpreter.call_frame().arguments;
  110. if (arguments.size() < 2)
  111. return {};
  112. auto* callback_object = arguments[0].to_object(interpreter.heap());
  113. if (!callback_object)
  114. return {};
  115. if (!callback_object->is_function())
  116. return interpreter.throw_exception<JS::TypeError>("Not a function");
  117. impl->set_interval(*static_cast<JS::Function*>(callback_object), arguments[1].to_i32());
  118. return JS::js_undefined();
  119. }
  120. JS::Value WindowObject::set_timeout(JS::Interpreter& interpreter)
  121. {
  122. auto* impl = impl_from(interpreter);
  123. if (!impl)
  124. return {};
  125. auto& arguments = interpreter.call_frame().arguments;
  126. if (arguments.size() < 1)
  127. return {};
  128. auto* callback_object = arguments[0].to_object(interpreter.heap());
  129. if (!callback_object)
  130. return {};
  131. if (!callback_object->is_function())
  132. return interpreter.throw_exception<JS::TypeError>("Not a function");
  133. i32 interval = 0;
  134. if (interpreter.argument_count() >= 2)
  135. interval = arguments[1].to_i32();
  136. impl->set_timeout(*static_cast<JS::Function*>(callback_object), interval);
  137. return JS::js_undefined();
  138. }
  139. JS::Value WindowObject::request_animation_frame(JS::Interpreter& interpreter)
  140. {
  141. auto* impl = impl_from(interpreter);
  142. if (!impl)
  143. return {};
  144. auto& arguments = interpreter.call_frame().arguments;
  145. if (arguments.size() < 1)
  146. return {};
  147. auto* callback_object = arguments[0].to_object(interpreter.heap());
  148. if (!callback_object)
  149. return {};
  150. if (!callback_object->is_function())
  151. return interpreter.throw_exception<JS::TypeError>("Not a function");
  152. return JS::Value(impl->request_animation_frame(*static_cast<JS::Function*>(callback_object)));
  153. }
  154. JS::Value WindowObject::cancel_animation_frame(JS::Interpreter& interpreter)
  155. {
  156. auto* impl = impl_from(interpreter);
  157. if (!impl)
  158. return {};
  159. auto& arguments = interpreter.call_frame().arguments;
  160. if (arguments.size() < 1)
  161. return {};
  162. impl->cancel_animation_frame(arguments[0].to_i32());
  163. return JS::js_undefined();
  164. }
  165. JS::Value WindowObject::document_getter(JS::Interpreter& interpreter)
  166. {
  167. auto* impl = impl_from(interpreter);
  168. if (!impl)
  169. return {};
  170. return wrap(interpreter.heap(), impl->document());
  171. }
  172. void WindowObject::document_setter(JS::Interpreter&, JS::Value)
  173. {
  174. // FIXME: Figure out what we should do here. Just ignore attempts to set window.document for now.
  175. }
  176. }
  177. }