Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use extension for data file updates #76

Merged
merged 11 commits into from
Apr 28, 2018
Merged
7 changes: 6 additions & 1 deletion browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ source_set("browser_process") {
"brave_tab_helpers.h",
"brave_profile_prefs.cc",
"brave_profile_prefs.h",
"component_updater/brave_component_installer.cc",
"component_updater/brave_component_installer.h",
"component_updater/brave_component_updater_configurator.cc",
"component_updater/brave_component_updater_configurator.h",
"importer/brave_external_process_importer_client.cc",
"importer/brave_external_process_importer_client.h",
"importer/brave_external_process_importer_host.cc",
"importer/brave_external_process_importer_host.h",
"importer/brave_in_process_importer_bridge.cc",
"importer/brave_in_process_importer_bridge.h",
"importer/brave_profile_writer.cc",
"importer/brave_profile_writer.h",
"importer/brave_profile_writer.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing commas are OK for future ref in .gn files.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not just ok, trailing commas are preferred

]

deps = [
"//components/component_updater",
"//content/public/browser",
"//brave/chromium_src:browser",
]
Expand Down
26 changes: 24 additions & 2 deletions browser/brave_browser_process_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

#include "brave/browser/brave_browser_process_impl.h"

#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/https_everywhere_service.h"
#include "brave/components/brave_shields/browser/tracking_protection_service.h"
#include "chrome/browser/io_thread.h"
#include "components/component_updater/component_updater_service.h"
#include "content/public/browser/browser_thread.h"

BraveBrowserProcessImpl* g_brave_browser_process = nullptr;

using content::BrowserThread;

BraveBrowserProcessImpl::~BraveBrowserProcessImpl() {
}
Expand All @@ -21,7 +26,24 @@ BraveBrowserProcessImpl::BraveBrowserProcessImpl(
g_brave_browser_process = this;
}

brave_shields::BaseBraveShieldsService*
component_updater::ComponentUpdateService*
BraveBrowserProcessImpl::component_updater() {
if (component_updater_)
return component_updater_.get();

if (!BrowserThread::CurrentlyOn(BrowserThread::UI))
return nullptr;

component_updater_ = component_updater::ComponentUpdateServiceFactory(
component_updater::MakeBraveComponentUpdaterConfigurator(
base::CommandLine::ForCurrentProcess(),
io_thread()->system_url_request_context_getter(),
g_browser_process->local_state()));

return component_updater_.get();
}

brave_shields::AdBlockService*
BraveBrowserProcessImpl::ad_block_service() {
if (ad_block_service_)
return ad_block_service_.get();
Expand All @@ -30,7 +52,7 @@ BraveBrowserProcessImpl::ad_block_service() {
return ad_block_service_.get();
}

brave_shields::BaseBraveShieldsService*
brave_shields::TrackingProtectionService*
BraveBrowserProcessImpl::tracking_protection_service() {
if (tracking_protection_service_)
return tracking_protection_service_.get();
Expand Down
18 changes: 10 additions & 8 deletions browser/brave_browser_process_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,26 @@
#include "chrome/browser/browser_process_impl.h"

namespace brave_shields {
class BaseBraveShieldsService;
class AdBlockService;
class HTTPSEverywhereService;
class TrackingProtectionService;
}

class BraveBrowserProcessImpl : public BrowserProcessImpl {
public:
BraveBrowserProcessImpl(base::SequencedTaskRunner* local_state_task_runner);
~BraveBrowserProcessImpl() override;

brave_shields::BaseBraveShieldsService* ad_block_service();
brave_shields::BaseBraveShieldsService*
tracking_protection_service();
brave_shields::HTTPSEverywhereService*
https_everywhere_service();
// BrowserProcess implementation.
component_updater::ComponentUpdateService* component_updater() override;

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

private:
std::unique_ptr<brave_shields::BaseBraveShieldsService> ad_block_service_;
std::unique_ptr<brave_shields::BaseBraveShieldsService>
std::unique_ptr<brave_shields::AdBlockService> ad_block_service_;
std::unique_ptr<brave_shields::TrackingProtectionService>
tracking_protection_service_;
std::unique_ptr<brave_shields::HTTPSEverywhereService>
https_everywhere_service_;
Expand Down
149 changes: 149 additions & 0 deletions browser/component_updater/brave_component_installer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/* 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/browser/component_updater/brave_component_installer.h"

#include <utility>

#include "base/base64.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/json/json_string_value_serializer.h"
#include "base/macros.h"
#include "base/values.h"
#include "base/version.h"
#include "components/component_updater/component_updater_service.h"
#include "components/crx_file/id_util.h"
#include "components/update_client/update_client.h"
#include "components/update_client/update_client_errors.h"
#include "components/update_client/utils.h"
#include "crypto/sha2.h"
#include "extensions/common/constants.h"
#include "extensions/common/manifest_constants.h"

namespace {
using Result = update_client::CrxInstaller::Result;
using InstallError = update_client::InstallError;
} // namespace

namespace {
bool RewriteManifestFile(
const base::FilePath& extension_root,
const base::DictionaryValue& manifest,
const std::string &public_key) {

// Add the public key
DCHECK(!public_key.empty());

std::unique_ptr<base::DictionaryValue> final_manifest(manifest.DeepCopy());
final_manifest->SetString(extensions::manifest_keys::kPublicKey, public_key);

std::string manifest_json;
JSONStringValueSerializer serializer(&manifest_json);
serializer.set_pretty_print(true);
if (!serializer.Serialize(*final_manifest)) {
return false;
}

base::FilePath manifest_path =
extension_root.Append(extensions::kManifestFilename);
int size = base::checked_cast<int>(manifest_json.size());
if (base::WriteFile(manifest_path, manifest_json.data(), size) != size) {
return false;
}
return true;
}

} // namespace

namespace brave {

BraveComponentInstallerPolicy::BraveComponentInstallerPolicy(
const std::string& name,
const std::string& base64_public_key,
const ReadyCallback& ready_callback)
: name_(name),
base64_public_key_(base64_public_key),
ready_callback_(ready_callback) {
base::Base64Decode(base64_public_key, &public_key_);
}

BraveComponentInstallerPolicy::~BraveComponentInstallerPolicy() {}

bool BraveComponentInstallerPolicy::VerifyInstallation(
const base::DictionaryValue& manifest,
const base::FilePath& install_dir) const {
// The manifest file will generate a random ID if we don't provide one.
// We want to write one with the actual extensions public key so we get
// the same extensionID which is generated from the public key.
if (!RewriteManifestFile(install_dir, manifest, base64_public_key_)) {
return false;
}
return base::PathExists(
install_dir.Append(FILE_PATH_LITERAL("manifest.json")));
}

bool BraveComponentInstallerPolicy::SupportsGroupPolicyEnabledComponentUpdates() const {
return false;
}

bool BraveComponentInstallerPolicy::RequiresNetworkEncryption() const {
return false;
}

update_client::CrxInstaller::Result BraveComponentInstallerPolicy::OnCustomInstall(
const base::DictionaryValue& manifest,
const base::FilePath& install_dir) {
return Result(InstallError::NONE);
}

void BraveComponentInstallerPolicy::OnCustomUninstall() {
}

void BraveComponentInstallerPolicy::ComponentReady(
const base::Version& version,
const base::FilePath& install_dir,
std::unique_ptr<base::DictionaryValue> manifest) {
ready_callback_.Run(install_dir);
}

base::FilePath BraveComponentInstallerPolicy::GetRelativeInstallDir() const {
// Get the extension ID from the public key
std::string extension_id = crx_file::id_util::GenerateId(public_key_);
return base::FilePath(
// Convert to wstring or string depending on OS
base::FilePath::StringType(extension_id.begin(), extension_id.end()));
}

void BraveComponentInstallerPolicy::GetHash(std::vector<uint8_t>* hash) const {
const std::string public_key_sha256 = crypto::SHA256HashString(public_key_);
hash->assign(public_key_sha256.begin(), public_key_sha256.end());
}

std::string BraveComponentInstallerPolicy::GetName() const {
return name_;
}

std::vector<std::string> BraveComponentInstallerPolicy::GetMimeTypes() const {
return std::vector<std::string>();
}

update_client::InstallerAttributes BraveComponentInstallerPolicy::GetInstallerAttributes() const {
return update_client::InstallerAttributes();
}

void RegisterComponent(
component_updater::ComponentUpdateService* cus,
const std::string& name,
const std::string& base64_public_key,
const base::Closure& registered_callback,
const ReadyCallback& ready_callback) {
auto installer = base::MakeRefCounted<component_updater::ComponentInstaller>(
std::make_unique<BraveComponentInstallerPolicy>(name, base64_public_key, ready_callback));
installer->Register(cus, registered_callback);
}

} // namespace brave
73 changes: 73 additions & 0 deletions browser/component_updater/brave_component_installer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* 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_BRAVE_COMPONENT_INSTALLER_H_
#define BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_BRAVE_COMPONENT_INSTALLER_H_

#include <stdint.h>

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

#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "components/component_updater/component_installer.h"
#include "components/update_client/update_client.h"

namespace base {
class FilePath;
} // namespace base

using ReadyCallback = base::Callback<void(const base::FilePath&)>;

namespace brave {

class BraveComponentInstallerPolicy :
public component_updater::ComponentInstallerPolicy {
public:
explicit BraveComponentInstallerPolicy(const std::string& name,
const std::string& base64_public_key,
const ReadyCallback& ready_callback);

~BraveComponentInstallerPolicy() override;

private:
// The following methods override ComponentInstallerPolicy
bool VerifyInstallation(const base::DictionaryValue& manifest,
const base::FilePath& install_dir) const override;
bool SupportsGroupPolicyEnabledComponentUpdates() const override;
bool RequiresNetworkEncryption() const override;
update_client::CrxInstaller::Result OnCustomInstall(
const base::DictionaryValue& manifest,
const base::FilePath& install_dir) override;
void OnCustomUninstall() override;
void ComponentReady(
const base::Version& version,
const base::FilePath& install_dir,
std::unique_ptr<base::DictionaryValue> manifest) override;
base::FilePath GetRelativeInstallDir() const override;
void GetHash(std::vector<uint8_t>* hash) const override;
std::string GetName() const override;
std::vector<std::string> GetMimeTypes() const override;
update_client::InstallerAttributes GetInstallerAttributes() const override;

std::string name_;
std::string base64_public_key_;
std::string public_key_;
ReadyCallback ready_callback_;

DISALLOW_COPY_AND_ASSIGN(BraveComponentInstallerPolicy);
};

void RegisterComponent(component_updater::ComponentUpdateService* cus,
const std::string& name,
const std::string& base64_public_key,
const base::Closure& registered_callback,
const ReadyCallback& ready_callback);

} // namespace brave

#endif // BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_BRAVE_COMPONENT_INSTALLER_H_
Loading