ConnectionFromClient.cpp 46 KB

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