blob: e3f6bd55eaada07a2e59344e1ec11863989344fd [file] [log] [blame]
[email protected]5cba3bf2012-01-14 02:40:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c5f1e332011-09-27 01:08:032// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "content/shell/shell_url_request_context_getter.h"
6
[email protected]c2dad292012-09-07 21:27:357#include "base/command_line.h"
[email protected]c5f1e332011-09-27 01:08:038#include "base/logging.h"
[email protected]cbfe7492012-10-03 18:40:429#include "base/string_number_conversions.h"
[email protected]ee4c30d2012-11-07 15:08:4310#include "base/string_util.h"
[email protected]27c05732013-02-15 21:55:4911#include "base/strings/string_split.h"
[email protected]5bab49ec2012-05-04 21:13:1912#include "base/threading/worker_pool.h"
[email protected]c38831a12011-10-28 12:44:4913#include "content/public/browser/browser_thread.h"
[email protected]cbfe7492012-10-03 18:40:4214#include "content/public/common/content_switches.h"
[email protected]6bd30072013-02-08 18:17:1115#include "content/public/common/url_constants.h"
[email protected]33bc2832012-03-29 08:18:1716#include "content/shell/shell_network_delegate.h"
[email protected]6a2a4892013-01-20 01:19:3817#include "content/shell/shell_switches.h"
[email protected]6e7845ae2013-03-29 21:48:1118#include "net/cert/cert_verifier.h"
[email protected]48839bfc2012-03-16 18:19:1119#include "net/cookies/cookie_monster.h"
[email protected]f2cb3cf2013-03-21 01:40:5320#include "net/dns/host_resolver.h"
21#include "net/dns/mapped_host_resolver.h"
[email protected]1e528d52013-03-21 21:38:0122#include "net/ftp/ftp_network_layer.h"
[email protected]c38831a12011-10-28 12:44:4923#include "net/http/http_auth_handler_factory.h"
24#include "net/http/http_cache.h"
[email protected]c2dad292012-09-07 21:27:3525#include "net/http/http_network_session.h"
[email protected]c38831a12011-10-28 12:44:4926#include "net/http/http_server_properties_impl.h"
[email protected]c5f1e332011-09-27 01:08:0327#include "net/proxy/proxy_service.h"
[email protected]536fd0b2013-03-14 17:41:5728#include "net/ssl/default_server_bound_cert_store.h"
29#include "net/ssl/server_bound_cert_service.h"
30#include "net/ssl/ssl_config_service_defaults.h"
[email protected]6bd30072013-02-08 18:17:1131#include "net/url_request/protocol_intercept_job_factory.h"
[email protected]ee4c30d2012-11-07 15:08:4332#include "net/url_request/static_http_user_agent_settings.h"
[email protected]c5f1e332011-09-27 01:08:0333#include "net/url_request/url_request_context.h"
[email protected]98cce3ae2011-10-15 16:19:4134#include "net/url_request/url_request_context_storage.h"
[email protected]9d5730b2012-08-24 17:42:4935#include "net/url_request/url_request_job_factory_impl.h"
[email protected]c5f1e332011-09-27 01:08:0336
37namespace content {
38
[email protected]672c8c12013-03-07 12:30:0639namespace {
40
41void InstallProtocolHandlers(net::URLRequestJobFactoryImpl* job_factory,
42 ProtocolHandlerMap* protocol_handlers) {
43 for (ProtocolHandlerMap::iterator it =
44 protocol_handlers->begin();
45 it != protocol_handlers->end();
46 ++it) {
47 bool set_protocol = job_factory->SetProtocolHandler(
48 it->first, it->second.release());
49 DCHECK(set_protocol);
50 }
51 protocol_handlers->clear();
52}
53
54} // namespace
55
[email protected]c5f1e332011-09-27 01:08:0356ShellURLRequestContextGetter::ShellURLRequestContextGetter(
[email protected]c2dad292012-09-07 21:27:3557 bool ignore_certificate_errors,
[email protected]d30a36f2013-02-07 04:16:2658 const base::FilePath& base_path,
[email protected]c5f1e332011-09-27 01:08:0359 MessageLoop* io_loop,
[email protected]6bd30072013-02-08 18:17:1160 MessageLoop* file_loop,
[email protected]672c8c12013-03-07 12:30:0661 ProtocolHandlerMap* protocol_handlers)
[email protected]c2dad292012-09-07 21:27:3562 : ignore_certificate_errors_(ignore_certificate_errors),
63 base_path_(base_path),
[email protected]5cba3bf2012-01-14 02:40:3564 io_loop_(io_loop),
[email protected]672c8c12013-03-07 12:30:0665 file_loop_(file_loop) {
[email protected]5cba3bf2012-01-14 02:40:3566 // Must first be created on the UI thread.
67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
68
[email protected]672c8c12013-03-07 12:30:0669 std::swap(protocol_handlers_, *protocol_handlers);
70
[email protected]5cba3bf2012-01-14 02:40:3571 // We must create the proxy config service on the UI loop on Linux because it
72 // must synchronously run on the glib message loop. This will be passed to
73 // the URLRequestContextStorage on the IO thread in GetURLRequestContext().
[email protected]6a2a4892013-01-20 01:19:3874 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) {
75 proxy_config_service_.reset(
76 net::ProxyService::CreateSystemProxyConfigService(
77 io_loop_->message_loop_proxy(), file_loop_));
78 }
[email protected]c5f1e332011-09-27 01:08:0379}
80
81ShellURLRequestContextGetter::~ShellURLRequestContextGetter() {
82}
83
84net::URLRequestContext* ShellURLRequestContextGetter::GetURLRequestContext() {
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
86
[email protected]59383c782013-04-17 16:43:2787 if (!url_request_context_) {
[email protected]cbfe7492012-10-03 18:40:4288 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
89
[email protected]ef2bf422012-05-11 03:27:0990 url_request_context_.reset(new net::URLRequestContext());
[email protected]33bc2832012-03-29 08:18:1791 network_delegate_.reset(new ShellNetworkDelegate);
[email protected]833b700d2013-02-18 19:43:0092 if (command_line.HasSwitch(switches::kDumpRenderTree))
93 ShellNetworkDelegate::SetAcceptAllCookies(false);
[email protected]33bc2832012-03-29 08:18:1794 url_request_context_->set_network_delegate(network_delegate_.get());
[email protected]ef2bf422012-05-11 03:27:0995 storage_.reset(
96 new net::URLRequestContextStorage(url_request_context_.get()));
[email protected]98cce3ae2011-10-15 16:19:4197 storage_->set_cookie_store(new net::CookieMonster(NULL, NULL));
[email protected]9c4eff22012-03-20 22:42:2998 storage_->set_server_bound_cert_service(new net::ServerBoundCertService(
[email protected]5bab49ec2012-05-04 21:13:1999 new net::DefaultServerBoundCertStore(NULL),
100 base::WorkerPool::GetTaskRunner(true)));
[email protected]ee4c30d2012-11-07 15:08:43101 storage_->set_http_user_agent_settings(
[email protected]84f05432013-03-15 01:00:12102 new net::StaticHttpUserAgentSettings("en-us,en", EmptyString()));
[email protected]98cce3ae2011-10-15 16:19:41103
[email protected]c54a8912012-10-22 22:09:43104 scoped_ptr<net::HostResolver> host_resolver(
105 net::HostResolver::CreateDefaultResolver(NULL));
106
[email protected]9f59fac2012-03-21 23:18:11107 storage_->set_cert_verifier(net::CertVerifier::CreateDefault());
[email protected]6a2a4892013-01-20 01:19:38108 if (command_line.HasSwitch(switches::kDumpRenderTree)) {
109 storage_->set_proxy_service(net::ProxyService::CreateDirect());
110 } else {
111 // TODO(jam): use v8 if possible, look at chrome code.
112 storage_->set_proxy_service(
113 net::ProxyService::CreateUsingSystemProxyResolver(
114 proxy_config_service_.release(),
115 0,
116 NULL));
117 }
[email protected]98cce3ae2011-10-15 16:19:41118 storage_->set_ssl_config_service(new net::SSLConfigServiceDefaults);
119 storage_->set_http_auth_handler_factory(
[email protected]c54a8912012-10-22 22:09:43120 net::HttpAuthHandlerFactory::CreateDefault(host_resolver.get()));
[email protected]98cce3ae2011-10-15 16:19:41121 storage_->set_http_server_properties(new net::HttpServerPropertiesImpl);
122
[email protected]d30a36f2013-02-07 04:16:26123 base::FilePath cache_path = base_path_.Append(FILE_PATH_LITERAL("Cache"));
[email protected]c5f1e332011-09-27 01:08:03124 net::HttpCache::DefaultBackend* main_backend =
125 new net::HttpCache::DefaultBackend(
126 net::DISK_CACHE,
127 cache_path,
128 0,
129 BrowserThread::GetMessageLoopProxyForThread(
130 BrowserThread::CACHE));
131
[email protected]c2dad292012-09-07 21:27:35132 net::HttpNetworkSession::Params network_session_params;
[email protected]c2dad292012-09-07 21:27:35133 network_session_params.cert_verifier =
134 url_request_context_->cert_verifier();
135 network_session_params.server_bound_cert_service =
136 url_request_context_->server_bound_cert_service();
137 network_session_params.proxy_service =
138 url_request_context_->proxy_service();
139 network_session_params.ssl_config_service =
140 url_request_context_->ssl_config_service();
141 network_session_params.http_auth_handler_factory =
142 url_request_context_->http_auth_handler_factory();
143 network_session_params.network_delegate =
[email protected]b9d4bfd2013-03-13 11:26:17144 network_delegate_.get();
[email protected]c2dad292012-09-07 21:27:35145 network_session_params.http_server_properties =
146 url_request_context_->http_server_properties();
147 network_session_params.ignore_certificate_errors =
148 ignore_certificate_errors_;
[email protected]cbfe7492012-10-03 18:40:42149 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
150 int value;
151 base::StringToInt(command_line.GetSwitchValueASCII(
152 switches::kTestingFixedHttpPort), &value);
153 network_session_params.testing_fixed_http_port = value;
154 }
155 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
156 int value;
157 base::StringToInt(command_line.GetSwitchValueASCII(
158 switches::kTestingFixedHttpsPort), &value);
159 network_session_params.testing_fixed_https_port = value;
160 }
161 if (command_line.HasSwitch(switches::kHostResolverRules)) {
[email protected]c54a8912012-10-22 22:09:43162 scoped_ptr<net::MappedHostResolver> mapped_host_resolver(
163 new net::MappedHostResolver(host_resolver.Pass()));
164 mapped_host_resolver->SetRulesFromString(
[email protected]cbfe7492012-10-03 18:40:42165 command_line.GetSwitchValueASCII(switches::kHostResolverRules));
[email protected]c54a8912012-10-22 22:09:43166 host_resolver = mapped_host_resolver.Pass();
[email protected]cbfe7492012-10-03 18:40:42167 }
[email protected]c2dad292012-09-07 21:27:35168
[email protected]c54a8912012-10-22 22:09:43169 // Give |storage_| ownership at the end in case it's |mapped_host_resolver|.
170 storage_->set_host_resolver(host_resolver.Pass());
171 network_session_params.host_resolver =
172 url_request_context_->host_resolver();
173
[email protected]c5f1e332011-09-27 01:08:03174 net::HttpCache* main_cache = new net::HttpCache(
[email protected]c2dad292012-09-07 21:27:35175 network_session_params, main_backend);
[email protected]98cce3ae2011-10-15 16:19:41176 storage_->set_http_transaction_factory(main_cache);
[email protected]c5f1e332011-09-27 01:08:03177
[email protected]1e528d52013-03-21 21:38:01178#if !defined(DISABLE_FTP_SUPPORT)
179 storage_->set_ftp_transaction_factory(
180 new net::FtpNetworkLayer(network_session_params.host_resolver));
181#endif
182
[email protected]6bd30072013-02-08 18:17:11183 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
184 new net::URLRequestJobFactoryImpl());
[email protected]672c8c12013-03-07 12:30:06185 InstallProtocolHandlers(job_factory.get(), &protocol_handlers_);
186 storage_->set_job_factory(job_factory.release());
[email protected]c5f1e332011-09-27 01:08:03187 }
188
[email protected]ef2bf422012-05-11 03:27:09189 return url_request_context_.get();
[email protected]c5f1e332011-09-27 01:08:03190}
191
[email protected]4969b0122012-06-16 01:58:28192scoped_refptr<base::SingleThreadTaskRunner>
193 ShellURLRequestContextGetter::GetNetworkTaskRunner() const {
[email protected]c5f1e332011-09-27 01:08:03194 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
195}
196
[email protected]98cce3ae2011-10-15 16:19:41197net::HostResolver* ShellURLRequestContextGetter::host_resolver() {
198 return url_request_context_->host_resolver();
199}
200
[email protected]c5f1e332011-09-27 01:08:03201} // namespace content