WindowObject.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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/Base64.h>
  27. #include <AK/ByteBuffer.h>
  28. #include <AK/FlyString.h>
  29. #include <AK/Function.h>
  30. #include <AK/String.h>
  31. #include <LibJS/Interpreter.h>
  32. #include <LibJS/Runtime/Error.h>
  33. #include <LibJS/Runtime/Function.h>
  34. #include <LibJS/Runtime/Shape.h>
  35. #include <LibWeb/Bindings/DocumentWrapper.h>
  36. #include <LibWeb/Bindings/LocationObject.h>
  37. #include <LibWeb/Bindings/NavigatorObject.h>
  38. #include <LibWeb/Bindings/NodeWrapperFactory.h>
  39. #include <LibWeb/Bindings/WindowObject.h>
  40. #include <LibWeb/Bindings/XMLHttpRequestConstructor.h>
  41. #include <LibWeb/Bindings/XMLHttpRequestPrototype.h>
  42. #include <LibWeb/DOM/Document.h>
  43. #include <LibWeb/DOM/Window.h>
  44. namespace Web {
  45. namespace Bindings {
  46. WindowObject::WindowObject(Window& impl)
  47. : m_impl(impl)
  48. {
  49. impl.set_wrapper({}, *this);
  50. }
  51. void WindowObject::initialize()
  52. {
  53. GlobalObject::initialize();
  54. define_property("window", this, JS::Attribute::Enumerable);
  55. define_native_property("document", document_getter, document_setter, JS::Attribute::Enumerable);
  56. define_native_function("alert", alert);
  57. define_native_function("confirm", confirm);
  58. define_native_function("setInterval", set_interval, 1);
  59. define_native_function("setTimeout", set_timeout, 1);
  60. define_native_function("clearInterval", clear_interval, 1);
  61. define_native_function("clearTimeout", clear_timeout, 1);
  62. define_native_function("requestAnimationFrame", request_animation_frame, 1);
  63. define_native_function("cancelAnimationFrame", cancel_animation_frame, 1);
  64. define_native_function("atob", atob, 1);
  65. define_native_function("btoa", btoa, 1);
  66. define_property("navigator", heap().allocate<NavigatorObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
  67. define_property("location", heap().allocate<LocationObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
  68. m_xhr_prototype = heap().allocate<XMLHttpRequestPrototype>(*this, *this);
  69. m_xhr_constructor = heap().allocate<XMLHttpRequestConstructor>(*this, *this);
  70. m_xhr_constructor->define_property("prototype", m_xhr_prototype, 0);
  71. add_constructor("XMLHttpRequest", m_xhr_constructor, *m_xhr_prototype);
  72. }
  73. WindowObject::~WindowObject()
  74. {
  75. }
  76. void WindowObject::visit_children(Visitor& visitor)
  77. {
  78. GlobalObject::visit_children(visitor);
  79. visitor.visit(m_xhr_constructor);
  80. visitor.visit(m_xhr_prototype);
  81. }
  82. static Window* impl_from(JS::Interpreter& interpreter, JS::GlobalObject& global_object)
  83. {
  84. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  85. if (!this_object) {
  86. ASSERT_NOT_REACHED();
  87. return nullptr;
  88. }
  89. if (StringView("WindowObject") != this_object->class_name()) {
  90. interpreter.throw_exception<JS::TypeError>(JS::ErrorType::NotA, "WindowObject");
  91. return nullptr;
  92. }
  93. return &static_cast<WindowObject*>(this_object)->impl();
  94. }
  95. JS_DEFINE_NATIVE_FUNCTION(WindowObject::alert)
  96. {
  97. auto* impl = impl_from(interpreter, global_object);
  98. if (!impl)
  99. return {};
  100. String message = "";
  101. if (interpreter.argument_count()) {
  102. message = interpreter.argument(0).to_string(interpreter);
  103. if (interpreter.exception())
  104. return {};
  105. }
  106. impl->alert(message);
  107. return JS::js_undefined();
  108. }
  109. JS_DEFINE_NATIVE_FUNCTION(WindowObject::confirm)
  110. {
  111. auto* impl = impl_from(interpreter, global_object);
  112. if (!impl)
  113. return {};
  114. String message = "";
  115. if (interpreter.argument_count()) {
  116. message = interpreter.argument(0).to_string(interpreter);
  117. if (interpreter.exception())
  118. return {};
  119. }
  120. return JS::Value(impl->confirm(message));
  121. }
  122. JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_interval)
  123. {
  124. auto* impl = impl_from(interpreter, global_object);
  125. if (!impl)
  126. return {};
  127. if (!interpreter.argument_count())
  128. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountAtLeastOne, "setInterval");
  129. auto* callback_object = interpreter.argument(0).to_object(interpreter, global_object);
  130. if (!callback_object)
  131. return {};
  132. if (!callback_object->is_function())
  133. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::NotAFunctionNoParam);
  134. i32 interval = 0;
  135. if (interpreter.argument_count() >= 2) {
  136. interval = interpreter.argument(1).to_i32(interpreter);
  137. if (interpreter.exception())
  138. return {};
  139. if (interval < 0)
  140. interval = 0;
  141. }
  142. auto timer_id = impl->set_interval(*static_cast<JS::Function*>(callback_object), interval);
  143. return JS::Value(timer_id);
  144. }
  145. JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_timeout)
  146. {
  147. auto* impl = impl_from(interpreter, global_object);
  148. if (!impl)
  149. return {};
  150. if (!interpreter.argument_count())
  151. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountAtLeastOne, "setTimeout");
  152. auto* callback_object = interpreter.argument(0).to_object(interpreter, global_object);
  153. if (!callback_object)
  154. return {};
  155. if (!callback_object->is_function())
  156. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::NotAFunctionNoParam);
  157. i32 interval = 0;
  158. if (interpreter.argument_count() >= 2) {
  159. interval = interpreter.argument(1).to_i32(interpreter);
  160. if (interpreter.exception())
  161. return {};
  162. if (interval < 0)
  163. interval = 0;
  164. }
  165. auto timer_id = impl->set_timeout(*static_cast<JS::Function*>(callback_object), interval);
  166. return JS::Value(timer_id);
  167. }
  168. JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_timeout)
  169. {
  170. auto* impl = impl_from(interpreter, global_object);
  171. if (!impl)
  172. return {};
  173. if (!interpreter.argument_count())
  174. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountAtLeastOne, "clearTimeout");
  175. i32 timer_id = interpreter.argument(0).to_i32(interpreter);
  176. if (interpreter.exception())
  177. return {};
  178. impl->clear_timeout(timer_id);
  179. return JS::js_undefined();
  180. }
  181. JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_interval)
  182. {
  183. auto* impl = impl_from(interpreter, global_object);
  184. if (!impl)
  185. return {};
  186. if (!interpreter.argument_count())
  187. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountAtLeastOne, "clearInterval");
  188. i32 timer_id = interpreter.argument(0).to_i32(interpreter);
  189. if (interpreter.exception())
  190. return {};
  191. impl->clear_timeout(timer_id);
  192. return JS::js_undefined();
  193. }
  194. JS_DEFINE_NATIVE_FUNCTION(WindowObject::request_animation_frame)
  195. {
  196. auto* impl = impl_from(interpreter, global_object);
  197. if (!impl)
  198. return {};
  199. if (!interpreter.argument_count())
  200. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountOne, "requestAnimationFrame");
  201. auto* callback_object = interpreter.argument(0).to_object(interpreter, global_object);
  202. if (!callback_object)
  203. return {};
  204. if (!callback_object->is_function())
  205. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::NotAFunctionNoParam);
  206. return JS::Value(impl->request_animation_frame(*static_cast<JS::Function*>(callback_object)));
  207. }
  208. JS_DEFINE_NATIVE_FUNCTION(WindowObject::cancel_animation_frame)
  209. {
  210. auto* impl = impl_from(interpreter, global_object);
  211. if (!impl)
  212. return {};
  213. if (!interpreter.argument_count())
  214. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountOne, "cancelAnimationFrame");
  215. auto id = interpreter.argument(0).to_i32(interpreter);
  216. if (interpreter.exception())
  217. return {};
  218. impl->cancel_animation_frame(id);
  219. return JS::js_undefined();
  220. }
  221. JS_DEFINE_NATIVE_FUNCTION(WindowObject::atob)
  222. {
  223. auto* impl = impl_from(interpreter, global_object);
  224. if (!impl)
  225. return {};
  226. if (!interpreter.argument_count())
  227. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountOne, "atob");
  228. auto string = interpreter.argument(0).to_string(interpreter);
  229. if (interpreter.exception())
  230. return {};
  231. // FIXME: This should convert string from a byte string to LibJS's internal string encoding (UTF-8).
  232. auto decoded = decode_base64(StringView(string));
  233. return JS::js_string(interpreter, String::copy(decoded));
  234. }
  235. JS_DEFINE_NATIVE_FUNCTION(WindowObject::btoa)
  236. {
  237. auto* impl = impl_from(interpreter, global_object);
  238. if (!impl)
  239. return {};
  240. if (!interpreter.argument_count())
  241. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountOne, "btoa");
  242. auto string = interpreter.argument(0).to_string(interpreter);
  243. if (interpreter.exception())
  244. return {};
  245. // FIXME: This should convert string to a non-UTF-8 byte string first.
  246. auto encoded = encode_base64(StringView(string));
  247. return JS::js_string(interpreter, String::copy(encoded));
  248. }
  249. JS_DEFINE_NATIVE_GETTER(WindowObject::document_getter)
  250. {
  251. auto* impl = impl_from(interpreter, global_object);
  252. if (!impl)
  253. return {};
  254. return wrap(global_object, impl->document());
  255. }
  256. JS_DEFINE_NATIVE_SETTER(WindowObject::document_setter)
  257. {
  258. // FIXME: Figure out what we should do here. Just ignore attempts to set window.document for now.
  259. UNUSED_PARAM(value);
  260. }
  261. }
  262. }