ViewImplementation.cpp 13 KB

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