OutOfProcessWebView.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. painter.draw_scaled_bitmap(rect(), *bitmap, bitmap->rect());
  59. else
  60. painter.blit({ 0, 0 }, *bitmap, bitmap->rect());
  61. return;
  62. }
  63. painter.fill_rect(frame_inner_rect(), palette().base());
  64. }
  65. void OutOfProcessWebView::resize_event(GUI::ResizeEvent& event)
  66. {
  67. Super::resize_event(event);
  68. client().async_set_viewport_rect(Gfx::IntRect({ horizontal_scrollbar().value(), vertical_scrollbar().value() }, available_size()));
  69. handle_resize();
  70. }
  71. Gfx::IntRect OutOfProcessWebView::viewport_rect() const
  72. {
  73. return visible_content_rect();
  74. }
  75. Gfx::IntPoint OutOfProcessWebView::to_content_position(Gfx::IntPoint widget_position) const
  76. {
  77. return GUI::AbstractScrollableWidget::to_content_position(widget_position);
  78. }
  79. Gfx::IntPoint OutOfProcessWebView::to_widget_position(Gfx::IntPoint content_position) const
  80. {
  81. return GUI::AbstractScrollableWidget::to_widget_position(content_position);
  82. }
  83. void OutOfProcessWebView::update_zoom()
  84. {
  85. client().async_set_device_pixels_per_css_pixel(m_device_pixel_ratio * m_zoom_level);
  86. // FIXME: Refactor this into separate update_viewport_rect() + request_repaint() like in Ladybird
  87. handle_resize();
  88. }
  89. void OutOfProcessWebView::keydown_event(GUI::KeyEvent& event)
  90. {
  91. enqueue_input_event(event);
  92. }
  93. void OutOfProcessWebView::keyup_event(GUI::KeyEvent& event)
  94. {
  95. enqueue_input_event(event);
  96. }
  97. void OutOfProcessWebView::mousedown_event(GUI::MouseEvent& event)
  98. {
  99. enqueue_input_event(event);
  100. }
  101. void OutOfProcessWebView::mouseup_event(GUI::MouseEvent& event)
  102. {
  103. enqueue_input_event(event);
  104. if (event.button() == GUI::MouseButton::Backward && on_back_button) {
  105. on_back_button();
  106. } else if (event.button() == GUI::MouseButton::Forward && on_forward_button) {
  107. on_forward_button();
  108. }
  109. }
  110. void OutOfProcessWebView::mousemove_event(GUI::MouseEvent& event)
  111. {
  112. enqueue_input_event(event);
  113. }
  114. void OutOfProcessWebView::mousewheel_event(GUI::MouseEvent& event)
  115. {
  116. enqueue_input_event(event);
  117. }
  118. void OutOfProcessWebView::doubleclick_event(GUI::MouseEvent& event)
  119. {
  120. enqueue_input_event(event);
  121. }
  122. void OutOfProcessWebView::theme_change_event(GUI::ThemeChangeEvent& event)
  123. {
  124. Super::theme_change_event(event);
  125. client().async_update_system_theme(Gfx::current_system_theme_buffer());
  126. request_repaint();
  127. }
  128. void OutOfProcessWebView::screen_rects_change_event(GUI::ScreenRectsChangeEvent& event)
  129. {
  130. client().async_update_screen_rects(event.rects(), event.main_screen_index());
  131. }
  132. void OutOfProcessWebView::notify_server_did_paint(Badge<WebContentClient>, i32 bitmap_id, Gfx::IntSize size)
  133. {
  134. if (m_client_state.back_bitmap.id == bitmap_id) {
  135. m_client_state.has_usable_bitmap = true;
  136. m_client_state.back_bitmap.pending_paints--;
  137. m_client_state.back_bitmap.last_painted_size = size;
  138. swap(m_client_state.back_bitmap, m_client_state.front_bitmap);
  139. // We don't need the backup bitmap anymore, so drop it.
  140. m_backup_bitmap = nullptr;
  141. update();
  142. if (m_client_state.got_repaint_requests_while_painting) {
  143. m_client_state.got_repaint_requests_while_painting = false;
  144. request_repaint();
  145. }
  146. }
  147. }
  148. void OutOfProcessWebView::notify_server_did_invalidate_content_rect(Badge<WebContentClient>, [[maybe_unused]] Gfx::IntRect const& content_rect)
  149. {
  150. request_repaint();
  151. }
  152. void OutOfProcessWebView::notify_server_did_change_selection(Badge<WebContentClient>)
  153. {
  154. request_repaint();
  155. }
  156. void OutOfProcessWebView::notify_server_did_request_cursor_change(Badge<WebContentClient>, Gfx::StandardCursor cursor)
  157. {
  158. set_override_cursor(cursor);
  159. }
  160. void OutOfProcessWebView::notify_server_did_layout(Badge<WebContentClient>, Gfx::IntSize content_size)
  161. {
  162. set_content_size(content_size);
  163. }
  164. void OutOfProcessWebView::notify_server_did_change_title(Badge<WebContentClient>, DeprecatedString const& title)
  165. {
  166. if (on_title_change)
  167. on_title_change(title);
  168. }
  169. void OutOfProcessWebView::notify_server_did_request_scroll(Badge<WebContentClient>, i32 x_delta, i32 y_delta)
  170. {
  171. horizontal_scrollbar().increase_slider_by(x_delta);
  172. vertical_scrollbar().increase_slider_by(y_delta);
  173. }
  174. void OutOfProcessWebView::notify_server_did_request_scroll_to(Badge<WebContentClient>, Gfx::IntPoint scroll_position)
  175. {
  176. horizontal_scrollbar().set_value(scroll_position.x());
  177. vertical_scrollbar().set_value(scroll_position.y());
  178. }
  179. void OutOfProcessWebView::notify_server_did_request_scroll_into_view(Badge<WebContentClient>, Gfx::IntRect const& rect)
  180. {
  181. scroll_into_view(rect, true, true);
  182. }
  183. void OutOfProcessWebView::notify_server_did_enter_tooltip_area(Badge<WebContentClient>, Gfx::IntPoint, DeprecatedString const& title)
  184. {
  185. GUI::Application::the()->show_tooltip(title, nullptr);
  186. }
  187. void OutOfProcessWebView::notify_server_did_leave_tooltip_area(Badge<WebContentClient>)
  188. {
  189. GUI::Application::the()->hide_tooltip();
  190. }
  191. void OutOfProcessWebView::notify_server_did_hover_link(Badge<WebContentClient>, const AK::URL& url)
  192. {
  193. if (on_link_hover)
  194. on_link_hover(url);
  195. }
  196. void OutOfProcessWebView::notify_server_did_unhover_link(Badge<WebContentClient>)
  197. {
  198. set_override_cursor(Gfx::StandardCursor::None);
  199. if (on_link_hover)
  200. on_link_hover({});
  201. }
  202. void OutOfProcessWebView::notify_server_did_click_link(Badge<WebContentClient>, const AK::URL& url, DeprecatedString const& target, unsigned int modifiers)
  203. {
  204. if (on_link_click)
  205. on_link_click(url, target, modifiers);
  206. }
  207. void OutOfProcessWebView::notify_server_did_middle_click_link(Badge<WebContentClient>, const AK::URL& url, DeprecatedString const& target, unsigned int modifiers)
  208. {
  209. if (on_link_middle_click)
  210. on_link_middle_click(url, target, modifiers);
  211. }
  212. void OutOfProcessWebView::notify_server_did_start_loading(Badge<WebContentClient>, const AK::URL& url, bool is_redirect)
  213. {
  214. m_url = url;
  215. if (on_load_start)
  216. on_load_start(url, is_redirect);
  217. }
  218. void OutOfProcessWebView::notify_server_did_finish_loading(Badge<WebContentClient>, const AK::URL& url)
  219. {
  220. m_url = url;
  221. if (on_load_finish)
  222. on_load_finish(url);
  223. }
  224. void OutOfProcessWebView::notify_server_did_request_navigate_back(Badge<WebContentClient>)
  225. {
  226. if (on_navigate_back)
  227. on_navigate_back();
  228. }
  229. void OutOfProcessWebView::notify_server_did_request_navigate_forward(Badge<WebContentClient>)
  230. {
  231. if (on_navigate_forward)
  232. on_navigate_forward();
  233. }
  234. void OutOfProcessWebView::notify_server_did_request_refresh(Badge<WebContentClient>)
  235. {
  236. if (on_refresh)
  237. on_refresh();
  238. }
  239. void OutOfProcessWebView::notify_server_did_request_context_menu(Badge<WebContentClient>, Gfx::IntPoint content_position)
  240. {
  241. if (on_context_menu_request)
  242. on_context_menu_request(screen_relative_rect().location().translated(to_widget_position(content_position)));
  243. }
  244. void OutOfProcessWebView::notify_server_did_request_link_context_menu(Badge<WebContentClient>, Gfx::IntPoint content_position, const AK::URL& url, DeprecatedString const&, unsigned)
  245. {
  246. if (on_link_context_menu_request)
  247. on_link_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)));
  248. }
  249. void OutOfProcessWebView::notify_server_did_request_image_context_menu(Badge<WebContentClient>, Gfx::IntPoint content_position, const AK::URL& url, DeprecatedString const&, unsigned, Gfx::ShareableBitmap const& bitmap)
  250. {
  251. if (on_image_context_menu_request)
  252. on_image_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)), bitmap);
  253. }
  254. void OutOfProcessWebView::notify_server_did_request_video_context_menu(Badge<WebContentClient>, Gfx::IntPoint content_position, const AK::URL& url, DeprecatedString const&, unsigned, bool is_playing, bool has_user_agent_controls, bool is_looping)
  255. {
  256. if (on_video_context_menu_request)
  257. on_video_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)), is_playing, has_user_agent_controls, is_looping);
  258. }
  259. void OutOfProcessWebView::notify_server_did_request_alert(Badge<WebContentClient>, String const& message)
  260. {
  261. m_dialog = GUI::MessageBox::create(window(), message, "Alert"sv, GUI::MessageBox::Type::Information, GUI::MessageBox::InputType::OK).release_value_but_fixme_should_propagate_errors();
  262. m_dialog->set_icon(window()->icon());
  263. m_dialog->exec();
  264. client().async_alert_closed();
  265. m_dialog = nullptr;
  266. }
  267. void OutOfProcessWebView::notify_server_did_request_confirm(Badge<WebContentClient>, String const& message)
  268. {
  269. m_dialog = GUI::MessageBox::create(window(), message, "Confirm"sv, GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OKCancel).release_value_but_fixme_should_propagate_errors();
  270. m_dialog->set_icon(window()->icon());
  271. client().async_confirm_closed(m_dialog->exec() == GUI::Dialog::ExecResult::OK);
  272. m_dialog = nullptr;
  273. }
  274. void OutOfProcessWebView::notify_server_did_request_prompt(Badge<WebContentClient>, String const& message, String const& default_)
  275. {
  276. String mutable_value = default_;
  277. m_dialog = GUI::InputBox::create(window(), mutable_value, message, "Prompt"sv, GUI::InputType::Text).release_value_but_fixme_should_propagate_errors();
  278. m_dialog->set_icon(window()->icon());
  279. if (m_dialog->exec() == GUI::InputBox::ExecResult::OK) {
  280. auto const& dialog = static_cast<GUI::InputBox const&>(*m_dialog);
  281. auto response = dialog.text_value();
  282. client().async_prompt_closed(move(response));
  283. } else {
  284. client().async_prompt_closed({});
  285. }
  286. m_dialog = nullptr;
  287. }
  288. void OutOfProcessWebView::notify_server_did_request_set_prompt_text(Badge<WebContentClient>, String const& message)
  289. {
  290. if (m_dialog && is<GUI::InputBox>(*m_dialog))
  291. static_cast<GUI::InputBox&>(*m_dialog).set_text_value(message);
  292. }
  293. void OutOfProcessWebView::notify_server_did_request_accept_dialog(Badge<WebContentClient>)
  294. {
  295. if (m_dialog)
  296. m_dialog->done(GUI::Dialog::ExecResult::OK);
  297. }
  298. void OutOfProcessWebView::notify_server_did_request_dismiss_dialog(Badge<WebContentClient>)
  299. {
  300. if (m_dialog)
  301. m_dialog->done(GUI::Dialog::ExecResult::Cancel);
  302. }
  303. void OutOfProcessWebView::notify_server_did_get_source(const AK::URL& url, DeprecatedString const& source)
  304. {
  305. if (on_get_source)
  306. on_get_source(url, source);
  307. }
  308. void OutOfProcessWebView::notify_server_did_get_dom_tree(DeprecatedString const& dom_tree)
  309. {
  310. if (on_get_dom_tree)
  311. on_get_dom_tree(dom_tree);
  312. }
  313. void OutOfProcessWebView::notify_server_did_get_dom_node_properties(i32 node_id, DeprecatedString const& computed_style, DeprecatedString const& resolved_style, DeprecatedString const& custom_properties, DeprecatedString const& node_box_sizing)
  314. {
  315. if (on_get_dom_node_properties)
  316. on_get_dom_node_properties(node_id, computed_style, resolved_style, custom_properties, node_box_sizing);
  317. }
  318. void OutOfProcessWebView::notify_server_did_output_js_console_message(i32 message_index)
  319. {
  320. if (on_js_console_new_message)
  321. on_js_console_new_message(message_index);
  322. }
  323. void OutOfProcessWebView::notify_server_did_get_js_console_messages(i32 start_index, Vector<DeprecatedString> const& message_types, Vector<DeprecatedString> const& messages)
  324. {
  325. if (on_get_js_console_messages)
  326. on_get_js_console_messages(start_index, message_types, messages);
  327. }
  328. void OutOfProcessWebView::notify_server_did_change_favicon(Gfx::Bitmap const& favicon)
  329. {
  330. if (on_favicon_change)
  331. on_favicon_change(favicon);
  332. }
  333. Vector<Web::Cookie::Cookie> OutOfProcessWebView::notify_server_did_request_all_cookies(Badge<WebContentClient>, AK::URL const& url)
  334. {
  335. if (on_get_all_cookies)
  336. return on_get_all_cookies(url);
  337. return {};
  338. }
  339. Optional<Web::Cookie::Cookie> OutOfProcessWebView::notify_server_did_request_named_cookie(Badge<WebContentClient>, AK::URL const& url, DeprecatedString const& name)
  340. {
  341. if (on_get_named_cookie)
  342. return on_get_named_cookie(url, name);
  343. return {};
  344. }
  345. DeprecatedString OutOfProcessWebView::notify_server_did_request_cookie(Badge<WebContentClient>, const AK::URL& url, Web::Cookie::Source source)
  346. {
  347. if (on_get_cookie)
  348. return on_get_cookie(url, source);
  349. return {};
  350. }
  351. void OutOfProcessWebView::notify_server_did_set_cookie(Badge<WebContentClient>, const AK::URL& url, Web::Cookie::ParsedCookie const& cookie, Web::Cookie::Source source)
  352. {
  353. if (on_set_cookie)
  354. on_set_cookie(url, cookie, source);
  355. }
  356. void OutOfProcessWebView::notify_server_did_update_cookie(Badge<WebContentClient>, Web::Cookie::Cookie const& cookie)
  357. {
  358. if (on_update_cookie)
  359. on_update_cookie(cookie);
  360. }
  361. String OutOfProcessWebView::notify_server_did_request_new_tab(Badge<WebContentClient>, Web::HTML::ActivateTab activate_tab)
  362. {
  363. if (on_new_tab)
  364. return on_new_tab(activate_tab);
  365. return {};
  366. }
  367. void OutOfProcessWebView::notify_server_did_request_activate_tab(Badge<WebContentClient>)
  368. {
  369. if (on_activate_tab)
  370. on_activate_tab();
  371. }
  372. void OutOfProcessWebView::notify_server_did_close_browsing_context(Badge<WebContentClient>)
  373. {
  374. if (on_close)
  375. on_close();
  376. }
  377. void OutOfProcessWebView::notify_server_did_update_resource_count(i32 count_waiting)
  378. {
  379. if (on_resource_status_change)
  380. on_resource_status_change(count_waiting);
  381. }
  382. void OutOfProcessWebView::notify_server_did_request_restore_window()
  383. {
  384. if (on_restore_window)
  385. on_restore_window();
  386. }
  387. Gfx::IntPoint OutOfProcessWebView::notify_server_did_request_reposition_window(Gfx::IntPoint position)
  388. {
  389. if (on_reposition_window)
  390. return on_reposition_window(position);
  391. return {};
  392. }
  393. Gfx::IntSize OutOfProcessWebView::notify_server_did_request_resize_window(Gfx::IntSize size)
  394. {
  395. if (on_resize_window)
  396. return on_resize_window(size);
  397. return {};
  398. }
  399. Gfx::IntRect OutOfProcessWebView::notify_server_did_request_maximize_window()
  400. {
  401. if (on_maximize_window)
  402. return on_maximize_window();
  403. return {};
  404. }
  405. Gfx::IntRect OutOfProcessWebView::notify_server_did_request_minimize_window()
  406. {
  407. if (on_minimize_window)
  408. return on_minimize_window();
  409. return {};
  410. }
  411. Gfx::IntRect OutOfProcessWebView::notify_server_did_request_fullscreen_window()
  412. {
  413. if (on_fullscreen_window)
  414. return on_fullscreen_window();
  415. return {};
  416. }
  417. void OutOfProcessWebView::notify_server_did_request_file(Badge<WebContentClient>, DeprecatedString const& path, i32 request_id)
  418. {
  419. auto file = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), path);
  420. if (file.is_error())
  421. client().async_handle_file_return(file.error().code(), {}, request_id);
  422. else
  423. client().async_handle_file_return(0, IPC::File(file.value().stream()), request_id);
  424. }
  425. void OutOfProcessWebView::did_scroll()
  426. {
  427. client().async_set_viewport_rect(visible_content_rect());
  428. request_repaint();
  429. }
  430. DeprecatedString OutOfProcessWebView::dump_layout_tree()
  431. {
  432. return client().dump_layout_tree();
  433. }
  434. OrderedHashMap<DeprecatedString, DeprecatedString> OutOfProcessWebView::get_local_storage_entries()
  435. {
  436. return client().get_local_storage_entries();
  437. }
  438. OrderedHashMap<DeprecatedString, DeprecatedString> OutOfProcessWebView::get_session_storage_entries()
  439. {
  440. return client().get_session_storage_entries();
  441. }
  442. void OutOfProcessWebView::set_content_filters(Vector<String> filters)
  443. {
  444. client().async_set_content_filters(move(filters));
  445. }
  446. void OutOfProcessWebView::set_autoplay_allowed_on_all_websites()
  447. {
  448. client().async_set_autoplay_allowed_on_all_websites();
  449. }
  450. void OutOfProcessWebView::set_autoplay_allowlist(Vector<String> allowlist)
  451. {
  452. client().async_set_autoplay_allowlist(move(allowlist));
  453. }
  454. void OutOfProcessWebView::set_proxy_mappings(Vector<DeprecatedString> proxies, HashMap<DeprecatedString, size_t> mappings)
  455. {
  456. client().async_set_proxy_mappings(move(proxies), move(mappings));
  457. }
  458. void OutOfProcessWebView::connect_to_webdriver(DeprecatedString const& webdriver_ipc_path)
  459. {
  460. client().async_connect_to_webdriver(webdriver_ipc_path);
  461. }
  462. void OutOfProcessWebView::set_window_position(Gfx::IntPoint position)
  463. {
  464. client().async_set_window_position(position);
  465. }
  466. void OutOfProcessWebView::set_window_size(Gfx::IntSize size)
  467. {
  468. client().async_set_window_size(size);
  469. }
  470. void OutOfProcessWebView::focusin_event(GUI::FocusEvent&)
  471. {
  472. client().async_set_has_focus(true);
  473. }
  474. void OutOfProcessWebView::focusout_event(GUI::FocusEvent&)
  475. {
  476. client().async_set_has_focus(false);
  477. }
  478. void OutOfProcessWebView::set_system_visibility_state(bool visible)
  479. {
  480. client().async_set_system_visibility_state(visible);
  481. }
  482. void OutOfProcessWebView::show_event(GUI::ShowEvent&)
  483. {
  484. set_system_visibility_state(true);
  485. }
  486. void OutOfProcessWebView::hide_event(GUI::HideEvent&)
  487. {
  488. set_system_visibility_state(false);
  489. }
  490. void OutOfProcessWebView::enqueue_input_event(InputEvent const& event)
  491. {
  492. m_pending_input_events.enqueue(event);
  493. process_next_input_event();
  494. }
  495. void OutOfProcessWebView::process_next_input_event()
  496. {
  497. if (m_pending_input_events.is_empty())
  498. return;
  499. if (m_is_awaiting_response_for_input_event)
  500. return;
  501. m_is_awaiting_response_for_input_event = true;
  502. // Send the next event over to the web content to be handled by JS.
  503. // We'll later get a message to say whether JS prevented the default event behavior,
  504. // at which point we either discard or handle that event, then try and process the next one.
  505. auto event = m_pending_input_events.head();
  506. event.visit(
  507. [this](GUI::KeyEvent const& event) {
  508. switch (event.type()) {
  509. case GUI::Event::Type::KeyDown:
  510. client().async_key_down(event.key(), event.modifiers(), event.code_point());
  511. break;
  512. case GUI::Event::Type::KeyUp:
  513. client().async_key_up(event.key(), event.modifiers(), event.code_point());
  514. break;
  515. default:
  516. dbgln("Unrecognized key event type in OOPWV input event queue: {}", event.type());
  517. VERIFY_NOT_REACHED();
  518. }
  519. },
  520. [this](GUI::MouseEvent const& event) {
  521. switch (event.type()) {
  522. case GUI::Event::Type::MouseDown:
  523. client().async_mouse_down(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers());
  524. break;
  525. case GUI::Event::Type::MouseUp:
  526. client().async_mouse_up(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers());
  527. break;
  528. case GUI::Event::Type::MouseMove:
  529. client().async_mouse_move(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers());
  530. break;
  531. case GUI::Event::Type::MouseWheel:
  532. client().async_mouse_wheel(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers(), event.wheel_delta_x(), event.wheel_delta_y());
  533. break;
  534. case GUI::Event::Type::MouseDoubleClick:
  535. client().async_doubleclick(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers());
  536. break;
  537. default:
  538. dbgln("Unrecognized mouse event type in OOPWV input event queue: {}", event.type());
  539. VERIFY_NOT_REACHED();
  540. }
  541. });
  542. }
  543. void OutOfProcessWebView::notify_server_did_finish_handling_input_event(bool event_was_accepted)
  544. {
  545. VERIFY(m_is_awaiting_response_for_input_event);
  546. auto event = m_pending_input_events.dequeue();
  547. m_is_awaiting_response_for_input_event = false;
  548. if (!event_was_accepted) {
  549. // Here we handle events that were not consumed or cancelled by web content.
  550. // That is, we manually implement the steps that would have happened if the original
  551. // OutOfProcessWebView::foo_event() had called event.ignore().
  552. //
  553. // The first step is to give our superclass a chance to handle the event.
  554. //
  555. // Then, if it does not, we dispatch the event to our parent widget, but limited so
  556. // that it will never bubble up to the Window. (Otherwise, it would then dispatch the
  557. // event to us since we are the focused widget, and it would go round indefinitely.)
  558. //
  559. // Finally, any unhandled KeyDown events are propagated to trigger any Actions.
  560. event.visit(
  561. [this](GUI::KeyEvent& event) {
  562. switch (event.type()) {
  563. case GUI::Event::Type::KeyDown:
  564. Super::keydown_event(event);
  565. break;
  566. case GUI::Event::Type::KeyUp:
  567. Super::keyup_event(event);
  568. break;
  569. default:
  570. dbgln("Unhandled key event type in OOPWV input event queue: {}", event.type());
  571. VERIFY_NOT_REACHED();
  572. }
  573. if (!event.is_accepted()) {
  574. parent_widget()->dispatch_event(event, window());
  575. // NOTE: If other events can ever trigger shortcuts, propagate those here.
  576. if (!event.is_accepted() && event.type() == GUI::Event::Type::KeyDown)
  577. window()->propagate_shortcuts(event, this);
  578. }
  579. },
  580. [this](GUI::MouseEvent& event) {
  581. switch (event.type()) {
  582. case GUI::Event::Type::MouseDown:
  583. Super::mousedown_event(event);
  584. break;
  585. case GUI::Event::Type::MouseUp:
  586. Super::mouseup_event(event);
  587. break;
  588. case GUI::Event::Type::MouseMove:
  589. Super::mousemove_event(event);
  590. break;
  591. case GUI::Event::Type::MouseWheel:
  592. Super::mousewheel_event(event);
  593. break;
  594. case GUI::Event::Type::MouseDoubleClick:
  595. Super::doubleclick_event(event);
  596. break;
  597. default:
  598. dbgln("Unhandled mouse event type in OOPWV input event queue: {}", event.type());
  599. VERIFY_NOT_REACHED();
  600. }
  601. if (!event.is_accepted())
  602. parent_widget()->dispatch_event(event, window());
  603. // FIXME: Propagate event for mouse-button shortcuts once that is implemented.
  604. });
  605. }
  606. process_next_input_event();
  607. }
  608. void OutOfProcessWebView::notify_server_did_get_accessibility_tree(DeprecatedString const& accessibility_tree)
  609. {
  610. if (on_get_accessibility_tree)
  611. on_get_accessibility_tree(accessibility_tree);
  612. }
  613. void OutOfProcessWebView::set_content_scales_to_viewport(bool b)
  614. {
  615. m_content_scales_to_viewport = b;
  616. }
  617. }