WindowObject.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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/ScreenWrapper.h>
  43. #include <LibWeb/Bindings/WindowObject.h>
  44. #include <LibWeb/DOM/Document.h>
  45. #include <LibWeb/DOM/Event.h>
  46. #include <LibWeb/DOM/Window.h>
  47. #include <LibWeb/Origin.h>
  48. #include <LibWeb/Bindings/WindowObjectHelper.h>
  49. namespace Web::Bindings {
  50. WindowObject::WindowObject(DOM::Window& impl)
  51. : m_impl(impl)
  52. {
  53. impl.set_wrapper({}, *this);
  54. }
  55. void WindowObject::initialize_global_object()
  56. {
  57. Base::initialize_global_object();
  58. set_prototype(&ensure_web_prototype<EventTargetPrototype>("EventTarget"));
  59. define_property("window", this, JS::Attribute::Enumerable);
  60. define_property("frames", this, JS::Attribute::Enumerable);
  61. define_property("self", this, JS::Attribute::Enumerable);
  62. define_native_property("document", document_getter, nullptr, JS::Attribute::Enumerable);
  63. define_native_property("performance", performance_getter, nullptr, JS::Attribute::Enumerable);
  64. define_native_property("screen", screen_getter, nullptr, JS::Attribute::Enumerable);
  65. define_native_property("innerWidth", inner_width_getter, nullptr, JS::Attribute::Enumerable);
  66. define_native_property("innerHeight", inner_height_getter, nullptr, JS::Attribute::Enumerable);
  67. define_native_function("alert", alert);
  68. define_native_function("confirm", confirm);
  69. define_native_function("prompt", prompt);
  70. define_native_function("setInterval", set_interval, 1);
  71. define_native_function("setTimeout", set_timeout, 1);
  72. define_native_function("clearInterval", clear_interval, 1);
  73. define_native_function("clearTimeout", clear_timeout, 1);
  74. define_native_function("requestAnimationFrame", request_animation_frame, 1);
  75. define_native_function("cancelAnimationFrame", cancel_animation_frame, 1);
  76. define_native_function("atob", atob, 1);
  77. define_native_function("btoa", btoa, 1);
  78. // Legacy
  79. define_native_property("event", event_getter, nullptr, JS::Attribute::Enumerable);
  80. define_property("navigator", heap().allocate<NavigatorObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
  81. define_property("location", heap().allocate<LocationObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
  82. ADD_WINDOW_OBJECT_INTERFACES;
  83. }
  84. WindowObject::~WindowObject()
  85. {
  86. }
  87. void WindowObject::visit_edges(Visitor& visitor)
  88. {
  89. GlobalObject::visit_edges(visitor);
  90. for (auto& it : m_prototypes)
  91. visitor.visit(it.value);
  92. for (auto& it : m_constructors)
  93. visitor.visit(it.value);
  94. }
  95. Origin WindowObject::origin() const
  96. {
  97. return impl().document().origin();
  98. }
  99. static DOM::Window* impl_from(JS::VM& vm, JS::GlobalObject& global_object)
  100. {
  101. auto* this_object = vm.this_value(global_object).to_object(global_object);
  102. if (!this_object) {
  103. VERIFY_NOT_REACHED();
  104. return nullptr;
  105. }
  106. if (StringView("WindowObject") != this_object->class_name()) {
  107. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "WindowObject");
  108. return nullptr;
  109. }
  110. return &static_cast<WindowObject*>(this_object)->impl();
  111. }
  112. JS_DEFINE_NATIVE_FUNCTION(WindowObject::alert)
  113. {
  114. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#simple-dialogs
  115. // Note: This method is defined using two overloads, instead of using an optional argument,
  116. // for historical reasons. The practical impact of this is that alert(undefined) is
  117. // treated as alert("undefined"), but alert() is treated as alert("").
  118. auto* impl = impl_from(vm, global_object);
  119. if (!impl)
  120. return {};
  121. String message = "";
  122. if (vm.argument_count()) {
  123. message = vm.argument(0).to_string(global_object);
  124. if (vm.exception())
  125. return {};
  126. }
  127. impl->alert(message);
  128. return JS::js_undefined();
  129. }
  130. JS_DEFINE_NATIVE_FUNCTION(WindowObject::confirm)
  131. {
  132. auto* impl = impl_from(vm, global_object);
  133. if (!impl)
  134. return {};
  135. String message = "";
  136. if (!vm.argument(0).is_undefined()) {
  137. message = vm.argument(0).to_string(global_object);
  138. if (vm.exception())
  139. return {};
  140. }
  141. return JS::Value(impl->confirm(message));
  142. }
  143. JS_DEFINE_NATIVE_FUNCTION(WindowObject::prompt)
  144. {
  145. auto* impl = impl_from(vm, global_object);
  146. if (!impl)
  147. return {};
  148. String message = "";
  149. String default_ = "";
  150. if (!vm.argument(0).is_undefined()) {
  151. message = vm.argument(0).to_string(global_object);
  152. if (vm.exception())
  153. return {};
  154. }
  155. if (!vm.argument(1).is_undefined()) {
  156. default_ = vm.argument(1).to_string(global_object);
  157. if (vm.exception())
  158. return {};
  159. }
  160. auto response = impl->prompt(message, default_);
  161. if (response.is_null())
  162. return JS::js_null();
  163. return JS::js_string(vm, response);
  164. }
  165. JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_interval)
  166. {
  167. auto* impl = impl_from(vm, global_object);
  168. if (!impl)
  169. return {};
  170. if (!vm.argument_count()) {
  171. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setInterval");
  172. return {};
  173. }
  174. auto* callback_object = vm.argument(0).to_object(global_object);
  175. if (!callback_object)
  176. return {};
  177. if (!callback_object->is_function()) {
  178. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
  179. return {};
  180. }
  181. i32 interval = 0;
  182. if (vm.argument_count() >= 2) {
  183. interval = vm.argument(1).to_i32(global_object);
  184. if (vm.exception())
  185. return {};
  186. if (interval < 0)
  187. interval = 0;
  188. }
  189. auto timer_id = impl->set_interval(*static_cast<JS::Function*>(callback_object), interval);
  190. return JS::Value(timer_id);
  191. }
  192. JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_timeout)
  193. {
  194. auto* impl = impl_from(vm, global_object);
  195. if (!impl)
  196. return {};
  197. if (!vm.argument_count()) {
  198. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setTimeout");
  199. return {};
  200. }
  201. auto* callback_object = vm.argument(0).to_object(global_object);
  202. if (!callback_object)
  203. return {};
  204. if (!callback_object->is_function()) {
  205. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
  206. return {};
  207. }
  208. i32 interval = 0;
  209. if (vm.argument_count() >= 2) {
  210. interval = vm.argument(1).to_i32(global_object);
  211. if (vm.exception())
  212. return {};
  213. if (interval < 0)
  214. interval = 0;
  215. }
  216. auto timer_id = impl->set_timeout(*static_cast<JS::Function*>(callback_object), interval);
  217. return JS::Value(timer_id);
  218. }
  219. JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_timeout)
  220. {
  221. auto* impl = impl_from(vm, global_object);
  222. if (!impl)
  223. return {};
  224. if (!vm.argument_count()) {
  225. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "clearTimeout");
  226. return {};
  227. }
  228. i32 timer_id = vm.argument(0).to_i32(global_object);
  229. if (vm.exception())
  230. return {};
  231. impl->clear_timeout(timer_id);
  232. return JS::js_undefined();
  233. }
  234. JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_interval)
  235. {
  236. auto* impl = impl_from(vm, global_object);
  237. if (!impl)
  238. return {};
  239. if (!vm.argument_count()) {
  240. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "clearInterval");
  241. return {};
  242. }
  243. i32 timer_id = vm.argument(0).to_i32(global_object);
  244. if (vm.exception())
  245. return {};
  246. impl->clear_timeout(timer_id);
  247. return JS::js_undefined();
  248. }
  249. JS_DEFINE_NATIVE_FUNCTION(WindowObject::request_animation_frame)
  250. {
  251. auto* impl = impl_from(vm, global_object);
  252. if (!impl)
  253. return {};
  254. if (!vm.argument_count()) {
  255. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "requestAnimationFrame");
  256. return {};
  257. }
  258. auto* callback_object = vm.argument(0).to_object(global_object);
  259. if (!callback_object)
  260. return {};
  261. if (!callback_object->is_function()) {
  262. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
  263. return {};
  264. }
  265. return JS::Value(impl->request_animation_frame(*static_cast<JS::Function*>(callback_object)));
  266. }
  267. JS_DEFINE_NATIVE_FUNCTION(WindowObject::cancel_animation_frame)
  268. {
  269. auto* impl = impl_from(vm, global_object);
  270. if (!impl)
  271. return {};
  272. if (!vm.argument_count()) {
  273. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "cancelAnimationFrame");
  274. return {};
  275. }
  276. auto id = vm.argument(0).to_i32(global_object);
  277. if (vm.exception())
  278. return {};
  279. impl->cancel_animation_frame(id);
  280. return JS::js_undefined();
  281. }
  282. JS_DEFINE_NATIVE_FUNCTION(WindowObject::atob)
  283. {
  284. auto* impl = impl_from(vm, global_object);
  285. if (!impl)
  286. return {};
  287. if (!vm.argument_count()) {
  288. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "atob");
  289. return {};
  290. }
  291. auto string = vm.argument(0).to_string(global_object);
  292. if (vm.exception())
  293. return {};
  294. auto decoded = decode_base64(StringView(string));
  295. // decode_base64() returns a byte string. LibJS uses UTF-8 for strings. Use Latin1Decoder to convert bytes 128-255 to UTF-8.
  296. auto decoder = TextCodec::decoder_for("windows-1252");
  297. VERIFY(decoder);
  298. return JS::js_string(vm, decoder->to_utf8(decoded));
  299. }
  300. JS_DEFINE_NATIVE_FUNCTION(WindowObject::btoa)
  301. {
  302. auto* impl = impl_from(vm, global_object);
  303. if (!impl)
  304. return {};
  305. if (!vm.argument_count()) {
  306. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "btoa");
  307. return {};
  308. }
  309. auto string = vm.argument(0).to_string(global_object);
  310. if (vm.exception())
  311. return {};
  312. Vector<u8> byte_string;
  313. byte_string.ensure_capacity(string.length());
  314. for (u32 code_point : Utf8View(string)) {
  315. if (code_point > 0xff) {
  316. vm.throw_exception<JS::InvalidCharacterError>(global_object, JS::ErrorType::NotAByteString, "btoa");
  317. return {};
  318. }
  319. byte_string.append(code_point);
  320. }
  321. auto encoded = encode_base64(byte_string.span());
  322. return JS::js_string(vm, move(encoded));
  323. }
  324. JS_DEFINE_NATIVE_GETTER(WindowObject::document_getter)
  325. {
  326. auto* impl = impl_from(vm, global_object);
  327. if (!impl)
  328. return {};
  329. return wrap(global_object, impl->document());
  330. }
  331. JS_DEFINE_NATIVE_GETTER(WindowObject::performance_getter)
  332. {
  333. auto* impl = impl_from(vm, global_object);
  334. if (!impl)
  335. return {};
  336. return wrap(global_object, impl->performance());
  337. }
  338. JS_DEFINE_NATIVE_GETTER(WindowObject::screen_getter)
  339. {
  340. auto* impl = impl_from(vm, global_object);
  341. if (!impl)
  342. return {};
  343. return wrap(global_object, impl->screen());
  344. }
  345. JS_DEFINE_NATIVE_GETTER(WindowObject::event_getter)
  346. {
  347. auto* impl = impl_from(vm, global_object);
  348. if (!impl)
  349. return {};
  350. if (!impl->current_event())
  351. return JS::js_undefined();
  352. return wrap(global_object, const_cast<DOM::Event&>(*impl->current_event()));
  353. }
  354. JS_DEFINE_NATIVE_GETTER(WindowObject::inner_width_getter)
  355. {
  356. auto* impl = impl_from(vm, global_object);
  357. if (!impl)
  358. return {};
  359. return JS::Value(impl->inner_width());
  360. }
  361. JS_DEFINE_NATIVE_GETTER(WindowObject::inner_height_getter)
  362. {
  363. auto* impl = impl_from(vm, global_object);
  364. if (!impl)
  365. return {};
  366. return JS::Value(impl->inner_height());
  367. }
  368. }