Window.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGUI/DisplayLink.h>
  8. #include <LibJS/Runtime/AbstractOperations.h>
  9. #include <LibJS/Runtime/FunctionObject.h>
  10. #include <LibWeb/Bindings/IDLAbstractOperations.h>
  11. #include <LibWeb/CSS/Parser/Parser.h>
  12. #include <LibWeb/CSS/ResolvedCSSStyleDeclaration.h>
  13. #include <LibWeb/Crypto/Crypto.h>
  14. #include <LibWeb/DOM/Document.h>
  15. #include <LibWeb/DOM/Event.h>
  16. #include <LibWeb/DOM/EventDispatcher.h>
  17. #include <LibWeb/HTML/BrowsingContext.h>
  18. #include <LibWeb/HTML/EventLoop/EventLoop.h>
  19. #include <LibWeb/HTML/MessageEvent.h>
  20. #include <LibWeb/HTML/PageTransitionEvent.h>
  21. #include <LibWeb/HTML/Scripting/ClassicScript.h>
  22. #include <LibWeb/HTML/Scripting/ExceptionReporter.h>
  23. #include <LibWeb/HTML/Storage.h>
  24. #include <LibWeb/HTML/Timer.h>
  25. #include <LibWeb/HTML/Window.h>
  26. #include <LibWeb/HighResolutionTime/Performance.h>
  27. #include <LibWeb/Layout/InitialContainingBlock.h>
  28. #include <LibWeb/Page/Page.h>
  29. #include <LibWeb/Selection/Selection.h>
  30. namespace Web::HTML {
  31. class RequestAnimationFrameCallback : public RefCounted<RequestAnimationFrameCallback> {
  32. public:
  33. explicit RequestAnimationFrameCallback(i32 id, Function<void(i32)> handler)
  34. : m_id(id)
  35. , m_handler(move(handler))
  36. {
  37. }
  38. ~RequestAnimationFrameCallback() = default;
  39. i32 id() const { return m_id; }
  40. bool is_cancelled() const { return !m_handler; }
  41. void cancel() { m_handler = nullptr; }
  42. void invoke() { m_handler(m_id); }
  43. private:
  44. i32 m_id { 0 };
  45. Function<void(i32)> m_handler;
  46. };
  47. struct RequestAnimationFrameDriver {
  48. RequestAnimationFrameDriver()
  49. {
  50. m_timer = Core::Timer::create_single_shot(16, [] {
  51. HTML::main_thread_event_loop().schedule();
  52. });
  53. }
  54. NonnullRefPtr<RequestAnimationFrameCallback> add(Function<void(i32)> handler)
  55. {
  56. auto id = m_id_allocator.allocate();
  57. auto callback = adopt_ref(*new RequestAnimationFrameCallback { id, move(handler) });
  58. m_callbacks.set(id, callback);
  59. if (!m_timer->is_active())
  60. m_timer->start();
  61. return callback;
  62. }
  63. bool remove(i32 id)
  64. {
  65. auto it = m_callbacks.find(id);
  66. if (it == m_callbacks.end())
  67. return false;
  68. m_callbacks.remove(it);
  69. m_id_allocator.deallocate(id);
  70. return true;
  71. }
  72. void run()
  73. {
  74. auto taken_callbacks = move(m_callbacks);
  75. for (auto& it : taken_callbacks) {
  76. if (!it.value->is_cancelled())
  77. it.value->invoke();
  78. }
  79. }
  80. private:
  81. HashMap<i32, NonnullRefPtr<RequestAnimationFrameCallback>> m_callbacks;
  82. IDAllocator m_id_allocator;
  83. RefPtr<Core::Timer> m_timer;
  84. };
  85. static RequestAnimationFrameDriver& request_animation_frame_driver()
  86. {
  87. static RequestAnimationFrameDriver driver;
  88. return driver;
  89. }
  90. // https://html.spec.whatwg.org/#run-the-animation-frame-callbacks
  91. void run_animation_frame_callbacks(DOM::Document&, double)
  92. {
  93. // FIXME: Bring this closer to the spec.
  94. request_animation_frame_driver().run();
  95. }
  96. NonnullRefPtr<Window> Window::create_with_document(DOM::Document& document)
  97. {
  98. return adopt_ref(*new Window(document));
  99. }
  100. Window::Window(DOM::Document& document)
  101. : DOM::EventTarget()
  102. , m_associated_document(document)
  103. , m_performance(make<HighResolutionTime::Performance>(*this))
  104. , m_crypto(Crypto::Crypto::create())
  105. , m_screen(CSS::Screen::create({}, *this))
  106. {
  107. }
  108. Window::~Window()
  109. {
  110. }
  111. void Window::set_wrapper(Badge<Bindings::WindowObject>, Bindings::WindowObject& wrapper)
  112. {
  113. m_wrapper = wrapper.make_weak_ptr();
  114. }
  115. void Window::alert(String const& message)
  116. {
  117. if (auto* page = this->page())
  118. page->client().page_did_request_alert(message);
  119. }
  120. bool Window::confirm(String const& message)
  121. {
  122. if (auto* page = this->page())
  123. return page->client().page_did_request_confirm(message);
  124. return false;
  125. }
  126. String Window::prompt(String const& message, String const& default_)
  127. {
  128. if (auto* page = this->page())
  129. return page->client().page_did_request_prompt(message, default_);
  130. return {};
  131. }
  132. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-settimeout
  133. i32 Window::set_timeout(Bindings::TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments)
  134. {
  135. return run_timer_initialization_steps(move(handler), timeout, move(arguments), Repeat::No);
  136. }
  137. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-setinterval
  138. i32 Window::set_interval(Bindings::TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments)
  139. {
  140. return run_timer_initialization_steps(move(handler), timeout, move(arguments), Repeat::Yes);
  141. }
  142. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-cleartimeout
  143. void Window::clear_timeout(i32 id)
  144. {
  145. m_timers.remove(id);
  146. }
  147. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-clearinterval
  148. void Window::clear_interval(i32 id)
  149. {
  150. m_timers.remove(id);
  151. }
  152. void Window::deallocate_timer_id(Badge<Timer>, i32 id)
  153. {
  154. m_timer_id_allocator.deallocate(id);
  155. }
  156. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timer-initialisation-steps
  157. i32 Window::run_timer_initialization_steps(Bindings::TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments, Repeat repeat, Optional<i32> previous_id)
  158. {
  159. // 1. Let thisArg be global if that is a WorkerGlobalScope object; otherwise let thisArg be the WindowProxy that corresponds to global.
  160. // 2. If previousId was given, let id be previousId; otherwise, let id be an implementation-defined integer that is greater than zero and does not already exist in global's map of active timers.
  161. auto id = previous_id.has_value() ? previous_id.value() : m_timer_id_allocator.allocate();
  162. // 3. FIXME: If the surrounding agent's event loop's currently running task is a task that was created by this algorithm, then let nesting level be the task's timer nesting level. Otherwise, let nesting level be zero.
  163. // 4. If timeout is less than 0, then set timeout to 0.
  164. if (timeout < 0)
  165. timeout = 0;
  166. // 5. FIXME: If nesting level is greater than 5, and timeout is less than 4, then set timeout to 4.
  167. // 6. Let callerRealm be the current Realm Record, and calleeRealm be global's relevant Realm.
  168. // FIXME: Implement this when step 9.2 is implemented.
  169. // 7. Let initiating script be the active script.
  170. // 8. Assert: initiating script is not null, since this algorithm is always called from some script.
  171. // 9. Let task be a task that runs the following substeps:
  172. auto task = [window = NonnullRefPtr(*this), handler = move(handler), timeout, arguments = move(arguments), repeat, id]() mutable {
  173. // 1. If id does not exist in global's map of active timers, then abort these steps.
  174. if (!window->m_timers.contains(id))
  175. return;
  176. handler.visit(
  177. // 2. If handler is a Function, then invoke handler given arguments with the callback this value set to thisArg. If this throws an exception, catch it, and report the exception.
  178. [&](Bindings::CallbackType& callback) {
  179. if (auto result = Bindings::IDL::invoke_callback(callback, window->wrapper(), arguments); result.is_error())
  180. HTML::report_exception(result);
  181. },
  182. // 3. Otherwise:
  183. [&](String const& source) {
  184. // 1. Assert: handler is a string.
  185. // 2. FIXME: Perform HostEnsureCanCompileStrings(callerRealm, calleeRealm). If this throws an exception, catch it, report the exception, and abort these steps.
  186. // 3. Let settings object be global's relevant settings object.
  187. auto& settings_object = window->associated_document().relevant_settings_object();
  188. // 4. Let base URL be initiating script's base URL.
  189. auto url = window->associated_document().url();
  190. // 5. Assert: base URL is not null, as initiating script is a classic script or a JavaScript module script.
  191. // 6. Let fetch options be a script fetch options whose cryptographic nonce is initiating script's fetch options's cryptographic nonce, integrity metadata is the empty string, parser metadata is "not-parser-inserted", credentials mode is initiating script's fetch options's credentials mode, and referrer policy is initiating script's fetch options's referrer policy.
  192. // 7. Let script be the result of creating a classic script given handler, settings object, base URL, and fetch options.
  193. auto script = HTML::ClassicScript::create(url.basename(), source, settings_object, url);
  194. // 8. Run the classic script script.
  195. (void)script->run();
  196. });
  197. // 4. If id does not exist in global's map of active timers, then abort these steps.
  198. if (!window->m_timers.contains(id))
  199. return;
  200. switch (repeat) {
  201. // 5. If repeat is true, then perform the timer initialization steps again, given global, handler, timeout, arguments, true, and id.
  202. case Repeat::Yes:
  203. window->run_timer_initialization_steps(handler, timeout, move(arguments), repeat, id);
  204. break;
  205. // 6. Otherwise, remove global's map of active timers[id].
  206. case Repeat::No:
  207. window->m_timers.remove(id);
  208. break;
  209. }
  210. };
  211. // 10. FIXME: Increment nesting level by one.
  212. // 11. FIXME: Set task's timer nesting level to nesting level.
  213. // 12. Let completionStep be an algorithm step which queues a global task on the timer task source given global to run task.
  214. auto completion_step = [window = NonnullRefPtr(*this), task = move(task)]() mutable {
  215. HTML::queue_global_task(HTML::Task::Source::TimerTask, *window->wrapper(), [task = move(task)]() mutable {
  216. task();
  217. });
  218. };
  219. // 13. Run steps after a timeout given global, "setTimeout/setInterval", timeout, completionStep, and id.
  220. auto timer = Timer::create(*this, timeout, move(completion_step), id);
  221. m_timers.set(id, timer);
  222. timer->start();
  223. // 14. Return id.
  224. return id;
  225. }
  226. // https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#run-the-animation-frame-callbacks
  227. i32 Window::request_animation_frame(NonnullOwnPtr<Bindings::CallbackType> js_callback)
  228. {
  229. auto callback = request_animation_frame_driver().add([this, js_callback = move(js_callback)](i32 id) mutable {
  230. // 3. Invoke callback, passing now as the only argument,
  231. auto result = Bindings::IDL::invoke_callback(*js_callback, {}, JS::Value(performance().now()));
  232. // and if an exception is thrown, report the exception.
  233. if (result.is_error())
  234. HTML::report_exception(result);
  235. m_request_animation_frame_callbacks.remove(id);
  236. });
  237. m_request_animation_frame_callbacks.set(callback->id(), callback);
  238. return callback->id();
  239. }
  240. void Window::cancel_animation_frame(i32 id)
  241. {
  242. auto it = m_request_animation_frame_callbacks.find(id);
  243. if (it == m_request_animation_frame_callbacks.end())
  244. return;
  245. it->value->cancel();
  246. m_request_animation_frame_callbacks.remove(it);
  247. }
  248. void Window::did_set_location_href(Badge<Bindings::LocationObject>, AK::URL const& new_href)
  249. {
  250. auto* browsing_context = associated_document().browsing_context();
  251. if (!browsing_context)
  252. return;
  253. browsing_context->loader().load(new_href, FrameLoader::Type::Navigation);
  254. }
  255. void Window::did_call_location_reload(Badge<Bindings::LocationObject>)
  256. {
  257. auto* browsing_context = associated_document().browsing_context();
  258. if (!browsing_context)
  259. return;
  260. browsing_context->loader().load(associated_document().url(), FrameLoader::Type::Reload);
  261. }
  262. void Window::did_call_location_replace(Badge<Bindings::LocationObject>, String url)
  263. {
  264. auto* browsing_context = associated_document().browsing_context();
  265. if (!browsing_context)
  266. return;
  267. auto new_url = associated_document().parse_url(url);
  268. browsing_context->loader().load(move(new_url), FrameLoader::Type::Navigation);
  269. }
  270. bool Window::dispatch_event(NonnullRefPtr<DOM::Event> event)
  271. {
  272. return DOM::EventDispatcher::dispatch(*this, event, true);
  273. }
  274. JS::Object* Window::create_wrapper(JS::GlobalObject& global_object)
  275. {
  276. return &global_object;
  277. }
  278. // https://www.w3.org/TR/cssom-view-1/#dom-window-innerwidth
  279. int Window::inner_width() const
  280. {
  281. // The innerWidth attribute must return the viewport width including the size of a rendered scroll bar (if any),
  282. // or zero if there is no viewport.
  283. if (auto const* browsing_context = associated_document().browsing_context())
  284. return browsing_context->viewport_rect().width();
  285. return 0;
  286. }
  287. // https://www.w3.org/TR/cssom-view-1/#dom-window-innerheight
  288. int Window::inner_height() const
  289. {
  290. // The innerHeight attribute must return the viewport height including the size of a rendered scroll bar (if any),
  291. // or zero if there is no viewport.
  292. if (auto const* browsing_context = associated_document().browsing_context())
  293. return browsing_context->viewport_rect().height();
  294. return 0;
  295. }
  296. Page* Window::page()
  297. {
  298. return associated_document().page();
  299. }
  300. Page const* Window::page() const
  301. {
  302. return associated_document().page();
  303. }
  304. NonnullRefPtr<CSS::CSSStyleDeclaration> Window::get_computed_style(DOM::Element& element) const
  305. {
  306. return CSS::ResolvedCSSStyleDeclaration::create(element);
  307. }
  308. NonnullRefPtr<CSS::MediaQueryList> Window::match_media(String media)
  309. {
  310. auto media_query_list = CSS::MediaQueryList::create(associated_document(), parse_media_query_list(CSS::ParsingContext(associated_document()), media));
  311. associated_document().add_media_query_list(media_query_list);
  312. return media_query_list;
  313. }
  314. Optional<CSS::MediaFeatureValue> Window::query_media_feature(CSS::MediaFeatureID media_feature) const
  315. {
  316. // FIXME: Many of these should be dependent on the hardware
  317. // https://www.w3.org/TR/mediaqueries-5/#media-descriptor-table
  318. switch (media_feature) {
  319. case CSS::MediaFeatureID::AnyHover:
  320. return CSS::MediaFeatureValue(CSS::ValueID::Hover);
  321. case CSS::MediaFeatureID::AnyPointer:
  322. return CSS::MediaFeatureValue(CSS::ValueID::Fine);
  323. case CSS::MediaFeatureID::AspectRatio:
  324. return CSS::MediaFeatureValue(CSS::Ratio(inner_width(), inner_height()));
  325. case CSS::MediaFeatureID::Color:
  326. return CSS::MediaFeatureValue(8);
  327. case CSS::MediaFeatureID::ColorGamut:
  328. return CSS::MediaFeatureValue(CSS::ValueID::Srgb);
  329. case CSS::MediaFeatureID::ColorIndex:
  330. return CSS::MediaFeatureValue(0);
  331. // FIXME: device-aspect-ratio
  332. // FIXME: device-height
  333. // FIXME: device-width
  334. case CSS::MediaFeatureID::DisplayMode:
  335. // FIXME: Detect if window is fullscreen
  336. return CSS::MediaFeatureValue(CSS::ValueID::Browser);
  337. case CSS::MediaFeatureID::DynamicRange:
  338. return CSS::MediaFeatureValue(CSS::ValueID::Standard);
  339. case CSS::MediaFeatureID::EnvironmentBlending:
  340. return CSS::MediaFeatureValue(CSS::ValueID::Opaque);
  341. case CSS::MediaFeatureID::ForcedColors:
  342. return CSS::MediaFeatureValue(CSS::ValueID::None);
  343. case CSS::MediaFeatureID::Grid:
  344. return CSS::MediaFeatureValue(0);
  345. case CSS::MediaFeatureID::Height:
  346. return CSS::MediaFeatureValue(CSS::Length::make_px(inner_height()));
  347. case CSS::MediaFeatureID::HorizontalViewportSegments:
  348. return CSS::MediaFeatureValue(1);
  349. case CSS::MediaFeatureID::Hover:
  350. return CSS::MediaFeatureValue(CSS::ValueID::Hover);
  351. case CSS::MediaFeatureID::InvertedColors:
  352. return CSS::MediaFeatureValue(CSS::ValueID::None);
  353. case CSS::MediaFeatureID::Monochrome:
  354. return CSS::MediaFeatureValue(0);
  355. case CSS::MediaFeatureID::NavControls:
  356. return CSS::MediaFeatureValue(CSS::ValueID::Back);
  357. case CSS::MediaFeatureID::Orientation:
  358. return CSS::MediaFeatureValue(inner_height() >= inner_width() ? CSS::ValueID::Portrait : CSS::ValueID::Landscape);
  359. case CSS::MediaFeatureID::OverflowBlock:
  360. return CSS::MediaFeatureValue(CSS::ValueID::Scroll);
  361. case CSS::MediaFeatureID::OverflowInline:
  362. return CSS::MediaFeatureValue(CSS::ValueID::Scroll);
  363. case CSS::MediaFeatureID::Pointer:
  364. return CSS::MediaFeatureValue(CSS::ValueID::Fine);
  365. case CSS::MediaFeatureID::PrefersColorScheme: {
  366. if (auto* page = this->page()) {
  367. switch (page->preferred_color_scheme()) {
  368. case CSS::PreferredColorScheme::Light:
  369. return CSS::MediaFeatureValue(CSS::ValueID::Light);
  370. case CSS::PreferredColorScheme::Dark:
  371. return CSS::MediaFeatureValue(CSS::ValueID::Dark);
  372. case CSS::PreferredColorScheme::Auto:
  373. default:
  374. return CSS::MediaFeatureValue(page->palette().is_dark() ? CSS::ValueID::Dark : CSS::ValueID::Light);
  375. }
  376. }
  377. return CSS::MediaFeatureValue(CSS::ValueID::Light);
  378. }
  379. case CSS::MediaFeatureID::PrefersContrast:
  380. // FIXME: Make this a preference
  381. return CSS::MediaFeatureValue(CSS::ValueID::NoPreference);
  382. case CSS::MediaFeatureID::PrefersReducedData:
  383. // FIXME: Make this a preference
  384. return CSS::MediaFeatureValue(CSS::ValueID::NoPreference);
  385. case CSS::MediaFeatureID::PrefersReducedMotion:
  386. // FIXME: Make this a preference
  387. return CSS::MediaFeatureValue(CSS::ValueID::NoPreference);
  388. case CSS::MediaFeatureID::PrefersReducedTransparency:
  389. // FIXME: Make this a preference
  390. return CSS::MediaFeatureValue(CSS::ValueID::NoPreference);
  391. // FIXME: resolution
  392. case CSS::MediaFeatureID::Scan:
  393. return CSS::MediaFeatureValue(CSS::ValueID::Progressive);
  394. case CSS::MediaFeatureID::Scripting:
  395. if (associated_document().is_scripting_enabled())
  396. return CSS::MediaFeatureValue(CSS::ValueID::Enabled);
  397. return CSS::MediaFeatureValue(CSS::ValueID::None);
  398. case CSS::MediaFeatureID::Update:
  399. return CSS::MediaFeatureValue(CSS::ValueID::Fast);
  400. case CSS::MediaFeatureID::VerticalViewportSegments:
  401. return CSS::MediaFeatureValue(1);
  402. case CSS::MediaFeatureID::VideoColorGamut:
  403. return CSS::MediaFeatureValue(CSS::ValueID::Srgb);
  404. case CSS::MediaFeatureID::VideoDynamicRange:
  405. return CSS::MediaFeatureValue(CSS::ValueID::Standard);
  406. case CSS::MediaFeatureID::Width:
  407. return CSS::MediaFeatureValue(CSS::Length::make_px(inner_width()));
  408. default:
  409. break;
  410. }
  411. return {};
  412. }
  413. // https://www.w3.org/TR/cssom-view/#dom-window-scrollx
  414. float Window::scroll_x() const
  415. {
  416. if (auto* page = this->page())
  417. return page->top_level_browsing_context().viewport_scroll_offset().x();
  418. return 0;
  419. }
  420. // https://www.w3.org/TR/cssom-view/#dom-window-scrolly
  421. float Window::scroll_y() const
  422. {
  423. if (auto* page = this->page())
  424. return page->top_level_browsing_context().viewport_scroll_offset().y();
  425. return 0;
  426. }
  427. // https://html.spec.whatwg.org/#fire-a-page-transition-event
  428. void Window::fire_a_page_transition_event(FlyString const& event_name, bool persisted)
  429. {
  430. // To fire a page transition event named eventName at a Window window with a boolean persisted,
  431. // fire an event named eventName at window, using PageTransitionEvent,
  432. // with the persisted attribute initialized to persisted,
  433. HTML::PageTransitionEventInit event_init {};
  434. event_init.persisted = persisted;
  435. auto event = HTML::PageTransitionEvent::create(event_name, event_init);
  436. // ...the cancelable attribute initialized to true,
  437. event->set_cancelable(true);
  438. // the bubbles attribute initialized to true,
  439. event->set_bubbles(true);
  440. // and legacy target override flag set.
  441. dispatch_event(move(event));
  442. }
  443. // https://html.spec.whatwg.org/#dom-queuemicrotask
  444. void Window::queue_microtask(NonnullOwnPtr<Bindings::CallbackType> callback)
  445. {
  446. // The queueMicrotask(callback) method must queue a microtask to invoke callback,
  447. HTML::queue_a_microtask(&associated_document(), [callback = move(callback)]() mutable {
  448. auto result = Bindings::IDL::invoke_callback(*callback, {});
  449. // and if callback throws an exception, report the exception.
  450. if (result.is_error())
  451. HTML::report_exception(result);
  452. });
  453. }
  454. float Window::device_pixel_ratio() const
  455. {
  456. // FIXME: Return 2.0f if we're in HiDPI mode!
  457. return 1.0f;
  458. }
  459. // https://drafts.csswg.org/cssom-view/#dom-window-screenx
  460. int Window::screen_x() const
  461. {
  462. // The screenX and screenLeft attributes must return the x-coordinate, relative to the origin of the Web-exposed screen area,
  463. // of the left of the client window as number of CSS pixels, or zero if there is no such thing.
  464. return 0;
  465. }
  466. // https://drafts.csswg.org/cssom-view/#dom-window-screeny
  467. int Window::screen_y() const
  468. {
  469. // The screenY and screenTop attributes must return the y-coordinate, relative to the origin of the screen of the Web-exposed screen area,
  470. // of the top of the client window as number of CSS pixels, or zero if there is no such thing.
  471. return 0;
  472. }
  473. // https://w3c.github.io/selection-api/#dom-window-getselection
  474. Selection::Selection* Window::get_selection()
  475. {
  476. // FIXME: Implement.
  477. return nullptr;
  478. }
  479. // https://html.spec.whatwg.org/multipage/webstorage.html#dom-localstorage
  480. RefPtr<HTML::Storage> Window::local_storage()
  481. {
  482. // FIXME: Implement according to spec.
  483. static HashMap<Origin, NonnullRefPtr<HTML::Storage>> local_storage_per_origin;
  484. return local_storage_per_origin.ensure(associated_document().origin(), [] {
  485. return HTML::Storage::create();
  486. });
  487. }
  488. // https://html.spec.whatwg.org/multipage/webstorage.html#dom-sessionstorage
  489. RefPtr<HTML::Storage> Window::session_storage()
  490. {
  491. // FIXME: Implement according to spec.
  492. static HashMap<Origin, NonnullRefPtr<HTML::Storage>> session_storage_per_origin;
  493. return session_storage_per_origin.ensure(associated_document().origin(), [] {
  494. return HTML::Storage::create();
  495. });
  496. }
  497. // https://html.spec.whatwg.org/multipage/browsers.html#dom-parent
  498. Window* Window::parent()
  499. {
  500. // 1. Let current be this Window object's browsing context.
  501. auto* current = associated_document().browsing_context();
  502. // 2. If current is null, then return null.
  503. if (!current)
  504. return nullptr;
  505. // 3. If current is a child browsing context of another browsing context parent,
  506. // then return parent's WindowProxy object.
  507. if (current->parent()) {
  508. VERIFY(current->parent()->active_document());
  509. return &current->parent()->active_document()->window();
  510. }
  511. // 4. Assert: current is a top-level browsing context.
  512. VERIFY(current->is_top_level());
  513. // FIXME: 5. Return current's WindowProxy object.
  514. VERIFY(current->active_document());
  515. return &current->active_document()->window();
  516. }
  517. // https://html.spec.whatwg.org/multipage/web-messaging.html#window-post-message-steps
  518. DOM::ExceptionOr<void> Window::post_message(JS::Value message, String const&)
  519. {
  520. // FIXME: This is an ad-hoc hack implementation instead, since we don't currently
  521. // have serialization and deserialization of messages.
  522. HTML::queue_global_task(HTML::Task::Source::PostedMessage, *wrapper(), [strong_this = NonnullRefPtr(*this), message]() mutable {
  523. HTML::MessageEventInit event_init {};
  524. event_init.data = message;
  525. event_init.origin = "<origin>";
  526. strong_this->dispatch_event(HTML::MessageEvent::create(HTML::EventNames::message, event_init));
  527. });
  528. return {};
  529. }
  530. }