OutOfProcessWebView.cpp 27 KB

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