Skip to content

Commit

Permalink
Merge pull request #119 from brave/regional-ad-block
Browse files Browse the repository at this point in the history
Add support for regional Ad Block lists
  • Loading branch information
emerick authored Jun 4, 2018
2 parents 723e32e + 42fba3f commit 2242c6c
Show file tree
Hide file tree
Showing 25 changed files with 555 additions and 106 deletions.
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@504ae10e475005c559253846fba1c06e5a0186b4",
"vendor/ad-block": "https://github.com/brave/ad-block.git@11a4617c8bf3f52668e76cd4d165629294d9cc18",
"vendor/tracking-protection": "https://github.com/brave/tracking-protection.git@051177425a14121a22087d754ad8eb1c0ce8fb24",
"vendor/hashset-cpp": "https://github.com/brave/hashset-cpp.git@67ffffa69b56e330bab9d08f050727f891c916a1",
"vendor/bloom-filter-cpp": "https://github.com/brave/bloom-filter-cpp.git@d511cf872ea1d650ab8dc4662f6036dac012d197",
Expand Down
10 changes: 10 additions & 0 deletions browser/brave_browser_process_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "brave/browser/component_updater/brave_component_updater_configurator.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/https_everywhere_service.h"
#include "brave/components/brave_shields/browser/tracking_protection_service.h"
#include "chrome/browser/io_thread.h"
Expand Down Expand Up @@ -51,6 +52,15 @@ BraveBrowserProcessImpl::ad_block_service() {
return ad_block_service_.get();
}

brave_shields::AdBlockRegionalService*
BraveBrowserProcessImpl::ad_block_regional_service() {
if (ad_block_regional_service_)
return ad_block_regional_service_.get();

ad_block_regional_service_ = brave_shields::AdBlockRegionalServiceFactory();
return ad_block_regional_service_.get();
}

brave_shields::TrackingProtectionService*
BraveBrowserProcessImpl::tracking_protection_service() {
if (tracking_protection_service_)
Expand Down
4 changes: 4 additions & 0 deletions browser/brave_browser_process_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace brave_shields {
class AdBlockService;
class AdBlockRegionalService;
class HTTPSEverywhereService;
class TrackingProtectionService;
}
Expand All @@ -22,11 +23,14 @@ class BraveBrowserProcessImpl : public BrowserProcessImpl {
component_updater::ComponentUpdateService* component_updater() override;

brave_shields::AdBlockService* ad_block_service();
brave_shields::AdBlockRegionalService* ad_block_regional_service();
brave_shields::TrackingProtectionService* tracking_protection_service();
brave_shields::HTTPSEverywhereService* https_everywhere_service();

private:
std::unique_ptr<brave_shields::AdBlockService> ad_block_service_;
std::unique_ptr<brave_shields::AdBlockRegionalService>
ad_block_regional_service_;
std::unique_ptr<brave_shields::TrackingProtectionService>
tracking_protection_service_;
std::unique_ptr<brave_shields::HTTPSEverywhereService>
Expand Down
1 change: 1 addition & 0 deletions common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ const char kTrackersBlocked[] = "brave.stats.trackers_blocked";
const char kJavascriptBlocked[] = "brave.stats.javascript_blocked";
const char kHttpsUpgrades[] = "brave.stats.https_upgrades";
const char kFingerprintingBlocked[] = "brave.stats.fingerprinting_blocked";
const char kAdBlockCurrentRegion[] = "brave.ad_block.current_region";
1 change: 1 addition & 0 deletions common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ extern const char kTrackersBlocked[];
extern const char kJavascriptBlocked[];
extern const char kHttpsUpgrades[];
extern const char kFingerprintingBlocked[];
extern const char kAdBlockCurrentRegion[];

#endif
4 changes: 4 additions & 0 deletions components/brave_shields/browser/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
source_set("brave_shields") {
sources = [
"ad_block_base_service.cc",
"ad_block_base_service.h",
"ad_block_regional_service.cc",
"ad_block_regional_service.h",
"ad_block_service.cc",
"ad_block_service.h",
"base_brave_shields_service.cc",
Expand Down
92 changes: 92 additions & 0 deletions components/brave_shields/browser/ad_block_base_service.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* 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/components/brave_shields/browser/ad_block_base_service.h"

#include <algorithm>
#include <string>
#include <utility>
#include <vector>

#include "base/base_paths.h"
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
#include "brave/components/brave_shields/browser/dat_file_util.h"
#include "brave/vendor/ad-block/ad_block_client.h"

namespace brave_shields {

AdBlockBaseService::AdBlockBaseService()
: BaseBraveShieldsService(),
ad_block_client_(new AdBlockClient()),
weak_factory_(this) {
}

AdBlockBaseService::~AdBlockBaseService() {
Cleanup();
}

void AdBlockBaseService::Cleanup() {
ad_block_client_.reset();
}

bool AdBlockBaseService::ShouldStartRequest(const GURL& url,
content::ResourceType resource_type,
const std::string& tab_host) {

FilterOption current_option = FONoFilterOption;
content::ResourceType internalResource = (content::ResourceType)resource_type;
if (content::RESOURCE_TYPE_STYLESHEET == internalResource) {
current_option = FOStylesheet;
} else if (content::RESOURCE_TYPE_IMAGE == internalResource) {
current_option = FOImage;
} else if (content::RESOURCE_TYPE_SCRIPT == internalResource) {
current_option = FOScript;
}

if (ad_block_client_->matches(url.spec().c_str(),
current_option,
tab_host.c_str())) {
// LOG(ERROR) << "AdBlockBaseService::ShouldStartRequest(), host: " << tab_host
// << ", resource type: " << resource_type
// << ", url.spec(): " << url.spec();
return false;
}

return true;
}

void AdBlockBaseService::GetDATFileData(const base::FilePath& dat_file_path) {
GetTaskRunner()->PostTaskAndReply(
FROM_HERE,
base::Bind(&brave_shields::GetDATFileData, dat_file_path, &buffer_),
base::Bind(&AdBlockBaseService::OnDATFileDataReady,
weak_factory_.GetWeakPtr()));
}

void AdBlockBaseService::OnDATFileDataReady() {
if (buffer_.empty()) {
LOG(ERROR) << "Could not obtain ad block data";
return;
}
ad_block_client_.reset(new AdBlockClient());
if (!ad_block_client_->deserialize((char*)&buffer_.front())) {
ad_block_client_.reset();
LOG(ERROR) << "Failed to deserialize ad block data";
return;
}
}

bool AdBlockBaseService::Init() {
return true;
}

///////////////////////////////////////////////////////////////////////////////

} // namespace brave_shields
54 changes: 54 additions & 0 deletions components/brave_shields/browser/ad_block_base_service.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* 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_COMPONENTS_BRAVE_SHIELDS_BROWSER_AD_BLOCK_BASE_SERVICE_H_
#define BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_AD_BLOCK_BASE_SERVICE_H_

#include <stdint.h>

#include <memory>
#include <string>
#include <vector>

#include "base/files/file_path.h"
#include "base/memory/weak_ptr.h"
#include "brave/components/brave_shields/browser/base_brave_shields_service.h"
#include "brave/components/brave_shields/browser/dat_file_util.h"
#include "content/public/common/resource_type.h"

class AdBlockClient;

namespace brave_shields {

// The base class of the brave shields service in charge of ad-block
// checking and init.
class AdBlockBaseService : public BaseBraveShieldsService {
public:
AdBlockBaseService();
~AdBlockBaseService() override;

bool ShouldStartRequest(const GURL &url,
content::ResourceType resource_type,
const std::string& tab_host) override;

protected:
bool Init() override;
void Cleanup() override;

void GetDATFileData(const base::FilePath& dat_file_path);

std::unique_ptr<AdBlockClient> ad_block_client_;
DATFileDataBuffer buffer_;

private:
void OnDATFileDataReady();

base::WeakPtrFactory<AdBlockBaseService> weak_factory_;

DISALLOW_COPY_AND_ASSIGN(AdBlockBaseService);
};

} // namespace brave_shields

#endif // BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_AD_BLOCK_BASE_SERVICE_H_
125 changes: 125 additions & 0 deletions components/brave_shields/browser/ad_block_regional_service.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/* 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/components/brave_shields/browser/ad_block_regional_service.h"

#include <algorithm>
#include <string>
#include <utility>
#include <vector>

#include "base/base_paths.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/common/pref_names.h"
#include "brave/vendor/ad-block/ad_block_client.h"
#include "brave/vendor/ad-block/lists/regions.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "components/prefs/pref_service.h"

namespace {

std::vector<FilterList>::const_iterator FindFilterListByLocale(const std::string& locale) {
return std::find_if(region_lists.begin(), region_lists.end(),
[&locale](const FilterList& filter_list) {
return std::find_if(filter_list.langs.begin(),
filter_list.langs.end(),
[locale](const std::string& lang) {
return lang == locale;
}) != filter_list.langs.end();
});
}

} // namespace

namespace brave_shields {

std::string AdBlockRegionalService::g_ad_block_regional_component_id_;
std::string AdBlockRegionalService::g_ad_block_regional_component_base64_public_key_;

AdBlockRegionalService::AdBlockRegionalService() {
}

AdBlockRegionalService::~AdBlockRegionalService() {
}

bool AdBlockRegionalService::ShouldStartRequest(const GURL& url,
content::ResourceType resource_type,
const std::string& tab_host) {
return AdBlockBaseService::ShouldStartRequest(url, resource_type, tab_host);
}

bool AdBlockRegionalService::UnregisterComponentByLocale(const std::string& locale) {
auto it = FindFilterListByLocale(locale);
if (it == region_lists.end())
return false;
return Unregister(it->component_id);

}

bool AdBlockRegionalService::Init() {
auto it =
FindFilterListByLocale(g_brave_browser_process->GetApplicationLocale());
if (it == region_lists.end())
return false;

uuid_ = it->uuid;

Register(it->title,
!g_ad_block_regional_component_id_.empty()
? g_ad_block_regional_component_id_
: it->component_id,
!g_ad_block_regional_component_base64_public_key_.empty()
? g_ad_block_regional_component_base64_public_key_
: it->base64_public_key);

return true;
}

void AdBlockRegionalService::OnComponentRegistered(
const std::string& component_id) {
PrefService* prefs = ProfileManager::GetActiveUserProfile()->GetPrefs();
std::string ad_block_current_region = prefs->GetString(kAdBlockCurrentRegion);
std::string locale = g_brave_browser_process->GetApplicationLocale();
if (!ad_block_current_region.empty() && ad_block_current_region != locale)
UnregisterComponentByLocale(ad_block_current_region);
prefs->SetString(kAdBlockCurrentRegion, locale);
AdBlockBaseService::OnComponentRegistered(component_id);
}

void AdBlockRegionalService::OnComponentReady(
const std::string& component_id,
const base::FilePath& install_dir) {
base::FilePath dat_file_path =
install_dir.AppendASCII(uuid_).AddExtension(FILE_PATH_LITERAL(".dat"));
AdBlockBaseService::GetDATFileData(dat_file_path);
}

// static
bool AdBlockRegionalService::IsSupportedLocale(const std::string& locale) {
return (FindFilterListByLocale(locale) != region_lists.end());
}

// static
void AdBlockRegionalService::SetComponentIdAndBase64PublicKeyForTest(
const std::string& component_id,
const std::string& component_base64_public_key) {
g_ad_block_regional_component_id_ = component_id;
g_ad_block_regional_component_base64_public_key_ = component_base64_public_key;
}

///////////////////////////////////////////////////////////////////////////////

// The brave shields factory. Using the Brave Shields as a singleton
// is the job of the browser process.
std::unique_ptr<AdBlockRegionalService> AdBlockRegionalServiceFactory() {
return base::MakeUnique<AdBlockRegionalService>();
}

} // namespace brave_shields
Loading

0 comments on commit 2242c6c

Please sign in to comment.