OutOfProcessWebView.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "OutOfProcessWebView.h"
  27. #include "WebContentClient.h"
  28. #include <AK/String.h>
  29. #include <AK/URLParser.h>
  30. #include <LibGUI/Application.h>
  31. #include <LibGUI/Desktop.h>
  32. #include <LibGUI/InputBox.h>
  33. #include <LibGUI/MessageBox.h>
  34. #include <LibGUI/Painter.h>
  35. #include <LibGUI/ScrollBar.h>
  36. #include <LibGUI/Window.h>
  37. #include <LibGfx/Palette.h>
  38. #include <LibGfx/SystemTheme.h>
  39. REGISTER_WIDGET(Web, OutOfProcessWebView)
  40. namespace Web {
  41. OutOfProcessWebView::OutOfProcessWebView()
  42. {
  43. set_should_hide_unnecessary_scrollbars(true);
  44. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  45. create_client();
  46. }
  47. OutOfProcessWebView::~OutOfProcessWebView()
  48. {
  49. }
  50. void OutOfProcessWebView::handle_web_content_process_crash()
  51. {
  52. create_client();
  53. VERIFY(m_client_state.client);
  54. // Don't keep a stale backup bitmap around.
  55. m_backup_bitmap = nullptr;
  56. handle_resize();
  57. StringBuilder builder;
  58. builder.append("<html><head><title>Crashed: ");
  59. builder.append(escape_html_entities(m_url.to_string()));
  60. builder.append("</title></head><body>");
  61. builder.append("<h1>Web page crashed");
  62. if (!m_url.host().is_empty()) {
  63. builder.appendff(" on {}", escape_html_entities(m_url.host()));
  64. }
  65. builder.append("</h1>");
  66. 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()));
  67. builder.append("</body></html>");
  68. load_html(builder.to_string(), m_url);
  69. }
  70. void OutOfProcessWebView::create_client()
  71. {
  72. m_client_state = {};
  73. m_client_state.client = WebContentClient::construct(*this);
  74. m_client_state.client->on_web_content_process_crash = [this] {
  75. deferred_invoke([this] {
  76. handle_web_content_process_crash();
  77. });
  78. };
  79. client().post_message(Messages::WebContentServer::UpdateSystemTheme(Gfx::current_system_theme_buffer()));
  80. client().post_message(Messages::WebContentServer::UpdateScreenRect(GUI::Desktop::the().rect()));
  81. }
  82. void OutOfProcessWebView::load(const URL& url)
  83. {
  84. m_url = url;
  85. client().post_message(Messages::WebContentServer::LoadURL(url));
  86. }
  87. void OutOfProcessWebView::load_html(const StringView& html, const URL& url)
  88. {
  89. m_url = url;
  90. client().post_message(Messages::WebContentServer::LoadHTML(html, url));
  91. }
  92. void OutOfProcessWebView::load_empty_document()
  93. {
  94. m_url = {};
  95. client().post_message(Messages::WebContentServer::LoadHTML("", {}));
  96. }
  97. void OutOfProcessWebView::paint_event(GUI::PaintEvent& event)
  98. {
  99. GUI::ScrollableWidget::paint_event(event);
  100. // If the available size is empty, we don't have a front or back bitmap to draw.
  101. if (available_size().is_empty())
  102. return;
  103. GUI::Painter painter(*this);
  104. painter.add_clip_rect(event.rect());
  105. if (auto* bitmap = m_client_state.has_usable_bitmap ? m_client_state.front_bitmap.ptr() : m_backup_bitmap.ptr()) {
  106. painter.add_clip_rect(frame_inner_rect());
  107. painter.translate(frame_thickness(), frame_thickness());
  108. painter.blit({ 0, 0 }, *bitmap, bitmap->rect());
  109. return;
  110. }
  111. painter.fill_rect(frame_inner_rect(), palette().base());
  112. }
  113. void OutOfProcessWebView::resize_event(GUI::ResizeEvent& event)
  114. {
  115. GUI::ScrollableWidget::resize_event(event);
  116. handle_resize();
  117. }
  118. void OutOfProcessWebView::handle_resize()
  119. {
  120. client().post_message(Messages::WebContentServer::SetViewportRect(Gfx::IntRect({ horizontal_scrollbar().value(), vertical_scrollbar().value() }, available_size())));
  121. if (m_client_state.has_usable_bitmap) {
  122. // NOTE: We keep the outgoing front bitmap as a backup so we have something to paint until we get a new one.
  123. m_backup_bitmap = m_client_state.front_bitmap;
  124. }
  125. if (m_client_state.front_bitmap) {
  126. m_client_state.front_bitmap = nullptr;
  127. client().post_message(Messages::WebContentServer::RemoveBackingStore(m_client_state.front_bitmap_id));
  128. }
  129. if (m_client_state.back_bitmap) {
  130. m_client_state.back_bitmap = nullptr;
  131. client().post_message(Messages::WebContentServer::RemoveBackingStore(m_client_state.back_bitmap_id));
  132. }
  133. m_client_state.front_bitmap_id = -1;
  134. m_client_state.back_bitmap_id = -1;
  135. m_client_state.has_usable_bitmap = false;
  136. if (available_size().is_empty())
  137. return;
  138. if (auto new_bitmap = Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::BGRx8888, available_size())) {
  139. m_client_state.front_bitmap = move(new_bitmap);
  140. m_client_state.front_bitmap_id = m_client_state.next_bitmap_id++;
  141. client().post_message(Messages::WebContentServer::AddBackingStore(m_client_state.front_bitmap_id, m_client_state.front_bitmap->to_shareable_bitmap()));
  142. }
  143. if (auto new_bitmap = Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::BGRx8888, available_size())) {
  144. m_client_state.back_bitmap = move(new_bitmap);
  145. m_client_state.back_bitmap_id = m_client_state.next_bitmap_id++;
  146. client().post_message(Messages::WebContentServer::AddBackingStore(m_client_state.back_bitmap_id, m_client_state.back_bitmap->to_shareable_bitmap()));
  147. }
  148. request_repaint();
  149. }
  150. void OutOfProcessWebView::keydown_event(GUI::KeyEvent& event)
  151. {
  152. client().post_message(Messages::WebContentServer::KeyDown(event.key(), event.modifiers(), event.code_point()));
  153. }
  154. void OutOfProcessWebView::mousedown_event(GUI::MouseEvent& event)
  155. {
  156. client().post_message(Messages::WebContentServer::MouseDown(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers()));
  157. }
  158. void OutOfProcessWebView::mouseup_event(GUI::MouseEvent& event)
  159. {
  160. client().post_message(Messages::WebContentServer::MouseUp(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers()));
  161. }
  162. void OutOfProcessWebView::mousemove_event(GUI::MouseEvent& event)
  163. {
  164. client().post_message(Messages::WebContentServer::MouseMove(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers()));
  165. }
  166. void OutOfProcessWebView::mousewheel_event(GUI::MouseEvent& event)
  167. {
  168. client().post_message(Messages::WebContentServer::MouseWheel(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers(), event.wheel_delta()));
  169. }
  170. void OutOfProcessWebView::theme_change_event(GUI::ThemeChangeEvent& event)
  171. {
  172. GUI::ScrollableWidget::theme_change_event(event);
  173. client().post_message(Messages::WebContentServer::UpdateSystemTheme(Gfx::current_system_theme_buffer()));
  174. request_repaint();
  175. }
  176. void OutOfProcessWebView::screen_rect_change_event(GUI::ScreenRectChangeEvent& event)
  177. {
  178. client().post_message(Messages::WebContentServer::UpdateScreenRect(event.rect()));
  179. }
  180. void OutOfProcessWebView::notify_server_did_paint(Badge<WebContentClient>, i32 bitmap_id)
  181. {
  182. if (m_client_state.back_bitmap_id == bitmap_id) {
  183. m_client_state.has_usable_bitmap = true;
  184. swap(m_client_state.back_bitmap, m_client_state.front_bitmap);
  185. swap(m_client_state.back_bitmap_id, m_client_state.front_bitmap_id);
  186. // We don't need the backup bitmap anymore, so drop it.
  187. m_backup_bitmap = nullptr;
  188. update();
  189. }
  190. }
  191. void OutOfProcessWebView::notify_server_did_invalidate_content_rect(Badge<WebContentClient>, [[maybe_unused]] const Gfx::IntRect& content_rect)
  192. {
  193. request_repaint();
  194. }
  195. void OutOfProcessWebView::notify_server_did_change_selection(Badge<WebContentClient>)
  196. {
  197. request_repaint();
  198. }
  199. void OutOfProcessWebView::notify_server_did_request_cursor_change(Badge<WebContentClient>, Gfx::StandardCursor cursor)
  200. {
  201. set_override_cursor(cursor);
  202. }
  203. void OutOfProcessWebView::notify_server_did_layout(Badge<WebContentClient>, const Gfx::IntSize& content_size)
  204. {
  205. set_content_size(content_size);
  206. }
  207. void OutOfProcessWebView::notify_server_did_change_title(Badge<WebContentClient>, const String& title)
  208. {
  209. if (on_title_change)
  210. on_title_change(title);
  211. }
  212. void OutOfProcessWebView::notify_server_did_request_scroll(Badge<WebContentClient>, int wheel_delta)
  213. {
  214. vertical_scrollbar().set_value(vertical_scrollbar().value() + wheel_delta * 20);
  215. }
  216. void OutOfProcessWebView::notify_server_did_request_scroll_into_view(Badge<WebContentClient>, const Gfx::IntRect& rect)
  217. {
  218. scroll_into_view(rect, true, true);
  219. }
  220. void OutOfProcessWebView::notify_server_did_enter_tooltip_area(Badge<WebContentClient>, const Gfx::IntPoint&, const String& title)
  221. {
  222. GUI::Application::the()->show_tooltip(title, nullptr);
  223. }
  224. void OutOfProcessWebView::notify_server_did_leave_tooltip_area(Badge<WebContentClient>)
  225. {
  226. GUI::Application::the()->hide_tooltip();
  227. }
  228. void OutOfProcessWebView::notify_server_did_hover_link(Badge<WebContentClient>, const URL& url)
  229. {
  230. if (on_link_hover)
  231. on_link_hover(url);
  232. }
  233. void OutOfProcessWebView::notify_server_did_unhover_link(Badge<WebContentClient>)
  234. {
  235. set_override_cursor(Gfx::StandardCursor::None);
  236. if (on_link_hover)
  237. on_link_hover({});
  238. }
  239. void OutOfProcessWebView::notify_server_did_click_link(Badge<WebContentClient>, const URL& url, const String& target, unsigned int modifiers)
  240. {
  241. if (on_link_click)
  242. on_link_click(url, target, modifiers);
  243. }
  244. void OutOfProcessWebView::notify_server_did_middle_click_link(Badge<WebContentClient>, const URL& url, const String& target, unsigned int modifiers)
  245. {
  246. if (on_link_middle_click)
  247. on_link_middle_click(url, target, modifiers);
  248. }
  249. void OutOfProcessWebView::notify_server_did_start_loading(Badge<WebContentClient>, const URL& url)
  250. {
  251. if (on_load_start)
  252. on_load_start(url);
  253. }
  254. void OutOfProcessWebView::notify_server_did_finish_loading(Badge<WebContentClient>, const URL& url)
  255. {
  256. if (on_load_finish)
  257. on_load_finish(url);
  258. }
  259. void OutOfProcessWebView::notify_server_did_request_context_menu(Badge<WebContentClient>, const Gfx::IntPoint& content_position)
  260. {
  261. if (on_context_menu_request)
  262. on_context_menu_request(screen_relative_rect().location().translated(to_widget_position(content_position)));
  263. }
  264. void OutOfProcessWebView::notify_server_did_request_link_context_menu(Badge<WebContentClient>, const Gfx::IntPoint& content_position, const URL& url, const String&, unsigned)
  265. {
  266. if (on_link_context_menu_request)
  267. on_link_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)));
  268. }
  269. 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)
  270. {
  271. if (on_image_context_menu_request)
  272. on_image_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)), bitmap);
  273. }
  274. void OutOfProcessWebView::notify_server_did_request_alert(Badge<WebContentClient>, const String& message)
  275. {
  276. GUI::MessageBox::show(window(), message, "Alert", GUI::MessageBox::Type::Information);
  277. }
  278. bool OutOfProcessWebView::notify_server_did_request_confirm(Badge<WebContentClient>, const String& message)
  279. {
  280. auto confirm_result = GUI::MessageBox::show(window(), message, "Confirm", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OKCancel);
  281. return confirm_result == GUI::Dialog::ExecResult::ExecOK;
  282. }
  283. String OutOfProcessWebView::notify_server_did_request_prompt(Badge<WebContentClient>, const String& message, const String& default_)
  284. {
  285. String response { default_ };
  286. if (GUI::InputBox::show(window(), response, message, "Prompt") == GUI::InputBox::ExecOK)
  287. return response;
  288. return {};
  289. }
  290. void OutOfProcessWebView::notify_server_did_get_source(const URL& url, const String& source)
  291. {
  292. if (on_get_source)
  293. on_get_source(url, source);
  294. }
  295. void OutOfProcessWebView::notify_server_did_js_console_output(const String& method, const String& line)
  296. {
  297. if (on_js_console_output)
  298. on_js_console_output(method, line);
  299. }
  300. void OutOfProcessWebView::notify_server_did_change_favicon(const Gfx::Bitmap& favicon)
  301. {
  302. if (on_favicon_change)
  303. on_favicon_change(favicon);
  304. }
  305. String OutOfProcessWebView::notify_server_did_request_cookie(Badge<WebContentClient>, const URL& url)
  306. {
  307. if (on_get_cookie)
  308. return on_get_cookie(url);
  309. return {};
  310. }
  311. void OutOfProcessWebView::notify_server_did_set_cookie(Badge<WebContentClient>, const URL& url, const String& cookie)
  312. {
  313. if (on_set_cookie)
  314. on_set_cookie(url, cookie);
  315. }
  316. void OutOfProcessWebView::did_scroll()
  317. {
  318. client().post_message(Messages::WebContentServer::SetViewportRect(visible_content_rect()));
  319. request_repaint();
  320. }
  321. void OutOfProcessWebView::request_repaint()
  322. {
  323. // If this widget was instantiated but not yet added to a window,
  324. // it won't have a back bitmap yet, so we can just skip repaint requests.
  325. if (!m_client_state.back_bitmap)
  326. return;
  327. client().post_message(Messages::WebContentServer::Paint(m_client_state.back_bitmap->rect().translated(horizontal_scrollbar().value(), vertical_scrollbar().value()), m_client_state.back_bitmap_id));
  328. }
  329. WebContentClient& OutOfProcessWebView::client()
  330. {
  331. VERIFY(m_client_state.client);
  332. return *m_client_state.client;
  333. }
  334. void OutOfProcessWebView::debug_request(const String& request, const String& argument)
  335. {
  336. client().post_message(Messages::WebContentServer::DebugRequest(request, argument));
  337. }
  338. void OutOfProcessWebView::get_source()
  339. {
  340. client().post_message(Messages::WebContentServer::GetSource());
  341. }
  342. void OutOfProcessWebView::js_console_initialize()
  343. {
  344. client().post_message(Messages::WebContentServer::JSConsoleInitialize());
  345. }
  346. void OutOfProcessWebView::js_console_input(const String& js_source)
  347. {
  348. client().post_message(Messages::WebContentServer::JSConsoleInput(js_source));
  349. }
  350. }