Skip to content

Commit

Permalink
Call OnDemandUpdate to force component install once component is regi…
Browse files Browse the repository at this point in the history
…stered
  • Loading branch information
emerick committed Apr 26, 2018
1 parent da8ae29 commit 20df890
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 112 deletions.
29 changes: 13 additions & 16 deletions components/brave_shields/browser/ad_block_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@

namespace brave_shields {

std::string AdBlockService::g_ad_block_updater_id_(
kAdBlockUpdaterId);
std::string AdBlockService::g_ad_block_updater_base64_public_key_(
kAdBlockUpdaterBase64PublicKey);
std::string AdBlockService::g_ad_block_component_id_(
kAdBlockComponentId);
std::string AdBlockService::g_ad_block_component_base64_public_key_(
kAdBlockComponentBase64PublicKey);

AdBlockService::AdBlockService()
: BaseBraveShieldsService(kAdBlockUpdaterName,
g_ad_block_updater_id_,
g_ad_block_updater_base64_public_key_),
: BaseBraveShieldsService(kAdBlockComponentName,
g_ad_block_component_id_,
g_ad_block_component_base64_public_key_),
ad_block_client_(new AdBlockClient()) {
}

Expand Down Expand Up @@ -73,10 +73,7 @@ bool AdBlockService::Init() {
return true;
}

void AdBlockService::OnComponentRegistered(const std::string& extension_id) {
}

void AdBlockService::OnComponentReady(const std::string& extension_id,
void AdBlockService::OnComponentReady(const std::string& component_id,
const base::FilePath& install_dir) {
base::FilePath dat_file_path = install_dir.AppendASCII(DAT_FILE);
if (!GetDATFileData(dat_file_path, buffer_)) {
Expand All @@ -96,11 +93,11 @@ void AdBlockService::OnComponentReady(const std::string& extension_id,
}

// static
void AdBlockService::SetIdAndBase64PublicKeyForTest(
const std::string& id,
const std::string& base64_public_key) {
g_ad_block_updater_id_ = id;
g_ad_block_updater_base64_public_key_ = base64_public_key;
void AdBlockService::SetComponentIdAndBase64PublicKeyForTest(
const std::string& component_id,
const std::string& component_base64_public_key) {
g_ad_block_component_id_ = component_id;
g_ad_block_component_base64_public_key_ = component_base64_public_key;
}

///////////////////////////////////////////////////////////////////////////////
Expand Down
19 changes: 9 additions & 10 deletions components/brave_shields/browser/ad_block_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class AdBlockServiceTest;

namespace brave_shields {

const std::string kAdBlockUpdaterName("Brave Ad Block Updater");
const std::string kAdBlockUpdaterId("cffkpbalmllkdoenhmdmpbkajipdjfam");
const std::string kAdBlockComponentName("Brave Ad Block Updater");
const std::string kAdBlockComponentId("cffkpbalmllkdoenhmdmpbkajipdjfam");

const std::string kAdBlockUpdaterBase64PublicKey =
const std::string kAdBlockComponentBase64PublicKey =
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs0qzJmHSgIiw7IGFCxij"
"1NnB5hJ5ZQ1LKW9htL4EBOaMJvmqaDs/wfq0nw/goBHWsqqkMBynRTu2Hxxirvdb"
"cugn1Goys5QKPgAvKwDHJp9jlnADWm5xQvPQ4GE1mK1/I3ka9cEOCzPW6GI+wGLi"
Expand All @@ -46,17 +46,16 @@ class AdBlockService : public BaseBraveShieldsService {
protected:
bool Init() override;
void Cleanup() override;
void OnComponentRegistered(const std::string& extension_id) override;
void OnComponentReady(const std::string& extension_id,
void OnComponentReady(const std::string& component_id,
const base::FilePath& install_dir) override;

private:
friend class ::AdBlockServiceTest;
static std::string g_ad_block_updater_id_;
static std::string g_ad_block_updater_base64_public_key_;
static void SetIdAndBase64PublicKeyForTest(
const std::string& id,
const std::string& base64_public_key);
static std::string g_ad_block_component_id_;
static std::string g_ad_block_component_base64_public_key_;
static void SetComponentIdAndBase64PublicKeyForTest(
const std::string& component_id,
const std::string& component_base64_public_key);

std::vector<unsigned char> buffer_;
std::unique_ptr<AdBlockClient> ad_block_client_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
const char kAdsPage[] = "/blocking.html";
const char kNoAdsPage[] = "/no_blocking.html";

const std::string kAdBlockUpdaterTestId("naccapggpomhlhoifnlebfoocegenbol");
const std::string kAdBlockComponentTestId("naccapggpomhlhoifnlebfoocegenbol");

const std::string kAdBlockUpdaterTestBase64PublicKey =
const std::string kAdBlockComponentTestBase64PublicKey =
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtV7Vr69kkvSvu2lhcMDh"
"j4Jm3FKU1zpUkALaum5719/cccVvGpMKKFyy4WYXsmAfcIONmGO4ThK/q6jkgC5v"
"8HrkjPOf7HHebKEnsJJucz/Z1t6dq0CE+UA2IWfbGfFM4nJ8AKIv2gqiw2d4ydAs"
Expand Down Expand Up @@ -51,8 +51,8 @@ class AdBlockServiceTest : public ExtensionBrowserTest {
}

void InitService() {
brave_shields::AdBlockService::SetIdAndBase64PublicKeyForTest(
kAdBlockUpdaterTestId, kAdBlockUpdaterTestBase64PublicKey);
brave_shields::AdBlockService::SetComponentIdAndBase64PublicKeyForTest(
kAdBlockComponentTestId, kAdBlockComponentTestBase64PublicKey);
}

bool InstallAdBlockExtension() {
Expand Down
36 changes: 26 additions & 10 deletions components/brave_shields/browser/base_brave_shields_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,36 @@
#include "base/task_runner_util.h"
#include "base/task_scheduler/post_task.h"
#include "base/threading/thread_restrictions.h"
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/browser/component_updater/brave_component_installer.h"
#include "chrome/browser/browser_process.h"

void ComponentsUI::OnDemandUpdate(
component_updater::ComponentUpdateService* cus,
const std::string& component_id) {
cus->GetOnDemandUpdater().OnDemandUpdate(component_id,
component_updater::Callback());
}

namespace brave_shields {

BaseBraveShieldsService::BaseBraveShieldsService(
const std::string& updater_name,
const std::string& updater_id,
const std::string& updater_base64_public_key)
const std::string& component_name,
const std::string& component_id,
const std::string& component_base64_public_key)
: initialized_(false),
task_runner_(
base::CreateSequencedTaskRunnerWithTraits({base::MayBlock()})),
updater_name_(updater_name),
updater_id_(updater_id),
updater_base64_public_key_(updater_base64_public_key) {
component_name_(component_name),
component_id_(component_id),
component_base64_public_key_(component_base64_public_key) {
base::Closure registered_callback =
base::Bind(&BaseBraveShieldsService::OnComponentRegistered,
base::Unretained(this), updater_id_);
base::Unretained(this), component_id_);
ReadyCallback ready_callback =
base::Bind(&BaseBraveShieldsService::OnComponentReady,
base::Unretained(this), updater_id_);
base::Unretained(this), component_id_);
brave::RegisterComponent(g_browser_process->component_updater(),
updater_name_, updater_base64_public_key_,
component_name_, component_base64_public_key_,
registered_callback, ready_callback);
}

Expand Down Expand Up @@ -81,4 +88,13 @@ bool BaseBraveShieldsService::ShouldStartRequest(const GURL& url,
return true;
}

void BaseBraveShieldsService::OnComponentRegistered(const std::string& component_id) {
OnDemandUpdate(g_browser_process->component_updater(), component_id);
}

void BaseBraveShieldsService::OnComponentReady(
const std::string& component_id,
const base::FilePath& install_dir) {
}

} // namespace brave_shields
31 changes: 21 additions & 10 deletions components/brave_shields/browser/base_brave_shields_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,29 @@

#include "base/files/file_path.h"
#include "base/sequenced_task_runner.h"
#include "components/component_updater/component_updater_service.h"
#include "content/public/common/resource_type.h"
#include "url/gurl.h"

// Just used to give access to OnDemandUpdater since it's private.
// Chromium has ComponentsUI which is a friend class, so we just
// do this hack here to gain access.
class ComponentsUI {
public:
void OnDemandUpdate(
component_updater::ComponentUpdateService* cus,
const std::string& component_id);
};

namespace brave_shields {

// The brave shields service in charge of checking brave shields like ad-block,
// tracking protection, etc.
class BaseBraveShieldsService {
class BaseBraveShieldsService : public ComponentsUI {
public:
BaseBraveShieldsService(const std::string& updater_name,
const std::string& updater_id,
const std::string& updater_base64_public_key);
BaseBraveShieldsService(const std::string& component_name,
const std::string& component_id,
const std::string& component_base64_public_key);
virtual ~BaseBraveShieldsService();
bool Start();
void Stop();
Expand All @@ -40,19 +51,19 @@ class BaseBraveShieldsService {
protected:
virtual bool Init() = 0;
virtual void Cleanup() = 0;
virtual void OnComponentRegistered(const std::string& extension_id) = 0;
virtual void OnComponentReady(const std::string& extension_id,
const base::FilePath& install_dir) = 0;
virtual void OnComponentRegistered(const std::string& component_id);
virtual void OnComponentReady(const std::string& component_id,
const base::FilePath& install_dir);

private:
void InitShields();

bool initialized_;
std::mutex initialized_mutex_;
scoped_refptr<base::SequencedTaskRunner> task_runner_;
std::string updater_name_;
std::string updater_id_;
std::string updater_base64_public_key_;
std::string component_name_;
std::string component_id_;
std::string component_base64_public_key_;
};

} // namespace brave_shields
Expand Down
29 changes: 13 additions & 16 deletions components/brave_shields/browser/https_everywhere_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ namespace {

namespace brave_shields {

std::string HTTPSEverywhereService::g_https_everywhere_updater_id_(
kHTTPSEverywhereUpdaterId);
std::string HTTPSEverywhereService::g_https_everywhere_updater_base64_public_key_(
kHTTPSEverywhereUpdaterBase64PublicKey);
std::string HTTPSEverywhereService::g_https_everywhere_component_id_(
kHTTPSEverywhereComponentId);
std::string HTTPSEverywhereService::g_https_everywhere_component_base64_public_key_(
kHTTPSEverywhereComponentBase64PublicKey);

HTTPSEverywhereService::HTTPSEverywhereService()
: BaseBraveShieldsService(kHTTPSEverywhereUpdaterName,
g_https_everywhere_updater_id_,
g_https_everywhere_updater_base64_public_key_),
: BaseBraveShieldsService(kHTTPSEverywhereComponentName,
g_https_everywhere_component_id_,
g_https_everywhere_component_base64_public_key_),
level_db_(nullptr) {
}

Expand All @@ -99,10 +99,7 @@ bool HTTPSEverywhereService::Init() {
return true;
}

void HTTPSEverywhereService::OnComponentRegistered(const std::string& extension_id) {
}

void HTTPSEverywhereService::OnComponentReady(const std::string& extension_id,
void HTTPSEverywhereService::OnComponentReady(const std::string& component_id,
const base::FilePath& install_dir) {
base::FilePath zip_db_file_path = install_dir.AppendASCII(DAT_FILE);

Expand Down Expand Up @@ -349,11 +346,11 @@ void HTTPSEverywhereService::CloseDatabase()
}

// static
void HTTPSEverywhereService::SetIdAndBase64PublicKeyForTest(
const std::string& id,
const std::string& base64_public_key) {
g_https_everywhere_updater_id_ = id;
g_https_everywhere_updater_base64_public_key_ = base64_public_key;
void HTTPSEverywhereService::SetComponentIdAndBase64PublicKeyForTest(
const std::string& component_id,
const std::string& component_base64_public_key) {
g_https_everywhere_component_id_ = component_id;
g_https_everywhere_component_base64_public_key_ = component_base64_public_key;
}

///////////////////////////////////////////////////////////////////////////////
Expand Down
19 changes: 9 additions & 10 deletions components/brave_shields/browser/https_everywhere_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class HTTPSEverywhereServiceTest;

namespace brave_shields {

const std::string kHTTPSEverywhereUpdaterName("Brave HTTPS Everywhere Updater");
const std::string kHTTPSEverywhereUpdaterId("oofiananboodjbbmdelgdommihjbkfag");
const std::string kHTTPSEverywhereComponentName("Brave HTTPS Everywhere Updater");
const std::string kHTTPSEverywhereComponentId("oofiananboodjbbmdelgdommihjbkfag");

const std::string kHTTPSEverywhereUpdaterBase64PublicKey =
const std::string kHTTPSEverywhereComponentBase64PublicKey =
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvn9zSMjTmhkQyrZu5UdN"
"350nPqLoSeCYngcC7yDFwaUHjoBQXCZqGeDC69ciCQ2mlRhcV2nxXqlUDkiC6+7m"
"651nI+gi4oVqHagc7EFUyGA0yuIk7qIMvCBdH7wbET27de0rzbRzRht9EKzEjIhC"
Expand Down Expand Up @@ -61,8 +61,7 @@ class HTTPSEverywhereService : public BaseBraveShieldsService {
protected:
bool Init() override;
void Cleanup() override;
void OnComponentRegistered(const std::string& extension_id) override;
void OnComponentReady(const std::string& extension_id,
void OnComponentReady(const std::string& component_id,
const base::FilePath& install_dir) override;

void AddHTTPSEUrlToRedirectList(const uint64_t& request_id);
Expand All @@ -73,11 +72,11 @@ class HTTPSEverywhereService : public BaseBraveShieldsService {

private:
friend class ::HTTPSEverywhereServiceTest;
static std::string g_https_everywhere_updater_id_;
static std::string g_https_everywhere_updater_base64_public_key_;
static void SetIdAndBase64PublicKeyForTest(
const std::string& id,
const std::string& base64_public_key);
static std::string g_https_everywhere_component_id_;
static std::string g_https_everywhere_component_base64_public_key_;
static void SetComponentIdAndBase64PublicKeyForTest(
const std::string& component_id,
const std::string& component_base64_public_key);

void CloseDatabase();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#include "content/public/test/browser_test_utils.h"
#include "net/dns/mock_host_resolver.h"

const std::string kHTTPSEverywhereUpdaterTestId("bhlmpjhncoojbkemjkeppfahkglffilp");
const std::string kHTTPSEverywhereComponentTestId("bhlmpjhncoojbkemjkeppfahkglffilp");

const std::string kHTTPSEverywhereUpdaterTestBase64PublicKey =
const std::string kHTTPSEverywhereComponentTestBase64PublicKey =
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3tAm7HooTNVGQ9cm7Yuc"
"M9sLM/V38JOXzdj7z9dyDIfO64N69Gr5dn3XRzLuD+Pyzpl8MzfY/tIbWNSw3I2a"
"8YcEPmyHl2L4HByKTm+eJ02ArhtkgtZKjiTDc84KQcsTBHqINkMUQYeUN3VW1lz2"
Expand Down Expand Up @@ -59,9 +59,10 @@ class HTTPSEverywhereServiceTest : public ExtensionBrowserTest {
}

void InitService() {
brave_shields::HTTPSEverywhereService::SetIdAndBase64PublicKeyForTest(
kHTTPSEverywhereUpdaterTestId,
kHTTPSEverywhereUpdaterTestBase64PublicKey);
brave_shields::HTTPSEverywhereService::
SetComponentIdAndBase64PublicKeyForTest(
kHTTPSEverywhereComponentTestId,
kHTTPSEverywhereComponentTestBase64PublicKey);
}

bool InstallHTTPSEverywhereExtension() {
Expand Down
Loading

0 comments on commit 20df890

Please sign in to comment.