Skip to content

Truly independent web browser

LibWeb+LibWebView: Count blocked requests by page load

Timothy Flynn93305b23e18f
diff --git a/Libraries/LibWeb/Loader/ResourceLoader.cpp b/Libraries/LibWeb/Loader/ResourceLoader.cpp
index 3fd428bc776..80c9225103a 100644
--- a/Libraries/LibWeb/Loader/ResourceLoader.cpp
+++ b/Libraries/LibWeb/Loader/ResourceLoader.cpp
@@ -195,6 +195,8 @@ static bool should_block_request(LoadRequest const& request)
auto source_url = request.source_url().value_or(url);
if (ContentBlocker::the().is_filtered(url, source_url, request.destination(), request.initiator_type(), request.request_mode())) {
log_filtered_request(request);
+ if (page)
+ page->client().page_did_block_resource();
return true;
}
}
diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.h
index a33bb25daa7..09c477c0464 100644
--- a/Libraries/LibWeb/Page/Page.h
+++ b/Libraries/LibWeb/Page/Page.h
@@ -862,6 +862,7 @@ public:
virtual Gamepad::ReceivedRumbleEffects virtual_gamepad_received_rumble_effects([[maybe_unused]] Gamepad::GamepadHandle handle) { return {}; }
virtual void pump_gamepad_events() { }
+ virtual void page_did_block_resource() { }
virtual void page_did_start_network_request([[maybe_unused]] u64 request_id, [[maybe_unused]] URL::URL const& url, [[maybe_unused]] ByteString const& method, [[maybe_unused]] Vector<HTTP::Header> const& request_headers, [[maybe_unused]] ReadonlyBytes request_body, [[maybe_unused]] Optional<String> initiator_type, [[maybe_unused]] String const& referrer_policy, [[maybe_unused]] bool is_navigation_request, [[maybe_unused]] Fetch::Infrastructure::Request::Priority priority) { }
virtual void page_did_receive_network_response_headers([[maybe_unused]] u64 request_id, [[maybe_unused]] u32 status_code, [[maybe_unused]] Optional<String> reason_phrase, [[maybe_unused]] Vector<HTTP::Header> const& response_headers, [[maybe_unused]] Requests::CacheState cache_state) { }
virtual void page_did_receive_network_response_body([[maybe_unused]] u64 request_id, [[maybe_unused]] ReadonlyBytes data) { }
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp
index e0a3223a90b..3d4bde1aa71 100644
--- a/Libraries/LibWebView/Application.cpp
+++ b/Libraries/LibWebView/Application.cpp
@@ -2810,6 +2810,10 @@ void Application::apply_content_blocker_settings()
client.async_set_content_blockers(*m_content_blocker_list_buffer);
return IterationDecision::Continue;
});
+ ViewImplementation::for_each_view([](ViewImplementation& view) {
+ view.notify_content_blocking_changed();
+ return IterationDecision::Continue;
+ });
}
bool Application::content_blocker_list_update_in_progress() const
diff --git a/Libraries/LibWebView/CanonicalNavigable.cpp b/Libraries/LibWebView/CanonicalNavigable.cpp
index cdc25f5909a..84a15fb8028 100644
--- a/Libraries/LibWebView/CanonicalNavigable.cpp
+++ b/Libraries/LibWebView/CanonicalNavigable.cpp
@@ -1010,6 +1010,10 @@ void CanonicalNavigable::update_hosted_state(Web::HTML::HostedNavigableState sta
state.container = m_hosted_state->container;
set_hosted_state(move(state));
send_replicated_state();
+ if (is_top_level_traversable()) {
+ if (auto view = top_level_traversable().view(); view.has_value())
+ view->notify_content_blocking_changed();
+ }
}
Optional<Web::HTML::ReplicatedNavigableState> CanonicalNavigable::replicated_state() const
diff --git a/Libraries/LibWebView/CanonicalTraversable.cpp b/Libraries/LibWebView/CanonicalTraversable.cpp
index d26bce866b6..dd5169565fc 100644
--- a/Libraries/LibWebView/CanonicalTraversable.cpp
+++ b/Libraries/LibWebView/CanonicalTraversable.cpp
@@ -1313,9 +1313,16 @@ bool CanonicalTraversable::select_changing_navigable_history_step_job_endpoint(H
if (!endpoint || !endpoint->is_open())
return false;
- if (navigable->is_top_level_traversable() && operation.is_browser_traversal()) {
- if (auto callback = move(operation.browser_traversal().on_ready))
- callback();
+ if (navigable->is_top_level_traversable()) {
+ if (!operation.parameters.has<Web::ReloadHistoryOperationParameters>()
+ && job.target_entry->document_state->document.ptr() != &navigable->active_document()) {
+ if (auto view = this->view(); view.has_value())
+ view->reset_blocked_request_count();
+ }
+ if (operation.is_browser_traversal()) {
+ if (auto callback = move(operation.browser_traversal().on_ready))
+ callback();
+ }
}
add_history_operation_completion_endpoint(operation, *endpoint);
diff --git a/Libraries/LibWebView/ContentBlocking.h b/Libraries/LibWebView/ContentBlocking.h
new file mode 100644
index 00000000000..39398b2deb2
--- /dev/null
+++ b/Libraries/LibWebView/ContentBlocking.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Optional.h>
+#include <AK/String.h>
+#include <AK/Types.h>
+
+namespace WebView {
+
+enum class ContentBlockingState : u8 {
+ Unavailable,
+ Disabled,
+ SiteDisabled,
+ Enabled,
+ NoRules,
+};
+
+struct ContentBlockingSnapshot {
+ Optional<String> site;
+ ContentBlockingState state { ContentBlockingState::Unavailable };
+ u64 blocked_request_count { 0 };
+
+ bool enabled() const { return state == ContentBlockingState::Enabled || state == ContentBlockingState::NoRules; }
+ bool can_toggle() const { return site.has_value() && (enabled() || state == ContentBlockingState::SiteDisabled); }
+};
+
+}
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index 727b1af3cb5..570eb71b594 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -24,6 +24,7 @@
#include <LibWebCommon/CSS/SystemColor.h>
#include <LibWebCommon/Geolocation/GeolocationPositionErrorCode.h>
#include <LibWebCommon/Infra/Strings.h>
+#include <LibWebCommon/Loader/ContentBlocking.h>
#include <LibWebCommon/WebDriver/Error.h>
#include <LibWebCommon/WebView/SiteIsolation.h>
#include <LibWebView/Application.h>
@@ -1943,6 +1944,51 @@ void ViewImplementation::request_style_sheet_source(Web::CSS::StyleSheetIdentifi
page().async_request_style_sheet_source(identifier);
}
+void ViewImplementation::did_block_resource(Badge<WebContentPage>)
+{
+ if (m_blocked_request_count != NumericLimits<u64>::max())
+ ++m_blocked_request_count;
+ notify_content_blocking_changed();
+}
+
+void ViewImplementation::reset_blocked_request_count()
+{
+ m_blocked_request_count = 0;
+ notify_content_blocking_changed();
+}
+
+void ViewImplementation::notify_content_blocking_changed()
+{
+ if (on_content_blocking_change)
+ on_content_blocking_change(content_blocking_snapshot());
+}
+
+ContentBlockingSnapshot ViewImplementation::content_blocking_snapshot() const
+{
+ ContentBlockingSnapshot snapshot;
+ snapshot.blocked_request_count = m_blocked_request_count;
+
+ if (m_crash_state.has_value() || !traversable().active_document().host())
+ return snapshot;
+
+ auto entry = traversable().active_session_history_entry();
+ if (!entry)
+ return snapshot;
+
+ snapshot.site = Web::content_blocking_site(entry->url);
+ if (!snapshot.site.has_value())
+ return snapshot;
+
+ if (!Application::the().content_blocking_enabled())
+ snapshot.state = ContentBlockingState::Disabled;
+ else if (!Application::settings().content_blocker_enabled_for_site(*snapshot.site))
+ snapshot.state = ContentBlockingState::SiteDisabled;
+ else
+ snapshot.state = Application::the().has_content_blocking_rules() ? ContentBlockingState::Enabled : ContentBlockingState::NoRules;
+
+ return snapshot;
+}
+
void ViewImplementation::debug_request(ByteString const& request, ByteString const& argument)
{
if (request == "dump-session-history"sv)
@@ -2473,6 +2519,7 @@ void ViewImplementation::cancel_all_native_geolocation_requests()
void ViewImplementation::did_start_navigation()
{
+ reset_blocked_request_count();
set_loading_state(true);
dump_session_history("did-start-navigation"sv);
}
@@ -3335,6 +3382,7 @@ void ViewImplementation::set_crash_state(Optional<CrashState> state)
{
auto active = state.has_value();
m_crash_state = move(state);
+ notify_content_blocking_changed();
if (on_crash_overlay_state_change)
on_crash_overlay_state_change(active);
}
diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h
index cf0444e8d76..b07d64e7f0c 100644
--- a/Libraries/LibWebView/ViewImplementation.h
+++ b/Libraries/LibWebView/ViewImplementation.h
@@ -69,6 +69,7 @@
#include <LibWebView/BookmarkStore.h>
#include <LibWebView/BrowsingSession.h>
#include <LibWebView/CanonicalTraversable.h>
+#include <LibWebView/ContentBlocking.h>
#include <LibWebView/ExternalURLHandler.h>
#include <LibWebView/Forward.h>
#include <LibWebView/HistoryVisitTransition.h>
@@ -326,6 +327,11 @@ public:
void list_style_sheets();
void request_style_sheet_source(Web::CSS::StyleSheetIdentifier const&);
+ void did_block_resource(Badge<WebContentPage>);
+ void reset_blocked_request_count();
+ void notify_content_blocking_changed();
+ ContentBlockingSnapshot content_blocking_snapshot() const;
+
void debug_request(ByteString const& request, ByteString const& argument = {});
void run_javascript(String const&);
@@ -449,6 +455,7 @@ public:
Function<void()> on_stop_tooltip_override;
Function<void(ByteString const&)> on_enter_tooltip_area;
Function<void()> on_leave_tooltip_area;
+ Function<void(ContentBlockingSnapshot const&)> on_content_blocking_change;
Function<void(Utf16String const& message)> on_request_alert;
Function<void(Utf16String const& message)> on_request_confirm;
Function<void(Utf16String const& message, Utf16String const& default_)> on_request_prompt;
@@ -948,6 +955,8 @@ protected:
HashMap<u64, NavigationListener> m_navigation_listeners;
u64 m_next_navigation_listener_id { 1 };
+ u64 m_blocked_request_count { 0 };
+
enum class NodePickerRequestType : u8 {
Hovered,
Picked,
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index 13f3a33e970..cc68b4e6590 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -1158,6 +1158,11 @@ void WebContentPage::did_output_js_console_message(ConsoleOutput console_output)
}
}
+void WebContentPage::did_block_resource()
+{
+ view().did_block_resource({});
+}
+
void WebContentPage::did_start_network_request(u64 request_id, URL::URL url, ByteString method, Vector<HTTP::Header> request_headers, ByteBuffer request_body, Optional<String> initiator_type, String referrer_policy, bool is_navigation_request, Web::Fetch::Infrastructure::RequestPriority priority)
{
if (displays_tab()) {
diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.h
index be8e36f6331..efe0c1a6b6c 100644
--- a/Libraries/LibWebView/WebContentPage.h
+++ b/Libraries/LibWebView/WebContentPage.h
@@ -216,6 +216,7 @@ private:
virtual void did_cut_selected_text(u64 request_id, ByteString selection) override;
virtual void did_execute_js_console_input(JsonValue result) override;
virtual void did_output_js_console_message(ConsoleOutput console_output) override;
+ virtual void did_block_resource() override;
virtual void did_start_network_request(u64 request_id, URL::URL url, ByteString method, Vector<HTTP::Header> request_headers, ByteBuffer request_body, Optional<String> initiator_type, String referrer_policy, bool is_navigation_request, Web::Fetch::Infrastructure::RequestPriority priority) override;
virtual void did_receive_network_response_body(u64 request_id, ByteBuffer data) override;
virtual void did_finish_network_request(u64 request_id, u64 body_size, Requests::RequestTimingInfo timing_info, Optional<Requests::NetworkError> network_error) override;
diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cpp
index 7b10084942a..88cc291ee62 100644
--- a/Services/WebContent/PageClient.cpp
+++ b/Services/WebContent/PageClient.cpp
@@ -2105,6 +2105,11 @@ void PageClient::received_message_from_web_ui(Utf16String const& name, JS::Value
m_web_ui->received_message_from_web_ui(name, data);
}
+void PageClient::page_did_block_resource()
+{
+ client().async_did_block_resource(id());
+}
+
void PageClient::page_did_start_network_request(u64 request_id, URL::URL const& url, ByteString const& method, Vector<HTTP::Header> const& request_headers, ReadonlyBytes request_body, Optional<String> initiator_type, String const& referrer_policy, bool is_navigation_request, Web::Fetch::Infrastructure::Request::Priority priority)
{
client().async_did_start_network_request(m_id, request_id, url, method, request_headers, request_body, move(initiator_type), referrer_policy, is_navigation_request, priority);
diff --git a/Services/WebContent/PageClient.h b/Services/WebContent/PageClient.h
index 93ee2580807..1f4cd822017 100644
--- a/Services/WebContent/PageClient.h
+++ b/Services/WebContent/PageClient.h
@@ -367,6 +367,7 @@ private:
virtual void page_did_change_accessibility_tree() override;
virtual void page_did_take_screenshot(Gfx::ShareableBitmap const& screenshot) override;
virtual void received_message_from_web_ui(Utf16String const& name, JS::Value data) override;
+ virtual void page_did_block_resource() override;
virtual void page_did_start_network_request(u64 request_id, URL::URL const&, ByteString const&, Vector<HTTP::Header> const&, ReadonlyBytes, Optional<String>, String const& referrer_policy, bool is_navigation_request, Web::Fetch::Infrastructure::Request::Priority) override;
virtual void page_did_receive_network_response_headers(u64 request_id, u32 status_code, Optional<String>, Vector<HTTP::Header> const&, Requests::CacheState) override;
virtual void page_did_receive_network_response_body(u64 request_id, ReadonlyBytes) override;
diff --git a/Services/WebContent/WebContentClient.ipc b/Services/WebContent/WebContentClient.ipc
index 4012a630f3b..6a13129d853 100644
--- a/Services/WebContent/WebContentClient.ipc
+++ b/Services/WebContent/WebContentClient.ipc
@@ -276,6 +276,7 @@ endpoint WebContentClient routed by Web::PageId page_id
did_execute_js_console_input(Web::PageId page_id, JsonValue result) =|
did_output_js_console_message(Web::PageId page_id, WebView::ConsoleOutput console_output) =|
+ did_block_resource(Web::PageId page_id) =|
did_start_network_request(Web::PageId page_id, u64 request_id, URL::URL url, ByteString method, Vector<HTTP::Header> request_headers, ByteBuffer request_body, Optional<String> initiator_type, String referrer_policy, bool is_navigation_request, Web::Fetch::Infrastructure::RequestPriority priority) =|
did_receive_network_response_headers(Web::PageId page_id, u64 request_id, u32 status_code, Optional<String> reason_phrase, Vector<HTTP::Header> response_headers, Requests::CacheState cache_state) =|
did_receive_network_response_body(Web::PageId page_id, u64 request_id, ByteBuffer data) =|