WindowObject.cpp 12 KB

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