OutOfProcessWebView.cpp 13 KB

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