OutOfProcessWebView.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. }
  170. void OutOfProcessWebView::screen_rects_change_event(GUI::ScreenRectsChangeEvent& event)
  171. {
  172. Vector<Web::DevicePixelRect> screen_rects;
  173. for (auto const& screen_rect : event.rects()) {
  174. screen_rects.append(screen_rect.to_type<Web::DevicePixels>());
  175. }
  176. client().async_update_screen_rects(screen_rects, event.main_screen_index());
  177. }
  178. void OutOfProcessWebView::did_scroll()
  179. {
  180. client().async_set_viewport_rect(visible_content_rect().to_type<Web::DevicePixels>());
  181. }
  182. ByteString OutOfProcessWebView::dump_layout_tree()
  183. {
  184. return client().dump_layout_tree();
  185. }
  186. OrderedHashMap<String, String> OutOfProcessWebView::get_local_storage_entries()
  187. {
  188. return client().get_local_storage_entries();
  189. }
  190. OrderedHashMap<String, String> OutOfProcessWebView::get_session_storage_entries()
  191. {
  192. return client().get_session_storage_entries();
  193. }
  194. void OutOfProcessWebView::set_content_filters(Vector<String> filters)
  195. {
  196. client().async_set_content_filters(move(filters));
  197. }
  198. void OutOfProcessWebView::set_autoplay_allowed_on_all_websites()
  199. {
  200. client().async_set_autoplay_allowed_on_all_websites();
  201. }
  202. void OutOfProcessWebView::set_autoplay_allowlist(Vector<String> allowlist)
  203. {
  204. client().async_set_autoplay_allowlist(move(allowlist));
  205. }
  206. void OutOfProcessWebView::set_proxy_mappings(Vector<ByteString> proxies, HashMap<ByteString, size_t> mappings)
  207. {
  208. client().async_set_proxy_mappings(move(proxies), move(mappings));
  209. }
  210. void OutOfProcessWebView::connect_to_webdriver(ByteString const& webdriver_ipc_path)
  211. {
  212. client().async_connect_to_webdriver(webdriver_ipc_path);
  213. }
  214. void OutOfProcessWebView::set_window_position(Gfx::IntPoint position)
  215. {
  216. client().async_set_window_position(position.to_type<Web::DevicePixels>());
  217. }
  218. void OutOfProcessWebView::set_window_size(Gfx::IntSize size)
  219. {
  220. client().async_set_window_size(size.to_type<Web::DevicePixels>());
  221. }
  222. void OutOfProcessWebView::focusin_event(GUI::FocusEvent&)
  223. {
  224. client().async_set_has_focus(true);
  225. }
  226. void OutOfProcessWebView::focusout_event(GUI::FocusEvent&)
  227. {
  228. client().async_set_has_focus(false);
  229. }
  230. void OutOfProcessWebView::set_system_visibility_state(bool visible)
  231. {
  232. client().async_set_system_visibility_state(visible);
  233. }
  234. void OutOfProcessWebView::show_event(GUI::ShowEvent&)
  235. {
  236. set_system_visibility_state(true);
  237. }
  238. void OutOfProcessWebView::hide_event(GUI::HideEvent&)
  239. {
  240. set_system_visibility_state(false);
  241. }
  242. void OutOfProcessWebView::enqueue_input_event(InputEvent const& event)
  243. {
  244. m_pending_input_events.enqueue(event);
  245. process_next_input_event();
  246. }
  247. void OutOfProcessWebView::process_next_input_event()
  248. {
  249. if (m_pending_input_events.is_empty())
  250. return;
  251. if (m_is_awaiting_response_for_input_event)
  252. return;
  253. m_is_awaiting_response_for_input_event = true;
  254. // Send the next event over to the web content to be handled by JS.
  255. // We'll later get a message to say whether JS prevented the default event behavior,
  256. // at which point we either discard or handle that event, then try and process the next one.
  257. auto event = m_pending_input_events.head();
  258. event.visit(
  259. [this](GUI::KeyEvent const& event) {
  260. switch (event.type()) {
  261. case GUI::Event::Type::KeyDown:
  262. client().async_key_down(event.key(), event.modifiers(), event.code_point());
  263. break;
  264. case GUI::Event::Type::KeyUp:
  265. client().async_key_up(event.key(), event.modifiers(), event.code_point());
  266. break;
  267. default:
  268. dbgln("Unrecognized key event type in OOPWV input event queue: {}", event.type());
  269. VERIFY_NOT_REACHED();
  270. }
  271. },
  272. [this](GUI::MouseEvent const& event) {
  273. auto position = to_content_position(event.position()).to_type<Web::DevicePixels>();
  274. auto screen_position = (event.position() + (window()->position() + relative_position())).to_type<Web::DevicePixels>();
  275. switch (event.type()) {
  276. case GUI::Event::Type::MouseDown:
  277. client().async_mouse_down(position, screen_position, event.button(), event.buttons(), event.modifiers());
  278. break;
  279. case GUI::Event::Type::MouseUp:
  280. client().async_mouse_up(position, screen_position, event.button(), event.buttons(), event.modifiers());
  281. break;
  282. case GUI::Event::Type::MouseMove:
  283. client().async_mouse_move(position, screen_position, event.button(), event.buttons(), event.modifiers());
  284. break;
  285. case GUI::Event::Type::MouseWheel: {
  286. // FIXME: This wheel delta step size multiplier is used to remain the old scroll behaviour, in future use system step size.
  287. constexpr int scroll_step_size = 24;
  288. 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);
  289. break;
  290. }
  291. case GUI::Event::Type::MouseDoubleClick:
  292. client().async_doubleclick(position, screen_position, event.button(), event.buttons(), event.modifiers());
  293. break;
  294. default:
  295. dbgln("Unrecognized mouse event type in OOPWV input event queue: {}", event.type());
  296. VERIFY_NOT_REACHED();
  297. }
  298. });
  299. }
  300. void OutOfProcessWebView::did_finish_handling_input_event(bool event_was_accepted)
  301. {
  302. VERIFY(m_is_awaiting_response_for_input_event);
  303. auto event = m_pending_input_events.dequeue();
  304. m_is_awaiting_response_for_input_event = false;
  305. if (!event_was_accepted) {
  306. // Here we handle events that were not consumed or cancelled by web content.
  307. // That is, we manually implement the steps that would have happened if the original
  308. // OutOfProcessWebView::foo_event() had called event.ignore().
  309. //
  310. // The first step is to give our superclass a chance to handle the event.
  311. //
  312. // Then, if it does not, we dispatch the event to our parent widget, but limited so
  313. // that it will never bubble up to the Window. (Otherwise, it would then dispatch the
  314. // event to us since we are the focused widget, and it would go round indefinitely.)
  315. //
  316. // Finally, any unhandled KeyDown events are propagated to trigger any Actions.
  317. event.visit(
  318. [this](GUI::KeyEvent& event) {
  319. switch (event.type()) {
  320. case GUI::Event::Type::KeyDown:
  321. Super::keydown_event(event);
  322. break;
  323. case GUI::Event::Type::KeyUp:
  324. Super::keyup_event(event);
  325. break;
  326. default:
  327. dbgln("Unhandled key event type in OOPWV input event queue: {}", event.type());
  328. VERIFY_NOT_REACHED();
  329. }
  330. if (!event.is_accepted()) {
  331. parent_widget()->dispatch_event(event, window());
  332. // NOTE: If other events can ever trigger shortcuts, propagate those here.
  333. if (!event.is_accepted() && event.type() == GUI::Event::Type::KeyDown)
  334. window()->propagate_shortcuts(event, this);
  335. }
  336. },
  337. [this](GUI::MouseEvent& event) {
  338. switch (event.type()) {
  339. case GUI::Event::Type::MouseDown:
  340. Super::mousedown_event(event);
  341. break;
  342. case GUI::Event::Type::MouseUp:
  343. Super::mouseup_event(event);
  344. break;
  345. case GUI::Event::Type::MouseMove:
  346. Super::mousemove_event(event);
  347. break;
  348. case GUI::Event::Type::MouseWheel:
  349. Super::mousewheel_event(event);
  350. break;
  351. case GUI::Event::Type::MouseDoubleClick:
  352. Super::doubleclick_event(event);
  353. break;
  354. default:
  355. dbgln("Unhandled mouse event type in OOPWV input event queue: {}", event.type());
  356. VERIFY_NOT_REACHED();
  357. }
  358. if (!event.is_accepted())
  359. parent_widget()->dispatch_event(event, window());
  360. // FIXME: Propagate event for mouse-button shortcuts once that is implemented.
  361. });
  362. }
  363. process_next_input_event();
  364. }
  365. void OutOfProcessWebView::set_content_scales_to_viewport(bool b)
  366. {
  367. m_content_scales_to_viewport = b;
  368. }
  369. }