-
Notifications
You must be signed in to change notification settings - Fork 901
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 #316 from brave/tor-extension
Implement Tor client updater as component extension
- Loading branch information
Showing
19 changed files
with
481 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* 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/extensions/brave_component_extension.h" | ||
|
||
#include <string> | ||
|
||
#include "base/bind.h" | ||
#include "base/bind_helpers.h" | ||
#include "base/callback.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::OnDemandUpdater::Priority::FOREGROUND, | ||
component_updater::Callback()); | ||
} | ||
|
||
BraveComponentExtension::BraveComponentExtension() { | ||
} | ||
|
||
BraveComponentExtension::~BraveComponentExtension() { | ||
} | ||
|
||
void BraveComponentExtension::Register( | ||
const std::string& component_name, | ||
const std::string& component_id, | ||
const std::string& component_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(&BraveComponentExtension::OnComponentRegistered, | ||
base::Unretained(this), component_id_); | ||
ReadyCallback ready_callback = | ||
base::Bind(&BraveComponentExtension::OnComponentReady, | ||
base::Unretained(this), component_id_); | ||
brave::RegisterComponent(g_browser_process->component_updater(), | ||
component_name_, component_base64_public_key_, | ||
registered_callback, ready_callback); | ||
} | ||
|
||
// static | ||
bool BraveComponentExtension::Unregister(const std::string& component_id) { | ||
return g_browser_process->component_updater()->UnregisterComponent( | ||
component_id); | ||
} | ||
|
||
void BraveComponentExtension::OnComponentRegistered(const std::string& component_id) { | ||
OnDemandUpdate(g_browser_process->component_updater(), component_id); | ||
} | ||
|
||
void BraveComponentExtension::OnComponentReady( | ||
const std::string& component_id, | ||
const base::FilePath& install_dir) { | ||
} |
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,40 @@ | ||
/* 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_BROWSER_EXTENSIONS_BRAVE_COMPONENT_EXTENSION_H_ | ||
#define BRAVE_BROWSER_EXTENSIONS_BRAVE_COMPONENT_EXTENSION_H_ | ||
|
||
#include "base/files/file_path.h" | ||
#include "components/component_updater/component_updater_service.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); | ||
}; | ||
|
||
class BraveComponentExtension : public ComponentsUI { | ||
public: | ||
BraveComponentExtension(); | ||
virtual ~BraveComponentExtension(); | ||
void Register(const std::string& component_name, | ||
const std::string& component_id, | ||
const std::string& component_base64_public_key); | ||
static bool Unregister(const std::string& component_id); | ||
|
||
protected: | ||
virtual void OnComponentRegistered(const std::string& component_id); | ||
virtual void OnComponentReady(const std::string& component_id, | ||
const base::FilePath& install_dir); | ||
|
||
private: | ||
std::string component_name_; | ||
std::string component_id_; | ||
std::string component_base64_public_key_; | ||
}; | ||
|
||
#endif // BRAVE_BROWSER_EXTENSIONS_BRAVE_COMPONENT_EXTENSION_H_ |
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,84 @@ | ||
/* 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/extensions/brave_tor_client_updater.h" | ||
|
||
#include "base/files/file_enumerator.h" | ||
#include "base/files/file_path.h" | ||
#include "base/task_scheduler/post_task.h" | ||
#include "third_party/re2/src/re2/re2.h" | ||
|
||
namespace extensions { | ||
|
||
std::string BraveTorClientUpdater::g_tor_client_component_id_( | ||
kTorClientComponentId); | ||
std::string BraveTorClientUpdater::g_tor_client_component_base64_public_key_( | ||
kTorClientComponentBase64PublicKey); | ||
|
||
BraveTorClientUpdater::BraveTorClientUpdater() | ||
: task_runner_( | ||
base::CreateSequencedTaskRunnerWithTraits({base::MayBlock()})), | ||
registered_(false) { | ||
} | ||
|
||
BraveTorClientUpdater::~BraveTorClientUpdater() { | ||
} | ||
|
||
void BraveTorClientUpdater::Register() { | ||
if (registered_) | ||
return; | ||
|
||
BraveComponentExtension::Register(kTorClientComponentName, | ||
g_tor_client_component_id_, | ||
g_tor_client_component_base64_public_key_); | ||
registered_ = true; | ||
} | ||
|
||
base::FilePath BraveTorClientUpdater::GetExecutablePath() const { | ||
return executable_path_; | ||
} | ||
|
||
void BraveTorClientUpdater::InitExecutablePath( | ||
const base::FilePath& install_dir) { | ||
base::FileEnumerator traversal(install_dir, false, | ||
base::FileEnumerator::FILES, | ||
FILE_PATH_LITERAL("tor-*")); | ||
for (base::FilePath current = traversal.Next(); !current.empty(); | ||
current = traversal.Next()) { | ||
base::FileEnumerator::FileInfo file_info = traversal.GetInfo(); | ||
if (RE2::FullMatch(file_info.GetName().MaybeAsASCII(), | ||
"tor-\\d+\\.\\d+\\.\\d+\\.\\d+-\\w+-brave-\\d+")) { | ||
executable_path_ = current; | ||
return; | ||
} | ||
} | ||
|
||
LOG(ERROR) << "Failed to locate Tor client executable in " | ||
<< install_dir.value().c_str(); | ||
} | ||
|
||
void BraveTorClientUpdater::OnComponentReady( | ||
const std::string& component_id, | ||
const base::FilePath& install_dir) { | ||
GetTaskRunner()->PostTask( | ||
FROM_HERE, base::Bind(&BraveTorClientUpdater::InitExecutablePath, | ||
base::Unretained(this), install_dir)); | ||
} | ||
|
||
// static | ||
void BraveTorClientUpdater::SetComponentIdAndBase64PublicKeyForTest( | ||
const std::string& component_id, | ||
const std::string& component_base64_public_key) { | ||
g_tor_client_component_id_ = component_id; | ||
g_tor_client_component_base64_public_key_ = component_base64_public_key; | ||
} | ||
|
||
/////////////////////////////////////////////////////////////////////////////// | ||
|
||
// The Brave Tor client extension factory. | ||
std::unique_ptr<BraveTorClientUpdater> BraveTorClientUpdaterFactory() { | ||
return std::make_unique<BraveTorClientUpdater>(); | ||
} | ||
|
||
} // namespace extensions |
Oops, something went wrong.