WindowObject.cpp 11 KB

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