ConnectionFromClient.cpp 30 KB

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