ConnectionFromClient.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. /*
  2. * Copyright (c) 2020-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.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 <LibGfx/Bitmap.h>
  14. #include <LibGfx/Font/FontDatabase.h>
  15. #include <LibGfx/SystemTheme.h>
  16. #include <LibJS/Heap/Heap.h>
  17. #include <LibJS/Runtime/ConsoleObject.h>
  18. #include <LibWeb/ARIA/RoleType.h>
  19. #include <LibWeb/Bindings/MainThreadVM.h>
  20. #include <LibWeb/CSS/StyleComputer.h>
  21. #include <LibWeb/DOM/Attr.h>
  22. #include <LibWeb/DOM/CharacterData.h>
  23. #include <LibWeb/DOM/Document.h>
  24. #include <LibWeb/DOM/Element.h>
  25. #include <LibWeb/DOM/ElementFactory.h>
  26. #include <LibWeb/DOM/Text.h>
  27. #include <LibWeb/Dump.h>
  28. #include <LibWeb/HTML/BrowsingContext.h>
  29. #include <LibWeb/HTML/HTMLInputElement.h>
  30. #include <LibWeb/HTML/SelectedFile.h>
  31. #include <LibWeb/HTML/Storage.h>
  32. #include <LibWeb/HTML/TraversableNavigable.h>
  33. #include <LibWeb/HTML/Window.h>
  34. #include <LibWeb/Infra/Strings.h>
  35. #include <LibWeb/Layout/Viewport.h>
  36. #include <LibWeb/Loader/ContentFilter.h>
  37. #include <LibWeb/Loader/ProxyMappings.h>
  38. #include <LibWeb/Loader/ResourceLoader.h>
  39. #include <LibWeb/Namespace.h>
  40. #include <LibWeb/Painting/StackingContext.h>
  41. #include <LibWeb/Painting/ViewportPaintable.h>
  42. #include <LibWeb/PermissionsPolicy/AutoplayAllowlist.h>
  43. #include <LibWeb/Platform/EventLoopPlugin.h>
  44. #include <LibWebView/Attribute.h>
  45. #include <WebContent/ConnectionFromClient.h>
  46. #include <WebContent/PageClient.h>
  47. #include <WebContent/PageHost.h>
  48. #include <WebContent/WebContentClientEndpoint.h>
  49. #ifdef AK_OS_SERENITY
  50. # include <pthread.h>
  51. #endif
  52. namespace WebContent {
  53. ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket> socket)
  54. : IPC::ConnectionFromClient<WebContentClientEndpoint, WebContentServerEndpoint>(*this, move(socket), 1)
  55. , m_page_host(PageHost::create(*this))
  56. {
  57. m_input_event_queue_timer = Web::Platform::Timer::create_single_shot(0, [this] { process_next_input_event(); });
  58. async_notify_process_information({ ::getpid() });
  59. }
  60. void ConnectionFromClient::die()
  61. {
  62. Web::Platform::EventLoopPlugin::the().quit();
  63. }
  64. Optional<PageClient&> ConnectionFromClient::page(u64 index, SourceLocation location)
  65. {
  66. if (auto page = m_page_host->page(index); page.has_value())
  67. return *page;
  68. dbgln("ConnectionFromClient::{}: Did not find a page with ID {}", location.function_name(), index);
  69. return {};
  70. }
  71. Optional<PageClient const&> ConnectionFromClient::page(u64 index, SourceLocation location) const
  72. {
  73. if (auto page = m_page_host->page(index); page.has_value())
  74. return *page;
  75. dbgln("ConnectionFromClient::{}: Did not find a page with ID {}", location.function_name(), index);
  76. return {};
  77. }
  78. Messages::WebContentServer::GetWindowHandleResponse ConnectionFromClient::get_window_handle(u64 page_id)
  79. {
  80. if (auto page = this->page(page_id); page.has_value())
  81. return page->page().top_level_traversable()->window_handle();
  82. return String {};
  83. }
  84. void ConnectionFromClient::set_window_handle(u64 page_id, String const& handle)
  85. {
  86. if (auto page = this->page(page_id); page.has_value())
  87. page->page().top_level_traversable()->set_window_handle(handle);
  88. }
  89. void ConnectionFromClient::connect_to_webdriver(u64 page_id, ByteString const& webdriver_ipc_path)
  90. {
  91. if (auto page = this->page(page_id); page.has_value()) {
  92. // FIXME: Propagate this error back to the browser.
  93. if (auto result = page->connect_to_webdriver(webdriver_ipc_path); result.is_error())
  94. dbgln("Unable to connect to the WebDriver process: {}", result.error());
  95. }
  96. }
  97. void ConnectionFromClient::update_system_theme(u64 page_id, Core::AnonymousBuffer const& theme_buffer)
  98. {
  99. auto page = this->page(page_id);
  100. if (!page.has_value())
  101. return;
  102. Gfx::set_system_theme(theme_buffer);
  103. auto impl = Gfx::PaletteImpl::create_with_anonymous_buffer(theme_buffer);
  104. page->set_palette_impl(*impl);
  105. }
  106. void ConnectionFromClient::update_system_fonts(u64, ByteString const& default_font_query, ByteString const& fixed_width_font_query, ByteString const& window_title_font_query)
  107. {
  108. Gfx::FontDatabase::set_default_font_query(default_font_query);
  109. Gfx::FontDatabase::set_fixed_width_font_query(fixed_width_font_query);
  110. Gfx::FontDatabase::set_window_title_font_query(window_title_font_query);
  111. }
  112. void ConnectionFromClient::update_screen_rects(u64 page_id, Vector<Web::DevicePixelRect> const& rects, u32 main_screen)
  113. {
  114. if (auto page = this->page(page_id); page.has_value())
  115. page->set_screen_rects(rects, main_screen);
  116. }
  117. void ConnectionFromClient::load_url(u64 page_id, const URL::URL& url)
  118. {
  119. auto page = this->page(page_id);
  120. if (!page.has_value())
  121. return;
  122. #if defined(AK_OS_SERENITY)
  123. ByteString process_name;
  124. if (url.host().has<Empty>() || url.host() == String {})
  125. process_name = "WebContent";
  126. else
  127. process_name = ByteString::formatted("WebContent: {}", url.serialized_host().release_value_but_fixme_should_propagate_errors());
  128. pthread_setname_np(pthread_self(), process_name.characters());
  129. #endif
  130. page->page().load(url);
  131. }
  132. void ConnectionFromClient::load_html(u64 page_id, ByteString const& html)
  133. {
  134. if (auto page = this->page(page_id); page.has_value())
  135. page->page().load_html(html);
  136. }
  137. void ConnectionFromClient::set_viewport_rect(u64 page_id, Web::DevicePixelRect const& rect)
  138. {
  139. if (auto page = this->page(page_id); page.has_value())
  140. page->set_viewport_rect(rect);
  141. }
  142. void ConnectionFromClient::add_backing_store(u64 page_id, i32 front_bitmap_id, Gfx::ShareableBitmap const& front_bitmap, i32 back_bitmap_id, Gfx::ShareableBitmap const& back_bitmap)
  143. {
  144. if (auto page = this->page(page_id); page.has_value())
  145. page->add_backing_store(front_bitmap_id, front_bitmap, back_bitmap_id, back_bitmap);
  146. }
  147. void ConnectionFromClient::ready_to_paint(u64 page_id)
  148. {
  149. if (auto page = this->page(page_id); page.has_value())
  150. page->ready_to_paint();
  151. }
  152. void ConnectionFromClient::process_next_input_event()
  153. {
  154. if (m_input_event_queue.is_empty())
  155. return;
  156. auto event = m_input_event_queue.dequeue();
  157. auto page = this->page(event.page_id);
  158. if (!page.has_value())
  159. return;
  160. auto handled = event.event.visit(
  161. [&](Web::KeyEvent const& event) {
  162. switch (event.type) {
  163. case Web::KeyEvent::Type::KeyDown:
  164. return page->page().handle_keydown(event.key, event.modifiers, event.code_point);
  165. case Web::KeyEvent::Type::KeyUp:
  166. return page->page().handle_keyup(event.key, event.modifiers, event.code_point);
  167. }
  168. VERIFY_NOT_REACHED();
  169. },
  170. [&](Web::MouseEvent const& event) {
  171. switch (event.type) {
  172. case Web::MouseEvent::Type::MouseDown:
  173. return page->page().handle_mousedown(event.position, event.screen_position, event.button, event.buttons, event.modifiers);
  174. case Web::MouseEvent::Type::MouseUp:
  175. return page->page().handle_mouseup(event.position, event.screen_position, event.button, event.buttons, event.modifiers);
  176. case Web::MouseEvent::Type::MouseMove:
  177. return page->page().handle_mousemove(event.position, event.screen_position, event.buttons, event.modifiers);
  178. case Web::MouseEvent::Type::MouseWheel:
  179. return page->page().handle_mousewheel(event.position, event.screen_position, event.button, event.buttons, event.modifiers, event.wheel_delta_x, event.wheel_delta_y);
  180. case Web::MouseEvent::Type::DoubleClick:
  181. return page->page().handle_doubleclick(event.position, event.screen_position, event.button, event.buttons, event.modifiers);
  182. }
  183. VERIFY_NOT_REACHED();
  184. });
  185. // We have to notify the client about coalesced events, so we do that by saying none of them were handled by the web page->
  186. for (size_t i = 0; i < event.coalesced_event_count; ++i)
  187. report_finished_handling_input_event(event.page_id, false);
  188. report_finished_handling_input_event(event.page_id, handled);
  189. if (!m_input_event_queue.is_empty())
  190. m_input_event_queue_timer->start();
  191. }
  192. void ConnectionFromClient::key_event(u64 page_id, Web::KeyEvent const& event)
  193. {
  194. enqueue_input_event({ page_id, move(const_cast<Web::KeyEvent&>(event)), 0 });
  195. }
  196. void ConnectionFromClient::mouse_event(u64 page_id, Web::MouseEvent const& event)
  197. {
  198. // OPTIMIZATION: Coalesce consecutive unprocessed mouse move and wheel events.
  199. auto event_to_coalesce = [&]() -> Web::MouseEvent const* {
  200. if (m_input_event_queue.is_empty())
  201. return nullptr;
  202. if (m_input_event_queue.tail().page_id != page_id)
  203. return nullptr;
  204. if (event.type != Web::MouseEvent::Type::MouseMove && event.type != Web::MouseEvent::Type::MouseWheel)
  205. return nullptr;
  206. if (auto const* mouse_event = m_input_event_queue.tail().event.get_pointer<Web::MouseEvent>()) {
  207. if (mouse_event->type == event.type)
  208. return mouse_event;
  209. }
  210. return nullptr;
  211. };
  212. if (auto const* last_mouse_event = event_to_coalesce()) {
  213. auto& mutable_event = const_cast<Web::MouseEvent&>(event);
  214. mutable_event.wheel_delta_x += last_mouse_event->wheel_delta_x;
  215. mutable_event.wheel_delta_y += last_mouse_event->wheel_delta_y;
  216. m_input_event_queue.tail().event = move(mutable_event);
  217. ++m_input_event_queue.tail().coalesced_event_count;
  218. return;
  219. }
  220. enqueue_input_event({ page_id, move(const_cast<Web::MouseEvent&>(event)), 0 });
  221. }
  222. void ConnectionFromClient::enqueue_input_event(QueuedInputEvent event)
  223. {
  224. m_input_event_queue.enqueue(move(event));
  225. m_input_event_queue_timer->start();
  226. }
  227. void ConnectionFromClient::report_finished_handling_input_event(u64 page_id, bool event_was_handled)
  228. {
  229. async_did_finish_handling_input_event(page_id, event_was_handled);
  230. }
  231. void ConnectionFromClient::debug_request(u64 page_id, ByteString const& request, ByteString const& argument)
  232. {
  233. auto page = this->page(page_id);
  234. if (!page.has_value())
  235. return;
  236. if (request == "dump-session-history") {
  237. auto const& traversable = page->page().top_level_traversable();
  238. Web::dump_tree(*traversable);
  239. }
  240. if (request == "dump-dom-tree") {
  241. if (auto* doc = page->page().top_level_browsing_context().active_document())
  242. Web::dump_tree(*doc);
  243. return;
  244. }
  245. if (request == "dump-layout-tree") {
  246. if (auto* doc = page->page().top_level_browsing_context().active_document()) {
  247. if (auto* viewport = doc->layout_node())
  248. Web::dump_tree(*viewport);
  249. }
  250. return;
  251. }
  252. if (request == "dump-paint-tree") {
  253. if (auto* doc = page->page().top_level_browsing_context().active_document()) {
  254. if (auto* paintable = doc->paintable())
  255. Web::dump_tree(*paintable);
  256. }
  257. return;
  258. }
  259. if (request == "dump-stacking-context-tree") {
  260. if (auto* doc = page->page().top_level_browsing_context().active_document()) {
  261. if (auto* viewport = doc->layout_node()) {
  262. if (auto* stacking_context = viewport->paintable_box()->stacking_context())
  263. stacking_context->dump();
  264. }
  265. }
  266. return;
  267. }
  268. if (request == "dump-style-sheets") {
  269. if (auto* doc = page->page().top_level_browsing_context().active_document()) {
  270. for (auto& sheet : doc->style_sheets().sheets()) {
  271. if (auto result = Web::dump_sheet(sheet); result.is_error())
  272. dbgln("Failed to dump style sheets: {}", result.error());
  273. }
  274. }
  275. return;
  276. }
  277. if (request == "dump-all-resolved-styles") {
  278. if (auto* doc = page->page().top_level_browsing_context().active_document()) {
  279. Queue<Web::DOM::Node*> elements_to_visit;
  280. elements_to_visit.enqueue(doc->document_element());
  281. while (!elements_to_visit.is_empty()) {
  282. auto element = elements_to_visit.dequeue();
  283. for (auto& child : element->children_as_vector())
  284. elements_to_visit.enqueue(child.ptr());
  285. if (element->is_element()) {
  286. auto styles = doc->style_computer().compute_style(*static_cast<Web::DOM::Element*>(element));
  287. dbgln("+ Element {}", element->debug_description());
  288. auto& properties = styles->properties();
  289. for (size_t i = 0; i < properties.size(); ++i)
  290. dbgln("| {} = {}", Web::CSS::string_from_property_id(static_cast<Web::CSS::PropertyID>(i)), properties[i].style ? properties[i].style->to_string() : ""_string);
  291. dbgln("---");
  292. }
  293. }
  294. }
  295. return;
  296. }
  297. if (request == "collect-garbage") {
  298. Web::Bindings::main_thread_vm().heap().collect_garbage(JS::Heap::CollectionType::CollectGarbage, true);
  299. return;
  300. }
  301. if (request == "set-line-box-borders") {
  302. bool state = argument == "on";
  303. page->set_should_show_line_box_borders(state);
  304. page->page().top_level_traversable()->set_needs_display(page->page().top_level_traversable()->viewport_rect());
  305. return;
  306. }
  307. if (request == "clear-cache") {
  308. Web::ResourceLoader::the().clear_cache();
  309. return;
  310. }
  311. if (request == "spoof-user-agent") {
  312. Web::ResourceLoader::the().set_user_agent(MUST(String::from_byte_string(argument)));
  313. return;
  314. }
  315. if (request == "same-origin-policy") {
  316. page->page().set_same_origin_policy_enabled(argument == "on");
  317. return;
  318. }
  319. if (request == "scripting") {
  320. page->page().set_is_scripting_enabled(argument == "on");
  321. return;
  322. }
  323. if (request == "block-pop-ups") {
  324. page->page().set_should_block_pop_ups(argument == "on");
  325. return;
  326. }
  327. if (request == "dump-local-storage") {
  328. if (auto* document = page->page().top_level_browsing_context().active_document())
  329. document->window()->local_storage().release_value_but_fixme_should_propagate_errors()->dump();
  330. return;
  331. }
  332. if (request == "load-reference-page") {
  333. if (auto* document = page->page().top_level_browsing_context().active_document()) {
  334. auto maybe_link = document->query_selector("link[rel=match]"sv);
  335. if (maybe_link.is_error() || !maybe_link.value()) {
  336. // To make sure that we fail the ref-test if the link is missing, load the error page->
  337. 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.");
  338. } else {
  339. auto link = maybe_link.release_value();
  340. auto url = document->parse_url(link->get_attribute_value(Web::HTML::AttributeNames::href));
  341. load_url(page_id, url);
  342. }
  343. }
  344. return;
  345. }
  346. }
  347. void ConnectionFromClient::get_source(u64 page_id)
  348. {
  349. if (auto page = this->page(page_id); page.has_value()) {
  350. if (auto* doc = page->page().top_level_browsing_context().active_document())
  351. async_did_get_source(page_id, doc->url(), doc->source().to_byte_string());
  352. }
  353. }
  354. void ConnectionFromClient::inspect_dom_tree(u64 page_id)
  355. {
  356. if (auto page = this->page(page_id); page.has_value()) {
  357. if (auto* doc = page->page().top_level_browsing_context().active_document())
  358. async_did_inspect_dom_tree(page_id, doc->dump_dom_tree_as_json().to_byte_string());
  359. }
  360. }
  361. void ConnectionFromClient::inspect_dom_node(u64 page_id, i32 node_id, Optional<Web::CSS::Selector::PseudoElement::Type> const& pseudo_element)
  362. {
  363. auto page = this->page(page_id);
  364. if (!page.has_value())
  365. return;
  366. auto& top_context = page->page().top_level_browsing_context();
  367. top_context.for_each_in_inclusive_subtree([&](auto& ctx) {
  368. if (ctx.active_document() != nullptr) {
  369. ctx.active_document()->set_inspected_node(nullptr, {});
  370. }
  371. return IterationDecision::Continue;
  372. });
  373. Web::DOM::Node* node = Web::DOM::Node::from_unique_id(node_id);
  374. // Note: Nodes without layout (aka non-visible nodes, don't have style computed)
  375. if (!node || !node->layout_node()) {
  376. async_did_inspect_dom_node(page_id, false, {}, {}, {}, {}, {});
  377. return;
  378. }
  379. node->document().set_inspected_node(node, pseudo_element);
  380. if (node->is_element()) {
  381. auto& element = verify_cast<Web::DOM::Element>(*node);
  382. if (!element.computed_css_values()) {
  383. async_did_inspect_dom_node(page_id, false, {}, {}, {}, {}, {});
  384. return;
  385. }
  386. auto serialize_json = [](Web::CSS::StyleProperties const& properties) -> ByteString {
  387. StringBuilder builder;
  388. auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
  389. properties.for_each_property([&](auto property_id, auto& value) {
  390. MUST(serializer.add(Web::CSS::string_from_property_id(property_id), value.to_string().to_byte_string()));
  391. });
  392. MUST(serializer.finish());
  393. return builder.to_byte_string();
  394. };
  395. auto serialize_custom_properties_json = [](Web::DOM::Element const& element, Optional<Web::CSS::Selector::PseudoElement::Type> pseudo_element) -> ByteString {
  396. StringBuilder builder;
  397. auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
  398. HashTable<FlyString> seen_properties;
  399. auto const* element_to_check = &element;
  400. while (element_to_check) {
  401. for (auto const& property : element_to_check->custom_properties(pseudo_element)) {
  402. if (!seen_properties.contains(property.key)) {
  403. seen_properties.set(property.key);
  404. MUST(serializer.add(property.key, property.value.value->to_string()));
  405. }
  406. }
  407. element_to_check = element_to_check->parent_element();
  408. }
  409. MUST(serializer.finish());
  410. return builder.to_byte_string();
  411. };
  412. auto serialize_node_box_sizing_json = [](Web::Layout::Node const* layout_node) -> ByteString {
  413. if (!layout_node || !layout_node->is_box()) {
  414. return "{}";
  415. }
  416. auto* box = static_cast<Web::Layout::Box const*>(layout_node);
  417. auto box_model = box->box_model();
  418. StringBuilder builder;
  419. auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
  420. MUST(serializer.add("padding_top"sv, box_model.padding.top.to_double()));
  421. MUST(serializer.add("padding_right"sv, box_model.padding.right.to_double()));
  422. MUST(serializer.add("padding_bottom"sv, box_model.padding.bottom.to_double()));
  423. MUST(serializer.add("padding_left"sv, box_model.padding.left.to_double()));
  424. MUST(serializer.add("margin_top"sv, box_model.margin.top.to_double()));
  425. MUST(serializer.add("margin_right"sv, box_model.margin.right.to_double()));
  426. MUST(serializer.add("margin_bottom"sv, box_model.margin.bottom.to_double()));
  427. MUST(serializer.add("margin_left"sv, box_model.margin.left.to_double()));
  428. MUST(serializer.add("border_top"sv, box_model.border.top.to_double()));
  429. MUST(serializer.add("border_right"sv, box_model.border.right.to_double()));
  430. MUST(serializer.add("border_bottom"sv, box_model.border.bottom.to_double()));
  431. MUST(serializer.add("border_left"sv, box_model.border.left.to_double()));
  432. if (auto* paintable_box = box->paintable_box()) {
  433. MUST(serializer.add("content_width"sv, paintable_box->content_width().to_double()));
  434. MUST(serializer.add("content_height"sv, paintable_box->content_height().to_double()));
  435. } else {
  436. MUST(serializer.add("content_width"sv, 0));
  437. MUST(serializer.add("content_height"sv, 0));
  438. }
  439. MUST(serializer.finish());
  440. return builder.to_byte_string();
  441. };
  442. auto serialize_aria_properties_state_json = [](Web::DOM::Element const& element) -> ByteString {
  443. auto role_name = element.role_or_default();
  444. if (!role_name.has_value()) {
  445. return "";
  446. }
  447. auto aria_data = MUST(Web::ARIA::AriaData::build_data(element));
  448. auto role = MUST(Web::ARIA::RoleType::build_role_object(role_name.value(), element.is_focusable(), *aria_data));
  449. StringBuilder builder;
  450. auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
  451. MUST(role->serialize_as_json(serializer));
  452. MUST(serializer.finish());
  453. return builder.to_byte_string();
  454. };
  455. if (pseudo_element.has_value()) {
  456. auto pseudo_element_node = element.get_pseudo_element_node(pseudo_element.value());
  457. if (!pseudo_element_node) {
  458. async_did_inspect_dom_node(page_id, false, {}, {}, {}, {}, {});
  459. return;
  460. }
  461. // FIXME: Pseudo-elements only exist as Layout::Nodes, which don't have style information
  462. // in a format we can use. So, we run the StyleComputer again to get the specified
  463. // values, and have to ignore the computed values and custom properties.
  464. auto pseudo_element_style = page->page().focused_context().active_document()->style_computer().compute_style(element, pseudo_element);
  465. ByteString computed_values = serialize_json(pseudo_element_style);
  466. ByteString resolved_values = "{}";
  467. ByteString custom_properties_json = serialize_custom_properties_json(element, pseudo_element);
  468. ByteString node_box_sizing_json = serialize_node_box_sizing_json(pseudo_element_node.ptr());
  469. async_did_inspect_dom_node(page_id, true, move(computed_values), move(resolved_values), move(custom_properties_json), move(node_box_sizing_json), {});
  470. return;
  471. }
  472. ByteString computed_values = serialize_json(*element.computed_css_values());
  473. ByteString resolved_values = serialize_json(element.resolved_css_values());
  474. ByteString custom_properties_json = serialize_custom_properties_json(element, {});
  475. ByteString node_box_sizing_json = serialize_node_box_sizing_json(element.layout_node());
  476. ByteString aria_properties_state_json = serialize_aria_properties_state_json(element);
  477. 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));
  478. return;
  479. }
  480. async_did_inspect_dom_node(page_id, false, {}, {}, {}, {}, {});
  481. }
  482. void ConnectionFromClient::inspect_accessibility_tree(u64 page_id)
  483. {
  484. if (auto page = this->page(page_id); page.has_value()) {
  485. if (auto* doc = page->page().top_level_browsing_context().active_document())
  486. async_did_inspect_accessibility_tree(page_id, doc->dump_accessibility_tree_as_json().to_byte_string());
  487. }
  488. }
  489. void ConnectionFromClient::get_hovered_node_id(u64 page_id)
  490. {
  491. auto page = this->page(page_id);
  492. if (!page.has_value())
  493. return;
  494. i32 node_id = 0;
  495. if (auto* document = page->page().top_level_browsing_context().active_document()) {
  496. if (auto* hovered_node = document->hovered_node())
  497. node_id = hovered_node->unique_id();
  498. }
  499. async_did_get_hovered_node_id(page_id, node_id);
  500. }
  501. void ConnectionFromClient::set_dom_node_text(u64 page_id, i32 node_id, String const& text)
  502. {
  503. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  504. if (!dom_node || (!dom_node->is_text() && !dom_node->is_comment())) {
  505. async_did_finish_editing_dom_node(page_id, {});
  506. return;
  507. }
  508. auto& character_data = static_cast<Web::DOM::CharacterData&>(*dom_node);
  509. character_data.set_data(text);
  510. async_did_finish_editing_dom_node(page_id, character_data.unique_id());
  511. }
  512. void ConnectionFromClient::set_dom_node_tag(u64 page_id, i32 node_id, String const& name)
  513. {
  514. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  515. if (!dom_node || !dom_node->is_element() || !dom_node->parent()) {
  516. async_did_finish_editing_dom_node(page_id, {});
  517. return;
  518. }
  519. auto& element = static_cast<Web::DOM::Element&>(*dom_node);
  520. 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();
  521. element.for_each_attribute([&](auto const& attribute) {
  522. new_element->set_attribute_value(attribute.local_name(), attribute.value(), attribute.prefix(), attribute.namespace_uri());
  523. });
  524. while (auto* child_node = element.first_child()) {
  525. MUST(element.remove_child(*child_node));
  526. MUST(new_element->append_child(*child_node));
  527. }
  528. element.parent()->replace_child(*new_element, element).release_value_but_fixme_should_propagate_errors();
  529. async_did_finish_editing_dom_node(page_id, new_element->unique_id());
  530. }
  531. void ConnectionFromClient::add_dom_node_attributes(u64 page_id, i32 node_id, Vector<WebView::Attribute> const& attributes)
  532. {
  533. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  534. if (!dom_node || !dom_node->is_element()) {
  535. async_did_finish_editing_dom_node(page_id, {});
  536. return;
  537. }
  538. auto& element = static_cast<Web::DOM::Element&>(*dom_node);
  539. for (auto const& attribute : attributes)
  540. element.set_attribute(attribute.name, attribute.value).release_value_but_fixme_should_propagate_errors();
  541. async_did_finish_editing_dom_node(page_id, element.unique_id());
  542. }
  543. void ConnectionFromClient::replace_dom_node_attribute(u64 page_id, i32 node_id, String const& name, Vector<WebView::Attribute> const& replacement_attributes)
  544. {
  545. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  546. if (!dom_node || !dom_node->is_element()) {
  547. async_did_finish_editing_dom_node(page_id, {});
  548. return;
  549. }
  550. auto& element = static_cast<Web::DOM::Element&>(*dom_node);
  551. bool should_remove_attribute = true;
  552. for (auto const& attribute : replacement_attributes) {
  553. if (should_remove_attribute && Web::Infra::is_ascii_case_insensitive_match(name, attribute.name))
  554. should_remove_attribute = false;
  555. element.set_attribute(attribute.name, attribute.value).release_value_but_fixme_should_propagate_errors();
  556. }
  557. if (should_remove_attribute)
  558. element.remove_attribute(name);
  559. async_did_finish_editing_dom_node(page_id, element.unique_id());
  560. }
  561. void ConnectionFromClient::create_child_element(u64 page_id, i32 node_id)
  562. {
  563. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  564. if (!dom_node) {
  565. async_did_finish_editing_dom_node(page_id, {});
  566. return;
  567. }
  568. auto element = Web::DOM::create_element(dom_node->document(), Web::HTML::TagNames::div, Web::Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  569. dom_node->append_child(element).release_value_but_fixme_should_propagate_errors();
  570. async_did_finish_editing_dom_node(page_id, element->unique_id());
  571. }
  572. void ConnectionFromClient::create_child_text_node(u64 page_id, i32 node_id)
  573. {
  574. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  575. if (!dom_node) {
  576. async_did_finish_editing_dom_node(page_id, {});
  577. return;
  578. }
  579. auto text_node = dom_node->heap().allocate<Web::DOM::Text>(dom_node->realm(), dom_node->document(), "text"_string);
  580. dom_node->append_child(text_node).release_value_but_fixme_should_propagate_errors();
  581. async_did_finish_editing_dom_node(page_id, text_node->unique_id());
  582. }
  583. void ConnectionFromClient::clone_dom_node(u64 page_id, i32 node_id)
  584. {
  585. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  586. if (!dom_node || !dom_node->parent_node()) {
  587. async_did_finish_editing_dom_node(page_id, {});
  588. return;
  589. }
  590. auto dom_node_clone = dom_node->clone_node(nullptr, true);
  591. dom_node->parent_node()->insert_before(dom_node_clone, dom_node->next_sibling());
  592. async_did_finish_editing_dom_node(page_id, dom_node_clone->unique_id());
  593. }
  594. void ConnectionFromClient::remove_dom_node(u64 page_id, i32 node_id)
  595. {
  596. auto page = this->page(page_id);
  597. if (!page.has_value())
  598. return;
  599. auto* active_document = page->page().top_level_browsing_context().active_document();
  600. if (!active_document) {
  601. async_did_finish_editing_dom_node(page_id, {});
  602. return;
  603. }
  604. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  605. if (!dom_node) {
  606. async_did_finish_editing_dom_node(page_id, {});
  607. return;
  608. }
  609. auto* previous_dom_node = dom_node->previous_sibling();
  610. if (!previous_dom_node)
  611. previous_dom_node = dom_node->parent();
  612. dom_node->remove();
  613. // FIXME: When nodes are removed from the DOM, the associated layout nodes become stale and still
  614. // remain in the layout tree. This has to be fixed, this just causes everything to be recomputed
  615. // which really hurts performance.
  616. active_document->force_layout();
  617. async_did_finish_editing_dom_node(page_id, previous_dom_node->unique_id());
  618. }
  619. void ConnectionFromClient::get_dom_node_html(u64 page_id, i32 node_id)
  620. {
  621. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  622. if (!dom_node)
  623. return;
  624. // FIXME: Implement Element's outerHTML attribute.
  625. auto container = Web::DOM::create_element(dom_node->document(), Web::HTML::TagNames::div, Web::Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  626. container->append_child(dom_node->clone_node(nullptr, true)).release_value_but_fixme_should_propagate_errors();
  627. auto html = container->inner_html().release_value_but_fixme_should_propagate_errors();
  628. async_did_get_dom_node_html(page_id, move(html));
  629. }
  630. void ConnectionFromClient::take_document_screenshot(u64 page_id)
  631. {
  632. auto page = this->page(page_id);
  633. if (!page.has_value())
  634. return;
  635. auto* document = page->page().top_level_browsing_context().active_document();
  636. if (!document || !document->document_element()) {
  637. async_did_take_screenshot(page_id, {});
  638. return;
  639. }
  640. auto const& content_size = page->content_size();
  641. Web::DevicePixelRect rect { { 0, 0 }, content_size };
  642. auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, rect.size().to_type<int>()).release_value_but_fixme_should_propagate_errors();
  643. page->paint(rect, *bitmap);
  644. async_did_take_screenshot(page_id, bitmap->to_shareable_bitmap());
  645. }
  646. void ConnectionFromClient::take_dom_node_screenshot(u64 page_id, i32 node_id)
  647. {
  648. auto page = this->page(page_id);
  649. if (!page.has_value())
  650. return;
  651. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  652. if (!dom_node || !dom_node->paintable_box()) {
  653. async_did_take_screenshot(page_id, {});
  654. return;
  655. }
  656. auto rect = page->page().enclosing_device_rect(dom_node->paintable_box()->absolute_border_box_rect());
  657. auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, rect.size().to_type<int>()).release_value_but_fixme_should_propagate_errors();
  658. page->paint(rect, *bitmap, { .paint_overlay = Web::PaintOptions::PaintOverlay::No });
  659. async_did_take_screenshot(page_id, bitmap->to_shareable_bitmap());
  660. }
  661. Messages::WebContentServer::DumpGcGraphResponse ConnectionFromClient::dump_gc_graph(u64)
  662. {
  663. auto gc_graph_json = Web::Bindings::main_thread_vm().heap().dump_graph();
  664. return MUST(String::from_byte_string(gc_graph_json.to_byte_string()));
  665. }
  666. Messages::WebContentServer::GetSelectedTextResponse ConnectionFromClient::get_selected_text(u64 page_id)
  667. {
  668. if (auto page = this->page(page_id); page.has_value())
  669. return page->page().focused_context().selected_text().to_byte_string();
  670. return ByteString {};
  671. }
  672. void ConnectionFromClient::select_all(u64 page_id)
  673. {
  674. if (auto page = this->page(page_id); page.has_value())
  675. page->page().focused_context().select_all();
  676. }
  677. void ConnectionFromClient::paste(u64 page_id, String const& text)
  678. {
  679. if (auto page = this->page(page_id); page.has_value())
  680. page->page().focused_context().paste(text);
  681. }
  682. Messages::WebContentServer::DumpLayoutTreeResponse ConnectionFromClient::dump_layout_tree(u64 page_id)
  683. {
  684. auto page = this->page(page_id);
  685. if (!page.has_value())
  686. return ByteString { "(no page)" };
  687. auto* document = page->page().top_level_browsing_context().active_document();
  688. if (!document)
  689. return ByteString { "(no DOM tree)" };
  690. document->update_layout();
  691. auto* layout_root = document->layout_node();
  692. if (!layout_root)
  693. return ByteString { "(no layout tree)" };
  694. StringBuilder builder;
  695. Web::dump_tree(builder, *layout_root);
  696. return builder.to_byte_string();
  697. }
  698. Messages::WebContentServer::DumpPaintTreeResponse ConnectionFromClient::dump_paint_tree(u64 page_id)
  699. {
  700. auto page = this->page(page_id);
  701. if (!page.has_value())
  702. return ByteString { "(no page)" };
  703. auto* document = page->page().top_level_browsing_context().active_document();
  704. if (!document)
  705. return ByteString { "(no DOM tree)" };
  706. document->update_layout();
  707. auto* layout_root = document->layout_node();
  708. if (!layout_root)
  709. return ByteString { "(no layout tree)" };
  710. if (!layout_root->paintable())
  711. return ByteString { "(no paint tree)" };
  712. StringBuilder builder;
  713. Web::dump_tree(builder, *layout_root->paintable());
  714. return builder.to_byte_string();
  715. }
  716. Messages::WebContentServer::DumpTextResponse ConnectionFromClient::dump_text(u64 page_id)
  717. {
  718. auto page = this->page(page_id);
  719. if (!page.has_value())
  720. return ByteString { "(no page)" };
  721. auto* document = page->page().top_level_browsing_context().active_document();
  722. if (!document)
  723. return ByteString { "(no DOM tree)" };
  724. if (!document->body())
  725. return ByteString { "(no body)" };
  726. return document->body()->inner_text();
  727. }
  728. void ConnectionFromClient::set_content_filters(u64, Vector<String> const& filters)
  729. {
  730. Web::ContentFilter::the().set_patterns(filters).release_value_but_fixme_should_propagate_errors();
  731. }
  732. void ConnectionFromClient::set_autoplay_allowed_on_all_websites(u64)
  733. {
  734. auto& autoplay_allowlist = Web::PermissionsPolicy::AutoplayAllowlist::the();
  735. autoplay_allowlist.enable_globally();
  736. }
  737. void ConnectionFromClient::set_autoplay_allowlist(u64, Vector<String> const& allowlist)
  738. {
  739. auto& autoplay_allowlist = Web::PermissionsPolicy::AutoplayAllowlist::the();
  740. autoplay_allowlist.enable_for_origins(allowlist).release_value_but_fixme_should_propagate_errors();
  741. }
  742. void ConnectionFromClient::set_proxy_mappings(u64, Vector<ByteString> const& proxies, HashMap<ByteString, size_t> const& mappings)
  743. {
  744. auto keys = mappings.keys();
  745. quick_sort(keys, [&](auto& a, auto& b) { return a.length() < b.length(); });
  746. OrderedHashMap<ByteString, size_t> sorted_mappings;
  747. for (auto& key : keys) {
  748. auto value = *mappings.get(key);
  749. if (value >= proxies.size())
  750. continue;
  751. sorted_mappings.set(key, value);
  752. }
  753. Web::ProxyMappings::the().set_mappings(proxies, move(sorted_mappings));
  754. }
  755. void ConnectionFromClient::set_preferred_color_scheme(u64 page_id, Web::CSS::PreferredColorScheme const& color_scheme)
  756. {
  757. if (auto page = this->page(page_id); page.has_value())
  758. page->set_preferred_color_scheme(color_scheme);
  759. }
  760. void ConnectionFromClient::set_has_focus(u64 page_id, bool has_focus)
  761. {
  762. if (auto page = this->page(page_id); page.has_value())
  763. page->set_has_focus(has_focus);
  764. }
  765. void ConnectionFromClient::set_is_scripting_enabled(u64 page_id, bool is_scripting_enabled)
  766. {
  767. if (auto page = this->page(page_id); page.has_value())
  768. page->set_is_scripting_enabled(is_scripting_enabled);
  769. }
  770. void ConnectionFromClient::set_device_pixels_per_css_pixel(u64 page_id, float device_pixels_per_css_pixel)
  771. {
  772. if (auto page = this->page(page_id); page.has_value())
  773. page->set_device_pixels_per_css_pixel(device_pixels_per_css_pixel);
  774. }
  775. void ConnectionFromClient::set_window_position(u64 page_id, Web::DevicePixelPoint position)
  776. {
  777. if (auto page = this->page(page_id); page.has_value())
  778. page->set_window_position(position);
  779. }
  780. void ConnectionFromClient::set_window_size(u64 page_id, Web::DevicePixelSize size)
  781. {
  782. if (auto page = this->page(page_id); page.has_value())
  783. page->set_window_size(size);
  784. }
  785. Messages::WebContentServer::GetLocalStorageEntriesResponse ConnectionFromClient::get_local_storage_entries(u64 page_id)
  786. {
  787. auto page = this->page(page_id);
  788. if (!page.has_value())
  789. return OrderedHashMap<String, String> {};
  790. auto* document = page->page().top_level_browsing_context().active_document();
  791. auto local_storage = document->window()->local_storage().release_value_but_fixme_should_propagate_errors();
  792. return local_storage->map();
  793. }
  794. Messages::WebContentServer::GetSessionStorageEntriesResponse ConnectionFromClient::get_session_storage_entries(u64 page_id)
  795. {
  796. auto page = this->page(page_id);
  797. if (!page.has_value())
  798. return OrderedHashMap<String, String> {};
  799. auto* document = page->page().top_level_browsing_context().active_document();
  800. auto session_storage = document->window()->session_storage().release_value_but_fixme_should_propagate_errors();
  801. return session_storage->map();
  802. }
  803. void ConnectionFromClient::handle_file_return(u64, i32 error, Optional<IPC::File> const& file, i32 request_id)
  804. {
  805. auto file_request = m_requested_files.take(request_id);
  806. VERIFY(file_request.has_value());
  807. VERIFY(file_request.value().on_file_request_finish);
  808. file_request.value().on_file_request_finish(error != 0 ? Error::from_errno(error) : ErrorOr<i32> { file->take_fd() });
  809. }
  810. void ConnectionFromClient::request_file(u64 page_id, Web::FileRequest file_request)
  811. {
  812. i32 const id = last_id++;
  813. auto path = file_request.path();
  814. m_requested_files.set(id, move(file_request));
  815. async_did_request_file(page_id, path, id);
  816. }
  817. void ConnectionFromClient::set_system_visibility_state(u64 page_id, bool visible)
  818. {
  819. if (auto page = this->page(page_id); page.has_value()) {
  820. page->page().top_level_traversable()->set_system_visibility_state(
  821. visible
  822. ? Web::HTML::VisibilityState::Visible
  823. : Web::HTML::VisibilityState::Hidden);
  824. }
  825. }
  826. void ConnectionFromClient::js_console_input(u64 page_id, ByteString const& js_source)
  827. {
  828. auto page = this->page(page_id);
  829. if (!page.has_value())
  830. return;
  831. page->js_console_input(js_source);
  832. }
  833. void ConnectionFromClient::run_javascript(u64 page_id, ByteString const& js_source)
  834. {
  835. if (auto page = this->page(page_id); page.has_value())
  836. page->run_javascript(js_source);
  837. }
  838. void ConnectionFromClient::js_console_request_messages(u64 page_id, i32 start_index)
  839. {
  840. if (auto page = this->page(page_id); page.has_value())
  841. page->js_console_request_messages(start_index);
  842. }
  843. void ConnectionFromClient::alert_closed(u64 page_id)
  844. {
  845. if (auto page = this->page(page_id); page.has_value())
  846. page->page().alert_closed();
  847. }
  848. void ConnectionFromClient::confirm_closed(u64 page_id, bool accepted)
  849. {
  850. if (auto page = this->page(page_id); page.has_value())
  851. page->page().confirm_closed(accepted);
  852. }
  853. void ConnectionFromClient::prompt_closed(u64 page_id, Optional<String> const& response)
  854. {
  855. if (auto page = this->page(page_id); page.has_value())
  856. page->page().prompt_closed(response);
  857. }
  858. void ConnectionFromClient::color_picker_update(u64 page_id, Optional<Color> const& picked_color, Web::HTML::ColorPickerUpdateState const& state)
  859. {
  860. if (auto page = this->page(page_id); page.has_value())
  861. page->page().color_picker_update(picked_color, state);
  862. }
  863. void ConnectionFromClient::file_picker_closed(u64 page_id, Vector<Web::HTML::SelectedFile> const& selected_files)
  864. {
  865. if (auto page = this->page(page_id); page.has_value())
  866. page->page().file_picker_closed(const_cast<Vector<Web::HTML::SelectedFile>&>(selected_files));
  867. }
  868. void ConnectionFromClient::select_dropdown_closed(u64 page_id, Optional<String> const& value)
  869. {
  870. if (auto page = this->page(page_id); page.has_value())
  871. page->page().select_dropdown_closed(value);
  872. }
  873. void ConnectionFromClient::toggle_media_play_state(u64 page_id)
  874. {
  875. if (auto page = this->page(page_id); page.has_value())
  876. page->page().toggle_media_play_state().release_value_but_fixme_should_propagate_errors();
  877. }
  878. void ConnectionFromClient::toggle_media_mute_state(u64 page_id)
  879. {
  880. if (auto page = this->page(page_id); page.has_value())
  881. page->page().toggle_media_mute_state();
  882. }
  883. void ConnectionFromClient::toggle_media_loop_state(u64 page_id)
  884. {
  885. if (auto page = this->page(page_id); page.has_value())
  886. page->page().toggle_media_loop_state().release_value_but_fixme_should_propagate_errors();
  887. }
  888. void ConnectionFromClient::toggle_media_controls_state(u64 page_id)
  889. {
  890. if (auto page = this->page(page_id); page.has_value())
  891. page->page().toggle_media_controls_state().release_value_but_fixme_should_propagate_errors();
  892. }
  893. void ConnectionFromClient::toggle_page_mute_state(u64 page_id)
  894. {
  895. if (auto page = this->page(page_id); page.has_value())
  896. page->page().toggle_page_mute_state();
  897. }
  898. void ConnectionFromClient::set_user_style(u64 page_id, String const& source)
  899. {
  900. if (auto page = this->page(page_id); page.has_value())
  901. page->page().set_user_style(source);
  902. }
  903. void ConnectionFromClient::enable_inspector_prototype(u64)
  904. {
  905. Web::HTML::Window::set_inspector_object_exposed(true);
  906. }
  907. }