WindowObject.cpp 7.8 KB

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