From 6aa0f4c591d107b583a915d14d692e780efee114 Mon Sep 17 00:00:00 2001 From: Anton Lazarev Date: Tue, 3 Sep 2019 12:01:51 -0700 Subject: [PATCH] add toggle for default cosmetic filtering behavior in settings --- app/brave_generated_resources.grd | 3 ++ browser/brave_profile_prefs.cc | 1 + .../api/settings_private/brave_prefs_util.cc | 2 ++ .../default_brave_shields_browser_proxy.js | 19 +++++++++++ .../default_brave_shields_page.html | 5 +++ .../default_brave_shields_page.js | 3 ++ .../settings/default_brave_shields_handler.cc | 32 +++++++++++++++++++ .../settings/default_brave_shields_handler.h | 2 ++ .../settings_localized_strings_provider.cc | 2 ++ common/pref_names.cc | 1 + common/pref_names.h | 1 + 11 files changed, 71 insertions(+) diff --git a/app/brave_generated_resources.grd b/app/brave_generated_resources.grd index e5d12adb70e2..3ae5c5a83772 100644 --- a/app/brave_generated_resources.grd +++ b/app/brave_generated_resources.grd @@ -325,6 +325,9 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U Advanced view + + Block first-party ads + Block cross-site trackers diff --git a/browser/brave_profile_prefs.cc b/browser/brave_profile_prefs.cc index 3adcc95f7e9f..37517a670c59 100644 --- a/browser/brave_profile_prefs.cc +++ b/browser/brave_profile_prefs.cc @@ -61,6 +61,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { // Default Brave shields registry->RegisterBooleanPref(kHTTPSEVerywhereControlType, true); registry->RegisterBooleanPref(kNoScriptControlType, false); + registry->RegisterBooleanPref(kCosmeticFilteringControlType, true); registry->RegisterBooleanPref(kAdControlType, true); // > advanced view is defaulted to true for EXISTING users; false for new bool is_new_user = false; diff --git a/browser/extensions/api/settings_private/brave_prefs_util.cc b/browser/extensions/api/settings_private/brave_prefs_util.cc index 5772904119e7..11c2554a9cde 100644 --- a/browser/extensions/api/settings_private/brave_prefs_util.cc +++ b/browser/extensions/api/settings_private/brave_prefs_util.cc @@ -42,6 +42,8 @@ const PrefsUtil::TypedPrefMap& BravePrefsUtil::GetWhitelistedKeys() { // Default Brave shields (*s_brave_whitelist)[kShieldsAdvancedViewEnabled] = settings_api::PrefType::PREF_TYPE_BOOLEAN; + (*s_brave_whitelist)[kCosmeticFilteringControlType] = + settings_api::PrefType::PREF_TYPE_BOOLEAN; (*s_brave_whitelist)[kAdControlType] = settings_api::PrefType::PREF_TYPE_BOOLEAN; (*s_brave_whitelist)[kHTTPSEVerywhereControlType] = diff --git a/browser/resources/settings/default_brave_shields_page/default_brave_shields_browser_proxy.js b/browser/resources/settings/default_brave_shields_page/default_brave_shields_browser_proxy.js index 77e1a38027c8..cc210329b44f 100644 --- a/browser/resources/settings/default_brave_shields_page/default_brave_shields_browser_proxy.js +++ b/browser/resources/settings/default_brave_shields_page/default_brave_shields_browser_proxy.js @@ -5,6 +5,15 @@ cr.define('settings', function() { /** @interface */ class DefaultBraveShieldsBrowserProxy { + /** + * @return {!Promise} + */ + getCosmeticFilteringControlType() {} + /** + * @param {string} value name. + */ + setCosmeticFilteringControlType(value) {} + /** * @return {!Promise} */ @@ -47,6 +56,16 @@ cr.define('settings', function() { * @implements {settings.DefaultBraveShieldsBrowserProxy} */ class DefaultBraveShieldsBrowserProxyImpl { + /** @override */ + getCosmeticFilteringControlType() { + return cr.sendWithPromise('getCosmeticFilteringControlType'); + } + + /** @override */ + setCosmeticFilteringControlType(value) { + chrome.send('setCosmeticFilteringControlType', [value]); + } + /** @override */ getAdControlType() { return cr.sendWithPromise('getAdControlType'); diff --git a/browser/resources/settings/default_brave_shields_page/default_brave_shields_page.html b/browser/resources/settings/default_brave_shields_page/default_brave_shields_page.html index 35ce0c6e9b89..e3b9c48efa64 100644 --- a/browser/resources/settings/default_brave_shields_page/default_brave_shields_page.html +++ b/browser/resources/settings/default_brave_shields_page/default_brave_shields_page.html @@ -90,6 +90,11 @@ + + RegisterMessageCallback( + "getCosmeticFilteringControlType", + base::BindRepeating(&DefaultBraveShieldsHandler::GetCosmeticFilteringControlType, + base::Unretained(this))); + web_ui()->RegisterMessageCallback( + "setCosmeticFilteringControlType", + base::BindRepeating(&DefaultBraveShieldsHandler::SetCosmeticFilteringControlType, + base::Unretained(this))); web_ui()->RegisterMessageCallback( "getAdControlType", base::BindRepeating(&DefaultBraveShieldsHandler::GetAdControlType, @@ -56,6 +64,30 @@ void DefaultBraveShieldsHandler::RegisterMessages() { base::Unretained(this))); } +void DefaultBraveShieldsHandler::GetCosmeticFilteringControlType(const base::ListValue* args) { + CHECK_EQ(args->GetSize(), 1U); + CHECK(profile_); + + ControlType setting = brave_shields::GetCosmeticFilteringControlType(profile_, GURL()); + + AllowJavascript(); + ResolveJavascriptCallback( + args->GetList()[0].Clone(), + base::Value(setting == ControlType::ALLOW)); +} + +void DefaultBraveShieldsHandler::SetCosmeticFilteringControlType(const base::ListValue* args) { + CHECK_EQ(args->GetSize(), 1U); + CHECK(profile_); + bool value; + args->GetBoolean(0, &value); + + brave_shields::SetCosmeticFilteringControlType(profile_, + value ? ControlType::BLOCK + : ControlType::ALLOW, + GURL()); +} + void DefaultBraveShieldsHandler::GetAdControlType(const base::ListValue* args) { CHECK_EQ(args->GetSize(), 1U); CHECK(profile_); diff --git a/browser/ui/webui/settings/default_brave_shields_handler.h b/browser/ui/webui/settings/default_brave_shields_handler.h index 37955d80a24b..b2fa00c2c7f3 100644 --- a/browser/ui/webui/settings/default_brave_shields_handler.h +++ b/browser/ui/webui/settings/default_brave_shields_handler.h @@ -21,6 +21,8 @@ class DefaultBraveShieldsHandler : public settings::SettingsPageUIHandler { void OnJavascriptAllowed() override {} void OnJavascriptDisallowed() override {} + void SetCosmeticFilteringControlType(const base::ListValue* args); + void GetCosmeticFilteringControlType(const base::ListValue* args); void SetAdControlType(const base::ListValue* args); void GetAdControlType(const base::ListValue* args); void SetCookieControlType(const base::ListValue* args); diff --git a/chromium_src/chrome/browser/ui/webui/settings/settings_localized_strings_provider.cc b/chromium_src/chrome/browser/ui/webui/settings/settings_localized_strings_provider.cc index d75e5a003bff..0d787d011c76 100644 --- a/chromium_src/chrome/browser/ui/webui/settings/settings_localized_strings_provider.cc +++ b/chromium_src/chrome/browser/ui/webui/settings/settings_localized_strings_provider.cc @@ -87,6 +87,8 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source, IDS_SETTINGS_BRAVE_SHIELDS_DEFAULTS_SIMPLE_VIEW_LABEL}, {"advancedView", IDS_SETTINGS_BRAVE_SHIELDS_DEFAULTS_ADVANCED_VIEW_LABEL}, + {"cosmeticFilteringControlLabel", + IDS_SETTINGS_BRAVE_SHIELDS_COSMETIC_FILTERING_CONTROL_LABEL}, {"adControlLabel", IDS_SETTINGS_BRAVE_SHIELDS_AD_CONTROL_LABEL}, {"cookieControlLabel", diff --git a/common/pref_names.cc b/common/pref_names.cc index ac01e1e73254..01acaeabb769 100644 --- a/common/pref_names.cc +++ b/common/pref_names.cc @@ -47,6 +47,7 @@ const char kHTTPSEVerywhereControlType[] = "brave.https_everywhere_default"; const char kNoScriptControlType[] = "brave.no_script_default"; const char kShieldsAdvancedViewEnabled[] = "brave.shields.advanced_view_enabled"; +const char kCosmeticFilteringControlType[] = "brave.cosmetic_filtering_default"; const char kAdControlType[] = "brave.ad_default"; const char kFBEmbedControlType[] = "brave.fb_embed_default"; const char kTwitterEmbedControlType[] = "brave.twitter_embed_default"; diff --git a/common/pref_names.h b/common/pref_names.h index 49d5bd976951..1b1bf9a8d77e 100644 --- a/common/pref_names.h +++ b/common/pref_names.h @@ -40,6 +40,7 @@ extern const char kReferralAndroidFirstRunTimestamp[]; extern const char kHTTPSEVerywhereControlType[]; extern const char kNoScriptControlType[]; extern const char kShieldsAdvancedViewEnabled[]; +extern const char kCosmeticFilteringControlType[]; extern const char kAdControlType[]; extern const char kFBEmbedControlType[]; extern const char kTwitterEmbedControlType[];