WindowObject.cpp 15 KB

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