Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
Merge pull request #473 from brave/tor_browser_context
Browse files Browse the repository at this point in the history
Tor browser context
  • Loading branch information
darkdh authored Jul 18, 2018
2 parents 73748b3 + 0ef0d3c commit 4ffc7f0
Show file tree
Hide file tree
Showing 30 changed files with 1,499 additions and 90 deletions.
34 changes: 32 additions & 2 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import("//tools/v8_context_snapshot/v8_context_snapshot.gni")
import("//v8/gni/v8.gni")
import("//third_party/icu/config.gni")
import("//media/cdm/library_cdm/cdm_paths.gni")
import("//mojo/public/tools/bindings/mojom.gni")
import("//electron/build/config.gni")

group("electron") {
Expand Down Expand Up @@ -55,7 +56,10 @@ group("electron") {
}

if (use_aura && (is_win || is_linux)) {
data_deps += [ "app:service_manifests" ]
data_deps += [
"//chrome/app:service_manifests",
"//chrome/app:chrome_renderer_manifest",
]
}
}

Expand Down Expand Up @@ -84,7 +88,7 @@ grit("brave_resources") {
]

deps = [
"app:brave_content_manifest_overlays",
"//chrome/app:chrome_content_manifest_overlays",
"//chrome/browser/safe_browsing",
]

Expand Down Expand Up @@ -239,13 +243,25 @@ source_set("common") {
"//base",
"//components/url_formatter",
"//content/public/child",
":mojo_bindings",
]

if (enable_extensions) {
public_deps += [
"brave/common/extensions/api",
]
}

}

mojom("mojo_bindings") {
sources = [
"brave/common/tor/tor.mojom",
]

public_deps = [
"//mojo/public/mojom/base",
]
}

source_set("utility") {
Expand All @@ -266,6 +282,7 @@ source_set("utility") {
"//components/cookie_config",
"//services/proxy_resolver:lib",
"//third_party/protobuf:protobuf_lite",
":tor",
]

if (use_glib) {
Expand All @@ -291,6 +308,19 @@ source_set("utility") {
]
}

source_set("tor") {
sources = [
"brave/utility/tor/tor_launcher_impl.cc",
"brave/utility/tor/tor_launcher_impl.h",
"brave/utility/tor/tor_service.cc",
"brave/utility/tor/tor_service.h",
]

deps = [
":mojo_bindings",
]
}

source_set("renderer") {
public_configs = [
"build:electron_config",
Expand Down
30 changes: 3 additions & 27 deletions app/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,9 @@ import("//build/config/ui.gni")
import("//tools/grit/grit_rule.gni")
import("//services/service_manager/public/service_manifest.gni")

service_manifest("brave_content_packaged_services_manifest_overlay") {
source = "//chrome/browser/chrome_content_packaged_services_manifest_overlay.json"
packaged_services = [
"//chrome/utility:profile_import_manifest",
]
}

group("brave_content_manifest_overlays") {
deps = [
"//chrome/app:chrome_content_manifest_overlays",
":brave_content_packaged_services_manifest_overlay",
]
}

if (use_aura) {
service_manifest("brave_content_packaged_services_manifest") {
source_manifest = "//content/public/app:packaged_services_manifest"
overlays = [ ":brave_content_packaged_services_manifest_overlay" ]
}

group("service_manifests") {
data_deps = [
"//chrome/app:service_manifests",
"//chrome/app:chrome_renderer_manifest",
":brave_content_packaged_services_manifest_overlay",
]
}
service_manifest("tor_launcher_manifest") {
name = "tor_launcher"
source = "//electron//brave/utility/tor/tor_manifest.json"
}

grit("brave_strings") {
Expand Down
3 changes: 3 additions & 0 deletions app/brave_strings.grd
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Brave. These strings will be translated for each locale that Brave supports -->
<message name="IDS_SHORT_PRODUCT_NAME" desc="The Brave application short name">
Brave
</message>
<message name="IDS_UTILITY_PROCESS_TOR_LAUNCHER_NAME" desc="The utility process which launch Tor">
Tor Launcher
</message>
</messages>
</release>
</grit>
109 changes: 92 additions & 17 deletions atom/browser/api/atom_api_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "base/time/time.h"
#include "brave/browser/brave_content_browser_client.h"
#include "brave/browser/brave_permission_manager.h"
#include "brave/browser/tor/tor_launcher_factory.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
Expand Down Expand Up @@ -177,6 +178,19 @@ struct Converter<net::ProxyConfig> {
}
};

template<>
struct Converter<brave::TorLauncherFactory::TorProcessState> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate, brave::TorLauncherFactory::TorProcessState val) {
if (val == brave::TorLauncherFactory::TorProcessState::LAUNCH_SUCCEEDED)
return mate::StringToV8(isolate, "launch-succeeded");
else if (val == brave::TorLauncherFactory::TorProcessState::LAUNCH_FAILED)
return mate::StringToV8(isolate, "launch-failed");
else if (val == brave::TorLauncherFactory::TorProcessState::CRASHED)
return mate::StringToV8(isolate, "crashed");
}
};

} // namespace mate

namespace atom {
Expand Down Expand Up @@ -215,14 +229,14 @@ class ResolveProxyHelper {
const GURL& url) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

net::ProxyResolutionService* proxy_service =
net::ProxyResolutionService* proxy_resolution_service =
context_getter->GetURLRequestContext()->proxy_resolution_service();
net::CompletionCallback completion_callback =
base::Bind(&ResolveProxyHelper::OnResolveProxyCompleted,
base::Unretained(this));

// Start the request.
int result = proxy_service->ResolveProxy(
int result = proxy_resolution_service->ResolveProxy(
url, "GET", &proxy_info_, completion_callback,
&pac_req_, nullptr, net::NetLogWithSource());

Expand Down Expand Up @@ -293,20 +307,6 @@ void DoCacheActionInIO(
on_get_backend.Run(net::OK);
}

void SetProxyInIO(scoped_refptr<net::URLRequestContextGetter> getter,
const net::ProxyConfig& config,
const base::Closure& callback) {
auto proxy_service =
getter->GetURLRequestContext()->proxy_resolution_service();
proxy_service->ResetConfigService(
base::WrapUnique(new net::ProxyConfigServiceFixed(
net::ProxyConfigWithAnnotation(config, NO_TRAFFIC_ANNOTATION_YET))));
// Refetches and applies the new pac script if provided.
proxy_service->ForceReloadProxyConfig();
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE, callback);
}

void SetCertVerifyProcInIO(
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
const AtomCertVerifier::VerifyProc& proc) {
Expand Down Expand Up @@ -464,10 +464,24 @@ void Session::FlushStorageData() {
storage_partition->Flush();
}

void SetProxyInIO(scoped_refptr<net::URLRequestContextGetter> getter,
const net::ProxyConfig& config,
const base::Closure& callback) {
auto proxy_resolution_service =
getter->GetURLRequestContext()->proxy_resolution_service();
proxy_resolution_service->ResetConfigService(base::WrapUnique(
new net::ProxyConfigServiceFixed(
net::ProxyConfigWithAnnotation(config, NO_TRAFFIC_ANNOTATION_YET))));
// Refetches and applies the new pac script if provided.
proxy_resolution_service->ForceReloadProxyConfig();
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE, callback);
}

void Session::SetProxy(const net::ProxyConfig& config,
const base::Closure& callback) {
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&SetProxyInIO, request_context_getter_, config, callback));
base::Bind(&SetProxyInIO, request_context_getter_, config, callback));
}

void Session::SetDownloadPath(const base::FilePath& path) {
Expand Down Expand Up @@ -601,6 +615,62 @@ bool Session::Equal(Session* session) const {
#endif
}

bool Session::IsOffTheRecord() const {
brave::BraveBrowserContext* brave_browser_context =
brave::BraveBrowserContext::FromBrowserContext(profile_);
if (brave_browser_context->IsOffTheRecord())
return true;
if (brave_browser_context->IsIsolatedStorage())
return true;
return false;
}

void Session::SetTorNewIdentity(const GURL& url,
const base::Closure& callback) const {
brave::BraveBrowserContext* brave_browser_context =
brave::BraveBrowserContext::FromBrowserContext(profile_);
if (!brave_browser_context->IsTorBrowserContext()) {
LOG(ERROR) << __func__ << " only available for tor browser context";
return;
}
brave_browser_context->SetTorNewIdentity(url, callback);
}

void Session::RelaunchTor() const {
brave::BraveBrowserContext* brave_browser_context =
brave::BraveBrowserContext::FromBrowserContext(profile_);
if (!brave_browser_context->IsTorBrowserContext()) {
LOG(ERROR) << __func__ << " only available for tor browser context";
return;
}
brave_browser_context->RelaunchTor();
}

int64_t Session::GetTorPid() const {
brave::BraveBrowserContext* brave_browser_context =
brave::BraveBrowserContext::FromBrowserContext(profile_);
if (!brave_browser_context->IsTorBrowserContext()) {
LOG(ERROR) << __func__ << " only available for tor browser context";
return -1;
}
return brave_browser_context->GetTorPid();
}

void Session::SetTorLauncherCallback(mate::Arguments* args) {
brave::TorLauncherFactory::TorLauncherCallback callback;
if (!args->GetNext(&callback)) {
args->ThrowError("`callback(result, pid)` is a required field");
return;
}
brave::BraveBrowserContext* brave_browser_context =
brave::BraveBrowserContext::FromBrowserContext(profile_);
if (!brave_browser_context->IsTorBrowserContext()) {
LOG(ERROR) << __func__ << " only available for tor browser context";
return;
}
brave_browser_context->SetTorLauncherCallback(callback);
}

// static
mate::Handle<Session> Session::CreateFrom(
v8::Isolate* isolate, content::BrowserContext* browser_context) {
Expand Down Expand Up @@ -659,6 +729,11 @@ void Session::BuildPrototype(v8::Isolate* isolate,
&Session::AllowNTLMCredentialsForDomains)
.SetMethod("setEnableBrotli", &Session::SetEnableBrotli)
.SetMethod("equal", &Session::Equal)
.SetMethod("isOffTheRecord", &Session::IsOffTheRecord)
.SetMethod("setTorNewIdentity", &Session::SetTorNewIdentity)
.SetMethod("relaunchTor", &Session::RelaunchTor)
.SetMethod("setTorLauncherCallback", &Session::SetTorLauncherCallback)
.SetMethod("getTorPid", &Session::GetTorPid)
.SetProperty("partition", &Session::Partition)
.SetProperty("contentSettings", &Session::ContentSettings)
.SetProperty("userPrefs", &Session::UserPrefs)
Expand Down
6 changes: 6 additions & 0 deletions atom/browser/api/atom_api_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ class Session: public mate::TrackableObject<Session>,
v8::Local<v8::Value> SpellChecker(v8::Isolate* isolate);
v8::Local<v8::Value> Extensions(v8::Isolate* isolate);
bool Equal(Session* session) const;
bool IsOffTheRecord() const;
void SetTorNewIdentity(const GURL& url,
const base::Closure& callback) const;
void RelaunchTor() const;
void SetTorLauncherCallback(mate::Arguments* args);
int64_t GetTorPid() const;

protected:
Session(v8::Isolate* isolate, Profile* browser_context);
Expand Down
12 changes: 12 additions & 0 deletions atom/browser/api/atom_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,18 @@ mate::Handle<api::Session> SessionFromOptions(v8::Isolate* isolate,
if (options.Get("parent_partition", &parent_partition)) {
session_options.SetString("parent_partition", parent_partition);
}
bool isolated_storage;
if (options.Get("isolated_storage", &isolated_storage)) {
session_options.SetBoolean("isolated_storage", isolated_storage);
}
std::string tor_proxy;
if (options.Get("tor_proxy", &tor_proxy)) {
session_options.SetString("tor_proxy", tor_proxy);
}
std::string tor_path;
if (options.Get("tor_path", &tor_path)) {
session_options.SetString("tor_path", tor_path);
}
session = Session::FromPartition(isolate, partition, session_options);
} else {
// Use the default session if not specified.
Expand Down
18 changes: 10 additions & 8 deletions atom/browser/extensions/atom_extensions_network_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,24 @@ class AtomExtensionsNetworkDelegate : public atom::AtomNetworkDelegate {

static void SetAcceptAllCookies(bool accept);

protected:
int OnBeforeURLRequest(net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url) override;
int OnBeforeStartTransaction(net::URLRequest* request,
const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers) override;
void OnBeforeRedirect(net::URLRequest* request,
const GURL& new_location) override;

private:
// NetworkDelegate implementation.
int OnBeforeURLRequestInternal(
net::URLRequest* request,
GURL* new_url);
int OnBeforeURLRequest(net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url) override;
int OnBeforeStartTransactionInternal(
net::URLRequest* request,
net::HttpRequestHeaders* headers);
int OnBeforeStartTransaction(net::URLRequest* request,
const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers) override;
void OnStartTransaction(net::URLRequest* request,
const net::HttpRequestHeaders& headers) override;
int OnHeadersReceivedInternal(
Expand All @@ -57,8 +61,6 @@ class AtomExtensionsNetworkDelegate : public atom::AtomNetworkDelegate {
const net::HttpResponseHeaders* original_response_headers,
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
GURL* allowed_unsafe_redirect_url) override;
void OnBeforeRedirect(net::URLRequest* request,
const GURL& new_location) override;
void OnResponseStarted(net::URLRequest* request, int net_error) override;
void OnCompleted(net::URLRequest* request,
bool started,
Expand Down
8 changes: 8 additions & 0 deletions atom/utility/atom_content_utility_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/time/time.h"
#include "brave/common/tor/tor.mojom.h"
#include "brave/utility/importer/brave_profile_import_service.h"
#include "brave/utility/tor/tor_service.h"
#include "chrome/common/importer/profile_import.mojom.h"
#include "components/services/unzip/public/interfaces/constants.mojom.h"
#include "components/services/unzip/unzip_service.h"
Expand Down Expand Up @@ -129,6 +131,12 @@ void AtomContentUtilityClient::RegisterServices(
base::BindRepeating(&unzip::UnzipService::CreateService);
services->emplace(unzip::mojom::kServiceName, service_info);
}

service_manager::EmbeddedServiceInfo tor_info;
tor_info.factory =
base::Bind(&TorService::CreateService);
services->emplace(tor::mojom::kTorServiceName,
tor_info);
}

// static
Expand Down
Loading

0 comments on commit 4ffc7f0

Please sign in to comment.