ConnectionFromClient.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. /*
  2. * Copyright (c) 2020-2023, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
  4. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  5. * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
  6. * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
  7. * Copyright (c) 2023-2024, Andrew Kaster <akaster@serenityos.org>
  8. *
  9. * SPDX-License-Identifier: BSD-2-Clause
  10. */
  11. #include <AK/JsonObject.h>
  12. #include <AK/QuickSort.h>
  13. #include <LibCore/EventLoop.h>
  14. #include <LibGC/Heap.h>
  15. #include <LibGfx/Bitmap.h>
  16. #include <LibGfx/Font/FontDatabase.h>
  17. #include <LibGfx/SystemTheme.h>
  18. #include <LibJS/Runtime/ConsoleObject.h>
  19. #include <LibJS/Runtime/Date.h>
  20. #include <LibUnicode/TimeZone.h>
  21. #include <LibWeb/ARIA/RoleType.h>
  22. #include <LibWeb/Bindings/MainThreadVM.h>
  23. #include <LibWeb/CSS/StyleComputer.h>
  24. #include <LibWeb/DOM/Attr.h>
  25. #include <LibWeb/DOM/CharacterData.h>
  26. #include <LibWeb/DOM/Document.h>
  27. #include <LibWeb/DOM/Element.h>
  28. #include <LibWeb/DOM/ElementFactory.h>
  29. #include <LibWeb/DOM/ShadowRoot.h>
  30. #include <LibWeb/DOM/Text.h>
  31. #include <LibWeb/Dump.h>
  32. #include <LibWeb/HTML/BrowsingContext.h>
  33. #include <LibWeb/HTML/HTMLInputElement.h>
  34. #include <LibWeb/HTML/SelectedFile.h>
  35. #include <LibWeb/HTML/Storage.h>
  36. #include <LibWeb/HTML/TraversableNavigable.h>
  37. #include <LibWeb/HTML/Window.h>
  38. #include <LibWeb/Infra/Strings.h>
  39. #include <LibWeb/Layout/Viewport.h>
  40. #include <LibWeb/Loader/ContentFilter.h>
  41. #include <LibWeb/Loader/ProxyMappings.h>
  42. #include <LibWeb/Loader/ResourceLoader.h>
  43. #include <LibWeb/Loader/UserAgent.h>
  44. #include <LibWeb/Namespace.h>
  45. #include <LibWeb/Painting/StackingContext.h>
  46. #include <LibWeb/Painting/ViewportPaintable.h>
  47. #include <LibWeb/PermissionsPolicy/AutoplayAllowlist.h>
  48. #include <LibWeb/Platform/EventLoopPlugin.h>
  49. #include <LibWebView/Attribute.h>
  50. #include <WebContent/ConnectionFromClient.h>
  51. #include <WebContent/PageClient.h>
  52. #include <WebContent/PageHost.h>
  53. #include <WebContent/WebContentClientEndpoint.h>
  54. namespace WebContent {
  55. ConnectionFromClient::ConnectionFromClient(GC::Heap& heap, IPC::Transport transport)
  56. : IPC::ConnectionFromClient<WebContentClientEndpoint, WebContentServerEndpoint>(*this, move(transport), 1)
  57. , m_heap(heap)
  58. , m_page_host(PageHost::create(*this))
  59. {
  60. m_input_event_queue_timer = Web::Platform::Timer::create_single_shot(m_heap, 0, GC::create_function(heap, [this] { process_next_input_event(); }));
  61. }
  62. ConnectionFromClient::~ConnectionFromClient() = default;
  63. void ConnectionFromClient::die()
  64. {
  65. Web::Platform::EventLoopPlugin::the().quit();
  66. }
  67. Optional<PageClient&> ConnectionFromClient::page(u64 index, SourceLocation location)
  68. {
  69. if (auto page = m_page_host->page(index); page.has_value())
  70. return *page;
  71. dbgln("ConnectionFromClient::{}: Did not find a page with ID {}", location.function_name(), index);
  72. return {};
  73. }
  74. Optional<PageClient const&> ConnectionFromClient::page(u64 index, SourceLocation location) const
  75. {
  76. if (auto page = m_page_host->page(index); page.has_value())
  77. return *page;
  78. dbgln("ConnectionFromClient::{}: Did not find a page with ID {}", location.function_name(), index);
  79. return {};
  80. }
  81. void ConnectionFromClient::close_server()
  82. {
  83. shutdown();
  84. }
  85. Messages::WebContentServer::GetWindowHandleResponse ConnectionFromClient::get_window_handle(u64 page_id)
  86. {
  87. if (auto page = this->page(page_id); page.has_value())
  88. return page->page().top_level_traversable()->window_handle();
  89. return String {};
  90. }
  91. void ConnectionFromClient::set_window_handle(u64 page_id, String const& handle)
  92. {
  93. if (auto page = this->page(page_id); page.has_value())
  94. page->page().top_level_traversable()->set_window_handle(handle);
  95. }
  96. void ConnectionFromClient::connect_to_webdriver(u64 page_id, ByteString const& webdriver_ipc_path)
  97. {
  98. if (auto page = this->page(page_id); page.has_value()) {
  99. // FIXME: Propagate this error back to the browser.
  100. if (auto result = page->connect_to_webdriver(webdriver_ipc_path); result.is_error())
  101. dbgln("Unable to connect to the WebDriver process: {}", result.error());
  102. }
  103. }
  104. void ConnectionFromClient::connect_to_image_decoder(IPC::File const& image_decoder_socket)
  105. {
  106. if (on_image_decoder_connection)
  107. on_image_decoder_connection(image_decoder_socket);
  108. }
  109. void ConnectionFromClient::update_system_theme(u64 page_id, Core::AnonymousBuffer const& theme_buffer)
  110. {
  111. auto page = this->page(page_id);
  112. if (!page.has_value())
  113. return;
  114. Gfx::set_system_theme(theme_buffer);
  115. auto impl = Gfx::PaletteImpl::create_with_anonymous_buffer(theme_buffer);
  116. page->set_palette_impl(*impl);
  117. }
  118. void ConnectionFromClient::update_screen_rects(u64 page_id, Vector<Web::DevicePixelRect> const& rects, u32 main_screen)
  119. {
  120. if (auto page = this->page(page_id); page.has_value())
  121. page->set_screen_rects(rects, main_screen);
  122. }
  123. void ConnectionFromClient::load_url(u64 page_id, const URL::URL& url)
  124. {
  125. auto page = this->page(page_id);
  126. if (!page.has_value())
  127. return;
  128. page->page().load(url);
  129. }
  130. void ConnectionFromClient::load_html(u64 page_id, ByteString const& html)
  131. {
  132. if (auto page = this->page(page_id); page.has_value())
  133. page->page().load_html(html);
  134. }
  135. void ConnectionFromClient::reload(u64 page_id)
  136. {
  137. if (auto page = this->page(page_id); page.has_value())
  138. page->page().reload();
  139. }
  140. void ConnectionFromClient::traverse_the_history_by_delta(u64 page_id, i32 delta)
  141. {
  142. if (auto page = this->page(page_id); page.has_value())
  143. page->page().traverse_the_history_by_delta(delta);
  144. }
  145. void ConnectionFromClient::set_viewport_size(u64 page_id, Web::DevicePixelSize const size)
  146. {
  147. if (auto page = this->page(page_id); page.has_value())
  148. page->set_viewport_size(size);
  149. }
  150. void ConnectionFromClient::ready_to_paint(u64 page_id)
  151. {
  152. if (auto page = this->page(page_id); page.has_value())
  153. page->ready_to_paint();
  154. }
  155. void ConnectionFromClient::process_next_input_event()
  156. {
  157. if (m_input_event_queue.is_empty())
  158. return;
  159. auto event = m_input_event_queue.dequeue();
  160. auto page = this->page(event.page_id);
  161. if (!page.has_value())
  162. return;
  163. auto result = event.event.visit(
  164. [&](Web::KeyEvent const& event) {
  165. switch (event.type) {
  166. case Web::KeyEvent::Type::KeyDown:
  167. return page->page().handle_keydown(event.key, event.modifiers, event.code_point, event.repeat);
  168. case Web::KeyEvent::Type::KeyUp:
  169. return page->page().handle_keyup(event.key, event.modifiers, event.code_point, event.repeat);
  170. }
  171. VERIFY_NOT_REACHED();
  172. },
  173. [&](Web::MouseEvent const& event) {
  174. switch (event.type) {
  175. case Web::MouseEvent::Type::MouseDown:
  176. return page->page().handle_mousedown(event.position, event.screen_position, event.button, event.buttons, event.modifiers);
  177. case Web::MouseEvent::Type::MouseUp:
  178. return page->page().handle_mouseup(event.position, event.screen_position, event.button, event.buttons, event.modifiers);
  179. case Web::MouseEvent::Type::MouseMove:
  180. return page->page().handle_mousemove(event.position, event.screen_position, event.buttons, event.modifiers);
  181. case Web::MouseEvent::Type::MouseWheel:
  182. return page->page().handle_mousewheel(event.position, event.screen_position, event.button, event.buttons, event.modifiers, event.wheel_delta_x, event.wheel_delta_y);
  183. case Web::MouseEvent::Type::DoubleClick:
  184. return page->page().handle_doubleclick(event.position, event.screen_position, event.button, event.buttons, event.modifiers);
  185. }
  186. VERIFY_NOT_REACHED();
  187. },
  188. [&](Web::DragEvent& event) {
  189. return page->page().handle_drag_and_drop_event(event.type, event.position, event.screen_position, event.button, event.buttons, event.modifiers, move(event.files));
  190. });
  191. // We have to notify the client about coalesced events, so we do that by saying none of them were handled by the web page.
  192. for (size_t i = 0; i < event.coalesced_event_count; ++i)
  193. report_finished_handling_input_event(event.page_id, Web::EventResult::Dropped);
  194. report_finished_handling_input_event(event.page_id, result);
  195. if (!m_input_event_queue.is_empty())
  196. m_input_event_queue_timer->start();
  197. }
  198. void ConnectionFromClient::key_event(u64 page_id, Web::KeyEvent const& event)
  199. {
  200. enqueue_input_event({ page_id, move(const_cast<Web::KeyEvent&>(event)), 0 });
  201. }
  202. void ConnectionFromClient::mouse_event(u64 page_id, Web::MouseEvent const& event)
  203. {
  204. // OPTIMIZATION: Coalesce consecutive unprocessed mouse move and wheel events.
  205. auto event_to_coalesce = [&]() -> Web::MouseEvent const* {
  206. if (m_input_event_queue.is_empty())
  207. return nullptr;
  208. if (m_input_event_queue.tail().page_id != page_id)
  209. return nullptr;
  210. if (event.type != Web::MouseEvent::Type::MouseMove && event.type != Web::MouseEvent::Type::MouseWheel)
  211. return nullptr;
  212. if (auto const* mouse_event = m_input_event_queue.tail().event.get_pointer<Web::MouseEvent>()) {
  213. if (mouse_event->type == event.type)
  214. return mouse_event;
  215. }
  216. return nullptr;
  217. };
  218. if (auto const* last_mouse_event = event_to_coalesce()) {
  219. auto& mutable_event = const_cast<Web::MouseEvent&>(event);
  220. mutable_event.wheel_delta_x += last_mouse_event->wheel_delta_x;
  221. mutable_event.wheel_delta_y += last_mouse_event->wheel_delta_y;
  222. m_input_event_queue.tail().event = move(mutable_event);
  223. ++m_input_event_queue.tail().coalesced_event_count;
  224. return;
  225. }
  226. enqueue_input_event({ page_id, move(const_cast<Web::MouseEvent&>(event)), 0 });
  227. }
  228. void ConnectionFromClient::drag_event(u64 page_id, Web::DragEvent const& event)
  229. {
  230. enqueue_input_event({ page_id, move(const_cast<Web::DragEvent&>(event)), 0 });
  231. }
  232. void ConnectionFromClient::enqueue_input_event(QueuedInputEvent event)
  233. {
  234. m_input_event_queue.enqueue(move(event));
  235. m_input_event_queue_timer->start();
  236. }
  237. void ConnectionFromClient::report_finished_handling_input_event(u64 page_id, Web::EventResult event_result)
  238. {
  239. async_did_finish_handling_input_event(page_id, event_result);
  240. }
  241. void ConnectionFromClient::debug_request(u64 page_id, ByteString const& request, ByteString const& argument)
  242. {
  243. auto page = this->page(page_id);
  244. if (!page.has_value())
  245. return;
  246. if (request == "dump-session-history") {
  247. auto const& traversable = page->page().top_level_traversable();
  248. Web::dump_tree(*traversable);
  249. }
  250. if (request == "dump-dom-tree") {
  251. if (auto* doc = page->page().top_level_browsing_context().active_document())
  252. Web::dump_tree(*doc);
  253. return;
  254. }
  255. if (request == "dump-layout-tree") {
  256. if (auto* doc = page->page().top_level_browsing_context().active_document()) {
  257. if (auto* viewport = doc->layout_node())
  258. Web::dump_tree(*viewport);
  259. }
  260. return;
  261. }
  262. if (request == "dump-paint-tree") {
  263. if (auto* doc = page->page().top_level_browsing_context().active_document()) {
  264. if (auto* paintable = doc->paintable())
  265. Web::dump_tree(*paintable);
  266. }
  267. return;
  268. }
  269. if (request == "dump-stacking-context-tree") {
  270. if (auto* doc = page->page().top_level_browsing_context().active_document()) {
  271. if (auto* viewport = doc->layout_node()) {
  272. if (auto* stacking_context = viewport->paintable_box()->stacking_context())
  273. stacking_context->dump();
  274. }
  275. }
  276. return;
  277. }
  278. if (request == "dump-style-sheets") {
  279. if (auto* doc = page->page().top_level_browsing_context().active_document()) {
  280. dbgln("=== In document: ===");
  281. for (auto& sheet : doc->style_sheets().sheets()) {
  282. Web::dump_sheet(sheet);
  283. }
  284. doc->for_each_shadow_root([&](auto& shadow_root) {
  285. dbgln("=== In shadow root {}: ===", shadow_root.host()->debug_description());
  286. shadow_root.for_each_css_style_sheet([&](auto& sheet) {
  287. Web::dump_sheet(sheet);
  288. });
  289. });
  290. }
  291. return;
  292. }
  293. if (request == "dump-all-resolved-styles") {
  294. if (auto* doc = page->page().top_level_browsing_context().active_document()) {
  295. Queue<Web::DOM::Node*> elements_to_visit;
  296. elements_to_visit.enqueue(doc->document_element());
  297. while (!elements_to_visit.is_empty()) {
  298. auto element = elements_to_visit.dequeue();
  299. for (auto& child : element->children_as_vector())
  300. elements_to_visit.enqueue(child.ptr());
  301. if (element->is_element()) {
  302. auto styles = doc->style_computer().compute_style(*static_cast<Web::DOM::Element*>(element));
  303. dbgln("+ Element {}", element->debug_description());
  304. for (size_t i = 0; i < Web::CSS::StyleProperties::number_of_properties; ++i) {
  305. auto property = styles.maybe_null_property(static_cast<Web::CSS::PropertyID>(i));
  306. dbgln("| {} = {}", Web::CSS::string_from_property_id(static_cast<Web::CSS::PropertyID>(i)), property ? property->to_string(Web::CSS::CSSStyleValue::SerializationMode::Normal) : ""_string);
  307. }
  308. dbgln("---");
  309. }
  310. }
  311. }
  312. return;
  313. }
  314. if (request == "collect-garbage") {
  315. // NOTE: We use deferred_invoke here to ensure that GC runs with as little on the stack as possible.
  316. Core::deferred_invoke([] {
  317. Web::Bindings::main_thread_vm().heap().collect_garbage(GC::Heap::CollectionType::CollectGarbage, true);
  318. });
  319. return;
  320. }
  321. if (request == "set-line-box-borders") {
  322. bool state = argument == "on";
  323. page->set_should_show_line_box_borders(state);
  324. page->page().top_level_traversable()->set_needs_display();
  325. return;
  326. }
  327. if (request == "clear-cache") {
  328. Web::ResourceLoader::the().clear_cache();
  329. return;
  330. }
  331. if (request == "spoof-user-agent") {
  332. Web::ResourceLoader::the().set_user_agent(MUST(String::from_byte_string(argument)));
  333. return;
  334. }
  335. if (request == "same-origin-policy") {
  336. page->page().set_same_origin_policy_enabled(argument == "on");
  337. return;
  338. }
  339. if (request == "scripting") {
  340. page->page().set_is_scripting_enabled(argument == "on");
  341. return;
  342. }
  343. if (request == "block-pop-ups") {
  344. page->page().set_should_block_pop_ups(argument == "on");
  345. return;
  346. }
  347. if (request == "dump-local-storage") {
  348. if (auto* document = page->page().top_level_browsing_context().active_document())
  349. document->window()->local_storage().release_value_but_fixme_should_propagate_errors()->dump();
  350. return;
  351. }
  352. if (request == "load-reference-page") {
  353. if (auto* document = page->page().top_level_browsing_context().active_document()) {
  354. auto has_mismatch_selector = false;
  355. auto maybe_link = [&]() -> Web::WebIDL::ExceptionOr<GC::Ptr<Web::DOM::Element>> {
  356. auto maybe_link = document->query_selector("link[rel=match]"sv);
  357. if (maybe_link.is_error() || maybe_link.value())
  358. return maybe_link;
  359. auto maybe_mismatch_link = document->query_selector("link[rel=mismatch]"sv);
  360. if (maybe_mismatch_link.is_error() || maybe_mismatch_link.value()) {
  361. has_mismatch_selector = maybe_mismatch_link.value();
  362. return maybe_mismatch_link;
  363. }
  364. return nullptr;
  365. }();
  366. if (maybe_link.is_error() || !maybe_link.value()) {
  367. // To make sure that we fail the ref-test if the link is missing, load the error page->
  368. load_html(page_id, "<h1>Failed to find &lt;link rel=&quot;match&quot; /&gt; or &lt;link rel=&quot;mismatch&quot; /&gt; in ref test page!</h1> Make sure you added it.");
  369. } else {
  370. auto link = maybe_link.release_value();
  371. auto url = document->encoding_parse_url(link->get_attribute_value(Web::HTML::AttributeNames::href));
  372. if (url.query().has_value() && !url.query()->is_empty()) {
  373. load_html(page_id, "<h1>Invalid ref test link - query string must be empty</h1>");
  374. return;
  375. }
  376. if (has_mismatch_selector)
  377. url.set_query("mismatch"_string);
  378. load_url(page_id, url);
  379. }
  380. }
  381. return;
  382. }
  383. if (request == "navigator-compatibility-mode") {
  384. Web::NavigatorCompatibilityMode compatibility_mode;
  385. if (argument == "chrome") {
  386. compatibility_mode = Web::NavigatorCompatibilityMode::Chrome;
  387. } else if (argument == "gecko") {
  388. compatibility_mode = Web::NavigatorCompatibilityMode::Gecko;
  389. } else if (argument == "webkit") {
  390. compatibility_mode = Web::NavigatorCompatibilityMode::WebKit;
  391. } else {
  392. dbgln("Unknown navigator compatibility mode '{}', defaulting to Chrome", argument);
  393. compatibility_mode = Web::NavigatorCompatibilityMode::Chrome;
  394. }
  395. Web::ResourceLoader::the().set_navigator_compatibility_mode(compatibility_mode);
  396. return;
  397. }
  398. }
  399. void ConnectionFromClient::get_source(u64 page_id)
  400. {
  401. if (auto page = this->page(page_id); page.has_value()) {
  402. if (auto* doc = page->page().top_level_browsing_context().active_document())
  403. async_did_get_source(page_id, doc->url(), doc->base_url(), doc->source());
  404. }
  405. }
  406. void ConnectionFromClient::inspect_dom_tree(u64 page_id)
  407. {
  408. if (auto page = this->page(page_id); page.has_value()) {
  409. if (auto* doc = page->page().top_level_browsing_context().active_document())
  410. async_did_inspect_dom_tree(page_id, doc->dump_dom_tree_as_json().to_byte_string());
  411. }
  412. }
  413. void ConnectionFromClient::inspect_dom_node(u64 page_id, Web::UniqueNodeID const& node_id, Optional<Web::CSS::Selector::PseudoElement::Type> const& pseudo_element)
  414. {
  415. auto page = this->page(page_id);
  416. if (!page.has_value())
  417. return;
  418. auto& top_context = page->page().top_level_browsing_context();
  419. top_context.for_each_in_inclusive_subtree([&](auto& ctx) {
  420. if (ctx.active_document() != nullptr) {
  421. ctx.active_document()->set_inspected_node(nullptr, {});
  422. }
  423. return Web::TraversalDecision::Continue;
  424. });
  425. auto* node = Web::DOM::Node::from_unique_id(node_id);
  426. // Note: Nodes without layout (aka non-visible nodes, don't have style computed)
  427. if (!node || !node->layout_node()) {
  428. async_did_inspect_dom_node(page_id, false, {}, {}, {}, {}, {}, {});
  429. return;
  430. }
  431. node->document().set_inspected_node(node, pseudo_element);
  432. if (node->is_element()) {
  433. auto& element = verify_cast<Web::DOM::Element>(*node);
  434. if (!element.computed_css_values().has_value()) {
  435. async_did_inspect_dom_node(page_id, false, {}, {}, {}, {}, {}, {});
  436. return;
  437. }
  438. auto serialize_json = [](Web::CSS::StyleProperties const& properties) -> ByteString {
  439. StringBuilder builder;
  440. auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
  441. properties.for_each_property([&](auto property_id, auto& value) {
  442. MUST(serializer.add(Web::CSS::string_from_property_id(property_id), value.to_string(Web::CSS::CSSStyleValue::SerializationMode::Normal).to_byte_string()));
  443. });
  444. MUST(serializer.finish());
  445. return builder.to_byte_string();
  446. };
  447. auto serialize_custom_properties_json = [](Web::DOM::Element const& element, Optional<Web::CSS::Selector::PseudoElement::Type> pseudo_element) -> ByteString {
  448. StringBuilder builder;
  449. auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
  450. HashTable<FlyString> seen_properties;
  451. auto const* element_to_check = &element;
  452. while (element_to_check) {
  453. for (auto const& property : element_to_check->custom_properties(pseudo_element)) {
  454. if (!seen_properties.contains(property.key)) {
  455. seen_properties.set(property.key);
  456. MUST(serializer.add(property.key, property.value.value->to_string(Web::CSS::CSSStyleValue::SerializationMode::Normal)));
  457. }
  458. }
  459. element_to_check = element_to_check->parent_element();
  460. }
  461. MUST(serializer.finish());
  462. return builder.to_byte_string();
  463. };
  464. auto serialize_node_box_sizing_json = [](Web::Layout::Node const* layout_node) -> ByteString {
  465. if (!layout_node || !layout_node->is_box()) {
  466. return "{}";
  467. }
  468. auto* box = static_cast<Web::Layout::Box const*>(layout_node);
  469. auto box_model = box->box_model();
  470. StringBuilder builder;
  471. auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
  472. MUST(serializer.add("padding_top"sv, box_model.padding.top.to_double()));
  473. MUST(serializer.add("padding_right"sv, box_model.padding.right.to_double()));
  474. MUST(serializer.add("padding_bottom"sv, box_model.padding.bottom.to_double()));
  475. MUST(serializer.add("padding_left"sv, box_model.padding.left.to_double()));
  476. MUST(serializer.add("margin_top"sv, box_model.margin.top.to_double()));
  477. MUST(serializer.add("margin_right"sv, box_model.margin.right.to_double()));
  478. MUST(serializer.add("margin_bottom"sv, box_model.margin.bottom.to_double()));
  479. MUST(serializer.add("margin_left"sv, box_model.margin.left.to_double()));
  480. MUST(serializer.add("border_top"sv, box_model.border.top.to_double()));
  481. MUST(serializer.add("border_right"sv, box_model.border.right.to_double()));
  482. MUST(serializer.add("border_bottom"sv, box_model.border.bottom.to_double()));
  483. MUST(serializer.add("border_left"sv, box_model.border.left.to_double()));
  484. if (auto* paintable_box = box->paintable_box()) {
  485. MUST(serializer.add("content_width"sv, paintable_box->content_width().to_double()));
  486. MUST(serializer.add("content_height"sv, paintable_box->content_height().to_double()));
  487. } else {
  488. MUST(serializer.add("content_width"sv, 0));
  489. MUST(serializer.add("content_height"sv, 0));
  490. }
  491. MUST(serializer.finish());
  492. return builder.to_byte_string();
  493. };
  494. auto serialize_aria_properties_state_json = [](Web::DOM::Element const& element) -> ByteString {
  495. auto role_name = element.role_or_default();
  496. if (!role_name.has_value()) {
  497. return "";
  498. }
  499. auto aria_data = MUST(Web::ARIA::AriaData::build_data(element));
  500. auto role = MUST(Web::ARIA::RoleType::build_role_object(role_name.value(), element.is_focusable(), *aria_data));
  501. StringBuilder builder;
  502. auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
  503. MUST(role->serialize_as_json(serializer));
  504. MUST(serializer.finish());
  505. return builder.to_byte_string();
  506. };
  507. auto serialize_fonts_json = [](Web::CSS::StyleProperties const& properties) -> ByteString {
  508. StringBuilder builder;
  509. auto serializer = MUST(JsonArraySerializer<>::try_create(builder));
  510. auto const& font_list = properties.computed_font_list();
  511. font_list.for_each_font_entry([&serializer](Gfx::FontCascadeList::Entry const& entry) {
  512. auto const& font = entry.font;
  513. auto font_json_object = MUST(serializer.add_object());
  514. MUST(font_json_object.add("name"sv, font->family()));
  515. MUST(font_json_object.add("size"sv, font->point_size()));
  516. MUST(font_json_object.add("weight"sv, font->weight()));
  517. MUST(font_json_object.finish());
  518. });
  519. MUST(serializer.finish());
  520. return builder.to_byte_string();
  521. };
  522. if (pseudo_element.has_value()) {
  523. auto pseudo_element_node = element.get_pseudo_element_node(pseudo_element.value());
  524. if (!pseudo_element_node) {
  525. async_did_inspect_dom_node(page_id, false, {}, {}, {}, {}, {}, {});
  526. return;
  527. }
  528. auto pseudo_element_style = element.pseudo_element_computed_css_values(pseudo_element.value());
  529. ByteString computed_values = serialize_json(*pseudo_element_style);
  530. ByteString resolved_values = serialize_json(element.resolved_css_values(pseudo_element.value()));
  531. ByteString custom_properties_json = serialize_custom_properties_json(element, pseudo_element);
  532. ByteString node_box_sizing_json = serialize_node_box_sizing_json(pseudo_element_node.ptr());
  533. ByteString fonts_json = serialize_fonts_json(*pseudo_element_style);
  534. async_did_inspect_dom_node(page_id, true, move(computed_values), move(resolved_values), move(custom_properties_json), move(node_box_sizing_json), {}, move(fonts_json));
  535. return;
  536. }
  537. ByteString computed_values = serialize_json(*element.computed_css_values());
  538. ByteString resolved_values = serialize_json(element.resolved_css_values());
  539. ByteString custom_properties_json = serialize_custom_properties_json(element, {});
  540. ByteString node_box_sizing_json = serialize_node_box_sizing_json(element.layout_node());
  541. ByteString aria_properties_state_json = serialize_aria_properties_state_json(element);
  542. ByteString fonts_json = serialize_fonts_json(*element.computed_css_values());
  543. async_did_inspect_dom_node(page_id, true, move(computed_values), move(resolved_values), move(custom_properties_json), move(node_box_sizing_json), move(aria_properties_state_json), move(fonts_json));
  544. return;
  545. }
  546. async_did_inspect_dom_node(page_id, false, {}, {}, {}, {}, {}, {});
  547. }
  548. void ConnectionFromClient::inspect_accessibility_tree(u64 page_id)
  549. {
  550. if (auto page = this->page(page_id); page.has_value()) {
  551. if (auto* doc = page->page().top_level_browsing_context().active_document())
  552. async_did_inspect_accessibility_tree(page_id, doc->dump_accessibility_tree_as_json().to_byte_string());
  553. }
  554. }
  555. void ConnectionFromClient::get_hovered_node_id(u64 page_id)
  556. {
  557. auto page = this->page(page_id);
  558. if (!page.has_value())
  559. return;
  560. Web::UniqueNodeID node_id = 0;
  561. if (auto* document = page->page().top_level_browsing_context().active_document()) {
  562. if (auto* hovered_node = document->hovered_node())
  563. node_id = hovered_node->unique_id();
  564. }
  565. async_did_get_hovered_node_id(page_id, node_id);
  566. }
  567. void ConnectionFromClient::list_style_sheets(u64 page_id)
  568. {
  569. auto page = this->page(page_id);
  570. if (!page.has_value())
  571. return;
  572. async_inspector_did_list_style_sheets(page_id, page->list_style_sheets());
  573. }
  574. void ConnectionFromClient::request_style_sheet_source(u64 page_id, Web::CSS::StyleSheetIdentifier const& identifier)
  575. {
  576. auto page = this->page(page_id);
  577. if (!page.has_value())
  578. return;
  579. if (auto* document = page->page().top_level_browsing_context().active_document()) {
  580. if (auto stylesheet = document->get_style_sheet_source(identifier); stylesheet.has_value())
  581. async_did_get_style_sheet_source(page_id, identifier, document->base_url(), stylesheet.value());
  582. }
  583. }
  584. void ConnectionFromClient::set_dom_node_text(u64 page_id, Web::UniqueNodeID const& node_id, String const& text)
  585. {
  586. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  587. if (!dom_node || (!dom_node->is_text() && !dom_node->is_comment())) {
  588. async_did_finish_editing_dom_node(page_id, {});
  589. return;
  590. }
  591. auto& character_data = static_cast<Web::DOM::CharacterData&>(*dom_node);
  592. character_data.set_data(text);
  593. async_did_finish_editing_dom_node(page_id, character_data.unique_id());
  594. }
  595. void ConnectionFromClient::set_dom_node_tag(u64 page_id, Web::UniqueNodeID const& node_id, String const& name)
  596. {
  597. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  598. if (!dom_node || !dom_node->is_element() || !dom_node->parent()) {
  599. async_did_finish_editing_dom_node(page_id, {});
  600. return;
  601. }
  602. auto& element = static_cast<Web::DOM::Element&>(*dom_node);
  603. auto new_element = Web::DOM::create_element(element.document(), name, element.namespace_uri(), element.prefix(), element.is_value()).release_value_but_fixme_should_propagate_errors();
  604. element.for_each_attribute([&](auto const& attribute) {
  605. new_element->set_attribute_value(attribute.local_name(), attribute.value(), attribute.prefix(), attribute.namespace_uri());
  606. });
  607. while (auto* child_node = element.first_child()) {
  608. MUST(element.remove_child(*child_node));
  609. MUST(new_element->append_child(*child_node));
  610. }
  611. element.parent()->replace_child(*new_element, element).release_value_but_fixme_should_propagate_errors();
  612. async_did_finish_editing_dom_node(page_id, new_element->unique_id());
  613. }
  614. void ConnectionFromClient::add_dom_node_attributes(u64 page_id, Web::UniqueNodeID const& node_id, Vector<WebView::Attribute> const& attributes)
  615. {
  616. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  617. if (!dom_node || !dom_node->is_element()) {
  618. async_did_finish_editing_dom_node(page_id, {});
  619. return;
  620. }
  621. auto& element = static_cast<Web::DOM::Element&>(*dom_node);
  622. for (auto const& attribute : attributes) {
  623. // NOTE: We ignore invalid attributes for now, but we may want to send feedback to the user that this failed.
  624. (void)element.set_attribute(attribute.name, attribute.value);
  625. }
  626. async_did_finish_editing_dom_node(page_id, element.unique_id());
  627. }
  628. void ConnectionFromClient::replace_dom_node_attribute(u64 page_id, Web::UniqueNodeID const& node_id, String const& name, Vector<WebView::Attribute> const& replacement_attributes)
  629. {
  630. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  631. if (!dom_node || !dom_node->is_element()) {
  632. async_did_finish_editing_dom_node(page_id, {});
  633. return;
  634. }
  635. auto& element = static_cast<Web::DOM::Element&>(*dom_node);
  636. bool should_remove_attribute = true;
  637. for (auto const& attribute : replacement_attributes) {
  638. if (should_remove_attribute && Web::Infra::is_ascii_case_insensitive_match(name, attribute.name))
  639. should_remove_attribute = false;
  640. // NOTE: We ignore invalid attributes for now, but we may want to send feedback to the user that this failed.
  641. (void)element.set_attribute(attribute.name, attribute.value);
  642. }
  643. if (should_remove_attribute)
  644. element.remove_attribute(name);
  645. async_did_finish_editing_dom_node(page_id, element.unique_id());
  646. }
  647. void ConnectionFromClient::create_child_element(u64 page_id, Web::UniqueNodeID const& node_id)
  648. {
  649. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  650. if (!dom_node) {
  651. async_did_finish_editing_dom_node(page_id, {});
  652. return;
  653. }
  654. auto element = Web::DOM::create_element(dom_node->document(), Web::HTML::TagNames::div, Web::Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  655. dom_node->append_child(element).release_value_but_fixme_should_propagate_errors();
  656. async_did_finish_editing_dom_node(page_id, element->unique_id());
  657. }
  658. void ConnectionFromClient::create_child_text_node(u64 page_id, Web::UniqueNodeID const& node_id)
  659. {
  660. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  661. if (!dom_node) {
  662. async_did_finish_editing_dom_node(page_id, {});
  663. return;
  664. }
  665. auto text_node = dom_node->realm().create<Web::DOM::Text>(dom_node->document(), "text"_string);
  666. dom_node->append_child(text_node).release_value_but_fixme_should_propagate_errors();
  667. async_did_finish_editing_dom_node(page_id, text_node->unique_id());
  668. }
  669. void ConnectionFromClient::clone_dom_node(u64 page_id, Web::UniqueNodeID const& node_id)
  670. {
  671. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  672. if (!dom_node || !dom_node->parent_node()) {
  673. async_did_finish_editing_dom_node(page_id, {});
  674. return;
  675. }
  676. auto dom_node_clone = MUST(dom_node->clone_node(nullptr, true));
  677. dom_node->parent_node()->insert_before(dom_node_clone, dom_node->next_sibling());
  678. async_did_finish_editing_dom_node(page_id, dom_node_clone->unique_id());
  679. }
  680. void ConnectionFromClient::remove_dom_node(u64 page_id, Web::UniqueNodeID const& node_id)
  681. {
  682. auto page = this->page(page_id);
  683. if (!page.has_value())
  684. return;
  685. auto* active_document = page->page().top_level_browsing_context().active_document();
  686. if (!active_document) {
  687. async_did_finish_editing_dom_node(page_id, {});
  688. return;
  689. }
  690. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  691. if (!dom_node) {
  692. async_did_finish_editing_dom_node(page_id, {});
  693. return;
  694. }
  695. auto* previous_dom_node = dom_node->previous_sibling();
  696. if (!previous_dom_node)
  697. previous_dom_node = dom_node->parent();
  698. dom_node->remove();
  699. async_did_finish_editing_dom_node(page_id, previous_dom_node->unique_id());
  700. }
  701. void ConnectionFromClient::get_dom_node_html(u64 page_id, Web::UniqueNodeID const& node_id)
  702. {
  703. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  704. if (!dom_node)
  705. return;
  706. String html;
  707. if (dom_node->is_element()) {
  708. auto const& element = static_cast<Web::DOM::Element const&>(*dom_node);
  709. html = element.outer_html().release_value_but_fixme_should_propagate_errors();
  710. } else if (dom_node->is_text() || dom_node->is_comment()) {
  711. auto const& character_data = static_cast<Web::DOM::CharacterData const&>(*dom_node);
  712. html = character_data.data();
  713. } else {
  714. return;
  715. }
  716. async_did_get_dom_node_html(page_id, move(html));
  717. }
  718. void ConnectionFromClient::take_document_screenshot(u64 page_id)
  719. {
  720. auto page = this->page(page_id);
  721. if (!page.has_value())
  722. return;
  723. page->queue_screenshot_task({});
  724. }
  725. void ConnectionFromClient::take_dom_node_screenshot(u64 page_id, Web::UniqueNodeID const& node_id)
  726. {
  727. auto page = this->page(page_id);
  728. if (!page.has_value())
  729. return;
  730. page->queue_screenshot_task(node_id);
  731. }
  732. static void append_page_text(Web::Page& page, StringBuilder& builder)
  733. {
  734. auto* document = page.top_level_browsing_context().active_document();
  735. if (!document) {
  736. builder.append("(no DOM tree)"sv);
  737. return;
  738. }
  739. auto* body = document->body();
  740. if (!body) {
  741. builder.append("(no body)"sv);
  742. return;
  743. }
  744. builder.append(body->inner_text());
  745. }
  746. static void append_layout_tree(Web::Page& page, StringBuilder& builder)
  747. {
  748. auto* document = page.top_level_browsing_context().active_document();
  749. if (!document) {
  750. builder.append("(no DOM tree)"sv);
  751. return;
  752. }
  753. document->update_layout();
  754. auto* layout_root = document->layout_node();
  755. if (!layout_root) {
  756. builder.append("(no layout tree)"sv);
  757. return;
  758. }
  759. Web::dump_tree(builder, *layout_root);
  760. }
  761. static void append_paint_tree(Web::Page& page, StringBuilder& builder)
  762. {
  763. auto* document = page.top_level_browsing_context().active_document();
  764. if (!document) {
  765. builder.append("(no DOM tree)"sv);
  766. return;
  767. }
  768. document->update_layout();
  769. auto* layout_root = document->layout_node();
  770. if (!layout_root) {
  771. builder.append("(no layout tree)"sv);
  772. return;
  773. }
  774. if (!layout_root->first_paintable()) {
  775. builder.append("(no paint tree)"sv);
  776. return;
  777. }
  778. Web::dump_tree(builder, *layout_root->first_paintable());
  779. }
  780. static void append_gc_graph(StringBuilder& builder)
  781. {
  782. auto gc_graph = Web::Bindings::main_thread_vm().heap().dump_graph();
  783. gc_graph.serialize(builder);
  784. }
  785. void ConnectionFromClient::request_internal_page_info(u64 page_id, WebView::PageInfoType type)
  786. {
  787. auto page = this->page(page_id);
  788. if (!page.has_value()) {
  789. async_did_get_internal_page_info(page_id, type, "(no page)"_string);
  790. return;
  791. }
  792. StringBuilder builder;
  793. if (has_flag(type, WebView::PageInfoType::Text)) {
  794. append_page_text(page->page(), builder);
  795. }
  796. if (has_flag(type, WebView::PageInfoType::LayoutTree)) {
  797. if (!builder.is_empty())
  798. builder.append("\n"sv);
  799. append_layout_tree(page->page(), builder);
  800. }
  801. if (has_flag(type, WebView::PageInfoType::PaintTree)) {
  802. if (!builder.is_empty())
  803. builder.append("\n"sv);
  804. append_paint_tree(page->page(), builder);
  805. }
  806. if (has_flag(type, WebView::PageInfoType::GCGraph)) {
  807. if (!builder.is_empty())
  808. builder.append("\n"sv);
  809. append_gc_graph(builder);
  810. }
  811. async_did_get_internal_page_info(page_id, type, MUST(builder.to_string()));
  812. }
  813. Messages::WebContentServer::GetSelectedTextResponse ConnectionFromClient::get_selected_text(u64 page_id)
  814. {
  815. if (auto page = this->page(page_id); page.has_value())
  816. return page->page().focused_navigable().selected_text().to_byte_string();
  817. return ByteString {};
  818. }
  819. void ConnectionFromClient::select_all(u64 page_id)
  820. {
  821. if (auto page = this->page(page_id); page.has_value())
  822. page->page().focused_navigable().select_all();
  823. }
  824. void ConnectionFromClient::find_in_page(u64 page_id, String const& query, CaseSensitivity case_sensitivity)
  825. {
  826. auto page = this->page(page_id);
  827. if (!page.has_value())
  828. return;
  829. auto result = page->page().find_in_page({ .string = query, .case_sensitivity = case_sensitivity });
  830. async_did_find_in_page(page_id, result.current_match_index, result.total_match_count);
  831. }
  832. void ConnectionFromClient::find_in_page_next_match(u64 page_id)
  833. {
  834. auto page = this->page(page_id);
  835. if (!page.has_value())
  836. return;
  837. auto result = page->page().find_in_page_next_match();
  838. async_did_find_in_page(page_id, result.current_match_index, result.total_match_count);
  839. }
  840. void ConnectionFromClient::find_in_page_previous_match(u64 page_id)
  841. {
  842. auto page = this->page(page_id);
  843. if (!page.has_value())
  844. return;
  845. auto result = page->page().find_in_page_previous_match();
  846. async_did_find_in_page(page_id, result.current_match_index, result.total_match_count);
  847. }
  848. void ConnectionFromClient::paste(u64 page_id, String const& text)
  849. {
  850. if (auto page = this->page(page_id); page.has_value())
  851. page->page().focused_navigable().paste(text);
  852. }
  853. void ConnectionFromClient::set_content_filters(u64, Vector<String> const& filters)
  854. {
  855. Web::ContentFilter::the().set_patterns(filters).release_value_but_fixme_should_propagate_errors();
  856. }
  857. void ConnectionFromClient::set_autoplay_allowed_on_all_websites(u64)
  858. {
  859. auto& autoplay_allowlist = Web::PermissionsPolicy::AutoplayAllowlist::the();
  860. autoplay_allowlist.enable_globally();
  861. }
  862. void ConnectionFromClient::set_autoplay_allowlist(u64, Vector<String> const& allowlist)
  863. {
  864. auto& autoplay_allowlist = Web::PermissionsPolicy::AutoplayAllowlist::the();
  865. autoplay_allowlist.enable_for_origins(allowlist).release_value_but_fixme_should_propagate_errors();
  866. }
  867. void ConnectionFromClient::set_proxy_mappings(u64, Vector<ByteString> const& proxies, HashMap<ByteString, size_t> const& mappings)
  868. {
  869. auto keys = mappings.keys();
  870. quick_sort(keys, [&](auto& a, auto& b) { return a.length() < b.length(); });
  871. OrderedHashMap<ByteString, size_t> sorted_mappings;
  872. for (auto& key : keys) {
  873. auto value = *mappings.get(key);
  874. if (value >= proxies.size())
  875. continue;
  876. sorted_mappings.set(key, value);
  877. }
  878. Web::ProxyMappings::the().set_mappings(proxies, move(sorted_mappings));
  879. }
  880. void ConnectionFromClient::set_preferred_color_scheme(u64 page_id, Web::CSS::PreferredColorScheme const& color_scheme)
  881. {
  882. if (auto page = this->page(page_id); page.has_value())
  883. page->set_preferred_color_scheme(color_scheme);
  884. }
  885. void ConnectionFromClient::set_preferred_contrast(u64 page_id, Web::CSS::PreferredContrast const& contrast)
  886. {
  887. if (auto page = this->page(page_id); page.has_value())
  888. page->set_preferred_contrast(contrast);
  889. }
  890. void ConnectionFromClient::set_preferred_motion(u64 page_id, Web::CSS::PreferredMotion const& motion)
  891. {
  892. if (auto page = this->page(page_id); page.has_value())
  893. page->set_preferred_motion(motion);
  894. }
  895. void ConnectionFromClient::set_preferred_languages(u64, Vector<String> const& preferred_languages)
  896. {
  897. // FIXME: Whenever the user agent needs to make the navigator.languages attribute of a Window or WorkerGlobalScope
  898. // object global return a new set of language tags, the user agent must queue a global task on the DOM manipulation
  899. // task source given global to fire an event named languagechange at global, and wait until that task begins to be
  900. // executed before actually returning a new value.
  901. Web::ResourceLoader::the().set_preferred_languages(preferred_languages);
  902. }
  903. void ConnectionFromClient::set_enable_do_not_track(u64, bool enable)
  904. {
  905. Web::ResourceLoader::the().set_enable_do_not_track(enable);
  906. }
  907. void ConnectionFromClient::set_has_focus(u64 page_id, bool has_focus)
  908. {
  909. if (auto page = this->page(page_id); page.has_value())
  910. page->set_has_focus(has_focus);
  911. }
  912. void ConnectionFromClient::set_is_scripting_enabled(u64 page_id, bool is_scripting_enabled)
  913. {
  914. if (auto page = this->page(page_id); page.has_value())
  915. page->set_is_scripting_enabled(is_scripting_enabled);
  916. }
  917. void ConnectionFromClient::set_device_pixels_per_css_pixel(u64 page_id, float device_pixels_per_css_pixel)
  918. {
  919. if (auto page = this->page(page_id); page.has_value())
  920. page->set_device_pixels_per_css_pixel(device_pixels_per_css_pixel);
  921. }
  922. void ConnectionFromClient::set_window_position(u64 page_id, Web::DevicePixelPoint position)
  923. {
  924. if (auto page = this->page(page_id); page.has_value())
  925. page->set_window_position(position);
  926. }
  927. void ConnectionFromClient::set_window_size(u64 page_id, Web::DevicePixelSize size)
  928. {
  929. if (auto page = this->page(page_id); page.has_value())
  930. page->set_window_size(size);
  931. }
  932. void ConnectionFromClient::did_update_window_rect(u64 page_id)
  933. {
  934. if (auto page = this->page(page_id); page.has_value())
  935. page->page().did_update_window_rect();
  936. }
  937. Messages::WebContentServer::GetLocalStorageEntriesResponse ConnectionFromClient::get_local_storage_entries(u64 page_id)
  938. {
  939. auto page = this->page(page_id);
  940. if (!page.has_value())
  941. return OrderedHashMap<String, String> {};
  942. auto* document = page->page().top_level_browsing_context().active_document();
  943. auto local_storage = document->window()->local_storage().release_value_but_fixme_should_propagate_errors();
  944. return local_storage->map();
  945. }
  946. Messages::WebContentServer::GetSessionStorageEntriesResponse ConnectionFromClient::get_session_storage_entries(u64 page_id)
  947. {
  948. auto page = this->page(page_id);
  949. if (!page.has_value())
  950. return OrderedHashMap<String, String> {};
  951. auto* document = page->page().top_level_browsing_context().active_document();
  952. auto session_storage = document->window()->session_storage().release_value_but_fixme_should_propagate_errors();
  953. return session_storage->map();
  954. }
  955. void ConnectionFromClient::handle_file_return(u64, i32 error, Optional<IPC::File> const& file, i32 request_id)
  956. {
  957. auto file_request = m_requested_files.take(request_id);
  958. VERIFY(file_request.has_value());
  959. VERIFY(file_request.value().on_file_request_finish);
  960. file_request.value().on_file_request_finish(error != 0 ? Error::from_errno(error) : ErrorOr<i32> { file->take_fd() });
  961. }
  962. void ConnectionFromClient::request_file(u64 page_id, Web::FileRequest file_request)
  963. {
  964. i32 const id = last_id++;
  965. auto path = file_request.path();
  966. m_requested_files.set(id, move(file_request));
  967. async_did_request_file(page_id, path, id);
  968. }
  969. void ConnectionFromClient::set_system_visibility_state(u64 page_id, Web::HTML::VisibilityState visibility_state)
  970. {
  971. if (auto page = this->page(page_id); page.has_value())
  972. page->page().top_level_traversable()->set_system_visibility_state(visibility_state);
  973. }
  974. void ConnectionFromClient::js_console_input(u64 page_id, ByteString const& js_source)
  975. {
  976. auto page = this->page(page_id);
  977. if (!page.has_value())
  978. return;
  979. page->js_console_input(js_source);
  980. }
  981. void ConnectionFromClient::run_javascript(u64 page_id, ByteString const& js_source)
  982. {
  983. if (auto page = this->page(page_id); page.has_value())
  984. page->run_javascript(js_source);
  985. }
  986. void ConnectionFromClient::js_console_request_messages(u64 page_id, i32 start_index)
  987. {
  988. if (auto page = this->page(page_id); page.has_value())
  989. page->js_console_request_messages(start_index);
  990. }
  991. void ConnectionFromClient::alert_closed(u64 page_id)
  992. {
  993. if (auto page = this->page(page_id); page.has_value())
  994. page->page().alert_closed();
  995. }
  996. void ConnectionFromClient::confirm_closed(u64 page_id, bool accepted)
  997. {
  998. if (auto page = this->page(page_id); page.has_value())
  999. page->page().confirm_closed(accepted);
  1000. }
  1001. void ConnectionFromClient::prompt_closed(u64 page_id, Optional<String> const& response)
  1002. {
  1003. if (auto page = this->page(page_id); page.has_value())
  1004. page->page().prompt_closed(response);
  1005. }
  1006. void ConnectionFromClient::color_picker_update(u64 page_id, Optional<Color> const& picked_color, Web::HTML::ColorPickerUpdateState const& state)
  1007. {
  1008. if (auto page = this->page(page_id); page.has_value())
  1009. page->page().color_picker_update(picked_color, state);
  1010. }
  1011. void ConnectionFromClient::file_picker_closed(u64 page_id, Vector<Web::HTML::SelectedFile> const& selected_files)
  1012. {
  1013. if (auto page = this->page(page_id); page.has_value())
  1014. page->page().file_picker_closed(const_cast<Vector<Web::HTML::SelectedFile>&>(selected_files));
  1015. }
  1016. void ConnectionFromClient::select_dropdown_closed(u64 page_id, Optional<u32> const& selected_item_id)
  1017. {
  1018. if (auto page = this->page(page_id); page.has_value())
  1019. page->page().select_dropdown_closed(selected_item_id);
  1020. }
  1021. void ConnectionFromClient::toggle_media_play_state(u64 page_id)
  1022. {
  1023. if (auto page = this->page(page_id); page.has_value())
  1024. page->page().toggle_media_play_state().release_value_but_fixme_should_propagate_errors();
  1025. }
  1026. void ConnectionFromClient::toggle_media_mute_state(u64 page_id)
  1027. {
  1028. if (auto page = this->page(page_id); page.has_value())
  1029. page->page().toggle_media_mute_state();
  1030. }
  1031. void ConnectionFromClient::toggle_media_loop_state(u64 page_id)
  1032. {
  1033. if (auto page = this->page(page_id); page.has_value())
  1034. page->page().toggle_media_loop_state().release_value_but_fixme_should_propagate_errors();
  1035. }
  1036. void ConnectionFromClient::toggle_media_controls_state(u64 page_id)
  1037. {
  1038. if (auto page = this->page(page_id); page.has_value())
  1039. page->page().toggle_media_controls_state().release_value_but_fixme_should_propagate_errors();
  1040. }
  1041. void ConnectionFromClient::toggle_page_mute_state(u64 page_id)
  1042. {
  1043. if (auto page = this->page(page_id); page.has_value())
  1044. page->page().toggle_page_mute_state();
  1045. }
  1046. void ConnectionFromClient::set_user_style(u64 page_id, String const& source)
  1047. {
  1048. if (auto page = this->page(page_id); page.has_value())
  1049. page->page().set_user_style(source);
  1050. }
  1051. void ConnectionFromClient::enable_inspector_prototype(u64)
  1052. {
  1053. Web::HTML::Window::set_inspector_object_exposed(true);
  1054. }
  1055. void ConnectionFromClient::system_time_zone_changed()
  1056. {
  1057. JS::clear_system_time_zone_cache();
  1058. Unicode::clear_system_time_zone_cache();
  1059. }
  1060. }