ConnectionFromClient.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. /*
  2. * Copyright (c) 2020-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.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, Andrew Kaster <akaster@serenityos.org>
  8. *
  9. * SPDX-License-Identifier: BSD-2-Clause
  10. */
  11. #include <AK/Debug.h>
  12. #include <AK/JsonObject.h>
  13. #include <AK/QuickSort.h>
  14. #include <LibGfx/Bitmap.h>
  15. #include <LibGfx/Font/FontDatabase.h>
  16. #include <LibGfx/SystemTheme.h>
  17. #include <LibJS/Bytecode/Interpreter.h>
  18. #include <LibJS/Console.h>
  19. #include <LibJS/Heap/Heap.h>
  20. #include <LibJS/Runtime/ConsoleObject.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/Text.h>
  30. #include <LibWeb/Dump.h>
  31. #include <LibWeb/HTML/BrowsingContext.h>
  32. #include <LibWeb/HTML/Scripting/ClassicScript.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/Namespace.h>
  42. #include <LibWeb/Painting/StackingContext.h>
  43. #include <LibWeb/Painting/ViewportPaintable.h>
  44. #include <LibWeb/PermissionsPolicy/AutoplayAllowlist.h>
  45. #include <LibWeb/Platform/EventLoopPlugin.h>
  46. #include <LibWebView/Attribute.h>
  47. #include <WebContent/ConnectionFromClient.h>
  48. #include <WebContent/PageClient.h>
  49. #include <WebContent/PageHost.h>
  50. #include <WebContent/WebContentClientEndpoint.h>
  51. #include <pthread.h>
  52. namespace WebContent {
  53. ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket> socket)
  54. : IPC::ConnectionFromClient<WebContentClientEndpoint, WebContentServerEndpoint>(*this, move(socket), 1)
  55. , m_page_host(PageHost::create(*this))
  56. {
  57. m_input_event_queue_timer = Web::Platform::Timer::create_single_shot(0, [this] { process_next_input_event(); });
  58. }
  59. void ConnectionFromClient::die()
  60. {
  61. Web::Platform::EventLoopPlugin::the().quit();
  62. }
  63. PageClient& ConnectionFromClient::page(u64 index)
  64. {
  65. return m_page_host->page(index);
  66. }
  67. PageClient const& ConnectionFromClient::page(u64 index) const
  68. {
  69. return m_page_host->page(index);
  70. }
  71. Messages::WebContentServer::GetWindowHandleResponse ConnectionFromClient::get_window_handle()
  72. {
  73. return page().page().top_level_browsing_context().window_handle();
  74. }
  75. void ConnectionFromClient::set_window_handle(String const& handle)
  76. {
  77. page().page().top_level_browsing_context().set_window_handle(handle);
  78. }
  79. void ConnectionFromClient::connect_to_webdriver(ByteString const& webdriver_ipc_path)
  80. {
  81. // FIXME: Propagate this error back to the browser.
  82. if (auto result = page().connect_to_webdriver(webdriver_ipc_path); result.is_error())
  83. dbgln("Unable to connect to the WebDriver process: {}", result.error());
  84. }
  85. void ConnectionFromClient::update_system_theme(Core::AnonymousBuffer const& theme_buffer)
  86. {
  87. Gfx::set_system_theme(theme_buffer);
  88. auto impl = Gfx::PaletteImpl::create_with_anonymous_buffer(theme_buffer);
  89. page().set_palette_impl(*impl);
  90. }
  91. void ConnectionFromClient::update_system_fonts(ByteString const& default_font_query, ByteString const& fixed_width_font_query, ByteString const& window_title_font_query)
  92. {
  93. Gfx::FontDatabase::set_default_font_query(default_font_query);
  94. Gfx::FontDatabase::set_fixed_width_font_query(fixed_width_font_query);
  95. Gfx::FontDatabase::set_window_title_font_query(window_title_font_query);
  96. }
  97. void ConnectionFromClient::update_screen_rects(Vector<Web::DevicePixelRect> const& rects, u32 main_screen)
  98. {
  99. page().set_screen_rects(rects, main_screen);
  100. }
  101. void ConnectionFromClient::load_url(const URL& url)
  102. {
  103. dbgln_if(SPAM_DEBUG, "handle: WebContentServer::LoadURL: url={}", url);
  104. #if defined(AK_OS_SERENITY)
  105. ByteString process_name;
  106. if (url.host().has<Empty>() || url.host() == String {})
  107. process_name = "WebContent";
  108. else
  109. process_name = ByteString::formatted("WebContent: {}", url.serialized_host().release_value_but_fixme_should_propagate_errors());
  110. pthread_setname_np(pthread_self(), process_name.characters());
  111. #endif
  112. page().page().load(url);
  113. }
  114. void ConnectionFromClient::load_html(ByteString const& html)
  115. {
  116. dbgln_if(SPAM_DEBUG, "handle: WebContentServer::LoadHTML: html={}", html);
  117. page().page().load_html(html);
  118. }
  119. void ConnectionFromClient::set_viewport_rect(Web::DevicePixelRect const& rect)
  120. {
  121. dbgln_if(SPAM_DEBUG, "handle: WebContentServer::SetViewportRect: rect={}", rect);
  122. page().set_viewport_rect(rect);
  123. }
  124. void ConnectionFromClient::add_backing_store(i32 front_bitmap_id, Gfx::ShareableBitmap const& front_bitmap, i32 back_bitmap_id, Gfx::ShareableBitmap const& back_bitmap)
  125. {
  126. m_backing_stores.front_bitmap_id = front_bitmap_id;
  127. m_backing_stores.back_bitmap_id = back_bitmap_id;
  128. m_backing_stores.front_bitmap = *const_cast<Gfx::ShareableBitmap&>(front_bitmap).bitmap();
  129. m_backing_stores.back_bitmap = *const_cast<Gfx::ShareableBitmap&>(back_bitmap).bitmap();
  130. }
  131. void ConnectionFromClient::ready_to_paint()
  132. {
  133. page().ready_to_paint();
  134. }
  135. void ConnectionFromClient::process_next_input_event()
  136. {
  137. if (m_input_event_queue.is_empty())
  138. return;
  139. auto event = m_input_event_queue.dequeue();
  140. event.visit(
  141. [&](QueuedMouseEvent const& event) {
  142. switch (event.type) {
  143. case QueuedMouseEvent::Type::MouseDown:
  144. report_finished_handling_input_event(page().page().handle_mousedown(
  145. event.position, event.screen_position,
  146. event.button, event.buttons, event.modifiers));
  147. break;
  148. case QueuedMouseEvent::Type::MouseUp:
  149. report_finished_handling_input_event(page().page().handle_mouseup(
  150. event.position, event.screen_position,
  151. event.button, event.buttons, event.modifiers));
  152. break;
  153. case QueuedMouseEvent::Type::MouseMove:
  154. // NOTE: We have to notify the client about coalesced MouseMoves,
  155. // so we do that by saying none of them were handled by the web page.
  156. for (size_t i = 0; i < event.coalesced_event_count; ++i) {
  157. report_finished_handling_input_event(false);
  158. }
  159. report_finished_handling_input_event(page().page().handle_mousemove(
  160. event.position, event.screen_position,
  161. event.buttons, event.modifiers));
  162. break;
  163. case QueuedMouseEvent::Type::DoubleClick:
  164. report_finished_handling_input_event(page().page().handle_doubleclick(
  165. event.position, event.screen_position,
  166. event.button, event.buttons, event.modifiers));
  167. break;
  168. case QueuedMouseEvent::Type::MouseWheel:
  169. for (size_t i = 0; i < event.coalesced_event_count; ++i) {
  170. report_finished_handling_input_event(false);
  171. }
  172. report_finished_handling_input_event(page().page().handle_mousewheel(
  173. event.position, event.screen_position,
  174. event.button, event.buttons, event.modifiers,
  175. event.wheel_delta_x, event.wheel_delta_y));
  176. break;
  177. }
  178. },
  179. [&](QueuedKeyboardEvent const& event) {
  180. switch (event.type) {
  181. case QueuedKeyboardEvent::Type::KeyDown:
  182. report_finished_handling_input_event(page().page().handle_keydown((KeyCode)event.key, event.modifiers, event.code_point));
  183. break;
  184. case QueuedKeyboardEvent::Type::KeyUp:
  185. report_finished_handling_input_event(page().page().handle_keyup((KeyCode)event.key, event.modifiers, event.code_point));
  186. break;
  187. }
  188. });
  189. if (!m_input_event_queue.is_empty())
  190. m_input_event_queue_timer->start();
  191. }
  192. void ConnectionFromClient::mouse_down(Web::DevicePixelPoint position, Web::DevicePixelPoint screen_position, u32 button, u32 buttons, u32 modifiers)
  193. {
  194. enqueue_input_event(
  195. QueuedMouseEvent {
  196. .type = QueuedMouseEvent::Type::MouseDown,
  197. .position = position,
  198. .screen_position = screen_position,
  199. .button = button,
  200. .buttons = buttons,
  201. .modifiers = modifiers,
  202. });
  203. }
  204. void ConnectionFromClient::mouse_move(Web::DevicePixelPoint position, Web::DevicePixelPoint screen_position, [[maybe_unused]] u32 button, u32 buttons, u32 modifiers)
  205. {
  206. auto event = QueuedMouseEvent {
  207. .type = QueuedMouseEvent::Type::MouseMove,
  208. .position = position,
  209. .screen_position = screen_position,
  210. .button = button,
  211. .buttons = buttons,
  212. .modifiers = modifiers,
  213. };
  214. // OPTIMIZATION: Coalesce with previous unprocessed event iff the previous event is also a MouseMove event.
  215. if (!m_input_event_queue.is_empty()
  216. && m_input_event_queue.tail().has<QueuedMouseEvent>()
  217. && m_input_event_queue.tail().get<QueuedMouseEvent>().type == QueuedMouseEvent::Type::MouseMove) {
  218. event.coalesced_event_count = m_input_event_queue.tail().get<QueuedMouseEvent>().coalesced_event_count + 1;
  219. m_input_event_queue.tail() = event;
  220. return;
  221. }
  222. enqueue_input_event(move(event));
  223. }
  224. void ConnectionFromClient::mouse_up(Web::DevicePixelPoint position, Web::DevicePixelPoint screen_position, u32 button, u32 buttons, u32 modifiers)
  225. {
  226. enqueue_input_event(
  227. QueuedMouseEvent {
  228. .type = QueuedMouseEvent::Type::MouseUp,
  229. .position = position,
  230. .screen_position = screen_position,
  231. .button = button,
  232. .buttons = buttons,
  233. .modifiers = modifiers,
  234. });
  235. }
  236. void ConnectionFromClient::mouse_wheel(Web::DevicePixelPoint position, Web::DevicePixelPoint screen_position, u32 button, u32 buttons, u32 modifiers, Web::DevicePixels wheel_delta_x, Web::DevicePixels wheel_delta_y)
  237. {
  238. auto event = QueuedMouseEvent {
  239. .type = QueuedMouseEvent::Type::MouseWheel,
  240. .position = position,
  241. .screen_position = screen_position,
  242. .button = button,
  243. .buttons = buttons,
  244. .modifiers = modifiers,
  245. .wheel_delta_x = wheel_delta_x,
  246. .wheel_delta_y = wheel_delta_y,
  247. };
  248. // OPTIMIZATION: Coalesce with previous unprocessed event if the previous event is also a MouseWheel event.
  249. if (!m_input_event_queue.is_empty()
  250. && m_input_event_queue.tail().has<QueuedMouseEvent>()
  251. && m_input_event_queue.tail().get<QueuedMouseEvent>().type == QueuedMouseEvent::Type::MouseWheel) {
  252. auto const& last_event = m_input_event_queue.tail().get<QueuedMouseEvent>();
  253. event.coalesced_event_count = last_event.coalesced_event_count + 1;
  254. event.wheel_delta_x += last_event.wheel_delta_x;
  255. event.wheel_delta_y += last_event.wheel_delta_y;
  256. m_input_event_queue.tail() = event;
  257. return;
  258. }
  259. enqueue_input_event(move(event));
  260. }
  261. void ConnectionFromClient::doubleclick(Web::DevicePixelPoint position, Web::DevicePixelPoint screen_position, u32 button, u32 buttons, u32 modifiers)
  262. {
  263. enqueue_input_event(
  264. QueuedMouseEvent {
  265. .type = QueuedMouseEvent::Type::DoubleClick,
  266. .position = position,
  267. .screen_position = screen_position,
  268. .button = button,
  269. .buttons = buttons,
  270. .modifiers = modifiers,
  271. });
  272. }
  273. void ConnectionFromClient::key_down(i32 key, u32 modifiers, u32 code_point)
  274. {
  275. enqueue_input_event(
  276. QueuedKeyboardEvent {
  277. .type = QueuedKeyboardEvent::Type::KeyDown,
  278. .key = key,
  279. .modifiers = modifiers,
  280. .code_point = code_point,
  281. });
  282. }
  283. void ConnectionFromClient::key_up(i32 key, u32 modifiers, u32 code_point)
  284. {
  285. enqueue_input_event(
  286. QueuedKeyboardEvent {
  287. .type = QueuedKeyboardEvent::Type::KeyUp,
  288. .key = key,
  289. .modifiers = modifiers,
  290. .code_point = code_point,
  291. });
  292. }
  293. void ConnectionFromClient::enqueue_input_event(Variant<QueuedMouseEvent, QueuedKeyboardEvent> event)
  294. {
  295. m_input_event_queue.enqueue(move(event));
  296. m_input_event_queue_timer->start();
  297. }
  298. void ConnectionFromClient::report_finished_handling_input_event(bool event_was_handled)
  299. {
  300. async_did_finish_handling_input_event(event_was_handled);
  301. }
  302. void ConnectionFromClient::debug_request(ByteString const& request, ByteString const& argument)
  303. {
  304. if (request == "dump-session-history") {
  305. auto const& traversable = page().page().top_level_traversable();
  306. Web::dump_tree(*traversable);
  307. }
  308. if (request == "dump-dom-tree") {
  309. if (auto* doc = page().page().top_level_browsing_context().active_document())
  310. Web::dump_tree(*doc);
  311. return;
  312. }
  313. if (request == "dump-layout-tree") {
  314. if (auto* doc = page().page().top_level_browsing_context().active_document()) {
  315. if (auto* viewport = doc->layout_node())
  316. Web::dump_tree(*viewport);
  317. }
  318. return;
  319. }
  320. if (request == "dump-paint-tree") {
  321. if (auto* doc = page().page().top_level_browsing_context().active_document()) {
  322. if (auto* paintable = doc->paintable())
  323. Web::dump_tree(*paintable);
  324. }
  325. return;
  326. }
  327. if (request == "dump-stacking-context-tree") {
  328. if (auto* doc = page().page().top_level_browsing_context().active_document()) {
  329. if (auto* viewport = doc->layout_node()) {
  330. if (auto* stacking_context = viewport->paintable_box()->stacking_context())
  331. stacking_context->dump();
  332. }
  333. }
  334. return;
  335. }
  336. if (request == "dump-style-sheets") {
  337. if (auto* doc = page().page().top_level_browsing_context().active_document()) {
  338. for (auto& sheet : doc->style_sheets().sheets()) {
  339. if (auto result = Web::dump_sheet(sheet); result.is_error())
  340. dbgln("Failed to dump style sheets: {}", result.error());
  341. }
  342. }
  343. return;
  344. }
  345. if (request == "dump-all-resolved-styles") {
  346. if (auto* doc = page().page().top_level_browsing_context().active_document()) {
  347. Queue<Web::DOM::Node*> elements_to_visit;
  348. elements_to_visit.enqueue(doc->document_element());
  349. while (!elements_to_visit.is_empty()) {
  350. auto element = elements_to_visit.dequeue();
  351. for (auto& child : element->children_as_vector())
  352. elements_to_visit.enqueue(child.ptr());
  353. if (element->is_element()) {
  354. auto styles = doc->style_computer().compute_style(*static_cast<Web::DOM::Element*>(element)).release_value_but_fixme_should_propagate_errors();
  355. dbgln("+ Element {}", element->debug_description());
  356. auto& properties = styles->properties();
  357. for (size_t i = 0; i < properties.size(); ++i)
  358. dbgln("| {} = {}", Web::CSS::string_from_property_id(static_cast<Web::CSS::PropertyID>(i)), properties[i].has_value() ? properties[i]->style->to_string() : ""_string);
  359. dbgln("---");
  360. }
  361. }
  362. }
  363. return;
  364. }
  365. if (request == "collect-garbage") {
  366. Web::Bindings::main_thread_vm().heap().collect_garbage(JS::Heap::CollectionType::CollectGarbage, true);
  367. return;
  368. }
  369. if (request == "set-line-box-borders") {
  370. bool state = argument == "on";
  371. page().set_should_show_line_box_borders(state);
  372. page().page().top_level_traversable()->set_needs_display(page().page().top_level_traversable()->viewport_rect());
  373. return;
  374. }
  375. if (request == "clear-cache") {
  376. Web::ResourceLoader::the().clear_cache();
  377. return;
  378. }
  379. if (request == "spoof-user-agent") {
  380. Web::ResourceLoader::the().set_user_agent(MUST(String::from_byte_string(argument)));
  381. return;
  382. }
  383. if (request == "same-origin-policy") {
  384. page().page().set_same_origin_policy_enabled(argument == "on");
  385. return;
  386. }
  387. if (request == "scripting") {
  388. page().page().set_is_scripting_enabled(argument == "on");
  389. return;
  390. }
  391. if (request == "block-pop-ups") {
  392. page().page().set_should_block_pop_ups(argument == "on");
  393. return;
  394. }
  395. if (request == "dump-local-storage") {
  396. if (auto* document = page().page().top_level_browsing_context().active_document())
  397. document->window().local_storage().release_value_but_fixme_should_propagate_errors()->dump();
  398. return;
  399. }
  400. if (request == "load-reference-page") {
  401. if (auto* document = page().page().top_level_browsing_context().active_document()) {
  402. auto maybe_link = document->query_selector("link[rel=match]"sv);
  403. if (maybe_link.is_error() || !maybe_link.value()) {
  404. // To make sure that we fail the ref-test if the link is missing, load the error page.
  405. load_html("<h1>Failed to find &lt;link rel=&quot;match&quot; /&gt; in ref test page!</h1> Make sure you added it.");
  406. } else {
  407. auto link = maybe_link.release_value();
  408. auto url = document->parse_url(link->deprecated_get_attribute(Web::HTML::AttributeNames::href));
  409. load_url(url);
  410. }
  411. }
  412. return;
  413. }
  414. }
  415. void ConnectionFromClient::get_source()
  416. {
  417. if (auto* doc = page().page().top_level_browsing_context().active_document()) {
  418. async_did_get_source(doc->url(), doc->source().to_byte_string());
  419. }
  420. }
  421. void ConnectionFromClient::inspect_dom_tree()
  422. {
  423. if (auto* doc = page().page().top_level_browsing_context().active_document()) {
  424. async_did_inspect_dom_tree(doc->dump_dom_tree_as_json().to_byte_string());
  425. }
  426. }
  427. void ConnectionFromClient::inspect_dom_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement::Type> const& pseudo_element)
  428. {
  429. auto& top_context = page().page().top_level_browsing_context();
  430. top_context.for_each_in_inclusive_subtree([&](auto& ctx) {
  431. if (ctx.active_document() != nullptr) {
  432. ctx.active_document()->set_inspected_node(nullptr, {});
  433. }
  434. return IterationDecision::Continue;
  435. });
  436. Web::DOM::Node* node = Web::DOM::Node::from_unique_id(node_id);
  437. // Note: Nodes without layout (aka non-visible nodes, don't have style computed)
  438. if (!node || !node->layout_node()) {
  439. async_did_inspect_dom_node(false, {}, {}, {}, {}, {});
  440. return;
  441. }
  442. node->document().set_inspected_node(node, pseudo_element);
  443. if (node->is_element()) {
  444. auto& element = verify_cast<Web::DOM::Element>(*node);
  445. if (!element.computed_css_values()) {
  446. async_did_inspect_dom_node(false, {}, {}, {}, {}, {});
  447. return;
  448. }
  449. auto serialize_json = [](Web::CSS::StyleProperties const& properties) -> ByteString {
  450. StringBuilder builder;
  451. auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
  452. properties.for_each_property([&](auto property_id, auto& value) {
  453. MUST(serializer.add(Web::CSS::string_from_property_id(property_id), value.to_string().to_byte_string()));
  454. });
  455. MUST(serializer.finish());
  456. return builder.to_byte_string();
  457. };
  458. auto serialize_custom_properties_json = [](Web::DOM::Element const& element, Optional<Web::CSS::Selector::PseudoElement::Type> pseudo_element) -> ByteString {
  459. StringBuilder builder;
  460. auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
  461. HashTable<FlyString> seen_properties;
  462. auto const* element_to_check = &element;
  463. while (element_to_check) {
  464. for (auto const& property : element_to_check->custom_properties(pseudo_element)) {
  465. if (!seen_properties.contains(property.key)) {
  466. seen_properties.set(property.key);
  467. MUST(serializer.add(property.key, property.value.value->to_string()));
  468. }
  469. }
  470. element_to_check = element_to_check->parent_element();
  471. }
  472. MUST(serializer.finish());
  473. return builder.to_byte_string();
  474. };
  475. auto serialize_node_box_sizing_json = [](Web::Layout::Node const* layout_node) -> ByteString {
  476. if (!layout_node || !layout_node->is_box()) {
  477. return "{}";
  478. }
  479. auto* box = static_cast<Web::Layout::Box const*>(layout_node);
  480. auto box_model = box->box_model();
  481. StringBuilder builder;
  482. auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
  483. MUST(serializer.add("padding_top"sv, box_model.padding.top.to_double()));
  484. MUST(serializer.add("padding_right"sv, box_model.padding.right.to_double()));
  485. MUST(serializer.add("padding_bottom"sv, box_model.padding.bottom.to_double()));
  486. MUST(serializer.add("padding_left"sv, box_model.padding.left.to_double()));
  487. MUST(serializer.add("margin_top"sv, box_model.margin.top.to_double()));
  488. MUST(serializer.add("margin_right"sv, box_model.margin.right.to_double()));
  489. MUST(serializer.add("margin_bottom"sv, box_model.margin.bottom.to_double()));
  490. MUST(serializer.add("margin_left"sv, box_model.margin.left.to_double()));
  491. MUST(serializer.add("border_top"sv, box_model.border.top.to_double()));
  492. MUST(serializer.add("border_right"sv, box_model.border.right.to_double()));
  493. MUST(serializer.add("border_bottom"sv, box_model.border.bottom.to_double()));
  494. MUST(serializer.add("border_left"sv, box_model.border.left.to_double()));
  495. if (auto* paintable_box = box->paintable_box()) {
  496. MUST(serializer.add("content_width"sv, paintable_box->content_width().to_double()));
  497. MUST(serializer.add("content_height"sv, paintable_box->content_height().to_double()));
  498. } else {
  499. MUST(serializer.add("content_width"sv, 0));
  500. MUST(serializer.add("content_height"sv, 0));
  501. }
  502. MUST(serializer.finish());
  503. return builder.to_byte_string();
  504. };
  505. auto serialize_aria_properties_state_json = [](Web::DOM::Element const& element) -> ByteString {
  506. auto role_name = element.role_or_default();
  507. if (!role_name.has_value()) {
  508. return "";
  509. }
  510. auto aria_data = MUST(Web::ARIA::AriaData::build_data(element));
  511. auto role = MUST(Web::ARIA::RoleType::build_role_object(role_name.value(), element.is_focusable(), *aria_data));
  512. StringBuilder builder;
  513. auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
  514. MUST(role->serialize_as_json(serializer));
  515. MUST(serializer.finish());
  516. return builder.to_byte_string();
  517. };
  518. if (pseudo_element.has_value()) {
  519. auto pseudo_element_node = element.get_pseudo_element_node(pseudo_element.value());
  520. if (!pseudo_element_node) {
  521. async_did_inspect_dom_node(false, {}, {}, {}, {}, {});
  522. return;
  523. }
  524. // FIXME: Pseudo-elements only exist as Layout::Nodes, which don't have style information
  525. // in a format we can use. So, we run the StyleComputer again to get the specified
  526. // values, and have to ignore the computed values and custom properties.
  527. auto pseudo_element_style = MUST(page().page().focused_context().active_document()->style_computer().compute_style(element, pseudo_element));
  528. ByteString computed_values = serialize_json(pseudo_element_style);
  529. ByteString resolved_values = "{}";
  530. ByteString custom_properties_json = serialize_custom_properties_json(element, pseudo_element);
  531. ByteString node_box_sizing_json = serialize_node_box_sizing_json(pseudo_element_node.ptr());
  532. async_did_inspect_dom_node(true, move(computed_values), move(resolved_values), move(custom_properties_json), move(node_box_sizing_json), {});
  533. return;
  534. }
  535. ByteString computed_values = serialize_json(*element.computed_css_values());
  536. ByteString resolved_values = serialize_json(element.resolved_css_values());
  537. ByteString custom_properties_json = serialize_custom_properties_json(element, {});
  538. ByteString node_box_sizing_json = serialize_node_box_sizing_json(element.layout_node());
  539. ByteString aria_properties_state_json = serialize_aria_properties_state_json(element);
  540. async_did_inspect_dom_node(true, move(computed_values), move(resolved_values), move(custom_properties_json), move(node_box_sizing_json), move(aria_properties_state_json));
  541. return;
  542. }
  543. async_did_inspect_dom_node(false, {}, {}, {}, {}, {});
  544. }
  545. void ConnectionFromClient::inspect_accessibility_tree()
  546. {
  547. if (auto* doc = page().page().top_level_browsing_context().active_document()) {
  548. async_did_inspect_accessibility_tree(doc->dump_accessibility_tree_as_json().to_byte_string());
  549. }
  550. }
  551. void ConnectionFromClient::get_hovered_node_id()
  552. {
  553. i32 node_id = 0;
  554. if (auto* document = page().page().top_level_browsing_context().active_document()) {
  555. if (auto* hovered_node = document->hovered_node())
  556. node_id = hovered_node->unique_id();
  557. }
  558. async_did_get_hovered_node_id(node_id);
  559. }
  560. void ConnectionFromClient::set_dom_node_text(i32 node_id, String const& text)
  561. {
  562. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  563. if (!dom_node || (!dom_node->is_text() && !dom_node->is_comment())) {
  564. async_did_finish_editing_dom_node({});
  565. return;
  566. }
  567. auto& character_data = static_cast<Web::DOM::CharacterData&>(*dom_node);
  568. character_data.set_data(text);
  569. async_did_finish_editing_dom_node(character_data.unique_id());
  570. }
  571. void ConnectionFromClient::set_dom_node_tag(i32 node_id, String const& name)
  572. {
  573. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  574. if (!dom_node || !dom_node->is_element() || !dom_node->parent()) {
  575. async_did_finish_editing_dom_node({});
  576. return;
  577. }
  578. auto& element = static_cast<Web::DOM::Element&>(*dom_node);
  579. 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();
  580. element.for_each_attribute([&](auto const& attribute) {
  581. new_element->set_attribute_value(attribute.local_name(), attribute.value(), attribute.prefix(), attribute.namespace_uri());
  582. });
  583. while (auto* child_node = element.first_child()) {
  584. MUST(element.remove_child(*child_node));
  585. MUST(new_element->append_child(*child_node));
  586. }
  587. element.parent()->replace_child(*new_element, element).release_value_but_fixme_should_propagate_errors();
  588. async_did_finish_editing_dom_node(new_element->unique_id());
  589. }
  590. void ConnectionFromClient::add_dom_node_attributes(i32 node_id, Vector<WebView::Attribute> const& attributes)
  591. {
  592. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  593. if (!dom_node || !dom_node->is_element()) {
  594. async_did_finish_editing_dom_node({});
  595. return;
  596. }
  597. auto& element = static_cast<Web::DOM::Element&>(*dom_node);
  598. for (auto const& attribute : attributes)
  599. element.set_attribute(attribute.name, attribute.value).release_value_but_fixme_should_propagate_errors();
  600. async_did_finish_editing_dom_node(element.unique_id());
  601. }
  602. void ConnectionFromClient::replace_dom_node_attribute(i32 node_id, String const& name, Vector<WebView::Attribute> const& replacement_attributes)
  603. {
  604. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  605. if (!dom_node || !dom_node->is_element()) {
  606. async_did_finish_editing_dom_node({});
  607. return;
  608. }
  609. auto& element = static_cast<Web::DOM::Element&>(*dom_node);
  610. bool should_remove_attribute = true;
  611. for (auto const& attribute : replacement_attributes) {
  612. if (should_remove_attribute && Web::Infra::is_ascii_case_insensitive_match(name, attribute.name))
  613. should_remove_attribute = false;
  614. element.set_attribute(attribute.name, attribute.value).release_value_but_fixme_should_propagate_errors();
  615. }
  616. if (should_remove_attribute)
  617. element.remove_attribute(name);
  618. async_did_finish_editing_dom_node(element.unique_id());
  619. }
  620. void ConnectionFromClient::create_child_element(i32 node_id)
  621. {
  622. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  623. if (!dom_node) {
  624. async_did_finish_editing_dom_node({});
  625. return;
  626. }
  627. auto element = Web::DOM::create_element(dom_node->document(), Web::HTML::TagNames::div, Web::Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  628. dom_node->append_child(element).release_value_but_fixme_should_propagate_errors();
  629. async_did_finish_editing_dom_node(element->unique_id());
  630. }
  631. void ConnectionFromClient::create_child_text_node(i32 node_id)
  632. {
  633. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  634. if (!dom_node) {
  635. async_did_finish_editing_dom_node({});
  636. return;
  637. }
  638. auto text_node = dom_node->heap().allocate<Web::DOM::Text>(dom_node->realm(), dom_node->document(), "text"_string);
  639. dom_node->append_child(text_node).release_value_but_fixme_should_propagate_errors();
  640. async_did_finish_editing_dom_node(text_node->unique_id());
  641. }
  642. void ConnectionFromClient::clone_dom_node(i32 node_id)
  643. {
  644. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  645. if (!dom_node || !dom_node->parent_node()) {
  646. async_did_finish_editing_dom_node({});
  647. return;
  648. }
  649. auto dom_node_clone = dom_node->clone_node(nullptr, true);
  650. dom_node->parent_node()->insert_before(dom_node_clone, dom_node->next_sibling());
  651. async_did_finish_editing_dom_node(dom_node_clone->unique_id());
  652. }
  653. void ConnectionFromClient::remove_dom_node(i32 node_id)
  654. {
  655. auto* active_document = page().page().top_level_browsing_context().active_document();
  656. if (!active_document) {
  657. async_did_finish_editing_dom_node({});
  658. return;
  659. }
  660. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  661. if (!dom_node) {
  662. async_did_finish_editing_dom_node({});
  663. return;
  664. }
  665. auto* previous_dom_node = dom_node->previous_sibling();
  666. if (!previous_dom_node)
  667. previous_dom_node = dom_node->parent();
  668. dom_node->remove();
  669. // FIXME: When nodes are removed from the DOM, the associated layout nodes become stale and still
  670. // remain in the layout tree. This has to be fixed, this just causes everything to be recomputed
  671. // which really hurts performance.
  672. active_document->force_layout();
  673. async_did_finish_editing_dom_node(previous_dom_node->unique_id());
  674. }
  675. void ConnectionFromClient::get_dom_node_html(i32 node_id)
  676. {
  677. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  678. if (!dom_node)
  679. return;
  680. // FIXME: Implement Element's outerHTML attribute.
  681. auto container = Web::DOM::create_element(dom_node->document(), Web::HTML::TagNames::div, Web::Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  682. container->append_child(dom_node->clone_node(nullptr, true)).release_value_but_fixme_should_propagate_errors();
  683. auto html = container->inner_html().release_value_but_fixme_should_propagate_errors();
  684. async_did_get_dom_node_html(move(html));
  685. }
  686. void ConnectionFromClient::initialize_js_console(Badge<PageClient>, Web::DOM::Document& document)
  687. {
  688. auto& realm = document.realm();
  689. auto console_object = realm.intrinsics().console_object();
  690. auto console_client = make<WebContentConsoleClient>(console_object->console(), document.realm(), *this);
  691. console_object->console().set_client(*console_client);
  692. VERIFY(document.browsing_context());
  693. if (document.browsing_context()->is_top_level()) {
  694. m_top_level_document_console_client = console_client->make_weak_ptr();
  695. }
  696. m_console_clients.set(&document, move(console_client));
  697. }
  698. void ConnectionFromClient::destroy_js_console(Badge<PageClient>, Web::DOM::Document& document)
  699. {
  700. m_console_clients.remove(&document);
  701. }
  702. void ConnectionFromClient::js_console_input(ByteString const& js_source)
  703. {
  704. if (m_top_level_document_console_client)
  705. m_top_level_document_console_client->handle_input(js_source);
  706. }
  707. void ConnectionFromClient::run_javascript(ByteString const& js_source)
  708. {
  709. auto* active_document = page().page().top_level_browsing_context().active_document();
  710. if (!active_document)
  711. return;
  712. // This is partially based on "execute a javascript: URL request" https://html.spec.whatwg.org/multipage/browsing-the-web.html#javascript-protocol
  713. // Let settings be browsingContext's active document's relevant settings object.
  714. auto& settings = active_document->relevant_settings_object();
  715. // Let baseURL be settings's API base URL.
  716. auto base_url = settings.api_base_url();
  717. // Let script be the result of creating a classic script given scriptSource, settings, baseURL, and the default classic script fetch options.
  718. // FIXME: This doesn't pass in "default classic script fetch options"
  719. // FIXME: What should the filename be here?
  720. auto script = Web::HTML::ClassicScript::create("(client connection run_javascript)", js_source, settings, move(base_url));
  721. // Let evaluationStatus be the result of running the classic script script.
  722. auto evaluation_status = script->run();
  723. if (evaluation_status.is_error())
  724. dbgln("Exception :(");
  725. }
  726. void ConnectionFromClient::js_console_request_messages(i32 start_index)
  727. {
  728. if (m_top_level_document_console_client)
  729. m_top_level_document_console_client->send_messages(start_index);
  730. }
  731. void ConnectionFromClient::take_document_screenshot()
  732. {
  733. auto* document = page().page().top_level_browsing_context().active_document();
  734. if (!document || !document->document_element()) {
  735. async_did_take_screenshot({});
  736. return;
  737. }
  738. auto const& content_size = page().content_size();
  739. Web::DevicePixelRect rect { { 0, 0 }, content_size };
  740. auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, rect.size().to_type<int>()).release_value_but_fixme_should_propagate_errors();
  741. page().paint(rect, *bitmap);
  742. async_did_take_screenshot(bitmap->to_shareable_bitmap());
  743. }
  744. void ConnectionFromClient::take_dom_node_screenshot(i32 node_id)
  745. {
  746. auto* dom_node = Web::DOM::Node::from_unique_id(node_id);
  747. if (!dom_node || !dom_node->paintable_box()) {
  748. async_did_take_screenshot({});
  749. return;
  750. }
  751. auto rect = page().page().enclosing_device_rect(dom_node->paintable_box()->absolute_border_box_rect());
  752. auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, rect.size().to_type<int>()).release_value_but_fixme_should_propagate_errors();
  753. page().paint(rect, *bitmap, { .paint_overlay = Web::PaintOptions::PaintOverlay::No });
  754. async_did_take_screenshot(bitmap->to_shareable_bitmap());
  755. }
  756. Messages::WebContentServer::DumpGcGraphResponse ConnectionFromClient::dump_gc_graph()
  757. {
  758. auto gc_graph_json = Web::Bindings::main_thread_vm().heap().dump_graph();
  759. return MUST(String::from_byte_string(gc_graph_json.to_byte_string()));
  760. }
  761. Messages::WebContentServer::GetSelectedTextResponse ConnectionFromClient::get_selected_text()
  762. {
  763. return page().page().focused_context().selected_text().to_byte_string();
  764. }
  765. void ConnectionFromClient::select_all()
  766. {
  767. page().page().focused_context().select_all();
  768. }
  769. Messages::WebContentServer::DumpLayoutTreeResponse ConnectionFromClient::dump_layout_tree()
  770. {
  771. auto* document = page().page().top_level_browsing_context().active_document();
  772. if (!document)
  773. return ByteString { "(no DOM tree)" };
  774. document->update_layout();
  775. auto* layout_root = document->layout_node();
  776. if (!layout_root)
  777. return ByteString { "(no layout tree)" };
  778. StringBuilder builder;
  779. Web::dump_tree(builder, *layout_root);
  780. return builder.to_byte_string();
  781. }
  782. Messages::WebContentServer::DumpPaintTreeResponse ConnectionFromClient::dump_paint_tree()
  783. {
  784. auto* document = page().page().top_level_browsing_context().active_document();
  785. if (!document)
  786. return ByteString { "(no DOM tree)" };
  787. document->update_layout();
  788. auto* layout_root = document->layout_node();
  789. if (!layout_root)
  790. return ByteString { "(no layout tree)" };
  791. if (!layout_root->paintable())
  792. return ByteString { "(no paint tree)" };
  793. StringBuilder builder;
  794. Web::dump_tree(builder, *layout_root->paintable());
  795. return builder.to_byte_string();
  796. }
  797. Messages::WebContentServer::DumpTextResponse ConnectionFromClient::dump_text()
  798. {
  799. auto* document = page().page().top_level_browsing_context().active_document();
  800. if (!document)
  801. return ByteString { "(no DOM tree)" };
  802. if (!document->body())
  803. return ByteString { "(no body)" };
  804. return document->body()->inner_text();
  805. }
  806. void ConnectionFromClient::set_content_filters(Vector<String> const& filters)
  807. {
  808. Web::ContentFilter::the().set_patterns(filters).release_value_but_fixme_should_propagate_errors();
  809. }
  810. void ConnectionFromClient::set_autoplay_allowed_on_all_websites()
  811. {
  812. auto& autoplay_allowlist = Web::PermissionsPolicy::AutoplayAllowlist::the();
  813. autoplay_allowlist.enable_globally();
  814. }
  815. void ConnectionFromClient::set_autoplay_allowlist(Vector<String> const& allowlist)
  816. {
  817. auto& autoplay_allowlist = Web::PermissionsPolicy::AutoplayAllowlist::the();
  818. autoplay_allowlist.enable_for_origins(allowlist).release_value_but_fixme_should_propagate_errors();
  819. }
  820. void ConnectionFromClient::set_proxy_mappings(Vector<ByteString> const& proxies, HashMap<ByteString, size_t> const& mappings)
  821. {
  822. auto keys = mappings.keys();
  823. quick_sort(keys, [&](auto& a, auto& b) { return a.length() < b.length(); });
  824. OrderedHashMap<ByteString, size_t> sorted_mappings;
  825. for (auto& key : keys) {
  826. auto value = *mappings.get(key);
  827. if (value >= proxies.size())
  828. continue;
  829. sorted_mappings.set(key, value);
  830. }
  831. Web::ProxyMappings::the().set_mappings(proxies, move(sorted_mappings));
  832. }
  833. void ConnectionFromClient::set_preferred_color_scheme(Web::CSS::PreferredColorScheme const& color_scheme)
  834. {
  835. page().set_preferred_color_scheme(color_scheme);
  836. }
  837. void ConnectionFromClient::set_has_focus(bool has_focus)
  838. {
  839. page().set_has_focus(has_focus);
  840. }
  841. void ConnectionFromClient::set_is_scripting_enabled(bool is_scripting_enabled)
  842. {
  843. page().set_is_scripting_enabled(is_scripting_enabled);
  844. }
  845. void ConnectionFromClient::set_device_pixels_per_css_pixel(float device_pixels_per_css_pixel)
  846. {
  847. page().set_device_pixels_per_css_pixel(device_pixels_per_css_pixel);
  848. }
  849. void ConnectionFromClient::set_window_position(Web::DevicePixelPoint position)
  850. {
  851. page().set_window_position(position);
  852. }
  853. void ConnectionFromClient::set_window_size(Web::DevicePixelSize size)
  854. {
  855. page().set_window_size(size);
  856. }
  857. Messages::WebContentServer::GetLocalStorageEntriesResponse ConnectionFromClient::get_local_storage_entries()
  858. {
  859. auto* document = page().page().top_level_browsing_context().active_document();
  860. auto local_storage = document->window().local_storage().release_value_but_fixme_should_propagate_errors();
  861. return local_storage->map();
  862. }
  863. Messages::WebContentServer::GetSessionStorageEntriesResponse ConnectionFromClient::get_session_storage_entries()
  864. {
  865. auto* document = page().page().top_level_browsing_context().active_document();
  866. auto session_storage = document->window().session_storage().release_value_but_fixme_should_propagate_errors();
  867. return session_storage->map();
  868. }
  869. void ConnectionFromClient::handle_file_return(i32 error, Optional<IPC::File> const& file, i32 request_id)
  870. {
  871. auto file_request = m_requested_files.take(request_id);
  872. VERIFY(file_request.has_value());
  873. VERIFY(file_request.value().on_file_request_finish);
  874. file_request.value().on_file_request_finish(error != 0 ? Error::from_errno(error) : ErrorOr<i32> { file->take_fd() });
  875. }
  876. void ConnectionFromClient::request_file(Web::FileRequest file_request)
  877. {
  878. i32 const id = last_id++;
  879. auto path = file_request.path();
  880. m_requested_files.set(id, move(file_request));
  881. async_did_request_file(path, id);
  882. }
  883. void ConnectionFromClient::set_system_visibility_state(bool visible)
  884. {
  885. page().page().top_level_traversable()->set_system_visibility_state(
  886. visible
  887. ? Web::HTML::VisibilityState::Visible
  888. : Web::HTML::VisibilityState::Hidden);
  889. }
  890. void ConnectionFromClient::alert_closed()
  891. {
  892. page().page().alert_closed();
  893. }
  894. void ConnectionFromClient::confirm_closed(bool accepted)
  895. {
  896. page().page().confirm_closed(accepted);
  897. }
  898. void ConnectionFromClient::prompt_closed(Optional<String> const& response)
  899. {
  900. page().page().prompt_closed(response);
  901. }
  902. void ConnectionFromClient::color_picker_update(Optional<Color> const& picked_color, Web::HTML::ColorPickerUpdateState const& state)
  903. {
  904. page().page().color_picker_update(picked_color, state);
  905. }
  906. void ConnectionFromClient::select_dropdown_closed(Optional<String> const& value)
  907. {
  908. page().page().select_dropdown_closed(value);
  909. }
  910. void ConnectionFromClient::toggle_media_play_state()
  911. {
  912. page().page().toggle_media_play_state().release_value_but_fixme_should_propagate_errors();
  913. }
  914. void ConnectionFromClient::toggle_media_mute_state()
  915. {
  916. page().page().toggle_media_mute_state();
  917. }
  918. void ConnectionFromClient::toggle_media_loop_state()
  919. {
  920. page().page().toggle_media_loop_state().release_value_but_fixme_should_propagate_errors();
  921. }
  922. void ConnectionFromClient::toggle_media_controls_state()
  923. {
  924. page().page().toggle_media_controls_state().release_value_but_fixme_should_propagate_errors();
  925. }
  926. void ConnectionFromClient::set_user_style(String const& source)
  927. {
  928. page().page().set_user_style(source);
  929. }
  930. void ConnectionFromClient::enable_inspector_prototype()
  931. {
  932. Web::HTML::Window::set_inspector_object_exposed(true);
  933. }
  934. }