OutOfProcessWebView.cpp 15 KB

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