WindowObject.cpp 30 KB

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