WindowObject.cpp 12 KB

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