OutOfProcessWebView.cpp 17 KB

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