OutOfProcessWebView.cpp 29 KB

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