ConnectionFromClient.cpp 42 KB

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