WindowObject.cpp 28 KB

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