OutOfProcessWebView.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "OutOfProcessWebView.h"
  7. #include "WebContentClient.h"
  8. #include <AK/String.h>
  9. #include <LibFileSystemAccessClient/Client.h>
  10. #include <LibGUI/Application.h>
  11. #include <LibGUI/Desktop.h>
  12. #include <LibGUI/InputBox.h>
  13. #include <LibGUI/MessageBox.h>
  14. #include <LibGUI/Painter.h>
  15. #include <LibGUI/Scrollbar.h>
  16. #include <LibGUI/Window.h>
  17. #include <LibGfx/Font/FontDatabase.h>
  18. #include <LibGfx/Palette.h>
  19. #include <LibGfx/SystemTheme.h>
  20. REGISTER_WIDGET(WebView, OutOfProcessWebView)
  21. namespace WebView {
  22. OutOfProcessWebView::OutOfProcessWebView()
  23. {
  24. set_should_hide_unnecessary_scrollbars(true);
  25. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  26. create_client();
  27. }
  28. OutOfProcessWebView::~OutOfProcessWebView() = default;
  29. void OutOfProcessWebView::handle_web_content_process_crash()
  30. {
  31. create_client();
  32. VERIFY(m_client_state.client);
  33. // Don't keep a stale backup bitmap around.
  34. m_backup_bitmap = nullptr;
  35. handle_resize();
  36. StringBuilder builder;
  37. builder.append("<html><head><title>Crashed: "sv);
  38. builder.append(escape_html_entities(m_url.to_string()));
  39. builder.append("</title></head><body>"sv);
  40. builder.append("<h1>Web page crashed"sv);
  41. if (!m_url.host().is_empty()) {
  42. builder.appendff(" on {}", escape_html_entities(m_url.host()));
  43. }
  44. builder.append("</h1>"sv);
  45. auto escaped_url = escape_html_entities(m_url.to_string());
  46. builder.appendff("The web page <a href=\"{}\">{}</a> has crashed.<br><br>You can reload the page to try again.", escaped_url, escaped_url);
  47. builder.append("</body></html>"sv);
  48. load_html(builder.to_string(), m_url);
  49. }
  50. void OutOfProcessWebView::create_client()
  51. {
  52. m_client_state = {};
  53. m_client_state.client = WebContentClient::try_create(*this).release_value_but_fixme_should_propagate_errors();
  54. m_client_state.client->on_web_content_process_crash = [this] {
  55. deferred_invoke([this] {
  56. handle_web_content_process_crash();
  57. });
  58. };
  59. client().async_update_system_theme(Gfx::current_system_theme_buffer());
  60. client().async_update_system_fonts(Gfx::FontDatabase::default_font_query(), Gfx::FontDatabase::fixed_width_font_query(), Gfx::FontDatabase::window_title_font_query());
  61. client().async_update_screen_rects(GUI::Desktop::the().rects(), GUI::Desktop::the().main_screen_index());
  62. }
  63. void OutOfProcessWebView::load(const AK::URL& url)
  64. {
  65. m_url = url;
  66. client().async_load_url(url);
  67. }
  68. void OutOfProcessWebView::load_html(StringView html, const AK::URL& url)
  69. {
  70. m_url = url;
  71. client().async_load_html(html, url);
  72. }
  73. void OutOfProcessWebView::load_empty_document()
  74. {
  75. m_url = {};
  76. client().async_load_html("", {});
  77. }
  78. void OutOfProcessWebView::paint_event(GUI::PaintEvent& event)
  79. {
  80. GUI::AbstractScrollableWidget::paint_event(event);
  81. // If the available size is empty, we don't have a front or back bitmap to draw.
  82. if (available_size().is_empty())
  83. return;
  84. GUI::Painter painter(*this);
  85. painter.add_clip_rect(event.rect());
  86. if (auto* bitmap = m_client_state.has_usable_bitmap ? m_client_state.front_bitmap.bitmap.ptr() : m_backup_bitmap.ptr()) {
  87. painter.add_clip_rect(frame_inner_rect());
  88. painter.translate(frame_thickness(), frame_thickness());
  89. painter.blit({ 0, 0 }, *bitmap, bitmap->rect());
  90. return;
  91. }
  92. painter.fill_rect(frame_inner_rect(), palette().base());
  93. }
  94. void OutOfProcessWebView::resize_event(GUI::ResizeEvent& event)
  95. {
  96. GUI::AbstractScrollableWidget::resize_event(event);
  97. handle_resize();
  98. }
  99. void OutOfProcessWebView::handle_resize()
  100. {
  101. client().async_set_viewport_rect(Gfx::IntRect({ horizontal_scrollbar().value(), vertical_scrollbar().value() }, available_size()));
  102. if (m_client_state.has_usable_bitmap) {
  103. // NOTE: We keep the outgoing front bitmap as a backup so we have something to paint until we get a new one.
  104. m_backup_bitmap = m_client_state.front_bitmap.bitmap;
  105. }
  106. if (m_client_state.front_bitmap.bitmap)
  107. client().async_remove_backing_store(m_client_state.front_bitmap.id);
  108. if (m_client_state.back_bitmap.bitmap)
  109. client().async_remove_backing_store(m_client_state.back_bitmap.id);
  110. m_client_state.front_bitmap = {};
  111. m_client_state.back_bitmap = {};
  112. m_client_state.has_usable_bitmap = false;
  113. if (available_size().is_empty())
  114. return;
  115. if (auto new_bitmap_or_error = Gfx::Bitmap::try_create_shareable(Gfx::BitmapFormat::BGRx8888, available_size()); !new_bitmap_or_error.is_error()) {
  116. m_client_state.front_bitmap.bitmap = new_bitmap_or_error.release_value();
  117. m_client_state.front_bitmap.id = m_client_state.next_bitmap_id++;
  118. client().async_add_backing_store(m_client_state.front_bitmap.id, m_client_state.front_bitmap.bitmap->to_shareable_bitmap());
  119. }
  120. if (auto new_bitmap_or_error = Gfx::Bitmap::try_create_shareable(Gfx::BitmapFormat::BGRx8888, available_size()); !new_bitmap_or_error.is_error()) {
  121. m_client_state.back_bitmap.bitmap = new_bitmap_or_error.release_value();
  122. m_client_state.back_bitmap.id = m_client_state.next_bitmap_id++;
  123. client().async_add_backing_store(m_client_state.back_bitmap.id, m_client_state.back_bitmap.bitmap->to_shareable_bitmap());
  124. }
  125. request_repaint();
  126. }
  127. void OutOfProcessWebView::keydown_event(GUI::KeyEvent& event)
  128. {
  129. client().async_key_down(event.key(), event.modifiers(), event.code_point());
  130. }
  131. void OutOfProcessWebView::keyup_event(GUI::KeyEvent& event)
  132. {
  133. client().async_key_up(event.key(), event.modifiers(), event.code_point());
  134. }
  135. void OutOfProcessWebView::mousedown_event(GUI::MouseEvent& event)
  136. {
  137. client().async_mouse_down(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers());
  138. }
  139. void OutOfProcessWebView::mouseup_event(GUI::MouseEvent& event)
  140. {
  141. client().async_mouse_up(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers());
  142. }
  143. void OutOfProcessWebView::mousemove_event(GUI::MouseEvent& event)
  144. {
  145. client().async_mouse_move(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers());
  146. }
  147. void OutOfProcessWebView::mousewheel_event(GUI::MouseEvent& event)
  148. {
  149. client().async_mouse_wheel(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers(), event.wheel_delta_x(), event.wheel_delta_y());
  150. }
  151. void OutOfProcessWebView::doubleclick_event(GUI::MouseEvent& event)
  152. {
  153. client().async_doubleclick(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers());
  154. }
  155. void OutOfProcessWebView::theme_change_event(GUI::ThemeChangeEvent& event)
  156. {
  157. GUI::AbstractScrollableWidget::theme_change_event(event);
  158. client().async_update_system_theme(Gfx::current_system_theme_buffer());
  159. request_repaint();
  160. }
  161. void OutOfProcessWebView::screen_rects_change_event(GUI::ScreenRectsChangeEvent& event)
  162. {
  163. client().async_update_screen_rects(event.rects(), event.main_screen_index());
  164. }
  165. void OutOfProcessWebView::notify_server_did_paint(Badge<WebContentClient>, i32 bitmap_id)
  166. {
  167. if (m_client_state.back_bitmap.id == bitmap_id) {
  168. m_client_state.has_usable_bitmap = true;
  169. m_client_state.back_bitmap.pending_paints--;
  170. swap(m_client_state.back_bitmap, m_client_state.front_bitmap);
  171. // We don't need the backup bitmap anymore, so drop it.
  172. m_backup_bitmap = nullptr;
  173. update();
  174. if (m_client_state.got_repaint_requests_while_painting) {
  175. m_client_state.got_repaint_requests_while_painting = false;
  176. request_repaint();
  177. }
  178. }
  179. }
  180. void OutOfProcessWebView::notify_server_did_invalidate_content_rect(Badge<WebContentClient>, [[maybe_unused]] Gfx::IntRect const& content_rect)
  181. {
  182. request_repaint();
  183. }
  184. void OutOfProcessWebView::notify_server_did_change_selection(Badge<WebContentClient>)
  185. {
  186. request_repaint();
  187. }
  188. void OutOfProcessWebView::notify_server_did_request_cursor_change(Badge<WebContentClient>, Gfx::StandardCursor cursor)
  189. {
  190. set_override_cursor(cursor);
  191. }
  192. void OutOfProcessWebView::notify_server_did_layout(Badge<WebContentClient>, Gfx::IntSize const& content_size)
  193. {
  194. set_content_size(content_size);
  195. }
  196. void OutOfProcessWebView::notify_server_did_change_title(Badge<WebContentClient>, String const& title)
  197. {
  198. if (on_title_change)
  199. on_title_change(title);
  200. }
  201. void OutOfProcessWebView::notify_server_did_request_scroll(Badge<WebContentClient>, i32 x_delta, i32 y_delta)
  202. {
  203. horizontal_scrollbar().increase_slider_by(x_delta);
  204. vertical_scrollbar().increase_slider_by(y_delta);
  205. }
  206. void OutOfProcessWebView::notify_server_did_request_scroll_to(Badge<WebContentClient>, Gfx::IntPoint const& scroll_position)
  207. {
  208. horizontal_scrollbar().set_value(scroll_position.x());
  209. vertical_scrollbar().set_value(scroll_position.y());
  210. }
  211. void OutOfProcessWebView::notify_server_did_request_scroll_into_view(Badge<WebContentClient>, Gfx::IntRect const& rect)
  212. {
  213. scroll_into_view(rect, true, true);
  214. }
  215. void OutOfProcessWebView::notify_server_did_enter_tooltip_area(Badge<WebContentClient>, Gfx::IntPoint const&, String const& title)
  216. {
  217. GUI::Application::the()->show_tooltip(title, nullptr);
  218. }
  219. void OutOfProcessWebView::notify_server_did_leave_tooltip_area(Badge<WebContentClient>)
  220. {
  221. GUI::Application::the()->hide_tooltip();
  222. }
  223. void OutOfProcessWebView::notify_server_did_hover_link(Badge<WebContentClient>, const AK::URL& url)
  224. {
  225. if (on_link_hover)
  226. on_link_hover(url);
  227. }
  228. void OutOfProcessWebView::notify_server_did_unhover_link(Badge<WebContentClient>)
  229. {
  230. set_override_cursor(Gfx::StandardCursor::None);
  231. if (on_link_hover)
  232. on_link_hover({});
  233. }
  234. void OutOfProcessWebView::notify_server_did_click_link(Badge<WebContentClient>, const AK::URL& url, String const& target, unsigned int modifiers)
  235. {
  236. if (on_link_click)
  237. on_link_click(url, target, modifiers);
  238. }
  239. void OutOfProcessWebView::notify_server_did_middle_click_link(Badge<WebContentClient>, const AK::URL& url, String const& target, unsigned int modifiers)
  240. {
  241. if (on_link_middle_click)
  242. on_link_middle_click(url, target, modifiers);
  243. }
  244. void OutOfProcessWebView::notify_server_did_start_loading(Badge<WebContentClient>, const AK::URL& url)
  245. {
  246. if (on_load_start)
  247. on_load_start(url);
  248. }
  249. void OutOfProcessWebView::notify_server_did_finish_loading(Badge<WebContentClient>, const AK::URL& url)
  250. {
  251. if (on_load_finish)
  252. on_load_finish(url);
  253. }
  254. void OutOfProcessWebView::notify_server_did_request_context_menu(Badge<WebContentClient>, Gfx::IntPoint const& content_position)
  255. {
  256. if (on_context_menu_request)
  257. on_context_menu_request(screen_relative_rect().location().translated(to_widget_position(content_position)));
  258. }
  259. void OutOfProcessWebView::notify_server_did_request_link_context_menu(Badge<WebContentClient>, Gfx::IntPoint const& content_position, const AK::URL& url, String const&, unsigned)
  260. {
  261. if (on_link_context_menu_request)
  262. on_link_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)));
  263. }
  264. void OutOfProcessWebView::notify_server_did_request_image_context_menu(Badge<WebContentClient>, Gfx::IntPoint const& content_position, const AK::URL& url, String const&, unsigned, Gfx::ShareableBitmap const& bitmap)
  265. {
  266. if (on_image_context_menu_request)
  267. on_image_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)), bitmap);
  268. }
  269. void OutOfProcessWebView::notify_server_did_request_alert(Badge<WebContentClient>, String const& message)
  270. {
  271. GUI::MessageBox::show(window(), message, "Alert"sv, GUI::MessageBox::Type::Information);
  272. }
  273. bool OutOfProcessWebView::notify_server_did_request_confirm(Badge<WebContentClient>, String const& message)
  274. {
  275. auto confirm_result = GUI::MessageBox::show(window(), message, "Confirm"sv, GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OKCancel);
  276. return confirm_result == GUI::Dialog::ExecResult::OK;
  277. }
  278. String OutOfProcessWebView::notify_server_did_request_prompt(Badge<WebContentClient>, String const& message, String const& default_)
  279. {
  280. String response { default_ };
  281. if (GUI::InputBox::show(window(), response, message, "Prompt"sv) == GUI::InputBox::ExecResult::OK)
  282. return response;
  283. return {};
  284. }
  285. void OutOfProcessWebView::notify_server_did_get_source(const AK::URL& url, String const& source)
  286. {
  287. if (on_get_source)
  288. on_get_source(url, source);
  289. }
  290. void OutOfProcessWebView::notify_server_did_get_dom_tree(String const& dom_tree)
  291. {
  292. if (on_get_dom_tree)
  293. on_get_dom_tree(dom_tree);
  294. }
  295. void OutOfProcessWebView::notify_server_did_get_dom_node_properties(i32 node_id, String const& specified_style, String const& computed_style, String const& custom_properties, String const& node_box_sizing)
  296. {
  297. if (on_get_dom_node_properties)
  298. on_get_dom_node_properties(node_id, specified_style, computed_style, custom_properties, node_box_sizing);
  299. }
  300. void OutOfProcessWebView::notify_server_did_output_js_console_message(i32 message_index)
  301. {
  302. if (on_js_console_new_message)
  303. on_js_console_new_message(message_index);
  304. }
  305. void OutOfProcessWebView::notify_server_did_get_js_console_messages(i32 start_index, Vector<String> const& message_types, Vector<String> const& messages)
  306. {
  307. if (on_get_js_console_messages)
  308. on_get_js_console_messages(start_index, message_types, messages);
  309. }
  310. void OutOfProcessWebView::notify_server_did_change_favicon(Gfx::Bitmap const& favicon)
  311. {
  312. if (on_favicon_change)
  313. on_favicon_change(favicon);
  314. }
  315. String OutOfProcessWebView::notify_server_did_request_cookie(Badge<WebContentClient>, const AK::URL& url, Web::Cookie::Source source)
  316. {
  317. if (on_get_cookie)
  318. return on_get_cookie(url, source);
  319. return {};
  320. }
  321. void OutOfProcessWebView::notify_server_did_set_cookie(Badge<WebContentClient>, const AK::URL& url, Web::Cookie::ParsedCookie const& cookie, Web::Cookie::Source source)
  322. {
  323. if (on_set_cookie)
  324. on_set_cookie(url, cookie, source);
  325. }
  326. void OutOfProcessWebView::notify_server_did_update_resource_count(i32 count_waiting)
  327. {
  328. if (on_resource_status_change)
  329. on_resource_status_change(count_waiting);
  330. }
  331. void OutOfProcessWebView::notify_server_did_request_file(Badge<WebContentClient>, String const& path, i32 request_id)
  332. {
  333. auto file = FileSystemAccessClient::Client::the().try_request_file_read_only_approved(window(), path);
  334. if (file.is_error())
  335. client().async_handle_file_return(file.error().code(), {}, request_id);
  336. else
  337. client().async_handle_file_return(0, IPC::File(file.value()->leak_fd()), request_id);
  338. }
  339. void OutOfProcessWebView::did_scroll()
  340. {
  341. client().async_set_viewport_rect(visible_content_rect());
  342. request_repaint();
  343. }
  344. void OutOfProcessWebView::request_repaint()
  345. {
  346. // If this widget was instantiated but not yet added to a window,
  347. // it won't have a back bitmap yet, so we can just skip repaint requests.
  348. if (!m_client_state.back_bitmap.bitmap)
  349. return;
  350. // Don't request a repaint until pending paint requests have finished.
  351. if (m_client_state.back_bitmap.pending_paints) {
  352. m_client_state.got_repaint_requests_while_painting = true;
  353. return;
  354. }
  355. m_client_state.back_bitmap.pending_paints++;
  356. client().async_paint(m_client_state.back_bitmap.bitmap->rect().translated(horizontal_scrollbar().value(), vertical_scrollbar().value()), m_client_state.back_bitmap.id);
  357. }
  358. WebContentClient& OutOfProcessWebView::client()
  359. {
  360. VERIFY(m_client_state.client);
  361. return *m_client_state.client;
  362. }
  363. void OutOfProcessWebView::debug_request(String const& request, String const& argument)
  364. {
  365. client().async_debug_request(request, argument);
  366. }
  367. void OutOfProcessWebView::get_source()
  368. {
  369. client().async_get_source();
  370. }
  371. void OutOfProcessWebView::inspect_dom_tree()
  372. {
  373. client().async_inspect_dom_tree();
  374. }
  375. Optional<OutOfProcessWebView::DOMNodeProperties> OutOfProcessWebView::inspect_dom_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> pseudo_element)
  376. {
  377. auto response = client().inspect_dom_node(node_id, pseudo_element);
  378. if (!response.has_style())
  379. return {};
  380. return DOMNodeProperties {
  381. .specified_values_json = response.specified_style(),
  382. .computed_values_json = response.computed_style(),
  383. .custom_properties_json = response.custom_properties(),
  384. .node_box_sizing_json = response.node_box_sizing()
  385. };
  386. }
  387. void OutOfProcessWebView::clear_inspected_dom_node()
  388. {
  389. client().inspect_dom_node(0, {});
  390. }
  391. i32 OutOfProcessWebView::get_hovered_node_id()
  392. {
  393. return client().get_hovered_node_id();
  394. }
  395. void OutOfProcessWebView::js_console_input(String const& js_source)
  396. {
  397. client().async_js_console_input(js_source);
  398. }
  399. void OutOfProcessWebView::js_console_request_messages(i32 start_index)
  400. {
  401. client().async_js_console_request_messages(start_index);
  402. }
  403. void OutOfProcessWebView::run_javascript(StringView js_source)
  404. {
  405. client().async_run_javascript(js_source);
  406. }
  407. String OutOfProcessWebView::selected_text()
  408. {
  409. return client().get_selected_text();
  410. }
  411. void OutOfProcessWebView::select_all()
  412. {
  413. client().async_select_all();
  414. }
  415. String OutOfProcessWebView::dump_layout_tree()
  416. {
  417. return client().dump_layout_tree();
  418. }
  419. OrderedHashMap<String, String> OutOfProcessWebView::get_local_storage_entries()
  420. {
  421. return client().get_local_storage_entries();
  422. }
  423. OrderedHashMap<String, String> OutOfProcessWebView::get_session_storage_entries()
  424. {
  425. return client().get_session_storage_entries();
  426. }
  427. void OutOfProcessWebView::set_content_filters(Vector<String> filters)
  428. {
  429. client().async_set_content_filters(filters);
  430. }
  431. void OutOfProcessWebView::set_proxy_mappings(Vector<String> proxies, HashMap<String, size_t> mappings)
  432. {
  433. client().async_set_proxy_mappings(move(proxies), move(mappings));
  434. }
  435. void OutOfProcessWebView::set_preferred_color_scheme(Web::CSS::PreferredColorScheme color_scheme)
  436. {
  437. client().async_set_preferred_color_scheme(color_scheme);
  438. }
  439. void OutOfProcessWebView::focusin_event(GUI::FocusEvent&)
  440. {
  441. client().async_set_has_focus(true);
  442. }
  443. void OutOfProcessWebView::focusout_event(GUI::FocusEvent&)
  444. {
  445. client().async_set_has_focus(false);
  446. }
  447. }