OutOfProcessWebView.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 <LibGUI/MessageBox.h>
  29. #include <LibGUI/Painter.h>
  30. #include <LibGUI/ScrollBar.h>
  31. #include <LibGUI/Window.h>
  32. #include <LibGfx/Palette.h>
  33. #include <LibGfx/SystemTheme.h>
  34. REGISTER_WIDGET(Web, OutOfProcessWebView)
  35. namespace Web {
  36. OutOfProcessWebView::OutOfProcessWebView()
  37. {
  38. set_should_hide_unnecessary_scrollbars(true);
  39. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  40. create_client();
  41. }
  42. OutOfProcessWebView::~OutOfProcessWebView()
  43. {
  44. }
  45. void OutOfProcessWebView::create_client()
  46. {
  47. m_client_state = {};
  48. m_client_state.client = WebContentClient::construct(*this);
  49. m_client_state.client->on_web_content_process_crash = [this] {
  50. create_client();
  51. ASSERT(m_client_state.client);
  52. handle_resize();
  53. StringBuilder builder;
  54. builder.append("<html><head><title>Crashed: ");
  55. builder.append(m_url.to_string());
  56. builder.append("</title></head><body>");
  57. builder.append("<h1>Web page crashed");
  58. if (!m_url.host().is_empty()) {
  59. builder.appendff(" on {}", m_url.host());
  60. }
  61. builder.append("</h1>");
  62. builder.appendff("The web page <a href='{}'>{}</a> has crashed.<br><br>You can reload the page to try again.", m_url, m_url);
  63. builder.append("</body></html>");
  64. load_html(builder.to_string(), m_url);
  65. };
  66. client().post_message(Messages::WebContentServer::UpdateSystemTheme(Gfx::current_system_theme_buffer()));
  67. }
  68. void OutOfProcessWebView::load(const URL& url)
  69. {
  70. m_url = url;
  71. client().post_message(Messages::WebContentServer::LoadURL(url));
  72. }
  73. void OutOfProcessWebView::load_html(const StringView& html, const URL& url)
  74. {
  75. m_url = url;
  76. client().post_message(Messages::WebContentServer::LoadHTML(html, url));
  77. }
  78. void OutOfProcessWebView::load_empty_document()
  79. {
  80. m_url = {};
  81. client().post_message(Messages::WebContentServer::LoadHTML("", {}));
  82. }
  83. void OutOfProcessWebView::paint_event(GUI::PaintEvent& event)
  84. {
  85. GUI::ScrollableWidget::paint_event(event);
  86. // If the available size is empty, we don't have a front or back bitmap to draw.
  87. if (available_size().is_empty())
  88. return;
  89. GUI::Painter painter(*this);
  90. painter.add_clip_rect(event.rect());
  91. if (!m_client_state.has_usable_bitmap) {
  92. painter.fill_rect(frame_inner_rect(), palette().base());
  93. return;
  94. }
  95. painter.add_clip_rect(frame_inner_rect());
  96. painter.translate(frame_thickness(), frame_thickness());
  97. ASSERT(m_client_state.front_bitmap);
  98. painter.blit({ 0, 0 }, *m_client_state.front_bitmap, m_client_state.front_bitmap->rect());
  99. }
  100. void OutOfProcessWebView::resize_event(GUI::ResizeEvent& event)
  101. {
  102. GUI::ScrollableWidget::resize_event(event);
  103. handle_resize();
  104. }
  105. void OutOfProcessWebView::handle_resize()
  106. {
  107. client().post_message(Messages::WebContentServer::SetViewportRect(Gfx::IntRect({ horizontal_scrollbar().value(), vertical_scrollbar().value() }, available_size())));
  108. if (m_client_state.front_bitmap) {
  109. m_client_state.front_bitmap = nullptr;
  110. client().post_message(Messages::WebContentServer::RemoveBackingStore(m_client_state.front_bitmap_id));
  111. }
  112. if (m_client_state.back_bitmap) {
  113. m_client_state.back_bitmap = nullptr;
  114. client().post_message(Messages::WebContentServer::RemoveBackingStore(m_client_state.back_bitmap_id));
  115. }
  116. m_client_state.front_bitmap_id = -1;
  117. m_client_state.back_bitmap_id = -1;
  118. m_client_state.has_usable_bitmap = false;
  119. if (available_size().is_empty())
  120. return;
  121. if (auto new_bitmap = Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::RGB32, available_size())) {
  122. m_client_state.front_bitmap = move(new_bitmap);
  123. m_client_state.front_bitmap_id = m_client_state.next_bitmap_id++;
  124. client().post_message(Messages::WebContentServer::AddBackingStore(m_client_state.front_bitmap_id, m_client_state.front_bitmap->to_shareable_bitmap()));
  125. }
  126. if (auto new_bitmap = Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::RGB32, available_size())) {
  127. m_client_state.back_bitmap = move(new_bitmap);
  128. m_client_state.back_bitmap_id = m_client_state.next_bitmap_id++;
  129. client().post_message(Messages::WebContentServer::AddBackingStore(m_client_state.back_bitmap_id, m_client_state.back_bitmap->to_shareable_bitmap()));
  130. }
  131. request_repaint();
  132. }
  133. void OutOfProcessWebView::keydown_event(GUI::KeyEvent& event)
  134. {
  135. client().post_message(Messages::WebContentServer::KeyDown(event.key(), event.modifiers(), event.code_point()));
  136. }
  137. void OutOfProcessWebView::mousedown_event(GUI::MouseEvent& event)
  138. {
  139. client().post_message(Messages::WebContentServer::MouseDown(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers()));
  140. }
  141. void OutOfProcessWebView::mouseup_event(GUI::MouseEvent& event)
  142. {
  143. client().post_message(Messages::WebContentServer::MouseUp(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers()));
  144. }
  145. void OutOfProcessWebView::mousemove_event(GUI::MouseEvent& event)
  146. {
  147. client().post_message(Messages::WebContentServer::MouseMove(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers()));
  148. }
  149. void OutOfProcessWebView::theme_change_event(GUI::ThemeChangeEvent& event)
  150. {
  151. GUI::ScrollableWidget::theme_change_event(event);
  152. client().post_message(Messages::WebContentServer::UpdateSystemTheme(Gfx::current_system_theme_buffer()));
  153. request_repaint();
  154. }
  155. void OutOfProcessWebView::notify_server_did_paint(Badge<WebContentClient>, i32 bitmap_id)
  156. {
  157. if (m_client_state.back_bitmap_id == bitmap_id) {
  158. m_client_state.has_usable_bitmap = true;
  159. swap(m_client_state.back_bitmap, m_client_state.front_bitmap);
  160. swap(m_client_state.back_bitmap_id, m_client_state.front_bitmap_id);
  161. update();
  162. }
  163. }
  164. void OutOfProcessWebView::notify_server_did_invalidate_content_rect(Badge<WebContentClient>, [[maybe_unused]] const Gfx::IntRect& content_rect)
  165. {
  166. request_repaint();
  167. }
  168. void OutOfProcessWebView::notify_server_did_change_selection(Badge<WebContentClient>)
  169. {
  170. request_repaint();
  171. }
  172. void OutOfProcessWebView::notify_server_did_layout(Badge<WebContentClient>, const Gfx::IntSize& content_size)
  173. {
  174. set_content_size(content_size);
  175. }
  176. void OutOfProcessWebView::notify_server_did_change_title(Badge<WebContentClient>, const String& title)
  177. {
  178. if (on_title_change)
  179. on_title_change(title);
  180. }
  181. void OutOfProcessWebView::notify_server_did_request_scroll_into_view(Badge<WebContentClient>, const Gfx::IntRect& rect)
  182. {
  183. scroll_into_view(rect, true, true);
  184. }
  185. void OutOfProcessWebView::notify_server_did_hover_link(Badge<WebContentClient>, const URL& url)
  186. {
  187. set_override_cursor(Gfx::StandardCursor::Hand);
  188. if (on_link_hover)
  189. on_link_hover(url);
  190. }
  191. void OutOfProcessWebView::notify_server_did_unhover_link(Badge<WebContentClient>)
  192. {
  193. set_override_cursor(Gfx::StandardCursor::None);
  194. if (on_link_hover)
  195. on_link_hover({});
  196. }
  197. void OutOfProcessWebView::notify_server_did_click_link(Badge<WebContentClient>, const URL& url, const String& target, unsigned int modifiers)
  198. {
  199. if (on_link_click)
  200. on_link_click(url, target, modifiers);
  201. }
  202. void OutOfProcessWebView::notify_server_did_middle_click_link(Badge<WebContentClient>, const URL& url, const String& target, unsigned int modifiers)
  203. {
  204. if (on_link_middle_click)
  205. on_link_middle_click(url, target, modifiers);
  206. }
  207. void OutOfProcessWebView::notify_server_did_start_loading(Badge<WebContentClient>, const URL& url)
  208. {
  209. if (on_load_start)
  210. on_load_start(url);
  211. }
  212. void OutOfProcessWebView::notify_server_did_finish_loading(Badge<WebContentClient>, const URL& url)
  213. {
  214. if (on_load_finish)
  215. on_load_finish(url);
  216. }
  217. void OutOfProcessWebView::notify_server_did_request_context_menu(Badge<WebContentClient>, const Gfx::IntPoint& content_position)
  218. {
  219. if (on_context_menu_request)
  220. on_context_menu_request(screen_relative_rect().location().translated(to_widget_position(content_position)));
  221. }
  222. void OutOfProcessWebView::notify_server_did_request_link_context_menu(Badge<WebContentClient>, const Gfx::IntPoint& content_position, const URL& url, const String&, unsigned)
  223. {
  224. if (on_link_context_menu_request)
  225. on_link_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)));
  226. }
  227. void OutOfProcessWebView::notify_server_did_request_alert(Badge<WebContentClient>, const String& message)
  228. {
  229. GUI::MessageBox::show(window(), message, "Alert", GUI::MessageBox::Type::Information);
  230. }
  231. void OutOfProcessWebView::did_scroll()
  232. {
  233. client().post_message(Messages::WebContentServer::SetViewportRect(visible_content_rect()));
  234. request_repaint();
  235. }
  236. void OutOfProcessWebView::request_repaint()
  237. {
  238. // If this widget was instantiated but not yet added to a window,
  239. // it won't have a back bitmap yet, so we can just skip repaint requests.
  240. if (!m_client_state.back_bitmap)
  241. return;
  242. 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));
  243. }
  244. WebContentClient& OutOfProcessWebView::client()
  245. {
  246. ASSERT(m_client_state.client);
  247. return *m_client_state.client;
  248. }
  249. }