OutOfProcessWebView.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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/DeprecatedString.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. REGISTER_WIDGET(WebView, OutOfProcessWebView)
  20. namespace WebView {
  21. OutOfProcessWebView::OutOfProcessWebView()
  22. {
  23. set_should_hide_unnecessary_scrollbars(true);
  24. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  25. create_client();
  26. on_did_layout = [this](auto content_size) {
  27. set_content_size(content_size);
  28. };
  29. on_ready_to_paint = [this]() {
  30. update();
  31. };
  32. on_request_file = [this](auto const& path, auto request_id) {
  33. auto file = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), path);
  34. if (file.is_error())
  35. client().async_handle_file_return(file.error().code(), {}, request_id);
  36. else
  37. client().async_handle_file_return(0, IPC::File(file.value().stream()), request_id);
  38. };
  39. on_scroll_by_delta = [this](auto x_delta, auto y_delta) {
  40. horizontal_scrollbar().increase_slider_by(x_delta);
  41. vertical_scrollbar().increase_slider_by(y_delta);
  42. };
  43. on_scroll_to_point = [this](auto position) {
  44. horizontal_scrollbar().set_value(position.x());
  45. vertical_scrollbar().set_value(position.y());
  46. };
  47. on_scroll_into_view = [this](auto rect) {
  48. scroll_into_view(rect, true, true);
  49. };
  50. on_cursor_change = [this](auto cursor) {
  51. set_override_cursor(cursor);
  52. };
  53. on_enter_tooltip_area = [](auto, auto tooltip) {
  54. GUI::Application::the()->show_tooltip(MUST(String::from_deprecated_string(tooltip)), nullptr);
  55. };
  56. on_leave_tooltip_area = []() {
  57. GUI::Application::the()->hide_tooltip();
  58. };
  59. on_finish_handling_input_event = [this](auto event_was_accepted) {
  60. did_finish_handling_input_event(event_was_accepted);
  61. };
  62. }
  63. OutOfProcessWebView::~OutOfProcessWebView() = default;
  64. void OutOfProcessWebView::create_client()
  65. {
  66. m_client_state = {};
  67. m_client_state.client = WebContentClient::try_create(*this).release_value_but_fixme_should_propagate_errors();
  68. m_client_state.client->on_web_content_process_crash = [this] {
  69. deferred_invoke([this] {
  70. handle_web_content_process_crash();
  71. });
  72. };
  73. m_client_state.client_handle = Web::Crypto::generate_random_uuid().release_value_but_fixme_should_propagate_errors();
  74. client().async_set_window_handle(m_client_state.client_handle);
  75. client().async_update_system_theme(Gfx::current_system_theme_buffer());
  76. client().async_update_system_fonts(Gfx::FontDatabase::default_font_query(), Gfx::FontDatabase::fixed_width_font_query(), Gfx::FontDatabase::window_title_font_query());
  77. Vector<Web::DevicePixelRect> screen_rects;
  78. for (auto const& screen_rect : GUI::Desktop::the().rects()) {
  79. screen_rects.append(screen_rect.to_type<Web::DevicePixels>());
  80. }
  81. client().async_update_screen_rects(screen_rects, GUI::Desktop::the().main_screen_index());
  82. }
  83. void OutOfProcessWebView::paint_event(GUI::PaintEvent& event)
  84. {
  85. Super::paint_event(event);
  86. // If the available size is empty, we don't have a front or back bitmap to draw.
  87. if (available_size().is_empty())
  88. return;
  89. GUI::Painter painter(*this);
  90. painter.add_clip_rect(event.rect());
  91. if (auto* bitmap = m_client_state.has_usable_bitmap ? m_client_state.front_bitmap.bitmap.ptr() : m_backup_bitmap.ptr()) {
  92. painter.add_clip_rect(frame_inner_rect());
  93. painter.translate(frame_thickness(), frame_thickness());
  94. if (m_content_scales_to_viewport) {
  95. auto bitmap_rect = Gfx::IntRect {
  96. {},
  97. m_client_state.has_usable_bitmap
  98. ? m_client_state.front_bitmap.last_painted_size
  99. : m_backup_bitmap_size
  100. };
  101. painter.draw_scaled_bitmap(rect(), *bitmap, bitmap_rect);
  102. } else {
  103. painter.blit({ 0, 0 }, *bitmap, bitmap->rect());
  104. }
  105. return;
  106. }
  107. painter.fill_rect(frame_inner_rect(), palette().base());
  108. }
  109. void OutOfProcessWebView::resize_event(GUI::ResizeEvent& event)
  110. {
  111. Super::resize_event(event);
  112. client().async_set_viewport_rect(Web::DevicePixelRect({ horizontal_scrollbar().value(), vertical_scrollbar().value() }, available_size()));
  113. handle_resize();
  114. }
  115. Web::DevicePixelRect OutOfProcessWebView::viewport_rect() const
  116. {
  117. return visible_content_rect().to_type<Web::DevicePixels>();
  118. }
  119. Gfx::IntPoint OutOfProcessWebView::to_content_position(Gfx::IntPoint widget_position) const
  120. {
  121. return GUI::AbstractScrollableWidget::to_content_position(widget_position);
  122. }
  123. Gfx::IntPoint OutOfProcessWebView::to_widget_position(Gfx::IntPoint content_position) const
  124. {
  125. return GUI::AbstractScrollableWidget::to_widget_position(content_position);
  126. }
  127. void OutOfProcessWebView::update_zoom()
  128. {
  129. client().async_set_device_pixels_per_css_pixel(m_device_pixel_ratio * m_zoom_level);
  130. // FIXME: Refactor this into separate update_viewport_rect() + request_repaint() like in Ladybird
  131. handle_resize();
  132. }
  133. void OutOfProcessWebView::keydown_event(GUI::KeyEvent& event)
  134. {
  135. enqueue_input_event(event);
  136. }
  137. void OutOfProcessWebView::keyup_event(GUI::KeyEvent& event)
  138. {
  139. enqueue_input_event(event);
  140. }
  141. void OutOfProcessWebView::mousedown_event(GUI::MouseEvent& event)
  142. {
  143. enqueue_input_event(event);
  144. }
  145. void OutOfProcessWebView::mouseup_event(GUI::MouseEvent& event)
  146. {
  147. enqueue_input_event(event);
  148. if (event.button() == GUI::MouseButton::Backward) {
  149. if (on_navigate_back)
  150. on_navigate_back();
  151. } else if (event.button() == GUI::MouseButton::Forward) {
  152. if (on_navigate_forward)
  153. on_navigate_forward();
  154. }
  155. }
  156. void OutOfProcessWebView::mousemove_event(GUI::MouseEvent& event)
  157. {
  158. enqueue_input_event(event);
  159. }
  160. void OutOfProcessWebView::mousewheel_event(GUI::MouseEvent& event)
  161. {
  162. enqueue_input_event(event);
  163. }
  164. void OutOfProcessWebView::doubleclick_event(GUI::MouseEvent& event)
  165. {
  166. enqueue_input_event(event);
  167. }
  168. void OutOfProcessWebView::theme_change_event(GUI::ThemeChangeEvent& event)
  169. {
  170. Super::theme_change_event(event);
  171. client().async_update_system_theme(Gfx::current_system_theme_buffer());
  172. request_repaint();
  173. }
  174. void OutOfProcessWebView::screen_rects_change_event(GUI::ScreenRectsChangeEvent& event)
  175. {
  176. Vector<Web::DevicePixelRect> screen_rects;
  177. for (auto const& screen_rect : event.rects()) {
  178. screen_rects.append(screen_rect.to_type<Web::DevicePixels>());
  179. }
  180. client().async_update_screen_rects(screen_rects, event.main_screen_index());
  181. }
  182. void OutOfProcessWebView::did_scroll()
  183. {
  184. client().async_set_viewport_rect(visible_content_rect().to_type<Web::DevicePixels>());
  185. request_repaint();
  186. }
  187. DeprecatedString OutOfProcessWebView::dump_layout_tree()
  188. {
  189. return client().dump_layout_tree();
  190. }
  191. OrderedHashMap<String, String> OutOfProcessWebView::get_local_storage_entries()
  192. {
  193. return client().get_local_storage_entries();
  194. }
  195. OrderedHashMap<String, String> OutOfProcessWebView::get_session_storage_entries()
  196. {
  197. return client().get_session_storage_entries();
  198. }
  199. void OutOfProcessWebView::set_content_filters(Vector<String> filters)
  200. {
  201. client().async_set_content_filters(move(filters));
  202. }
  203. void OutOfProcessWebView::set_autoplay_allowed_on_all_websites()
  204. {
  205. client().async_set_autoplay_allowed_on_all_websites();
  206. }
  207. void OutOfProcessWebView::set_autoplay_allowlist(Vector<String> allowlist)
  208. {
  209. client().async_set_autoplay_allowlist(move(allowlist));
  210. }
  211. void OutOfProcessWebView::set_proxy_mappings(Vector<DeprecatedString> proxies, HashMap<DeprecatedString, size_t> mappings)
  212. {
  213. client().async_set_proxy_mappings(move(proxies), move(mappings));
  214. }
  215. void OutOfProcessWebView::connect_to_webdriver(DeprecatedString const& webdriver_ipc_path)
  216. {
  217. client().async_connect_to_webdriver(webdriver_ipc_path);
  218. }
  219. void OutOfProcessWebView::set_window_position(Gfx::IntPoint position)
  220. {
  221. client().async_set_window_position(position.to_type<Web::DevicePixels>());
  222. }
  223. void OutOfProcessWebView::set_window_size(Gfx::IntSize size)
  224. {
  225. client().async_set_window_size(size.to_type<Web::DevicePixels>());
  226. }
  227. void OutOfProcessWebView::focusin_event(GUI::FocusEvent&)
  228. {
  229. client().async_set_has_focus(true);
  230. }
  231. void OutOfProcessWebView::focusout_event(GUI::FocusEvent&)
  232. {
  233. client().async_set_has_focus(false);
  234. }
  235. void OutOfProcessWebView::set_system_visibility_state(bool visible)
  236. {
  237. client().async_set_system_visibility_state(visible);
  238. }
  239. void OutOfProcessWebView::show_event(GUI::ShowEvent&)
  240. {
  241. set_system_visibility_state(true);
  242. }
  243. void OutOfProcessWebView::hide_event(GUI::HideEvent&)
  244. {
  245. set_system_visibility_state(false);
  246. }
  247. void OutOfProcessWebView::enqueue_input_event(InputEvent const& event)
  248. {
  249. m_pending_input_events.enqueue(event);
  250. process_next_input_event();
  251. }
  252. void OutOfProcessWebView::process_next_input_event()
  253. {
  254. if (m_pending_input_events.is_empty())
  255. return;
  256. if (m_is_awaiting_response_for_input_event)
  257. return;
  258. m_is_awaiting_response_for_input_event = true;
  259. // Send the next event over to the web content to be handled by JS.
  260. // We'll later get a message to say whether JS prevented the default event behavior,
  261. // at which point we either discard or handle that event, then try and process the next one.
  262. auto event = m_pending_input_events.head();
  263. event.visit(
  264. [this](GUI::KeyEvent const& event) {
  265. switch (event.type()) {
  266. case GUI::Event::Type::KeyDown:
  267. client().async_key_down(event.key(), event.modifiers(), event.code_point());
  268. break;
  269. case GUI::Event::Type::KeyUp:
  270. client().async_key_up(event.key(), event.modifiers(), event.code_point());
  271. break;
  272. default:
  273. dbgln("Unrecognized key event type in OOPWV input event queue: {}", event.type());
  274. VERIFY_NOT_REACHED();
  275. }
  276. },
  277. [this](GUI::MouseEvent const& event) {
  278. auto screen_position = event.position() + (window()->position() + relative_position());
  279. switch (event.type()) {
  280. case GUI::Event::Type::MouseDown:
  281. client().async_mouse_down(to_content_position(event.position()), screen_position, event.button(), event.buttons(), event.modifiers());
  282. break;
  283. case GUI::Event::Type::MouseUp:
  284. client().async_mouse_up(to_content_position(event.position()), screen_position, event.button(), event.buttons(), event.modifiers());
  285. break;
  286. case GUI::Event::Type::MouseMove:
  287. client().async_mouse_move(to_content_position(event.position()), screen_position, event.button(), event.buttons(), event.modifiers());
  288. break;
  289. case GUI::Event::Type::MouseWheel: {
  290. // FIXME: This wheel delta step size multiplier is used to remain the old scroll behaviour, in future use system step size.
  291. constexpr int scroll_step_size = 24;
  292. client().async_mouse_wheel(to_content_position(event.position()), screen_position, event.button(), event.buttons(), event.modifiers(), event.wheel_delta_x() * scroll_step_size, event.wheel_delta_y() * scroll_step_size);
  293. break;
  294. }
  295. case GUI::Event::Type::MouseDoubleClick:
  296. client().async_doubleclick(to_content_position(event.position()), screen_position, event.button(), event.buttons(), event.modifiers());
  297. break;
  298. default:
  299. dbgln("Unrecognized mouse event type in OOPWV input event queue: {}", event.type());
  300. VERIFY_NOT_REACHED();
  301. }
  302. });
  303. }
  304. void OutOfProcessWebView::did_finish_handling_input_event(bool event_was_accepted)
  305. {
  306. VERIFY(m_is_awaiting_response_for_input_event);
  307. auto event = m_pending_input_events.dequeue();
  308. m_is_awaiting_response_for_input_event = false;
  309. if (!event_was_accepted) {
  310. // Here we handle events that were not consumed or cancelled by web content.
  311. // That is, we manually implement the steps that would have happened if the original
  312. // OutOfProcessWebView::foo_event() had called event.ignore().
  313. //
  314. // The first step is to give our superclass a chance to handle the event.
  315. //
  316. // Then, if it does not, we dispatch the event to our parent widget, but limited so
  317. // that it will never bubble up to the Window. (Otherwise, it would then dispatch the
  318. // event to us since we are the focused widget, and it would go round indefinitely.)
  319. //
  320. // Finally, any unhandled KeyDown events are propagated to trigger any Actions.
  321. event.visit(
  322. [this](GUI::KeyEvent& event) {
  323. switch (event.type()) {
  324. case GUI::Event::Type::KeyDown:
  325. Super::keydown_event(event);
  326. break;
  327. case GUI::Event::Type::KeyUp:
  328. Super::keyup_event(event);
  329. break;
  330. default:
  331. dbgln("Unhandled key event type in OOPWV input event queue: {}", event.type());
  332. VERIFY_NOT_REACHED();
  333. }
  334. if (!event.is_accepted()) {
  335. parent_widget()->dispatch_event(event, window());
  336. // NOTE: If other events can ever trigger shortcuts, propagate those here.
  337. if (!event.is_accepted() && event.type() == GUI::Event::Type::KeyDown)
  338. window()->propagate_shortcuts(event, this);
  339. }
  340. },
  341. [this](GUI::MouseEvent& event) {
  342. switch (event.type()) {
  343. case GUI::Event::Type::MouseDown:
  344. Super::mousedown_event(event);
  345. break;
  346. case GUI::Event::Type::MouseUp:
  347. Super::mouseup_event(event);
  348. break;
  349. case GUI::Event::Type::MouseMove:
  350. Super::mousemove_event(event);
  351. break;
  352. case GUI::Event::Type::MouseWheel:
  353. Super::mousewheel_event(event);
  354. break;
  355. case GUI::Event::Type::MouseDoubleClick:
  356. Super::doubleclick_event(event);
  357. break;
  358. default:
  359. dbgln("Unhandled mouse event type in OOPWV input event queue: {}", event.type());
  360. VERIFY_NOT_REACHED();
  361. }
  362. if (!event.is_accepted())
  363. parent_widget()->dispatch_event(event, window());
  364. // FIXME: Propagate event for mouse-button shortcuts once that is implemented.
  365. });
  366. }
  367. process_next_input_event();
  368. }
  369. void OutOfProcessWebView::set_content_scales_to_viewport(bool b)
  370. {
  371. m_content_scales_to_viewport = b;
  372. }
  373. }