PageClient.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /*
  2. * Copyright (c) 2020-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibGfx/ShareableBitmap.h>
  9. #include <LibJS/Console.h>
  10. #include <LibJS/Runtime/ConsoleObject.h>
  11. #include <LibWeb/Bindings/MainThreadVM.h>
  12. #include <LibWeb/CSS/CSSImportRule.h>
  13. #include <LibWeb/Cookie/ParsedCookie.h>
  14. #include <LibWeb/DOM/Attr.h>
  15. #include <LibWeb/DOM/NamedNodeMap.h>
  16. #include <LibWeb/HTML/HTMLLinkElement.h>
  17. #include <LibWeb/HTML/HTMLStyleElement.h>
  18. #include <LibWeb/HTML/Scripting/ClassicScript.h>
  19. #include <LibWeb/HTML/TraversableNavigable.h>
  20. #include <LibWeb/Layout/Viewport.h>
  21. #include <LibWeb/Painting/PaintableBox.h>
  22. #include <LibWeb/Painting/ViewportPaintable.h>
  23. #include <LibWebView/Attribute.h>
  24. #include <WebContent/ConnectionFromClient.h>
  25. #include <WebContent/PageClient.h>
  26. #include <WebContent/PageHost.h>
  27. #include <WebContent/WebContentClientEndpoint.h>
  28. #include <WebContent/WebDriverConnection.h>
  29. namespace WebContent {
  30. static PageClient::UseSkiaPainter s_use_skia_painter = PageClient::UseSkiaPainter::GPUBackendIfAvailable;
  31. JS_DEFINE_ALLOCATOR(PageClient);
  32. void PageClient::set_use_skia_painter(UseSkiaPainter use_skia_painter)
  33. {
  34. s_use_skia_painter = use_skia_painter;
  35. }
  36. JS::NonnullGCPtr<PageClient> PageClient::create(JS::VM& vm, PageHost& page_host, u64 id)
  37. {
  38. return vm.heap().allocate_without_realm<PageClient>(page_host, id);
  39. }
  40. PageClient::PageClient(PageHost& owner, u64 id)
  41. : m_owner(owner)
  42. , m_page(Web::Page::create(Web::Bindings::main_thread_vm(), *this))
  43. , m_id(id)
  44. , m_backing_store_manager(*this)
  45. {
  46. setup_palette();
  47. }
  48. PageClient::~PageClient() = default;
  49. void PageClient::schedule_repaint()
  50. {
  51. if (m_paint_state != PaintState::Ready) {
  52. m_paint_state = PaintState::PaintWhenReady;
  53. return;
  54. }
  55. }
  56. bool PageClient::is_ready_to_paint() const
  57. {
  58. return m_paint_state == PaintState::Ready;
  59. }
  60. void PageClient::ready_to_paint()
  61. {
  62. auto old_paint_state = exchange(m_paint_state, PaintState::Ready);
  63. if (old_paint_state == PaintState::PaintWhenReady) {
  64. // NOTE: Repainting always has to be scheduled from HTML event loop processing steps
  65. // to make sure style and layout are up-to-date.
  66. Web::HTML::main_thread_event_loop().schedule();
  67. }
  68. }
  69. void PageClient::visit_edges(JS::Cell::Visitor& visitor)
  70. {
  71. Base::visit_edges(visitor);
  72. visitor.visit(m_page);
  73. }
  74. ConnectionFromClient& PageClient::client() const
  75. {
  76. return m_owner.client();
  77. }
  78. void PageClient::set_has_focus(bool has_focus)
  79. {
  80. m_has_focus = has_focus;
  81. }
  82. void PageClient::setup_palette()
  83. {
  84. // FIXME: Get the proper palette from our peer somehow
  85. auto buffer_or_error = Core::AnonymousBuffer::create_with_size(sizeof(Gfx::SystemTheme));
  86. VERIFY(!buffer_or_error.is_error());
  87. auto buffer = buffer_or_error.release_value();
  88. auto* theme = buffer.data<Gfx::SystemTheme>();
  89. theme->color[to_underlying(Gfx::ColorRole::Window)] = Color(Color::Magenta).value();
  90. theme->color[to_underlying(Gfx::ColorRole::WindowText)] = Color(Color::Cyan).value();
  91. m_palette_impl = Gfx::PaletteImpl::create_with_anonymous_buffer(buffer);
  92. }
  93. bool PageClient::is_connection_open() const
  94. {
  95. return client().is_open();
  96. }
  97. Gfx::Palette PageClient::palette() const
  98. {
  99. return Gfx::Palette(*m_palette_impl);
  100. }
  101. void PageClient::set_palette_impl(Gfx::PaletteImpl& impl)
  102. {
  103. m_palette_impl = impl;
  104. if (auto* document = page().top_level_browsing_context().active_document())
  105. document->invalidate_style();
  106. }
  107. void PageClient::set_preferred_color_scheme(Web::CSS::PreferredColorScheme color_scheme)
  108. {
  109. m_preferred_color_scheme = color_scheme;
  110. if (auto* document = page().top_level_browsing_context().active_document())
  111. document->invalidate_style();
  112. }
  113. void PageClient::set_preferred_contrast(Web::CSS::PreferredContrast contrast)
  114. {
  115. m_preferred_contrast = contrast;
  116. if (auto* document = page().top_level_browsing_context().active_document())
  117. document->invalidate_style();
  118. }
  119. void PageClient::set_preferred_motion(Web::CSS::PreferredMotion motion)
  120. {
  121. m_preferred_motion = motion;
  122. if (auto* document = page().top_level_browsing_context().active_document())
  123. document->invalidate_style();
  124. }
  125. void PageClient::set_is_scripting_enabled(bool is_scripting_enabled)
  126. {
  127. page().set_is_scripting_enabled(is_scripting_enabled);
  128. }
  129. void PageClient::set_window_position(Web::DevicePixelPoint position)
  130. {
  131. page().set_window_position(position);
  132. }
  133. void PageClient::set_window_size(Web::DevicePixelSize size)
  134. {
  135. page().set_window_size(size);
  136. }
  137. Web::Layout::Viewport* PageClient::layout_root()
  138. {
  139. auto* document = page().top_level_browsing_context().active_document();
  140. if (!document)
  141. return nullptr;
  142. return document->layout_node();
  143. }
  144. void PageClient::process_screenshot_requests()
  145. {
  146. while (!m_screenshot_tasks.is_empty()) {
  147. auto task = m_screenshot_tasks.dequeue();
  148. if (task.node_id.has_value()) {
  149. auto* dom_node = Web::DOM::Node::from_unique_id(*task.node_id);
  150. if (!dom_node || !dom_node->paintable_box()) {
  151. client().async_did_take_screenshot(m_id, {});
  152. continue;
  153. }
  154. auto rect = page().enclosing_device_rect(dom_node->paintable_box()->absolute_border_box_rect());
  155. auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, rect.size().to_type<int>()).release_value_but_fixme_should_propagate_errors();
  156. auto backing_store = Web::Painting::BitmapBackingStore(*bitmap);
  157. paint(rect, backing_store, { .paint_overlay = Web::PaintOptions::PaintOverlay::No });
  158. client().async_did_take_screenshot(m_id, bitmap->to_shareable_bitmap());
  159. } else {
  160. Web::DevicePixelRect rect { { 0, 0 }, content_size() };
  161. auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, rect.size().to_type<int>()).release_value_but_fixme_should_propagate_errors();
  162. auto backing_store = Web::Painting::BitmapBackingStore(*bitmap);
  163. paint(rect, backing_store);
  164. client().async_did_take_screenshot(m_id, bitmap->to_shareable_bitmap());
  165. }
  166. }
  167. }
  168. void PageClient::paint_next_frame()
  169. {
  170. process_screenshot_requests();
  171. auto back_store = m_backing_store_manager.back_store();
  172. if (!back_store)
  173. return;
  174. auto viewport_rect = page().css_to_device_rect(page().top_level_traversable()->viewport_rect());
  175. paint(viewport_rect, *back_store);
  176. m_backing_store_manager.swap_back_and_front();
  177. m_paint_state = PaintState::WaitingForClient;
  178. client().async_did_paint(m_id, viewport_rect.to_type<int>(), m_backing_store_manager.front_id());
  179. }
  180. void PageClient::paint(Web::DevicePixelRect const& content_rect, Web::Painting::BackingStore& target, Web::PaintOptions paint_options)
  181. {
  182. paint_options.should_show_line_box_borders = m_should_show_line_box_borders;
  183. paint_options.has_focus = m_has_focus;
  184. page().top_level_traversable()->paint(content_rect, target, paint_options);
  185. }
  186. void PageClient::set_viewport_size(Web::DevicePixelSize const& size)
  187. {
  188. page().top_level_traversable()->set_viewport_size(page().device_to_css_size(size));
  189. m_backing_store_manager.restart_resize_timer();
  190. m_backing_store_manager.resize_backing_stores_if_needed(BackingStoreManager::WindowResizingInProgress::Yes);
  191. }
  192. void PageClient::page_did_request_cursor_change(Gfx::StandardCursor cursor)
  193. {
  194. client().async_did_request_cursor_change(m_id, (u32)cursor);
  195. }
  196. void PageClient::page_did_layout()
  197. {
  198. auto* layout_root = this->layout_root();
  199. VERIFY(layout_root);
  200. if (layout_root->paintable_box()->has_scrollable_overflow())
  201. m_content_size = page().enclosing_device_rect(layout_root->paintable_box()->scrollable_overflow_rect().value()).size();
  202. else
  203. m_content_size = page().enclosing_device_rect(layout_root->paintable_box()->absolute_rect()).size();
  204. client().async_did_layout(m_id, m_content_size.to_type<int>());
  205. }
  206. void PageClient::page_did_change_title(ByteString const& title)
  207. {
  208. client().async_did_change_title(m_id, title);
  209. }
  210. void PageClient::page_did_change_url(URL::URL const& url)
  211. {
  212. client().async_did_change_url(m_id, url);
  213. }
  214. void PageClient::page_did_request_navigate_back()
  215. {
  216. client().async_did_request_navigate_back(m_id);
  217. }
  218. void PageClient::page_did_request_navigate_forward()
  219. {
  220. client().async_did_request_navigate_forward(m_id);
  221. }
  222. void PageClient::page_did_request_refresh()
  223. {
  224. client().async_did_request_refresh(m_id);
  225. }
  226. Gfx::IntSize PageClient::page_did_request_resize_window(Gfx::IntSize size)
  227. {
  228. return client().did_request_resize_window(m_id, size);
  229. }
  230. Gfx::IntPoint PageClient::page_did_request_reposition_window(Gfx::IntPoint position)
  231. {
  232. return client().did_request_reposition_window(m_id, position);
  233. }
  234. void PageClient::page_did_request_restore_window()
  235. {
  236. client().async_did_request_restore_window(m_id);
  237. }
  238. Gfx::IntRect PageClient::page_did_request_maximize_window()
  239. {
  240. return client().did_request_maximize_window(m_id);
  241. }
  242. Gfx::IntRect PageClient::page_did_request_minimize_window()
  243. {
  244. return client().did_request_minimize_window(m_id);
  245. }
  246. Gfx::IntRect PageClient::page_did_request_fullscreen_window()
  247. {
  248. return client().did_request_fullscreen_window(m_id);
  249. }
  250. void PageClient::page_did_request_tooltip_override(Web::CSSPixelPoint position, ByteString const& title)
  251. {
  252. auto device_position = page().css_to_device_point(position);
  253. client().async_did_request_tooltip_override(m_id, { device_position.x(), device_position.y() }, title);
  254. }
  255. void PageClient::page_did_stop_tooltip_override()
  256. {
  257. client().async_did_leave_tooltip_area(m_id);
  258. }
  259. void PageClient::page_did_enter_tooltip_area(ByteString const& title)
  260. {
  261. client().async_did_enter_tooltip_area(m_id, title);
  262. }
  263. void PageClient::page_did_leave_tooltip_area()
  264. {
  265. client().async_did_leave_tooltip_area(m_id);
  266. }
  267. void PageClient::page_did_hover_link(URL::URL const& url)
  268. {
  269. client().async_did_hover_link(m_id, url);
  270. }
  271. void PageClient::page_did_unhover_link()
  272. {
  273. client().async_did_unhover_link(m_id);
  274. }
  275. void PageClient::page_did_click_link(URL::URL const& url, ByteString const& target, unsigned modifiers)
  276. {
  277. client().async_did_click_link(m_id, url, target, modifiers);
  278. }
  279. void PageClient::page_did_middle_click_link(URL::URL const& url, ByteString const& target, unsigned modifiers)
  280. {
  281. client().async_did_middle_click_link(m_id, url, target, modifiers);
  282. }
  283. void PageClient::page_did_start_loading(URL::URL const& url, bool is_redirect)
  284. {
  285. client().async_did_start_loading(m_id, url, is_redirect);
  286. }
  287. void PageClient::page_did_create_new_document(Web::DOM::Document& document)
  288. {
  289. initialize_js_console(document);
  290. }
  291. void PageClient::page_did_change_active_document_in_top_level_browsing_context(Web::DOM::Document& document)
  292. {
  293. auto& realm = document.realm();
  294. if (auto console_client = document.console_client()) {
  295. auto& web_content_console_client = verify_cast<WebContentConsoleClient>(*console_client);
  296. m_top_level_document_console_client = web_content_console_client;
  297. auto console_object = realm.intrinsics().console_object();
  298. console_object->console().set_client(*console_client);
  299. }
  300. }
  301. void PageClient::page_did_finish_loading(URL::URL const& url)
  302. {
  303. client().async_did_finish_loading(m_id, url);
  304. }
  305. void PageClient::page_did_finish_text_test()
  306. {
  307. client().async_did_finish_text_test(m_id);
  308. }
  309. void PageClient::page_did_request_context_menu(Web::CSSPixelPoint content_position)
  310. {
  311. client().async_did_request_context_menu(m_id, page().css_to_device_point(content_position).to_type<int>());
  312. }
  313. void PageClient::page_did_request_link_context_menu(Web::CSSPixelPoint content_position, URL::URL const& url, ByteString const& target, unsigned modifiers)
  314. {
  315. client().async_did_request_link_context_menu(m_id, page().css_to_device_point(content_position).to_type<int>(), url, target, modifiers);
  316. }
  317. void PageClient::page_did_request_image_context_menu(Web::CSSPixelPoint content_position, URL::URL const& url, ByteString const& target, unsigned modifiers, Gfx::Bitmap const* bitmap_pointer)
  318. {
  319. auto bitmap = bitmap_pointer ? bitmap_pointer->to_shareable_bitmap() : Gfx::ShareableBitmap();
  320. client().async_did_request_image_context_menu(m_id, page().css_to_device_point(content_position).to_type<int>(), url, target, modifiers, bitmap);
  321. }
  322. void PageClient::page_did_request_media_context_menu(Web::CSSPixelPoint content_position, ByteString const& target, unsigned modifiers, Web::Page::MediaContextMenu menu)
  323. {
  324. client().async_did_request_media_context_menu(m_id, page().css_to_device_point(content_position).to_type<int>(), target, modifiers, move(menu));
  325. }
  326. void PageClient::page_did_request_alert(String const& message)
  327. {
  328. client().async_did_request_alert(m_id, message);
  329. }
  330. void PageClient::alert_closed()
  331. {
  332. page().alert_closed();
  333. }
  334. void PageClient::page_did_request_confirm(String const& message)
  335. {
  336. client().async_did_request_confirm(m_id, message);
  337. }
  338. void PageClient::confirm_closed(bool accepted)
  339. {
  340. page().confirm_closed(accepted);
  341. }
  342. void PageClient::page_did_request_prompt(String const& message, String const& default_)
  343. {
  344. client().async_did_request_prompt(m_id, message, default_);
  345. }
  346. void PageClient::page_did_request_set_prompt_text(String const& text)
  347. {
  348. client().async_did_request_set_prompt_text(m_id, text);
  349. }
  350. void PageClient::prompt_closed(Optional<String> response)
  351. {
  352. page().prompt_closed(move(response));
  353. }
  354. void PageClient::color_picker_update(Optional<Color> picked_color, Web::HTML::ColorPickerUpdateState state)
  355. {
  356. page().color_picker_update(picked_color, state);
  357. }
  358. void PageClient::select_dropdown_closed(Optional<u32> const& selected_item_id)
  359. {
  360. page().select_dropdown_closed(selected_item_id);
  361. }
  362. Web::WebIDL::ExceptionOr<void> PageClient::toggle_media_play_state()
  363. {
  364. return page().toggle_media_play_state();
  365. }
  366. void PageClient::toggle_media_mute_state()
  367. {
  368. page().toggle_media_mute_state();
  369. }
  370. Web::WebIDL::ExceptionOr<void> PageClient::toggle_media_loop_state()
  371. {
  372. return page().toggle_media_loop_state();
  373. }
  374. Web::WebIDL::ExceptionOr<void> PageClient::toggle_media_controls_state()
  375. {
  376. return page().toggle_media_controls_state();
  377. }
  378. void PageClient::set_user_style(String source)
  379. {
  380. page().set_user_style(source);
  381. }
  382. void PageClient::page_did_request_accept_dialog()
  383. {
  384. client().async_did_request_accept_dialog(m_id);
  385. }
  386. void PageClient::page_did_request_dismiss_dialog()
  387. {
  388. client().async_did_request_dismiss_dialog(m_id);
  389. }
  390. void PageClient::page_did_change_favicon(Gfx::Bitmap const& favicon)
  391. {
  392. client().async_did_change_favicon(m_id, favicon.to_shareable_bitmap());
  393. }
  394. Vector<Web::Cookie::Cookie> PageClient::page_did_request_all_cookies(URL::URL const& url)
  395. {
  396. return client().did_request_all_cookies(m_id, url);
  397. }
  398. Optional<Web::Cookie::Cookie> PageClient::page_did_request_named_cookie(URL::URL const& url, String const& name)
  399. {
  400. return client().did_request_named_cookie(m_id, url, name);
  401. }
  402. String PageClient::page_did_request_cookie(URL::URL const& url, Web::Cookie::Source source)
  403. {
  404. auto response = client().send_sync_but_allow_failure<Messages::WebContentClient::DidRequestCookie>(m_id, move(url), source);
  405. if (!response) {
  406. dbgln("WebContent client disconnected during DidRequestCookie. Exiting peacefully.");
  407. exit(0);
  408. }
  409. return response->take_cookie();
  410. }
  411. void PageClient::page_did_set_cookie(URL::URL const& url, Web::Cookie::ParsedCookie const& cookie, Web::Cookie::Source source)
  412. {
  413. auto response = client().send_sync_but_allow_failure<Messages::WebContentClient::DidSetCookie>(m_id, url, cookie, source);
  414. if (!response) {
  415. dbgln("WebContent client disconnected during DidSetCookie. Exiting peacefully.");
  416. exit(0);
  417. }
  418. }
  419. void PageClient::page_did_update_cookie(Web::Cookie::Cookie cookie)
  420. {
  421. client().async_did_update_cookie(m_id, move(cookie));
  422. }
  423. void PageClient::page_did_update_resource_count(i32 count_waiting)
  424. {
  425. client().async_did_update_resource_count(m_id, count_waiting);
  426. }
  427. PageClient::NewWebViewResult PageClient::page_did_request_new_web_view(Web::HTML::ActivateTab activate_tab, Web::HTML::WebViewHints hints, Web::HTML::TokenizedFeature::NoOpener no_opener)
  428. {
  429. auto& new_client = m_owner.create_page();
  430. Optional<u64> page_id;
  431. if (no_opener == Web::HTML::TokenizedFeature::NoOpener::Yes) {
  432. // FIXME: Create an abstraction to let this WebContent process know about a new process we create?
  433. // FIXME: For now, just create a new page in the same process anyway
  434. }
  435. page_id = new_client.m_id;
  436. auto response = client().send_sync_but_allow_failure<Messages::WebContentClient::DidRequestNewWebView>(m_id, activate_tab, hints, page_id);
  437. if (!response) {
  438. dbgln("WebContent client disconnected during DidRequestNewWebView. Exiting peacefully.");
  439. exit(0);
  440. }
  441. return { &new_client.page(), response->take_handle() };
  442. }
  443. void PageClient::page_did_request_activate_tab()
  444. {
  445. client().async_did_request_activate_tab(m_id);
  446. }
  447. void PageClient::page_did_close_top_level_traversable()
  448. {
  449. // FIXME: Rename this IPC call
  450. client().async_did_close_browsing_context(m_id);
  451. // NOTE: This only removes the strong reference the PageHost has for this PageClient.
  452. // It will be GC'd 'later'.
  453. m_owner.remove_page({}, m_id);
  454. }
  455. void PageClient::page_did_update_navigation_buttons_state(bool back_enabled, bool forward_enabled)
  456. {
  457. client().async_did_update_navigation_buttons_state(m_id, back_enabled, forward_enabled);
  458. }
  459. void PageClient::request_file(Web::FileRequest file_request)
  460. {
  461. client().request_file(m_id, move(file_request));
  462. }
  463. void PageClient::page_did_request_color_picker(Color current_color)
  464. {
  465. client().async_did_request_color_picker(m_id, current_color);
  466. }
  467. void PageClient::page_did_request_file_picker(Web::HTML::FileFilter accepted_file_types, Web::HTML::AllowMultipleFiles allow_multiple_files)
  468. {
  469. client().async_did_request_file_picker(m_id, move(accepted_file_types), allow_multiple_files);
  470. }
  471. void PageClient::page_did_request_select_dropdown(Web::CSSPixelPoint content_position, Web::CSSPixels minimum_width, Vector<Web::HTML::SelectItem> items)
  472. {
  473. client().async_did_request_select_dropdown(m_id, page().css_to_device_point(content_position).to_type<int>(), minimum_width * device_pixels_per_css_pixel(), items);
  474. }
  475. void PageClient::page_did_change_theme_color(Gfx::Color color)
  476. {
  477. client().async_did_change_theme_color(m_id, color);
  478. }
  479. void PageClient::page_did_insert_clipboard_entry(String data, String presentation_style, String mime_type)
  480. {
  481. client().async_did_insert_clipboard_entry(m_id, move(data), move(presentation_style), move(mime_type));
  482. }
  483. void PageClient::page_did_change_audio_play_state(Web::HTML::AudioPlayState play_state)
  484. {
  485. client().async_did_change_audio_play_state(m_id, play_state);
  486. }
  487. void PageClient::page_did_allocate_backing_stores(i32 front_bitmap_id, Gfx::ShareableBitmap front_bitmap, i32 back_bitmap_id, Gfx::ShareableBitmap back_bitmap)
  488. {
  489. client().async_did_allocate_backing_stores(m_id, front_bitmap_id, front_bitmap, back_bitmap_id, back_bitmap);
  490. }
  491. IPC::File PageClient::request_worker_agent()
  492. {
  493. auto response = client().send_sync_but_allow_failure<Messages::WebContentClient::RequestWorkerAgent>(m_id);
  494. if (!response) {
  495. dbgln("WebContent client disconnected during RequestWorkerAgent. Exiting peacefully.");
  496. exit(0);
  497. }
  498. return response->take_socket();
  499. }
  500. void PageClient::inspector_did_load()
  501. {
  502. client().async_inspector_did_load(m_id);
  503. }
  504. void PageClient::inspector_did_select_dom_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement::Type> const& pseudo_element)
  505. {
  506. client().async_inspector_did_select_dom_node(m_id, node_id, pseudo_element);
  507. }
  508. void PageClient::inspector_did_set_dom_node_text(i32 node_id, String const& text)
  509. {
  510. client().async_inspector_did_set_dom_node_text(m_id, node_id, text);
  511. }
  512. void PageClient::inspector_did_set_dom_node_tag(i32 node_id, String const& tag)
  513. {
  514. client().async_inspector_did_set_dom_node_tag(m_id, node_id, tag);
  515. }
  516. static Vector<WebView::Attribute> named_node_map_to_vector(JS::NonnullGCPtr<Web::DOM::NamedNodeMap> map)
  517. {
  518. Vector<WebView::Attribute> attributes;
  519. attributes.ensure_capacity(map->length());
  520. for (size_t i = 0; i < map->length(); ++i) {
  521. auto const* attribute = map->item(i);
  522. VERIFY(attribute);
  523. attributes.empend(attribute->name().to_string(), attribute->value());
  524. }
  525. return attributes;
  526. }
  527. void PageClient::inspector_did_add_dom_node_attributes(i32 node_id, JS::NonnullGCPtr<Web::DOM::NamedNodeMap> attributes)
  528. {
  529. client().async_inspector_did_add_dom_node_attributes(m_id, node_id, named_node_map_to_vector(attributes));
  530. }
  531. void PageClient::inspector_did_replace_dom_node_attribute(i32 node_id, size_t attribute_index, JS::NonnullGCPtr<Web::DOM::NamedNodeMap> replacement_attributes)
  532. {
  533. client().async_inspector_did_replace_dom_node_attribute(m_id, node_id, attribute_index, named_node_map_to_vector(replacement_attributes));
  534. }
  535. void PageClient::inspector_did_request_dom_tree_context_menu(i32 node_id, Web::CSSPixelPoint position, String const& type, Optional<String> const& tag, Optional<size_t> const& attribute_index)
  536. {
  537. client().async_inspector_did_request_dom_tree_context_menu(m_id, node_id, page().css_to_device_point(position).to_type<int>(), type, tag, attribute_index);
  538. }
  539. void PageClient::inspector_did_execute_console_script(String const& script)
  540. {
  541. client().async_inspector_did_execute_console_script(m_id, script);
  542. }
  543. void PageClient::inspector_did_export_inspector_html(String const& html)
  544. {
  545. client().async_inspector_did_export_inspector_html(m_id, html);
  546. }
  547. ErrorOr<void> PageClient::connect_to_webdriver(ByteString const& webdriver_ipc_path)
  548. {
  549. VERIFY(!m_webdriver);
  550. m_webdriver = TRY(WebDriverConnection::connect(*this, webdriver_ipc_path));
  551. return {};
  552. }
  553. void PageClient::initialize_js_console(Web::DOM::Document& document)
  554. {
  555. if (document.is_temporary_document_for_fragment_parsing())
  556. return;
  557. auto& realm = document.realm();
  558. auto console_object = realm.intrinsics().console_object();
  559. auto console_client = heap().allocate_without_realm<WebContentConsoleClient>(console_object->console(), document.realm(), *this);
  560. document.set_console_client(console_client);
  561. }
  562. void PageClient::js_console_input(ByteString const& js_source)
  563. {
  564. if (m_top_level_document_console_client)
  565. m_top_level_document_console_client->handle_input(js_source);
  566. }
  567. void PageClient::run_javascript(ByteString const& js_source)
  568. {
  569. auto* active_document = page().top_level_browsing_context().active_document();
  570. if (!active_document)
  571. return;
  572. // This is partially based on "execute a javascript: URL request" https://html.spec.whatwg.org/multipage/browsing-the-web.html#javascript-protocol
  573. // Let settings be browsingContext's active document's relevant settings object.
  574. auto& settings = active_document->relevant_settings_object();
  575. // Let baseURL be settings's API base URL.
  576. auto base_url = settings.api_base_url();
  577. // Let script be the result of creating a classic script given scriptSource, settings, baseURL, and the default classic script fetch options.
  578. // FIXME: This doesn't pass in "default classic script fetch options"
  579. // FIXME: What should the filename be here?
  580. auto script = Web::HTML::ClassicScript::create("(client connection run_javascript)", js_source, settings, move(base_url));
  581. // Let evaluationStatus be the result of running the classic script script.
  582. auto evaluation_status = script->run();
  583. if (evaluation_status.is_error())
  584. dbgln("Exception :(");
  585. }
  586. void PageClient::js_console_request_messages(i32 start_index)
  587. {
  588. if (m_top_level_document_console_client)
  589. m_top_level_document_console_client->send_messages(start_index);
  590. }
  591. void PageClient::did_output_js_console_message(i32 message_index)
  592. {
  593. client().async_did_output_js_console_message(m_id, message_index);
  594. }
  595. void PageClient::console_peer_did_misbehave(char const* reason)
  596. {
  597. client().did_misbehave(reason);
  598. }
  599. void PageClient::did_get_js_console_messages(i32 start_index, Vector<ByteString> message_types, Vector<ByteString> messages)
  600. {
  601. client().async_did_get_js_console_messages(m_id, start_index, move(message_types), move(messages));
  602. }
  603. static void gather_style_sheets(Vector<Web::CSS::StyleSheetIdentifier>& results, Web::CSS::CSSStyleSheet& sheet)
  604. {
  605. Web::CSS::StyleSheetIdentifier identifier {};
  606. bool valid = true;
  607. if (sheet.owner_rule()) {
  608. identifier.type = Web::CSS::StyleSheetIdentifier::Type::ImportRule;
  609. } else if (auto* node = sheet.owner_node()) {
  610. if (node->is_html_style_element() || node->is_svg_style_element()) {
  611. identifier.type = Web::CSS::StyleSheetIdentifier::Type::StyleElement;
  612. } else if (is<Web::HTML::HTMLLinkElement>(node)) {
  613. identifier.type = Web::CSS::StyleSheetIdentifier::Type::LinkElement;
  614. } else {
  615. dbgln("Can't identify where style sheet came from; owner node is {}", node->debug_description());
  616. identifier.type = Web::CSS::StyleSheetIdentifier::Type::StyleElement;
  617. }
  618. identifier.dom_element_unique_id = node->unique_id();
  619. } else {
  620. dbgln("Style sheet has no owner rule or owner node; skipping");
  621. valid = false;
  622. }
  623. if (valid) {
  624. if (auto location = sheet.location(); location.has_value())
  625. identifier.url = location.release_value();
  626. results.append(move(identifier));
  627. }
  628. for (auto& import_rule : sheet.import_rules()) {
  629. if (import_rule->loaded_style_sheet()) {
  630. gather_style_sheets(results, *import_rule->loaded_style_sheet());
  631. } else {
  632. // We can gather this anyway, and hope it loads later
  633. results.append({ .type = Web::CSS::StyleSheetIdentifier::Type::ImportRule,
  634. .url = MUST(import_rule->url().to_string()) });
  635. }
  636. }
  637. }
  638. Vector<Web::CSS::StyleSheetIdentifier> PageClient::list_style_sheets() const
  639. {
  640. Vector<Web::CSS::StyleSheetIdentifier> results;
  641. auto const* document = page().top_level_browsing_context().active_document();
  642. if (document) {
  643. for (auto& sheet : document->style_sheets().sheets()) {
  644. gather_style_sheets(results, sheet);
  645. }
  646. }
  647. // User style
  648. if (page().user_style().has_value()) {
  649. results.append({
  650. .type = Web::CSS::StyleSheetIdentifier::Type::UserStyle,
  651. });
  652. }
  653. // User-agent
  654. results.append({
  655. .type = Web::CSS::StyleSheetIdentifier::Type::UserAgent,
  656. .url = "CSS/Default.css"_string,
  657. });
  658. if (document && document->in_quirks_mode()) {
  659. results.append({
  660. .type = Web::CSS::StyleSheetIdentifier::Type::UserAgent,
  661. .url = "CSS/QuirksMode.css"_string,
  662. });
  663. }
  664. results.append({
  665. .type = Web::CSS::StyleSheetIdentifier::Type::UserAgent,
  666. .url = "MathML/Default.css"_string,
  667. });
  668. results.append({
  669. .type = Web::CSS::StyleSheetIdentifier::Type::UserAgent,
  670. .url = "SVG/Default.css"_string,
  671. });
  672. return results;
  673. }
  674. Web::DisplayListPlayerType PageClient::display_list_player_type() const
  675. {
  676. switch (s_use_skia_painter) {
  677. case UseSkiaPainter::GPUBackendIfAvailable:
  678. return Web::DisplayListPlayerType::SkiaGPUIfAvailable;
  679. case UseSkiaPainter::CPUBackend:
  680. return Web::DisplayListPlayerType::SkiaCPU;
  681. default:
  682. VERIFY_NOT_REACHED();
  683. }
  684. }
  685. void PageClient::queue_screenshot_task(Optional<i32> node_id)
  686. {
  687. m_screenshot_tasks.enqueue({ node_id });
  688. page().top_level_traversable()->set_needs_display();
  689. }
  690. }