WindowObject.cpp 29 KB

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