WindowObject.cpp 12 KB

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