Skip to content

Commit

Permalink
Merge pull request #1966 from brave/adblock-custom-filters
Browse files Browse the repository at this point in the history
Adblock custom filters
  • Loading branch information
emerick authored Mar 15, 2019
2 parents 57170b7 + 0b2a964 commit 4760679
Show file tree
Hide file tree
Showing 28 changed files with 410 additions and 31 deletions.
21 changes: 17 additions & 4 deletions browser/brave_browser_process_impl.cc
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
/* 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/brave_browser_process_impl.h"

#include <utility>

#include "base/bind.h"
#include "base/task/post_task.h"
#include "base/path_service.h"
#include "base/task/post_task.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "brave/browser/brave_stats_updater.h"
#include "brave/browser/component_updater/brave_component_updater_configurator.h"
#include "brave/browser/extensions/brave_tor_client_updater.h"
#include "brave/browser/profiles/brave_profile_manager.h"
#include "brave/components/brave_referrals/browser/brave_referrals_service.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
#include "brave/components/brave_shields/browser/ad_block_custom_filters_service.h"
#include "brave/components/brave_shields/browser/ad_block_regional_service.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
#include "brave/components/brave_shields/browser/autoplay_whitelist_service.h"
#include "brave/components/brave_shields/browser/extension_whitelist_service.h"
#include "brave/components/brave_shields/browser/https_everywhere_service.h"
Expand All @@ -37,7 +41,8 @@ using content::BrowserThread;
BraveBrowserProcessImpl::~BraveBrowserProcessImpl() {
}

BraveBrowserProcessImpl::BraveBrowserProcessImpl(ChromeFeatureListCreator* chrome_feature_list_creator)
BraveBrowserProcessImpl::BraveBrowserProcessImpl(
ChromeFeatureListCreator* chrome_feature_list_creator)
: BrowserProcessImpl(chrome_feature_list_creator) {
g_browser_process = this;
g_brave_browser_process = this;
Expand Down Expand Up @@ -106,6 +111,14 @@ BraveBrowserProcessImpl::ad_block_service() {
return ad_block_service_.get();
}

brave_shields::AdBlockCustomFiltersService*
BraveBrowserProcessImpl::ad_block_custom_filters_service() {
if (!ad_block_custom_filters_service_)
ad_block_custom_filters_service_ =
brave_shields::AdBlockCustomFiltersServiceFactory();
return ad_block_custom_filters_service_.get();
}

brave_shields::AdBlockRegionalService*
BraveBrowserProcessImpl::ad_block_regional_service() {
if (!ad_block_regional_service_)
Expand Down
12 changes: 10 additions & 2 deletions browser/brave_browser_process_impl.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* 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/. */

#ifndef BRAVE_BROWSER_BRAVE_BROWSER_PROCESS_IMPL_H_
#define BRAVE_BROWSER_BRAVE_BROWSER_PROCESS_IMPL_H_

#include <memory>

#include "chrome/browser/browser_process_impl.h"
#include "third_party/widevine/cdm/buildflags.h"

Expand All @@ -19,6 +22,7 @@ class BraveWidevineBundleManager;

namespace brave_shields {
class AdBlockService;
class AdBlockCustomFiltersService;
class AdBlockRegionalService;
class AutoplayWhitelistService;
class ExtensionWhitelistService;
Expand All @@ -33,7 +37,8 @@ class BraveTorClientUpdater;

class BraveBrowserProcessImpl : public BrowserProcessImpl {
public:
BraveBrowserProcessImpl(ChromeFeatureListCreator* chrome_feature_list_creator);
BraveBrowserProcessImpl(
ChromeFeatureListCreator* chrome_feature_list_creator);
~BraveBrowserProcessImpl() override;

// BrowserProcess implementation.
Expand All @@ -42,6 +47,7 @@ class BraveBrowserProcessImpl : public BrowserProcessImpl {
ProfileManager* profile_manager() override;

brave_shields::AdBlockService* ad_block_service();
brave_shields::AdBlockCustomFiltersService* ad_block_custom_filters_service();
brave_shields::AdBlockRegionalService* ad_block_regional_service();
brave_shields::AutoplayWhitelistService* autoplay_whitelist_service();
brave_shields::ExtensionWhitelistService* extension_whitelist_service();
Expand All @@ -57,6 +63,8 @@ class BraveBrowserProcessImpl : public BrowserProcessImpl {
void CreateProfileManager();

std::unique_ptr<brave_shields::AdBlockService> ad_block_service_;
std::unique_ptr<brave_shields::AdBlockCustomFiltersService>
ad_block_custom_filters_service_;
std::unique_ptr<brave_shields::AdBlockRegionalService>
ad_block_regional_service_;
std::unique_ptr<brave_shields::AutoplayWhitelistService>
Expand Down
5 changes: 4 additions & 1 deletion browser/brave_local_state_prefs.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 @@ -8,13 +9,15 @@
#include "brave/browser/brave_stats_updater.h"
#include "brave/browser/tor/tor_profile_service.h"
#include "brave/components/brave_referrals/browser/brave_referrals_service.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
#include "chrome/browser/first_run/first_run.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_registry_simple.h"

namespace brave {

void RegisterLocalStatePrefs(PrefRegistrySimple* registry) {
brave_shields::RegisterPrefsForAdBlockService(registry);
RegisterPrefsForBraveStatsUpdater(registry);
RegisterPrefsForBraveReferralsService(registry);
#if defined(OS_MACOSX)
Expand Down
22 changes: 14 additions & 8 deletions browser/net/brave_ad_block_tp_network_delegate_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/common/network_constants.h"
#include "brave/common/shield_exceptions.h"
#include "brave/components/brave_shields/browser/ad_block_custom_filters_service.h"
#include "brave/components/brave_shields/browser/ad_block_regional_service.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
Expand Down Expand Up @@ -96,18 +97,23 @@ void OnBeforeURLRequestAdBlockTPOnTaskRunner(
bool did_match_exception = false;
std::string tab_host = ctx->tab_origin.host();
if (!g_brave_browser_process->ad_block_service()->ShouldStartRequest(
ctx->request_url, ctx->resource_type, tab_host,
&did_match_exception)) {
ctx->request_url, ctx->resource_type, tab_host,
&did_match_exception)) {
ctx->blocked_by = kAdBlocked;
} else if (!did_match_exception &&
!g_brave_browser_process->ad_block_regional_service()
->ShouldStartRequest(ctx->request_url, ctx->resource_type,
tab_host, &did_match_exception)) {
!g_brave_browser_process->ad_block_regional_service()
->ShouldStartRequest(ctx->request_url, ctx->resource_type,
tab_host, &did_match_exception)) {
ctx->blocked_by = kAdBlocked;
} else if (!did_match_exception &&
!g_brave_browser_process->tracking_protection_service()
->ShouldStartRequest(ctx->request_url, ctx->resource_type, tab_host,
&did_match_exception)) {
!g_brave_browser_process->ad_block_custom_filters_service()
->ShouldStartRequest(ctx->request_url, ctx->resource_type,
tab_host, &did_match_exception)) {
ctx->blocked_by = kAdBlocked;
} else if (!did_match_exception &&
!g_brave_browser_process->tracking_protection_service()
->ShouldStartRequest(ctx->request_url, ctx->resource_type,
tab_host, &did_match_exception)) {
ctx->blocked_by = kTrackerBlocked;
}
}
Expand Down
5 changes: 4 additions & 1 deletion browser/net/brave_network_delegate_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
#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_custom_filters_service.h"
#include "brave/components/brave_shields/browser/ad_block_regional_service.h"
#include "brave/components/brave_shields/browser/ad_block_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"
Expand Down Expand Up @@ -429,4 +430,6 @@ void BraveNetworkDelegateBase::UpdateAdBlockFromPref(
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);
g_brave_browser_process->ad_block_custom_filters_service()->EnableTag(
tag, enabled);
}
76 changes: 69 additions & 7 deletions browser/ui/webui/brave_adblock_ui.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 @@ -8,6 +9,7 @@
#include "brave/common/pref_names.h"
#include "brave/common/webui_url_constants.h"
#include "brave/components/brave_adblock/resources/grit/brave_adblock_generated_map.h"
#include "brave/components/brave_shields/browser/ad_block_custom_filters_service.h"
#include "brave/components/brave_shields/browser/ad_block_regional_service.h"
#include "chrome/browser/profiles/profile.h"
#include "components/grit/brave_components_resources.h"
Expand All @@ -18,6 +20,61 @@
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/browser/web_ui_message_handler.h"

namespace {

class AdblockDOMHandler : public content::WebUIMessageHandler {
public:
AdblockDOMHandler();
~AdblockDOMHandler() override;

// WebUIMessageHandler implementation.
void RegisterMessages() override;

private:
void HandleGetCustomFilters(const base::ListValue* args);
void HandleUpdateCustomFilters(const base::ListValue* args);

DISALLOW_COPY_AND_ASSIGN(AdblockDOMHandler);
};

AdblockDOMHandler::AdblockDOMHandler() {}

AdblockDOMHandler::~AdblockDOMHandler() {}

void AdblockDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"brave_adblock.getCustomFilters",
base::BindRepeating(&AdblockDOMHandler::HandleGetCustomFilters,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"brave_adblock.updateCustomFilters",
base::BindRepeating(&AdblockDOMHandler::HandleUpdateCustomFilters,
base::Unretained(this)));
}

void AdblockDOMHandler::HandleGetCustomFilters(const base::ListValue* args) {
DCHECK_EQ(args->GetSize(), 0U);
const std::string custom_filters =
g_brave_browser_process->ad_block_custom_filters_service()
->GetCustomFilters();
if (!web_ui()->CanCallJavascript())
return;
web_ui()->CallJavascriptFunctionUnsafe("brave_adblock.onGetCustomFilters",
base::Value(custom_filters));
}

void AdblockDOMHandler::HandleUpdateCustomFilters(const base::ListValue* args) {
DCHECK_EQ(args->GetSize(), 1U);
std::string custom_filters;
if (!args->GetString(0, &custom_filters))
return;

g_brave_browser_process->ad_block_custom_filters_service()
->UpdateCustomFilters(custom_filters);
}

} // namespace

BraveAdblockUI::BraveAdblockUI(content::WebUI* web_ui, const std::string& name)
: BasicUI(web_ui, name, kBraveAdblockGenerated,
kBraveAdblockGeneratedSize, IDR_BRAVE_ADBLOCK_HTML) {
Expand All @@ -27,22 +84,27 @@ BraveAdblockUI::BraveAdblockUI(content::WebUI* web_ui, const std::string& name)
pref_change_registrar_->Init(prefs);
pref_change_registrar_->Add(kAdsBlocked,
base::Bind(&BraveAdblockUI::OnPreferenceChanged, base::Unretained(this)));
web_ui->AddMessageHandler(std::make_unique<AdblockDOMHandler>());
}

BraveAdblockUI::~BraveAdblockUI() {
}

void BraveAdblockUI::CustomizeWebUIProperties(content::RenderViewHost* render_view_host) {
void BraveAdblockUI::CustomizeWebUIProperties(
content::RenderViewHost* render_view_host) {
DCHECK(IsSafeToSetWebUIProperties());

Profile* profile = Profile::FromWebUI(web_ui());
PrefService* prefs = profile->GetPrefs();
if (render_view_host) {
render_view_host->SetWebUIProperty("adsBlockedStat", std::to_string(prefs->GetUint64(kAdsBlocked)));
render_view_host->SetWebUIProperty("regionalAdBlockEnabled",
std::to_string(
g_brave_browser_process->ad_block_regional_service()->IsInitialized()));
render_view_host->SetWebUIProperty("regionalAdBlockTitle",
render_view_host->SetWebUIProperty(
"adsBlockedStat", std::to_string(prefs->GetUint64(kAdsBlocked)));
render_view_host->SetWebUIProperty(
"regionalAdBlockEnabled",
std::to_string(g_brave_browser_process->ad_block_regional_service()
->IsInitialized()));
render_view_host->SetWebUIProperty(
"regionalAdBlockTitle",
g_brave_browser_process->ad_block_regional_service()->GetTitle());
}
}
Expand Down
5 changes: 4 additions & 1 deletion browser/ui/webui/brave_adblock_ui.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
/* 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/. */

#ifndef BRAVE_BROWSER_UI_WEBUI_BRAVE_ADBLOCK_UI_H_
#define BRAVE_BROWSER_UI_WEBUI_BRAVE_ADBLOCK_UI_H_

#include <memory>
#include <string>

#include "brave/browser/ui/webui/basic_ui.h"

class PrefChangeRegistrar;
Expand Down
2 changes: 2 additions & 0 deletions browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,8 @@ void CustomizeWebUIHTMLSource(const std::string &name,
}, {
std::string("adblock"), {
{ "adsBlocked", IDS_ADBLOCK_TOTAL_ADS_BLOCKED },
{ "customFiltersTitle", IDS_ADBLOCK_CUSTOM_FILTERS_TITLE },
{ "customFiltersInstructions", IDS_ADBLOCK_CUSTOM_FILTERS_INSTRUCTIONS }, // NOLINT
{ "regionalAdblockEnabledTitle", IDS_ADBLOCK_REGIONAL_AD_BLOCK_ENABLED_TITLE}, // NOLINT
{ "regionalAdblockEnabled", IDS_ADBLOCK_REGIONAL_AD_BLOCK_ENABLED },
{ "regionalAdblockDisabled", IDS_ADBLOCK_REGIONAL_AD_BLOCK_DISABLED },
Expand Down
1 change: 1 addition & 0 deletions common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const char kLastCheckMonth[] = "brave.stats.last_check_month";
const char kFirstCheckMade[] = "brave.stats.first_check_made";
const char kWeekOfInstallation[] = "brave.stats.week_of_installation";
const char kAdBlockCurrentRegion[] = "brave.ad_block.current_region";
const char kAdBlockCustomFilters[] = "brave.ad_block.custom_filters";
const char kWidevineOptedIn[] = "brave.widevine_opted_in";
const char kWidevineInstalledVersion[] = "brave.widevine_installed_version";
const char kUseAlternativeSearchEngineProvider[] =
Expand Down
1 change: 1 addition & 0 deletions common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern const char kLastCheckMonth[];
extern const char kFirstCheckMade[];
extern const char kWeekOfInstallation[];
extern const char kAdBlockCurrentRegion[];
extern const char kAdBlockCustomFilters[];
extern const char kWidevineOptedIn[];
extern const char kWidevineInstalledVersion[];
extern const char kUseAlternativeSearchEngineProvider[];
Expand Down
1 change: 1 addition & 0 deletions components/brave_adblock_ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ transpile_web_ui("brave_adblock_ui") {
"store.ts",
"actions/adblock_actions.ts",
"components/app.tsx",
"components/customFilters.tsx",
"components/numBlockedStat.tsx",
"components/regionalAdBlockEnabled.tsx",
"constants/adblock_types.ts",
Expand Down
12 changes: 12 additions & 0 deletions components/brave_adblock_ui/actions/adblock_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,16 @@ import { action } from 'typesafe-actions'
// Constants
import { types } from '../constants/adblock_types'

export const getCustomFilters = () => action(types.ADBLOCK_GET_CUSTOM_FILTERS)

export const onGetCustomFilters = (customFilters: string) =>
action(types.ADBLOCK_ON_GET_CUSTOM_FILTERS, {
customFilters
})

export const statsUpdated = () => action(types.ADBLOCK_STATS_UPDATED)

export const updateCustomFilters = (customFilters: string) =>
action(types.ADBLOCK_UPDATE_CUSTOM_FILTERS, {
customFilters
})
12 changes: 12 additions & 0 deletions components/brave_adblock_ui/brave_adblock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ import * as adblockActions from './actions/adblock_actions'
window.cr.define('brave_adblock', function () {
'use strict'

function getCustomFilters () {
const actions = bindActionCreators(adblockActions, store.dispatch.bind(store))
actions.getCustomFilters()
}

function initialize () {
getCustomFilters()
render(
<Provider store={store}>
<App />
Expand All @@ -26,13 +32,19 @@ window.cr.define('brave_adblock', function () {
window.i18nTemplate.process(window.document, window.loadTimeData)
}

function onGetCustomFilters (customFilters: string) {
const actions = bindActionCreators(adblockActions, store.dispatch.bind(store))
actions.onGetCustomFilters(customFilters)
}

function statsUpdated () {
const actions = bindActionCreators(adblockActions, store.dispatch.bind(store))
actions.statsUpdated()
}

return {
initialize,
onGetCustomFilters,
statsUpdated
}
})
Expand Down
Loading

0 comments on commit 4760679

Please sign in to comment.