WindowObject.cpp 11 KB

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