ConnectionFromClient.cpp 41 KB

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