LadybirdWebViewBridge.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. initialize_client(CreateNewClient::Yes);
  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_to_point = [this](auto position) {
  41. if (on_scroll)
  42. on_scroll(to_widget_position(position));
  43. };
  44. on_request_worker_agent = [this]() {
  45. auto worker_client = MUST(launch_web_worker_process(MUST(get_paths_for_helper_process("WebWorker"sv)), m_web_content_options.certificates));
  46. return worker_client->dup_sockets();
  47. };
  48. }
  49. WebViewBridge::~WebViewBridge() = default;
  50. void WebViewBridge::set_device_pixel_ratio(float device_pixel_ratio)
  51. {
  52. m_device_pixel_ratio = device_pixel_ratio;
  53. client().async_set_device_pixels_per_css_pixel(m_client_state.page_index, m_device_pixel_ratio * m_zoom_level);
  54. }
  55. void WebViewBridge::set_system_visibility_state(bool is_visible)
  56. {
  57. client().async_set_system_visibility_state(m_client_state.page_index, is_visible);
  58. }
  59. void WebViewBridge::set_viewport_rect(Gfx::IntRect viewport_rect, ForResize for_resize)
  60. {
  61. viewport_rect.set_size(scale_for_device(viewport_rect.size(), m_device_pixel_ratio));
  62. m_viewport_rect = viewport_rect;
  63. client().async_set_viewport_rect(m_client_state.page_index, m_viewport_rect.to_type<Web::DevicePixels>());
  64. if (for_resize == ForResize::Yes) {
  65. handle_resize();
  66. }
  67. }
  68. void WebViewBridge::update_palette()
  69. {
  70. auto theme = create_system_palette();
  71. client().async_update_system_theme(m_client_state.page_index, move(theme));
  72. }
  73. void WebViewBridge::set_preferred_color_scheme(Web::CSS::PreferredColorScheme color_scheme)
  74. {
  75. m_preferred_color_scheme = color_scheme;
  76. client().async_set_preferred_color_scheme(m_client_state.page_index, color_scheme);
  77. }
  78. void WebViewBridge::enqueue_input_event(Web::MouseEvent event)
  79. {
  80. event.position = to_content_position(event.position.to_type<int>()).to_type<Web::DevicePixels>();
  81. event.screen_position = to_content_position(event.screen_position.to_type<int>()).to_type<Web::DevicePixels>();
  82. ViewImplementation::enqueue_input_event(move(event));
  83. }
  84. void WebViewBridge::enqueue_input_event(Web::KeyEvent event)
  85. {
  86. ViewImplementation::enqueue_input_event(move(event));
  87. }
  88. Optional<WebViewBridge::Paintable> WebViewBridge::paintable()
  89. {
  90. Gfx::Bitmap* bitmap = nullptr;
  91. Gfx::IntSize bitmap_size;
  92. if (m_client_state.has_usable_bitmap) {
  93. bitmap = m_client_state.front_bitmap.bitmap.ptr();
  94. bitmap_size = m_client_state.front_bitmap.last_painted_size.to_type<int>();
  95. } else {
  96. bitmap = m_backup_bitmap.ptr();
  97. bitmap_size = m_backup_bitmap_size.to_type<int>();
  98. }
  99. if (!bitmap)
  100. return {};
  101. return Paintable { *bitmap, bitmap_size };
  102. }
  103. void WebViewBridge::update_zoom()
  104. {
  105. client().async_set_device_pixels_per_css_pixel(m_client_state.page_index, m_device_pixel_ratio * m_zoom_level);
  106. if (on_zoom_level_changed)
  107. on_zoom_level_changed();
  108. }
  109. Web::DevicePixelRect WebViewBridge::viewport_rect() const
  110. {
  111. return m_viewport_rect.to_type<Web::DevicePixels>();
  112. }
  113. Gfx::IntPoint WebViewBridge::to_content_position(Gfx::IntPoint widget_position) const
  114. {
  115. return scale_for_device(widget_position, m_device_pixel_ratio);
  116. }
  117. Gfx::IntPoint WebViewBridge::to_widget_position(Gfx::IntPoint content_position) const
  118. {
  119. return scale_for_device(content_position, inverse_device_pixel_ratio());
  120. }
  121. void WebViewBridge::initialize_client(CreateNewClient)
  122. {
  123. // FIXME: Don't create a new process when CreateNewClient is false
  124. // We should create a new tab/window in the UI instead, and re-use the existing WebContentClient object.
  125. m_client_state = {};
  126. auto candidate_web_content_paths = MUST(get_paths_for_helper_process("WebContent"sv));
  127. auto new_client = MUST(launch_web_content_process(*this, candidate_web_content_paths, m_web_content_options));
  128. m_client_state.client = new_client;
  129. m_client_state.client->on_web_content_process_crash = [this] {
  130. Core::deferred_invoke([this] {
  131. handle_web_content_process_crash();
  132. });
  133. };
  134. m_client_state.client_handle = MUST(Web::Crypto::generate_random_uuid());
  135. client().async_set_window_handle(m_client_state.page_index, m_client_state.client_handle);
  136. client().async_set_device_pixels_per_css_pixel(m_client_state.page_index, m_device_pixel_ratio);
  137. client().async_update_system_fonts(m_client_state.page_index, Gfx::FontDatabase::default_font_query(), Gfx::FontDatabase::fixed_width_font_query(), Gfx::FontDatabase::window_title_font_query());
  138. client().async_set_preferred_color_scheme(m_client_state.page_index, m_preferred_color_scheme);
  139. update_palette();
  140. if (!m_screen_rects.is_empty()) {
  141. // FIXME: Update the screens again if they ever change.
  142. client().async_update_screen_rects(m_client_state.page_index, m_screen_rects, 0);
  143. }
  144. if (m_webdriver_content_ipc_path.has_value()) {
  145. client().async_connect_to_webdriver(m_client_state.page_index, *m_webdriver_content_ipc_path);
  146. }
  147. }
  148. }