LadybirdWebViewBridge.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #import <UI/Palette.h>
  15. namespace Ladybird {
  16. template<typename T>
  17. static T scale_for_device(T size, float device_pixel_ratio)
  18. {
  19. return size.template to_type<float>().scaled(device_pixel_ratio).template to_type<int>();
  20. }
  21. ErrorOr<NonnullOwnPtr<WebViewBridge>> WebViewBridge::create(Vector<Web::DevicePixelRect> screen_rects, float device_pixel_ratio, WebContentOptions const& web_content_options, Optional<StringView> webdriver_content_ipc_path, Web::CSS::PreferredColorScheme preferred_color_scheme)
  22. {
  23. return adopt_nonnull_own_or_enomem(new (nothrow) WebViewBridge(move(screen_rects), device_pixel_ratio, web_content_options, move(webdriver_content_ipc_path), preferred_color_scheme));
  24. }
  25. WebViewBridge::WebViewBridge(Vector<Web::DevicePixelRect> screen_rects, float device_pixel_ratio, WebContentOptions const& web_content_options, Optional<StringView> webdriver_content_ipc_path, Web::CSS::PreferredColorScheme preferred_color_scheme)
  26. : m_screen_rects(move(screen_rects))
  27. , m_web_content_options(web_content_options)
  28. , m_webdriver_content_ipc_path(move(webdriver_content_ipc_path))
  29. , m_preferred_color_scheme(preferred_color_scheme)
  30. {
  31. m_device_pixel_ratio = device_pixel_ratio;
  32. create_client();
  33. on_scroll_by_delta = [this](auto x_delta, auto y_delta) {
  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. on_scroll_to_point = [this](auto position) {
  55. if (on_scroll)
  56. on_scroll(to_widget_position(position));
  57. };
  58. }
  59. WebViewBridge::~WebViewBridge() = default;
  60. void WebViewBridge::set_device_pixel_ratio(float device_pixel_ratio)
  61. {
  62. m_device_pixel_ratio = device_pixel_ratio;
  63. client().async_set_device_pixels_per_css_pixel(m_device_pixel_ratio * m_zoom_level);
  64. }
  65. void WebViewBridge::set_system_visibility_state(bool is_visible)
  66. {
  67. client().async_set_system_visibility_state(is_visible);
  68. }
  69. void WebViewBridge::set_viewport_rect(Gfx::IntRect viewport_rect, ForResize for_resize)
  70. {
  71. viewport_rect.set_size(scale_for_device(viewport_rect.size(), m_device_pixel_ratio));
  72. m_viewport_rect = viewport_rect;
  73. client().async_set_viewport_rect(m_viewport_rect.to_type<Web::DevicePixels>());
  74. request_repaint();
  75. if (for_resize == ForResize::Yes) {
  76. handle_resize();
  77. }
  78. }
  79. void WebViewBridge::update_palette()
  80. {
  81. auto theme = create_system_palette();
  82. client().async_update_system_theme(move(theme));
  83. }
  84. void WebViewBridge::set_preferred_color_scheme(Web::CSS::PreferredColorScheme color_scheme)
  85. {
  86. m_preferred_color_scheme = color_scheme;
  87. client().async_set_preferred_color_scheme(color_scheme);
  88. }
  89. void WebViewBridge::mouse_wheel_event(Gfx::IntPoint position, Gfx::IntPoint screen_position, GUI::MouseButton button, KeyModifier modifiers, int wheel_delta_x, int wheel_delta_y)
  90. {
  91. client().async_mouse_wheel(to_content_position(position), screen_position, to_underlying(button), to_underlying(button), modifiers, wheel_delta_x, wheel_delta_y);
  92. }
  93. void WebViewBridge::mouse_down_event(Gfx::IntPoint position, Gfx::IntPoint screen_position, GUI::MouseButton button, KeyModifier modifiers)
  94. {
  95. client().async_mouse_down(to_content_position(position), screen_position, to_underlying(button), to_underlying(button), modifiers);
  96. }
  97. void WebViewBridge::mouse_up_event(Gfx::IntPoint position, Gfx::IntPoint screen_position, GUI::MouseButton button, KeyModifier modifiers)
  98. {
  99. client().async_mouse_up(to_content_position(position), screen_position, to_underlying(button), to_underlying(button), modifiers);
  100. }
  101. void WebViewBridge::mouse_move_event(Gfx::IntPoint position, Gfx::IntPoint screen_position, GUI::MouseButton button, KeyModifier modifiers)
  102. {
  103. client().async_mouse_move(to_content_position(position), screen_position, 0, to_underlying(button), modifiers);
  104. }
  105. void WebViewBridge::mouse_double_click_event(Gfx::IntPoint position, Gfx::IntPoint screen_position, GUI::MouseButton button, KeyModifier modifiers)
  106. {
  107. client().async_doubleclick(to_content_position(position), screen_position, button, to_underlying(button), modifiers);
  108. }
  109. void WebViewBridge::key_down_event(KeyCode key_code, KeyModifier modifiers, u32 code_point)
  110. {
  111. client().async_key_down(key_code, modifiers, code_point);
  112. }
  113. void WebViewBridge::key_up_event(KeyCode key_code, KeyModifier modifiers, u32 code_point)
  114. {
  115. client().async_key_up(key_code, modifiers, code_point);
  116. }
  117. Optional<WebViewBridge::Paintable> WebViewBridge::paintable()
  118. {
  119. Gfx::Bitmap* bitmap = nullptr;
  120. Gfx::IntSize bitmap_size;
  121. if (m_client_state.has_usable_bitmap) {
  122. bitmap = m_client_state.front_bitmap.bitmap.ptr();
  123. bitmap_size = m_client_state.front_bitmap.last_painted_size.to_type<int>();
  124. } else {
  125. bitmap = m_backup_bitmap.ptr();
  126. bitmap_size = m_backup_bitmap_size.to_type<int>();
  127. }
  128. if (!bitmap)
  129. return {};
  130. return Paintable { *bitmap, bitmap_size };
  131. }
  132. void WebViewBridge::update_zoom()
  133. {
  134. client().async_set_device_pixels_per_css_pixel(m_device_pixel_ratio * m_zoom_level);
  135. if (on_zoom_level_changed)
  136. on_zoom_level_changed();
  137. }
  138. Web::DevicePixelRect WebViewBridge::viewport_rect() const
  139. {
  140. return m_viewport_rect.to_type<Web::DevicePixels>();
  141. }
  142. Gfx::IntPoint WebViewBridge::to_content_position(Gfx::IntPoint widget_position) const
  143. {
  144. return scale_for_device(widget_position, m_device_pixel_ratio);
  145. }
  146. Gfx::IntPoint WebViewBridge::to_widget_position(Gfx::IntPoint content_position) const
  147. {
  148. return scale_for_device(content_position, inverse_device_pixel_ratio());
  149. }
  150. void WebViewBridge::create_client()
  151. {
  152. m_client_state = {};
  153. auto candidate_web_content_paths = MUST(get_paths_for_helper_process("WebContent"sv));
  154. auto new_client = MUST(launch_web_content_process(*this, candidate_web_content_paths, m_web_content_options));
  155. m_client_state.client = new_client;
  156. m_client_state.client->on_web_content_process_crash = [this] {
  157. Core::deferred_invoke([this] {
  158. handle_web_content_process_crash();
  159. });
  160. };
  161. m_client_state.client_handle = MUST(Web::Crypto::generate_random_uuid());
  162. client().async_set_window_handle(m_client_state.client_handle);
  163. client().async_set_device_pixels_per_css_pixel(m_device_pixel_ratio);
  164. client().async_update_system_fonts(Gfx::FontDatabase::default_font_query(), Gfx::FontDatabase::fixed_width_font_query(), Gfx::FontDatabase::window_title_font_query());
  165. client().async_set_preferred_color_scheme(m_preferred_color_scheme);
  166. update_palette();
  167. if (!m_screen_rects.is_empty()) {
  168. // FIXME: Update the screens again if they ever change.
  169. client().async_update_screen_rects(m_screen_rects, 0);
  170. }
  171. if (m_webdriver_content_ipc_path.has_value()) {
  172. client().async_connect_to_webdriver(*m_webdriver_content_ipc_path);
  173. }
  174. }
  175. }