Skip to content

Commit

Permalink
Merge pull request #3319 from brave/tor_profile_rework_take2_rebase
Browse files Browse the repository at this point in the history
Tor profile rework
  • Loading branch information
yrliou authored Oct 3, 2019
2 parents 249a03e + b1100ba commit 16b7cba
Show file tree
Hide file tree
Showing 61 changed files with 1,403 additions and 308 deletions.
8 changes: 8 additions & 0 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import("//brave/components/brave_webtorrent/browser/buildflags/buildflags.gni")
import("//brave/components/greaselion/browser/buildflags/buildflags.gni")
import("//build/buildflag_header.gni")
import("//build/config/features.gni")
import("//chrome/common/features.gni")
import("//extensions/buildflags/buildflags.gni")
import("//third_party/widevine/cdm/widevine.gni")
import("//third_party/protobuf/proto_library.gni")
Expand Down Expand Up @@ -81,6 +82,13 @@ source_set("browser_process") {
"update_util.h",
]

if (enable_tor) {
sources += [
# TODO(bridiver) - change the directory for these
"extensions/brave_tor_client_updater.cc",
"extensions/brave_tor_client_updater.h",
]
}
if (enable_sparkle) {
sources += [
"mac/sparkle_glue.mm",
Expand Down
28 changes: 27 additions & 1 deletion browser/brave_browser_main_parts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,43 @@
#include "brave/browser/brave_browser_main_parts.h"

#include "brave/browser/browsing_data/brave_clear_browsing_data.h"
#include "brave/browser/tor/buildflags.h"

#if BUILDFLAG(ENABLE_TOR)
#include "base/files/file_util.h"
#include "brave/common/tor/tor_constants.h"
#include "chrome/browser/browser_process_impl.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_metrics.h"
#endif

#if !defined(OS_ANDROID)
#include "brave/browser/infobars/brave_confirm_p3a_infobar_delegate.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "content/public/browser/web_contents.h"
#endif

#if BUILDFLAG(ENABLE_TOR) || !defined(OS_ANDROID)
#include "chrome/browser/browser_process.h"
#endif

void BraveBrowserMainParts::PostBrowserStart() {
ChromeBrowserMainParts::PostBrowserStart();

#if BUILDFLAG(ENABLE_TOR)
ProfileManager* profile_manager = g_browser_process->profile_manager();
base::FilePath tor_legacy_path =
profile_manager->user_data_dir().Append(tor::kTorProfileDir);

// Delete Tor legacy profile if exists.
if (base::PathExists(tor_legacy_path)) {
profile_manager->MaybeScheduleProfileForDeletion(
tor_legacy_path, base::DoNothing(),
ProfileMetrics::DELETE_PROFILE_SETTINGS);
}
#endif

#if !defined(OS_ANDROID)
Browser* browser = chrome::FindLastActive();
content::WebContents* active_web_contents = nullptr;
Expand Down
7 changes: 6 additions & 1 deletion browser/brave_browser_process_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#include "brave/browser/brave_stats_updater.h"
#include "brave/browser/component_updater/brave_component_updater_configurator.h"
#include "brave/browser/component_updater/brave_component_updater_delegate.h"
#include "brave/browser/extensions/brave_tor_client_updater.h"
#include "brave/browser/profiles/brave_profile_manager.h"
#include "brave/browser/tor/buildflags.h"
#include "brave/components/brave_component_updater/browser/local_data_files_service.h"
#include "brave/components/brave_shields/browser/ad_block_custom_filters_service.h"
#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h"
Expand All @@ -27,6 +27,7 @@
#include "brave/components/p3a/buildflags.h"
#include "brave/components/p3a/brave_histogram_rewrite.h"
#include "brave/components/p3a/brave_p3a_service.h"
#include "chrome/common/buildflags.h"
#include "chrome/common/chrome_paths.h"
#include "components/component_updater/component_updater_service.h"
#include "components/component_updater/timer_update_scheduler.h"
Expand All @@ -49,6 +50,10 @@
#include "brave/components/greaselion/browser/greaselion_download_service.h"
#endif

#if BUILDFLAG(ENABLE_TOR)
#include "brave/browser/extensions/brave_tor_client_updater.h"
#endif

#if defined(OS_ANDROID)
#include "chrome/browser/android/chrome_feature_list.h"
#include "chrome/browser/android/component_updater/background_task_update_scheduler.h"
Expand Down
7 changes: 4 additions & 3 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,10 @@ void BraveContentBrowserClient::AdjustUtilityServiceProcessCommandLine(
if (identity.name() == tor::mojom::kServiceName) {
base::FilePath path =
g_brave_browser_process->tor_client_updater()->GetExecutablePath();
DCHECK(!path.empty());
command_line->AppendSwitchPath(tor::switches::kTorExecutablePath,
path.BaseName());
if (!path.empty()) {
command_line->AppendSwitchPath(tor::switches::kTorExecutablePath,
path.BaseName());
}
}
#endif
}
Expand Down
4 changes: 2 additions & 2 deletions browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ source_set("extensions") {
"brave_extension_service.h",
"brave_extensions_browser_api_provider.cc",
"brave_extensions_browser_api_provider.h",
"brave_extensions_browser_client_impl.cc",
"brave_extensions_browser_client_impl.h",
"brave_theme_event_router.cc",
"brave_theme_event_router.h",
"brave_tor_client_updater.cc",
"brave_tor_client_updater.h",
"updater/brave_update_client_config.cc",
"updater/brave_update_client_config.h",
]
Expand Down
31 changes: 31 additions & 0 deletions browser/extensions/brave_extensions_browser_client_impl.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* 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_extensions_browser_client_impl.h"

#include <memory>

#include "brave/browser/extensions/brave_extensions_browser_api_provider.h"
#include "brave/browser/profiles/profile_util.h"
#include "components/prefs/pref_service.h"

namespace extensions {

BraveExtensionsBrowserClientImpl::BraveExtensionsBrowserClientImpl() {
BraveExtensionsBrowserClient::Set(this);
AddAPIProvider(std::make_unique<BraveExtensionsBrowserAPIProvider>());
}

bool BraveExtensionsBrowserClientImpl::AreExtensionsDisabled(
const base::CommandLine& command_line,
content::BrowserContext* context) {
if (brave::IsTorProfile(context))
return true;

return ChromeExtensionsBrowserClient::AreExtensionsDisabled(command_line,
context);
}

} // namespace extensions
28 changes: 28 additions & 0 deletions browser/extensions/brave_extensions_browser_client_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* 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_EXTENSIONS_BROWSER_CLIENT_IMPL_H_
#define BRAVE_BROWSER_EXTENSIONS_BRAVE_EXTENSIONS_BROWSER_CLIENT_IMPL_H_

#include "brave/extensions/browser/brave_extensions_browser_client.h"
#include "chrome/browser/extensions/chrome_extensions_browser_client.h"

namespace extensions {

class BraveExtensionsBrowserClientImpl : public ChromeExtensionsBrowserClient,
public BraveExtensionsBrowserClient {
public:
BraveExtensionsBrowserClientImpl();
~BraveExtensionsBrowserClientImpl() override = default;

bool AreExtensionsDisabled(const base::CommandLine& command_line,
content::BrowserContext* context) override;

DISALLOW_COPY_AND_ASSIGN(BraveExtensionsBrowserClientImpl);
};

} // namespace extensions

#endif // BRAVE_BROWSER_EXTENSIONS_BRAVE_EXTENSIONS_BROWSER_CLIENT_IMPL_H_
103 changes: 62 additions & 41 deletions browser/extensions/brave_tor_client_updater.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,53 @@
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/task/post_task.h"
#include "base/task_runner.h"
#include "base/task_runner_util.h"
#include "third_party/re2/src/re2/re2.h"

using brave_component_updater::BraveComponent;

namespace extensions {

namespace {

base::FilePath InitExecutablePath(const base::FilePath& install_dir) {
base::FilePath executable_path;
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+"))
continue;
executable_path = current;
break;
}

if (executable_path.empty()) {
LOG(ERROR) << "Failed to locate Tor client executable in "
<< install_dir.value().c_str();
return base::FilePath();
}

#if defined(OS_POSIX)
// Ensure that Tor client executable has appropriate file
// permissions, as CRX unzipping does not preserve them.
// See https://crbug.com/555011
if (!base::SetPosixFilePermissions(executable_path, 0755)) {
LOG(ERROR) << "Failed to set executable permission on "
<< executable_path.value().c_str();
return base::FilePath();
}
#endif // defined(OS_POSIX)

return executable_path;
}

} // namespace

#if defined(OS_WIN)
const char kTorClientComponentName[] = "Brave Tor Client Updater (Windows)";
const char kTorClientComponentId[] = "cpoalefficncklhjfpglfiplenlpccdb";
Expand Down Expand Up @@ -62,8 +103,8 @@ BraveTorClientUpdater::BraveTorClientUpdater(BraveComponent::Delegate* delegate)
: BraveComponent(delegate),
task_runner_(
base::CreateSequencedTaskRunnerWithTraits({base::MayBlock()})),
registered_(false) {
}
registered_(false),
weak_ptr_factory_(this) {}

BraveTorClientUpdater::~BraveTorClientUpdater() {
}
Expand All @@ -78,53 +119,33 @@ void BraveTorClientUpdater::Register() {
registered_ = true;
}

base::FilePath BraveTorClientUpdater::GetExecutablePath() const {
return executable_path_;
void BraveTorClientUpdater::SetExecutablePath(const base::FilePath& path) {
executable_path_ = path;
for (Observer& observer : observers_)
observer.OnExecutableReady(path);
}

void BraveTorClientUpdater::InitExecutablePath(
const base::FilePath& install_dir) {
base::FilePath executable_path;
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+"))
continue;
executable_path = current;
break;
}

if (executable_path.empty()) {
LOG(ERROR) << "Failed to locate Tor client executable in "
<< install_dir.value().c_str();
return;
}

#if defined(OS_POSIX)
// Ensure that Tor client executable has appropriate file
// permissions, as CRX unzipping does not preserve them.
// See https://crbug.com/555011
if (!base::SetPosixFilePermissions(executable_path, 0755)) {
LOG(ERROR) << "Failed to set executable permission on "
<< executable_path.value().c_str();
return;
}
#endif // defined(OS_POSIX)

executable_path_ = executable_path;
base::FilePath BraveTorClientUpdater::GetExecutablePath() const {
return executable_path_;
}

void BraveTorClientUpdater::OnComponentReady(
const std::string& component_id,
const base::FilePath& install_dir,
const std::string& manifest) {
GetTaskRunner()->PostTask(
FROM_HERE, base::Bind(&BraveTorClientUpdater::InitExecutablePath,
base::Unretained(this), install_dir));
base::PostTaskAndReplyWithResult(
GetTaskRunner().get(), FROM_HERE,
base::BindOnce(&InitExecutablePath, install_dir),
base::BindOnce(&BraveTorClientUpdater::SetExecutablePath,
weak_ptr_factory_.GetWeakPtr()));
}

void BraveTorClientUpdater::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}

void BraveTorClientUpdater::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}

// static
Expand Down
26 changes: 25 additions & 1 deletion browser/extensions/brave_tor_client_updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@
#include <string>

#include "base/files/file_path.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/sequenced_task_runner.h"
#include "brave/components/brave_component_updater/browser/brave_component.h"

class BraveProfileManagerTest;
class BraveProfileManagerExtensionTest;
class BraveTorClientUpdaterTest;
class SearchEngineProviderServiceTest;

using brave_component_updater::BraveComponent;

Expand All @@ -35,6 +40,14 @@ extern const char kTorClientComponentBase64PublicKey[];

class BraveTorClientUpdater : public BraveComponent {
public:
class Observer : public base::CheckedObserver {
public:
virtual void OnExecutableReady(const base::FilePath& path) = 0;

protected:
~Observer() override = default;
};

explicit BraveTorClientUpdater(BraveComponent::Delegate* delegate);
~BraveTorClientUpdater() override;

Expand All @@ -44,23 +57,34 @@ class BraveTorClientUpdater : public BraveComponent {
return task_runner_;
}

void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);

protected:
void OnComponentReady(const std::string& component_id,
const base::FilePath& install_dir,
const std::string& manifest) override;

private:
friend class ::BraveProfileManagerTest;
friend class ::BraveProfileManagerExtensionTest;
friend class ::BraveTorClientUpdaterTest;
friend class ::SearchEngineProviderServiceTest;

static std::string g_tor_client_component_name_;
static std::string g_tor_client_component_id_;
static std::string g_tor_client_component_base64_public_key_;
static void SetComponentIdAndBase64PublicKeyForTest(
const std::string& component_id,
const std::string& component_base64_public_key);
void InitExecutablePath(const base::FilePath& install_dir);
void SetExecutablePath(const base::FilePath& path);

scoped_refptr<base::SequencedTaskRunner> task_runner_;
bool registered_;
base::FilePath executable_path_;
base::ObserverList<Observer> observers_;

base::WeakPtrFactory<BraveTorClientUpdater> weak_ptr_factory_;

DISALLOW_COPY_AND_ASSIGN(BraveTorClientUpdater);
};
Expand Down
Loading

0 comments on commit 16b7cba

Please sign in to comment.