-
Notifications
You must be signed in to change notification settings - Fork 904
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from brave/regional-ad-block
Add support for regional Ad Block lists
- Loading branch information
Showing
25 changed files
with
555 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
125
components/brave_shields/browser/ad_block_regional_service.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.