WindowObject.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Base64.h>
  8. #include <AK/String.h>
  9. #include <AK/Utf8View.h>
  10. #include <LibJS/Runtime/Completion.h>
  11. #include <LibJS/Runtime/Error.h>
  12. #include <LibJS/Runtime/FunctionObject.h>
  13. #include <LibJS/Runtime/Shape.h>
  14. #include <LibTextCodec/Decoder.h>
  15. #include <LibWeb/Bindings/CSSNamespace.h>
  16. #include <LibWeb/Bindings/CSSStyleDeclarationWrapper.h>
  17. #include <LibWeb/Bindings/CryptoWrapper.h>
  18. #include <LibWeb/Bindings/DocumentWrapper.h>
  19. #include <LibWeb/Bindings/ElementWrapper.h>
  20. #include <LibWeb/Bindings/EventTargetConstructor.h>
  21. #include <LibWeb/Bindings/EventTargetPrototype.h>
  22. #include <LibWeb/Bindings/EventWrapper.h>
  23. #include <LibWeb/Bindings/EventWrapperFactory.h>
  24. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  25. #include <LibWeb/Bindings/HistoryWrapper.h>
  26. #include <LibWeb/Bindings/LocationObject.h>
  27. #include <LibWeb/Bindings/MediaQueryListWrapper.h>
  28. #include <LibWeb/Bindings/NavigatorObject.h>
  29. #include <LibWeb/Bindings/NodeWrapperFactory.h>
  30. #include <LibWeb/Bindings/PerformanceWrapper.h>
  31. #include <LibWeb/Bindings/Replaceable.h>
  32. #include <LibWeb/Bindings/ScreenWrapper.h>
  33. #include <LibWeb/Bindings/SelectionWrapper.h>
  34. #include <LibWeb/Bindings/StorageWrapper.h>
  35. #include <LibWeb/Bindings/WindowObject.h>
  36. #include <LibWeb/Bindings/WindowObjectHelper.h>
  37. #include <LibWeb/Bindings/WindowPrototype.h>
  38. #include <LibWeb/Crypto/Crypto.h>
  39. #include <LibWeb/DOM/Document.h>
  40. #include <LibWeb/DOM/Event.h>
  41. #include <LibWeb/HTML/BrowsingContext.h>
  42. #include <LibWeb/HTML/EventHandler.h>
  43. #include <LibWeb/HTML/Origin.h>
  44. #include <LibWeb/HTML/Scripting/Environments.h>
  45. #include <LibWeb/HTML/Storage.h>
  46. #include <LibWeb/HTML/Window.h>
  47. #include <LibWeb/Page/Page.h>
  48. #include <LibWeb/WebAssembly/WebAssemblyObject.h>
  49. namespace Web::Bindings {
  50. WindowObject::WindowObject(JS::Realm& realm, HTML::Window& impl)
  51. : GlobalObject(realm)
  52. , m_impl(impl)
  53. {
  54. impl.set_wrapper({}, *this);
  55. }
  56. void WindowObject::initialize_global_object()
  57. {
  58. Base::initialize_global_object();
  59. Object::set_prototype(&ensure_web_prototype<WindowPrototype>("Window"));
  60. auto& realm = *associated_realm();
  61. // FIXME: These should be native accessors, not properties
  62. define_direct_property("window", this, JS::Attribute::Enumerable);
  63. define_direct_property("frames", this, JS::Attribute::Enumerable);
  64. define_direct_property("self", this, JS::Attribute::Enumerable);
  65. define_native_accessor("top", top_getter, nullptr, JS::Attribute::Enumerable);
  66. define_native_accessor("parent", parent_getter, {}, JS::Attribute::Enumerable);
  67. define_native_accessor("document", document_getter, {}, JS::Attribute::Enumerable);
  68. define_native_accessor("name", name_getter, name_setter, JS::Attribute::Enumerable);
  69. define_native_accessor("history", history_getter, {}, JS::Attribute::Enumerable);
  70. define_native_accessor("performance", performance_getter, performance_setter, JS::Attribute::Enumerable | JS::Attribute::Configurable);
  71. define_native_accessor("crypto", crypto_getter, {}, JS::Attribute::Enumerable);
  72. define_native_accessor("screen", screen_getter, {}, JS::Attribute::Enumerable);
  73. define_native_accessor("innerWidth", inner_width_getter, {}, JS::Attribute::Enumerable);
  74. define_native_accessor("innerHeight", inner_height_getter, {}, JS::Attribute::Enumerable);
  75. define_native_accessor("devicePixelRatio", device_pixel_ratio_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
  76. u8 attr = JS::Attribute::Writable | JS::Attribute::Enumerable | JS::Attribute::Configurable;
  77. define_native_function("alert", alert, 0, attr);
  78. define_native_function("confirm", confirm, 0, attr);
  79. define_native_function("prompt", prompt, 0, attr);
  80. define_native_function("setInterval", set_interval, 1, attr);
  81. define_native_function("setTimeout", set_timeout, 1, attr);
  82. define_native_function("clearInterval", clear_interval, 1, attr);
  83. define_native_function("clearTimeout", clear_timeout, 1, attr);
  84. define_native_function("requestAnimationFrame", request_animation_frame, 1, attr);
  85. define_native_function("cancelAnimationFrame", cancel_animation_frame, 1, attr);
  86. define_native_function("atob", atob, 1, attr);
  87. define_native_function("btoa", btoa, 1, attr);
  88. define_native_function("queueMicrotask", queue_microtask, 1, attr);
  89. define_native_function("requestIdleCallback", request_idle_callback, 1, attr);
  90. define_native_function("cancelIdleCallback", cancel_idle_callback, 1, attr);
  91. define_native_function("getComputedStyle", get_computed_style, 1, attr);
  92. define_native_function("matchMedia", match_media, 1, attr);
  93. define_native_function("getSelection", get_selection, 0, attr);
  94. define_native_function("postMessage", post_message, 1, attr);
  95. // FIXME: These properties should be [Replaceable] according to the spec, but [Writable+Configurable] is the closest we have.
  96. define_native_accessor("scrollX", scroll_x_getter, {}, attr);
  97. define_native_accessor("pageXOffset", scroll_x_getter, {}, attr);
  98. define_native_accessor("scrollY", scroll_y_getter, {}, attr);
  99. define_native_accessor("pageYOffset", scroll_y_getter, {}, attr);
  100. define_native_function("scroll", scroll, 2, attr);
  101. define_native_function("scrollTo", scroll, 2, attr);
  102. define_native_function("scrollBy", scroll_by, 2, attr);
  103. define_native_accessor("screenX", screen_x_getter, {}, attr);
  104. define_native_accessor("screenY", screen_y_getter, {}, attr);
  105. define_native_accessor("screenLeft", screen_left_getter, {}, attr);
  106. define_native_accessor("screenTop", screen_top_getter, {}, attr);
  107. define_direct_property("CSS", heap().allocate<CSSNamespace>(realm, realm), 0);
  108. define_native_accessor("localStorage", local_storage_getter, {}, attr);
  109. define_native_accessor("sessionStorage", session_storage_getter, {}, attr);
  110. define_native_accessor("origin", origin_getter, {}, attr);
  111. // Legacy
  112. define_native_accessor("event", event_getter, event_setter, JS::Attribute::Enumerable);
  113. m_location_object = heap().allocate<LocationObject>(realm, realm);
  114. auto* m_navigator_object = heap().allocate<NavigatorObject>(realm, realm);
  115. define_direct_property("navigator", m_navigator_object, JS::Attribute::Enumerable | JS::Attribute::Configurable);
  116. define_direct_property("clientInformation", m_navigator_object, JS::Attribute::Enumerable | JS::Attribute::Configurable);
  117. // NOTE: location is marked as [LegacyUnforgeable], meaning it isn't configurable.
  118. define_native_accessor("location", location_getter, location_setter, JS::Attribute::Enumerable);
  119. // WebAssembly "namespace"
  120. define_direct_property("WebAssembly", heap().allocate<WebAssemblyObject>(realm, realm), JS::Attribute::Enumerable | JS::Attribute::Configurable);
  121. // HTML::GlobalEventHandlers and HTML::WindowEventHandlers
  122. #define __ENUMERATE(attribute, event_name) \
  123. define_native_accessor(#attribute, attribute##_getter, attribute##_setter, attr);
  124. ENUMERATE_GLOBAL_EVENT_HANDLERS(__ENUMERATE);
  125. ENUMERATE_WINDOW_EVENT_HANDLERS(__ENUMERATE);
  126. #undef __ENUMERATE
  127. ADD_WINDOW_OBJECT_INTERFACES;
  128. }
  129. void WindowObject::visit_edges(Visitor& visitor)
  130. {
  131. GlobalObject::visit_edges(visitor);
  132. visitor.visit(m_location_object);
  133. for (auto& it : m_prototypes)
  134. visitor.visit(it.value);
  135. for (auto& it : m_constructors)
  136. visitor.visit(it.value);
  137. }
  138. HTML::Origin WindowObject::origin() const
  139. {
  140. return impl().associated_document().origin();
  141. }
  142. // https://webidl.spec.whatwg.org/#platform-object-setprototypeof
  143. JS::ThrowCompletionOr<bool> WindowObject::internal_set_prototype_of(JS::Object* prototype)
  144. {
  145. // 1. Return ? SetImmutablePrototype(O, V).
  146. return set_immutable_prototype(prototype);
  147. }
  148. static JS::ThrowCompletionOr<HTML::Window*> impl_from(JS::VM& vm, JS::GlobalObject& global_object)
  149. {
  150. // Since this is a non built-in function we must treat it as non-strict mode
  151. // this means that a nullish this_value should be converted to the
  152. // global_object. Generally this does not matter as we try to convert the
  153. // this_value to a specific object type in the bindings. But since window is
  154. // the global object we make an exception here.
  155. // This allows calls like `setTimeout(f, 10)` to work.
  156. auto this_value = vm.this_value();
  157. if (this_value.is_nullish())
  158. this_value = &global_object;
  159. auto* this_object = MUST(this_value.to_object(vm));
  160. if (!is<WindowObject>(*this_object))
  161. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WindowObject");
  162. return &static_cast<WindowObject*>(this_object)->impl();
  163. }
  164. JS_DEFINE_NATIVE_FUNCTION(WindowObject::alert)
  165. {
  166. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#simple-dialogs
  167. // Note: This method is defined using two overloads, instead of using an optional argument,
  168. // for historical reasons. The practical impact of this is that alert(undefined) is
  169. // treated as alert("undefined"), but alert() is treated as alert("").
  170. auto* impl = TRY(impl_from(vm, global_object));
  171. String message = "";
  172. if (vm.argument_count())
  173. message = TRY(vm.argument(0).to_string(vm));
  174. impl->alert(message);
  175. return JS::js_undefined();
  176. }
  177. JS_DEFINE_NATIVE_FUNCTION(WindowObject::confirm)
  178. {
  179. auto* impl = TRY(impl_from(vm, global_object));
  180. String message = "";
  181. if (!vm.argument(0).is_undefined())
  182. message = TRY(vm.argument(0).to_string(vm));
  183. return JS::Value(impl->confirm(message));
  184. }
  185. JS_DEFINE_NATIVE_FUNCTION(WindowObject::prompt)
  186. {
  187. auto* impl = TRY(impl_from(vm, global_object));
  188. String message = "";
  189. String default_ = "";
  190. if (!vm.argument(0).is_undefined())
  191. message = TRY(vm.argument(0).to_string(vm));
  192. if (!vm.argument(1).is_undefined())
  193. default_ = TRY(vm.argument(1).to_string(vm));
  194. auto response = impl->prompt(message, default_);
  195. if (response.is_null())
  196. return JS::js_null();
  197. return JS::js_string(vm, response);
  198. }
  199. static JS::ThrowCompletionOr<TimerHandler> make_timer_handler(JS::GlobalObject& global_object, JS::Value handler)
  200. {
  201. auto& vm = global_object.vm();
  202. if (handler.is_function())
  203. return Bindings::CallbackType(JS::make_handle<JS::Object>(handler.as_function()), HTML::incumbent_settings_object());
  204. return TRY(handler.to_string(vm));
  205. }
  206. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-settimeout
  207. JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_timeout)
  208. {
  209. auto* impl = TRY(impl_from(vm, global_object));
  210. if (!vm.argument_count())
  211. return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountAtLeastOne, "setTimeout");
  212. auto handler = TRY(make_timer_handler(global_object, vm.argument(0)));
  213. i32 timeout = 0;
  214. if (vm.argument_count() >= 2)
  215. timeout = TRY(vm.argument(1).to_i32(vm));
  216. JS::MarkedVector<JS::Value> arguments { vm.heap() };
  217. for (size_t i = 2; i < vm.argument_count(); ++i)
  218. arguments.append(vm.argument(i));
  219. auto id = impl->set_timeout(move(handler), timeout, move(arguments));
  220. return JS::Value(id);
  221. }
  222. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-setinterval
  223. JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_interval)
  224. {
  225. auto* impl = TRY(impl_from(vm, global_object));
  226. if (!vm.argument_count())
  227. return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountAtLeastOne, "setInterval");
  228. auto handler = TRY(make_timer_handler(global_object, vm.argument(0)));
  229. i32 timeout = 0;
  230. if (vm.argument_count() >= 2)
  231. timeout = TRY(vm.argument(1).to_i32(vm));
  232. JS::MarkedVector<JS::Value> arguments { vm.heap() };
  233. for (size_t i = 2; i < vm.argument_count(); ++i)
  234. arguments.append(vm.argument(i));
  235. auto id = impl->set_interval(move(handler), timeout, move(arguments));
  236. return JS::Value(id);
  237. }
  238. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-cleartimeout
  239. JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_timeout)
  240. {
  241. auto* impl = TRY(impl_from(vm, global_object));
  242. i32 id = 0;
  243. if (vm.argument_count())
  244. id = TRY(vm.argument(0).to_i32(vm));
  245. impl->clear_timeout(id);
  246. return JS::js_undefined();
  247. }
  248. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-clearinterval
  249. JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_interval)
  250. {
  251. auto* impl = TRY(impl_from(vm, global_object));
  252. i32 id = 0;
  253. if (vm.argument_count())
  254. id = TRY(vm.argument(0).to_i32(vm));
  255. impl->clear_interval(id);
  256. return JS::js_undefined();
  257. }
  258. JS_DEFINE_NATIVE_FUNCTION(WindowObject::request_animation_frame)
  259. {
  260. auto* impl = TRY(impl_from(vm, global_object));
  261. if (!vm.argument_count())
  262. return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountOne, "requestAnimationFrame");
  263. auto* callback_object = TRY(vm.argument(0).to_object(vm));
  264. if (!callback_object->is_function())
  265. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAFunctionNoParam);
  266. NonnullOwnPtr<Bindings::CallbackType> callback = adopt_own(*new Bindings::CallbackType(JS::make_handle(callback_object), HTML::incumbent_settings_object()));
  267. return JS::Value(impl->request_animation_frame(move(callback)));
  268. }
  269. JS_DEFINE_NATIVE_FUNCTION(WindowObject::cancel_animation_frame)
  270. {
  271. auto* impl = TRY(impl_from(vm, global_object));
  272. if (!vm.argument_count())
  273. return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountOne, "cancelAnimationFrame");
  274. auto id = TRY(vm.argument(0).to_i32(vm));
  275. impl->cancel_animation_frame(id);
  276. return JS::js_undefined();
  277. }
  278. JS_DEFINE_NATIVE_FUNCTION(WindowObject::queue_microtask)
  279. {
  280. auto* impl = TRY(impl_from(vm, global_object));
  281. if (!vm.argument_count())
  282. return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountAtLeastOne, "queueMicrotask");
  283. auto* callback_object = TRY(vm.argument(0).to_object(vm));
  284. if (!callback_object->is_function())
  285. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAFunctionNoParam);
  286. auto callback = adopt_own(*new Bindings::CallbackType(JS::make_handle(callback_object), HTML::incumbent_settings_object()));
  287. impl->queue_microtask(move(callback));
  288. return JS::js_undefined();
  289. }
  290. JS_DEFINE_NATIVE_FUNCTION(WindowObject::request_idle_callback)
  291. {
  292. auto* impl = TRY(impl_from(vm, global_object));
  293. if (!vm.argument_count())
  294. return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountAtLeastOne, "requestIdleCallback");
  295. auto* callback_object = TRY(vm.argument(0).to_object(vm));
  296. if (!callback_object->is_function())
  297. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAFunctionNoParam);
  298. // FIXME: accept options object
  299. auto callback = adopt_own(*new Bindings::CallbackType(JS::make_handle(callback_object), HTML::incumbent_settings_object()));
  300. return JS::Value(impl->request_idle_callback(move(callback)));
  301. }
  302. JS_DEFINE_NATIVE_FUNCTION(WindowObject::cancel_idle_callback)
  303. {
  304. auto* impl = TRY(impl_from(vm, global_object));
  305. if (!vm.argument_count())
  306. return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountOne, "cancelIdleCallback");
  307. auto id = TRY(vm.argument(0).to_u32(vm));
  308. impl->cancel_idle_callback(id);
  309. return JS::js_undefined();
  310. }
  311. JS_DEFINE_NATIVE_FUNCTION(WindowObject::atob)
  312. {
  313. if (!vm.argument_count())
  314. return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountOne, "atob");
  315. auto string = TRY(vm.argument(0).to_string(vm));
  316. auto decoded = decode_base64(StringView(string));
  317. if (decoded.is_error())
  318. return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidFormat, "Base64");
  319. // decode_base64() returns a byte string. LibJS uses UTF-8 for strings. Use Latin1Decoder to convert bytes 128-255 to UTF-8.
  320. auto decoder = TextCodec::decoder_for("windows-1252");
  321. VERIFY(decoder);
  322. return JS::js_string(vm, decoder->to_utf8(decoded.value()));
  323. }
  324. JS_DEFINE_NATIVE_FUNCTION(WindowObject::btoa)
  325. {
  326. if (!vm.argument_count())
  327. return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountOne, "btoa");
  328. auto string = TRY(vm.argument(0).to_string(vm));
  329. Vector<u8> byte_string;
  330. byte_string.ensure_capacity(string.length());
  331. for (u32 code_point : Utf8View(string)) {
  332. if (code_point > 0xff)
  333. return vm.throw_completion<JS::InvalidCharacterError>(JS::ErrorType::NotAByteString, "btoa");
  334. byte_string.append(code_point);
  335. }
  336. auto encoded = encode_base64(byte_string.span());
  337. return JS::js_string(vm, move(encoded));
  338. }
  339. // https://html.spec.whatwg.org/multipage/browsers.html#dom-top
  340. JS_DEFINE_NATIVE_FUNCTION(WindowObject::top_getter)
  341. {
  342. auto* impl = TRY(impl_from(vm, global_object));
  343. auto* this_browsing_context = impl->associated_document().browsing_context();
  344. if (!this_browsing_context)
  345. return JS::js_null();
  346. VERIFY(this_browsing_context->top_level_browsing_context().active_document());
  347. auto& top_window = this_browsing_context->top_level_browsing_context().active_document()->window();
  348. return top_window.wrapper();
  349. }
  350. JS_DEFINE_NATIVE_FUNCTION(WindowObject::parent_getter)
  351. {
  352. auto* impl = TRY(impl_from(vm, global_object));
  353. auto* parent = impl->parent();
  354. if (!parent)
  355. return JS::js_null();
  356. return parent->wrapper();
  357. }
  358. JS_DEFINE_NATIVE_FUNCTION(WindowObject::document_getter)
  359. {
  360. auto& realm = *vm.current_realm();
  361. auto* impl = TRY(impl_from(vm, global_object));
  362. return wrap(realm, impl->associated_document());
  363. }
  364. JS_DEFINE_NATIVE_FUNCTION(WindowObject::performance_getter)
  365. {
  366. auto& realm = *vm.current_realm();
  367. auto* impl = TRY(impl_from(vm, global_object));
  368. return wrap(realm, impl->performance());
  369. }
  370. JS_DEFINE_NATIVE_FUNCTION(WindowObject::performance_setter)
  371. {
  372. // https://webidl.spec.whatwg.org/#dfn-attribute-setter
  373. // 4.1. If no arguments were passed, then throw a TypeError.
  374. if (vm.argument_count() == 0)
  375. return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountOne, "set performance");
  376. auto* impl = TRY(impl_from(vm, global_object));
  377. // 5. If attribute is declared with the [Replaceable] extended attribute, then:
  378. // 1. Perform ? CreateDataProperty(esValue, id, V).
  379. VERIFY(impl->wrapper());
  380. TRY(impl->wrapper()->create_data_property("performance", vm.argument(0)));
  381. // 2. Return undefined.
  382. return JS::js_undefined();
  383. }
  384. JS_DEFINE_NATIVE_FUNCTION(WindowObject::screen_getter)
  385. {
  386. auto& realm = *vm.current_realm();
  387. auto* impl = TRY(impl_from(vm, global_object));
  388. return wrap(realm, impl->screen());
  389. }
  390. JS_DEFINE_NATIVE_FUNCTION(WindowObject::event_getter)
  391. {
  392. auto& realm = *vm.current_realm();
  393. auto* impl = TRY(impl_from(vm, global_object));
  394. if (!impl->current_event())
  395. return JS::js_undefined();
  396. return wrap(realm, const_cast<DOM::Event&>(*impl->current_event()));
  397. }
  398. JS_DEFINE_NATIVE_FUNCTION(WindowObject::event_setter)
  399. {
  400. REPLACEABLE_PROPERTY_SETTER(WindowObject, event);
  401. }
  402. JS_DEFINE_NATIVE_FUNCTION(WindowObject::location_getter)
  403. {
  404. auto* impl = TRY(impl_from(vm, global_object));
  405. VERIFY(impl->wrapper());
  406. return impl->wrapper()->m_location_object;
  407. }
  408. JS_DEFINE_NATIVE_FUNCTION(WindowObject::location_setter)
  409. {
  410. auto* impl = TRY(impl_from(vm, global_object));
  411. VERIFY(impl->wrapper());
  412. TRY(impl->wrapper()->m_location_object->set(JS::PropertyKey("href"), vm.argument(0), JS::Object::ShouldThrowExceptions::Yes));
  413. return JS::js_undefined();
  414. }
  415. JS_DEFINE_NATIVE_FUNCTION(WindowObject::crypto_getter)
  416. {
  417. auto& realm = *vm.current_realm();
  418. auto* impl = TRY(impl_from(vm, global_object));
  419. return wrap(realm, impl->crypto());
  420. }
  421. JS_DEFINE_NATIVE_FUNCTION(WindowObject::inner_width_getter)
  422. {
  423. auto* impl = TRY(impl_from(vm, global_object));
  424. return JS::Value(impl->inner_width());
  425. }
  426. JS_DEFINE_NATIVE_FUNCTION(WindowObject::inner_height_getter)
  427. {
  428. auto* impl = TRY(impl_from(vm, global_object));
  429. return JS::Value(impl->inner_height());
  430. }
  431. JS_DEFINE_NATIVE_FUNCTION(WindowObject::device_pixel_ratio_getter)
  432. {
  433. auto* impl = TRY(impl_from(vm, global_object));
  434. return JS::Value(impl->device_pixel_ratio());
  435. }
  436. JS_DEFINE_NATIVE_FUNCTION(WindowObject::get_computed_style)
  437. {
  438. auto& realm = *vm.current_realm();
  439. auto* impl = TRY(impl_from(vm, global_object));
  440. auto* object = TRY(vm.argument(0).to_object(vm));
  441. if (!is<ElementWrapper>(object))
  442. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "DOM element");
  443. return wrap(realm, impl->get_computed_style(static_cast<ElementWrapper*>(object)->impl()));
  444. }
  445. JS_DEFINE_NATIVE_FUNCTION(WindowObject::get_selection)
  446. {
  447. auto& realm = *vm.current_realm();
  448. auto* impl = TRY(impl_from(vm, global_object));
  449. auto* selection = impl->get_selection();
  450. if (!selection)
  451. return JS::js_null();
  452. return wrap(realm, *selection);
  453. }
  454. JS_DEFINE_NATIVE_FUNCTION(WindowObject::match_media)
  455. {
  456. auto& realm = *vm.current_realm();
  457. auto* impl = TRY(impl_from(vm, global_object));
  458. auto media = TRY(vm.argument(0).to_string(vm));
  459. return wrap(realm, impl->match_media(move(media)));
  460. }
  461. // https://www.w3.org/TR/cssom-view/#dom-window-scrollx
  462. JS_DEFINE_NATIVE_FUNCTION(WindowObject::scroll_x_getter)
  463. {
  464. auto* impl = TRY(impl_from(vm, global_object));
  465. return JS::Value(impl->scroll_x());
  466. }
  467. // https://www.w3.org/TR/cssom-view/#dom-window-scrolly
  468. JS_DEFINE_NATIVE_FUNCTION(WindowObject::scroll_y_getter)
  469. {
  470. auto* impl = TRY(impl_from(vm, global_object));
  471. return JS::Value(impl->scroll_y());
  472. }
  473. enum class ScrollBehavior {
  474. Auto,
  475. Smooth
  476. };
  477. // https://www.w3.org/TR/cssom-view/#perform-a-scroll
  478. static void perform_a_scroll(Page& page, double x, double y, ScrollBehavior)
  479. {
  480. // FIXME: Stop any existing smooth-scrolls
  481. // FIXME: Implement smooth-scroll
  482. page.client().page_did_request_scroll_to({ x, y });
  483. }
  484. // https://www.w3.org/TR/cssom-view/#dom-window-scroll
  485. JS_DEFINE_NATIVE_FUNCTION(WindowObject::scroll)
  486. {
  487. auto* impl = TRY(impl_from(vm, global_object));
  488. if (!impl->page())
  489. return JS::js_undefined();
  490. auto& page = *impl->page();
  491. auto viewport_rect = page.top_level_browsing_context().viewport_rect();
  492. auto x_value = JS::Value(viewport_rect.x());
  493. auto y_value = JS::Value(viewport_rect.y());
  494. String behavior_string = "auto";
  495. if (vm.argument_count() == 1) {
  496. auto* options = TRY(vm.argument(0).to_object(vm));
  497. auto left = TRY(options->get("left"));
  498. if (!left.is_undefined())
  499. x_value = left;
  500. auto top = TRY(options->get("top"));
  501. if (!top.is_undefined())
  502. y_value = top;
  503. auto behavior_string_value = TRY(options->get("behavior"));
  504. if (!behavior_string_value.is_undefined())
  505. behavior_string = TRY(behavior_string_value.to_string(vm));
  506. if (behavior_string != "smooth" && behavior_string != "auto")
  507. return vm.throw_completion<JS::TypeError>("Behavior is not one of 'smooth' or 'auto'");
  508. } else if (vm.argument_count() >= 2) {
  509. // We ignore arguments 2+ in line with behavior of Chrome and Firefox
  510. x_value = vm.argument(0);
  511. y_value = vm.argument(1);
  512. }
  513. ScrollBehavior behavior = (behavior_string == "smooth") ? ScrollBehavior::Smooth : ScrollBehavior::Auto;
  514. double x = TRY(x_value.to_double(vm));
  515. x = JS::Value(x).is_finite_number() ? x : 0.0;
  516. double y = TRY(y_value.to_double(vm));
  517. y = JS::Value(y).is_finite_number() ? y : 0.0;
  518. // FIXME: Are we calculating the viewport in the way this function expects?
  519. // FIXME: Handle overflow-directions other than top-left to bottom-right
  520. perform_a_scroll(page, x, y, behavior);
  521. return JS::js_undefined();
  522. }
  523. // https://www.w3.org/TR/cssom-view/#dom-window-scrollby
  524. JS_DEFINE_NATIVE_FUNCTION(WindowObject::scroll_by)
  525. {
  526. auto& realm = *global_object.associated_realm();
  527. auto* impl = TRY(impl_from(vm, global_object));
  528. if (!impl->page())
  529. return JS::js_undefined();
  530. auto& page = *impl->page();
  531. JS::Object* options = nullptr;
  532. if (vm.argument_count() == 0) {
  533. options = JS::Object::create(realm, nullptr);
  534. } else if (vm.argument_count() == 1) {
  535. options = TRY(vm.argument(0).to_object(vm));
  536. } else if (vm.argument_count() >= 2) {
  537. // We ignore arguments 2+ in line with behavior of Chrome and Firefox
  538. options = JS::Object::create(realm, nullptr);
  539. MUST(options->set("left", vm.argument(0), ShouldThrowExceptions::No));
  540. MUST(options->set("top", vm.argument(1), ShouldThrowExceptions::No));
  541. MUST(options->set("behavior", JS::js_string(vm, "auto"), ShouldThrowExceptions::No));
  542. }
  543. auto left_value = TRY(options->get("left"));
  544. auto left = TRY(left_value.to_double(vm));
  545. auto top_value = TRY(options->get("top"));
  546. auto top = TRY(top_value.to_double(vm));
  547. left = JS::Value(left).is_finite_number() ? left : 0.0;
  548. top = JS::Value(top).is_finite_number() ? top : 0.0;
  549. auto current_scroll_position = page.top_level_browsing_context().viewport_scroll_offset();
  550. left = left + current_scroll_position.x();
  551. top = top + current_scroll_position.y();
  552. auto behavior_string_value = TRY(options->get("behavior"));
  553. auto behavior_string = behavior_string_value.is_undefined() ? "auto" : TRY(behavior_string_value.to_string(vm));
  554. if (behavior_string != "smooth" && behavior_string != "auto")
  555. return vm.throw_completion<JS::TypeError>("Behavior is not one of 'smooth' or 'auto'");
  556. ScrollBehavior behavior = (behavior_string == "smooth") ? ScrollBehavior::Smooth : ScrollBehavior::Auto;
  557. // FIXME: Spec wants us to call scroll(options) here.
  558. // The only difference is that would invoke the viewport calculations that scroll()
  559. // is not actually doing yet, so this is the same for now.
  560. perform_a_scroll(page, left, top, behavior);
  561. return JS::js_undefined();
  562. }
  563. JS_DEFINE_NATIVE_FUNCTION(WindowObject::history_getter)
  564. {
  565. auto& realm = *vm.current_realm();
  566. auto* impl = TRY(impl_from(vm, global_object));
  567. return wrap(realm, impl->associated_document().history());
  568. }
  569. JS_DEFINE_NATIVE_FUNCTION(WindowObject::screen_left_getter)
  570. {
  571. auto* impl = TRY(impl_from(vm, global_object));
  572. return JS::Value(impl->screen_x());
  573. }
  574. JS_DEFINE_NATIVE_FUNCTION(WindowObject::screen_top_getter)
  575. {
  576. auto* impl = TRY(impl_from(vm, global_object));
  577. return JS::Value(impl->screen_y());
  578. }
  579. JS_DEFINE_NATIVE_FUNCTION(WindowObject::screen_x_getter)
  580. {
  581. auto* impl = TRY(impl_from(vm, global_object));
  582. return JS::Value(impl->screen_x());
  583. }
  584. JS_DEFINE_NATIVE_FUNCTION(WindowObject::screen_y_getter)
  585. {
  586. auto* impl = TRY(impl_from(vm, global_object));
  587. return JS::Value(impl->screen_y());
  588. }
  589. JS_DEFINE_NATIVE_FUNCTION(WindowObject::post_message)
  590. {
  591. auto* impl = TRY(impl_from(vm, global_object));
  592. auto target_origin = TRY(vm.argument(1).to_string(vm));
  593. impl->post_message(vm.argument(0), target_origin);
  594. return JS::js_undefined();
  595. }
  596. // https://html.spec.whatwg.org/multipage/webappapis.html#dom-origin
  597. JS_DEFINE_NATIVE_FUNCTION(WindowObject::origin_getter)
  598. {
  599. auto* impl = TRY(impl_from(vm, global_object));
  600. return JS::js_string(vm, impl->associated_document().origin().serialize());
  601. }
  602. JS_DEFINE_NATIVE_FUNCTION(WindowObject::local_storage_getter)
  603. {
  604. auto& realm = *vm.current_realm();
  605. auto* impl = TRY(impl_from(vm, global_object));
  606. // FIXME: localStorage may throw. We have to deal with that here.
  607. return wrap(realm, *impl->local_storage());
  608. }
  609. JS_DEFINE_NATIVE_FUNCTION(WindowObject::session_storage_getter)
  610. {
  611. auto& realm = *vm.current_realm();
  612. auto* impl = TRY(impl_from(vm, global_object));
  613. // FIXME: sessionStorage may throw. We have to deal with that here.
  614. return wrap(realm, *impl->session_storage());
  615. }
  616. JS_DEFINE_NATIVE_FUNCTION(WindowObject::name_getter)
  617. {
  618. auto* impl = TRY(impl_from(vm, global_object));
  619. return JS::js_string(vm, impl->name());
  620. }
  621. JS_DEFINE_NATIVE_FUNCTION(WindowObject::name_setter)
  622. {
  623. auto* impl = TRY(impl_from(vm, global_object));
  624. impl->set_name(TRY(vm.argument(0).to_string(vm)));
  625. return JS::js_undefined();
  626. }
  627. #define __ENUMERATE(attribute, event_name) \
  628. JS_DEFINE_NATIVE_FUNCTION(WindowObject::attribute##_getter) \
  629. { \
  630. auto* impl = TRY(impl_from(vm, global_object)); \
  631. auto retval = impl->attribute(); \
  632. if (!retval) \
  633. return JS::js_null(); \
  634. return retval->callback.cell(); \
  635. } \
  636. JS_DEFINE_NATIVE_FUNCTION(WindowObject::attribute##_setter) \
  637. { \
  638. auto* impl = TRY(impl_from(vm, global_object)); \
  639. auto value = vm.argument(0); \
  640. Optional<Bindings::CallbackType> cpp_value; \
  641. if (value.is_object()) { \
  642. cpp_value = Bindings::CallbackType { JS::make_handle(&value.as_object()), HTML::incumbent_settings_object() }; \
  643. } \
  644. impl->set_##attribute(cpp_value); \
  645. return JS::js_undefined(); \
  646. }
  647. ENUMERATE_GLOBAL_EVENT_HANDLERS(__ENUMERATE)
  648. ENUMERATE_WINDOW_EVENT_HANDLERS(__ENUMERATE)
  649. #undef __ENUMERATE
  650. }