WindowObject.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-setinterval
  206. JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_interval)
  207. {
  208. // FIXME: Ideally this would share more code with setTimeout() using the "timer initialization steps"
  209. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timer-initialisation-steps
  210. auto* impl = impl_from(vm, global_object);
  211. if (!impl)
  212. return {};
  213. if (!vm.argument_count()) {
  214. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setInterval");
  215. return {};
  216. }
  217. JS::FunctionObject* callback;
  218. if (vm.argument(0).is_function()) {
  219. callback = &vm.argument(0).as_function();
  220. } else {
  221. auto script_source = vm.argument(0).to_string(global_object);
  222. if (vm.exception())
  223. return {};
  224. // FIXME: This needs more work once we have a environment settings object.
  225. // The spec wants us to use a task for the "run function or script string" part,
  226. // using a NativeFunction for the latter is a workaround so that we can reuse the
  227. // DOM::Timer API unaltered (always expects a JS::FunctionObject).
  228. callback = JS::NativeFunction::create(global_object, "", [impl, script_source = move(script_source)](auto&, auto&) mutable {
  229. auto script = HTML::ClassicScript::create(impl->associated_document().url().to_string(), script_source, impl->associated_document().realm(), AK::URL());
  230. return script->run();
  231. });
  232. }
  233. i32 interval = 0;
  234. if (vm.argument_count() >= 2) {
  235. interval = vm.argument(1).to_i32(global_object);
  236. if (vm.exception())
  237. return {};
  238. if (interval < 0)
  239. interval = 0;
  240. }
  241. // FIXME: Pass ...arguments to the callback function when it's invoked
  242. auto timer_id = impl->set_interval(*callback, interval);
  243. return JS::Value(timer_id);
  244. }
  245. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-settimeout
  246. JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_timeout)
  247. {
  248. // FIXME: Ideally this would share more code with setInterval() using the "timer initialization steps"
  249. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timer-initialisation-steps
  250. auto* impl = impl_from(vm, global_object);
  251. if (!impl)
  252. return {};
  253. if (!vm.argument_count()) {
  254. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setTimeout");
  255. return {};
  256. }
  257. JS::FunctionObject* callback;
  258. if (vm.argument(0).is_function()) {
  259. callback = &vm.argument(0).as_function();
  260. } else {
  261. auto script_source = vm.argument(0).to_string(global_object);
  262. if (vm.exception())
  263. return {};
  264. // FIXME: This needs more work once we have a environment settings object.
  265. // The spec wants us to use a task for the "run function or script string" part,
  266. // using a NativeFunction for the latter is a workaround so that we can reuse the
  267. // DOM::Timer API unaltered (always expects a JS::FunctionObject).
  268. callback = JS::NativeFunction::create(global_object, "", [impl, script_source = move(script_source)](auto&, auto&) mutable {
  269. auto script = HTML::ClassicScript::create(impl->associated_document().url().to_string(), script_source, impl->associated_document().realm(), AK::URL());
  270. return script->run();
  271. });
  272. }
  273. i32 interval = 0;
  274. if (vm.argument_count() >= 2) {
  275. interval = vm.argument(1).to_i32(global_object);
  276. if (vm.exception())
  277. return {};
  278. if (interval < 0)
  279. interval = 0;
  280. }
  281. // FIXME: Pass ...arguments to the callback function when it's invoked
  282. auto timer_id = impl->set_timeout(*callback, interval);
  283. return JS::Value(timer_id);
  284. }
  285. JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_timeout)
  286. {
  287. auto* impl = impl_from(vm, global_object);
  288. if (!impl)
  289. return {};
  290. if (!vm.argument_count()) {
  291. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "clearTimeout");
  292. return {};
  293. }
  294. i32 timer_id = vm.argument(0).to_i32(global_object);
  295. if (vm.exception())
  296. return {};
  297. impl->clear_timeout(timer_id);
  298. return JS::js_undefined();
  299. }
  300. JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_interval)
  301. {
  302. auto* impl = impl_from(vm, global_object);
  303. if (!impl)
  304. return {};
  305. if (!vm.argument_count()) {
  306. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "clearInterval");
  307. return {};
  308. }
  309. i32 timer_id = vm.argument(0).to_i32(global_object);
  310. if (vm.exception())
  311. return {};
  312. impl->clear_timeout(timer_id);
  313. return JS::js_undefined();
  314. }
  315. JS_DEFINE_NATIVE_FUNCTION(WindowObject::request_animation_frame)
  316. {
  317. auto* impl = impl_from(vm, global_object);
  318. if (!impl)
  319. return {};
  320. if (!vm.argument_count()) {
  321. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "requestAnimationFrame");
  322. return {};
  323. }
  324. auto* callback_object = vm.argument(0).to_object(global_object);
  325. if (!callback_object)
  326. return {};
  327. if (!callback_object->is_function()) {
  328. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
  329. return {};
  330. }
  331. return JS::Value(impl->request_animation_frame(*static_cast<JS::FunctionObject*>(callback_object)));
  332. }
  333. JS_DEFINE_NATIVE_FUNCTION(WindowObject::cancel_animation_frame)
  334. {
  335. auto* impl = impl_from(vm, global_object);
  336. if (!impl)
  337. return {};
  338. if (!vm.argument_count()) {
  339. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "cancelAnimationFrame");
  340. return {};
  341. }
  342. auto id = vm.argument(0).to_i32(global_object);
  343. if (vm.exception())
  344. return {};
  345. impl->cancel_animation_frame(id);
  346. return JS::js_undefined();
  347. }
  348. JS_DEFINE_NATIVE_FUNCTION(WindowObject::queue_microtask)
  349. {
  350. auto* impl = impl_from(vm, global_object);
  351. if (!impl)
  352. return {};
  353. if (!vm.argument_count()) {
  354. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "queueMicrotask");
  355. return {};
  356. }
  357. auto* callback_object = vm.argument(0).to_object(global_object);
  358. if (!callback_object)
  359. return {};
  360. if (!callback_object->is_function()) {
  361. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
  362. return {};
  363. }
  364. impl->queue_microtask(static_cast<JS::FunctionObject&>(*callback_object));
  365. return JS::js_undefined();
  366. }
  367. JS_DEFINE_NATIVE_FUNCTION(WindowObject::atob)
  368. {
  369. auto* impl = impl_from(vm, global_object);
  370. if (!impl)
  371. return {};
  372. if (!vm.argument_count()) {
  373. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "atob");
  374. return {};
  375. }
  376. auto string = vm.argument(0).to_string(global_object);
  377. if (vm.exception())
  378. return {};
  379. auto decoded = decode_base64(StringView(string));
  380. // decode_base64() returns a byte string. LibJS uses UTF-8 for strings. Use Latin1Decoder to convert bytes 128-255 to UTF-8.
  381. auto decoder = TextCodec::decoder_for("windows-1252");
  382. VERIFY(decoder);
  383. return JS::js_string(vm, decoder->to_utf8(decoded));
  384. }
  385. JS_DEFINE_NATIVE_FUNCTION(WindowObject::btoa)
  386. {
  387. auto* impl = impl_from(vm, global_object);
  388. if (!impl)
  389. return {};
  390. if (!vm.argument_count()) {
  391. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "btoa");
  392. return {};
  393. }
  394. auto string = vm.argument(0).to_string(global_object);
  395. if (vm.exception())
  396. return {};
  397. Vector<u8> byte_string;
  398. byte_string.ensure_capacity(string.length());
  399. for (u32 code_point : Utf8View(string)) {
  400. if (code_point > 0xff) {
  401. vm.throw_exception<JS::InvalidCharacterError>(global_object, JS::ErrorType::NotAByteString, "btoa");
  402. return {};
  403. }
  404. byte_string.append(code_point);
  405. }
  406. auto encoded = encode_base64(byte_string.span());
  407. return JS::js_string(vm, move(encoded));
  408. }
  409. // https://html.spec.whatwg.org/multipage/browsers.html#dom-top
  410. JS_DEFINE_NATIVE_FUNCTION(WindowObject::top_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. VERIFY(this_browsing_context->top_level_browsing_context().active_document());
  419. auto& top_window = this_browsing_context->top_level_browsing_context().active_document()->window();
  420. return top_window.wrapper();
  421. }
  422. // https://html.spec.whatwg.org/multipage/browsers.html#dom-parent
  423. JS_DEFINE_NATIVE_FUNCTION(WindowObject::parent_getter)
  424. {
  425. auto* impl = impl_from(vm, global_object);
  426. if (!impl)
  427. return {};
  428. auto* this_browsing_context = impl->associated_document().browsing_context();
  429. if (!this_browsing_context)
  430. return JS::js_null();
  431. if (this_browsing_context->parent()) {
  432. VERIFY(this_browsing_context->parent()->active_document());
  433. auto& parent_window = this_browsing_context->parent()->active_document()->window();
  434. return parent_window.wrapper();
  435. }
  436. VERIFY(this_browsing_context == &this_browsing_context->top_level_browsing_context());
  437. return impl->wrapper();
  438. }
  439. JS_DEFINE_NATIVE_FUNCTION(WindowObject::document_getter)
  440. {
  441. auto* impl = impl_from(vm, global_object);
  442. if (!impl)
  443. return {};
  444. return wrap(global_object, impl->associated_document());
  445. }
  446. JS_DEFINE_NATIVE_FUNCTION(WindowObject::performance_getter)
  447. {
  448. auto* impl = impl_from(vm, global_object);
  449. if (!impl)
  450. return {};
  451. return wrap(global_object, impl->performance());
  452. }
  453. JS_DEFINE_NATIVE_FUNCTION(WindowObject::screen_getter)
  454. {
  455. auto* impl = impl_from(vm, global_object);
  456. if (!impl)
  457. return {};
  458. return wrap(global_object, impl->screen());
  459. }
  460. JS_DEFINE_NATIVE_FUNCTION(WindowObject::event_getter)
  461. {
  462. auto* impl = impl_from(vm, global_object);
  463. if (!impl)
  464. return {};
  465. if (!impl->current_event())
  466. return JS::js_undefined();
  467. return wrap(global_object, const_cast<DOM::Event&>(*impl->current_event()));
  468. }
  469. JS_DEFINE_NATIVE_FUNCTION(WindowObject::event_setter)
  470. {
  471. REPLACEABLE_PROPERTY_SETTER(WindowObject, event);
  472. }
  473. JS_DEFINE_NATIVE_FUNCTION(WindowObject::crypto_getter)
  474. {
  475. auto* impl = impl_from(vm, global_object);
  476. if (!impl)
  477. return {};
  478. return wrap(global_object, impl->crypto());
  479. }
  480. JS_DEFINE_NATIVE_FUNCTION(WindowObject::inner_width_getter)
  481. {
  482. auto* impl = impl_from(vm, global_object);
  483. if (!impl)
  484. return {};
  485. return JS::Value(impl->inner_width());
  486. }
  487. JS_DEFINE_NATIVE_FUNCTION(WindowObject::inner_height_getter)
  488. {
  489. auto* impl = impl_from(vm, global_object);
  490. if (!impl)
  491. return {};
  492. return JS::Value(impl->inner_height());
  493. }
  494. JS_DEFINE_NATIVE_FUNCTION(WindowObject::device_pixel_ratio_getter)
  495. {
  496. auto* impl = impl_from(vm, global_object);
  497. if (!impl)
  498. return {};
  499. return JS::Value(impl->device_pixel_ratio());
  500. }
  501. JS_DEFINE_NATIVE_FUNCTION(WindowObject::get_computed_style)
  502. {
  503. auto* impl = impl_from(vm, global_object);
  504. if (!impl)
  505. return {};
  506. auto* object = vm.argument(0).to_object(global_object);
  507. if (vm.exception())
  508. return {};
  509. if (!is<ElementWrapper>(object)) {
  510. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "DOM element");
  511. return {};
  512. }
  513. return wrap(global_object, impl->get_computed_style(static_cast<ElementWrapper*>(object)->impl()));
  514. }
  515. JS_DEFINE_NATIVE_FUNCTION(WindowObject::match_media)
  516. {
  517. auto* impl = impl_from(vm, global_object);
  518. if (!impl)
  519. return {};
  520. auto media = vm.argument(0).to_string(global_object);
  521. if (vm.exception())
  522. return {};
  523. return wrap(global_object, impl->match_media(move(media)));
  524. }
  525. // https://www.w3.org/TR/cssom-view/#dom-window-scrollx
  526. JS_DEFINE_NATIVE_GETTER(WindowObject::scroll_x_getter)
  527. {
  528. auto* impl = impl_from(vm, global_object);
  529. if (!impl)
  530. return {};
  531. return JS::Value(impl->scroll_x());
  532. }
  533. // https://www.w3.org/TR/cssom-view/#dom-window-scrolly
  534. JS_DEFINE_NATIVE_GETTER(WindowObject::scroll_y_getter)
  535. {
  536. auto* impl = impl_from(vm, global_object);
  537. if (!impl)
  538. return {};
  539. return JS::Value(impl->scroll_y());
  540. }
  541. enum class ScrollBehavior {
  542. Auto,
  543. Smooth
  544. };
  545. // https://www.w3.org/TR/cssom-view/#perform-a-scroll
  546. static void perform_a_scroll(Page& page, double x, double y, ScrollBehavior)
  547. {
  548. // FIXME: Stop any existing smooth-scrolls
  549. // FIXME: Implement smooth-scroll
  550. page.client().page_did_request_scroll_to({ x, y });
  551. }
  552. // https://www.w3.org/TR/cssom-view/#dom-window-scroll
  553. JS_DEFINE_NATIVE_FUNCTION(WindowObject::scroll)
  554. {
  555. auto* impl = impl_from(vm, global_object);
  556. if (!impl)
  557. return {};
  558. if (!impl->page())
  559. return {};
  560. auto& page = *impl->page();
  561. auto viewport_rect = page.top_level_browsing_context().viewport_rect();
  562. auto x_value = JS::Value(viewport_rect.x());
  563. auto y_value = JS::Value(viewport_rect.y());
  564. String behavior_string = "auto";
  565. if (vm.argument_count() == 1) {
  566. auto* options = vm.argument(0).to_object(global_object);
  567. if (vm.exception())
  568. return {};
  569. auto left = TRY_OR_DISCARD(options->get("left"));
  570. if (!left.is_undefined())
  571. x_value = left;
  572. auto top = TRY_OR_DISCARD(options->get("top"));
  573. if (!top.is_undefined())
  574. y_value = top;
  575. auto behavior_string_value = TRY_OR_DISCARD(options->get("behavior"));
  576. if (!behavior_string_value.is_undefined())
  577. behavior_string = behavior_string_value.to_string(global_object);
  578. if (vm.exception())
  579. return {};
  580. if (behavior_string != "smooth" && behavior_string != "auto") {
  581. vm.throw_exception<JS::TypeError>(global_object, "Behavior is not one of 'smooth' or 'auto'");
  582. return {};
  583. }
  584. } else if (vm.argument_count() >= 2) {
  585. // We ignore arguments 2+ in line with behavior of Chrome and Firefox
  586. x_value = vm.argument(0);
  587. y_value = vm.argument(1);
  588. }
  589. ScrollBehavior behavior = (behavior_string == "smooth") ? ScrollBehavior::Smooth : ScrollBehavior::Auto;
  590. double x = x_value.to_double(global_object);
  591. if (vm.exception())
  592. return {};
  593. x = JS::Value(x).is_finite_number() ? x : 0.0;
  594. double y = y_value.to_double(global_object);
  595. if (vm.exception())
  596. return {};
  597. y = JS::Value(y).is_finite_number() ? y : 0.0;
  598. // FIXME: Are we calculating the viewport in the way this function expects?
  599. // FIXME: Handle overflow-directions other than top-left to bottom-right
  600. perform_a_scroll(page, x, y, behavior);
  601. return JS::js_undefined();
  602. }
  603. // https://www.w3.org/TR/cssom-view/#dom-window-scrollby
  604. JS_DEFINE_NATIVE_FUNCTION(WindowObject::scroll_by)
  605. {
  606. auto* impl = impl_from(vm, global_object);
  607. if (!impl)
  608. return {};
  609. if (!impl->page())
  610. return {};
  611. auto& page = *impl->page();
  612. JS::Object* options = nullptr;
  613. if (vm.argument_count() == 0) {
  614. options = JS::Object::create(global_object, nullptr);
  615. } else if (vm.argument_count() == 1) {
  616. options = vm.argument(0).to_object(global_object);
  617. if (vm.exception())
  618. return {};
  619. } else if (vm.argument_count() >= 2) {
  620. // We ignore arguments 2+ in line with behavior of Chrome and Firefox
  621. options = JS::Object::create(global_object, nullptr);
  622. MUST(options->set("left", vm.argument(0), ShouldThrowExceptions::No));
  623. MUST(options->set("top", vm.argument(1), ShouldThrowExceptions::No));
  624. MUST(options->set("behavior", JS::js_string(vm, "auto"), ShouldThrowExceptions::No));
  625. }
  626. auto left_value = TRY_OR_DISCARD(options->get("left"));
  627. auto left = left_value.to_double(global_object);
  628. if (vm.exception())
  629. return {};
  630. auto top_value = TRY_OR_DISCARD(options->get("top"));
  631. auto top = top_value.to_double(global_object);
  632. if (vm.exception())
  633. return {};
  634. left = JS::Value(left).is_finite_number() ? left : 0.0;
  635. top = JS::Value(top).is_finite_number() ? top : 0.0;
  636. auto current_scroll_position = page.top_level_browsing_context().viewport_scroll_offset();
  637. left = left + current_scroll_position.x();
  638. top = top + current_scroll_position.y();
  639. auto behavior_string_value = TRY_OR_DISCARD(options->get("behavior"));
  640. auto behavior_string = behavior_string_value.is_undefined() ? "auto" : behavior_string_value.to_string(global_object);
  641. if (vm.exception())
  642. return {};
  643. if (behavior_string != "smooth" && behavior_string != "auto") {
  644. vm.throw_exception<JS::TypeError>(global_object, "Behavior is not one of 'smooth' or 'auto'");
  645. return {};
  646. }
  647. ScrollBehavior behavior = (behavior_string == "smooth") ? ScrollBehavior::Smooth : ScrollBehavior::Auto;
  648. // FIXME: Spec wants us to call scroll(options) here.
  649. // The only difference is that would invoke the viewport calculations that scroll()
  650. // is not actually doing yet, so this is the same for now.
  651. perform_a_scroll(page, left, top, behavior);
  652. return JS::js_undefined();
  653. }
  654. JS_DEFINE_NATIVE_FUNCTION(WindowObject::history_getter)
  655. {
  656. auto* impl = impl_from(vm, global_object);
  657. if (!impl)
  658. return {};
  659. return wrap(global_object, impl->associated_document().history());
  660. }
  661. JS_DEFINE_NATIVE_GETTER(WindowObject::screen_left_getter)
  662. {
  663. auto* impl = impl_from(vm, global_object);
  664. if (!impl)
  665. return {};
  666. return JS::Value(impl->screen_x());
  667. }
  668. JS_DEFINE_NATIVE_GETTER(WindowObject::screen_top_getter)
  669. {
  670. auto* impl = impl_from(vm, global_object);
  671. if (!impl)
  672. return {};
  673. return JS::Value(impl->screen_y());
  674. }
  675. JS_DEFINE_NATIVE_GETTER(WindowObject::screen_x_getter)
  676. {
  677. auto* impl = impl_from(vm, global_object);
  678. if (!impl)
  679. return {};
  680. return JS::Value(impl->screen_x());
  681. }
  682. JS_DEFINE_NATIVE_GETTER(WindowObject::screen_y_getter)
  683. {
  684. auto* impl = impl_from(vm, global_object);
  685. if (!impl)
  686. return {};
  687. return JS::Value(impl->screen_y());
  688. }
  689. #define __ENUMERATE(attribute, event_name) \
  690. JS_DEFINE_NATIVE_FUNCTION(WindowObject::attribute##_getter) \
  691. { \
  692. auto* impl = impl_from(vm, global_object); \
  693. if (!impl) \
  694. return {}; \
  695. auto retval = impl->attribute(); \
  696. if (retval.callback.is_null()) \
  697. return JS::js_null(); \
  698. return retval.callback.cell(); \
  699. } \
  700. JS_DEFINE_NATIVE_FUNCTION(WindowObject::attribute##_setter) \
  701. { \
  702. auto* impl = impl_from(vm, global_object); \
  703. if (!impl) \
  704. return {}; \
  705. auto value = vm.argument(0); \
  706. HTML::EventHandler cpp_value; \
  707. if (value.is_function()) { \
  708. cpp_value.callback = JS::make_handle(&value.as_function()); \
  709. } else if (value.is_string()) { \
  710. cpp_value.string = value.as_string().string(); \
  711. } else { \
  712. return JS::js_undefined(); \
  713. } \
  714. auto result = throw_dom_exception_if_needed(vm, global_object, [&] { \
  715. return impl->set_##attribute(cpp_value); \
  716. }); \
  717. if (should_return_empty(result)) \
  718. return {}; \
  719. return JS::js_undefined(); \
  720. }
  721. ENUMERATE_GLOBAL_EVENT_HANDLERS(__ENUMERATE)
  722. #undef __ENUMERATE
  723. }