OutOfProcessWebView.cpp 15 KB

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