OutOfProcessWebView.cpp 18 KB

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