WindowObject.cpp 14 KB

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