OutOfProcessWebView.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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/SystemTheme.h>
  34. namespace Web {
  35. OutOfProcessWebView::OutOfProcessWebView()
  36. {
  37. set_should_hide_unnecessary_scrollbars(true);
  38. m_client = WebContentClient::construct(*this);
  39. client().post_message(Messages::WebContentServer::UpdateSystemTheme(Gfx::current_system_theme_buffer_id()));
  40. }
  41. OutOfProcessWebView::~OutOfProcessWebView()
  42. {
  43. }
  44. void OutOfProcessWebView::load(const URL& url)
  45. {
  46. m_url = url;
  47. client().post_message(Messages::WebContentServer::LoadURL(url));
  48. }
  49. void OutOfProcessWebView::load_html(const StringView& html, const URL& url)
  50. {
  51. m_url = url;
  52. client().post_message(Messages::WebContentServer::LoadHTML(html, url));
  53. }
  54. void OutOfProcessWebView::paint_event(GUI::PaintEvent& event)
  55. {
  56. GUI::ScrollableWidget::paint_event(event);
  57. GUI::Painter painter(*this);
  58. painter.add_clip_rect(frame_inner_rect());
  59. painter.add_clip_rect(event.rect());
  60. painter.translate(frame_thickness(), frame_thickness());
  61. ASSERT(m_front_bitmap);
  62. painter.blit({ 0, 0 }, *m_front_bitmap, m_front_bitmap->rect());
  63. }
  64. void OutOfProcessWebView::resize_event(GUI::ResizeEvent& event)
  65. {
  66. GUI::ScrollableWidget::resize_event(event);
  67. m_front_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, available_size())->to_bitmap_backed_by_shared_buffer();
  68. m_front_bitmap->shared_buffer()->share_with(client().server_pid());
  69. m_back_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, available_size())->to_bitmap_backed_by_shared_buffer();
  70. m_back_bitmap->shared_buffer()->share_with(client().server_pid());
  71. client().post_message(Messages::WebContentServer::SetViewportRect(Gfx::IntRect({ horizontal_scrollbar().value(), vertical_scrollbar().value() }, available_size())));
  72. request_repaint();
  73. }
  74. void OutOfProcessWebView::keydown_event(GUI::KeyEvent& event)
  75. {
  76. client().post_message(Messages::WebContentServer::KeyDown(event.key(), event.modifiers(), event.code_point()));
  77. }
  78. void OutOfProcessWebView::mousedown_event(GUI::MouseEvent& event)
  79. {
  80. client().post_message(Messages::WebContentServer::MouseDown(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers()));
  81. }
  82. void OutOfProcessWebView::mouseup_event(GUI::MouseEvent& event)
  83. {
  84. client().post_message(Messages::WebContentServer::MouseUp(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers()));
  85. }
  86. void OutOfProcessWebView::mousemove_event(GUI::MouseEvent& event)
  87. {
  88. client().post_message(Messages::WebContentServer::MouseMove(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers()));
  89. }
  90. void OutOfProcessWebView::notify_server_did_paint(Badge<WebContentClient>, i32 shbuf_id)
  91. {
  92. if (m_back_bitmap->shbuf_id() == shbuf_id) {
  93. swap(m_back_bitmap, m_front_bitmap);
  94. update();
  95. }
  96. }
  97. void OutOfProcessWebView::notify_server_did_invalidate_content_rect(Badge<WebContentClient>, [[maybe_unused]] const Gfx::IntRect& content_rect)
  98. {
  99. #ifdef DEBUG_SPAM
  100. dbg() << "server did invalidate content_rect: " << content_rect << ", current front_shbuf_id=" << m_front_bitmap->shbuf_id() << ", current back_shbuf_id=" << m_back_bitmap->shbuf_id();
  101. #endif
  102. request_repaint();
  103. }
  104. void OutOfProcessWebView::notify_server_did_change_selection(Badge<WebContentClient>)
  105. {
  106. request_repaint();
  107. }
  108. void OutOfProcessWebView::notify_server_did_layout(Badge<WebContentClient>, const Gfx::IntSize& content_size)
  109. {
  110. set_content_size(content_size);
  111. }
  112. void OutOfProcessWebView::notify_server_did_change_title(Badge<WebContentClient>, const String& title)
  113. {
  114. if (on_title_change)
  115. on_title_change(title);
  116. }
  117. void OutOfProcessWebView::notify_server_did_request_scroll_into_view(Badge<WebContentClient>, const Gfx::IntRect& rect)
  118. {
  119. scroll_into_view(rect, true, true);
  120. }
  121. void OutOfProcessWebView::notify_server_did_hover_link(Badge<WebContentClient>, const URL& url)
  122. {
  123. set_override_cursor(Gfx::StandardCursor::Hand);
  124. if (on_link_hover)
  125. on_link_hover(url);
  126. }
  127. void OutOfProcessWebView::notify_server_did_unhover_link(Badge<WebContentClient>)
  128. {
  129. set_override_cursor(Gfx::StandardCursor::None);
  130. if (on_link_hover)
  131. on_link_hover({});
  132. }
  133. void OutOfProcessWebView::notify_server_did_click_link(Badge<WebContentClient>, const URL& url, const String& target, unsigned int modifiers)
  134. {
  135. if (on_link_click)
  136. on_link_click(url, target, modifiers);
  137. }
  138. void OutOfProcessWebView::notify_server_did_middle_click_link(Badge<WebContentClient>, const URL& url, const String& target, unsigned int modifiers)
  139. {
  140. if (on_link_middle_click)
  141. on_link_middle_click(url, target, modifiers);
  142. }
  143. void OutOfProcessWebView::notify_server_did_start_loading(Badge<WebContentClient>, const URL& url)
  144. {
  145. if (on_load_start)
  146. on_load_start(url);
  147. }
  148. void OutOfProcessWebView::notify_server_did_request_context_menu(Badge<WebContentClient>, const Gfx::IntPoint& content_position)
  149. {
  150. if (on_context_menu_request)
  151. on_context_menu_request(screen_relative_rect().location().translated(to_widget_position(content_position)));
  152. }
  153. void OutOfProcessWebView::notify_server_did_request_link_context_menu(Badge<WebContentClient>, const Gfx::IntPoint& content_position, const URL& url, const String&, unsigned)
  154. {
  155. if (on_link_context_menu_request)
  156. on_link_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)));
  157. }
  158. void OutOfProcessWebView::notify_server_did_request_alert(Badge<WebContentClient>, const String& message)
  159. {
  160. GUI::MessageBox::show(window(), message, "Alert", GUI::MessageBox::Type::Information);
  161. }
  162. void OutOfProcessWebView::did_scroll()
  163. {
  164. client().post_message(Messages::WebContentServer::SetViewportRect(visible_content_rect()));
  165. request_repaint();
  166. }
  167. void OutOfProcessWebView::request_repaint()
  168. {
  169. client().post_message(Messages::WebContentServer::Paint(m_back_bitmap->rect().translated(horizontal_scrollbar().value(), vertical_scrollbar().value()), m_back_bitmap->shbuf_id()));
  170. }
  171. WebContentClient& OutOfProcessWebView::client()
  172. {
  173. return *m_client;
  174. }
  175. }