2018-10-04 18:08:56 +00:00
|
|
|
// Copyright (c) 2017 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/io_thread.h"
|
2018-11-07 14:24:05 +00:00
|
|
|
|
2019-07-03 01:22:09 +00:00
|
|
|
#include <string>
|
2018-11-07 14:24:05 +00:00
|
|
|
#include <utility>
|
2018-10-04 18:08:56 +00:00
|
|
|
|
|
|
|
#include "components/net_log/chrome_net_log.h"
|
|
|
|
#include "content/public/browser/browser_thread.h"
|
2018-10-05 20:34:45 +00:00
|
|
|
#include "content/public/browser/network_service_instance.h"
|
2018-11-07 14:24:05 +00:00
|
|
|
#include "net/cert/caching_cert_verifier.h"
|
|
|
|
#include "net/cert/cert_verifier.h"
|
|
|
|
#include "net/cert/cert_verify_proc.h"
|
|
|
|
#include "net/cert/multi_threaded_cert_verifier.h"
|
2019-07-03 01:22:09 +00:00
|
|
|
#include "net/log/net_log_util.h"
|
2018-10-04 18:08:56 +00:00
|
|
|
#include "net/proxy_resolution/proxy_resolution_service.h"
|
|
|
|
#include "net/url_request/url_request_context.h"
|
2018-10-05 20:34:45 +00:00
|
|
|
#include "services/network/network_service.h"
|
2019-03-26 01:10:48 +00:00
|
|
|
#include "services/network/public/cpp/features.h"
|
2019-07-03 01:22:09 +00:00
|
|
|
#include "services/network/public/cpp/network_switches.h"
|
|
|
|
#include "services/network/public/mojom/net_log.mojom.h"
|
2018-11-07 14:24:05 +00:00
|
|
|
#include "services/network/url_request_context_builder_mojo.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/net/url_request_context_getter.h"
|
2018-10-04 18:08:56 +00:00
|
|
|
|
|
|
|
using content::BrowserThread;
|
|
|
|
|
2019-07-03 01:22:09 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Parses the desired granularity of NetLog capturing specified by the command
|
|
|
|
// line.
|
|
|
|
net::NetLogCaptureMode GetNetCaptureModeFromCommandLine(
|
|
|
|
const base::CommandLine& command_line) {
|
|
|
|
base::StringPiece switch_name = network::switches::kNetLogCaptureMode;
|
|
|
|
|
|
|
|
if (command_line.HasSwitch(switch_name)) {
|
|
|
|
std::string value = command_line.GetSwitchValueASCII(switch_name);
|
|
|
|
|
|
|
|
if (value == "Default")
|
|
|
|
return net::NetLogCaptureMode::Default();
|
|
|
|
if (value == "IncludeCookiesAndCredentials")
|
|
|
|
return net::NetLogCaptureMode::IncludeCookiesAndCredentials();
|
|
|
|
if (value == "IncludeSocketBytes")
|
|
|
|
return net::NetLogCaptureMode::IncludeSocketBytes();
|
|
|
|
|
|
|
|
LOG(ERROR) << "Unrecognized value for --" << switch_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return net::NetLogCaptureMode::Default();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
IOThread::IOThread(
|
|
|
|
SystemNetworkContextManager* system_network_context_manager) {
|
2018-10-04 18:08:56 +00:00
|
|
|
BrowserThread::SetIOThreadDelegate(this);
|
2018-11-07 14:24:05 +00:00
|
|
|
|
|
|
|
system_network_context_manager->SetUp(
|
|
|
|
&network_context_request_, &network_context_params_,
|
|
|
|
&http_auth_static_params_, &http_auth_dynamic_params_);
|
2018-10-04 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IOThread::~IOThread() {
|
|
|
|
BrowserThread::SetIOThreadDelegate(nullptr);
|
|
|
|
}
|
|
|
|
|
2019-06-04 03:44:12 +00:00
|
|
|
void IOThread::RegisterURLRequestContextGetter(
|
2019-06-19 21:23:04 +00:00
|
|
|
electron::URLRequestContextGetter* getter) {
|
2019-06-04 03:44:12 +00:00
|
|
|
base::AutoLock lock(lock_);
|
|
|
|
|
|
|
|
DCHECK(!base::FeatureList::IsEnabled(network::features::kNetworkService));
|
|
|
|
DCHECK_EQ(0u, request_context_getters_.count(getter));
|
|
|
|
request_context_getters_.insert(getter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOThread::DeregisterURLRequestContextGetter(
|
2019-06-19 21:23:04 +00:00
|
|
|
electron::URLRequestContextGetter* getter) {
|
2019-06-04 03:44:12 +00:00
|
|
|
base::AutoLock lock(lock_);
|
|
|
|
|
|
|
|
DCHECK(!base::FeatureList::IsEnabled(network::features::kNetworkService));
|
|
|
|
DCHECK_EQ(1u, request_context_getters_.count(getter));
|
|
|
|
request_context_getters_.erase(getter);
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:08:56 +00:00
|
|
|
void IOThread::Init() {
|
2019-03-26 01:10:48 +00:00
|
|
|
if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) {
|
|
|
|
std::unique_ptr<network::URLRequestContextBuilderMojo> builder =
|
|
|
|
std::make_unique<network::URLRequestContextBuilderMojo>();
|
2018-10-05 20:34:45 +00:00
|
|
|
|
2019-03-22 06:58:13 +00:00
|
|
|
// Enable file:// support.
|
|
|
|
builder->set_file_enabled(true);
|
|
|
|
|
2019-03-26 01:10:48 +00:00
|
|
|
auto cert_verifier = std::make_unique<net::CachingCertVerifier>(
|
|
|
|
std::make_unique<net::MultiThreadedCertVerifier>(
|
2019-05-01 00:18:22 +00:00
|
|
|
net::CertVerifyProc::CreateDefault(nullptr)));
|
2019-03-26 01:10:48 +00:00
|
|
|
builder->SetCertVerifier(std::move(cert_verifier));
|
2018-10-04 18:08:56 +00:00
|
|
|
|
2019-03-26 01:10:48 +00:00
|
|
|
// Create the network service, so that shared host resolver
|
|
|
|
// gets created which is required to set the auth preferences below.
|
|
|
|
network::NetworkService* network_service = content::GetNetworkServiceImpl();
|
|
|
|
network_service->SetUpHttpAuth(std::move(http_auth_static_params_));
|
|
|
|
network_service->ConfigureHttpAuthPrefs(
|
|
|
|
std::move(http_auth_dynamic_params_));
|
2018-11-07 14:24:05 +00:00
|
|
|
|
2019-07-03 01:22:09 +00:00
|
|
|
const base::CommandLine* command_line =
|
|
|
|
base::CommandLine::ForCurrentProcess();
|
|
|
|
// start net log trace if --log-net-log is passed in the command line.
|
|
|
|
if (command_line->HasSwitch(network::switches::kLogNetLog)) {
|
|
|
|
base::FilePath log_file =
|
|
|
|
command_line->GetSwitchValuePath(network::switches::kLogNetLog);
|
|
|
|
base::File file(log_file,
|
|
|
|
base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
|
|
|
|
if (log_file.empty() || !file.IsValid()) {
|
|
|
|
LOG(ERROR) << "Failed opening NetLog: " << log_file.value();
|
|
|
|
} else {
|
|
|
|
auto platform_dict = net_log::GetPlatformConstantsForNetLog(
|
|
|
|
base::CommandLine::ForCurrentProcess()->GetCommandLineString(),
|
|
|
|
std::string(ELECTRON_PRODUCT_NAME));
|
|
|
|
network_service->StartNetLog(
|
|
|
|
std::move(file), GetNetCaptureModeFromCommandLine(*command_line),
|
|
|
|
platform_dict ? std::move(*platform_dict)
|
|
|
|
: base::DictionaryValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-04 03:44:12 +00:00
|
|
|
system_network_context_ = network_service->CreateNetworkContextWithBuilder(
|
|
|
|
std::move(network_context_request_), std::move(network_context_params_),
|
|
|
|
std::move(builder), &system_request_context_);
|
2019-03-26 01:10:48 +00:00
|
|
|
}
|
2018-10-04 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IOThread::CleanUp() {
|
2019-06-04 03:44:12 +00:00
|
|
|
if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) {
|
2019-03-26 01:10:48 +00:00
|
|
|
system_request_context_->proxy_resolution_service()->OnShutdown();
|
2018-10-04 18:08:56 +00:00
|
|
|
|
2019-06-04 03:44:12 +00:00
|
|
|
base::AutoLock lock(lock_);
|
|
|
|
for (auto* getter : request_context_getters_) {
|
|
|
|
getter->NotifyContextShuttingDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
system_network_context_.reset();
|
|
|
|
}
|
2018-10-04 18:08:56 +00:00
|
|
|
}
|