OutOfProcessWebView.cpp 16 KB

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