ConnectionFromClient.cpp 24 KB

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