OutOfProcessWebView.cpp 18 KB

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