WindowObject.cpp 29 KB

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