WindowObject.cpp 14 KB

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