WindowObject.cpp 12 KB

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