ConnectionFromClient.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. *
  8. * SPDX-License-Identifier: BSD-2-Clause
  9. */
  10. #include <AK/Debug.h>
  11. #include <AK/JsonObject.h>
  12. #include <AK/QuickSort.h>
  13. #include <LibGfx/Bitmap.h>
  14. #include <LibGfx/Font/FontDatabase.h>
  15. #include <LibGfx/SystemTheme.h>
  16. #include <LibJS/Bytecode/Interpreter.h>
  17. #include <LibJS/Console.h>
  18. #include <LibJS/Heap/Heap.h>
  19. #include <LibJS/Runtime/ConsoleObject.h>
  20. #include <LibWeb/ARIA/RoleType.h>
  21. #include <LibWeb/Bindings/MainThreadVM.h>
  22. #include <LibWeb/CSS/StyleComputer.h>
  23. #include <LibWeb/DOM/Document.h>
  24. #include <LibWeb/Dump.h>
  25. #include <LibWeb/HTML/BrowsingContext.h>
  26. #include <LibWeb/HTML/Scripting/ClassicScript.h>
  27. #include <LibWeb/HTML/Storage.h>
  28. #include <LibWeb/HTML/Window.h>
  29. #include <LibWeb/Layout/Viewport.h>
  30. #include <LibWeb/Loader/ContentFilter.h>
  31. #include <LibWeb/Loader/ProxyMappings.h>
  32. #include <LibWeb/Loader/ResourceLoader.h>
  33. #include <LibWeb/Painting/StackingContext.h>
  34. #include <LibWeb/Painting/ViewportPaintable.h>
  35. #include <LibWeb/PermissionsPolicy/AutoplayAllowlist.h>
  36. #include <LibWeb/Platform/EventLoopPlugin.h>
  37. #include <WebContent/ConnectionFromClient.h>
  38. #include <WebContent/PageHost.h>
  39. #include <WebContent/WebContentClientEndpoint.h>
  40. #include <pthread.h>
  41. namespace WebContent {
  42. ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket> socket)
  43. : IPC::ConnectionFromClient<WebContentClientEndpoint, WebContentServerEndpoint>(*this, move(socket), 1)
  44. , m_page_host(PageHost::create(*this))
  45. {
  46. m_paint_flush_timer = Web::Platform::Timer::create_single_shot(0, [this] { flush_pending_paint_requests(); });
  47. m_input_event_queue_timer = Web::Platform::Timer::create_single_shot(0, [this] { process_next_input_event(); });
  48. }
  49. void ConnectionFromClient::die()
  50. {
  51. Web::Platform::EventLoopPlugin::the().quit();
  52. }
  53. Web::Page& ConnectionFromClient::page()
  54. {
  55. return m_page_host->page();
  56. }
  57. Web::Page const& ConnectionFromClient::page() const
  58. {
  59. return m_page_host->page();
  60. }
  61. Messages::WebContentServer::GetWindowHandleResponse ConnectionFromClient::get_window_handle()
  62. {
  63. return m_page_host->page().top_level_browsing_context().window_handle();
  64. }
  65. void ConnectionFromClient::set_window_handle(String const& handle)
  66. {
  67. m_page_host->page().top_level_browsing_context().set_window_handle(handle);
  68. }
  69. void ConnectionFromClient::connect_to_webdriver(DeprecatedString const& webdriver_ipc_path)
  70. {
  71. // FIXME: Propagate this error back to the browser.
  72. if (auto result = m_page_host->connect_to_webdriver(webdriver_ipc_path); result.is_error())
  73. dbgln("Unable to connect to the WebDriver process: {}", result.error());
  74. }
  75. void ConnectionFromClient::update_system_theme(Core::AnonymousBuffer const& theme_buffer)
  76. {
  77. Gfx::set_system_theme(theme_buffer);
  78. auto impl = Gfx::PaletteImpl::create_with_anonymous_buffer(theme_buffer);
  79. m_page_host->set_palette_impl(*impl);
  80. }
  81. void ConnectionFromClient::update_system_fonts(DeprecatedString const& default_font_query, DeprecatedString const& fixed_width_font_query, DeprecatedString const& window_title_font_query)
  82. {
  83. Gfx::FontDatabase::set_default_font_query(default_font_query);
  84. Gfx::FontDatabase::set_fixed_width_font_query(fixed_width_font_query);
  85. Gfx::FontDatabase::set_window_title_font_query(window_title_font_query);
  86. }
  87. void ConnectionFromClient::update_screen_rects(Vector<Gfx::IntRect> const& rects, u32 main_screen)
  88. {
  89. m_page_host->set_screen_rects(rects, main_screen);
  90. }
  91. void ConnectionFromClient::load_url(const URL& url)
  92. {
  93. dbgln_if(SPAM_DEBUG, "handle: WebContentServer::LoadURL: url={}", url);
  94. #if defined(AK_OS_SERENITY)
  95. DeprecatedString process_name;
  96. if (url.host().has<Empty>() || url.host() == String {})
  97. process_name = "WebContent";
  98. else
  99. process_name = DeprecatedString::formatted("WebContent: {}", url.serialized_host().release_value_but_fixme_should_propagate_errors());
  100. pthread_setname_np(pthread_self(), process_name.characters());
  101. #endif
  102. page().load(url);
  103. }
  104. void ConnectionFromClient::load_html(DeprecatedString const& html, const URL& url)
  105. {
  106. dbgln_if(SPAM_DEBUG, "handle: WebContentServer::LoadHTML: html={}, url={}", html, url);
  107. page().load_html(html, url);
  108. }
  109. void ConnectionFromClient::set_viewport_rect(Gfx::IntRect const& rect)
  110. {
  111. dbgln_if(SPAM_DEBUG, "handle: WebContentServer::SetViewportRect: rect={}", rect);
  112. m_page_host->set_viewport_rect(rect.to_type<Web::DevicePixels>());
  113. }
  114. void ConnectionFromClient::add_backing_store(i32 backing_store_id, Gfx::ShareableBitmap const& bitmap)
  115. {
  116. m_backing_stores.set(backing_store_id, *const_cast<Gfx::ShareableBitmap&>(bitmap).bitmap());
  117. }
  118. void ConnectionFromClient::remove_backing_store(i32 backing_store_id)
  119. {
  120. m_backing_stores.remove(backing_store_id);
  121. m_pending_paint_requests.remove_all_matching([backing_store_id](auto& pending_repaint_request) { return pending_repaint_request.bitmap_id == backing_store_id; });
  122. }
  123. void ConnectionFromClient::paint(Gfx::IntRect const& content_rect, i32 backing_store_id)
  124. {
  125. for (auto& pending_paint : m_pending_paint_requests) {
  126. if (pending_paint.bitmap_id == backing_store_id) {
  127. pending_paint.content_rect = content_rect;
  128. return;
  129. }
  130. }
  131. auto it = m_backing_stores.find(backing_store_id);
  132. if (it == m_backing_stores.end()) {
  133. did_misbehave("Client requested paint with backing store ID");
  134. return;
  135. }
  136. auto& bitmap = *it->value;
  137. m_pending_paint_requests.append({ content_rect, bitmap, backing_store_id });
  138. m_paint_flush_timer->start();
  139. }
  140. void ConnectionFromClient::flush_pending_paint_requests()
  141. {
  142. for (auto& pending_paint : m_pending_paint_requests) {
  143. m_page_host->paint(pending_paint.content_rect.to_type<Web::DevicePixels>(), *pending_paint.bitmap);
  144. async_did_paint(pending_paint.content_rect, pending_paint.bitmap_id);
  145. }
  146. m_pending_paint_requests.clear();
  147. }
  148. void ConnectionFromClient::process_next_input_event()
  149. {
  150. if (m_input_event_queue.is_empty())
  151. return;
  152. auto event = m_input_event_queue.dequeue();
  153. event.visit(
  154. [&](QueuedMouseEvent const& event) {
  155. switch (event.type) {
  156. case QueuedMouseEvent::Type::MouseDown:
  157. report_finished_handling_input_event(page().handle_mousedown(
  158. event.position.to_type<Web::DevicePixels>(),
  159. event.button, event.buttons, event.modifiers));
  160. break;
  161. case QueuedMouseEvent::Type::MouseUp:
  162. report_finished_handling_input_event(page().handle_mouseup(
  163. event.position.to_type<Web::DevicePixels>(),
  164. event.button, event.buttons, event.modifiers));
  165. break;
  166. case QueuedMouseEvent::Type::MouseMove:
  167. // NOTE: We have to notify the client about coalesced MouseMoves,
  168. // so we do that by saying none of them were handled by the web page.
  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().handle_mousemove(
  173. event.position.to_type<Web::DevicePixels>(),
  174. event.buttons, event.modifiers));
  175. break;
  176. case QueuedMouseEvent::Type::DoubleClick:
  177. report_finished_handling_input_event(page().handle_doubleclick(
  178. event.position.to_type<Web::DevicePixels>(),
  179. event.button, event.buttons, event.modifiers));
  180. break;
  181. case QueuedMouseEvent::Type::MouseWheel:
  182. for (size_t i = 0; i < event.coalesced_event_count; ++i) {
  183. report_finished_handling_input_event(false);
  184. }
  185. report_finished_handling_input_event(page().handle_mousewheel(
  186. event.position.to_type<Web::DevicePixels>(),
  187. event.button, event.buttons, event.modifiers, event.wheel_delta_x, event.wheel_delta_y));
  188. break;
  189. }
  190. },
  191. [&](QueuedKeyboardEvent const& event) {
  192. switch (event.type) {
  193. case QueuedKeyboardEvent::Type::KeyDown:
  194. report_finished_handling_input_event(page().handle_keydown((KeyCode)event.key, event.modifiers, event.code_point));
  195. break;
  196. case QueuedKeyboardEvent::Type::KeyUp:
  197. report_finished_handling_input_event(page().handle_keyup((KeyCode)event.key, event.modifiers, event.code_point));
  198. break;
  199. }
  200. });
  201. if (!m_input_event_queue.is_empty())
  202. m_input_event_queue_timer->start();
  203. }
  204. void ConnectionFromClient::mouse_down(Gfx::IntPoint position, unsigned int button, unsigned int buttons, unsigned int modifiers)
  205. {
  206. enqueue_input_event(
  207. QueuedMouseEvent {
  208. .type = QueuedMouseEvent::Type::MouseDown,
  209. .position = position,
  210. .button = button,
  211. .buttons = buttons,
  212. .modifiers = modifiers,
  213. });
  214. }
  215. void ConnectionFromClient::mouse_move(Gfx::IntPoint position, [[maybe_unused]] unsigned int button, unsigned int buttons, unsigned int modifiers)
  216. {
  217. auto event = QueuedMouseEvent {
  218. .type = QueuedMouseEvent::Type::MouseMove,
  219. .position = position,
  220. .button = button,
  221. .buttons = buttons,
  222. .modifiers = modifiers,
  223. };
  224. // OPTIMIZATION: Coalesce with previous unprocessed event iff the previous event is also a MouseMove event.
  225. if (!m_input_event_queue.is_empty()
  226. && m_input_event_queue.tail().has<QueuedMouseEvent>()
  227. && m_input_event_queue.tail().get<QueuedMouseEvent>().type == QueuedMouseEvent::Type::MouseMove) {
  228. event.coalesced_event_count = m_input_event_queue.tail().get<QueuedMouseEvent>().coalesced_event_count + 1;
  229. m_input_event_queue.tail() = event;
  230. return;
  231. }
  232. enqueue_input_event(move(event));
  233. }
  234. void ConnectionFromClient::mouse_up(Gfx::IntPoint position, unsigned int button, unsigned int buttons, unsigned int modifiers)
  235. {
  236. enqueue_input_event(
  237. QueuedMouseEvent {
  238. .type = QueuedMouseEvent::Type::MouseUp,
  239. .position = position,
  240. .button = button,
  241. .buttons = buttons,
  242. .modifiers = modifiers,
  243. });
  244. }
  245. void ConnectionFromClient::mouse_wheel(Gfx::IntPoint position, unsigned int button, unsigned int buttons, unsigned int modifiers, i32 wheel_delta_x, i32 wheel_delta_y)
  246. {
  247. auto event = QueuedMouseEvent {
  248. .type = QueuedMouseEvent::Type::MouseWheel,
  249. .position = position,
  250. .button = button,
  251. .buttons = buttons,
  252. .modifiers = modifiers,
  253. .wheel_delta_x = wheel_delta_x,
  254. .wheel_delta_y = wheel_delta_y,
  255. };
  256. // OPTIMIZATION: Coalesce with previous unprocessed event if the previous event is also a MouseWheel event.
  257. if (!m_input_event_queue.is_empty()
  258. && m_input_event_queue.tail().has<QueuedMouseEvent>()
  259. && m_input_event_queue.tail().get<QueuedMouseEvent>().type == QueuedMouseEvent::Type::MouseWheel) {
  260. auto const& last_event = m_input_event_queue.tail().get<QueuedMouseEvent>();
  261. event.coalesced_event_count = last_event.coalesced_event_count + 1;
  262. event.wheel_delta_x += last_event.wheel_delta_x;
  263. event.wheel_delta_y += last_event.wheel_delta_y;
  264. m_input_event_queue.tail() = event;
  265. return;
  266. }
  267. enqueue_input_event(move(event));
  268. }
  269. void ConnectionFromClient::doubleclick(Gfx::IntPoint position, unsigned int button, unsigned int buttons, unsigned int modifiers)
  270. {
  271. enqueue_input_event(
  272. QueuedMouseEvent {
  273. .type = QueuedMouseEvent::Type::DoubleClick,
  274. .position = position,
  275. .button = button,
  276. .buttons = buttons,
  277. .modifiers = modifiers,
  278. });
  279. }
  280. void ConnectionFromClient::key_down(i32 key, unsigned int modifiers, u32 code_point)
  281. {
  282. enqueue_input_event(
  283. QueuedKeyboardEvent {
  284. .type = QueuedKeyboardEvent::Type::KeyDown,
  285. .key = key,
  286. .modifiers = modifiers,
  287. .code_point = code_point,
  288. });
  289. }
  290. void ConnectionFromClient::key_up(i32 key, unsigned int modifiers, u32 code_point)
  291. {
  292. enqueue_input_event(
  293. QueuedKeyboardEvent {
  294. .type = QueuedKeyboardEvent::Type::KeyUp,
  295. .key = key,
  296. .modifiers = modifiers,
  297. .code_point = code_point,
  298. });
  299. }
  300. void ConnectionFromClient::enqueue_input_event(Variant<QueuedMouseEvent, QueuedKeyboardEvent> event)
  301. {
  302. m_input_event_queue.enqueue(move(event));
  303. m_input_event_queue_timer->start();
  304. }
  305. void ConnectionFromClient::report_finished_handling_input_event(bool event_was_handled)
  306. {
  307. async_did_finish_handling_input_event(event_was_handled);
  308. }
  309. void ConnectionFromClient::debug_request(DeprecatedString const& request, DeprecatedString const& argument)
  310. {
  311. if (request == "dump-dom-tree") {
  312. if (auto* doc = page().top_level_browsing_context().active_document())
  313. Web::dump_tree(*doc);
  314. }
  315. if (request == "dump-layout-tree") {
  316. if (auto* doc = page().top_level_browsing_context().active_document()) {
  317. if (auto* viewport = doc->layout_node())
  318. Web::dump_tree(*viewport);
  319. }
  320. }
  321. if (request == "dump-paint-tree") {
  322. if (auto* doc = page().top_level_browsing_context().active_document()) {
  323. if (auto* paintable = doc->paintable())
  324. Web::dump_tree(*paintable);
  325. }
  326. }
  327. if (request == "dump-stacking-context-tree") {
  328. if (auto* doc = 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. }
  335. if (request == "dump-style-sheets") {
  336. if (auto* doc = page().top_level_browsing_context().active_document()) {
  337. for (auto& sheet : doc->style_sheets().sheets()) {
  338. if (auto result = Web::dump_sheet(sheet); result.is_error())
  339. dbgln("Failed to dump style sheets: {}", result.error());
  340. }
  341. }
  342. }
  343. if (request == "dump-all-resolved-styles") {
  344. if (auto* doc = page().top_level_browsing_context().active_document()) {
  345. Queue<Web::DOM::Node*> elements_to_visit;
  346. elements_to_visit.enqueue(doc->document_element());
  347. while (!elements_to_visit.is_empty()) {
  348. auto element = elements_to_visit.dequeue();
  349. for (auto& child : element->children_as_vector())
  350. elements_to_visit.enqueue(child.ptr());
  351. if (element->is_element()) {
  352. auto styles = doc->style_computer().compute_style(*static_cast<Web::DOM::Element*>(element)).release_value_but_fixme_should_propagate_errors();
  353. dbgln("+ Element {}", element->debug_description());
  354. auto& properties = styles->properties();
  355. for (size_t i = 0; i < properties.size(); ++i)
  356. dbgln("| {} = {}", Web::CSS::string_from_property_id(static_cast<Web::CSS::PropertyID>(i)), properties[i].has_value() ? properties[i]->style->to_string() : ""_string);
  357. dbgln("---");
  358. }
  359. }
  360. }
  361. }
  362. if (request == "collect-garbage") {
  363. Web::Bindings::main_thread_vm().heap().collect_garbage(JS::Heap::CollectionType::CollectGarbage, true);
  364. }
  365. if (request == "dump-gc-graph") {
  366. Web::Bindings::main_thread_vm().heap().dump_graph();
  367. }
  368. if (request == "set-line-box-borders") {
  369. bool state = argument == "on";
  370. m_page_host->set_should_show_line_box_borders(state);
  371. page().top_level_browsing_context().set_needs_display(page().top_level_browsing_context().viewport_rect());
  372. }
  373. if (request == "clear-cache") {
  374. Web::ResourceLoader::the().clear_cache();
  375. }
  376. if (request == "spoof-user-agent") {
  377. Web::ResourceLoader::the().set_user_agent(argument);
  378. }
  379. if (request == "same-origin-policy") {
  380. m_page_host->page().set_same_origin_policy_enabled(argument == "on");
  381. }
  382. if (request == "scripting") {
  383. m_page_host->page().set_is_scripting_enabled(argument == "on");
  384. }
  385. if (request == "block-pop-ups") {
  386. m_page_host->page().set_should_block_pop_ups(argument == "on");
  387. }
  388. if (request == "dump-local-storage") {
  389. if (auto* document = page().top_level_browsing_context().active_document())
  390. document->window().local_storage().release_value_but_fixme_should_propagate_errors()->dump();
  391. }
  392. }
  393. void ConnectionFromClient::get_source()
  394. {
  395. if (auto* doc = page().top_level_browsing_context().active_document()) {
  396. async_did_get_source(doc->url(), doc->source());
  397. }
  398. }
  399. void ConnectionFromClient::inspect_dom_tree()
  400. {
  401. if (auto* doc = page().top_level_browsing_context().active_document()) {
  402. async_did_get_dom_tree(doc->dump_dom_tree_as_json());
  403. }
  404. }
  405. Messages::WebContentServer::InspectDomNodeResponse ConnectionFromClient::inspect_dom_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> const& pseudo_element)
  406. {
  407. auto& top_context = page().top_level_browsing_context();
  408. top_context.for_each_in_inclusive_subtree([&](auto& ctx) {
  409. if (ctx.active_document() != nullptr) {
  410. ctx.active_document()->set_inspected_node(nullptr, {});
  411. }
  412. return IterationDecision::Continue;
  413. });
  414. Web::DOM::Node* node = Web::DOM::Node::from_id(node_id);
  415. // Note: Nodes without layout (aka non-visible nodes, don't have style computed)
  416. if (!node || !node->layout_node()) {
  417. return { false, "", "", "", "", "" };
  418. }
  419. node->document().set_inspected_node(node, pseudo_element);
  420. if (node->is_element()) {
  421. auto& element = verify_cast<Web::DOM::Element>(*node);
  422. if (!element.computed_css_values())
  423. return { false, "", "", "", "", "" };
  424. auto serialize_json = [](Web::CSS::StyleProperties const& properties) -> DeprecatedString {
  425. StringBuilder builder;
  426. auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
  427. properties.for_each_property([&](auto property_id, auto& value) {
  428. MUST(serializer.add(Web::CSS::string_from_property_id(property_id), value.to_string().to_deprecated_string()));
  429. });
  430. MUST(serializer.finish());
  431. return builder.to_deprecated_string();
  432. };
  433. auto serialize_custom_properties_json = [](Web::DOM::Element const& element, Optional<Web::CSS::Selector::PseudoElement> pseudo_element) -> DeprecatedString {
  434. StringBuilder builder;
  435. auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
  436. HashTable<DeprecatedString> seen_properties;
  437. auto const* element_to_check = &element;
  438. while (element_to_check) {
  439. for (auto const& property : element_to_check->custom_properties(pseudo_element)) {
  440. if (!seen_properties.contains(property.key)) {
  441. seen_properties.set(property.key);
  442. MUST(serializer.add(property.key, property.value.value->to_string().to_deprecated_string()));
  443. }
  444. }
  445. element_to_check = element_to_check->parent_element();
  446. }
  447. MUST(serializer.finish());
  448. return builder.to_deprecated_string();
  449. };
  450. auto serialize_node_box_sizing_json = [](Web::Layout::Node const* layout_node) -> DeprecatedString {
  451. if (!layout_node || !layout_node->is_box()) {
  452. return "{}";
  453. }
  454. auto* box = static_cast<Web::Layout::Box const*>(layout_node);
  455. auto box_model = box->box_model();
  456. StringBuilder builder;
  457. auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
  458. MUST(serializer.add("padding_top"sv, box_model.padding.top.to_double()));
  459. MUST(serializer.add("padding_right"sv, box_model.padding.right.to_double()));
  460. MUST(serializer.add("padding_bottom"sv, box_model.padding.bottom.to_double()));
  461. MUST(serializer.add("padding_left"sv, box_model.padding.left.to_double()));
  462. MUST(serializer.add("margin_top"sv, box_model.margin.top.to_double()));
  463. MUST(serializer.add("margin_right"sv, box_model.margin.right.to_double()));
  464. MUST(serializer.add("margin_bottom"sv, box_model.margin.bottom.to_double()));
  465. MUST(serializer.add("margin_left"sv, box_model.margin.left.to_double()));
  466. MUST(serializer.add("border_top"sv, box_model.border.top.to_double()));
  467. MUST(serializer.add("border_right"sv, box_model.border.right.to_double()));
  468. MUST(serializer.add("border_bottom"sv, box_model.border.bottom.to_double()));
  469. MUST(serializer.add("border_left"sv, box_model.border.left.to_double()));
  470. if (auto* paintable_box = box->paintable_box()) {
  471. MUST(serializer.add("content_width"sv, paintable_box->content_width().to_double()));
  472. MUST(serializer.add("content_height"sv, paintable_box->content_height().to_double()));
  473. } else {
  474. MUST(serializer.add("content_width"sv, 0));
  475. MUST(serializer.add("content_height"sv, 0));
  476. }
  477. MUST(serializer.finish());
  478. return builder.to_deprecated_string();
  479. };
  480. auto serialize_aria_properties_state_json = [](Web::DOM::Element const& element) -> DeprecatedString {
  481. auto role_name = element.role_or_default();
  482. if (!role_name.has_value()) {
  483. return "";
  484. }
  485. auto aria_data = MUST(Web::ARIA::AriaData::build_data(element));
  486. auto role = MUST(Web::ARIA::RoleType::build_role_object(role_name.value(), element.is_focusable(), *aria_data));
  487. StringBuilder builder;
  488. auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
  489. MUST(role->serialize_as_json(serializer));
  490. MUST(serializer.finish());
  491. return builder.to_deprecated_string();
  492. };
  493. if (pseudo_element.has_value()) {
  494. auto pseudo_element_node = element.get_pseudo_element_node(pseudo_element.value());
  495. if (!pseudo_element_node)
  496. return { false, "", "", "", "", "" };
  497. // FIXME: Pseudo-elements only exist as Layout::Nodes, which don't have style information
  498. // in a format we can use. So, we run the StyleComputer again to get the specified
  499. // values, and have to ignore the computed values and custom properties.
  500. auto pseudo_element_style = MUST(page().focused_context().active_document()->style_computer().compute_style(element, pseudo_element));
  501. DeprecatedString computed_values = serialize_json(pseudo_element_style);
  502. DeprecatedString resolved_values = "{}";
  503. DeprecatedString custom_properties_json = serialize_custom_properties_json(element, pseudo_element);
  504. DeprecatedString node_box_sizing_json = serialize_node_box_sizing_json(pseudo_element_node.ptr());
  505. return { true, computed_values, resolved_values, custom_properties_json, node_box_sizing_json, "" };
  506. }
  507. DeprecatedString computed_values = serialize_json(*element.computed_css_values());
  508. DeprecatedString resolved_values_json = serialize_json(element.resolved_css_values());
  509. DeprecatedString custom_properties_json = serialize_custom_properties_json(element, {});
  510. DeprecatedString node_box_sizing_json = serialize_node_box_sizing_json(element.layout_node());
  511. DeprecatedString aria_properties_state_json = serialize_aria_properties_state_json(element);
  512. return { true, computed_values, resolved_values_json, custom_properties_json, node_box_sizing_json, aria_properties_state_json };
  513. }
  514. return { false, "", "", "", "", "" };
  515. }
  516. Messages::WebContentServer::GetHoveredNodeIdResponse ConnectionFromClient::get_hovered_node_id()
  517. {
  518. if (auto* document = page().top_level_browsing_context().active_document()) {
  519. auto hovered_node = document->hovered_node();
  520. if (hovered_node)
  521. return hovered_node->id();
  522. }
  523. return (i32)0;
  524. }
  525. void ConnectionFromClient::initialize_js_console(Badge<PageHost>)
  526. {
  527. auto* document = page().top_level_browsing_context().active_document();
  528. auto realm = document->realm().make_weak_ptr();
  529. if (m_realm.ptr() == realm.ptr())
  530. return;
  531. auto console_object = realm->intrinsics().console_object();
  532. m_realm = realm;
  533. m_console_client = make<WebContentConsoleClient>(console_object->console(), *m_realm, *this);
  534. console_object->console().set_client(*m_console_client.ptr());
  535. }
  536. void ConnectionFromClient::js_console_input(DeprecatedString const& js_source)
  537. {
  538. if (m_console_client)
  539. m_console_client->handle_input(js_source);
  540. }
  541. void ConnectionFromClient::run_javascript(DeprecatedString const& js_source)
  542. {
  543. auto* active_document = page().top_level_browsing_context().active_document();
  544. if (!active_document)
  545. return;
  546. // This is partially based on "execute a javascript: URL request" https://html.spec.whatwg.org/multipage/browsing-the-web.html#javascript-protocol
  547. // Let settings be browsingContext's active document's relevant settings object.
  548. auto& settings = active_document->relevant_settings_object();
  549. // Let baseURL be settings's API base URL.
  550. auto base_url = settings.api_base_url();
  551. // Let script be the result of creating a classic script given scriptSource, settings, baseURL, and the default classic script fetch options.
  552. // FIXME: This doesn't pass in "default classic script fetch options"
  553. // FIXME: What should the filename be here?
  554. auto script = Web::HTML::ClassicScript::create("(client connection run_javascript)", js_source, settings, move(base_url));
  555. // Let evaluationStatus be the result of running the classic script script.
  556. auto evaluation_status = script->run();
  557. if (evaluation_status.is_error())
  558. dbgln("Exception :(");
  559. }
  560. void ConnectionFromClient::js_console_request_messages(i32 start_index)
  561. {
  562. if (m_console_client)
  563. m_console_client->send_messages(start_index);
  564. }
  565. Messages::WebContentServer::TakeDocumentScreenshotResponse ConnectionFromClient::take_document_screenshot()
  566. {
  567. auto* document = page().top_level_browsing_context().active_document();
  568. if (!document || !document->document_element())
  569. return { {} };
  570. auto const& content_size = m_page_host->content_size();
  571. Web::DevicePixelRect rect { { 0, 0 }, content_size };
  572. auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, rect.size().to_type<int>()).release_value_but_fixme_should_propagate_errors();
  573. m_page_host->paint(rect, *bitmap);
  574. return { bitmap->to_shareable_bitmap() };
  575. }
  576. Messages::WebContentServer::GetSelectedTextResponse ConnectionFromClient::get_selected_text()
  577. {
  578. return page().focused_context().selected_text();
  579. }
  580. void ConnectionFromClient::select_all()
  581. {
  582. page().focused_context().select_all();
  583. page().client().page_did_change_selection();
  584. }
  585. Messages::WebContentServer::DumpLayoutTreeResponse ConnectionFromClient::dump_layout_tree()
  586. {
  587. auto* document = page().top_level_browsing_context().active_document();
  588. if (!document)
  589. return DeprecatedString { "(no DOM tree)" };
  590. document->update_layout();
  591. auto* layout_root = document->layout_node();
  592. if (!layout_root)
  593. return DeprecatedString { "(no layout tree)" };
  594. StringBuilder builder;
  595. Web::dump_tree(builder, *layout_root);
  596. return builder.to_deprecated_string();
  597. }
  598. Messages::WebContentServer::DumpPaintTreeResponse ConnectionFromClient::dump_paint_tree()
  599. {
  600. auto* document = page().top_level_browsing_context().active_document();
  601. if (!document)
  602. return DeprecatedString { "(no DOM tree)" };
  603. document->update_layout();
  604. auto* layout_root = document->layout_node();
  605. if (!layout_root)
  606. return DeprecatedString { "(no layout tree)" };
  607. if (!layout_root->paintable())
  608. return DeprecatedString { "(no paint tree)" };
  609. StringBuilder builder;
  610. Web::dump_tree(builder, *layout_root->paintable());
  611. return builder.to_deprecated_string();
  612. }
  613. Messages::WebContentServer::DumpTextResponse ConnectionFromClient::dump_text()
  614. {
  615. auto* document = page().top_level_browsing_context().active_document();
  616. if (!document)
  617. return DeprecatedString { "(no DOM tree)" };
  618. if (!document->body())
  619. return DeprecatedString { "(no body)" };
  620. return document->body()->inner_text();
  621. }
  622. void ConnectionFromClient::set_content_filters(Vector<String> const& filters)
  623. {
  624. Web::ContentFilter::the().set_patterns(filters).release_value_but_fixme_should_propagate_errors();
  625. }
  626. void ConnectionFromClient::set_autoplay_allowed_on_all_websites()
  627. {
  628. auto& autoplay_allowlist = Web::PermissionsPolicy::AutoplayAllowlist::the();
  629. autoplay_allowlist.enable_globally();
  630. }
  631. void ConnectionFromClient::set_autoplay_allowlist(Vector<String> const& allowlist)
  632. {
  633. auto& autoplay_allowlist = Web::PermissionsPolicy::AutoplayAllowlist::the();
  634. autoplay_allowlist.enable_for_origins(allowlist).release_value_but_fixme_should_propagate_errors();
  635. }
  636. void ConnectionFromClient::set_proxy_mappings(Vector<DeprecatedString> const& proxies, HashMap<DeprecatedString, size_t> const& mappings)
  637. {
  638. auto keys = mappings.keys();
  639. quick_sort(keys, [&](auto& a, auto& b) { return a.length() < b.length(); });
  640. OrderedHashMap<DeprecatedString, size_t> sorted_mappings;
  641. for (auto& key : keys) {
  642. auto value = *mappings.get(key);
  643. if (value >= proxies.size())
  644. continue;
  645. sorted_mappings.set(key, value);
  646. }
  647. Web::ProxyMappings::the().set_mappings(proxies, move(sorted_mappings));
  648. }
  649. void ConnectionFromClient::set_preferred_color_scheme(Web::CSS::PreferredColorScheme const& color_scheme)
  650. {
  651. m_page_host->set_preferred_color_scheme(color_scheme);
  652. }
  653. void ConnectionFromClient::set_has_focus(bool has_focus)
  654. {
  655. m_page_host->set_has_focus(has_focus);
  656. }
  657. void ConnectionFromClient::set_is_scripting_enabled(bool is_scripting_enabled)
  658. {
  659. m_page_host->set_is_scripting_enabled(is_scripting_enabled);
  660. }
  661. void ConnectionFromClient::set_device_pixels_per_css_pixel(float device_pixels_per_css_pixel)
  662. {
  663. m_page_host->set_device_pixels_per_css_pixel(device_pixels_per_css_pixel);
  664. }
  665. void ConnectionFromClient::set_window_position(Gfx::IntPoint position)
  666. {
  667. m_page_host->set_window_position(position.to_type<Web::DevicePixels>());
  668. }
  669. void ConnectionFromClient::set_window_size(Gfx::IntSize size)
  670. {
  671. m_page_host->set_window_size(size.to_type<Web::DevicePixels>());
  672. }
  673. Messages::WebContentServer::GetLocalStorageEntriesResponse ConnectionFromClient::get_local_storage_entries()
  674. {
  675. auto* document = page().top_level_browsing_context().active_document();
  676. auto local_storage = document->window().local_storage().release_value_but_fixme_should_propagate_errors();
  677. return local_storage->map();
  678. }
  679. Messages::WebContentServer::GetSessionStorageEntriesResponse ConnectionFromClient::get_session_storage_entries()
  680. {
  681. auto* document = page().top_level_browsing_context().active_document();
  682. auto session_storage = document->window().session_storage().release_value_but_fixme_should_propagate_errors();
  683. return session_storage->map();
  684. }
  685. void ConnectionFromClient::handle_file_return(i32 error, Optional<IPC::File> const& file, i32 request_id)
  686. {
  687. auto file_request = m_requested_files.take(request_id);
  688. VERIFY(file_request.has_value());
  689. VERIFY(file_request.value().on_file_request_finish);
  690. file_request.value().on_file_request_finish(error != 0 ? Error::from_errno(error) : ErrorOr<i32> { file->take_fd() });
  691. }
  692. void ConnectionFromClient::request_file(Web::FileRequest file_request)
  693. {
  694. i32 const id = last_id++;
  695. auto path = file_request.path();
  696. m_requested_files.set(id, move(file_request));
  697. async_did_request_file(path, id);
  698. }
  699. void ConnectionFromClient::set_system_visibility_state(bool visible)
  700. {
  701. m_page_host->page().top_level_browsing_context().set_system_visibility_state(
  702. visible
  703. ? Web::HTML::VisibilityState::Visible
  704. : Web::HTML::VisibilityState::Hidden);
  705. }
  706. void ConnectionFromClient::alert_closed()
  707. {
  708. m_page_host->alert_closed();
  709. }
  710. void ConnectionFromClient::confirm_closed(bool accepted)
  711. {
  712. m_page_host->confirm_closed(accepted);
  713. }
  714. void ConnectionFromClient::prompt_closed(Optional<String> const& response)
  715. {
  716. m_page_host->prompt_closed(response);
  717. }
  718. void ConnectionFromClient::toggle_media_play_state()
  719. {
  720. m_page_host->toggle_media_play_state().release_value_but_fixme_should_propagate_errors();
  721. }
  722. void ConnectionFromClient::toggle_media_mute_state()
  723. {
  724. m_page_host->toggle_media_mute_state();
  725. }
  726. void ConnectionFromClient::toggle_media_loop_state()
  727. {
  728. m_page_host->toggle_media_loop_state().release_value_but_fixme_should_propagate_errors();
  729. }
  730. void ConnectionFromClient::toggle_media_controls_state()
  731. {
  732. m_page_host->toggle_media_controls_state().release_value_but_fixme_should_propagate_errors();
  733. }
  734. void ConnectionFromClient::set_user_style(String const& source)
  735. {
  736. m_page_host->set_user_style(source);
  737. }
  738. void ConnectionFromClient::inspect_accessibility_tree()
  739. {
  740. if (auto* doc = page().top_level_browsing_context().active_document()) {
  741. async_did_get_accessibility_tree(doc->dump_accessibility_tree_as_json());
  742. }
  743. }
  744. }