ConnectionFromClient.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  5. * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/Debug.h>
  10. #include <AK/JsonObject.h>
  11. #include <AK/QuickSort.h>
  12. #include <LibGfx/Bitmap.h>
  13. #include <LibGfx/Font/FontDatabase.h>
  14. #include <LibGfx/SystemTheme.h>
  15. #include <LibJS/Console.h>
  16. #include <LibJS/Heap/Heap.h>
  17. #include <LibJS/Parser.h>
  18. #include <LibJS/Runtime/ConsoleObject.h>
  19. #include <LibWeb/Bindings/MainThreadVM.h>
  20. #include <LibWeb/CSS/PropertyID.h>
  21. #include <LibWeb/Cookie/ParsedCookie.h>
  22. #include <LibWeb/DOM/Document.h>
  23. #include <LibWeb/DOM/NodeList.h>
  24. #include <LibWeb/Dump.h>
  25. #include <LibWeb/HTML/BrowsingContext.h>
  26. #include <LibWeb/HTML/Scripting/ClassicScript.h>
  27. #include <LibWeb/HTML/Storage.h>
  28. #include <LibWeb/HTML/Window.h>
  29. #include <LibWeb/Layout/InitialContainingBlock.h>
  30. #include <LibWeb/Loader/ContentFilter.h>
  31. #include <LibWeb/Loader/ProxyMappings.h>
  32. #include <LibWeb/Loader/ResourceLoader.h>
  33. #include <LibWeb/Painting/PaintableBox.h>
  34. #include <LibWeb/Painting/StackingContext.h>
  35. #include <LibWeb/Platform/EventLoopPlugin.h>
  36. #include <WebContent/ConnectionFromClient.h>
  37. #include <WebContent/PageHost.h>
  38. #include <WebContent/WebContentClientEndpoint.h>
  39. #include <pthread.h>
  40. namespace WebContent {
  41. ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
  42. : IPC::ConnectionFromClient<WebContentClientEndpoint, WebContentServerEndpoint>(*this, move(socket), 1)
  43. , m_page_host(PageHost::create(*this))
  44. {
  45. m_paint_flush_timer = Web::Platform::Timer::create_single_shot(0, [this] { flush_pending_paint_requests(); });
  46. }
  47. void ConnectionFromClient::die()
  48. {
  49. Web::Platform::EventLoopPlugin::the().quit();
  50. }
  51. Web::Page& ConnectionFromClient::page()
  52. {
  53. return m_page_host->page();
  54. }
  55. Web::Page const& ConnectionFromClient::page() const
  56. {
  57. return m_page_host->page();
  58. }
  59. void ConnectionFromClient::update_system_theme(Core::AnonymousBuffer const& theme_buffer)
  60. {
  61. Gfx::set_system_theme(theme_buffer);
  62. auto impl = Gfx::PaletteImpl::create_with_anonymous_buffer(theme_buffer);
  63. m_page_host->set_palette_impl(*impl);
  64. }
  65. void ConnectionFromClient::update_system_fonts(String const& default_font_query, String const& fixed_width_font_query, String const& window_title_font_query)
  66. {
  67. Gfx::FontDatabase::set_default_font_query(default_font_query);
  68. Gfx::FontDatabase::set_fixed_width_font_query(fixed_width_font_query);
  69. Gfx::FontDatabase::set_window_title_font_query(window_title_font_query);
  70. }
  71. void ConnectionFromClient::update_screen_rects(Vector<Gfx::IntRect> const& rects, u32 main_screen)
  72. {
  73. m_page_host->set_screen_rects(rects, main_screen);
  74. }
  75. void ConnectionFromClient::load_url(const URL& url)
  76. {
  77. dbgln_if(SPAM_DEBUG, "handle: WebContentServer::LoadURL: url={}", url);
  78. #if defined(AK_OS_SERENITY)
  79. String process_name;
  80. if (url.host().is_empty())
  81. process_name = "WebContent";
  82. else
  83. process_name = String::formatted("WebContent: {}", url.host());
  84. pthread_setname_np(pthread_self(), process_name.characters());
  85. #endif
  86. page().load(url);
  87. }
  88. void ConnectionFromClient::load_html(String const& html, const URL& url)
  89. {
  90. dbgln_if(SPAM_DEBUG, "handle: WebContentServer::LoadHTML: html={}, url={}", html, url);
  91. page().load_html(html, url);
  92. }
  93. void ConnectionFromClient::set_viewport_rect(Gfx::IntRect const& rect)
  94. {
  95. dbgln_if(SPAM_DEBUG, "handle: WebContentServer::SetViewportRect: rect={}", rect);
  96. m_page_host->set_viewport_rect(rect);
  97. }
  98. void ConnectionFromClient::add_backing_store(i32 backing_store_id, Gfx::ShareableBitmap const& bitmap)
  99. {
  100. m_backing_stores.set(backing_store_id, *bitmap.bitmap());
  101. }
  102. void ConnectionFromClient::remove_backing_store(i32 backing_store_id)
  103. {
  104. m_backing_stores.remove(backing_store_id);
  105. m_pending_paint_requests.remove_all_matching([backing_store_id](auto& pending_repaint_request) { return pending_repaint_request.bitmap_id == backing_store_id; });
  106. }
  107. void ConnectionFromClient::paint(Gfx::IntRect const& content_rect, i32 backing_store_id)
  108. {
  109. for (auto& pending_paint : m_pending_paint_requests) {
  110. if (pending_paint.bitmap_id == backing_store_id) {
  111. pending_paint.content_rect = content_rect;
  112. return;
  113. }
  114. }
  115. auto it = m_backing_stores.find(backing_store_id);
  116. if (it == m_backing_stores.end()) {
  117. did_misbehave("Client requested paint with backing store ID");
  118. return;
  119. }
  120. auto& bitmap = *it->value;
  121. m_pending_paint_requests.append({ content_rect, bitmap, backing_store_id });
  122. m_paint_flush_timer->start();
  123. }
  124. void ConnectionFromClient::flush_pending_paint_requests()
  125. {
  126. for (auto& pending_paint : m_pending_paint_requests) {
  127. m_page_host->paint(pending_paint.content_rect, *pending_paint.bitmap);
  128. async_did_paint(pending_paint.content_rect, pending_paint.bitmap_id);
  129. }
  130. m_pending_paint_requests.clear();
  131. }
  132. void ConnectionFromClient::mouse_down(Gfx::IntPoint const& position, unsigned int button, unsigned int buttons, unsigned int modifiers)
  133. {
  134. page().handle_mousedown(position, button, buttons, modifiers);
  135. }
  136. void ConnectionFromClient::mouse_move(Gfx::IntPoint const& position, [[maybe_unused]] unsigned int button, unsigned int buttons, unsigned int modifiers)
  137. {
  138. page().handle_mousemove(position, buttons, modifiers);
  139. }
  140. void ConnectionFromClient::mouse_up(Gfx::IntPoint const& position, unsigned int button, unsigned int buttons, unsigned int modifiers)
  141. {
  142. page().handle_mouseup(position, button, buttons, modifiers);
  143. }
  144. void ConnectionFromClient::mouse_wheel(Gfx::IntPoint const& position, unsigned int button, unsigned int buttons, unsigned int modifiers, i32 wheel_delta_x, i32 wheel_delta_y)
  145. {
  146. page().handle_mousewheel(position, button, buttons, modifiers, wheel_delta_x, wheel_delta_y);
  147. }
  148. void ConnectionFromClient::doubleclick(Gfx::IntPoint const& position, unsigned int button, unsigned int buttons, unsigned int modifiers)
  149. {
  150. page().handle_doubleclick(position, button, buttons, modifiers);
  151. }
  152. void ConnectionFromClient::key_down(i32 key, unsigned int modifiers, u32 code_point)
  153. {
  154. page().handle_keydown((KeyCode)key, modifiers, code_point);
  155. }
  156. void ConnectionFromClient::key_up(i32 key, unsigned int modifiers, u32 code_point)
  157. {
  158. page().handle_keyup((KeyCode)key, modifiers, code_point);
  159. }
  160. void ConnectionFromClient::debug_request(String const& request, String const& argument)
  161. {
  162. if (request == "dump-dom-tree") {
  163. if (auto* doc = page().top_level_browsing_context().active_document())
  164. Web::dump_tree(*doc);
  165. }
  166. if (request == "dump-layout-tree") {
  167. if (auto* doc = page().top_level_browsing_context().active_document()) {
  168. if (auto* icb = doc->layout_node())
  169. Web::dump_tree(*icb);
  170. }
  171. }
  172. if (request == "dump-stacking-context-tree") {
  173. if (auto* doc = page().top_level_browsing_context().active_document()) {
  174. if (auto* icb = doc->layout_node()) {
  175. if (auto* stacking_context = icb->paint_box()->stacking_context())
  176. stacking_context->dump();
  177. }
  178. }
  179. }
  180. if (request == "dump-style-sheets") {
  181. if (auto* doc = page().top_level_browsing_context().active_document()) {
  182. for (auto& sheet : doc->style_sheets().sheets()) {
  183. Web::dump_sheet(sheet);
  184. }
  185. }
  186. }
  187. if (request == "collect-garbage") {
  188. Web::Bindings::main_thread_vm().heap().collect_garbage(JS::Heap::CollectionType::CollectGarbage, true);
  189. }
  190. if (request == "set-line-box-borders") {
  191. bool state = argument == "on";
  192. m_page_host->set_should_show_line_box_borders(state);
  193. page().top_level_browsing_context().set_needs_display(page().top_level_browsing_context().viewport_rect());
  194. }
  195. if (request == "clear-cache") {
  196. Web::ResourceLoader::the().clear_cache();
  197. }
  198. if (request == "spoof-user-agent") {
  199. Web::ResourceLoader::the().set_user_agent(argument);
  200. }
  201. if (request == "same-origin-policy") {
  202. m_page_host->page().set_same_origin_policy_enabled(argument == "on");
  203. }
  204. if (request == "scripting") {
  205. m_page_host->page().set_is_scripting_enabled(argument == "on");
  206. }
  207. if (request == "dump-local-storage") {
  208. if (auto* doc = page().top_level_browsing_context().active_document())
  209. doc->window().local_storage()->dump();
  210. }
  211. }
  212. void ConnectionFromClient::get_source()
  213. {
  214. if (auto* doc = page().top_level_browsing_context().active_document()) {
  215. async_did_get_source(doc->url(), doc->source());
  216. }
  217. }
  218. void ConnectionFromClient::inspect_dom_tree()
  219. {
  220. if (auto* doc = page().top_level_browsing_context().active_document()) {
  221. async_did_get_dom_tree(doc->dump_dom_tree_as_json());
  222. }
  223. }
  224. Messages::WebContentServer::InspectDomNodeResponse ConnectionFromClient::inspect_dom_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> const& pseudo_element)
  225. {
  226. auto& top_context = page().top_level_browsing_context();
  227. top_context.for_each_in_inclusive_subtree([&](auto& ctx) {
  228. if (ctx.active_document() != nullptr) {
  229. ctx.active_document()->set_inspected_node(nullptr);
  230. }
  231. return IterationDecision::Continue;
  232. });
  233. Web::DOM::Node* node = Web::DOM::Node::from_id(node_id);
  234. // Note: Nodes without layout (aka non-visible nodes, don't have style computed)
  235. if (!node || !node->layout_node()) {
  236. return { false, "", "", "", "" };
  237. }
  238. // FIXME: Pass the pseudo-element here.
  239. node->document().set_inspected_node(node);
  240. if (node->is_element()) {
  241. auto& element = verify_cast<Web::DOM::Element>(*node);
  242. if (!element.computed_css_values())
  243. return { false, "", "", "", "" };
  244. auto serialize_json = [](Web::CSS::StyleProperties const& properties) -> String {
  245. StringBuilder builder;
  246. auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
  247. properties.for_each_property([&](auto property_id, auto& value) {
  248. MUST(serializer.add(Web::CSS::string_from_property_id(property_id), value.to_string()));
  249. });
  250. MUST(serializer.finish());
  251. return builder.to_string();
  252. };
  253. auto serialize_custom_properties_json = [](Web::DOM::Element const& element) -> String {
  254. StringBuilder builder;
  255. auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
  256. HashTable<String> seen_properties;
  257. auto const* element_to_check = &element;
  258. while (element_to_check) {
  259. for (auto const& property : element_to_check->custom_properties()) {
  260. if (!seen_properties.contains(property.key)) {
  261. seen_properties.set(property.key);
  262. MUST(serializer.add(property.key, property.value.value->to_string()));
  263. }
  264. }
  265. element_to_check = element_to_check->parent_element();
  266. }
  267. MUST(serializer.finish());
  268. return builder.to_string();
  269. };
  270. auto serialize_node_box_sizing_json = [](Web::Layout::Node const* layout_node) -> String {
  271. if (!layout_node || !layout_node->is_box()) {
  272. return "{}";
  273. }
  274. auto* box = static_cast<Web::Layout::Box const*>(layout_node);
  275. auto box_model = box->box_model();
  276. StringBuilder builder;
  277. auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
  278. MUST(serializer.add("padding_top"sv, box_model.padding.top));
  279. MUST(serializer.add("padding_right"sv, box_model.padding.right));
  280. MUST(serializer.add("padding_bottom"sv, box_model.padding.bottom));
  281. MUST(serializer.add("padding_left"sv, box_model.padding.left));
  282. MUST(serializer.add("margin_top"sv, box_model.margin.top));
  283. MUST(serializer.add("margin_right"sv, box_model.margin.right));
  284. MUST(serializer.add("margin_bottom"sv, box_model.margin.bottom));
  285. MUST(serializer.add("margin_left"sv, box_model.margin.left));
  286. MUST(serializer.add("border_top"sv, box_model.border.top));
  287. MUST(serializer.add("border_right"sv, box_model.border.right));
  288. MUST(serializer.add("border_bottom"sv, box_model.border.bottom));
  289. MUST(serializer.add("border_left"sv, box_model.border.left));
  290. if (auto* paint_box = box->paint_box()) {
  291. MUST(serializer.add("content_width"sv, paint_box->content_width()));
  292. MUST(serializer.add("content_height"sv, paint_box->content_height()));
  293. } else {
  294. MUST(serializer.add("content_width"sv, 0));
  295. MUST(serializer.add("content_height"sv, 0));
  296. }
  297. MUST(serializer.finish());
  298. return builder.to_string();
  299. };
  300. if (pseudo_element.has_value()) {
  301. auto pseudo_element_node = element.get_pseudo_element_node(pseudo_element.value());
  302. if (!pseudo_element_node)
  303. return { false, "", "", "", "" };
  304. // FIXME: Pseudo-elements only exist as Layout::Nodes, which don't have style information
  305. // in a format we can use. So, we run the StyleComputer again to get the specified
  306. // values, and have to ignore the computed values and custom properties.
  307. auto pseudo_element_style = page().focused_context().active_document()->style_computer().compute_style(element, pseudo_element);
  308. String computed_values = serialize_json(pseudo_element_style);
  309. String resolved_values = "{}";
  310. String custom_properties_json = "{}";
  311. String node_box_sizing_json = serialize_node_box_sizing_json(pseudo_element_node.ptr());
  312. return { true, computed_values, resolved_values, custom_properties_json, node_box_sizing_json };
  313. }
  314. String computed_values = serialize_json(*element.computed_css_values());
  315. String resolved_values_json = serialize_json(element.resolved_css_values());
  316. String custom_properties_json = serialize_custom_properties_json(element);
  317. String node_box_sizing_json = serialize_node_box_sizing_json(element.layout_node());
  318. return { true, computed_values, resolved_values_json, custom_properties_json, node_box_sizing_json };
  319. }
  320. return { false, "", "", "", "" };
  321. }
  322. Messages::WebContentServer::GetHoveredNodeIdResponse ConnectionFromClient::get_hovered_node_id()
  323. {
  324. if (auto* document = page().top_level_browsing_context().active_document()) {
  325. auto hovered_node = document->hovered_node();
  326. if (hovered_node)
  327. return hovered_node->id();
  328. }
  329. return (i32)0;
  330. }
  331. void ConnectionFromClient::initialize_js_console(Badge<PageHost>)
  332. {
  333. auto* document = page().top_level_browsing_context().active_document();
  334. auto realm = document->realm().make_weak_ptr();
  335. if (m_realm.ptr() == realm.ptr())
  336. return;
  337. auto& console_object = *realm->intrinsics().console_object();
  338. m_realm = realm;
  339. m_console_client = make<WebContentConsoleClient>(console_object.console(), *m_realm, *this);
  340. console_object.console().set_client(*m_console_client.ptr());
  341. }
  342. void ConnectionFromClient::js_console_input(String const& js_source)
  343. {
  344. if (m_console_client)
  345. m_console_client->handle_input(js_source);
  346. }
  347. void ConnectionFromClient::run_javascript(String const& js_source)
  348. {
  349. auto* active_document = page().top_level_browsing_context().active_document();
  350. if (!active_document)
  351. return;
  352. // This is partially based on "execute a javascript: URL request" https://html.spec.whatwg.org/multipage/browsing-the-web.html#javascript-protocol
  353. // Let settings be browsingContext's active document's relevant settings object.
  354. auto& settings = active_document->relevant_settings_object();
  355. // Let baseURL be settings's API base URL.
  356. auto base_url = settings.api_base_url();
  357. // Let script be the result of creating a classic script given scriptSource, settings, baseURL, and the default classic script fetch options.
  358. // FIXME: This doesn't pass in "default classic script fetch options"
  359. // FIXME: What should the filename be here?
  360. auto script = Web::HTML::ClassicScript::create("(client connection run_javascript)", js_source, settings, move(base_url));
  361. // Let evaluationStatus be the result of running the classic script script.
  362. auto evaluation_status = script->run();
  363. if (evaluation_status.is_error())
  364. dbgln("Exception :(");
  365. }
  366. void ConnectionFromClient::js_console_request_messages(i32 start_index)
  367. {
  368. if (m_console_client)
  369. m_console_client->send_messages(start_index);
  370. }
  371. Messages::WebContentServer::GetDocumentElementResponse ConnectionFromClient::get_document_element()
  372. {
  373. auto* document = page().top_level_browsing_context().active_document();
  374. if (!document)
  375. return Optional<i32> {};
  376. return { document->id() };
  377. }
  378. Messages::WebContentServer::QuerySelectorAllResponse ConnectionFromClient::query_selector_all(i32 start_node_id, String const& selector)
  379. {
  380. auto* start_node = Web::DOM::Node::from_id(start_node_id);
  381. if (!start_node)
  382. return Optional<Vector<i32>> {};
  383. if (!start_node->is_element() && !start_node->is_document())
  384. return Optional<Vector<i32>> {};
  385. auto& start_element = verify_cast<Web::DOM::ParentNode>(*start_node);
  386. auto result = start_element.query_selector_all(selector);
  387. if (result.is_error())
  388. return Optional<Vector<i32>> {};
  389. auto element_list = result.release_value();
  390. Vector<i32> return_list;
  391. for (u32 i = 0; i < element_list->length(); i++) {
  392. auto node = element_list->item(i);
  393. return_list.append(node->id());
  394. }
  395. return { return_list };
  396. }
  397. Messages::WebContentServer::GetElementAttributeResponse ConnectionFromClient::get_element_attribute(i32 element_id, String const& name)
  398. {
  399. auto* node = Web::DOM::Node::from_id(element_id);
  400. if (!node)
  401. return Optional<String> {};
  402. if (!node->is_element())
  403. return Optional<String> {};
  404. auto& element = verify_cast<Web::DOM::Element>(*node);
  405. if (!element.has_attribute(name))
  406. return Optional<String> {};
  407. return { element.get_attribute(name) };
  408. }
  409. Messages::WebContentServer::GetElementPropertyResponse ConnectionFromClient::get_element_property(i32 element_id, String const& name)
  410. {
  411. auto* node = Web::DOM::Node::from_id(element_id);
  412. if (!node)
  413. return Optional<String> {};
  414. if (!node->is_element())
  415. return Optional<String> {};
  416. auto& element = verify_cast<Web::DOM::Element>(*node);
  417. auto property_or_error = element.get(name);
  418. if (property_or_error.is_throw_completion())
  419. return Optional<String> {};
  420. auto property = property_or_error.release_value();
  421. if (property.is_undefined())
  422. return Optional<String> {};
  423. auto string_or_error = property.to_string(element.vm());
  424. if (string_or_error.is_error())
  425. return Optional<String> {};
  426. return { string_or_error.release_value() };
  427. }
  428. Messages::WebContentServer::GetActiveDocumentsTypeResponse ConnectionFromClient::get_active_documents_type()
  429. {
  430. auto* active_document = page().top_level_browsing_context().active_document();
  431. if (!active_document)
  432. return { "" };
  433. auto type = active_document->document_type();
  434. switch (type) {
  435. case Web::DOM::Document::Type::HTML:
  436. return { "html" };
  437. break;
  438. case Web::DOM::Document::Type::XML:
  439. return { "xml" };
  440. break;
  441. }
  442. return { "" };
  443. }
  444. Messages::WebContentServer::GetComputedValueForElementResponse ConnectionFromClient::get_computed_value_for_element(i32 element_id, String const& property_name)
  445. {
  446. auto* node = Web::DOM::Node::from_id(element_id);
  447. if (!node)
  448. return { "" };
  449. if (!node->is_element())
  450. return { "" };
  451. auto& element = verify_cast<Web::DOM::Element>(*node);
  452. auto property_id = Web::CSS::property_id_from_string(property_name);
  453. auto computed_values = element.computed_css_values();
  454. if (!computed_values)
  455. return { "" };
  456. auto style_value = computed_values->property(property_id);
  457. return { style_value->to_string() };
  458. }
  459. Messages::WebContentServer::GetElementTagNameResponse ConnectionFromClient::get_element_tag_name(i32 element_id)
  460. {
  461. auto* node = Web::DOM::Node::from_id(element_id);
  462. if (!node)
  463. return { "" };
  464. if (!node->is_element())
  465. return { "" };
  466. auto& element = verify_cast<Web::DOM::Element>(*node);
  467. return { element.tag_name() };
  468. }
  469. Messages::WebContentServer::GetSelectedTextResponse ConnectionFromClient::get_selected_text()
  470. {
  471. return page().focused_context().selected_text();
  472. }
  473. void ConnectionFromClient::select_all()
  474. {
  475. page().focused_context().select_all();
  476. page().client().page_did_change_selection();
  477. }
  478. Messages::WebContentServer::DumpLayoutTreeResponse ConnectionFromClient::dump_layout_tree()
  479. {
  480. auto* document = page().top_level_browsing_context().active_document();
  481. if (!document)
  482. return String { "(no DOM tree)" };
  483. auto* layout_root = document->layout_node();
  484. if (!layout_root)
  485. return String { "(no layout tree)" };
  486. StringBuilder builder;
  487. Web::dump_tree(builder, *layout_root);
  488. return builder.to_string();
  489. }
  490. void ConnectionFromClient::set_content_filters(Vector<String> const& filters)
  491. {
  492. for (auto& filter : filters)
  493. Web::ContentFilter::the().add_pattern(filter);
  494. }
  495. void ConnectionFromClient::set_proxy_mappings(Vector<String> const& proxies, HashMap<String, size_t> const& mappings)
  496. {
  497. auto keys = mappings.keys();
  498. quick_sort(keys, [&](auto& a, auto& b) { return a.length() < b.length(); });
  499. OrderedHashMap<String, size_t> sorted_mappings;
  500. for (auto& key : keys) {
  501. auto value = *mappings.get(key);
  502. if (value >= proxies.size())
  503. continue;
  504. sorted_mappings.set(key, value);
  505. }
  506. Web::ProxyMappings::the().set_mappings(proxies, move(sorted_mappings));
  507. }
  508. void ConnectionFromClient::set_preferred_color_scheme(Web::CSS::PreferredColorScheme const& color_scheme)
  509. {
  510. m_page_host->set_preferred_color_scheme(color_scheme);
  511. }
  512. void ConnectionFromClient::set_has_focus(bool has_focus)
  513. {
  514. m_page_host->set_has_focus(has_focus);
  515. }
  516. void ConnectionFromClient::set_is_scripting_enabled(bool is_scripting_enabled)
  517. {
  518. m_page_host->set_is_scripting_enabled(is_scripting_enabled);
  519. }
  520. void ConnectionFromClient::set_is_webdriver_active(bool is_webdriver_active)
  521. {
  522. m_page_host->set_is_webdriver_active(is_webdriver_active);
  523. }
  524. Messages::WebContentServer::GetLocalStorageEntriesResponse ConnectionFromClient::get_local_storage_entries()
  525. {
  526. auto* document = page().top_level_browsing_context().active_document();
  527. auto local_storage = document->window().local_storage();
  528. return local_storage->map();
  529. }
  530. Messages::WebContentServer::GetSessionStorageEntriesResponse ConnectionFromClient::get_session_storage_entries()
  531. {
  532. auto* document = page().top_level_browsing_context().active_document();
  533. auto session_storage = document->window().session_storage();
  534. return session_storage->map();
  535. }
  536. void ConnectionFromClient::handle_file_return(i32 error, Optional<IPC::File> const& file, i32 request_id)
  537. {
  538. auto result = m_requested_files.get(request_id);
  539. VERIFY(result.has_value());
  540. VERIFY(result.value()->on_file_request_finish);
  541. result.value()->on_file_request_finish(error != 0 ? Error::from_errno(error) : ErrorOr<i32> { file->take_fd() });
  542. m_requested_files.remove(request_id);
  543. }
  544. void ConnectionFromClient::request_file(NonnullRefPtr<Web::FileRequest>& file_request)
  545. {
  546. i32 const id = last_id++;
  547. m_requested_files.set(id, file_request);
  548. async_did_request_file(file_request->path(), id);
  549. }
  550. void ConnectionFromClient::set_system_visibility_state(bool visible)
  551. {
  552. m_page_host->page().top_level_browsing_context().set_system_visibility_state(
  553. visible
  554. ? Web::HTML::VisibilityState::Visible
  555. : Web::HTML::VisibilityState::Hidden);
  556. }
  557. }