OutOfProcessWebView.cpp 8.7 KB

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