Skip to content

Commit 581b1b4

Browse files
committed
Use extension for data file updates
1 parent f711cc0 commit 581b1b4

13 files changed

+330
-85
lines changed

browser/extensions/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ source_set("extensions") {
88
"api/content_settings/brave_content_settings_store.h",
99
"brave_component_extension_resource_manager.cc",
1010
"brave_component_extension_resource_manager.h",
11+
"brave_component_installer.cc",
12+
"brave_component_installer.h",
1113
"brave_component_loader.cc",
1214
"brave_component_loader.h",
1315
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// Copyright 2014 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "brave/browser/extensions/brave_component_installer.h"
6+
7+
#include <utility>
8+
9+
#include "base/base64.h"
10+
#include "base/bind.h"
11+
#include "base/bind_helpers.h"
12+
#include "base/files/file_path.h"
13+
#include "base/files/file_util.h"
14+
#include "base/json/json_string_value_serializer.h"
15+
#include "base/macros.h"
16+
#include "base/values.h"
17+
#include "base/version.h"
18+
#include "components/component_updater/component_updater_service.h"
19+
#include "components/crx_file/id_util.h"
20+
#include "components/update_client/update_client.h"
21+
#include "components/update_client/update_client_errors.h"
22+
#include "components/update_client/utils.h"
23+
#include "crypto/sha2.h"
24+
#include "extensions/common/constants.h"
25+
#include "extensions/common/manifest_constants.h"
26+
27+
using component_updater::ComponentUpdateService;
28+
29+
namespace {
30+
31+
using Result = update_client::CrxInstaller::Result;
32+
using InstallError = update_client::InstallError;
33+
} // namespace
34+
35+
namespace {
36+
bool RewriteManifestFile(
37+
const base::FilePath& extension_root,
38+
const base::DictionaryValue& manifest,
39+
const std::string &public_key) {
40+
41+
// Add the public key
42+
DCHECK(!public_key.empty());
43+
44+
std::unique_ptr<base::DictionaryValue> final_manifest(manifest.DeepCopy());
45+
final_manifest->SetString(extensions::manifest_keys::kPublicKey, public_key);
46+
47+
std::string manifest_json;
48+
JSONStringValueSerializer serializer(&manifest_json);
49+
serializer.set_pretty_print(true);
50+
if (!serializer.Serialize(*final_manifest)) {
51+
return false;
52+
}
53+
54+
base::FilePath manifest_path =
55+
extension_root.Append(extensions::kManifestFilename);
56+
int size = base::checked_cast<int>(manifest_json.size());
57+
if (base::WriteFile(manifest_path, manifest_json.data(), size) != size) {
58+
return false;
59+
}
60+
return true;
61+
}
62+
63+
} // namespace
64+
65+
namespace brave {
66+
67+
BraveComponentInstallerPolicy::BraveComponentInstallerPolicy(
68+
const std::string& name,
69+
const std::string& public_key,
70+
const ReadyCallback& ready_callback) :
71+
name_(name), public_key_(public_key), ready_callback_(ready_callback) {
72+
}
73+
74+
BraveComponentInstallerPolicy::~BraveComponentInstallerPolicy() {}
75+
76+
bool BraveComponentInstallerPolicy::VerifyInstallation(const base::DictionaryValue& manifest,
77+
const base::FilePath& install_dir) const {
78+
// The manifest file will generate a random ID if we don't provide one.
79+
// We want to write one with the actual extensions public key so we get
80+
// the same extensionID which is generated from the public key.
81+
std::string base64_public_key;
82+
base::Base64Encode(public_key_, &base64_public_key);
83+
if (!RewriteManifestFile(install_dir, manifest, base64_public_key)) {
84+
return false;
85+
}
86+
return base::PathExists(
87+
install_dir.Append(FILE_PATH_LITERAL("manifest.json")));
88+
}
89+
90+
bool BraveComponentInstallerPolicy::SupportsGroupPolicyEnabledComponentUpdates() const {
91+
return false;
92+
}
93+
94+
bool BraveComponentInstallerPolicy::RequiresNetworkEncryption() const {
95+
return false;
96+
}
97+
98+
update_client::CrxInstaller::Result BraveComponentInstallerPolicy::OnCustomInstall(
99+
const base::DictionaryValue& manifest,
100+
const base::FilePath& install_dir) {
101+
return Result(InstallError::NONE);
102+
}
103+
104+
void BraveComponentInstallerPolicy::OnCustomUninstall() {
105+
}
106+
107+
void BraveComponentInstallerPolicy::ComponentReady(
108+
const base::Version& version,
109+
const base::FilePath& install_dir,
110+
std::unique_ptr<base::DictionaryValue> manifest) {
111+
ready_callback_.Run(install_dir);
112+
}
113+
114+
base::FilePath BraveComponentInstallerPolicy::GetRelativeInstallDir() const {
115+
// Get the extension ID from the public key
116+
std::string extension_id = crx_file::id_util::GenerateId(public_key_);
117+
return base::FilePath(
118+
// Convert to wstring or string depending on OS
119+
base::FilePath::StringType(extension_id.begin(), extension_id.end()));
120+
}
121+
122+
void BraveComponentInstallerPolicy::GetHash(std::vector<uint8_t>* hash) const {
123+
const std::string public_key_sha256 = crypto::SHA256HashString(public_key_);
124+
hash->assign(public_key_sha256.begin(), public_key_sha256.end());
125+
}
126+
127+
std::string BraveComponentInstallerPolicy::GetName() const {
128+
return name_;
129+
}
130+
131+
std::vector<std::string> BraveComponentInstallerPolicy::GetMimeTypes() const {
132+
return std::vector<std::string>();
133+
}
134+
135+
update_client::InstallerAttributes BraveComponentInstallerPolicy::GetInstallerAttributes() const {
136+
return update_client::InstallerAttributes();
137+
}
138+
139+
void RegisterComponent(
140+
ComponentUpdateService* cus,
141+
const std::string& name,
142+
const std::string& public_key,
143+
const base::Closure& registered_callback,
144+
const ReadyCallback& ready_callback) {
145+
auto installer = base::MakeRefCounted<component_updater::ComponentInstaller>(
146+
std::make_unique<BraveComponentInstallerPolicy>(name, public_key, ready_callback));
147+
installer->Register(cus, registered_callback);
148+
}
149+
150+
} // namespace brave
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2014 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef COMPONENTS_COMPONENT_UPDATER_BRAVE_COMPONENT_INSTALLER_H_
6+
#define COMPONENTS_COMPONENT_UPDATER_BRAVE_COMPONENT_INSTALLER_H_
7+
8+
#include <stdint.h>
9+
10+
#include <memory>
11+
#include <string>
12+
#include <vector>
13+
14+
#include "base/bind.h"
15+
#include "base/files/file_path.h"
16+
#include "base/macros.h"
17+
#include "components/component_updater/component_installer.h"
18+
#include "components/update_client/update_client.h"
19+
20+
namespace base {
21+
class FilePath;
22+
} // namespace base
23+
using ReadyCallback = base::Callback<void(const base::FilePath&)>;
24+
25+
namespace brave {
26+
27+
class BraveComponentInstallerPolicy :
28+
public component_updater::ComponentInstallerPolicy {
29+
public:
30+
explicit BraveComponentInstallerPolicy(const std::string& name,
31+
const std::string &public_key,
32+
const ReadyCallback& ready_callback);
33+
34+
~BraveComponentInstallerPolicy() override;
35+
36+
private:
37+
// The following methods override ComponentInstallerPolicy
38+
bool VerifyInstallation(const base::DictionaryValue& manifest,
39+
const base::FilePath& install_dir) const override;
40+
bool SupportsGroupPolicyEnabledComponentUpdates() const override;
41+
bool RequiresNetworkEncryption() const override;
42+
update_client::CrxInstaller::Result OnCustomInstall(
43+
const base::DictionaryValue& manifest,
44+
const base::FilePath& install_dir) override;
45+
void OnCustomUninstall() override;
46+
void ComponentReady(
47+
const base::Version& version,
48+
const base::FilePath& install_dir,
49+
std::unique_ptr<base::DictionaryValue> manifest) override;
50+
base::FilePath GetRelativeInstallDir() const override;
51+
void GetHash(std::vector<uint8_t>* hash) const override;
52+
std::string GetName() const override;
53+
std::vector<std::string> GetMimeTypes() const override;
54+
update_client::InstallerAttributes GetInstallerAttributes() const override;
55+
56+
std::string name_;
57+
std::string public_key_;
58+
ReadyCallback ready_callback_;
59+
60+
DISALLOW_COPY_AND_ASSIGN(BraveComponentInstallerPolicy);
61+
};
62+
63+
void RegisterComponent(component_updater::ComponentUpdateService* cus,
64+
const std::string& name,
65+
const std::string& public_key,
66+
const base::Closure& registered_callback,
67+
const ReadyCallback& ready_callback);
68+
69+
} // namespace brave
70+
71+
#endif // COMPONENTS_COMPONENT_UPDATER_BRAVE_COMPONENT_INSTALLER_H_

components/brave_shields/browser/BUILD.gn

-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ source_set("brave_shields") {
1414
"brave_resource_dispatcher_host_delegate.h",
1515
"dat_file_util.cc",
1616
"dat_file_util.h",
17-
"dat_file_web_request.cc",
18-
"dat_file_web_request.h",
1917
"https_everywhere_recently_used_cache.h",
2018
"https_everywhere_service.cc",
2119
"https_everywhere_service.h",

components/brave_shields/browser/ad_block_service.cc

+31-9
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,26 @@
1313
#include "base/bind.h"
1414
#include "base/bind_helpers.h"
1515
#include "base/callback.h"
16+
#include "base/files/file_path.h"
1617
#include "base/logging.h"
1718
#include "base/macros.h"
1819
#include "base/memory/ptr_util.h"
1920
#include "base/strings/utf_string_conversions.h"
2021
#include "base/threading/thread_restrictions.h"
22+
#include "brave/browser/extensions/brave_component_installer.h"
2123
#include "brave/components/brave_shields/browser/dat_file_util.h"
2224
#include "brave/vendor/ad-block/ad_block_client.h"
25+
#include "chrome/browser/browser_process.h"
26+
#include "components/component_updater/component_installer.h"
27+
#include "components/component_updater/component_updater_service.h"
2328

2429
#define DAT_FILE "ABPFilterParserData.dat"
25-
// TODO: Repalce this with the real version at runtime
26-
#define DAT_FILE_URL "https://s3.amazonaws.com/adblock-data/3/ABPFilterParserData.dat"
2730

2831
namespace brave_shields {
2932

30-
GURL AdBlockService::g_ad_block_url(DAT_FILE_URL);
33+
GURL AdBlockService::g_ad_block_url("");
3134

3235
AdBlockService::AdBlockService() :
33-
BaseBraveShieldsService(DAT_FILE, AdBlockService::g_ad_block_url),
3436
ad_block_client_(new AdBlockClient()) {
3537
}
3638

@@ -69,16 +71,36 @@ bool AdBlockService::ShouldStartRequest(const GURL& url,
6971
}
7072

7173
bool AdBlockService::Init() {
72-
if (!GetDATFileData(DAT_FILE, buffer_)) {
74+
base::Closure registered_callback =
75+
base::Bind(&AdBlockService::OnComponentRegistered,
76+
base::Unretained(this), kAdBlockPlusUpdaterId);
77+
ReadyCallback ready_callback =
78+
base::Bind(&AdBlockService::OnComponentReady,
79+
base::Unretained(this), kAdBlockPlusUpdaterId);
80+
brave::RegisterComponent(g_browser_process->component_updater(),
81+
kAdBlockPlusUpdaterName, kAdBlockPlusUpdaterPublicKeyStr,
82+
registered_callback, ready_callback);
83+
return true;
84+
}
85+
86+
void AdBlockService::OnComponentRegistered(const std::string& extension_id) {
87+
}
88+
89+
void AdBlockService::OnComponentReady(const std::string& extension_id,
90+
const base::FilePath& install_dir) {
91+
if (!GetDATFileData(install_dir, DAT_FILE, buffer_)) {
7392
LOG(ERROR) << "Could not obtain ad block data file";
74-
return false;
93+
return;
94+
}
95+
if (buffer_.empty()) {
96+
LOG(ERROR) << "Could not obtain ad block data";
97+
return;
7598
}
7699
if (!ad_block_client_->deserialize((char*)&buffer_.front())) {
77100
ad_block_client_.reset();
78-
LOG(ERROR) << "AdBlockService::InitAdBlock deserialize failed";
79-
return false;
101+
LOG(ERROR) << "Failed to deserialize ad block data";
102+
return;
80103
}
81-
return true;
82104
}
83105

84106
// static

components/brave_shields/browser/ad_block_service.h

+51
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,58 @@
1212
#include <vector>
1313
#include <mutex>
1414

15+
#include "base/files/file_path.h"
1516
#include "brave/components/brave_shields/browser/base_brave_shields_service.h"
1617
#include "content/public/common/resource_type.h"
1718

1819
class AdBlockClient;
1920
class AdBlockServiceTest;
2021

22+
const uint8_t kAdBlockPlusUpdaterPublicKey[294] = {
23+
0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09,
24+
0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
25+
0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
26+
0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01,
27+
0x00, 0xb3, 0x4a, 0xb3, 0x26, 0x61, 0xd2, 0x80,
28+
0x88, 0xb0, 0xec, 0x81, 0x85, 0x0b, 0x18, 0xa3,
29+
0xd4, 0xd9, 0xc1, 0xe6, 0x12, 0x79, 0x65, 0x0d,
30+
0x4b, 0x29, 0x6f, 0x61, 0xb4, 0xbe, 0x04, 0x04,
31+
0xe6, 0x8c, 0x26, 0xf9, 0xaa, 0x68, 0x3b, 0x3f,
32+
0xc1, 0xfa, 0xb4, 0x9f, 0x0f, 0xe0, 0xa0, 0x11,
33+
0xd6, 0xb2, 0xaa, 0xa4, 0x30, 0x1c, 0xa7, 0x45,
34+
0x3b, 0xb6, 0x1f, 0x1c, 0x62, 0xae, 0xf7, 0x5b,
35+
0x72, 0xe8, 0x27, 0xd4, 0x6a, 0x32, 0xb3, 0x94,
36+
0x0a, 0x3e, 0x00, 0x2f, 0x2b, 0x00, 0xc7, 0x26,
37+
0x9f, 0x63, 0x96, 0x70, 0x03, 0x5a, 0x6e, 0x71,
38+
0x42, 0xf3, 0xd0, 0xe0, 0x61, 0x35, 0x98, 0xad,
39+
0x7f, 0x23, 0x79, 0x1a, 0xf5, 0xc1, 0x0e, 0x0b,
40+
0x33, 0xd6, 0xe8, 0x62, 0x3e, 0xc0, 0x62, 0xe2,
41+
0x54, 0xfc, 0x7d, 0x55, 0x9a, 0xf1, 0x1c, 0x7b,
42+
0x12, 0x04, 0x82, 0x51, 0x68, 0x40, 0x79, 0x4f,
43+
0x28, 0xb9, 0x6e, 0x3d, 0x02, 0x67, 0xe9, 0x1c,
44+
0x7c, 0xc9, 0xd1, 0x4e, 0xc5, 0xc8, 0x07, 0x0d,
45+
0xc2, 0xeb, 0x12, 0x60, 0x09, 0x52, 0x88, 0x49,
46+
0x05, 0x24, 0x77, 0xc9, 0x9a, 0xc1, 0x5b, 0xc6,
47+
0xbe, 0xa3, 0xa1, 0x04, 0xea, 0x20, 0x09, 0xd6,
48+
0xfd, 0xab, 0x36, 0xb9, 0x78, 0x9d, 0x42, 0x83,
49+
0x1f, 0x11, 0x58, 0xab, 0x9c, 0x15, 0xf6, 0x96,
50+
0x5b, 0x12, 0x8b, 0x39, 0xe9, 0x42, 0x88, 0x13,
51+
0x13, 0xd4, 0x79, 0xb5, 0xce, 0x70, 0x28, 0x54,
52+
0x11, 0xc6, 0xfd, 0xb9, 0x61, 0x24, 0x93, 0x83,
53+
0xe4, 0x77, 0x07, 0xfe, 0x17, 0x91, 0xac, 0x1e,
54+
0x0c, 0xbe, 0xb1, 0x35, 0x27, 0xbe, 0x96, 0x17,
55+
0x77, 0xef, 0x67, 0x4d, 0xda, 0x1d, 0x57, 0x27,
56+
0x94, 0xbb, 0x50, 0x7a, 0x23, 0x4d, 0x39, 0x9e,
57+
0xd5, 0x6c, 0x10, 0xd7, 0x76, 0x27, 0xbc, 0x36,
58+
0x89, 0x9f, 0xfd, 0x0b, 0x30, 0x6c, 0x11, 0x9a,
59+
0x57, 0x02, 0x03, 0x01, 0x00, 0x01
60+
};
61+
62+
const std::string kAdBlockPlusUpdaterPublicKeyStr(
63+
(const char *)kAdBlockPlusUpdaterPublicKey, sizeof(kAdBlockPlusUpdaterPublicKey));
64+
const std::string kAdBlockPlusUpdaterId("cffkpbalmllkdoenhmdmpbkajipdjfam");
65+
const std::string kAdBlockPlusUpdaterName("AdBlock Plus Updater");
66+
2167
namespace brave_shields {
2268

2369
// The brave shields service in charge of ad-block checking and init.
@@ -35,6 +81,11 @@ class AdBlockService : public BaseBraveShieldsService {
3581
void Cleanup() override;
3682

3783
private:
84+
void OnComponentRegistered(const std::string& extension_id);
85+
void OnComponentReady(
86+
const std::string& extension_id,
87+
const base::FilePath& install_dir);
88+
3889
friend class ::AdBlockServiceTest;
3990
static GURL g_ad_block_url;
4091
static void SetAdBlockURLForTest(const GURL& url);

0 commit comments

Comments
 (0)