WindowObject.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. auto* this_object = vm.this_value(global_object).to_object(global_object);
  91. if (!this_object) {
  92. VERIFY_NOT_REACHED();
  93. return nullptr;
  94. }
  95. if (StringView("WindowObject") != this_object->class_name()) {
  96. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "WindowObject");
  97. return nullptr;
  98. }
  99. return &static_cast<WindowObject*>(this_object)->impl();
  100. }
  101. JS_DEFINE_NATIVE_FUNCTION(WindowObject::alert)
  102. {
  103. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#simple-dialogs
  104. // Note: This method is defined using two overloads, instead of using an optional argument,
  105. // for historical reasons. The practical impact of this is that alert(undefined) is
  106. // treated as alert("undefined"), but alert() is treated as alert("").
  107. auto* impl = impl_from(vm, global_object);
  108. if (!impl)
  109. return {};
  110. String message = "";
  111. if (vm.argument_count()) {
  112. message = vm.argument(0).to_string(global_object);
  113. if (vm.exception())
  114. return {};
  115. }
  116. impl->alert(message);
  117. return JS::js_undefined();
  118. }
  119. JS_DEFINE_NATIVE_FUNCTION(WindowObject::confirm)
  120. {
  121. auto* impl = impl_from(vm, global_object);
  122. if (!impl)
  123. return {};
  124. String message = "";
  125. if (!vm.argument(0).is_undefined()) {
  126. message = vm.argument(0).to_string(global_object);
  127. if (vm.exception())
  128. return {};
  129. }
  130. return JS::Value(impl->confirm(message));
  131. }
  132. JS_DEFINE_NATIVE_FUNCTION(WindowObject::prompt)
  133. {
  134. auto* impl = impl_from(vm, global_object);
  135. if (!impl)
  136. return {};
  137. String message = "";
  138. String default_ = "";
  139. if (!vm.argument(0).is_undefined()) {
  140. message = vm.argument(0).to_string(global_object);
  141. if (vm.exception())
  142. return {};
  143. }
  144. if (!vm.argument(1).is_undefined()) {
  145. default_ = vm.argument(1).to_string(global_object);
  146. if (vm.exception())
  147. return {};
  148. }
  149. auto response = impl->prompt(message, default_);
  150. if (response.is_null())
  151. return JS::js_null();
  152. return JS::js_string(vm, response);
  153. }
  154. JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_interval)
  155. {
  156. auto* impl = impl_from(vm, global_object);
  157. if (!impl)
  158. return {};
  159. if (!vm.argument_count()) {
  160. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setInterval");
  161. return {};
  162. }
  163. auto* callback_object = vm.argument(0).to_object(global_object);
  164. if (!callback_object)
  165. return {};
  166. if (!callback_object->is_function()) {
  167. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
  168. return {};
  169. }
  170. i32 interval = 0;
  171. if (vm.argument_count() >= 2) {
  172. interval = vm.argument(1).to_i32(global_object);
  173. if (vm.exception())
  174. return {};
  175. if (interval < 0)
  176. interval = 0;
  177. }
  178. auto timer_id = impl->set_interval(*static_cast<JS::FunctionObject*>(callback_object), interval);
  179. return JS::Value(timer_id);
  180. }
  181. JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_timeout)
  182. {
  183. auto* impl = impl_from(vm, global_object);
  184. if (!impl)
  185. return {};
  186. if (!vm.argument_count()) {
  187. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setTimeout");
  188. return {};
  189. }
  190. auto* callback_object = vm.argument(0).to_object(global_object);
  191. if (!callback_object)
  192. return {};
  193. if (!callback_object->is_function()) {
  194. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
  195. return {};
  196. }
  197. i32 interval = 0;
  198. if (vm.argument_count() >= 2) {
  199. interval = vm.argument(1).to_i32(global_object);
  200. if (vm.exception())
  201. return {};
  202. if (interval < 0)
  203. interval = 0;
  204. }
  205. auto timer_id = impl->set_timeout(*static_cast<JS::FunctionObject*>(callback_object), interval);
  206. return JS::Value(timer_id);
  207. }
  208. JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_timeout)
  209. {
  210. auto* impl = impl_from(vm, global_object);
  211. if (!impl)
  212. return {};
  213. if (!vm.argument_count()) {
  214. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "clearTimeout");
  215. return {};
  216. }
  217. i32 timer_id = vm.argument(0).to_i32(global_object);
  218. if (vm.exception())
  219. return {};
  220. impl->clear_timeout(timer_id);
  221. return JS::js_undefined();
  222. }
  223. JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_interval)
  224. {
  225. auto* impl = impl_from(vm, global_object);
  226. if (!impl)
  227. return {};
  228. if (!vm.argument_count()) {
  229. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "clearInterval");
  230. return {};
  231. }
  232. i32 timer_id = vm.argument(0).to_i32(global_object);
  233. if (vm.exception())
  234. return {};
  235. impl->clear_timeout(timer_id);
  236. return JS::js_undefined();
  237. }
  238. JS_DEFINE_NATIVE_FUNCTION(WindowObject::request_animation_frame)
  239. {
  240. auto* impl = impl_from(vm, global_object);
  241. if (!impl)
  242. return {};
  243. if (!vm.argument_count()) {
  244. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "requestAnimationFrame");
  245. return {};
  246. }
  247. auto* callback_object = vm.argument(0).to_object(global_object);
  248. if (!callback_object)
  249. return {};
  250. if (!callback_object->is_function()) {
  251. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
  252. return {};
  253. }
  254. return JS::Value(impl->request_animation_frame(*static_cast<JS::FunctionObject*>(callback_object)));
  255. }
  256. JS_DEFINE_NATIVE_FUNCTION(WindowObject::cancel_animation_frame)
  257. {
  258. auto* impl = impl_from(vm, global_object);
  259. if (!impl)
  260. return {};
  261. if (!vm.argument_count()) {
  262. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "cancelAnimationFrame");
  263. return {};
  264. }
  265. auto id = vm.argument(0).to_i32(global_object);
  266. if (vm.exception())
  267. return {};
  268. impl->cancel_animation_frame(id);
  269. return JS::js_undefined();
  270. }
  271. JS_DEFINE_NATIVE_FUNCTION(WindowObject::atob)
  272. {
  273. auto* impl = impl_from(vm, global_object);
  274. if (!impl)
  275. return {};
  276. if (!vm.argument_count()) {
  277. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "atob");
  278. return {};
  279. }
  280. auto string = vm.argument(0).to_string(global_object);
  281. if (vm.exception())
  282. return {};
  283. auto decoded = decode_base64(StringView(string));
  284. // decode_base64() returns a byte string. LibJS uses UTF-8 for strings. Use Latin1Decoder to convert bytes 128-255 to UTF-8.
  285. auto decoder = TextCodec::decoder_for("windows-1252");
  286. VERIFY(decoder);
  287. return JS::js_string(vm, decoder->to_utf8(decoded));
  288. }
  289. JS_DEFINE_NATIVE_FUNCTION(WindowObject::btoa)
  290. {
  291. auto* impl = impl_from(vm, global_object);
  292. if (!impl)
  293. return {};
  294. if (!vm.argument_count()) {
  295. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "btoa");
  296. return {};
  297. }
  298. auto string = vm.argument(0).to_string(global_object);
  299. if (vm.exception())
  300. return {};
  301. Vector<u8> byte_string;
  302. byte_string.ensure_capacity(string.length());
  303. for (u32 code_point : Utf8View(string)) {
  304. if (code_point > 0xff) {
  305. vm.throw_exception<JS::InvalidCharacterError>(global_object, JS::ErrorType::NotAByteString, "btoa");
  306. return {};
  307. }
  308. byte_string.append(code_point);
  309. }
  310. auto encoded = encode_base64(byte_string.span());
  311. return JS::js_string(vm, move(encoded));
  312. }
  313. // https://html.spec.whatwg.org/multipage/browsers.html#dom-top
  314. JS_DEFINE_NATIVE_FUNCTION(WindowObject::top_getter)
  315. {
  316. auto* impl = impl_from(vm, global_object);
  317. if (!impl)
  318. return {};
  319. auto* this_browsing_context = impl->document().browsing_context();
  320. if (!this_browsing_context)
  321. return JS::js_null();
  322. VERIFY(this_browsing_context->top_level_browsing_context().document());
  323. auto& top_window = this_browsing_context->top_level_browsing_context().document()->window();
  324. return top_window.wrapper();
  325. }
  326. // https://html.spec.whatwg.org/multipage/browsers.html#dom-parent
  327. JS_DEFINE_NATIVE_FUNCTION(WindowObject::parent_getter)
  328. {
  329. auto* impl = impl_from(vm, global_object);
  330. if (!impl)
  331. return {};
  332. auto* this_browsing_context = impl->document().browsing_context();
  333. if (!this_browsing_context)
  334. return JS::js_null();
  335. if (this_browsing_context->parent()) {
  336. VERIFY(this_browsing_context->parent()->document());
  337. auto& parent_window = this_browsing_context->parent()->document()->window();
  338. return parent_window.wrapper();
  339. }
  340. VERIFY(this_browsing_context == &this_browsing_context->top_level_browsing_context());
  341. return impl->wrapper();
  342. }
  343. JS_DEFINE_NATIVE_FUNCTION(WindowObject::document_getter)
  344. {
  345. auto* impl = impl_from(vm, global_object);
  346. if (!impl)
  347. return {};
  348. return wrap(global_object, impl->document());
  349. }
  350. JS_DEFINE_NATIVE_FUNCTION(WindowObject::performance_getter)
  351. {
  352. auto* impl = impl_from(vm, global_object);
  353. if (!impl)
  354. return {};
  355. return wrap(global_object, impl->performance());
  356. }
  357. JS_DEFINE_NATIVE_FUNCTION(WindowObject::screen_getter)
  358. {
  359. auto* impl = impl_from(vm, global_object);
  360. if (!impl)
  361. return {};
  362. return wrap(global_object, impl->screen());
  363. }
  364. JS_DEFINE_NATIVE_FUNCTION(WindowObject::event_getter)
  365. {
  366. auto* impl = impl_from(vm, global_object);
  367. if (!impl)
  368. return {};
  369. if (!impl->current_event())
  370. return JS::js_undefined();
  371. return wrap(global_object, const_cast<DOM::Event&>(*impl->current_event()));
  372. }
  373. JS_DEFINE_NATIVE_FUNCTION(WindowObject::inner_width_getter)
  374. {
  375. auto* impl = impl_from(vm, global_object);
  376. if (!impl)
  377. return {};
  378. return JS::Value(impl->inner_width());
  379. }
  380. JS_DEFINE_NATIVE_FUNCTION(WindowObject::inner_height_getter)
  381. {
  382. auto* impl = impl_from(vm, global_object);
  383. if (!impl)
  384. return {};
  385. return JS::Value(impl->inner_height());
  386. }
  387. }