OutOfProcessWebView.cpp 18 KB

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