ViewImplementation.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright (c) 2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Error.h>
  7. #include <AK/LexicalPath.h>
  8. #include <AK/String.h>
  9. #include <LibCore/DateTime.h>
  10. #include <LibCore/StandardPaths.h>
  11. #include <LibGfx/ImageFormats/PNGWriter.h>
  12. #include <LibWebView/ViewImplementation.h>
  13. namespace WebView {
  14. ViewImplementation::ViewImplementation()
  15. {
  16. m_backing_store_shrink_timer = Core::Timer::create_single_shot(3000, [this] {
  17. resize_backing_stores_if_needed(WindowResizeInProgress::No);
  18. }).release_value_but_fixme_should_propagate_errors();
  19. }
  20. WebContentClient& ViewImplementation::client()
  21. {
  22. VERIFY(m_client_state.client);
  23. return *m_client_state.client;
  24. }
  25. WebContentClient const& ViewImplementation::client() const
  26. {
  27. VERIFY(m_client_state.client);
  28. return *m_client_state.client;
  29. }
  30. void ViewImplementation::load(AK::URL const& url)
  31. {
  32. m_url = url;
  33. client().async_load_url(url);
  34. }
  35. void ViewImplementation::load_html(StringView html, AK::URL const& url)
  36. {
  37. m_url = url;
  38. client().async_load_html(html, url);
  39. }
  40. void ViewImplementation::load_empty_document()
  41. {
  42. load_html(""sv, {});
  43. }
  44. void ViewImplementation::zoom_in()
  45. {
  46. if (m_zoom_level >= ZOOM_MAX_LEVEL)
  47. return;
  48. m_zoom_level += ZOOM_STEP;
  49. update_zoom();
  50. }
  51. void ViewImplementation::zoom_out()
  52. {
  53. if (m_zoom_level <= ZOOM_MIN_LEVEL)
  54. return;
  55. m_zoom_level -= ZOOM_STEP;
  56. update_zoom();
  57. }
  58. void ViewImplementation::reset_zoom()
  59. {
  60. m_zoom_level = 1.0f;
  61. update_zoom();
  62. }
  63. void ViewImplementation::set_preferred_color_scheme(Web::CSS::PreferredColorScheme color_scheme)
  64. {
  65. client().async_set_preferred_color_scheme(color_scheme);
  66. }
  67. DeprecatedString ViewImplementation::selected_text()
  68. {
  69. return client().get_selected_text();
  70. }
  71. void ViewImplementation::select_all()
  72. {
  73. client().async_select_all();
  74. }
  75. void ViewImplementation::get_source()
  76. {
  77. client().async_get_source();
  78. }
  79. void ViewImplementation::inspect_dom_tree()
  80. {
  81. client().async_inspect_dom_tree();
  82. }
  83. void ViewImplementation::inspect_accessibility_tree()
  84. {
  85. client().async_inspect_accessibility_tree();
  86. }
  87. ErrorOr<ViewImplementation::DOMNodeProperties> ViewImplementation::inspect_dom_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> pseudo_element)
  88. {
  89. auto response = client().inspect_dom_node(node_id, pseudo_element);
  90. if (!response.has_style())
  91. return Error::from_string_view("Inspected node returned no style"sv);
  92. return DOMNodeProperties {
  93. .computed_style_json = TRY(String::from_deprecated_string(response.take_computed_style())),
  94. .resolved_style_json = TRY(String::from_deprecated_string(response.take_resolved_style())),
  95. .custom_properties_json = TRY(String::from_deprecated_string(response.take_custom_properties())),
  96. .node_box_sizing_json = TRY(String::from_deprecated_string(response.take_node_box_sizing())),
  97. };
  98. }
  99. void ViewImplementation::clear_inspected_dom_node()
  100. {
  101. client().inspect_dom_node(0, {});
  102. }
  103. i32 ViewImplementation::get_hovered_node_id()
  104. {
  105. return client().get_hovered_node_id();
  106. }
  107. void ViewImplementation::debug_request(DeprecatedString const& request, DeprecatedString const& argument)
  108. {
  109. client().async_debug_request(request, argument);
  110. }
  111. void ViewImplementation::run_javascript(StringView js_source)
  112. {
  113. client().async_run_javascript(js_source);
  114. }
  115. void ViewImplementation::js_console_input(DeprecatedString const& js_source)
  116. {
  117. client().async_js_console_input(js_source);
  118. }
  119. void ViewImplementation::js_console_request_messages(i32 start_index)
  120. {
  121. client().async_js_console_request_messages(start_index);
  122. }
  123. void ViewImplementation::toggle_video_play_state()
  124. {
  125. client().async_toggle_video_play_state();
  126. }
  127. void ViewImplementation::toggle_video_loop_state()
  128. {
  129. client().async_toggle_video_loop_state();
  130. }
  131. void ViewImplementation::toggle_video_controls_state()
  132. {
  133. client().async_toggle_video_controls_state();
  134. }
  135. void ViewImplementation::handle_resize()
  136. {
  137. resize_backing_stores_if_needed(WindowResizeInProgress::Yes);
  138. m_backing_store_shrink_timer->restart();
  139. }
  140. #if !defined(AK_OS_SERENITY)
  141. ErrorOr<NonnullRefPtr<WebView::WebContentClient>> ViewImplementation::launch_web_content_process(ReadonlySpan<String> candidate_web_content_paths, EnableCallgrindProfiling enable_callgrind_profiling, IsLayoutTestMode is_layout_test_mode)
  142. {
  143. int socket_fds[2] {};
  144. TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, socket_fds));
  145. int ui_fd = socket_fds[0];
  146. int wc_fd = socket_fds[1];
  147. int fd_passing_socket_fds[2] {};
  148. TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, fd_passing_socket_fds));
  149. int ui_fd_passing_fd = fd_passing_socket_fds[0];
  150. int wc_fd_passing_fd = fd_passing_socket_fds[1];
  151. if (auto child_pid = TRY(Core::System::fork()); child_pid == 0) {
  152. TRY(Core::System::close(ui_fd_passing_fd));
  153. TRY(Core::System::close(ui_fd));
  154. auto takeover_string = TRY(String::formatted("WebContent:{}", wc_fd));
  155. TRY(Core::System::setenv("SOCKET_TAKEOVER"sv, takeover_string, true));
  156. auto webcontent_fd_passing_socket_string = TRY(String::number(wc_fd_passing_fd));
  157. ErrorOr<void> result;
  158. for (auto const& path : candidate_web_content_paths) {
  159. constexpr auto callgrind_prefix_length = 3;
  160. auto arguments = Vector {
  161. "valgrind"sv,
  162. "--tool=callgrind"sv,
  163. "--instr-atstart=no"sv,
  164. path.bytes_as_string_view(),
  165. "--webcontent-fd-passing-socket"sv,
  166. webcontent_fd_passing_socket_string
  167. };
  168. if (enable_callgrind_profiling == EnableCallgrindProfiling::No)
  169. arguments.remove(0, callgrind_prefix_length);
  170. if (is_layout_test_mode == IsLayoutTestMode::Yes)
  171. arguments.append("--layout-test-mode"sv);
  172. result = Core::System::exec(arguments[0], arguments.span(), Core::System::SearchInPath::Yes);
  173. if (!result.is_error())
  174. break;
  175. }
  176. if (result.is_error())
  177. warnln("Could not launch any of {}: {}", candidate_web_content_paths, result.error());
  178. VERIFY_NOT_REACHED();
  179. }
  180. TRY(Core::System::close(wc_fd_passing_fd));
  181. TRY(Core::System::close(wc_fd));
  182. auto socket = TRY(Core::LocalSocket::adopt_fd(ui_fd));
  183. TRY(socket->set_blocking(true));
  184. auto new_client = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) WebView::WebContentClient(move(socket), *this)));
  185. new_client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(ui_fd_passing_fd)));
  186. if (enable_callgrind_profiling == EnableCallgrindProfiling::Yes) {
  187. dbgln();
  188. dbgln("\033[1;45mLaunched WebContent process under callgrind!\033[0m");
  189. dbgln("\033[100mRun `\033[4mcallgrind_control -i on\033[24m` to start instrumentation and `\033[4mcallgrind_control -i off\033[24m` stop it again.\033[0m");
  190. dbgln();
  191. }
  192. return new_client;
  193. }
  194. #endif
  195. void ViewImplementation::resize_backing_stores_if_needed(WindowResizeInProgress window_resize_in_progress)
  196. {
  197. if (m_client_state.has_usable_bitmap) {
  198. // NOTE: We keep the outgoing front bitmap as a backup so we have something to paint until we get a new one.
  199. m_backup_bitmap = m_client_state.front_bitmap.bitmap;
  200. m_backup_bitmap_size = m_client_state.front_bitmap.last_painted_size;
  201. }
  202. m_client_state.has_usable_bitmap = false;
  203. auto viewport_rect = this->viewport_rect();
  204. if (viewport_rect.is_empty())
  205. return;
  206. Gfx::IntSize minimum_needed_size;
  207. if (window_resize_in_progress == WindowResizeInProgress::Yes) {
  208. // Pad the minimum needed size so that we don't have to keep reallocating backing stores while the window is being resized.
  209. minimum_needed_size = { viewport_rect.width() + 256, viewport_rect.height() + 256 };
  210. } else {
  211. // If we're not in the middle of a resize, we can shrink the backing store size to match the viewport size.
  212. minimum_needed_size = viewport_rect.size();
  213. m_client_state.front_bitmap = {};
  214. m_client_state.back_bitmap = {};
  215. }
  216. auto reallocate_backing_store_if_needed = [&](SharedBitmap& backing_store) {
  217. if (!backing_store.bitmap || !backing_store.bitmap->size().contains(minimum_needed_size)) {
  218. if (auto new_bitmap_or_error = Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::BGRx8888, minimum_needed_size); !new_bitmap_or_error.is_error()) {
  219. if (backing_store.bitmap)
  220. client().async_remove_backing_store(backing_store.id);
  221. backing_store.pending_paints = 0;
  222. backing_store.bitmap = new_bitmap_or_error.release_value();
  223. backing_store.id = m_client_state.next_bitmap_id++;
  224. client().async_add_backing_store(backing_store.id, backing_store.bitmap->to_shareable_bitmap());
  225. }
  226. backing_store.last_painted_size = viewport_rect.size();
  227. }
  228. };
  229. reallocate_backing_store_if_needed(m_client_state.front_bitmap);
  230. reallocate_backing_store_if_needed(m_client_state.back_bitmap);
  231. request_repaint();
  232. }
  233. void ViewImplementation::request_repaint()
  234. {
  235. // If this widget was instantiated but not yet added to a window,
  236. // it won't have a back bitmap yet, so we can just skip repaint requests.
  237. if (!m_client_state.back_bitmap.bitmap)
  238. return;
  239. // Don't request a repaint until pending paint requests have finished.
  240. if (m_client_state.back_bitmap.pending_paints) {
  241. m_client_state.got_repaint_requests_while_painting = true;
  242. return;
  243. }
  244. m_client_state.back_bitmap.pending_paints++;
  245. client().async_paint(viewport_rect(), m_client_state.back_bitmap.id);
  246. }
  247. void ViewImplementation::handle_web_content_process_crash()
  248. {
  249. dbgln("WebContent process crashed!");
  250. create_client();
  251. VERIFY(m_client_state.client);
  252. // Don't keep a stale backup bitmap around.
  253. m_backup_bitmap = nullptr;
  254. handle_resize();
  255. StringBuilder builder;
  256. builder.append("<html><head><title>Crashed: "sv);
  257. builder.append(escape_html_entities(m_url.to_deprecated_string()));
  258. builder.append("</title></head><body>"sv);
  259. builder.append("<h1>Web page crashed"sv);
  260. if (!m_url.host().is_empty()) {
  261. builder.appendff(" on {}", escape_html_entities(m_url.host()));
  262. }
  263. builder.append("</h1>"sv);
  264. auto escaped_url = escape_html_entities(m_url.to_deprecated_string());
  265. builder.appendff("The web page <a href=\"{}\">{}</a> has crashed.<br><br>You can reload the page to try again.", escaped_url, escaped_url);
  266. builder.append("</body></html>"sv);
  267. load_html(builder.to_deprecated_string(), m_url);
  268. }
  269. ErrorOr<void> ViewImplementation::take_screenshot(ScreenshotType type)
  270. {
  271. Gfx::ShareableBitmap bitmap;
  272. switch (type) {
  273. case ScreenshotType::Visible:
  274. if (auto* visible_bitmap = m_client_state.has_usable_bitmap ? m_client_state.front_bitmap.bitmap.ptr() : m_backup_bitmap.ptr())
  275. bitmap = visible_bitmap->to_shareable_bitmap();
  276. break;
  277. case ScreenshotType::Full:
  278. bitmap = client().take_document_screenshot();
  279. break;
  280. }
  281. if (!bitmap.is_valid())
  282. return Error::from_string_view("Failed to take a screenshot of the current tab"sv);
  283. LexicalPath path { Core::StandardPaths::downloads_directory() };
  284. path = path.append(TRY(Core::DateTime::now().to_string("screenshot-%Y-%m-%d-%H-%M-%S.png"sv)));
  285. auto encoded = TRY(Gfx::PNGWriter::encode(*bitmap.bitmap()));
  286. auto screenshot_file = TRY(Core::File::open(path.string(), Core::File::OpenMode::Write));
  287. TRY(screenshot_file->write_until_depleted(encoded));
  288. return {};
  289. }
  290. }