Truly independent web browser
LibWebView+UI/Qt: Make the blocked request badge configurable
Timothy Flynn0c3eb4a7e77e
diff --git a/Base/res/ladybird/about-pages/settings.html b/Base/res/ladybird/about-pages/settings.htmlindex e73e6817b44..5ac42390d5b 100644--- a/Base/res/ladybird/about-pages/settings.html+++ b/Base/res/ladybird/about-pages/settings.html@@ -647,13 +647,19 @@<div class="tab-panel" id="tab-blocking"><div class="card card-body">- <div class="inline-container">+ <div class="card-group inline-container"><label for="enable-content-blocking">Enable ad blocking<p class="description">Use filter lists to block ads, trackers, and other unwanted content.</p></label><input id="enable-content-blocking" type="checkbox" switch /></div>+ <div class="card-group inline-container hidden" id="show-content-blocker-count-setting">+ <label for="show-content-blocker-count">+ Show the number of blocked requests on the ad blocking icon+ </label>+ <input id="show-content-blocker-count" type="checkbox" switch />+ </div></div><template id="filter-list-row"><div class="card-group inline-container filter-list-row">diff --git a/Base/res/ladybird/about-pages/settings/blocking.js b/Base/res/ladybird/about-pages/settings/blocking.jsindex 69bd05cdfed..fa07592ebba 100644--- a/Base/res/ladybird/about-pages/settings/blocking.js+++ b/Base/res/ladybird/about-pages/settings/blocking.js@@ -12,8 +12,12 @@ const localListStatus = document.querySelector("#local-content-blocker-list-statconst customFilters = document.querySelector("#custom-content-blocker-filters");const saveCustomFilters = document.querySelector("#save-custom-content-blocker-filters");const customFiltersStatus = document.querySelector("#custom-content-blocker-filters-status");-const maximumCustomFilterSize = 4 * 1024 * 1024;const enableContentBlocking = document.querySelector("#enable-content-blocking");+const showContentBlockerCount = document.querySelector("#show-content-blocker-count");+const showContentBlockerCountSetting = document.querySelector("#show-content-blocker-count-setting");++const maximumCustomFilterSize = 4 * 1024 * 1024;+let updateTimer;let savedCustomFilters = "";@@ -71,12 +75,17 @@ function showEmptyState(container, message) {container.append(emptyState);}+function loadFeatures(features) {+ showContentBlockerCountSetting.classList.toggle("hidden", !features?.contentBlockingControls);+}+function loadSettings(settings) {const contentBlockers = settings.contentBlockers;if (!contentBlockers) {return;}enableContentBlocking.checked = contentBlockers.enabled;+ showContentBlockerCount.checked = contentBlockers.showBlockedCount ?? true;for (const container of [builtInLists, languageLists, customSubscriptions, localLists]) {container.replaceChildren();@@ -174,8 +183,14 @@ saveCustomFilters.addEventListener("click", () => {customFiltersStatus.innerText = "Saved";});+showContentBlockerCount.addEventListener("change", () => {+ ladybird.sendMessage("setShowContentBlockerCount", showContentBlockerCount.checked);+});+document.addEventListener("WebUIMessage", event => {- if (event.detail.name === "loadSettings") {+ if (event.detail.name === "loadFeatures") {+ loadFeatures(event.detail.data);+ } else if (event.detail.name === "loadSettings") {loadSettings(event.detail.data);} else if (event.detail.name === "contentBlockerResult") {const result = event.detail.data;diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.hindex 9d5b040f63e..688ec1de125 100644--- a/Libraries/LibWebView/Application.h+++ b/Libraries/LibWebView/Application.h@@ -128,6 +128,7 @@ public:virtual bool supports_system_menu_bar() const { return false; }virtual bool supports_vertical_tabs() const { return false; }+ virtual bool supports_content_blocking_controls() const { return false; }virtual bool supports_private_browsing_windows() const { return false; }virtual bool supports_client_side_window_decorations() const { return false; }// Returns true if the platform sends momentum scroll events after a touchpad flick.diff --git a/Libraries/LibWebView/Settings.cpp b/Libraries/LibWebView/Settings.cppindex 976ae42e1d3..b0a775dfd9b 100644--- a/Libraries/LibWebView/Settings.cpp+++ b/Libraries/LibWebView/Settings.cpp@@ -79,6 +79,7 @@ static constexpr auto BACKGROUND_NETWORKING_ENABLED_KEY = "enabled"sv;static constexpr auto BACKGROUND_NETWORKING_FEATURES_KEY = "features"sv;static constexpr auto CONTENT_BLOCKERS_KEY = "contentBlockers"sv;+static constexpr auto CONTENT_BLOCKER_SHOW_BLOCKED_COUNT_KEY = "showBlockedCount"sv;static constexpr auto CONTENT_BLOCKER_DISABLED_SITES_KEY = "disabledSites"sv;static constexpr auto CONTENT_BLOCKER_BUILT_IN_LISTS_KEY = "builtInLists"sv;static constexpr auto CONTENT_BLOCKER_CUSTOM_SUBSCRIPTIONS_KEY = "customSubscriptions"sv;@@ -356,6 +357,7 @@ Settings Settings::create(ByteString settings_path)if (auto content_blockers = settings_json.value().get_object(CONTENT_BLOCKERS_KEY); content_blockers.has_value()) {settings.m_content_blocker_enabled = content_blockers->get_bool(CONTENT_BLOCKER_ENABLED_KEY).value_or(false);+ settings.m_show_content_blocker_count = content_blockers->get_bool(CONTENT_BLOCKER_SHOW_BLOCKED_COUNT_KEY).value_or(true);if (auto sites = content_blockers->get_array(CONTENT_BLOCKER_DISABLED_SITES_KEY); sites.has_value()) {sites->for_each([&](JsonValue const& entry) {@@ -586,6 +588,7 @@ JsonValue Settings::serialize_json() constJsonObject content_blockers;content_blockers.set(CONTENT_BLOCKER_ENABLED_KEY, m_content_blocker_enabled);+ content_blockers.set(CONTENT_BLOCKER_SHOW_BLOCKED_COUNT_KEY, m_show_content_blocker_count);content_blockers.set(CONTENT_BLOCKER_DISABLED_SITES_KEY, move(disabled_sites));content_blockers.set(CONTENT_BLOCKER_BUILT_IN_LISTS_KEY, move(built_in_content_blocker_lists));content_blockers.set(CONTENT_BLOCKER_CUSTOM_SUBSCRIPTIONS_KEY, move(custom_content_blocker_subscriptions));@@ -1108,6 +1111,16 @@ void Settings::set_content_blocker_enabled(bool enabled)observer.content_blocker_settings_changed();}+void Settings::set_show_content_blocker_count(bool show)+{+ if (m_show_content_blocker_count == show)+ return;+ m_show_content_blocker_count = show;+ persist_settings();+ for (auto& observer : m_observers)+ observer.content_blocker_count_visibility_changed();+}+void Settings::set_content_blocker_list_enabled(StringView identifier, bool enabled){for (auto& list : m_content_blocker_lists) {diff --git a/Libraries/LibWebView/Settings.h b/Libraries/LibWebView/Settings.hindex 3e48664f4c5..cc134540ff8 100644--- a/Libraries/LibWebView/Settings.h+++ b/Libraries/LibWebView/Settings.h@@ -126,6 +126,7 @@ public:virtual void geolocation_settings_changed() { }virtual void background_networking_settings_changed() { }virtual void content_blocker_settings_changed() { }+ virtual void content_blocker_count_visibility_changed() { }};class WEBVIEW_API Settings {@@ -204,6 +205,9 @@ public:bool content_blocker_enabled() const { return m_content_blocker_enabled; }void set_content_blocker_enabled(bool);+ bool show_content_blocker_count() const { return m_show_content_blocker_count; }+ void set_show_content_blocker_count(bool);+HashTable<String> const& content_blocker_disabled_sites() const { return m_content_blocker_disabled_sites; }bool content_blocker_enabled_for_site(StringView) const;ErrorOr<void> set_content_blocker_enabled_for_site(StringView, bool enabled);@@ -260,6 +264,7 @@ private:bool m_background_networking_enabled { true };bool m_filter_list_updates_enabled { false };bool m_content_blocker_enabled { false };+ bool m_show_content_blocker_count { true };HashTable<String> m_content_blocker_disabled_sites;Vector<ContentBlockerList> m_content_blocker_lists;String m_custom_content_blocker_filters;diff --git a/Libraries/LibWebView/WebUI/SettingsUI.cpp b/Libraries/LibWebView/WebUI/SettingsUI.cppindex 80ff0cf01ed..c6a9ba86591 100644--- a/Libraries/LibWebView/WebUI/SettingsUI.cpp+++ b/Libraries/LibWebView/WebUI/SettingsUI.cpp@@ -153,6 +153,9 @@ void SettingsUI::register_interfaces()register_interface("setContentBlockerEnabled"sv, [this](auto const& data) {set_content_blocker_enabled(data);});+ register_interface("setShowContentBlockerCount"sv, [this](auto const& data) {+ set_show_content_blocker_count(data);+ });register_interface("addCustomContentBlockerSubscription"sv, [this](auto const& data) {add_custom_content_blocker_subscription(data);});@@ -180,6 +183,7 @@ void SettingsUI::load_features()features.set("primaryPaste"_string, application.supports_clipboard_type(Application::ClipboardType::Selection));features.set("verticalTabs"_string, application.supports_vertical_tabs());features.set("geolocation"_string, Core::GeolocationProvider::is_available());+ features.set("contentBlockingControls"_string, application.supports_content_blocking_controls());async_send_message("loadFeatures"sv, move(features));}@@ -634,6 +638,15 @@ void SettingsUI::set_content_blocker_enabled(JsonValue const& enabled)load_current_settings();}+void SettingsUI::set_show_content_blocker_count(JsonValue const& show)+{+ if (!show.is_bool() || !Application::the().supports_content_blocking_controls())+ return;++ Application::settings().set_show_content_blocker_count(show.as_bool());+ load_current_settings();+}+void SettingsUI::set_content_blocker_list_enabled(JsonValue const& value){if (!value.is_object())diff --git a/Libraries/LibWebView/WebUI/SettingsUI.h b/Libraries/LibWebView/WebUI/SettingsUI.hindex 7701a0d7078..897c631a6e1 100644--- a/Libraries/LibWebView/WebUI/SettingsUI.h+++ b/Libraries/LibWebView/WebUI/SettingsUI.h@@ -50,6 +50,7 @@ private:void set_filter_list_updates_enabled(JsonValue const&);void update_content_blocker_lists(JsonValue const&);void set_content_blocker_enabled(JsonValue const&);+ void set_show_content_blocker_count(JsonValue const&);void set_content_blocker_list_enabled(JsonValue const&);void add_custom_content_blocker_subscription(JsonValue const&);void remove_content_blocker_list(JsonValue const&);diff --git a/Tests/LibWebView/test-webdriver-content-blockers.py b/Tests/LibWebView/test-webdriver-content-blockers.pyindex f9664c2f89d..cba1b7330d9 100755--- a/Tests/LibWebView/test-webdriver-content-blockers.py+++ b/Tests/LibWebView/test-webdriver-content-blockers.py@@ -141,6 +141,32 @@ with tempfile.TemporaryDirectory() as directory:assert command("execute/sync", {"script": "return location.href", "args": []}) == "about:about"command("url", {"url": "about:blocking"})state = settings()+ assert state["contentBlockers"]["showBlockedCount"] is True+ features = command(+ "execute/async",+ {+ "script": """+ const done = arguments[arguments.length - 1];+ document.addEventListener('WebUIMessage', function receive(event) {+ if (event.detail.name !== 'loadFeatures') return;+ document.removeEventListener('WebUIMessage', receive);+ done(event.detail.data);+ });+ ladybird.sendMessage('loadFeatures');+ """,+ "args": [],+ },+ )+ display = command(+ "execute/sync",+ {+ "script": "return getComputedStyle(document.querySelector('#show-content-blocker-count-setting')).display",+ "args": [],+ },+ )+ assert (display != "none") == features["contentBlockingControls"]+ command("execute/sync", {"script": "ladybird.sendMessage('setShowContentBlockerCount', false)", "args": []})+ assert settings()["contentBlockers"]["showBlockedCount"] == (not features["contentBlockingControls"])assert command("execute/sync",{diff --git a/UI/Qt/Application.h b/UI/Qt/Application.hindex 898a8eb899f..2161d0c1fe0 100644--- a/UI/Qt/Application.h+++ b/UI/Qt/Application.h@@ -145,6 +145,7 @@ private:virtual bool supports_system_menu_bar() const override;virtual bool supports_vertical_tabs() const override { return true; }virtual bool supports_private_browsing_windows() const override { return true; }+ virtual bool supports_content_blocking_controls() const override { return true; }virtual bool supports_client_side_window_decorations() const override{#if defined(AK_OS_MACOS)diff --git a/UI/Qt/ContentBlockingButton.cpp b/UI/Qt/ContentBlockingButton.cppindex 64ca47b368a..af0967c5f20 100644--- a/UI/Qt/ContentBlockingButton.cpp+++ b/UI/Qt/ContentBlockingButton.cpp@@ -31,9 +31,17 @@ void ContentBlockingButton::set_snapshot(WebView::ContentBlockingSnapshot const&update();}+void ContentBlockingButton::set_show_count_badge(bool show)+{+ if (m_show_count_badge == show)+ return;+ m_show_count_badge = show;+ update();+}+QString ContentBlockingButton::badge_text() const{- if (!m_snapshot.enabled() || m_snapshot.blocked_request_count == 0)+ if (!m_show_count_badge || !m_snapshot.enabled() || m_snapshot.blocked_request_count == 0)return {};return m_snapshot.blocked_request_count > 99 ? "99+" : qformatted("{}", m_snapshot.blocked_request_count);}diff --git a/UI/Qt/ContentBlockingButton.h b/UI/Qt/ContentBlockingButton.hindex 9560d7bb686..a64043fb022 100644--- a/UI/Qt/ContentBlockingButton.h+++ b/UI/Qt/ContentBlockingButton.h@@ -21,6 +21,8 @@ public:explicit ContentBlockingButton(QWidget* parent);void set_snapshot(WebView::ContentBlockingSnapshot const&);+ void set_show_count_badge(bool);+QString badge_text() const;private:@@ -29,6 +31,7 @@ private:QIcon m_logo { ":/Icons/ladybird.png" };WebView::ContentBlockingSnapshot m_snapshot;+ bool m_show_count_badge { true };};}diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cppindex f4411c02d20..3cec7a3831e 100644--- a/UI/Qt/Tab.cpp+++ b/UI/Qt/Tab.cpp@@ -703,6 +703,7 @@ Tab::Tab(BrowserWindow* window, Optional<WebView::CanonicalTraversable&> traversQObject::connect(m_content_blocking_button, &QToolButton::clicked, this, [this] { show_content_blocking_popover(); });view().on_content_blocking_change = [this](auto const& snapshot) { update_content_blocking_controls(snapshot); };update_content_blocking_controls(view().content_blocking_snapshot());+ content_blocker_count_visibility_changed();m_right_toggle_vertical_tabs_expanded_button = create_toolbar_button(*m_toolbar, *m_toggle_vertical_tabs_expanded_action);toolbar_layout->addWidget(m_right_toggle_vertical_tabs_expanded_button, 0, Qt::AlignVCenter);@@ -1346,6 +1347,11 @@ void Tab::config_variable_changed(WebView::ConfigVariableID variable)update_tab_title();}+void Tab::content_blocker_count_visibility_changed()+{+ m_content_blocking_button->set_show_count_badge(WebView::Application::settings().show_content_blocker_count());+}+void Tab::set_loading(bool is_loading){if (m_is_loading == is_loading)diff --git a/UI/Qt/Tab.h b/UI/Qt/Tab.hindex 61ae5a7203b..71004eb151c 100644--- a/UI/Qt/Tab.h+++ b/UI/Qt/Tab.h@@ -135,6 +135,7 @@ private:virtual void tab_settings_changed() override;virtual void config_variable_changed(WebView::ConfigVariableID) override;+ virtual void content_blocker_count_visibility_changed() override;void recreate_toolbar_icons();void update_vertical_tabs_toolbar_button_placement();