From: uazo Date: Thu, 29 Sep 2022 11:27:35 +0000 Subject: Eyeo Adblock for Bromite Change the normal behaviour of Eyeo Chromium SDK to suit Bromite logic --- .../android/java/res/xml/main_preferences.xml | 19 +- chrome/browser/BUILD.gn | 2 - .../adblock/adblock_content_browser_client.cc | 26 +- .../adblock_private/adblock_private_api.cc | 74 +- .../api/adblock_private/adblock_private_api.h | 49 + .../eyeo_filtering_private_api.cc | 25 +- ...hrome_browser_main_extra_parts_profiles.cc | 2 - chrome/browser/resources/settings/BUILD.gn | 2 + .../settings/adblock_page/adblock_page.html | 207 + .../settings/adblock_page/adblock_page.ts | 286 ++ .../settings/basic_page/basic_page.html | 7 + .../settings/basic_page/basic_page.ts | 1 + .../resources/settings/page_visibility.ts | 1 + chrome/browser/resources/settings/route.ts | 4 + chrome/browser/resources/settings/router.ts | 1 + chrome/browser/resources/settings/settings.ts | 1 + .../settings/settings_menu/settings_menu.html | 5 + chrome/browser/ui/tab_helpers.cc | 6 +- .../adblock_internals_page_handler_impl.cc | 20 +- .../extensions/api/_permission_features.json | 8 - .../common/extensions/api/adblock_private.idl | 8 + chrome/test/BUILD.gn | 5 - components/adblock/android/BUILD.gn | 3 +- components/adblock/android/adblock_jni.cc | 53 +- .../adblock/android/adblock_strings.grd | 54 +- .../java/res/layout/adblock_custom_item.xml | 14 +- .../layout/adblock_filter_lists_list_item.xml | 15 + ...ences.xml => eyeo_adblock_preferences.xml} | 39 +- .../components/adblock/AdblockController.java | 51 +- .../AdblockAllowedDomainsFragment.java | 7 +- .../AdblockCustomFilterListsFragment.java | 32 +- .../AdblockCustomFiltersFragment.java | 7 +- .../settings/AdblockCustomItemFragment.java | 20 +- .../settings/AdblockFilterListsAdapter.java | 6 + .../settings/AdblockSettingsFragment.java | 45 +- components/adblock/content/browser/BUILD.gn | 18 +- .../adblock_controller_factory_base.cc | 5 +- .../browser/adblock_webcontents_observer.cc | 25 +- .../browser/adblock_webcontents_observer.h | 5 +- .../content_security_policy_injector_impl.cc | 1 + .../content/browser/element_hider_impl.cc | 4 +- .../subscription_service_factory_base.cc | 11 +- .../subscription_service_factory_base.h | 1 + components/adblock/core/BUILD.gn | 39 - .../activeping_telemetry_topic_provider.cc | 285 -- .../activeping_telemetry_topic_provider.h | 87 - .../adblock/core/adblock_controller_impl.cc | 10 +- components/adblock/core/adblock_switches.cc | 1 - components/adblock/core/adblock_switches.h | 1 - .../adblock/core/adblock_telemetry_service.cc | 258 -- .../adblock/core/adblock_telemetry_service.h | 120 - components/adblock/core/common/BUILD.gn | 8 - .../adblock/core/common/adblock_constants.cc | 2 - .../adblock/core/common/adblock_constants.h | 1 - .../adblock/core/common/adblock_prefs.cc | 47 +- .../adblock/core/common/adblock_prefs.h | 1 + .../adblock/core/common/adblock_utils.cc | 23 - .../configuration/filtering_configuration.h | 3 + .../persistent_filtering_configuration.cc | 10 + .../persistent_filtering_configuration.h | 3 + .../core/converter/flatbuffer_converter.cc | 2 +- .../adblock/core/converter/parser/metadata.cc | 10 +- .../core/converter/parser/test/test_rules.txt | 21 + .../core/converter/parser/url_filter.cc | 10 +- .../converter/parser/url_filter_options.cc | 21 +- .../serializer/flatbuffer_serializer.cc | 75 +- .../adblock/core/sitekey_storage_impl.cc | 7 + .../core/subscription/conversion_executors.h | 1 + .../filtering_configuration_maintainer.h | 4 + ...filtering_configuration_maintainer_impl.cc | 31 +- .../filtering_configuration_maintainer_impl.h | 4 +- .../ongoing_subscription_request_impl.cc | 33 +- .../preloaded_subscription_provider_impl.cc | 4 +- .../adblock/core/subscription/subscription.cc | 19 + .../adblock/core/subscription/subscription.h | 3 + .../subscription_collection_impl.cc | 1 + .../core/subscription/subscription_config.cc | 20 +- .../core/subscription/subscription_config.h | 6 +- .../subscription_downloader_impl.cc | 21 +- .../subscription_persistent_metadata.h | 1 + .../subscription_persistent_metadata_impl.cc | 7 + .../subscription_persistent_metadata_impl.h | 1 + .../subscription_persistent_storage_impl.cc | 11 +- .../core/subscription/subscription_service.h | 9 + .../subscription/subscription_service_impl.cc | 78 +- .../subscription/subscription_service_impl.h | 9 + .../subscription/subscription_updater_impl.cc | 8 +- .../subscription_validator_impl.cc | 4 +- .../browser/bromite_content_settings/ads.inc | 3 + components/resources/BUILD.gn | 1 - components/resources/adblock_resources.grdp | 3 - components/resources/adblocking/.gitignore | 2 +- components/resources/adblocking/BUILD.gn | 30 +- .../adblocking/elemhide_for_selector.jst | 2 +- .../resources/adblocking/elemhideemu.jst | 2 + .../snippets/dist/isolated-first-xpath3.jst | 62 + .../dist/isolated-first-xpath3.source.jst | 3696 +++++++++++++++++ .../snippets/dist/isolated-first.jst | 65 + .../snippets/dist/isolated-first.source.jst | 3624 ++++++++++++++++ .../blink/renderer/core/css/style_engine.cc | 8 + .../blink/renderer/core/css/style_engine.h | 1 + .../renderer/core/exported/web_document.cc | 15 +- .../definitions/adblock_private.d.ts | 14 + 103 files changed, 8857 insertions(+), 1166 deletions(-) create mode 100644 chrome/browser/resources/settings/adblock_page/adblock_page.html create mode 100644 chrome/browser/resources/settings/adblock_page/adblock_page.ts rename components/adblock/android/java/res/xml/{adblock_preferences.xml => eyeo_adblock_preferences.xml} (56%) delete mode 100644 components/adblock/core/activeping_telemetry_topic_provider.cc delete mode 100644 components/adblock/core/activeping_telemetry_topic_provider.h delete mode 100644 components/adblock/core/adblock_telemetry_service.cc delete mode 100644 components/adblock/core/adblock_telemetry_service.h create mode 100644 components/adblock/core/converter/parser/test/test_rules.txt create mode 100644 components/content_settings/core/browser/bromite_content_settings/ads.inc create mode 100644 components/resources/adblocking/snippets/dist/isolated-first-xpath3.jst create mode 100644 components/resources/adblocking/snippets/dist/isolated-first-xpath3.source.jst create mode 100755 components/resources/adblocking/snippets/dist/isolated-first.jst create mode 100755 components/resources/adblocking/snippets/dist/isolated-first.source.jst diff --git a/chrome/android/java/res/xml/main_preferences.xml b/chrome/android/java/res/xml/main_preferences.xml --- a/chrome/android/java/res/xml/main_preferences.xml +++ b/chrome/android/java/res/xml/main_preferences.xml @@ -1,13 +1,4 @@ - - @@ -75,6 +66,11 @@ Use of this source code is governed by the GPLv3 that can be found in the compon android:key="safety_check" android:order="12" android:title="@string/prefs_safety_check"/> + - GetProcess()->GetBrowserContext()); if (profile) { + content::RenderFrameHost* embedder = frame->GetOutermostMainFrameOrEmbedder(); + const auto& embedder_url = embedder->GetLastCommittedURL(); + HostContentSettingsMap* settings_map = HostContentSettingsMapFactory::GetForProfile(profile); + if (settings_map && settings_map->GetContentSetting(embedder_url, GURL(), ContentSettingsType::ADS) + == CONTENT_SETTING_ALLOW) { + return false; + } // Filtering may be needed if there's at least one enabled // FilteringConfiguration. return base::ranges::any_of( @@ -102,23 +111,6 @@ class AdblockContextData : public base::SupportsUserData::Data { adblock::SitekeyStorageFactory::GetForBrowserContext(browser_context), adblock::ContentSecurityPolicyInjectorFactory::GetForBrowserContext( browser_context)}; -#ifdef EYEO_INTERCEPT_DEBUG_URL - if (use_test_loader) { - auto proxy = std::make_unique( - std::move(config), - content::GlobalRenderFrameHostId(render_process_id, - frame->GetRoutingID()), - std::move(receiver), std::move(target_factory), - embedder_support::GetUserAgent(), - base::BindOnce(&AdblockContextData::RemoveProxy, - self->weak_factory_.GetWeakPtr()), - adblock::SubscriptionServiceFactory::GetForBrowserContext( - Profile::FromBrowserContext( - frame->GetProcess()->GetBrowserContext()))); - self->proxies_.emplace(std::move(proxy)); - return; - } -#endif auto proxy = std::make_unique( std::move(config), content::GlobalRenderFrameHostId(render_process_id, diff --git a/chrome/browser/extensions/api/adblock_private/adblock_private_api.cc b/chrome/browser/extensions/api/adblock_private/adblock_private_api.cc --- a/chrome/browser/extensions/api/adblock_private/adblock_private_api.cc +++ b/chrome/browser/extensions/api/adblock_private/adblock_private_api.cc @@ -18,7 +18,8 @@ #include "base/containers/flat_map.h" #include "base/logging.h" #include "base/no_destructor.h" -#include "base/time/time_to_iso8601.h" +#include "base/i18n/time_formatting.h" +#include "base/strings/utf_string_conversions.h" #include "base/values.h" #include "chrome/browser/adblock/resource_classification_runner_factory.h" #include "chrome/browser/adblock/session_stats_factory.h" @@ -77,26 +78,12 @@ std::vector CopySessionsStats( return result; } -std::string SubscriptionInstallationStateToString( - adblock::Subscription::InstallationState state) { - using State = adblock::Subscription::InstallationState; - switch (state) { - case State::Installed: - return "Installed"; - case State::Installing: - return "Installing"; - case State::Preloaded: - return "Preloaded"; - case State::Unknown: - return "Unknown"; - } - NOTREACHED(); - return ""; -} - std::vector CopySubscriptions( + raw_ptr subscription_service, const std::vector> current_subscriptions) { + raw_ptr metadata = + subscription_service->GetMetadata(); std::vector result; for (auto& sub : current_subscriptions) { api::adblock_private::Subscription js_sub; @@ -104,9 +91,13 @@ std::vector CopySubscriptions( js_sub.title = sub->GetTitle(); js_sub.current_version = sub->GetCurrentVersion(); js_sub.installation_state = - SubscriptionInstallationStateToString(sub->GetInstallationState()); + adblock::Subscription::SubscriptionInstallationStateToString(sub->GetInstallationState()); js_sub.last_installation_time = - base::TimeToISO8601(sub->GetInstallationTime()); + base::UTF16ToUTF8(base::TimeFormatFriendlyDateAndTime(sub->GetInstallationTime())); + if (metadata) { + js_sub.download_success_count = metadata->GetDownloadSuccessCount(sub->GetSourceUrl()); + js_sub.download_error_count = metadata->GetDownloadErrorCount(sub->GetSourceUrl()); + } result.emplace_back(std::move(js_sub)); } return result; @@ -347,6 +338,47 @@ void AdblockPrivateAPI::OnListenerAdded( namespace api { +AdblockPrivateStartUpdateFunction::AdblockPrivateStartUpdateFunction() {} + +AdblockPrivateStartUpdateFunction::~AdblockPrivateStartUpdateFunction() {} + +ExtensionFunction::ResponseAction AdblockPrivateStartUpdateFunction::Run() { + auto* subscription_service = + adblock::SubscriptionServiceFactory::GetForBrowserContext( + browser_context()); + subscription_service->StartUpdate(); + return RespondNow(NoArguments()); +} + +AdblockPrivateSetPrivilegedFiltersEnabledFunction::AdblockPrivateSetPrivilegedFiltersEnabledFunction() {} + +AdblockPrivateSetPrivilegedFiltersEnabledFunction::~AdblockPrivateSetPrivilegedFiltersEnabledFunction() {} + +ExtensionFunction::ResponseAction AdblockPrivateSetPrivilegedFiltersEnabledFunction::Run() { + absl::optional params = + api::adblock_private::SetEnabled::Params::Create(args()); + EXTENSION_FUNCTION_VALIDATE(params); + + auto* subscription_service = + adblock::SubscriptionServiceFactory::GetForBrowserContext( + browser_context()); + subscription_service->SetPrivilegedFiltersEnabled(params->enabled); + return RespondNow(NoArguments()); +} + +AdblockPrivateIsPrivilegedFiltersEnabledFunction::AdblockPrivateIsPrivilegedFiltersEnabledFunction() {} + +AdblockPrivateIsPrivilegedFiltersEnabledFunction::~AdblockPrivateIsPrivilegedFiltersEnabledFunction() {} + +ExtensionFunction::ResponseAction AdblockPrivateIsPrivilegedFiltersEnabledFunction::Run() { + auto* subscription_service = + adblock::SubscriptionServiceFactory::GetForBrowserContext( + browser_context()); + return RespondNow( + ArgumentList(api::adblock_private::IsEnabled::Results::Create( + subscription_service->IsPrivilegedFiltersEnabled()))); +} + AdblockPrivateSetEnabledFunction::AdblockPrivateSetEnabledFunction() {} AdblockPrivateSetEnabledFunction::~AdblockPrivateSetEnabledFunction() {} @@ -514,7 +546,7 @@ AdblockPrivateGetInstalledSubscriptionsFunction::Run() { << "adblock_private expects \"adblock\" configuration"; return RespondNow(ArgumentList( api::adblock_private::GetInstalledSubscriptions::Results::Create( - CopySubscriptions(subscription_service->GetCurrentSubscriptions( + CopySubscriptions(subscription_service, subscription_service->GetCurrentSubscriptions( subscription_service->GetAdblockFilteringConfiguration()))))); } diff --git a/chrome/browser/extensions/api/adblock_private/adblock_private_api.h b/chrome/browser/extensions/api/adblock_private/adblock_private_api.h --- a/chrome/browser/extensions/api/adblock_private/adblock_private_api.h +++ b/chrome/browser/extensions/api/adblock_private/adblock_private_api.h @@ -58,6 +58,55 @@ void BrowserContextKeyedAPIFactory< namespace api { +class AdblockPrivateStartUpdateFunction : public ExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("adblockPrivate.startUpdate", UNKNOWN) + AdblockPrivateStartUpdateFunction(); + + private: + ~AdblockPrivateStartUpdateFunction() override; + + ResponseAction Run() override; + + AdblockPrivateStartUpdateFunction(const AdblockPrivateStartUpdateFunction&) = + delete; + AdblockPrivateStartUpdateFunction& operator=( + const AdblockPrivateStartUpdateFunction&) = delete; +}; + + +class AdblockPrivateSetPrivilegedFiltersEnabledFunction : public ExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("adblockPrivate.setPrivilegedFiltersEnabled", UNKNOWN) + AdblockPrivateSetPrivilegedFiltersEnabledFunction(); + + private: + ~AdblockPrivateSetPrivilegedFiltersEnabledFunction() override; + + ResponseAction Run() override; + + AdblockPrivateSetPrivilegedFiltersEnabledFunction(const AdblockPrivateSetPrivilegedFiltersEnabledFunction&) = + delete; + AdblockPrivateSetPrivilegedFiltersEnabledFunction& operator=( + const AdblockPrivateSetPrivilegedFiltersEnabledFunction&) = delete; +}; + +class AdblockPrivateIsPrivilegedFiltersEnabledFunction : public ExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("adblockPrivate.isPrivilegedFiltersEnabled", UNKNOWN) + AdblockPrivateIsPrivilegedFiltersEnabledFunction(); + + private: + ~AdblockPrivateIsPrivilegedFiltersEnabledFunction() override; + + ResponseAction Run() override; + + AdblockPrivateIsPrivilegedFiltersEnabledFunction(const AdblockPrivateIsPrivilegedFiltersEnabledFunction&) = + delete; + AdblockPrivateIsPrivilegedFiltersEnabledFunction& operator=( + const AdblockPrivateIsPrivilegedFiltersEnabledFunction&) = delete; +}; + class AdblockPrivateSetEnabledFunction : public ExtensionFunction { public: DECLARE_EXTENSION_FUNCTION("adblockPrivate.setEnabled", UNKNOWN) diff --git a/chrome/browser/extensions/api/eyeo_filtering_private/eyeo_filtering_private_api.cc b/chrome/browser/extensions/api/eyeo_filtering_private/eyeo_filtering_private_api.cc --- a/chrome/browser/extensions/api/eyeo_filtering_private/eyeo_filtering_private_api.cc +++ b/chrome/browser/extensions/api/eyeo_filtering_private/eyeo_filtering_private_api.cc @@ -18,7 +18,8 @@ #include "base/containers/flat_map.h" #include "base/logging.h" #include "base/no_destructor.h" -#include "base/time/time_to_iso8601.h" +#include "base/i18n/time_formatting.h" +#include "base/strings/utf_string_conversions.h" #include "base/values.h" #include "chrome/browser/adblock/resource_classification_runner_factory.h" #include "chrome/browser/adblock/session_stats_factory.h" @@ -29,6 +30,7 @@ #include "components/adblock/content/browser/resource_classification_runner.h" #include "components/adblock/core/common/adblock_utils.h" #include "components/adblock/core/common/content_type.h" +#include "components/adblock/core/subscription/subscription.h" #include "components/adblock/core/configuration/filtering_configuration.h" #include "components/adblock/core/configuration/persistent_filtering_configuration.h" #include "components/adblock/core/session_stats.h" @@ -94,23 +96,6 @@ std::vector CopySessionsStats( return result; } -std::string SubscriptionInstallationStateToString( - adblock::Subscription::InstallationState state) { - using State = adblock::Subscription::InstallationState; - switch (state) { - case State::Installed: - return "Installed"; - case State::Installing: - return "Installing"; - case State::Preloaded: - return "Preloaded"; - case State::Unknown: - return "Unknown"; - } - NOTREACHED(); - return ""; -} - std::vector CopySubscriptions( const std::vector> current_subscriptions) { @@ -121,9 +106,9 @@ std::vector CopySubscriptions( js_sub.title = sub->GetTitle(); js_sub.current_version = sub->GetCurrentVersion(); js_sub.installation_state = - SubscriptionInstallationStateToString(sub->GetInstallationState()); + adblock::Subscription::SubscriptionInstallationStateToString(sub->GetInstallationState()); js_sub.last_installation_time = - base::TimeToISO8601(sub->GetInstallationTime()); + base::UTF16ToUTF8(base::TimeFormatFriendlyDateAndTime(sub->GetInstallationTime())); result.emplace_back(std::move(js_sub)); } return result; diff --git a/chrome/browser/profiles/chrome_browser_main_extra_parts_profiles.cc b/chrome/browser/profiles/chrome_browser_main_extra_parts_profiles.cc --- a/chrome/browser/profiles/chrome_browser_main_extra_parts_profiles.cc +++ b/chrome/browser/profiles/chrome_browser_main_extra_parts_profiles.cc @@ -17,7 +17,6 @@ #include "chrome/browser/accessibility/accessibility_labels_service_factory.h" #include "chrome/browser/accessibility/page_colors_factory.h" #include "chrome/browser/adblock/adblock_controller_factory.h" -#include "chrome/browser/adblock/adblock_telemetry_service_factory.h" #include "chrome/browser/adblock/content_security_policy_injector_factory.h" #include "chrome/browser/adblock/element_hider_factory.h" #include "chrome/browser/adblock/resource_classification_runner_factory.h" @@ -756,7 +755,6 @@ void ChromeBrowserMainExtraPartsProfiles:: ExitTypeServiceFactory::GetInstance(); #endif adblock::AdblockControllerFactory::GetInstance(); - adblock::AdblockTelemetryServiceFactory::GetInstance(); adblock::ContentSecurityPolicyInjectorFactory::GetInstance(); adblock::ElementHiderFactory::GetInstance(); adblock::ResourceClassificationRunnerFactory::GetInstance(); diff --git a/chrome/browser/resources/settings/BUILD.gn b/chrome/browser/resources/settings/BUILD.gn --- a/chrome/browser/resources/settings/BUILD.gn +++ b/chrome/browser/resources/settings/BUILD.gn @@ -74,6 +74,7 @@ build_webui("build") { web_component_files = [ "a11y_page/a11y_page.ts", "about_page/about_page.ts", + "adblock_page/adblock_page.ts", "appearance_page/appearance_fonts_page.ts", "appearance_page/appearance_page.ts", "appearance_page/home_url_input.ts", @@ -372,6 +373,7 @@ build_webui("build") { ts_composite = true ts_definitions = [ "//tools/typescript/definitions/autofill_private.d.ts", + "//tools/typescript/definitions/adblock_private.d.ts", "//tools/typescript/definitions/chrome_send.d.ts", "//tools/typescript/definitions/language_settings_private.d.ts", "//tools/typescript/definitions/management.d.ts", diff --git a/chrome/browser/resources/settings/adblock_page/adblock_page.html b/chrome/browser/resources/settings/adblock_page/adblock_page.html new file mode 100644 --- /dev/null +++ b/chrome/browser/resources/settings/adblock_page/adblock_page.html @@ -0,0 +1,207 @@ + + + + + + +
+ +
+
+
Enable anti-circumvention and snippets
+
+ Snippets are pieces of JavaScript code, injected by the Adblock Plus, that execute within the context of a website and combat advanced ads that circumvent ordinary blocking. + The functionality is ONLY allowed for the list + abp-filters-anti-cv.txt + which is activated by this setting. +
Open ABP anti-circumvention filter list repo +
Open ABP Snippets Overview +
+
+
+ + +
+ +
+
+
+ + Check for updates now + +
+ +
+ +
+
Built in Subscriptions ([[countEnabled]] selected)
+
+ Add the languages in which you regularly browse websites in +
+
+
+ +
+ +
+
+
+
+ +
+ Allowed Domains ([[allowedDomainsCount]] added) +
+
+ Support your favorite websites by adding them to this list. You might see ads on them. +
+
+ +
+ +
+
+ + + Add + + +
+
+
+
+
+
+ +
+ Custom Subscriptions ([[customSubscriptions.length]] added) +
+
+ Add custom filter urls +
+
+ +
+ +
+
+ + + Add + + +
+
+
+
+
+
+ +
+ Custom Filters ([[customFilters.length]] added) +
+
+ Add custom filter commands +
+
+ +
+ +
+
+ + + Add + + +
+
+
+
+
+ +
+ diff --git a/chrome/browser/resources/settings/adblock_page/adblock_page.ts b/chrome/browser/resources/settings/adblock_page/adblock_page.ts new file mode 100644 --- /dev/null +++ b/chrome/browser/resources/settings/adblock_page/adblock_page.ts @@ -0,0 +1,286 @@ +/* + This file is part of Bromite. + + Bromite is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Bromite is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Bromite. If not, see . +*/ +import 'chrome://resources/cr_elements/cr_button/cr_button.js'; +import 'chrome://resources/cr_elements/cr_shared_style.css.js'; +import 'chrome://resources/polymer/v3_0/iron-collapse/iron-collapse.js'; +import 'chrome://resources/polymer/v3_0/iron-flex-layout/iron-flex-layout-classes.js'; +import 'chrome://resources/polymer/v3_0/iron-icon/iron-icon.js'; +import '../settings_shared.css.js'; + +import { I18nMixin } from 'chrome://resources/cr_elements/i18n_mixin.js'; +import { BaseMixin } from '../base_mixin.js'; +import { PolymerElement } from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js'; +import { SettingsToggleButtonElement } from '/shared/settings/controls/settings_toggle_button.js'; +import { PrefsMixin } from 'chrome://resources/cr_components/settings_prefs/prefs_mixin.js'; +import { CrCheckboxElement } from 'chrome://resources/cr_elements/cr_checkbox/cr_checkbox.js'; +import { getTemplate } from './adblock_page.html.js'; +import { getToastManager } from 'chrome://resources/cr_elements/cr_toast/cr_toast_manager.js'; + +const SettingsAdblockPageElementBase = + I18nMixin(PrefsMixin(BaseMixin((PolymerElement)))); + +interface Subscription { + url: string; + title: string; + enabled: boolean; + current_version: string; + download_error_count: number; + download_success_count: number; + installation_state: string; + last_installation_time: string; +} + +export class SettingsAdblockPageElement extends + SettingsAdblockPageElementBase { + static get is() { + return 'settings-adblock-page'; + } + + static get template() { + return getTemplate(); + } + + // input fields updated by html + public customSubscriptionInput: string; + public customFilterInput: string; + public allowedDomainInput: string; + public additionalMessage: string; + public additionalMessageIcon: string; + public countEnabled: number; + public allowedDomainsCount: number; + public isEnablePrivilegedFiltersToggle_: boolean; + + // models that will fill templates lists in html + public customSubscriptions: Subscription[] = []; + public customFilters: Array = []; + public allowedDomains: Array = []; + public subscriptions: Subscription[] = []; + + private syncSubscriptions() { + this.subscriptions = [] + this.customSubscriptions = []; + + chrome.adblockPrivate.getBuiltInSubscriptions(list => { + chrome.adblockPrivate.getInstalledSubscriptions(activelist => { + let new_subscriptions: Subscription[] = []; + let custom_subscriptions: Subscription[] = []; + + list.forEach(obj => { + new_subscriptions.push({ + title: obj.title, + enabled: false, + url: obj.url, + current_version: '', + download_error_count: 0, + download_success_count: 0, + installation_state: '', + last_installation_time: '' + }) + }) + + activelist.forEach(obj => { + var found = new_subscriptions.find(element => element.url == obj.url); + if (found === undefined) { + found = { + title: obj.title, + enabled: false, + url: obj.url, + current_version: '', + download_error_count: 0, + download_success_count: 0, + installation_state: '', + last_installation_time: '' + } + custom_subscriptions.push(found) + } + found.enabled = true; + found.current_version = obj.current_version; + found.download_error_count = obj.download_error_count; + found.download_success_count = obj.download_success_count; + found.installation_state = obj.installation_state; + found.last_installation_time = new Date(obj.last_installation_time).toLocaleString(); + }) + + this.subscriptions = new_subscriptions; + this.customSubscriptions = custom_subscriptions; + this.updateUI(); + }) + }); + } + + private updateUI() { + this.additionalMessage = ""; + this.additionalMessageIcon = ""; + this.countEnabled = 0; + + chrome.adblockPrivate.isEnabled(enabled => { + if (!enabled) return; + + let c = 0; + this.subscriptions.forEach(obj => { + if (obj.enabled) c++; + }); + this.countEnabled = c; + if (this.countEnabled == 0 && + this.customSubscriptions.length == 0 && + this.customFilters.length == 0) { + this.additionalMessage = "No subscriptions selected. Adblock is not active."; + this.additionalMessageIcon = "cr:warning"; + } + }); + + chrome.adblockPrivate.isPrivilegedFiltersEnabled(enabled => { + this.isEnablePrivilegedFiltersToggle_ = enabled; + }); + } + + private syncCustomFilters() { + chrome.adblockPrivate.getCustomFilters(domain => { + this.customFilters = []; + domain.forEach(value => { + this.customFilters.push(value); + }) + this.updateUI(); + }); + } + + private syncAllowedDomains() { + this.allowedDomainsCount = 0; + chrome.adblockPrivate.getAllowedDomains(domain => { + this.allowedDomains = []; + domain.forEach(value => { + this.allowedDomains.push(value); + }) + this.allowedDomainsCount = this.allowedDomains.length; + this.updateUI(); + }); + } + + public override ready() { + super.ready(); + + this.syncSubscriptions(); + this.syncCustomFilters(); + this.syncAllowedDomains(); + } + + private onAdblockEnabled_(event: Event) { + if ((event.target as SettingsToggleButtonElement).checked) { + chrome.adblockPrivate.setEnabled(true); + } else { + chrome.adblockPrivate.setEnabled(false); + } + this.updateUI(); + } + + private onEnablePrivilegedFiltersToggle_(_evt: any, enabled: boolean) { + chrome.adblockPrivate.setPrivilegedFiltersEnabled(enabled); + this.syncSubscriptions(); + } + + private cleanUrl(url: string) : string { + let cleanedUrl : string = ""; + try { + cleanedUrl = new URL(url).host; + } catch (err) { + try { + // one last try by adding schema + cleanedUrl = new URL("https://" + url).host; + } + catch (err) { + console.log("malformed url " + url); + return ""; + } + } + return cleanedUrl; + } + + private selectRecommendedSubscription(e: Event) { + const url = ((e.target as CrCheckboxElement).id); + const enabled = ((e.target as CrCheckboxElement).checked); + if (enabled) { + chrome.adblockPrivate.installSubscription(url); + } else { + chrome.adblockPrivate.uninstallSubscription(url); + } + this.updateUI(); + } + + private removeCustomFilter(e: Event) { + const filter = ((e.target as HTMLElement).id); + chrome.adblockPrivate.removeCustomFilter(filter); + const i = this.customFilters.indexOf(filter); + this.splice('customFilters', i, 1); + this.updateUI(); + } + + private addCustomFilter() { + if (this.customFilterInput == undefined || this.customFilterInput == "") return; + chrome.adblockPrivate.addCustomFilter(this.customFilterInput); + this.customFilterInput = ""; + this.syncCustomFilters(); + } + + private removeAllowedDomain(e: Event) { + const allowedDomain = ((e.target as HTMLElement).id); + chrome.adblockPrivate.removeAllowedDomain(allowedDomain); + const i = this.allowedDomains.indexOf(allowedDomain); + this.splice('allowedDomains', i, 1); + this.allowedDomainsCount = this.allowedDomains.length; + this.updateUI(); + } + + private addAllowedDomain() { + if (this.allowedDomainInput == undefined || this.allowedDomainInput == "") return; + const cleanedUrl = this.cleanUrl(this.allowedDomainInput); + if (cleanedUrl == "") return; + chrome.adblockPrivate.addAllowedDomain(cleanedUrl); + this.allowedDomainInput = ""; + this.syncAllowedDomains(); + } + + private removeCustomSubscription(e: Event) { + const url = ((e.target as HTMLElement).id); + const subscription = this.customSubscriptions.find( x => x.url = url); + chrome.adblockPrivate.uninstallSubscription(subscription!.url); + this.splice('customSubscriptions', this.customSubscriptions.indexOf(subscription!), 1); + this.updateUI(); + } + + private addCustomSubscription() { + if (this.customSubscriptionInput == undefined || this.customSubscriptionInput == "") return; + chrome.adblockPrivate.installSubscription(this.customSubscriptionInput); + this.customSubscriptionInput = ""; + this.syncSubscriptions(); + } + + private startUpdateCycle() { + const toastManager = getToastManager(); + chrome.adblockPrivate.startUpdate(); + toastManager.duration = 5000; + toastManager.show("Starting update..."); + } +} + +declare global { + interface HTMLElementTagNameMap { + 'settings-adblock-page': SettingsAdblockPageElement; + } +} + +customElements.define( + SettingsAdblockPageElement.is, SettingsAdblockPageElement); diff --git a/chrome/browser/resources/settings/basic_page/basic_page.html b/chrome/browser/resources/settings/basic_page/basic_page.html --- a/chrome/browser/resources/settings/basic_page/basic_page.html +++ b/chrome/browser/resources/settings/basic_page/basic_page.html @@ -24,6 +24,13 @@ +