LadybirdWebViewBridge.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Ladybird/HelperProcess.h>
  7. #include <Ladybird/Types.h>
  8. #include <Ladybird/Utilities.h>
  9. #include <LibGfx/Font/FontDatabase.h>
  10. #include <LibGfx/Rect.h>
  11. #include <LibIPC/File.h>
  12. #include <LibWeb/Crypto/Crypto.h>
  13. #include <UI/LadybirdWebViewBridge.h>
  14. namespace Ladybird {
  15. template<typename T>
  16. static T scale_for_device(T size, float device_pixel_ratio)
  17. {
  18. return size.template to_type<float>().scaled(device_pixel_ratio).template to_type<int>();
  19. }
  20. ErrorOr<NonnullOwnPtr<WebViewBridge>> WebViewBridge::create(Vector<Gfx::IntRect> screen_rects, float device_pixel_ratio, Optional<StringView> webdriver_content_ipc_path)
  21. {
  22. return adopt_nonnull_own_or_enomem(new (nothrow) WebViewBridge(move(screen_rects), device_pixel_ratio, move(webdriver_content_ipc_path)));
  23. }
  24. WebViewBridge::WebViewBridge(Vector<Gfx::IntRect> screen_rects, float device_pixel_ratio, Optional<StringView> webdriver_content_ipc_path)
  25. : m_screen_rects(move(screen_rects))
  26. , m_webdriver_content_ipc_path(move(webdriver_content_ipc_path))
  27. {
  28. m_device_pixel_ratio = device_pixel_ratio;
  29. m_inverse_device_pixel_ratio = 1.0 / device_pixel_ratio;
  30. create_client(WebView::EnableCallgrindProfiling::No);
  31. on_scroll_by_delta = [this](auto x_delta, auto y_delta) {
  32. // FIXME: This currently isn't reached because we do not yet propagate mouse wheel events to WebContent.
  33. // When that is implemented, make sure our mutations to the viewport position here are correct.
  34. auto position = m_viewport_rect.location();
  35. position.set_x(position.x() + x_delta);
  36. position.set_y(position.y() + y_delta);
  37. if (on_scroll_to_point)
  38. on_scroll_to_point(position);
  39. };
  40. on_scroll_into_view = [this](auto rect) {
  41. if (m_viewport_rect.contains(rect))
  42. return;
  43. auto position = m_viewport_rect.location();
  44. if (rect.top() < m_viewport_rect.top()) {
  45. position.set_y(rect.top());
  46. } else if (rect.top() > m_viewport_rect.top() && rect.bottom() > m_viewport_rect.bottom()) {
  47. position.set_y(rect.bottom() - m_viewport_rect.height());
  48. } else {
  49. return;
  50. }
  51. if (on_scroll_to_point)
  52. on_scroll_to_point(position);
  53. };
  54. }
  55. WebViewBridge::~WebViewBridge() = default;
  56. void WebViewBridge::set_system_visibility_state(bool is_visible)
  57. {
  58. client().async_set_system_visibility_state(is_visible);
  59. }
  60. void WebViewBridge::set_viewport_rect(Gfx::IntRect viewport_rect, ForResize for_resize)
  61. {
  62. viewport_rect.set_size(scale_for_device(viewport_rect.size(), m_device_pixel_ratio));
  63. m_viewport_rect = viewport_rect;
  64. client().async_set_viewport_rect(m_viewport_rect);
  65. request_repaint();
  66. if (for_resize == ForResize::Yes) {
  67. handle_resize();
  68. }
  69. }
  70. void WebViewBridge::mouse_down_event(Gfx::IntPoint position, GUI::MouseButton button, KeyModifier modifiers)
  71. {
  72. client().async_mouse_down(to_content_position(position), to_underlying(button), to_underlying(button), modifiers);
  73. }
  74. void WebViewBridge::mouse_up_event(Gfx::IntPoint position, GUI::MouseButton button, KeyModifier modifiers)
  75. {
  76. client().async_mouse_up(to_content_position(position), to_underlying(button), to_underlying(button), modifiers);
  77. }
  78. void WebViewBridge::mouse_move_event(Gfx::IntPoint position, GUI::MouseButton button, KeyModifier modifiers)
  79. {
  80. client().async_mouse_move(to_content_position(position), 0, to_underlying(button), modifiers);
  81. }
  82. void WebViewBridge::mouse_double_click_event(Gfx::IntPoint position, GUI::MouseButton button, KeyModifier modifiers)
  83. {
  84. client().async_doubleclick(to_content_position(position), button, to_underlying(button), modifiers);
  85. }
  86. void WebViewBridge::key_down_event(KeyCode key_code, KeyModifier modifiers, u32 code_point)
  87. {
  88. client().async_key_down(key_code, modifiers, code_point);
  89. }
  90. void WebViewBridge::key_up_event(KeyCode key_code, KeyModifier modifiers, u32 code_point)
  91. {
  92. client().async_key_up(key_code, modifiers, code_point);
  93. }
  94. Optional<WebViewBridge::Paintable> WebViewBridge::paintable()
  95. {
  96. Gfx::Bitmap* bitmap = nullptr;
  97. Gfx::IntSize bitmap_size;
  98. if (m_client_state.has_usable_bitmap) {
  99. bitmap = m_client_state.front_bitmap.bitmap.ptr();
  100. bitmap_size = m_client_state.front_bitmap.last_painted_size;
  101. } else {
  102. bitmap = m_backup_bitmap.ptr();
  103. bitmap_size = m_backup_bitmap_size;
  104. }
  105. if (!bitmap)
  106. return {};
  107. return Paintable { *bitmap, bitmap_size };
  108. }
  109. void WebViewBridge::notify_server_did_enter_tooltip_area(Badge<WebView::WebContentClient>, Gfx::IntPoint, DeprecatedString const& tooltip)
  110. {
  111. if (on_tooltip_entered)
  112. on_tooltip_entered(tooltip);
  113. }
  114. void WebViewBridge::notify_server_did_leave_tooltip_area(Badge<WebView::WebContentClient>)
  115. {
  116. if (on_tooltip_left)
  117. on_tooltip_left();
  118. }
  119. void WebViewBridge::notify_server_did_finish_handling_input_event(bool)
  120. {
  121. }
  122. void WebViewBridge::update_zoom()
  123. {
  124. }
  125. Gfx::IntRect WebViewBridge::viewport_rect() const
  126. {
  127. return m_viewport_rect;
  128. }
  129. Gfx::IntPoint WebViewBridge::to_content_position(Gfx::IntPoint widget_position) const
  130. {
  131. return scale_for_device(widget_position, m_device_pixel_ratio);
  132. }
  133. Gfx::IntPoint WebViewBridge::to_widget_position(Gfx::IntPoint content_position) const
  134. {
  135. return scale_for_device(content_position, m_inverse_device_pixel_ratio);
  136. }
  137. void WebViewBridge::create_client(WebView::EnableCallgrindProfiling enable_callgrind_profiling)
  138. {
  139. m_client_state = {};
  140. auto candidate_web_content_paths = MUST(get_paths_for_helper_process("WebContent"sv));
  141. auto new_client = MUST(launch_web_content_process(*this, candidate_web_content_paths, enable_callgrind_profiling, WebView::IsLayoutTestMode::No, Ladybird::UseLagomNetworking::Yes));
  142. m_client_state.client = new_client;
  143. m_client_state.client->on_web_content_process_crash = [this] {
  144. Core::deferred_invoke([this] {
  145. handle_web_content_process_crash();
  146. });
  147. };
  148. m_client_state.client_handle = MUST(Web::Crypto::generate_random_uuid());
  149. client().async_set_window_handle(m_client_state.client_handle);
  150. client().async_set_device_pixels_per_css_pixel(m_device_pixel_ratio);
  151. client().async_update_system_fonts(Gfx::FontDatabase::default_font_query(), Gfx::FontDatabase::fixed_width_font_query(), Gfx::FontDatabase::window_title_font_query());
  152. update_palette();
  153. if (!m_screen_rects.is_empty()) {
  154. // FIXME: Update the screens again if they ever change.
  155. client().async_update_screen_rects(m_screen_rects, 0);
  156. }
  157. if (m_webdriver_content_ipc_path.has_value()) {
  158. client().async_connect_to_webdriver(*m_webdriver_content_ipc_path);
  159. }
  160. }
  161. void WebViewBridge::update_palette()
  162. {
  163. auto theme = MUST(Gfx::load_system_theme(DeprecatedString::formatted("{}/res/themes/Default.ini", s_serenity_resource_root)));
  164. auto palette_impl = Gfx::PaletteImpl::create_with_anonymous_buffer(theme);
  165. auto palette = Gfx::Palette(move(palette_impl));
  166. client().async_update_system_theme(move(theme));
  167. }
  168. }