WindowObject.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Base64.h>
  7. #include <AK/String.h>
  8. #include <AK/Utf8View.h>
  9. #include <LibJS/Runtime/Error.h>
  10. #include <LibJS/Runtime/FunctionObject.h>
  11. #include <LibJS/Runtime/Shape.h>
  12. #include <LibTextCodec/Decoder.h>
  13. #include <LibWeb/Bindings/DocumentWrapper.h>
  14. #include <LibWeb/Bindings/EventTargetConstructor.h>
  15. #include <LibWeb/Bindings/EventTargetPrototype.h>
  16. #include <LibWeb/Bindings/EventWrapper.h>
  17. #include <LibWeb/Bindings/EventWrapperFactory.h>
  18. #include <LibWeb/Bindings/LocationObject.h>
  19. #include <LibWeb/Bindings/NavigatorObject.h>
  20. #include <LibWeb/Bindings/NodeWrapperFactory.h>
  21. #include <LibWeb/Bindings/PerformanceWrapper.h>
  22. #include <LibWeb/Bindings/ScreenWrapper.h>
  23. #include <LibWeb/Bindings/WindowObject.h>
  24. #include <LibWeb/DOM/Document.h>
  25. #include <LibWeb/DOM/Event.h>
  26. #include <LibWeb/DOM/Window.h>
  27. #include <LibWeb/Origin.h>
  28. #include <LibWeb/Page/BrowsingContext.h>
  29. #include <LibWeb/WebAssembly/WebAssemblyObject.h>
  30. #include <LibWeb/Bindings/WindowObjectHelper.h>
  31. namespace Web::Bindings {
  32. WindowObject::WindowObject(DOM::Window& impl)
  33. : m_impl(impl)
  34. {
  35. impl.set_wrapper({}, *this);
  36. }
  37. void WindowObject::initialize_global_object()
  38. {
  39. Base::initialize_global_object();
  40. auto success = Object::internal_set_prototype_of(&ensure_web_prototype<EventTargetPrototype>("EventTarget"));
  41. VERIFY(success);
  42. // FIXME: These should be native accessors, not properties
  43. define_direct_property("window", this, JS::Attribute::Enumerable);
  44. define_direct_property("frames", this, JS::Attribute::Enumerable);
  45. define_direct_property("self", this, JS::Attribute::Enumerable);
  46. define_native_accessor("top", top_getter, nullptr, JS::Attribute::Enumerable);
  47. define_native_accessor("parent", parent_getter, {}, JS::Attribute::Enumerable);
  48. define_native_accessor("document", document_getter, {}, JS::Attribute::Enumerable);
  49. define_native_accessor("performance", performance_getter, {}, JS::Attribute::Enumerable);
  50. define_native_accessor("screen", screen_getter, {}, JS::Attribute::Enumerable);
  51. define_native_accessor("innerWidth", inner_width_getter, {}, JS::Attribute::Enumerable);
  52. define_native_accessor("innerHeight", inner_height_getter, {}, JS::Attribute::Enumerable);
  53. u8 attr = JS::Attribute::Writable | JS::Attribute::Enumerable | JS::Attribute::Configurable;
  54. define_native_function("alert", alert, 0, attr);
  55. define_native_function("confirm", confirm, 0, attr);
  56. define_native_function("prompt", prompt, 0, attr);
  57. define_native_function("setInterval", set_interval, 1, attr);
  58. define_native_function("setTimeout", set_timeout, 1, attr);
  59. define_native_function("clearInterval", clear_interval, 1, attr);
  60. define_native_function("clearTimeout", clear_timeout, 1, attr);
  61. define_native_function("requestAnimationFrame", request_animation_frame, 1, attr);
  62. define_native_function("cancelAnimationFrame", cancel_animation_frame, 1, attr);
  63. define_native_function("atob", atob, 1, attr);
  64. define_native_function("btoa", btoa, 1, attr);
  65. // Legacy
  66. define_native_accessor("event", event_getter, {}, JS::Attribute::Enumerable);
  67. define_direct_property("navigator", heap().allocate<NavigatorObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
  68. define_direct_property("location", heap().allocate<LocationObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
  69. // WebAssembly "namespace"
  70. define_direct_property("WebAssembly", heap().allocate<WebAssemblyObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
  71. ADD_WINDOW_OBJECT_INTERFACES;
  72. }
  73. WindowObject::~WindowObject()
  74. {
  75. }
  76. void WindowObject::visit_edges(Visitor& visitor)
  77. {
  78. GlobalObject::visit_edges(visitor);
  79. for (auto& it : m_prototypes)
  80. visitor.visit(it.value);
  81. for (auto& it : m_constructors)
  82. visitor.visit(it.value);
  83. }
  84. Origin WindowObject::origin() const
  85. {
  86. return impl().document().origin();
  87. }
  88. static DOM::Window* impl_from(JS::VM& vm, JS::GlobalObject& global_object)
  89. {
  90. // Since this is a non built-in function we must treat it as non-strict mode
  91. // this means that a nullish this_value should be converted to the
  92. // global_object. Generally this does not matter as we try to convert the
  93. // this_value to a specific object type in the bindings. But since window is
  94. // the global object we make an exception here.
  95. // This allows calls like `setTimeout(f, 10)` to work.
  96. auto this_value = vm.this_value(global_object);
  97. if (this_value.is_nullish()) {
  98. this_value = global_object.value_of();
  99. }
  100. auto* this_object = this_value.to_object(global_object);
  101. VERIFY(this_object);
  102. if (StringView("WindowObject") != this_object->class_name()) {
  103. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "WindowObject");
  104. return nullptr;
  105. }
  106. return &static_cast<WindowObject*>(this_object)->impl();
  107. }
  108. JS_DEFINE_NATIVE_FUNCTION(WindowObject::alert)
  109. {
  110. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#simple-dialogs
  111. // Note: This method is defined using two overloads, instead of using an optional argument,
  112. // for historical reasons. The practical impact of this is that alert(undefined) is
  113. // treated as alert("undefined"), but alert() is treated as alert("").
  114. auto* impl = impl_from(vm, global_object);
  115. if (!impl)
  116. return {};
  117. String message = "";
  118. if (vm.argument_count()) {
  119. message = vm.argument(0).to_string(global_object);
  120. if (vm.exception())
  121. return {};
  122. }
  123. impl->alert(message);
  124. return JS::js_undefined();
  125. }
  126. JS_DEFINE_NATIVE_FUNCTION(WindowObject::confirm)
  127. {
  128. auto* impl = impl_from(vm, global_object);
  129. if (!impl)
  130. return {};
  131. String message = "";
  132. if (!vm.argument(0).is_undefined()) {
  133. message = vm.argument(0).to_string(global_object);
  134. if (vm.exception())
  135. return {};
  136. }
  137. return JS::Value(impl->confirm(message));
  138. }
  139. JS_DEFINE_NATIVE_FUNCTION(WindowObject::prompt)
  140. {
  141. auto* impl = impl_from(vm, global_object);
  142. if (!impl)
  143. return {};
  144. String message = "";
  145. String default_ = "";
  146. if (!vm.argument(0).is_undefined()) {
  147. message = vm.argument(0).to_string(global_object);
  148. if (vm.exception())
  149. return {};
  150. }
  151. if (!vm.argument(1).is_undefined()) {
  152. default_ = vm.argument(1).to_string(global_object);
  153. if (vm.exception())
  154. return {};
  155. }
  156. auto response = impl->prompt(message, default_);
  157. if (response.is_null())
  158. return JS::js_null();
  159. return JS::js_string(vm, response);
  160. }
  161. JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_interval)
  162. {
  163. auto* impl = impl_from(vm, global_object);
  164. if (!impl)
  165. return {};
  166. if (!vm.argument_count()) {
  167. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setInterval");
  168. return {};
  169. }
  170. auto* callback_object = vm.argument(0).to_object(global_object);
  171. if (!callback_object)
  172. return {};
  173. if (!callback_object->is_function()) {
  174. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
  175. return {};
  176. }
  177. i32 interval = 0;
  178. if (vm.argument_count() >= 2) {
  179. interval = vm.argument(1).to_i32(global_object);
  180. if (vm.exception())
  181. return {};
  182. if (interval < 0)
  183. interval = 0;
  184. }
  185. auto timer_id = impl->set_interval(*static_cast<JS::FunctionObject*>(callback_object), interval);
  186. return JS::Value(timer_id);
  187. }
  188. JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_timeout)
  189. {
  190. auto* impl = impl_from(vm, global_object);
  191. if (!impl)
  192. return {};
  193. if (!vm.argument_count()) {
  194. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setTimeout");
  195. return {};
  196. }
  197. auto* callback_object = vm.argument(0).to_object(global_object);
  198. if (!callback_object)
  199. return {};
  200. if (!callback_object->is_function()) {
  201. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
  202. return {};
  203. }
  204. i32 interval = 0;
  205. if (vm.argument_count() >= 2) {
  206. interval = vm.argument(1).to_i32(global_object);
  207. if (vm.exception())
  208. return {};
  209. if (interval < 0)
  210. interval = 0;
  211. }
  212. auto timer_id = impl->set_timeout(*static_cast<JS::FunctionObject*>(callback_object), interval);
  213. return JS::Value(timer_id);
  214. }
  215. JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_timeout)
  216. {
  217. auto* impl = impl_from(vm, global_object);
  218. if (!impl)
  219. return {};
  220. if (!vm.argument_count()) {
  221. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "clearTimeout");
  222. return {};
  223. }
  224. i32 timer_id = vm.argument(0).to_i32(global_object);
  225. if (vm.exception())
  226. return {};
  227. impl->clear_timeout(timer_id);
  228. return JS::js_undefined();
  229. }
  230. JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_interval)
  231. {
  232. auto* impl = impl_from(vm, global_object);
  233. if (!impl)
  234. return {};
  235. if (!vm.argument_count()) {
  236. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "clearInterval");
  237. return {};
  238. }
  239. i32 timer_id = vm.argument(0).to_i32(global_object);
  240. if (vm.exception())
  241. return {};
  242. impl->clear_timeout(timer_id);
  243. return JS::js_undefined();
  244. }
  245. JS_DEFINE_NATIVE_FUNCTION(WindowObject::request_animation_frame)
  246. {
  247. auto* impl = impl_from(vm, global_object);
  248. if (!impl)
  249. return {};
  250. if (!vm.argument_count()) {
  251. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "requestAnimationFrame");
  252. return {};
  253. }
  254. auto* callback_object = vm.argument(0).to_object(global_object);
  255. if (!callback_object)
  256. return {};
  257. if (!callback_object->is_function()) {
  258. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
  259. return {};
  260. }
  261. return JS::Value(impl->request_animation_frame(*static_cast<JS::FunctionObject*>(callback_object)));
  262. }
  263. JS_DEFINE_NATIVE_FUNCTION(WindowObject::cancel_animation_frame)
  264. {
  265. auto* impl = impl_from(vm, global_object);
  266. if (!impl)
  267. return {};
  268. if (!vm.argument_count()) {
  269. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "cancelAnimationFrame");
  270. return {};
  271. }
  272. auto id = vm.argument(0).to_i32(global_object);
  273. if (vm.exception())
  274. return {};
  275. impl->cancel_animation_frame(id);
  276. return JS::js_undefined();
  277. }
  278. JS_DEFINE_NATIVE_FUNCTION(WindowObject::atob)
  279. {
  280. auto* impl = impl_from(vm, global_object);
  281. if (!impl)
  282. return {};
  283. if (!vm.argument_count()) {
  284. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "atob");
  285. return {};
  286. }
  287. auto string = vm.argument(0).to_string(global_object);
  288. if (vm.exception())
  289. return {};
  290. auto decoded = decode_base64(StringView(string));
  291. // decode_base64() returns a byte string. LibJS uses UTF-8 for strings. Use Latin1Decoder to convert bytes 128-255 to UTF-8.
  292. auto decoder = TextCodec::decoder_for("windows-1252");
  293. VERIFY(decoder);
  294. return JS::js_string(vm, decoder->to_utf8(decoded));
  295. }
  296. JS_DEFINE_NATIVE_FUNCTION(WindowObject::btoa)
  297. {
  298. auto* impl = impl_from(vm, global_object);
  299. if (!impl)
  300. return {};
  301. if (!vm.argument_count()) {
  302. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "btoa");
  303. return {};
  304. }
  305. auto string = vm.argument(0).to_string(global_object);
  306. if (vm.exception())
  307. return {};
  308. Vector<u8> byte_string;
  309. byte_string.ensure_capacity(string.length());
  310. for (u32 code_point : Utf8View(string)) {
  311. if (code_point > 0xff) {
  312. vm.throw_exception<JS::InvalidCharacterError>(global_object, JS::ErrorType::NotAByteString, "btoa");
  313. return {};
  314. }
  315. byte_string.append(code_point);
  316. }
  317. auto encoded = encode_base64(byte_string.span());
  318. return JS::js_string(vm, move(encoded));
  319. }
  320. // https://html.spec.whatwg.org/multipage/browsers.html#dom-top
  321. JS_DEFINE_NATIVE_FUNCTION(WindowObject::top_getter)
  322. {
  323. auto* impl = impl_from(vm, global_object);
  324. if (!impl)
  325. return {};
  326. auto* this_browsing_context = impl->document().browsing_context();
  327. if (!this_browsing_context)
  328. return JS::js_null();
  329. VERIFY(this_browsing_context->top_level_browsing_context().document());
  330. auto& top_window = this_browsing_context->top_level_browsing_context().document()->window();
  331. return top_window.wrapper();
  332. }
  333. // https://html.spec.whatwg.org/multipage/browsers.html#dom-parent
  334. JS_DEFINE_NATIVE_FUNCTION(WindowObject::parent_getter)
  335. {
  336. auto* impl = impl_from(vm, global_object);
  337. if (!impl)
  338. return {};
  339. auto* this_browsing_context = impl->document().browsing_context();
  340. if (!this_browsing_context)
  341. return JS::js_null();
  342. if (this_browsing_context->parent()) {
  343. VERIFY(this_browsing_context->parent()->document());
  344. auto& parent_window = this_browsing_context->parent()->document()->window();
  345. return parent_window.wrapper();
  346. }
  347. VERIFY(this_browsing_context == &this_browsing_context->top_level_browsing_context());
  348. return impl->wrapper();
  349. }
  350. JS_DEFINE_NATIVE_FUNCTION(WindowObject::document_getter)
  351. {
  352. auto* impl = impl_from(vm, global_object);
  353. if (!impl)
  354. return {};
  355. return wrap(global_object, impl->document());
  356. }
  357. JS_DEFINE_NATIVE_FUNCTION(WindowObject::performance_getter)
  358. {
  359. auto* impl = impl_from(vm, global_object);
  360. if (!impl)
  361. return {};
  362. return wrap(global_object, impl->performance());
  363. }
  364. JS_DEFINE_NATIVE_FUNCTION(WindowObject::screen_getter)
  365. {
  366. auto* impl = impl_from(vm, global_object);
  367. if (!impl)
  368. return {};
  369. return wrap(global_object, impl->screen());
  370. }
  371. JS_DEFINE_NATIVE_FUNCTION(WindowObject::event_getter)
  372. {
  373. auto* impl = impl_from(vm, global_object);
  374. if (!impl)
  375. return {};
  376. if (!impl->current_event())
  377. return JS::js_undefined();
  378. return wrap(global_object, const_cast<DOM::Event&>(*impl->current_event()));
  379. }
  380. JS_DEFINE_NATIVE_FUNCTION(WindowObject::inner_width_getter)
  381. {
  382. auto* impl = impl_from(vm, global_object);
  383. if (!impl)
  384. return {};
  385. return JS::Value(impl->inner_width());
  386. }
  387. JS_DEFINE_NATIVE_FUNCTION(WindowObject::inner_height_getter)
  388. {
  389. auto* impl = impl_from(vm, global_object);
  390. if (!impl)
  391. return {};
  392. return JS::Value(impl->inner_height());
  393. }
  394. }