WindowObject.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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/String.h>
  28. #include <AK/Utf8View.h>
  29. #include <LibJS/Runtime/Error.h>
  30. #include <LibJS/Runtime/Function.h>
  31. #include <LibJS/Runtime/Shape.h>
  32. #include <LibTextCodec/Decoder.h>
  33. #include <LibWeb/Bindings/DocumentWrapper.h>
  34. #include <LibWeb/Bindings/EventTargetConstructor.h>
  35. #include <LibWeb/Bindings/EventTargetPrototype.h>
  36. #include <LibWeb/Bindings/EventWrapper.h>
  37. #include <LibWeb/Bindings/EventWrapperFactory.h>
  38. #include <LibWeb/Bindings/LocationObject.h>
  39. #include <LibWeb/Bindings/NavigatorObject.h>
  40. #include <LibWeb/Bindings/NodeWrapperFactory.h>
  41. #include <LibWeb/Bindings/PerformanceWrapper.h>
  42. #include <LibWeb/Bindings/WindowObject.h>
  43. #include <LibWeb/DOM/Document.h>
  44. #include <LibWeb/DOM/Event.h>
  45. #include <LibWeb/DOM/Window.h>
  46. #include <LibWeb/Origin.h>
  47. #include <LibWeb/Bindings/WindowObjectHelper.h>
  48. namespace Web::Bindings {
  49. WindowObject::WindowObject(DOM::Window& impl)
  50. : m_impl(impl)
  51. {
  52. impl.set_wrapper({}, *this);
  53. }
  54. void WindowObject::initialize_global_object()
  55. {
  56. Base::initialize_global_object();
  57. set_prototype(&ensure_web_prototype<EventTargetPrototype>("EventTarget"));
  58. define_property("window", this, JS::Attribute::Enumerable);
  59. define_property("frames", this, JS::Attribute::Enumerable);
  60. define_property("self", this, JS::Attribute::Enumerable);
  61. define_native_property("document", document_getter, document_setter, JS::Attribute::Enumerable);
  62. define_native_property("performance", performance_getter, nullptr, JS::Attribute::Enumerable);
  63. define_native_property("innerWidth", inner_width_getter, nullptr, JS::Attribute::Enumerable);
  64. define_native_property("innerHeight", inner_height_getter, nullptr, JS::Attribute::Enumerable);
  65. define_native_function("alert", alert);
  66. define_native_function("confirm", confirm);
  67. define_native_function("prompt", prompt);
  68. define_native_function("setInterval", set_interval, 1);
  69. define_native_function("setTimeout", set_timeout, 1);
  70. define_native_function("clearInterval", clear_interval, 1);
  71. define_native_function("clearTimeout", clear_timeout, 1);
  72. define_native_function("requestAnimationFrame", request_animation_frame, 1);
  73. define_native_function("cancelAnimationFrame", cancel_animation_frame, 1);
  74. define_native_function("atob", atob, 1);
  75. define_native_function("btoa", btoa, 1);
  76. // Legacy
  77. define_native_property("event", event_getter, nullptr, JS::Attribute::Enumerable);
  78. define_property("navigator", heap().allocate<NavigatorObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
  79. define_property("location", heap().allocate<LocationObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
  80. ADD_WINDOW_OBJECT_INTERFACES;
  81. }
  82. WindowObject::~WindowObject()
  83. {
  84. }
  85. void WindowObject::visit_edges(Visitor& visitor)
  86. {
  87. GlobalObject::visit_edges(visitor);
  88. for (auto& it : m_prototypes)
  89. visitor.visit(it.value);
  90. for (auto& it : m_constructors)
  91. visitor.visit(it.value);
  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. VERIFY_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. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#simple-dialogs
  113. // Note: This method is defined using two overloads, instead of using an optional argument,
  114. // for historical reasons. The practical impact of this is that alert(undefined) is
  115. // treated as alert("undefined"), but alert() is treated as alert("").
  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(0).is_undefined()) {
  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::prompt)
  142. {
  143. auto* impl = impl_from(vm, global_object);
  144. if (!impl)
  145. return {};
  146. String message = "";
  147. String default_ = "";
  148. if (!vm.argument(0).is_undefined()) {
  149. message = vm.argument(0).to_string(global_object);
  150. if (vm.exception())
  151. return {};
  152. }
  153. if (!vm.argument(1).is_undefined()) {
  154. default_ = vm.argument(1).to_string(global_object);
  155. if (vm.exception())
  156. return {};
  157. }
  158. auto response = impl->prompt(message, default_);
  159. if (response.is_null())
  160. return JS::js_null();
  161. return JS::js_string(vm, response);
  162. }
  163. JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_interval)
  164. {
  165. auto* impl = impl_from(vm, global_object);
  166. if (!impl)
  167. return {};
  168. if (!vm.argument_count()) {
  169. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setInterval");
  170. return {};
  171. }
  172. auto* callback_object = vm.argument(0).to_object(global_object);
  173. if (!callback_object)
  174. return {};
  175. if (!callback_object->is_function()) {
  176. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
  177. return {};
  178. }
  179. i32 interval = 0;
  180. if (vm.argument_count() >= 2) {
  181. interval = vm.argument(1).to_i32(global_object);
  182. if (vm.exception())
  183. return {};
  184. if (interval < 0)
  185. interval = 0;
  186. }
  187. auto timer_id = impl->set_interval(*static_cast<JS::Function*>(callback_object), interval);
  188. return JS::Value(timer_id);
  189. }
  190. JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_timeout)
  191. {
  192. auto* impl = impl_from(vm, global_object);
  193. if (!impl)
  194. return {};
  195. if (!vm.argument_count()) {
  196. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setTimeout");
  197. return {};
  198. }
  199. auto* callback_object = vm.argument(0).to_object(global_object);
  200. if (!callback_object)
  201. return {};
  202. if (!callback_object->is_function()) {
  203. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
  204. return {};
  205. }
  206. i32 interval = 0;
  207. if (vm.argument_count() >= 2) {
  208. interval = vm.argument(1).to_i32(global_object);
  209. if (vm.exception())
  210. return {};
  211. if (interval < 0)
  212. interval = 0;
  213. }
  214. auto timer_id = impl->set_timeout(*static_cast<JS::Function*>(callback_object), interval);
  215. return JS::Value(timer_id);
  216. }
  217. JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_timeout)
  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, "clearTimeout");
  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::clear_interval)
  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::BadArgCountAtLeastOne, "clearInterval");
  239. return {};
  240. }
  241. i32 timer_id = vm.argument(0).to_i32(global_object);
  242. if (vm.exception())
  243. return {};
  244. impl->clear_timeout(timer_id);
  245. return JS::js_undefined();
  246. }
  247. JS_DEFINE_NATIVE_FUNCTION(WindowObject::request_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, "requestAnimationFrame");
  254. return {};
  255. }
  256. auto* callback_object = vm.argument(0).to_object(global_object);
  257. if (!callback_object)
  258. return {};
  259. if (!callback_object->is_function()) {
  260. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
  261. return {};
  262. }
  263. return JS::Value(impl->request_animation_frame(*static_cast<JS::Function*>(callback_object)));
  264. }
  265. JS_DEFINE_NATIVE_FUNCTION(WindowObject::cancel_animation_frame)
  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, "cancelAnimationFrame");
  272. return {};
  273. }
  274. auto id = vm.argument(0).to_i32(global_object);
  275. if (vm.exception())
  276. return {};
  277. impl->cancel_animation_frame(id);
  278. return JS::js_undefined();
  279. }
  280. JS_DEFINE_NATIVE_FUNCTION(WindowObject::atob)
  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, "atob");
  287. return {};
  288. }
  289. auto string = vm.argument(0).to_string(global_object);
  290. if (vm.exception())
  291. return {};
  292. auto decoded = decode_base64(StringView(string));
  293. // decode_base64() returns a byte string. LibJS uses UTF-8 for strings. Use Latin1Decoder to convert bytes 128-255 to UTF-8.
  294. auto decoder = TextCodec::decoder_for("windows-1252");
  295. VERIFY(decoder);
  296. return JS::js_string(vm, decoder->to_utf8(decoded));
  297. }
  298. JS_DEFINE_NATIVE_FUNCTION(WindowObject::btoa)
  299. {
  300. auto* impl = impl_from(vm, global_object);
  301. if (!impl)
  302. return {};
  303. if (!vm.argument_count()) {
  304. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "btoa");
  305. return {};
  306. }
  307. auto string = vm.argument(0).to_string(global_object);
  308. if (vm.exception())
  309. return {};
  310. Vector<u8> byte_string;
  311. byte_string.ensure_capacity(string.length());
  312. for (u32 code_point : Utf8View(string)) {
  313. if (code_point > 0xff) {
  314. vm.throw_exception<JS::InvalidCharacterError>(global_object, JS::ErrorType::NotAByteString, "btoa");
  315. return {};
  316. }
  317. byte_string.append(code_point);
  318. }
  319. auto encoded = encode_base64(byte_string.span());
  320. return JS::js_string(vm, move(encoded));
  321. }
  322. JS_DEFINE_NATIVE_GETTER(WindowObject::document_getter)
  323. {
  324. auto* impl = impl_from(vm, global_object);
  325. if (!impl)
  326. return {};
  327. return wrap(global_object, impl->document());
  328. }
  329. JS_DEFINE_NATIVE_SETTER(WindowObject::document_setter)
  330. {
  331. // FIXME: Figure out what we should do here. Just ignore attempts to set window.document for now.
  332. }
  333. JS_DEFINE_NATIVE_GETTER(WindowObject::performance_getter)
  334. {
  335. auto* impl = impl_from(vm, global_object);
  336. if (!impl)
  337. return {};
  338. return wrap(global_object, impl->performance());
  339. }
  340. JS_DEFINE_NATIVE_GETTER(WindowObject::event_getter)
  341. {
  342. auto* impl = impl_from(vm, global_object);
  343. if (!impl)
  344. return {};
  345. if (!impl->current_event())
  346. return JS::js_undefined();
  347. return wrap(global_object, const_cast<DOM::Event&>(*impl->current_event()));
  348. }
  349. JS_DEFINE_NATIVE_GETTER(WindowObject::inner_width_getter)
  350. {
  351. auto* impl = impl_from(vm, global_object);
  352. if (!impl)
  353. return {};
  354. return JS::Value(impl->inner_width());
  355. }
  356. JS_DEFINE_NATIVE_GETTER(WindowObject::inner_height_getter)
  357. {
  358. auto* impl = impl_from(vm, global_object);
  359. if (!impl)
  360. return {};
  361. return JS::Value(impl->inner_height());
  362. }
  363. }