OutOfProcessWebView.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "OutOfProcessWebView.h"
  7. #include "WebContentClient.h"
  8. #include <AK/ByteString.h>
  9. #include <LibFileSystemAccessClient/Client.h>
  10. #include <LibGUI/Application.h>
  11. #include <LibGUI/Desktop.h>
  12. #include <LibGUI/Painter.h>
  13. #include <LibGUI/Scrollbar.h>
  14. #include <LibGUI/Window.h>
  15. #include <LibGfx/Font/FontDatabase.h>
  16. #include <LibGfx/Palette.h>
  17. #include <LibGfx/SystemTheme.h>
  18. #include <LibWeb/Crypto/Crypto.h>
  19. #include <LibWeb/Worker/WebWorkerClient.h>
  20. REGISTER_WIDGET(WebView, OutOfProcessWebView)
  21. namespace WebView {
  22. OutOfProcessWebView::OutOfProcessWebView()
  23. {
  24. set_should_hide_unnecessary_scrollbars(true);
  25. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  26. initialize_client(CreateNewClient::Yes);
  27. on_did_layout = [this](auto content_size) {
  28. set_content_size(content_size);
  29. };
  30. on_ready_to_paint = [this]() {
  31. update();
  32. };
  33. on_request_file = [this](auto const& path, auto request_id) {
  34. auto file = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), path);
  35. if (file.is_error())
  36. client().async_handle_file_return(m_client_state.page_index, file.error().code(), {}, request_id);
  37. else
  38. client().async_handle_file_return(m_client_state.page_index, 0, IPC::File::adopt_file(file.release_value().release_stream()), request_id);
  39. };
  40. on_scroll_by_delta = [this](auto x_delta, auto y_delta) {
  41. horizontal_scrollbar().increase_slider_by(x_delta);
  42. vertical_scrollbar().increase_slider_by(y_delta);
  43. };
  44. on_scroll_to_point = [this](auto position) {
  45. horizontal_scrollbar().set_value(position.x());
  46. vertical_scrollbar().set_value(position.y());
  47. };
  48. on_cursor_change = [this](auto cursor) {
  49. set_override_cursor(cursor);
  50. };
  51. on_enter_tooltip_area = [](auto, auto tooltip) {
  52. GUI::Application::the()->show_tooltip(MUST(String::from_byte_string(tooltip)), nullptr);
  53. };
  54. on_leave_tooltip_area = []() {
  55. GUI::Application::the()->hide_tooltip();
  56. };
  57. on_finish_handling_key_event = [this](auto const& event) {
  58. finish_handling_key_event(event);
  59. };
  60. on_request_worker_agent = []() {
  61. auto worker_client = MUST(Web::HTML::WebWorkerClient::try_create());
  62. return worker_client->dup_socket();
  63. };
  64. }
  65. OutOfProcessWebView::~OutOfProcessWebView() = default;
  66. void OutOfProcessWebView::initialize_client(WebView::ViewImplementation::CreateNewClient)
  67. {
  68. // FIXME: Don't create a new process when CreateNewClient is false
  69. // We should create a new tab/window in the UI instead, and re-use the existing WebContentClient object.
  70. m_client_state = {};
  71. m_client_state.client = WebContentClient::try_create(*this).release_value_but_fixme_should_propagate_errors();
  72. m_client_state.client->on_web_content_process_crash = [this] {
  73. deferred_invoke([this] {
  74. handle_web_content_process_crash();
  75. });
  76. };
  77. m_client_state.client_handle = Web::Crypto::generate_random_uuid().release_value_but_fixme_should_propagate_errors();
  78. client().async_set_window_handle(m_client_state.page_index, m_client_state.client_handle);
  79. client().async_update_system_theme(m_client_state.page_index, Gfx::current_system_theme_buffer());
  80. 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());
  81. Vector<Web::DevicePixelRect> screen_rects;
  82. for (auto const& screen_rect : GUI::Desktop::the().rects()) {
  83. screen_rects.append(screen_rect.to_type<Web::DevicePixels>());
  84. }
  85. client().async_update_screen_rects(m_client_state.page_index, screen_rects, GUI::Desktop::the().main_screen_index());
  86. }
  87. void OutOfProcessWebView::paint_event(GUI::PaintEvent& event)
  88. {
  89. Super::paint_event(event);
  90. // If the available size is empty, we don't have a front or back bitmap to draw.
  91. if (available_size().is_empty())
  92. return;
  93. GUI::Painter painter(*this);
  94. painter.add_clip_rect(event.rect());
  95. if (auto* bitmap = m_client_state.has_usable_bitmap ? m_client_state.front_bitmap.bitmap.ptr() : m_backup_bitmap.ptr()) {
  96. painter.add_clip_rect(frame_inner_rect());
  97. painter.translate(frame_thickness(), frame_thickness());
  98. if (m_content_scales_to_viewport) {
  99. auto bitmap_rect = Gfx::IntRect {
  100. {},
  101. m_client_state.has_usable_bitmap
  102. ? m_client_state.front_bitmap.last_painted_size
  103. : m_backup_bitmap_size
  104. };
  105. painter.draw_scaled_bitmap(rect(), *bitmap, bitmap_rect);
  106. } else {
  107. painter.blit({ 0, 0 }, *bitmap, bitmap->rect());
  108. }
  109. return;
  110. }
  111. painter.fill_rect(frame_inner_rect(), palette().base());
  112. }
  113. void OutOfProcessWebView::resize_event(GUI::ResizeEvent& event)
  114. {
  115. Super::resize_event(event);
  116. client().async_set_viewport_rect(m_client_state.page_index, Web::DevicePixelRect({ horizontal_scrollbar().value(), vertical_scrollbar().value() }, available_size()));
  117. handle_resize();
  118. }
  119. Web::DevicePixelRect OutOfProcessWebView::viewport_rect() const
  120. {
  121. return visible_content_rect().to_type<Web::DevicePixels>();
  122. }
  123. Gfx::IntPoint OutOfProcessWebView::to_content_position(Gfx::IntPoint widget_position) const
  124. {
  125. return GUI::AbstractScrollableWidget::to_content_position(widget_position);
  126. }
  127. Gfx::IntPoint OutOfProcessWebView::to_widget_position(Gfx::IntPoint content_position) const
  128. {
  129. return GUI::AbstractScrollableWidget::to_widget_position(content_position);
  130. }
  131. void OutOfProcessWebView::update_zoom()
  132. {
  133. client().async_set_device_pixels_per_css_pixel(m_client_state.page_index, m_device_pixel_ratio * m_zoom_level);
  134. // FIXME: Refactor this into separate update_viewport_rect() + request_repaint() like in Ladybird
  135. handle_resize();
  136. }
  137. void OutOfProcessWebView::keydown_event(GUI::KeyEvent& event)
  138. {
  139. enqueue_native_event(Web::KeyEvent::Type::KeyDown, event);
  140. }
  141. void OutOfProcessWebView::keyup_event(GUI::KeyEvent& event)
  142. {
  143. enqueue_native_event(Web::KeyEvent::Type::KeyUp, event);
  144. }
  145. void OutOfProcessWebView::mousedown_event(GUI::MouseEvent& event)
  146. {
  147. enqueue_native_event(Web::MouseEvent::Type::MouseDown, event);
  148. }
  149. void OutOfProcessWebView::mouseup_event(GUI::MouseEvent& event)
  150. {
  151. enqueue_native_event(Web::MouseEvent::Type::MouseUp, event);
  152. if (event.button() == GUI::MouseButton::Backward) {
  153. if (on_navigate_back)
  154. on_navigate_back();
  155. } else if (event.button() == GUI::MouseButton::Forward) {
  156. if (on_navigate_forward)
  157. on_navigate_forward();
  158. }
  159. }
  160. void OutOfProcessWebView::mousemove_event(GUI::MouseEvent& event)
  161. {
  162. enqueue_native_event(Web::MouseEvent::Type::MouseMove, event);
  163. }
  164. void OutOfProcessWebView::mousewheel_event(GUI::MouseEvent& event)
  165. {
  166. enqueue_native_event(Web::MouseEvent::Type::MouseWheel, event);
  167. }
  168. void OutOfProcessWebView::doubleclick_event(GUI::MouseEvent& event)
  169. {
  170. enqueue_native_event(Web::MouseEvent::Type::DoubleClick, event);
  171. }
  172. void OutOfProcessWebView::theme_change_event(GUI::ThemeChangeEvent& event)
  173. {
  174. Super::theme_change_event(event);
  175. client().async_update_system_theme(m_client_state.page_index, Gfx::current_system_theme_buffer());
  176. }
  177. void OutOfProcessWebView::screen_rects_change_event(GUI::ScreenRectsChangeEvent& event)
  178. {
  179. Vector<Web::DevicePixelRect> screen_rects;
  180. for (auto const& screen_rect : event.rects()) {
  181. screen_rects.append(screen_rect.to_type<Web::DevicePixels>());
  182. }
  183. client().async_update_screen_rects(m_client_state.page_index, screen_rects, event.main_screen_index());
  184. }
  185. void OutOfProcessWebView::did_scroll()
  186. {
  187. client().async_set_viewport_rect(m_client_state.page_index, visible_content_rect().to_type<Web::DevicePixels>());
  188. }
  189. ByteString OutOfProcessWebView::dump_layout_tree()
  190. {
  191. return client().dump_layout_tree(m_client_state.page_index);
  192. }
  193. OrderedHashMap<String, String> OutOfProcessWebView::get_local_storage_entries()
  194. {
  195. return client().get_local_storage_entries(m_client_state.page_index);
  196. }
  197. OrderedHashMap<String, String> OutOfProcessWebView::get_session_storage_entries()
  198. {
  199. return client().get_session_storage_entries(m_client_state.page_index);
  200. }
  201. void OutOfProcessWebView::set_content_filters(Vector<String> filters)
  202. {
  203. client().async_set_content_filters(m_client_state.page_index, move(filters));
  204. }
  205. void OutOfProcessWebView::set_autoplay_allowed_on_all_websites()
  206. {
  207. client().async_set_autoplay_allowed_on_all_websites(m_client_state.page_index);
  208. }
  209. void OutOfProcessWebView::set_autoplay_allowlist(Vector<String> allowlist)
  210. {
  211. client().async_set_autoplay_allowlist(m_client_state.page_index, move(allowlist));
  212. }
  213. void OutOfProcessWebView::set_proxy_mappings(Vector<ByteString> proxies, HashMap<ByteString, size_t> mappings)
  214. {
  215. client().async_set_proxy_mappings(m_client_state.page_index, move(proxies), move(mappings));
  216. }
  217. void OutOfProcessWebView::connect_to_webdriver(ByteString const& webdriver_ipc_path)
  218. {
  219. client().async_connect_to_webdriver(m_client_state.page_index, webdriver_ipc_path);
  220. }
  221. void OutOfProcessWebView::set_window_position(Gfx::IntPoint position)
  222. {
  223. client().async_set_window_position(m_client_state.page_index, position.to_type<Web::DevicePixels>());
  224. }
  225. void OutOfProcessWebView::set_window_size(Gfx::IntSize size)
  226. {
  227. client().async_set_window_size(m_client_state.page_index, size.to_type<Web::DevicePixels>());
  228. }
  229. void OutOfProcessWebView::focusin_event(GUI::FocusEvent&)
  230. {
  231. client().async_set_has_focus(m_client_state.page_index, true);
  232. }
  233. void OutOfProcessWebView::focusout_event(GUI::FocusEvent&)
  234. {
  235. client().async_set_has_focus(m_client_state.page_index, false);
  236. }
  237. void OutOfProcessWebView::set_system_visibility_state(bool visible)
  238. {
  239. client().async_set_system_visibility_state(m_client_state.page_index, visible);
  240. }
  241. void OutOfProcessWebView::show_event(GUI::ShowEvent&)
  242. {
  243. set_system_visibility_state(true);
  244. }
  245. void OutOfProcessWebView::hide_event(GUI::HideEvent&)
  246. {
  247. set_system_visibility_state(false);
  248. }
  249. void OutOfProcessWebView::enqueue_native_event(Web::MouseEvent::Type type, GUI::MouseEvent const& event)
  250. {
  251. auto position = to_content_position(event.position()).to_type<Web::DevicePixels>();
  252. auto screen_position = (event.position() + (window()->position() + relative_position())).to_type<Web::DevicePixels>();
  253. // FIXME: This wheel delta step size multiplier is used to remain the old scroll behaviour, in future use system step size.
  254. static constexpr int SCROLL_STEP_SIZE = 24;
  255. auto wheel_delta_x = event.wheel_delta_x() * SCROLL_STEP_SIZE;
  256. auto wheel_delta_y = event.wheel_delta_y() * SCROLL_STEP_SIZE;
  257. enqueue_input_event(Web::MouseEvent { type, position, screen_position, static_cast<Web::UIEvents::MouseButton>(to_underlying(event.button())), static_cast<Web::UIEvents::MouseButton>(event.buttons()), static_cast<KeyModifier>(event.modifiers()), wheel_delta_x, wheel_delta_y, nullptr });
  258. }
  259. struct KeyData : Web::ChromeInputData {
  260. explicit KeyData(GUI::KeyEvent const& event)
  261. : event(make<GUI::KeyEvent>(event))
  262. {
  263. }
  264. NonnullOwnPtr<GUI::KeyEvent> event;
  265. };
  266. void OutOfProcessWebView::enqueue_native_event(Web::KeyEvent::Type type, GUI::KeyEvent const& event)
  267. {
  268. enqueue_input_event(Web::KeyEvent { type, event.key(), static_cast<KeyModifier>(event.modifiers()), event.code_point(), make<KeyData>(event) });
  269. }
  270. void OutOfProcessWebView::finish_handling_key_event(Web::KeyEvent const& key_event)
  271. {
  272. // First, we give our superclass a chance to handle the event.
  273. //
  274. // If it does not, we dispatch the event to our parent widget, but limited such that it will never bubble up to the
  275. // Window. (Otherwise, it would then dispatch the event to us since we are the focused widget, and it would go around
  276. // indefinitely.)
  277. //
  278. // Finally, any unhandled KeyDown events are propagated to trigger any shortcut Actions.
  279. auto& chrome_data = verify_cast<KeyData>(*key_event.chrome_data);
  280. auto& event = *chrome_data.event;
  281. switch (key_event.type) {
  282. case Web::KeyEvent::Type::KeyDown:
  283. Super::keydown_event(event);
  284. break;
  285. case Web::KeyEvent::Type::KeyUp:
  286. Super::keyup_event(event);
  287. break;
  288. }
  289. if (!event.is_accepted()) {
  290. parent_widget()->dispatch_event(event, window());
  291. // NOTE: If other events can ever trigger shortcuts, propagate those here.
  292. if (!event.is_accepted() && event.type() == GUI::Event::Type::KeyDown)
  293. window()->propagate_shortcuts(static_cast<GUI::KeyEvent&>(event), this);
  294. }
  295. }
  296. void OutOfProcessWebView::set_content_scales_to_viewport(bool b)
  297. {
  298. m_content_scales_to_viewport = b;
  299. }
  300. }