ConnectionFromClient.cpp 46 KB

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