Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add social blocking options #1818

Merged
merged 13 commits into from
Mar 15, 2019
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use_relative_paths = True

deps = {
"vendor/ad-block": "https://github.com/brave/ad-block.git@534c153a3e8d1c8eda02cc80297b9abf87e9a7da",
"vendor/ad-block": "https://github.com/brave/ad-block.git@74b167c8b49c33f3f61c15dafa2ffde953a25653",
"vendor/autoplay-whitelist": "https://github.com/brave/autoplay-whitelist.git@458053a3c95b403cbe0872f289a2aafa106ee9d8",
"vendor/extension-whitelist": "https://github.com/brave/extension-whitelist.git@463e5e4e06e0ca84927176e8c72f6076ae9b6829",
"vendor/tracking-protection": "https://github.com/brave/tracking-protection.git@29b1f86b11a8c7438fd7d57b446a77a84946712a",
Expand Down
15 changes: 15 additions & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
<message name="IDS_SETTINGS_BRAVE_SHIELDS_DEFAULTS_TITLE" desc="The title for Brave shields defaults in settings">
Brave shields defaults
</message>
<message name="IDS_SETTINGS_SOCIAL_BLOCKING_DEFAULTS_TITLE" desc="The title for social blocking defaults in settings">
Social Buttons and Logins
</message>
<message name="IDS_SETTINGS_BRAVE_SHIELDS_AD_CONTROL_LABEL" desc="Default Brave ad control setting label">
Ad Control
</message>
Expand All @@ -285,6 +288,18 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
<message name="IDS_SETTINGS_BRAVE_SHIELDS_NO_SCRIPT_CONTROL_LABEL" desc="Default Brave script blocking control setting label">
Script blocking
</message>
<message name="IDS_SETTINGS_BRAVE_SHIELDS_GOOGLE_LOGIN_LABEL" desc="Label for a switch control which allows Google social buttons to be enabled/disabled">
Allow Google login buttons on third party sites
</message>
<message name="IDS_SETTINGS_BRAVE_SHIELDS_FACEBOOK_EMBEDDED_POSTS_LABEL" desc="Label for a switch control which allows Facebook embedded posts">
Allow Facebook logins and embedded posts
</message>
<message name="IDS_SETTINGS_BRAVE_SHIELDS_TWITTER_EMBEDDED_TWEETS_LABEL" desc="Label for a switch control which allows Twitter embedded tweets">
Allow Twitter embedded tweets
</message>
<message name="IDS_SETTINGS_BRAVE_SHIELDS_LINKEDIN_EMBEDDED_POSTS_LABEL" desc="Label for a switch control which allows LinkedIn embedded posts">
Allow LinkedIn embeds
</message>
<message name="IDS_SETTINGS_BLOCK_ADS" desc="Select value">
Block ads
</message>
Expand Down
19 changes: 18 additions & 1 deletion browser/autoplay/autoplay_permission_context_unittest.cc
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/autoplay/autoplay_permission_context.h"

#include <memory>
#include <string>
#include <utility>

#include "base/bind.h"
#include "base/macros.h"
#include "brave/common/pref_names.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/permissions/permission_request_id.h"
#include "chrome/browser/permissions/permission_request_manager.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/mock_render_process_host.h"
Expand Down Expand Up @@ -66,6 +72,17 @@ class AutoplayPermissionContextTests : public ChromeRenderViewHostTestHarness {
ChromeRenderViewHostTestHarness::TearDown();
}

content::BrowserContext* CreateBrowserContext() override {
TestingProfile::Builder builder;
auto prefs =
std::make_unique<sync_preferences::TestingPrefServiceSyncable>();
RegisterUserProfilePrefs(prefs->registry());
prefs->registry()->
RegisterBooleanPref(kGoogleLoginControlType, true);
builder.SetPrefService(std::move(prefs));
return builder.Build().release();
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was needed otherwise a new unregistered pref error occurs only for 1 test in this file.


DISALLOW_COPY_AND_ASSIGN(AutoplayPermissionContextTests);
};

Expand Down
6 changes: 4 additions & 2 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ bool HandleURLRewrite(GURL* url,

BraveContentBrowserClient::BraveContentBrowserClient(
ChromeFeatureListCreator* chrome_feature_list_creator)
: ChromeContentBrowserClient(chrome_feature_list_creator) {}
: ChromeContentBrowserClient(chrome_feature_list_creator) {
}

BraveContentBrowserClient::~BraveContentBrowserClient() {}

Expand Down Expand Up @@ -167,7 +168,8 @@ bool BraveContentBrowserClient::AllowAccessCookie(
content_settings::BraveCookieSettings* cookie_settings =
(content_settings::BraveCookieSettings*)io_data->GetCookieSettings();
bool allow = !ShouldBlockCookie(allow_brave_shields, allow_1p_cookies,
allow_3p_cookies, first_party, url) &&
allow_3p_cookies, first_party, url,
cookie_settings->GetAllowGoogleAuth()) &&
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ShouldBlockCookie will need a refactor but not in the scope of this issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed on both counts

cookie_settings->IsCookieAccessAllowed(url, first_party, tab_origin);
return allow;
}
Expand Down
9 changes: 9 additions & 0 deletions browser/brave_content_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
#include <string>

#include "chrome/browser/chrome_content_browser_client.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/content_browser_client.h"

class PrefChangeRegistrar;

namespace content {
class BrowserContext;
}
Expand Down Expand Up @@ -79,6 +82,12 @@ class BraveContentBrowserClient : public ChromeContentBrowserClient {
bool AllowAccessCookie(const GURL& url, const GURL& first_party,
content::ResourceContext* context, int render_process_id,
int render_frame_id);
void OnAllowGoogleAuthChanged();

std::unique_ptr<PrefChangeRegistrar, content::BrowserThread::DeleteOnUIThread>
pref_change_registrar_;
bool allow_google_auth_;

DISALLOW_COPY_AND_ASSIGN(BraveContentBrowserClient);
};

Expand Down
4 changes: 4 additions & 0 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
// Default Brave shields
registry->RegisterBooleanPref(kHTTPSEVerywhereControlType, true);
registry->RegisterBooleanPref(kNoScriptControlType, false);
registry->RegisterBooleanPref(kGoogleLoginControlType, true);
Copy link
Member Author

@bbondy bbondy Mar 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only thing that actually changes default behaviour in the context of this ticket.

registry->RegisterBooleanPref(kFBEmbedControlType, true);
registry->RegisterBooleanPref(kTwitterEmbedControlType, true);
registry->RegisterBooleanPref(kLinkedInEmbedControlType, false);

// WebTorrent
registry->RegisterBooleanPref(kWebTorrentEnabled, true);
Expand Down
8 changes: 8 additions & 0 deletions browser/brave_profile_prefs_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ IN_PROC_BROWSER_TEST_F(BraveProfilePrefsBrowserTest, MiscBravePrefs) {
kHTTPSEVerywhereControlType));
EXPECT_FALSE(
browser()->profile()->GetPrefs()->GetBoolean(kNoScriptControlType));
EXPECT_TRUE(
browser()->profile()->GetPrefs()->GetBoolean(kGoogleLoginControlType));
EXPECT_TRUE(
browser()->profile()->GetPrefs()->GetBoolean(kFBEmbedControlType));
EXPECT_TRUE(
browser()->profile()->GetPrefs()->GetBoolean(kTwitterEmbedControlType));
EXPECT_FALSE(
browser()->profile()->GetPrefs()->GetBoolean(kLinkedInEmbedControlType));
EXPECT_TRUE(
browser()->profile()->GetPrefs()->GetBoolean(kWebTorrentEnabled));
EXPECT_TRUE(
Expand Down
9 changes: 9 additions & 0 deletions browser/extensions/api/settings_private/brave_prefs_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ const PrefsUtil::TypedPrefMap& BravePrefsUtil::GetWhitelistedKeys() {
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_whitelist)[kNoScriptControlType] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_whitelist)[kGoogleLoginControlType] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_whitelist)[kFBEmbedControlType] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_whitelist)[kTwitterEmbedControlType] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_whitelist)[kLinkedInEmbedControlType] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;

// appearance prefs
(*s_brave_whitelist)[kLocationBarIsWide] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
Expand Down
65 changes: 64 additions & 1 deletion browser/net/brave_network_delegate_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
#include <utility>

#include "base/task/post_task.h"
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
#include "brave/components/brave_shields/browser/ad_block_regional_service.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "brave/components/brave_shields/browser/brave_shields_web_contents_observer.h"
#include "brave/components/brave_shields/common/brave_shield_constants.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/content_settings/tab_specific_content_settings.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_task_traits.h"
Expand Down Expand Up @@ -42,6 +46,19 @@ content::WebContents* GetWebContentsFromProcessAndFrameId(int render_process_id,
return content::WebContents::FromFrameTreeNodeId(render_frame_id);
}

std::string GetTagFromPrefName(const std::string& pref_name) {
if (pref_name == kFBEmbedControlType) {
return brave_shields::kFacebookEmbeds;
}
if (pref_name == kTwitterEmbedControlType) {
return brave_shields::kTwitterEmbeds;
}
if (pref_name == kLinkedInEmbedControlType) {
return brave_shields::kLinkedInEmbeds;
}
return "";
}

} // namespace

base::flat_set<base::StringPiece>* TrackableSecurityHeaders() {
Expand Down Expand Up @@ -81,7 +98,8 @@ void RemoveTrackableSecurityHeadersForThirdParty(

BraveNetworkDelegateBase::BraveNetworkDelegateBase(
extensions::EventRouterForwarder* event_router)
: ChromeNetworkDelegate(event_router), referral_headers_list_(nullptr) {
: ChromeNetworkDelegate(event_router), referral_headers_list_(nullptr),
allow_google_auth_(true) {
// Initialize the preference change registrar.
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::UI},
Expand All @@ -102,6 +120,30 @@ void BraveNetworkDelegateBase::InitPrefChangeRegistrarOnUI() {
base::Unretained(this)));
// Retrieve current referral headers, if any.
OnReferralHeadersChanged();

PrefService* user_prefs = ProfileManager::GetActiveUserProfile()->GetPrefs();
user_pref_change_registrar_.reset(new PrefChangeRegistrar());
user_pref_change_registrar_->Init(user_prefs);
user_pref_change_registrar_->Add(
kGoogleLoginControlType,
base::BindRepeating(&BraveNetworkDelegateBase::OnPreferenceChanged,
base::Unretained(this), kGoogleLoginControlType));
user_pref_change_registrar_->Add(
kFBEmbedControlType,
base::BindRepeating(&BraveNetworkDelegateBase::OnPreferenceChanged,
base::Unretained(this), kFBEmbedControlType));
user_pref_change_registrar_->Add(
kTwitterEmbedControlType,
base::BindRepeating(&BraveNetworkDelegateBase::OnPreferenceChanged,
base::Unretained(this), kTwitterEmbedControlType));
user_pref_change_registrar_->Add(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I first tried adding these observers inside of the ad block base class but it caused problems because of threading, it doesn't work from the main thread which prefs needed to.

kLinkedInEmbedControlType,
base::BindRepeating(&BraveNetworkDelegateBase::OnPreferenceChanged,
base::Unretained(this), kLinkedInEmbedControlType));
UpdateAdBlockFromPref(kFBEmbedControlType);
UpdateAdBlockFromPref(kTwitterEmbedControlType);
UpdateAdBlockFromPref(kLinkedInEmbedControlType);
allow_google_auth_ = user_prefs->GetBoolean(kGoogleLoginControlType);
}

void BraveNetworkDelegateBase::OnReferralHeadersChanged() {
Expand Down Expand Up @@ -195,6 +237,7 @@ bool BraveNetworkDelegateBase::OnCanGetCookies(
const net::CookieList& cookie_list,
bool allowed_from_caller) {
std::shared_ptr<brave::BraveRequestInfo> ctx(new brave::BraveRequestInfo());
ctx->allow_google_auth = allow_google_auth_;
brave::BraveRequestInfo::FillCTXFromRequest(&request, ctx);
ctx->event_type = brave::kOnCanGetCookies;
bool allow = std::all_of(can_get_cookies_callbacks_.begin(),
Expand All @@ -221,6 +264,7 @@ bool BraveNetworkDelegateBase::OnCanSetCookie(
net::CookieOptions* options,
bool allowed_from_caller) {
std::shared_ptr<brave::BraveRequestInfo> ctx(new brave::BraveRequestInfo());
ctx->allow_google_auth = allow_google_auth_;
brave::BraveRequestInfo::FillCTXFromRequest(&request, ctx);
ctx->event_type = brave::kOnCanSetCookies;

Expand Down Expand Up @@ -367,3 +411,22 @@ bool BraveNetworkDelegateBase::IsRequestIdentifierValid(
uint64_t request_identifier) {
return ContainsKey(callbacks_, request_identifier);
}

void BraveNetworkDelegateBase::OnPreferenceChanged(
const std::string& pref_name) {
UpdateAdBlockFromPref(pref_name);
}

void BraveNetworkDelegateBase::UpdateAdBlockFromPref(
const std::string& pref_name) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
PrefService* user_prefs = ProfileManager::GetActiveUserProfile()->GetPrefs();
allow_google_auth_ = user_prefs->GetBoolean(kGoogleLoginControlType);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not thread-safe to write allow_google_auth_ on UI and later read on IO

std::string tag = GetTagFromPrefName(pref_name);
if (tag.length() == 0) {
return;
}
bool enabled = user_prefs->GetBoolean(pref_name);
g_brave_browser_process->ad_block_service()->EnableTag(tag, enabled);
g_brave_browser_process->ad_block_regional_service()->EnableTag(tag, enabled);
}
7 changes: 7 additions & 0 deletions browser/net/brave_network_delegate_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ class BraveNetworkDelegateBase : public ChromeNetworkDelegate {
void InitPrefChangeRegistrarOnUI();
void SetReferralHeaders(base::ListValue* referral_headers);
void OnReferralHeadersChanged();
void OnPreferenceChanged(const std::string& pref_name);
void UpdateAdBlockFromPref(const std::string& pref_name);

// TODO(iefremov): actually, we don't have to keep the list here, since
// it is global for the whole browser and could live a singletonce in the
// rewards service. Eliminating this will also help to avoid using
Expand All @@ -98,6 +101,10 @@ class BraveNetworkDelegateBase : public ChromeNetworkDelegate {
std::map<uint64_t, net::CompletionOnceCallback> callbacks_;
std::unique_ptr<PrefChangeRegistrar, content::BrowserThread::DeleteOnUIThread>
pref_change_registrar_;
std::unique_ptr<PrefChangeRegistrar, content::BrowserThread::DeleteOnUIThread>
user_pref_change_registrar_;

bool allow_google_auth_;

DISALLOW_COPY_AND_ASSIGN(BraveNetworkDelegateBase);
};
Expand Down
6 changes: 5 additions & 1 deletion browser/net/brave_profile_network_delegate.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

Expand All @@ -11,11 +12,14 @@
#include "brave/browser/net/brave_referrals_network_delegate_helper.h"
#include "brave/browser/net/brave_site_hacks_network_delegate_helper.h"
#include "brave/browser/net/brave_tor_network_delegate_helper.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_rewards/browser/buildflags/buildflags.h"
#if BUILDFLAG(BRAVE_REWARDS_ENABLED)
#include "brave/components/brave_rewards/browser/net/network_delegate_helper.h"
#endif
#include "brave/components/brave_webtorrent/browser/net/brave_torrent_redirect_network_delegate_helper.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "components/prefs/pref_service.h"

BraveProfileNetworkDelegate::BraveProfileNetworkDelegate(
extensions::EventRouterForwarder* event_router) :
Expand Down
11 changes: 8 additions & 3 deletions browser/net/cookie_network_delegate_helper.cc
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/net/cookie_network_delegate_helper.h"

#include <memory>

#include "brave/common/brave_cookie_blocking.h"

namespace brave {

bool OnCanGetCookiesForBraveShields(std::shared_ptr<BraveRequestInfo> ctx) {
return !ShouldBlockCookie(ctx->allow_brave_shields, ctx->allow_1p_cookies,
ctx->allow_3p_cookies, ctx->tab_origin, ctx->request_url);
ctx->allow_3p_cookies, ctx->tab_origin, ctx->request_url,
ctx->allow_google_auth);
}

bool OnCanSetCookiesForBraveShields(std::shared_ptr<BraveRequestInfo> ctx) {
return !ShouldBlockCookie(ctx->allow_brave_shields, ctx->allow_1p_cookies,
ctx->allow_3p_cookies, ctx->tab_origin, ctx->request_url);
ctx->allow_3p_cookies, ctx->tab_origin, ctx->request_url,
ctx->allow_google_auth);
}

} // namespace brave
Loading