OutOfProcessWebView.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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](auto&) {
  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 URL& url)
  65. {
  66. m_url = url;
  67. client().async_load_url(url);
  68. }
  69. void OutOfProcessWebView::load_html(const StringView& html, const 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.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;
  106. }
  107. if (m_client_state.front_bitmap) {
  108. m_client_state.front_bitmap = nullptr;
  109. client().async_remove_backing_store(m_client_state.front_bitmap_id);
  110. }
  111. if (m_client_state.back_bitmap) {
  112. m_client_state.back_bitmap = nullptr;
  113. client().async_remove_backing_store(m_client_state.back_bitmap_id);
  114. }
  115. m_client_state.front_bitmap_id = -1;
  116. m_client_state.back_bitmap_id = -1;
  117. m_client_state.has_usable_bitmap = false;
  118. if (available_size().is_empty())
  119. return;
  120. if (auto new_bitmap = Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::BGRx8888, available_size())) {
  121. m_client_state.front_bitmap = move(new_bitmap);
  122. m_client_state.front_bitmap_id = m_client_state.next_bitmap_id++;
  123. client().async_add_backing_store(m_client_state.front_bitmap_id, m_client_state.front_bitmap->to_shareable_bitmap());
  124. }
  125. if (auto new_bitmap = Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::BGRx8888, available_size())) {
  126. m_client_state.back_bitmap = move(new_bitmap);
  127. m_client_state.back_bitmap_id = m_client_state.next_bitmap_id++;
  128. client().async_add_backing_store(m_client_state.back_bitmap_id, m_client_state.back_bitmap->to_shareable_bitmap());
  129. }
  130. request_repaint();
  131. }
  132. void OutOfProcessWebView::keydown_event(GUI::KeyEvent& event)
  133. {
  134. client().async_key_down(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());
  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. swap(m_client_state.back_bitmap, m_client_state.front_bitmap);
  167. swap(m_client_state.back_bitmap_id, m_client_state.front_bitmap_id);
  168. // We don't need the backup bitmap anymore, so drop it.
  169. m_backup_bitmap = nullptr;
  170. update();
  171. }
  172. }
  173. void OutOfProcessWebView::notify_server_did_invalidate_content_rect(Badge<WebContentClient>, [[maybe_unused]] const Gfx::IntRect& content_rect)
  174. {
  175. request_repaint();
  176. }
  177. void OutOfProcessWebView::notify_server_did_change_selection(Badge<WebContentClient>)
  178. {
  179. request_repaint();
  180. }
  181. void OutOfProcessWebView::notify_server_did_request_cursor_change(Badge<WebContentClient>, Gfx::StandardCursor cursor)
  182. {
  183. set_override_cursor(cursor);
  184. }
  185. void OutOfProcessWebView::notify_server_did_layout(Badge<WebContentClient>, const Gfx::IntSize& content_size)
  186. {
  187. set_content_size(content_size);
  188. }
  189. void OutOfProcessWebView::notify_server_did_change_title(Badge<WebContentClient>, const String& title)
  190. {
  191. if (on_title_change)
  192. on_title_change(title);
  193. }
  194. void OutOfProcessWebView::notify_server_did_request_scroll(Badge<WebContentClient>, int wheel_delta)
  195. {
  196. vertical_scrollbar().set_value(vertical_scrollbar().value() + wheel_delta * 20);
  197. }
  198. void OutOfProcessWebView::notify_server_did_request_scroll_into_view(Badge<WebContentClient>, const Gfx::IntRect& rect)
  199. {
  200. scroll_into_view(rect, true, true);
  201. }
  202. void OutOfProcessWebView::notify_server_did_enter_tooltip_area(Badge<WebContentClient>, const Gfx::IntPoint&, const String& title)
  203. {
  204. GUI::Application::the()->show_tooltip(title, nullptr);
  205. }
  206. void OutOfProcessWebView::notify_server_did_leave_tooltip_area(Badge<WebContentClient>)
  207. {
  208. GUI::Application::the()->hide_tooltip();
  209. }
  210. void OutOfProcessWebView::notify_server_did_hover_link(Badge<WebContentClient>, const URL& url)
  211. {
  212. if (on_link_hover)
  213. on_link_hover(url);
  214. }
  215. void OutOfProcessWebView::notify_server_did_unhover_link(Badge<WebContentClient>)
  216. {
  217. set_override_cursor(Gfx::StandardCursor::None);
  218. if (on_link_hover)
  219. on_link_hover({});
  220. }
  221. void OutOfProcessWebView::notify_server_did_click_link(Badge<WebContentClient>, const URL& url, const String& target, unsigned int modifiers)
  222. {
  223. if (on_link_click)
  224. on_link_click(url, target, modifiers);
  225. }
  226. void OutOfProcessWebView::notify_server_did_middle_click_link(Badge<WebContentClient>, const URL& url, const String& target, unsigned int modifiers)
  227. {
  228. if (on_link_middle_click)
  229. on_link_middle_click(url, target, modifiers);
  230. }
  231. void OutOfProcessWebView::notify_server_did_start_loading(Badge<WebContentClient>, const URL& url)
  232. {
  233. if (on_load_start)
  234. on_load_start(url);
  235. }
  236. void OutOfProcessWebView::notify_server_did_finish_loading(Badge<WebContentClient>, const URL& url)
  237. {
  238. if (on_load_finish)
  239. on_load_finish(url);
  240. }
  241. void OutOfProcessWebView::notify_server_did_request_context_menu(Badge<WebContentClient>, const Gfx::IntPoint& content_position)
  242. {
  243. if (on_context_menu_request)
  244. on_context_menu_request(screen_relative_rect().location().translated(to_widget_position(content_position)));
  245. }
  246. void OutOfProcessWebView::notify_server_did_request_link_context_menu(Badge<WebContentClient>, const Gfx::IntPoint& content_position, const URL& url, const String&, unsigned)
  247. {
  248. if (on_link_context_menu_request)
  249. on_link_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)));
  250. }
  251. void OutOfProcessWebView::notify_server_did_request_image_context_menu(Badge<WebContentClient>, const Gfx::IntPoint& content_position, const URL& url, const String&, unsigned, const Gfx::ShareableBitmap& bitmap)
  252. {
  253. if (on_image_context_menu_request)
  254. on_image_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)), bitmap);
  255. }
  256. void OutOfProcessWebView::notify_server_did_request_alert(Badge<WebContentClient>, const String& message)
  257. {
  258. GUI::MessageBox::show(window(), message, "Alert", GUI::MessageBox::Type::Information);
  259. }
  260. bool OutOfProcessWebView::notify_server_did_request_confirm(Badge<WebContentClient>, const String& message)
  261. {
  262. auto confirm_result = GUI::MessageBox::show(window(), message, "Confirm", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OKCancel);
  263. return confirm_result == GUI::Dialog::ExecResult::ExecOK;
  264. }
  265. String OutOfProcessWebView::notify_server_did_request_prompt(Badge<WebContentClient>, const String& message, const String& default_)
  266. {
  267. String response { default_ };
  268. if (GUI::InputBox::show(window(), response, message, "Prompt") == GUI::InputBox::ExecOK)
  269. return response;
  270. return {};
  271. }
  272. void OutOfProcessWebView::notify_server_did_get_source(const URL& url, const String& source)
  273. {
  274. if (on_get_source)
  275. on_get_source(url, source);
  276. }
  277. void OutOfProcessWebView::notify_server_did_get_dom_tree(const String& dom_tree)
  278. {
  279. if (on_get_dom_tree)
  280. on_get_dom_tree(dom_tree);
  281. }
  282. void OutOfProcessWebView::notify_server_did_js_console_output(const String& method, const String& line)
  283. {
  284. if (on_js_console_output)
  285. on_js_console_output(method, line);
  286. }
  287. void OutOfProcessWebView::notify_server_did_change_favicon(const Gfx::Bitmap& favicon)
  288. {
  289. if (on_favicon_change)
  290. on_favicon_change(favicon);
  291. }
  292. String OutOfProcessWebView::notify_server_did_request_cookie(Badge<WebContentClient>, const URL& url, Cookie::Source source)
  293. {
  294. if (on_get_cookie)
  295. return on_get_cookie(url, source);
  296. return {};
  297. }
  298. void OutOfProcessWebView::notify_server_did_set_cookie(Badge<WebContentClient>, const URL& url, const Cookie::ParsedCookie& cookie, Cookie::Source source)
  299. {
  300. if (on_set_cookie)
  301. on_set_cookie(url, cookie, source);
  302. }
  303. void OutOfProcessWebView::did_scroll()
  304. {
  305. client().async_set_viewport_rect(visible_content_rect());
  306. request_repaint();
  307. }
  308. void OutOfProcessWebView::request_repaint()
  309. {
  310. // If this widget was instantiated but not yet added to a window,
  311. // it won't have a back bitmap yet, so we can just skip repaint requests.
  312. if (!m_client_state.back_bitmap)
  313. return;
  314. client().async_paint(m_client_state.back_bitmap->rect().translated(horizontal_scrollbar().value(), vertical_scrollbar().value()), m_client_state.back_bitmap_id);
  315. }
  316. WebContentClient& OutOfProcessWebView::client()
  317. {
  318. VERIFY(m_client_state.client);
  319. return *m_client_state.client;
  320. }
  321. void OutOfProcessWebView::debug_request(const String& request, const String& argument)
  322. {
  323. client().async_debug_request(request, argument);
  324. }
  325. void OutOfProcessWebView::get_source()
  326. {
  327. client().async_get_source();
  328. }
  329. void OutOfProcessWebView::inspect_dom_tree()
  330. {
  331. client().async_inspect_dom_tree();
  332. }
  333. void OutOfProcessWebView::js_console_initialize()
  334. {
  335. client().async_js_console_initialize();
  336. }
  337. void OutOfProcessWebView::js_console_input(const String& js_source)
  338. {
  339. client().async_js_console_input(js_source);
  340. }
  341. String OutOfProcessWebView::selected_text()
  342. {
  343. return client().get_selected_text();
  344. }
  345. void OutOfProcessWebView::select_all()
  346. {
  347. client().async_select_all();
  348. }
  349. }