ConnectionFromClient.cpp 46 KB

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