From da171c3230caaee53213d0dd04007c9b4343e3e2 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 23 Aug 2024 11:18:35 +0100 Subject: [PATCH] Inspector: Add a basic style sheet inspector Choosing options from the ` + + + +
+
diff --git a/Userland/Libraries/LibWebView/ViewImplementation.cpp b/Userland/Libraries/LibWebView/ViewImplementation.cpp index ce321232a7c..83f2f7ba59e 100644 --- a/Userland/Libraries/LibWebView/ViewImplementation.cpp +++ b/Userland/Libraries/LibWebView/ViewImplementation.cpp @@ -305,6 +305,16 @@ void ViewImplementation::get_dom_node_html(i32 node_id) client().async_get_dom_node_html(page_id(), node_id); } +void ViewImplementation::list_style_sheets() +{ + client().async_list_style_sheets(page_id()); +} + +void ViewImplementation::request_style_sheet_source(Web::CSS::StyleSheetIdentifier const& identifier) +{ + client().async_request_style_sheet_source(page_id(), identifier); +} + void ViewImplementation::debug_request(ByteString const& request, ByteString const& argument) { client().async_debug_request(page_id(), request, argument); diff --git a/Userland/Libraries/LibWebView/ViewImplementation.h b/Userland/Libraries/LibWebView/ViewImplementation.h index 629f249fc77..a7a3abd821a 100644 --- a/Userland/Libraries/LibWebView/ViewImplementation.h +++ b/Userland/Libraries/LibWebView/ViewImplementation.h @@ -97,6 +97,9 @@ public: void remove_dom_node(i32 node_id); void get_dom_node_html(i32 node_id); + void list_style_sheets(); + void request_style_sheet_source(Web::CSS::StyleSheetIdentifier const&); + void debug_request(ByteString const& request, ByteString const& argument = {}); void run_javascript(StringView); @@ -182,6 +185,9 @@ public: Function on_received_dom_tree; Function)> on_received_dom_node_properties; Function on_received_accessibility_tree; + Function)> on_received_style_sheet_list; + Function on_inspector_requested_style_sheet_source; + Function on_received_style_sheet_source; Function on_received_hovered_node_id; Function const& node_id)> on_finshed_editing_dom_node; Function on_received_dom_node_html; diff --git a/Userland/Libraries/LibWebView/WebContentClient.cpp b/Userland/Libraries/LibWebView/WebContentClient.cpp index ed2600d5935..a0a863faa96 100644 --- a/Userland/Libraries/LibWebView/WebContentClient.cpp +++ b/Userland/Libraries/LibWebView/WebContentClient.cpp @@ -711,6 +711,30 @@ Messages::WebContentClient::RequestWorkerAgentResponse WebContentClient::request return IPC::File {}; } +void WebContentClient::inspector_did_list_style_sheets(u64 page_id, Vector const& stylesheets) +{ + if (auto view = view_for_page_id(page_id); view.has_value()) { + if (view->on_received_style_sheet_list) + view->on_received_style_sheet_list(stylesheets); + } +} + +void WebContentClient::inspector_did_request_style_sheet_source(u64 page_id, Web::CSS::StyleSheetIdentifier const& identifier) +{ + if (auto view = view_for_page_id(page_id); view.has_value()) { + if (view->on_inspector_requested_style_sheet_source) + view->on_inspector_requested_style_sheet_source(identifier); + } +} + +void WebContentClient::did_request_style_sheet_source(u64 page_id, Web::CSS::StyleSheetIdentifier const& identifier, String const& source) +{ + if (auto view = view_for_page_id(page_id); view.has_value()) { + if (view->on_received_style_sheet_source) + view->on_received_style_sheet_source(identifier, source); + } +} + Optional WebContentClient::view_for_page_id(u64 page_id, SourceLocation location) { if (auto view = m_views.get(page_id); view.has_value()) diff --git a/Userland/Libraries/LibWebView/WebContentClient.h b/Userland/Libraries/LibWebView/WebContentClient.h index bd7a40666b4..0e432d100a6 100644 --- a/Userland/Libraries/LibWebView/WebContentClient.h +++ b/Userland/Libraries/LibWebView/WebContentClient.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -122,6 +123,9 @@ private: virtual void inspector_did_execute_console_script(u64 page_id, String const& script) override; virtual void inspector_did_export_inspector_html(u64 page_id, String const& html) override; virtual Messages::WebContentClient::RequestWorkerAgentResponse request_worker_agent(u64 page_id) override; + virtual void inspector_did_list_style_sheets(u64 page_id, Vector const& stylesheets) override; + virtual void inspector_did_request_style_sheet_source(u64 page_id, Web::CSS::StyleSheetIdentifier const& identifier) override; + virtual void did_request_style_sheet_source(u64 page_id, Web::CSS::StyleSheetIdentifier const& identifier, String const& source) override; Optional view_for_page_id(u64, SourceLocation = SourceLocation::current()); diff --git a/Userland/Services/WebContent/ConnectionFromClient.cpp b/Userland/Services/WebContent/ConnectionFromClient.cpp index a567e52dbb5..cdba8d60f39 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.cpp +++ b/Userland/Services/WebContent/ConnectionFromClient.cpp @@ -1,6 +1,6 @@ /* * Copyright (c) 2020-2023, Andreas Kling - * Copyright (c) 2021-2023, Sam Atkins + * Copyright (c) 2021-2024, Sam Atkins * Copyright (c) 2021-2023, Linus Groh * Copyright (c) 2022, Tobias Christiansen * Copyright (c) 2022, Tim Flynn @@ -629,6 +629,28 @@ void ConnectionFromClient::get_hovered_node_id(u64 page_id) async_did_get_hovered_node_id(page_id, node_id); } +void ConnectionFromClient::list_style_sheets(u64 page_id) +{ + auto page = this->page(page_id); + if (!page.has_value()) + return; + + async_inspector_did_list_style_sheets(page_id, page->list_style_sheets()); +} + +void ConnectionFromClient::request_style_sheet_source(u64 page_id, Web::CSS::StyleSheetIdentifier const& identifier) +{ + auto page = this->page(page_id); + if (!page.has_value()) + return; + + if (auto* document = page->page().top_level_browsing_context().active_document()) { + auto stylesheet = document->get_style_sheet_source(identifier); + if (stylesheet.has_value()) + async_did_request_style_sheet_source(page_id, identifier, stylesheet.value()); + } +} + void ConnectionFromClient::set_dom_node_text(u64 page_id, i32 node_id, String const& text) { auto* dom_node = Web::DOM::Node::from_unique_id(node_id); diff --git a/Userland/Services/WebContent/ConnectionFromClient.h b/Userland/Services/WebContent/ConnectionFromClient.h index ac26864adf0..d18d9786f9b 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.h +++ b/Userland/Services/WebContent/ConnectionFromClient.h @@ -76,6 +76,9 @@ private: virtual void inspect_accessibility_tree(u64 page_id) override; virtual void get_hovered_node_id(u64 page_id) override; + virtual void list_style_sheets(u64 page_id) override; + virtual void request_style_sheet_source(u64 page_id, Web::CSS::StyleSheetIdentifier const& identifier) override; + virtual void set_dom_node_text(u64 page_id, i32 node_id, String const& text) override; virtual void set_dom_node_tag(u64 page_id, i32 node_id, String const& name) override; virtual void add_dom_node_attributes(u64 page_id, i32 node_id, Vector const& attributes) override; diff --git a/Userland/Services/WebContent/PageClient.cpp b/Userland/Services/WebContent/PageClient.cpp index eff6e086c52..3e69a9ee888 100644 --- a/Userland/Services/WebContent/PageClient.cpp +++ b/Userland/Services/WebContent/PageClient.cpp @@ -2,6 +2,7 @@ * Copyright (c) 2020-2023, Andreas Kling * Copyright (c) 2021-2022, Linus Groh * Copyright (c) 2023, Andrew Kaster + * Copyright (c) 2024, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -657,6 +658,11 @@ void PageClient::inspector_did_request_dom_tree_context_menu(i32 node_id, Web::C client().async_inspector_did_request_dom_tree_context_menu(m_id, node_id, page().css_to_device_point(position).to_type(), type, tag, attribute_index); } +void PageClient::inspector_did_request_style_sheet_source(Web::CSS::StyleSheetIdentifier const& identifier) +{ + client().async_inspector_did_request_style_sheet_source(m_id, identifier); +} + void PageClient::inspector_did_execute_console_script(String const& script) { client().async_inspector_did_execute_console_script(m_id, script); diff --git a/Userland/Services/WebContent/PageClient.h b/Userland/Services/WebContent/PageClient.h index b5799b350da..29efcb56826 100644 --- a/Userland/Services/WebContent/PageClient.h +++ b/Userland/Services/WebContent/PageClient.h @@ -169,6 +169,7 @@ private: virtual void inspector_did_add_dom_node_attributes(i32 node_id, JS::NonnullGCPtr attributes) override; virtual void inspector_did_replace_dom_node_attribute(i32 node_id, size_t attribute_index, JS::NonnullGCPtr replacement_attributes) override; virtual void inspector_did_request_dom_tree_context_menu(i32 node_id, Web::CSSPixelPoint position, String const& type, Optional const& tag, Optional const& attribute_index) override; + virtual void inspector_did_request_style_sheet_source(Web::CSS::StyleSheetIdentifier const& stylesheet_source) override; virtual void inspector_did_execute_console_script(String const& script) override; virtual void inspector_did_export_inspector_html(String const& script) override; diff --git a/Userland/Services/WebContent/WebContentClient.ipc b/Userland/Services/WebContent/WebContentClient.ipc index 45694704bb3..8e375ebe6a4 100644 --- a/Userland/Services/WebContent/WebContentClient.ipc +++ b/Userland/Services/WebContent/WebContentClient.ipc @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -54,6 +55,10 @@ endpoint WebContentClient did_finish_editing_dom_node(u64 page_id, Optional node_id) =| did_get_dom_node_html(u64 page_id, String html) =| + inspector_did_list_style_sheets(u64 page_id, Vector style_sheets) =| + inspector_did_request_style_sheet_source(u64 page_id, Web::CSS::StyleSheetIdentifier identifier) =| + did_request_style_sheet_source(u64 page_id, Web::CSS::StyleSheetIdentifier identifier, String source) =| + did_take_screenshot(u64 page_id, Gfx::ShareableBitmap screenshot) =| did_change_favicon(u64 page_id, Gfx::ShareableBitmap favicon) =| diff --git a/Userland/Services/WebContent/WebContentServer.ipc b/Userland/Services/WebContent/WebContentServer.ipc index 1273404f9f1..10e0f15292e 100644 --- a/Userland/Services/WebContent/WebContentServer.ipc +++ b/Userland/Services/WebContent/WebContentServer.ipc @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -45,6 +46,8 @@ endpoint WebContentServer get_hovered_node_id(u64 page_id) =| js_console_input(u64 page_id, ByteString js_source) =| js_console_request_messages(u64 page_id, i32 start_index) =| + list_style_sheets(u64 page_id) =| + request_style_sheet_source(u64 page_id, Web::CSS::StyleSheetIdentifier identifier) =| set_dom_node_text(u64 page_id, i32 node_id, String text) =| set_dom_node_tag(u64 page_id, i32 node_id, String name) =|