electron/brightray/browser/url_request_context_getter.cc

372 lines
14 KiB
C++
Raw Normal View History

2013-03-13 19:12:05 +00:00
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE-CHROMIUM file.
#include "browser/url_request_context_getter.h"
2013-03-13 19:12:05 +00:00
#include <algorithm>
#include "browser/net/devtools_network_controller_handle.h"
2015-09-26 18:19:27 +00:00
#include "browser/net/devtools_network_transaction_factory.h"
2015-06-05 14:54:38 +00:00
#include "browser/net_log.h"
#include "browser/network_delegate.h"
#include "common/switches.h"
2014-08-15 04:30:50 +00:00
#include "base/command_line.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_util.h"
#include "base/threading/sequenced_worker_pool.h"
2013-03-13 19:12:05 +00:00
#include "base/threading/worker_pool.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/cookie_store_factory.h"
#include "content/public/common/content_switches.h"
2014-08-15 04:30:50 +00:00
#include "net/base/host_mapping_rules.h"
#include "net/cert/cert_verifier.h"
2016-09-21 16:48:17 +00:00
#include "net/cert/ct_known_logs.h"
#include "net/cert/ct_log_verifier.h"
#include "net/cert/ct_policy_enforcer.h"
2016-09-28 18:17:42 +00:00
#include "net/cert/multi_log_ct_verifier.h"
2013-03-13 19:12:05 +00:00
#include "net/cookies/cookie_monster.h"
#include "net/dns/mapped_host_resolver.h"
2016-03-10 06:56:13 +00:00
#include "net/http/http_auth_filter.h"
2013-03-13 19:12:05 +00:00
#include "net/http/http_auth_handler_factory.h"
2016-03-08 11:59:29 +00:00
#include "net/http/http_auth_preferences.h"
2013-03-13 19:12:05 +00:00
#include "net/http/http_server_properties_impl.h"
2015-06-06 09:03:07 +00:00
#include "net/log/net_log.h"
#include "net/proxy/dhcp_proxy_script_fetcher_factory.h"
2015-11-22 20:56:35 +00:00
#include "net/proxy/proxy_config.h"
#include "net/proxy/proxy_config_service.h"
#include "net/proxy/proxy_script_fetcher_impl.h"
2013-03-13 19:12:05 +00:00
#include "net/proxy/proxy_service.h"
#include "net/proxy/proxy_service_v8.h"
2014-10-11 08:38:27 +00:00
#include "net/ssl/channel_id_service.h"
#include "net/ssl/default_channel_id_store.h"
2013-03-21 19:09:00 +00:00
#include "net/ssl/ssl_config_service_defaults.h"
2013-06-01 16:23:59 +00:00
#include "net/url_request/data_protocol_handler.h"
#include "net/url_request/file_protocol_handler.h"
2013-03-13 19:12:05 +00:00
#include "net/url_request/static_http_user_agent_settings.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_builder.h"
2013-03-13 19:12:05 +00:00
#include "net/url_request/url_request_context_storage.h"
2014-08-31 10:43:01 +00:00
#include "net/url_request/url_request_intercepting_job_factory.h"
2013-03-13 19:12:05 +00:00
#include "net/url_request/url_request_job_factory_impl.h"
#include "ui/base/l10n/l10n_util.h"
2014-08-31 10:43:01 +00:00
#include "url/url_constants.h"
#include "storage/browser/quota/special_storage_policy.h"
2013-03-13 19:12:05 +00:00
#if defined(USE_NSS_CERTS)
#include "net/cert_net/nss_ocsp.h"
#endif
2014-08-13 07:09:26 +00:00
using content::BrowserThread;
2013-03-13 19:12:05 +00:00
namespace brightray {
std::string URLRequestContextGetter::Delegate::GetUserAgent() {
return base::EmptyString();
}
std::unique_ptr<net::URLRequestJobFactory>
2015-12-07 11:55:01 +00:00
URLRequestContextGetter::Delegate::CreateURLRequestJobFactory(
content::ProtocolHandlerMap* protocol_handlers) {
std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory(
new net::URLRequestJobFactoryImpl);
for (auto& it : *protocol_handlers) {
2015-12-07 11:55:01 +00:00
job_factory->SetProtocolHandler(
it.first, base::WrapUnique(it.second.release()));
}
protocol_handlers->clear();
2015-12-07 11:55:01 +00:00
job_factory->SetProtocolHandler(
url::kDataScheme, base::WrapUnique(new net::DataProtocolHandler));
2015-12-07 11:55:01 +00:00
job_factory->SetProtocolHandler(
url::kFileScheme,
base::WrapUnique(new net::FileProtocolHandler(
2015-12-07 11:55:01 +00:00
BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN))));
return std::move(job_factory);
}
2015-01-05 21:29:16 +00:00
net::HttpCache::BackendFactory*
URLRequestContextGetter::Delegate::CreateHttpCacheBackendFactory(
const base::FilePath& base_path) {
2015-01-05 21:29:16 +00:00
base::FilePath cache_path = base_path.Append(FILE_PATH_LITERAL("Cache"));
return new net::HttpCache::DefaultBackend(
net::DISK_CACHE,
net::CACHE_BACKEND_DEFAULT,
cache_path,
0,
BrowserThread::GetTaskRunnerForThread(BrowserThread::CACHE));
2015-01-05 21:29:16 +00:00
}
std::unique_ptr<net::CertVerifier>
2015-12-07 11:55:01 +00:00
URLRequestContextGetter::Delegate::CreateCertVerifier() {
return net::CertVerifier::CreateDefault();
}
net::SSLConfigService*
URLRequestContextGetter::Delegate::CreateSSLConfigService() {
return new net::SSLConfigServiceDefaults;
}
std::vector<std::string>
URLRequestContextGetter::Delegate::GetCookieableSchemes() {
return { "http", "https", "ws", "wss" };
}
2013-03-13 19:12:05 +00:00
URLRequestContextGetter::URLRequestContextGetter(
Delegate* delegate,
DevToolsNetworkControllerHandle* handle,
2015-08-11 10:29:55 +00:00
NetLog* net_log,
2013-03-13 19:12:05 +00:00
const base::FilePath& base_path,
bool in_memory,
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner,
content::ProtocolHandlerMap* protocol_handlers,
2014-08-31 10:43:01 +00:00
content::URLRequestInterceptorScopedVector protocol_interceptors)
: delegate_(delegate),
network_controller_handle_(handle),
2015-08-11 10:29:55 +00:00
net_log_(net_log),
base_path_(base_path),
in_memory_(in_memory),
io_task_runner_(io_task_runner),
file_task_runner_(file_task_runner),
protocol_interceptors_(std::move(protocol_interceptors)),
job_factory_(nullptr) {
2013-03-13 19:12:05 +00:00
// Must first be created on the UI thread.
2014-08-13 07:09:26 +00:00
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2013-03-13 19:12:05 +00:00
2016-03-10 05:39:07 +00:00
if (protocol_handlers)
std::swap(protocol_handlers_, *protocol_handlers);
2013-03-13 19:12:05 +00:00
2016-06-22 06:52:04 +00:00
if (delegate_)
user_agent_ = delegate_->GetUserAgent();
// We must create the proxy config service on the UI loop on Linux because it
// must synchronously run on the glib message loop. This will be passed to
// the URLRequestContextStorage on the IO thread in GetURLRequestContext().
2015-12-07 11:55:01 +00:00
proxy_config_service_ = net::ProxyService::CreateSystemProxyConfigService(
io_task_runner_, file_task_runner_);
2013-03-13 19:12:05 +00:00
}
URLRequestContextGetter::~URLRequestContextGetter() {
#if defined(USE_NSS_CERTS)
net::SetURLRequestContextForNSSHttpIO(NULL);
#endif
2013-03-13 19:12:05 +00:00
}
net::HostResolver* URLRequestContextGetter::host_resolver() {
return url_request_context_->host_resolver();
}
net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
2014-08-13 07:09:26 +00:00
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
2013-03-13 19:12:05 +00:00
if (!url_request_context_.get()) {
auto& command_line = *base::CommandLine::ForCurrentProcess();
url_request_context_.reset(new net::URLRequestContext);
2015-10-14 01:47:22 +00:00
#if defined(USE_NSS_CERTS)
net::SetURLRequestContextForNSSHttpIO(url_request_context_.get());
#endif
// --log-net-log
2016-03-10 05:39:07 +00:00
if (net_log_) {
net_log_->StartLogging(url_request_context_.get());
url_request_context_->set_net_log(net_log_);
}
network_delegate_.reset(delegate_->CreateNetworkDelegate());
2013-03-13 19:12:05 +00:00
url_request_context_->set_network_delegate(network_delegate_.get());
storage_.reset(
new net::URLRequestContextStorage(url_request_context_.get()));
2016-09-21 16:09:43 +00:00
auto cookie_path = in_memory_ ?
base::FilePath() : base_path_.Append(FILE_PATH_LITERAL("Cookies"));
auto cookie_config = content::CookieStoreConfig(
cookie_path,
content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
nullptr,
delegate_->CreateCookieDelegate());
cookie_config.cookieable_schemes = delegate_->GetCookieableSchemes();
2016-09-28 23:49:26 +00:00
std::unique_ptr<net::CookieStore> cookie_store =
content::CreateCookieStore(cookie_config);
storage_->set_cookie_store(std::move(cookie_store));
2017-04-04 06:56:35 +00:00
storage_->set_channel_id_service(base::MakeUnique<net::ChannelIDService>(
new net::DefaultChannelIDStore(nullptr)));
std::string accept_lang = l10n_util::GetApplicationLocale("");
storage_->set_http_user_agent_settings(base::WrapUnique(
2015-12-07 11:55:01 +00:00
new net::StaticHttpUserAgentSettings(
net::HttpUtil::GenerateAcceptLanguageHeader(accept_lang),
2016-06-22 06:52:04 +00:00
user_agent_)));
2013-03-13 19:12:05 +00:00
2017-03-23 19:48:22 +00:00
std::unique_ptr<net::HostResolver> host_resolver(
net::HostResolver::CreateDefaultResolver(nullptr));
2013-03-13 19:12:05 +00:00
// --host-resolver-rules
if (command_line.HasSwitch(::switches::kHostResolverRules)) {
std::unique_ptr<net::MappedHostResolver> remapped_resolver(
2016-03-08 11:59:29 +00:00
new net::MappedHostResolver(std::move(host_resolver)));
remapped_resolver->SetRulesFromString(
command_line.GetSwitchValueASCII(::switches::kHostResolverRules));
2016-03-08 11:59:29 +00:00
host_resolver = std::move(remapped_resolver);
}
// --proxy-server
net::DhcpProxyScriptFetcherFactory dhcp_factory;
if (command_line.HasSwitch(switches::kNoProxyServer)) {
2014-08-20 03:44:45 +00:00
storage_->set_proxy_service(net::ProxyService::CreateDirect());
} else if (command_line.HasSwitch(switches::kProxyServer)) {
2015-11-22 20:56:35 +00:00
net::ProxyConfig proxy_config;
proxy_config.proxy_rules().ParseFromString(
command_line.GetSwitchValueASCII(switches::kProxyServer));
2015-11-22 20:56:35 +00:00
proxy_config.proxy_rules().bypass_rules.ParseFromString(
command_line.GetSwitchValueASCII(switches::kProxyBypassList));
2015-11-22 20:56:35 +00:00
storage_->set_proxy_service(net::ProxyService::CreateFixed(proxy_config));
} else if (command_line.HasSwitch(switches::kProxyPacUrl)) {
2015-07-10 08:22:02 +00:00
auto proxy_config = net::ProxyConfig::CreateFromCustomPacURL(
GURL(command_line.GetSwitchValueASCII(switches::kProxyPacUrl)));
2015-07-10 08:22:02 +00:00
proxy_config.set_pac_mandatory(true);
storage_->set_proxy_service(net::ProxyService::CreateFixed(
proxy_config));
} else {
2014-08-20 03:44:45 +00:00
storage_->set_proxy_service(
net::CreateProxyServiceUsingV8ProxyResolver(
2016-03-08 11:59:29 +00:00
std::move(proxy_config_service_),
2014-08-20 03:44:45 +00:00
new net::ProxyScriptFetcherImpl(url_request_context_.get()),
dhcp_factory.Create(url_request_context_.get()),
host_resolver.get(),
2016-07-10 09:56:02 +00:00
nullptr,
2014-08-20 03:44:45 +00:00
url_request_context_->network_delegate()));
2015-07-10 08:22:02 +00:00
}
std::vector<std::string> schemes;
schemes.push_back(std::string("basic"));
schemes.push_back(std::string("digest"));
schemes.push_back(std::string("ntlm"));
schemes.push_back(std::string("negotiate"));
2016-03-08 11:59:29 +00:00
#if defined(OS_POSIX)
http_auth_preferences_.reset(new net::HttpAuthPreferences(schemes,
std::string()));
#else
http_auth_preferences_.reset(new net::HttpAuthPreferences(schemes));
#endif
// --auth-server-whitelist
if (command_line.HasSwitch(switches::kAuthServerWhitelist)) {
http_auth_preferences_->set_server_whitelist(
command_line.GetSwitchValueASCII(switches::kAuthServerWhitelist));
}
// --auth-negotiate-delegate-whitelist
if (command_line.HasSwitch(switches::kAuthNegotiateDelegateWhitelist)) {
http_auth_preferences_->set_delegate_whitelist(
command_line.GetSwitchValueASCII(
switches::kAuthNegotiateDelegateWhitelist));
}
2016-03-08 14:28:28 +00:00
auto auth_handler_factory =
2015-05-06 05:28:00 +00:00
net::HttpAuthHandlerRegistryFactory::Create(
2016-03-08 14:28:28 +00:00
http_auth_preferences_.get(), host_resolver.get());
std::unique_ptr<net::TransportSecurityState> transport_security_state =
base::WrapUnique(new net::TransportSecurityState);
transport_security_state->SetRequireCTDelegate(
delegate_->GetRequireCTDelegate());
storage_->set_transport_security_state(std::move(transport_security_state));
storage_->set_cert_verifier(delegate_->CreateCertVerifier());
storage_->set_ssl_config_service(delegate_->CreateSSLConfigService());
2016-03-08 14:28:28 +00:00
storage_->set_http_auth_handler_factory(std::move(auth_handler_factory));
std::unique_ptr<net::HttpServerProperties> server_properties(
new net::HttpServerPropertiesImpl);
2016-03-08 11:59:29 +00:00
storage_->set_http_server_properties(std::move(server_properties));
2013-03-13 19:12:05 +00:00
std::unique_ptr<net::MultiLogCTVerifier> ct_verifier =
base::MakeUnique<net::MultiLogCTVerifier>();
ct_verifier->AddLogs(net::ct::CreateLogVerifiersForKnownLogs());
storage_->set_cert_transparency_verifier(std::move(ct_verifier));
storage_->set_ct_policy_enforcer(base::MakeUnique<net::CTPolicyEnforcer>());
2013-03-13 19:12:05 +00:00
net::HttpNetworkSession::Params network_session_params;
net::URLRequestContextBuilder::SetHttpNetworkSessionComponents(
url_request_context_.get(), &network_session_params);
network_session_params.ignore_certificate_errors = false;
// --disable-http2
if (command_line.HasSwitch(switches::kDisableHttp2))
network_session_params.enable_http2 = false;
2013-03-13 19:12:05 +00:00
// --ignore-certificate-errors
2016-09-06 08:22:52 +00:00
if (command_line.HasSwitch(switches::kIgnoreCertificateErrors))
network_session_params.ignore_certificate_errors = true;
2014-08-15 04:30:50 +00:00
// --host-rules
if (command_line.HasSwitch(switches::kHostRules)) {
2014-08-15 04:30:50 +00:00
host_mapping_rules_.reset(new net::HostMappingRules);
2017-03-23 19:48:22 +00:00
host_mapping_rules_->SetRulesFromString(
command_line.GetSwitchValueASCII(switches::kHostRules));
2014-08-15 04:30:50 +00:00
network_session_params.host_mapping_rules = host_mapping_rules_.get();
}
2013-03-13 19:12:05 +00:00
// Give |storage_| ownership at the end in case it's |mapped_host_resolver|.
2016-03-08 11:59:29 +00:00
storage_->set_host_resolver(std::move(host_resolver));
network_session_params.host_resolver =
url_request_context_->host_resolver();
2013-03-13 19:12:05 +00:00
2016-03-08 11:59:29 +00:00
http_network_session_.reset(
new net::HttpNetworkSession(network_session_params));
std::unique_ptr<net::HttpCache::BackendFactory> backend;
if (in_memory_) {
backend = net::HttpCache::DefaultBackend::InMemory(0);
} else {
2016-03-08 11:59:29 +00:00
backend.reset(delegate_->CreateHttpCacheBackendFactory(base_path_));
}
2016-03-10 05:39:07 +00:00
if (network_controller_handle_) {
storage_->set_http_transaction_factory(base::WrapUnique(
2016-03-10 05:39:07 +00:00
new net::HttpCache(
base::WrapUnique(new DevToolsNetworkTransactionFactory(
network_controller_handle_->GetController(),
http_network_session_.get())),
2016-03-10 05:39:07 +00:00
std::move(backend),
false)));
} else {
storage_->set_http_transaction_factory(base::WrapUnique(
2016-03-10 05:39:07 +00:00
new net::HttpCache(http_network_session_.get(),
std::move(backend),
false)));
}
2013-03-13 19:12:05 +00:00
std::unique_ptr<net::URLRequestJobFactory> job_factory =
delegate_->CreateURLRequestJobFactory(&protocol_handlers_);
job_factory_ = job_factory.get();
// Set up interceptors in the reverse order.
std::unique_ptr<net::URLRequestJobFactory> top_job_factory =
std::move(job_factory);
content::URLRequestInterceptorScopedVector::reverse_iterator it;
for (it = protocol_interceptors_.rbegin();
it != protocol_interceptors_.rend();
++it) {
top_job_factory.reset(new net::URLRequestInterceptingJobFactory(
2017-04-04 06:56:35 +00:00
std::move(top_job_factory), std::move(*it)));
}
2017-04-04 06:56:35 +00:00
protocol_interceptors_.clear();
storage_->set_job_factory(std::move(top_job_factory));
2013-03-13 19:12:05 +00:00
}
return url_request_context_.get();
}
scoped_refptr<base::SingleThreadTaskRunner>
URLRequestContextGetter::GetNetworkTaskRunner() const {
return BrowserThread::GetTaskRunnerForThread(BrowserThread::IO);
2013-03-13 19:12:05 +00:00
}
} // namespace brightray