OutOfProcessWebView.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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::construct(*this);
  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(const 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 = Gfx::Bitmap::try_create_shareable(Gfx::BitmapFormat::BGRx8888, available_size())) {
  117. m_client_state.front_bitmap.bitmap = move(new_bitmap);
  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 = Gfx::Bitmap::try_create_shareable(Gfx::BitmapFormat::BGRx8888, available_size())) {
  122. m_client_state.back_bitmap.bitmap = move(new_bitmap);
  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::mousedown_event(GUI::MouseEvent& event)
  133. {
  134. client().async_mouse_down(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers());
  135. }
  136. void OutOfProcessWebView::mouseup_event(GUI::MouseEvent& event)
  137. {
  138. client().async_mouse_up(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers());
  139. }
  140. void OutOfProcessWebView::mousemove_event(GUI::MouseEvent& event)
  141. {
  142. client().async_mouse_move(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers());
  143. }
  144. void OutOfProcessWebView::mousewheel_event(GUI::MouseEvent& event)
  145. {
  146. client().async_mouse_wheel(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers(), event.wheel_delta());
  147. }
  148. void OutOfProcessWebView::theme_change_event(GUI::ThemeChangeEvent& event)
  149. {
  150. GUI::AbstractScrollableWidget::theme_change_event(event);
  151. client().async_update_system_theme(Gfx::current_system_theme_buffer());
  152. request_repaint();
  153. }
  154. void OutOfProcessWebView::screen_rects_change_event(GUI::ScreenRectsChangeEvent& event)
  155. {
  156. client().async_update_screen_rects(event.rects(), event.main_screen_index());
  157. }
  158. void OutOfProcessWebView::notify_server_did_paint(Badge<WebContentClient>, i32 bitmap_id)
  159. {
  160. if (m_client_state.back_bitmap.id == bitmap_id) {
  161. m_client_state.has_usable_bitmap = true;
  162. m_client_state.back_bitmap.pending_paints--;
  163. swap(m_client_state.back_bitmap, m_client_state.front_bitmap);
  164. // We don't need the backup bitmap anymore, so drop it.
  165. m_backup_bitmap = nullptr;
  166. update();
  167. }
  168. }
  169. void OutOfProcessWebView::notify_server_did_invalidate_content_rect(Badge<WebContentClient>, [[maybe_unused]] const Gfx::IntRect& content_rect)
  170. {
  171. request_repaint();
  172. }
  173. void OutOfProcessWebView::notify_server_did_change_selection(Badge<WebContentClient>)
  174. {
  175. request_repaint();
  176. }
  177. void OutOfProcessWebView::notify_server_did_request_cursor_change(Badge<WebContentClient>, Gfx::StandardCursor cursor)
  178. {
  179. set_override_cursor(cursor);
  180. }
  181. void OutOfProcessWebView::notify_server_did_layout(Badge<WebContentClient>, const Gfx::IntSize& content_size)
  182. {
  183. set_content_size(content_size);
  184. }
  185. void OutOfProcessWebView::notify_server_did_change_title(Badge<WebContentClient>, const String& title)
  186. {
  187. if (on_title_change)
  188. on_title_change(title);
  189. }
  190. void OutOfProcessWebView::notify_server_did_request_scroll(Badge<WebContentClient>, i32 x_delta, i32 y_delta)
  191. {
  192. horizontal_scrollbar().set_value(horizontal_scrollbar().value() + x_delta);
  193. vertical_scrollbar().set_value(vertical_scrollbar().value() + y_delta);
  194. }
  195. void OutOfProcessWebView::notify_server_did_request_scroll_to(Badge<WebContentClient>, Gfx::IntPoint const& scroll_position)
  196. {
  197. horizontal_scrollbar().set_value(scroll_position.x());
  198. vertical_scrollbar().set_value(scroll_position.y());
  199. }
  200. void OutOfProcessWebView::notify_server_did_request_scroll_into_view(Badge<WebContentClient>, const Gfx::IntRect& rect)
  201. {
  202. scroll_into_view(rect, true, true);
  203. }
  204. void OutOfProcessWebView::notify_server_did_enter_tooltip_area(Badge<WebContentClient>, const Gfx::IntPoint&, const String& title)
  205. {
  206. GUI::Application::the()->show_tooltip(title, nullptr);
  207. }
  208. void OutOfProcessWebView::notify_server_did_leave_tooltip_area(Badge<WebContentClient>)
  209. {
  210. GUI::Application::the()->hide_tooltip();
  211. }
  212. void OutOfProcessWebView::notify_server_did_hover_link(Badge<WebContentClient>, const AK::URL& url)
  213. {
  214. if (on_link_hover)
  215. on_link_hover(url);
  216. }
  217. void OutOfProcessWebView::notify_server_did_unhover_link(Badge<WebContentClient>)
  218. {
  219. set_override_cursor(Gfx::StandardCursor::None);
  220. if (on_link_hover)
  221. on_link_hover({});
  222. }
  223. void OutOfProcessWebView::notify_server_did_click_link(Badge<WebContentClient>, const AK::URL& url, const String& target, unsigned int modifiers)
  224. {
  225. if (on_link_click)
  226. on_link_click(url, target, modifiers);
  227. }
  228. void OutOfProcessWebView::notify_server_did_middle_click_link(Badge<WebContentClient>, const AK::URL& url, const String& target, unsigned int modifiers)
  229. {
  230. if (on_link_middle_click)
  231. on_link_middle_click(url, target, modifiers);
  232. }
  233. void OutOfProcessWebView::notify_server_did_start_loading(Badge<WebContentClient>, const AK::URL& url)
  234. {
  235. if (on_load_start)
  236. on_load_start(url);
  237. }
  238. void OutOfProcessWebView::notify_server_did_finish_loading(Badge<WebContentClient>, const AK::URL& url)
  239. {
  240. if (on_load_finish)
  241. on_load_finish(url);
  242. }
  243. void OutOfProcessWebView::notify_server_did_request_context_menu(Badge<WebContentClient>, const Gfx::IntPoint& content_position)
  244. {
  245. if (on_context_menu_request)
  246. on_context_menu_request(screen_relative_rect().location().translated(to_widget_position(content_position)));
  247. }
  248. void OutOfProcessWebView::notify_server_did_request_link_context_menu(Badge<WebContentClient>, const Gfx::IntPoint& content_position, const AK::URL& url, const String&, unsigned)
  249. {
  250. if (on_link_context_menu_request)
  251. on_link_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)));
  252. }
  253. 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)
  254. {
  255. if (on_image_context_menu_request)
  256. on_image_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)), bitmap);
  257. }
  258. void OutOfProcessWebView::notify_server_did_request_alert(Badge<WebContentClient>, const String& message)
  259. {
  260. GUI::MessageBox::show(window(), message, "Alert", GUI::MessageBox::Type::Information);
  261. }
  262. bool OutOfProcessWebView::notify_server_did_request_confirm(Badge<WebContentClient>, const String& message)
  263. {
  264. auto confirm_result = GUI::MessageBox::show(window(), message, "Confirm", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OKCancel);
  265. return confirm_result == GUI::Dialog::ExecResult::ExecOK;
  266. }
  267. String OutOfProcessWebView::notify_server_did_request_prompt(Badge<WebContentClient>, const String& message, const String& default_)
  268. {
  269. String response { default_ };
  270. if (GUI::InputBox::show(window(), response, message, "Prompt") == GUI::InputBox::ExecOK)
  271. return response;
  272. return {};
  273. }
  274. void OutOfProcessWebView::notify_server_did_get_source(const AK::URL& url, const String& source)
  275. {
  276. if (on_get_source)
  277. on_get_source(url, source);
  278. }
  279. void OutOfProcessWebView::notify_server_did_get_dom_tree(const String& dom_tree)
  280. {
  281. if (on_get_dom_tree)
  282. on_get_dom_tree(dom_tree);
  283. }
  284. void OutOfProcessWebView::notify_server_did_get_dom_node_properties(i32 node_id, String const& specified_style, String const& computed_style)
  285. {
  286. if (on_get_dom_node_properties)
  287. on_get_dom_node_properties(node_id, specified_style, computed_style);
  288. }
  289. void OutOfProcessWebView::notify_server_did_output_js_console_message(i32 message_index)
  290. {
  291. if (on_js_console_new_message)
  292. on_js_console_new_message(message_index);
  293. }
  294. void OutOfProcessWebView::notify_server_did_get_js_console_messages(i32 start_index, const Vector<String>& message_types, const Vector<String>& messages)
  295. {
  296. if (on_get_js_console_messages)
  297. on_get_js_console_messages(start_index, message_types, messages);
  298. }
  299. void OutOfProcessWebView::notify_server_did_change_favicon(const Gfx::Bitmap& favicon)
  300. {
  301. if (on_favicon_change)
  302. on_favicon_change(favicon);
  303. }
  304. String OutOfProcessWebView::notify_server_did_request_cookie(Badge<WebContentClient>, const AK::URL& url, Cookie::Source source)
  305. {
  306. if (on_get_cookie)
  307. return on_get_cookie(url, source);
  308. return {};
  309. }
  310. void OutOfProcessWebView::notify_server_did_set_cookie(Badge<WebContentClient>, const AK::URL& url, const Cookie::ParsedCookie& cookie, Cookie::Source source)
  311. {
  312. if (on_set_cookie)
  313. on_set_cookie(url, cookie, source);
  314. }
  315. void OutOfProcessWebView::did_scroll()
  316. {
  317. client().async_set_viewport_rect(visible_content_rect());
  318. request_repaint();
  319. }
  320. void OutOfProcessWebView::request_repaint()
  321. {
  322. // If this widget was instantiated but not yet added to a window,
  323. // it won't have a back bitmap yet, so we can just skip repaint requests.
  324. if (!m_client_state.back_bitmap.bitmap)
  325. return;
  326. // Don't request a repaint until pending paint requests have finished.
  327. if (m_client_state.back_bitmap.pending_paints)
  328. return;
  329. m_client_state.back_bitmap.pending_paints++;
  330. client().async_paint(m_client_state.back_bitmap.bitmap->rect().translated(horizontal_scrollbar().value(), vertical_scrollbar().value()), m_client_state.back_bitmap.id);
  331. }
  332. WebContentClient& OutOfProcessWebView::client()
  333. {
  334. VERIFY(m_client_state.client);
  335. return *m_client_state.client;
  336. }
  337. void OutOfProcessWebView::debug_request(const String& request, const String& argument)
  338. {
  339. client().async_debug_request(request, argument);
  340. }
  341. void OutOfProcessWebView::get_source()
  342. {
  343. client().async_get_source();
  344. }
  345. void OutOfProcessWebView::inspect_dom_tree()
  346. {
  347. client().async_inspect_dom_tree();
  348. }
  349. Optional<OutOfProcessWebView::DOMNodeProperties> OutOfProcessWebView::inspect_dom_node(i32 node_id)
  350. {
  351. auto response = client().inspect_dom_node(node_id);
  352. if (!response.has_style())
  353. return {};
  354. return DOMNodeProperties {
  355. .specified_values_json = response.specified_style(),
  356. .computed_values_json = response.computed_style()
  357. };
  358. }
  359. void OutOfProcessWebView::clear_inspected_dom_node()
  360. {
  361. client().inspect_dom_node(0);
  362. }
  363. i32 OutOfProcessWebView::get_hovered_node_id()
  364. {
  365. return client().get_hovered_node_id();
  366. }
  367. void OutOfProcessWebView::js_console_input(const String& js_source)
  368. {
  369. client().async_js_console_input(js_source);
  370. }
  371. void OutOfProcessWebView::js_console_request_messages(i32 start_index)
  372. {
  373. client().async_js_console_request_messages(start_index);
  374. }
  375. void OutOfProcessWebView::run_javascript(StringView js_source)
  376. {
  377. client().async_run_javascript(js_source);
  378. }
  379. String OutOfProcessWebView::selected_text()
  380. {
  381. return client().get_selected_text();
  382. }
  383. void OutOfProcessWebView::select_all()
  384. {
  385. client().async_select_all();
  386. }
  387. String OutOfProcessWebView::dump_layout_tree()
  388. {
  389. return client().dump_layout_tree();
  390. }
  391. }