WindowObject.cpp 30 KB

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