WindowObject.cpp 12 KB

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