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 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::try_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::try_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>, i32 x_delta, i32 y_delta)
  195. {
  196. horizontal_scrollbar().set_value(horizontal_scrollbar().value() + x_delta);
  197. vertical_scrollbar().set_value(vertical_scrollbar().value() + y_delta);
  198. }
  199. void OutOfProcessWebView::notify_server_did_request_scroll_to(Badge<WebContentClient>, Gfx::IntPoint const& scroll_position)
  200. {
  201. horizontal_scrollbar().set_value(scroll_position.x());
  202. vertical_scrollbar().set_value(scroll_position.y());
  203. }
  204. void OutOfProcessWebView::notify_server_did_request_scroll_into_view(Badge<WebContentClient>, const Gfx::IntRect& rect)
  205. {
  206. scroll_into_view(rect, true, true);
  207. }
  208. void OutOfProcessWebView::notify_server_did_enter_tooltip_area(Badge<WebContentClient>, const Gfx::IntPoint&, const String& title)
  209. {
  210. GUI::Application::the()->show_tooltip(title, nullptr);
  211. }
  212. void OutOfProcessWebView::notify_server_did_leave_tooltip_area(Badge<WebContentClient>)
  213. {
  214. GUI::Application::the()->hide_tooltip();
  215. }
  216. void OutOfProcessWebView::notify_server_did_hover_link(Badge<WebContentClient>, const URL& url)
  217. {
  218. if (on_link_hover)
  219. on_link_hover(url);
  220. }
  221. void OutOfProcessWebView::notify_server_did_unhover_link(Badge<WebContentClient>)
  222. {
  223. set_override_cursor(Gfx::StandardCursor::None);
  224. if (on_link_hover)
  225. on_link_hover({});
  226. }
  227. void OutOfProcessWebView::notify_server_did_click_link(Badge<WebContentClient>, const URL& url, const String& target, unsigned int modifiers)
  228. {
  229. if (on_link_click)
  230. on_link_click(url, target, modifiers);
  231. }
  232. void OutOfProcessWebView::notify_server_did_middle_click_link(Badge<WebContentClient>, const URL& url, const String& target, unsigned int modifiers)
  233. {
  234. if (on_link_middle_click)
  235. on_link_middle_click(url, target, modifiers);
  236. }
  237. void OutOfProcessWebView::notify_server_did_start_loading(Badge<WebContentClient>, const URL& url)
  238. {
  239. if (on_load_start)
  240. on_load_start(url);
  241. }
  242. void OutOfProcessWebView::notify_server_did_finish_loading(Badge<WebContentClient>, const URL& url)
  243. {
  244. if (on_load_finish)
  245. on_load_finish(url);
  246. }
  247. void OutOfProcessWebView::notify_server_did_request_context_menu(Badge<WebContentClient>, const Gfx::IntPoint& content_position)
  248. {
  249. if (on_context_menu_request)
  250. on_context_menu_request(screen_relative_rect().location().translated(to_widget_position(content_position)));
  251. }
  252. void OutOfProcessWebView::notify_server_did_request_link_context_menu(Badge<WebContentClient>, const Gfx::IntPoint& content_position, const URL& url, const String&, unsigned)
  253. {
  254. if (on_link_context_menu_request)
  255. on_link_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)));
  256. }
  257. 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)
  258. {
  259. if (on_image_context_menu_request)
  260. on_image_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)), bitmap);
  261. }
  262. void OutOfProcessWebView::notify_server_did_request_alert(Badge<WebContentClient>, const String& message)
  263. {
  264. GUI::MessageBox::show(window(), message, "Alert", GUI::MessageBox::Type::Information);
  265. }
  266. bool OutOfProcessWebView::notify_server_did_request_confirm(Badge<WebContentClient>, const String& message)
  267. {
  268. auto confirm_result = GUI::MessageBox::show(window(), message, "Confirm", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OKCancel);
  269. return confirm_result == GUI::Dialog::ExecResult::ExecOK;
  270. }
  271. String OutOfProcessWebView::notify_server_did_request_prompt(Badge<WebContentClient>, const String& message, const String& default_)
  272. {
  273. String response { default_ };
  274. if (GUI::InputBox::show(window(), response, message, "Prompt") == GUI::InputBox::ExecOK)
  275. return response;
  276. return {};
  277. }
  278. void OutOfProcessWebView::notify_server_did_get_source(const URL& url, const String& source)
  279. {
  280. if (on_get_source)
  281. on_get_source(url, source);
  282. }
  283. void OutOfProcessWebView::notify_server_did_get_dom_tree(const String& dom_tree)
  284. {
  285. if (on_get_dom_tree)
  286. on_get_dom_tree(dom_tree);
  287. }
  288. void OutOfProcessWebView::notify_server_did_get_dom_node_properties(i32 node_id, String const& specified_style, String const& computed_style)
  289. {
  290. if (on_get_dom_node_properties)
  291. on_get_dom_node_properties(node_id, specified_style, computed_style);
  292. }
  293. void OutOfProcessWebView::notify_server_did_output_js_console_message(i32 message_index)
  294. {
  295. if (on_js_console_new_message)
  296. on_js_console_new_message(message_index);
  297. }
  298. void OutOfProcessWebView::notify_server_did_get_js_console_messages(i32 start_index, const Vector<String>& message_types, const Vector<String>& messages)
  299. {
  300. if (on_get_js_console_messages)
  301. on_get_js_console_messages(start_index, message_types, messages);
  302. }
  303. void OutOfProcessWebView::notify_server_did_change_favicon(const Gfx::Bitmap& favicon)
  304. {
  305. if (on_favicon_change)
  306. on_favicon_change(favicon);
  307. }
  308. String OutOfProcessWebView::notify_server_did_request_cookie(Badge<WebContentClient>, const URL& url, Cookie::Source source)
  309. {
  310. if (on_get_cookie)
  311. return on_get_cookie(url, source);
  312. return {};
  313. }
  314. void OutOfProcessWebView::notify_server_did_set_cookie(Badge<WebContentClient>, const URL& url, const Cookie::ParsedCookie& cookie, Cookie::Source source)
  315. {
  316. if (on_set_cookie)
  317. on_set_cookie(url, cookie, source);
  318. }
  319. void OutOfProcessWebView::did_scroll()
  320. {
  321. client().async_set_viewport_rect(visible_content_rect());
  322. request_repaint();
  323. }
  324. void OutOfProcessWebView::request_repaint()
  325. {
  326. // If this widget was instantiated but not yet added to a window,
  327. // it won't have a back bitmap yet, so we can just skip repaint requests.
  328. if (!m_client_state.back_bitmap)
  329. return;
  330. client().async_paint(m_client_state.back_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. }