Skip to content

Commit

Permalink
Reload adblocker on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyZhukovsky authored and samartnik committed Mar 14, 2018
1 parent 412016a commit 5adc8ba
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ public class ADBlockUpdater {
private static final String TAG = "ADBLOCK";
private static Semaphore mAvailable = new Semaphore(1);
private static List<String> mWhitelistedRegionalLocales = Arrays.asList("ru", "uk", "be", "hi");
private static boolean receivedAnUpdate = false;
private static final String UPDATE_ADBLOCKER = "update_adblocker";

public static void UpdateADBlock(Context context, boolean initShieldsConfig) {
try {
mAvailable.acquire();
try {
receivedAnUpdate = false;
DownloadTrackingProtectionData(context);
DownloadAdBlockData(context);
DownloadAdBlockRegionalData(context);
Expand All @@ -38,8 +41,12 @@ public static void UpdateADBlock(Context context, boolean initShieldsConfig) {
}
}
DownloadHTTPSData(context);
//new DownloadAdBlockTrackingProtectionDataAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
//new DownloadHTTPSDataAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
if (!initShieldsConfig && receivedAnUpdate) {
// Don't set an update flag on initial download
ContextUtils.getAppSharedPreferences().edit()
.putBoolean(UPDATE_ADBLOCKER, true)
.apply();
}
} finally {
mAvailable.release();
}
Expand All @@ -58,6 +65,7 @@ private static void DownloadTrackingProtectionData(Context context) {
ADBlockUtils.TRACKING_PROTECTION_LOCALFILENAME_DOWNLOADED, false)) {
ADBlockUtils.CreateDownloadedFile(context, ADBlockUtils.TRACKING_PROTECTION_LOCALFILENAME,
verNumber, ADBlockUtils.TRACKING_PROTECTION_LOCALFILENAME_DOWNLOADED, false);
receivedAnUpdate = true;
}
}

Expand All @@ -72,6 +80,7 @@ private static void DownloadAdBlockData(Context context) {
ADBlockUtils.ADBLOCK_LOCALFILENAME_DOWNLOADED, false)) {
ADBlockUtils.CreateDownloadedFile(context, ADBlockUtils.ADBLOCK_LOCALFILENAME,
verNumber, ADBlockUtils.ADBLOCK_LOCALFILENAME_DOWNLOADED, false);
receivedAnUpdate = true;
}
}

Expand All @@ -86,6 +95,7 @@ private static void DownloadAdBlockRegionalData(Context context) {
List<String> files = filesSt.uuid;
boolean changePreference = true;
for (int i = 0; i < files.size(); i ++) {
receivedAnUpdate = true;
if (!ADBlockUtils.CreateDownloadedFile(context, files.get(i) + ".dat",
verNumber, ADBlockUtils.ADBLOCK_REGIONAL_LOCALFILENAME_DOWNLOADED, i != 0) && 0 == i) {
changePreference = false;
Expand Down Expand Up @@ -125,6 +135,7 @@ private static void DownloadHTTPSData(Context context) {
ADBlockUtils.ETAG_PREPEND_HTTPS, verNumber,
ADBlockUtils.HTTPS_LOCALFILENAME_DOWNLOADED_NEW, true)) {
// Make temporary several attempts because it fails on unzipping sometimes
receivedAnUpdate = true;
boolean unzipped = false;
for (int i = 0; i < 5; i++) {
unzipped = ADBlockUtils.UnzipFile(ADBlockUtils.HTTPS_LOCALFILENAME_NEW, verNumber, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class ShieldsConfig {
private static final String PREF_FINGERPRINTING_PROTECTION = "fingerprinting_protection";
private static final String TAG = "ShieldsConfig";
private static final String SHIELDS_CONFIG_LOCALFILENAME = "shields_config.dat";
private static final String UPDATE_ADBLOCKER = "update_adblocker";
// The format is (<top shields switch>,<ads and tracking switch>,<HTTPSE switch>,<JavaScript switch>,<3rd party cookies switch><Fingerprinting switch>)
// We handle JavaScript blocking by internal implementation of Chromium, but save the state here also
private static final String ALL_SHIELDS_DEFAULT_MASK = "1,1,1,0,1,0";
Expand Down Expand Up @@ -573,6 +574,18 @@ public void setBlockedCountInfo(String url, int trackersBlocked, int adsBlocked,
}
}

@CalledByNative
public boolean needUpdateAdBlocker() {
return ContextUtils.getAppSharedPreferences().getBoolean(UPDATE_ADBLOCKER, false);
}

@CalledByNative
public void resetUpdateAdBlockerFlag() {
ContextUtils.getAppSharedPreferences().edit()
.putBoolean(UPDATE_ADBLOCKER, false)
.apply();
}

private void updateBraveStats(int trackersBlocked, int adsBlocked, int httpsUpgrades) {
long trackersBlockedCount = mSharedPreferences.getLong(PREF_TRACKERS_BLOCKED_COUNT, 0) + trackersBlocked;
long adsBlockedCount = mSharedPreferences.getLong(PREF_ADS_BLOCKED_COUNT, 0) + adsBlocked;
Expand Down
6 changes: 6 additions & 0 deletions chrome/browser/io_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,12 @@ void IOThread::Init() {
ct_tree_tracker_.get());
}

std::shared_ptr<net::blockers::BlockersWorker> IOThread::ResetBlockersWorker() {
globals_->blockers_worker_.reset(new net::blockers::BlockersWorker());

return globals_->blockers_worker_;
}

void IOThread::CleanUp() {
base::debug::LeakTracker<SafeBrowsingURLRequestContext>::CheckForLeaks();

Expand Down
2 changes: 2 additions & 0 deletions chrome/browser/io_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ class IOThread : public content::BrowserThreadDelegate {
network::mojom::NetworkService* GetNetworkServiceOnUIThread();

certificate_transparency::TreeStateTracker* ct_tree_tracker() const;

std::shared_ptr<net::blockers::BlockersWorker> ResetBlockersWorker();

private:
// BrowserThreadDelegate implementation, runs on the IO thread.
Expand Down
11 changes: 11 additions & 0 deletions chrome/browser/net/blockers/shields_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ void ShieldsConfig::setBlockedCountInfo(const std::string& url, int trackersBloc
jurl, trackersBlocked, adsBlocked, httpsUpgrades, scriptsBlocked, fingerprintingBlocked);
}

bool ShieldsConfig::needUpdateAdBlocker() {
JNIEnv* env = base::android::AttachCurrentThread();

return Java_ShieldsConfig_needUpdateAdBlocker(env, weak_java_shields_config_.get(env));
}

void ShieldsConfig::resetUpdateAdBlockerFlag() {
JNIEnv* env = base::android::AttachCurrentThread();
Java_ShieldsConfig_resetUpdateAdBlockerFlag(env, weak_java_shields_config_.get(env));
}

ShieldsConfig* ShieldsConfig::getShieldsConfig() {
return gShieldsConfig;
}
Expand Down
2 changes: 2 additions & 0 deletions chrome/browser/net/blockers/shields_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class ShieldsConfig {
std::string getHostSettings(const bool &incognitoTab, const std::string& host);
void setBlockedCountInfo(const std::string& url, int trackersBlocked, int adsBlocked, int httpsUpgrades,
int scriptsBlocked, int fingerprintingBlocked);
bool needUpdateAdBlocker();
void resetUpdateAdBlockerFlag();

static ShieldsConfig* getShieldsConfig();
// Register the ShieldsConfig's native methods through JNI.
Expand Down
57 changes: 56 additions & 1 deletion chrome/browser/net/chrome_network_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/sequenced_task_runner.h"
#include "base/task_scheduler/post_task.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
Expand Down Expand Up @@ -270,6 +272,7 @@ ChromeNetworkDelegate::ChromeNetworkDelegate(
switches::kEnableExperimentalWebPlatformFeatures)),
data_use_aggregator_(nullptr),
is_data_usage_off_the_record_(true),
reload_adblocker_(false),
incognito_(false) {
DCHECK(enable_referrers);
extensions_delegate_.reset(
Expand Down Expand Up @@ -457,10 +460,62 @@ int ChromeNetworkDelegate::OnBeforeURLRequest_PreBlockersWork(
ctx->trackersBlocked = 0;
ctx->httpsUpgrades = 0;

int rv = OnBeforeURLRequest_TpBlockPreFileWork(request, callback, new_url, ctx);
int rv = net::ERR_IO_PENDING;
if (reload_adblocker_) {
reload_adblocker_ = false;
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&ChromeNetworkDelegate::GetIOThread,
base::Unretained(this), base::Unretained(request), callback, new_url, ctx));
if (nullptr != shieldsConfig) {
shieldsConfig->resetUpdateAdBlockerFlag();
}
ctx->pendingAtLeastOnce = true;
pending_requests_->Insert(request->identifier());
} else {
rv = OnBeforeURLRequest_TpBlockPreFileWork(request, callback, new_url, ctx);
// Check do we need to reload adblocker. We will do that on next call
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::Bind(base::IgnoreResult(&ChromeNetworkDelegate::CheckAdBlockerReload),
base::Unretained(this), shieldsConfig));
}

return rv;
}

void ChromeNetworkDelegate::CheckAdBlockerReload(net::blockers::ShieldsConfig* shields_config) {
if (nullptr == shields_config) {
return;
}
reload_adblocker_ = shields_config->needUpdateAdBlocker();
}

void ChromeNetworkDelegate::GetIOThread(net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url,
std::shared_ptr<OnBeforeURLRequestContext> ctx) {
scoped_refptr<base::SequencedTaskRunner> task_runner =
base::CreateSequencedTaskRunnerWithTraits(
{base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN});
task_runner->PostTask(FROM_HERE, base::Bind(&ChromeNetworkDelegate::ResetBlocker,
base::Unretained(this), g_browser_process->io_thread(), base::Unretained(request), callback,
new_url, ctx));
}

void ChromeNetworkDelegate::ResetBlocker(IOThread* io_thread, net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url,
std::shared_ptr<OnBeforeURLRequestContext> ctx) {
blockers_worker_ = io_thread->ResetBlockersWorker();

content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::Bind(base::IgnoreResult(&ChromeNetworkDelegate::OnBeforeURLRequest_TpBlockPostFileWork),
base::Unretained(this), base::Unretained(request), callback, new_url, ctx)
);
}

int ChromeNetworkDelegate::OnBeforeURLRequest_TpBlockPreFileWork(
net::URLRequest* request,
const net::CompletionCallback& callback,
Expand Down
13 changes: 13 additions & 0 deletions chrome/browser/net/chrome_network_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ namespace net {
class URLRequest;
namespace blockers {
class BlockersWorker;
class ShieldsConfig;
}
}

struct OnBeforeURLRequestContext;
class PendingRequests;
class IOThread;

// ChromeNetworkDelegate is the central point from within the chrome code to
// add hooks into the network stack.
Expand All @@ -69,6 +71,16 @@ class ChromeNetworkDelegate : public net::NetworkDelegateImpl {
// Pass through to ChromeExtensionsNetworkDelegate::set_extension_info_map().
void set_extension_info_map(extensions::InfoMap* extension_info_map);

void ResetBlocker(IOThread* io_thread, net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url,
std::shared_ptr<OnBeforeURLRequestContext> ctx);
void GetIOThread(net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url,
std::shared_ptr<OnBeforeURLRequestContext> ctx);
void CheckAdBlockerReload(net::blockers::ShieldsConfig* shields_config);

// If |profile| is nullptr or not set, events will be broadcast to all
// profiles, otherwise they will only be sent to the specified profile.
// Also pass through to ChromeExtensionsNetworkDelegate::set_profile().
Expand Down Expand Up @@ -312,6 +324,7 @@ class ChromeNetworkDelegate : public net::NetworkDelegateImpl {

// Blockers
std::shared_ptr<net::blockers::BlockersWorker> blockers_worker_;
bool reload_adblocker_;

// (TODO)find a better way to handle last first party
GURL last_first_party_url_;
Expand Down

0 comments on commit 5adc8ba

Please sign in to comment.