WindowObject.cpp 6.9 KB

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