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 #1972

Merged
merged 2 commits into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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@84f5b7e7ae4de4e18b1e1673198a91ec15507889",
"vendor/ad-block": "https://github.com/brave/ad-block.git@74b167c8b49c33f3f61c15dafa2ffde953a25653",
"vendor/tracking-protection": "https://github.com/brave/tracking-protection.git@e67738e656244f7ab6e0ed9815071ca744f5468f",
"vendor/hashset-cpp": "https://github.com/brave/hashset-cpp.git@4b55fe39bb25bb0d8b11a43d547d75f00c6c46fb",
"vendor/bloom-filter-cpp": "https://github.com/brave/bloom-filter-cpp.git@9be5c63b14e094156e00c8b28f205e7794f0b92c",
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 @@ -288,6 +288,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 @@ -303,6 +306,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();
}

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 @@ -121,7 +121,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 @@ -170,7 +171,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()) &&
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);
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 @@ -38,6 +38,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 @@ -7,12 +7,16 @@
#include <algorithm>

#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 All @@ -38,11 +42,25 @@ 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

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 @@ -63,6 +81,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(
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 @@ -154,6 +196,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 @@ -180,6 +223,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 @@ -326,3 +370,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);
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 @@ -73,6 +73,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 @@ -82,6 +85,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
16 changes: 11 additions & 5 deletions browser/net/url_context.cc
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
/* 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/url_context.h"

#include <memory>
#include <string>

#include "brave/common/pref_names.h"
#include "brave/common/url_constants.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/profiles/profile_manager.h"
#include "components/prefs/testing_pref_service.h"
#include "content/public/browser/resource_request_info.h"

namespace brave {
Expand Down Expand Up @@ -51,12 +56,13 @@ void BraveRequestInfo::FillCTXFromRequest(const net::URLRequest* request,
ctx->allow_ads = brave_shields::IsAllowContentSettingFromIO(
request, ctx->tab_origin, ctx->tab_origin, CONTENT_SETTINGS_TYPE_PLUGINS,
brave_shields::kAds);
ctx->allow_http_upgradable_resource = brave_shields::IsAllowContentSettingFromIO(
request, ctx->tab_origin, ctx->tab_origin, CONTENT_SETTINGS_TYPE_PLUGINS,
ctx->allow_http_upgradable_resource =
brave_shields::IsAllowContentSettingFromIO(request, ctx->tab_origin,
ctx->tab_origin, CONTENT_SETTINGS_TYPE_PLUGINS,
brave_shields::kHTTPUpgradableResources);
ctx->allow_1p_cookies = brave_shields::IsAllowContentSettingFromIO(
request, ctx->tab_origin, GURL("https://firstParty/"), CONTENT_SETTINGS_TYPE_PLUGINS,
brave_shields::kCookies);
request, ctx->tab_origin, GURL("https://firstParty/"),
CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kCookies);
ctx->allow_3p_cookies = brave_shields::IsAllowContentSettingFromIO(
request, ctx->tab_origin, GURL(), CONTENT_SETTINGS_TYPE_PLUGINS,
brave_shields::kCookies);
Expand Down
Loading