Refactor management of overview window copy lifetime into a separate class.
[chromium-blink-merge.git] / content / shell / browser / shell_url_request_context_getter.cc
blobdfdb85275d7b501dd0f3d834ba2a1624ff3b3e47
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/shell/browser/shell_url_request_context_getter.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_split.h"
11 #include "base/strings/string_util.h"
12 #include "base/threading/sequenced_worker_pool.h"
13 #include "base/threading/worker_pool.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/common/content_switches.h"
16 #include "content/public/common/url_constants.h"
17 #include "content/shell/browser/shell_network_delegate.h"
18 #include "content/shell/common/shell_switches.h"
19 #include "net/base/cache_type.h"
20 #include "net/cert/cert_verifier.h"
21 #include "net/cookies/cookie_monster.h"
22 #include "net/dns/host_resolver.h"
23 #include "net/dns/mapped_host_resolver.h"
24 #include "net/http/http_auth_handler_factory.h"
25 #include "net/http/http_cache.h"
26 #include "net/http/http_network_session.h"
27 #include "net/http/http_server_properties_impl.h"
28 #include "net/http/transport_security_state.h"
29 #include "net/proxy/proxy_service.h"
30 #include "net/ssl/default_server_bound_cert_store.h"
31 #include "net/ssl/server_bound_cert_service.h"
32 #include "net/ssl/ssl_config_service_defaults.h"
33 #include "net/url_request/data_protocol_handler.h"
34 #include "net/url_request/file_protocol_handler.h"
35 #include "net/url_request/protocol_intercept_job_factory.h"
36 #include "net/url_request/static_http_user_agent_settings.h"
37 #include "net/url_request/url_request_context.h"
38 #include "net/url_request/url_request_context_storage.h"
39 #include "net/url_request/url_request_job_factory_impl.h"
41 namespace content {
43 namespace {
45 void InstallProtocolHandlers(net::URLRequestJobFactoryImpl* job_factory,
46 ProtocolHandlerMap* protocol_handlers) {
47 for (ProtocolHandlerMap::iterator it =
48 protocol_handlers->begin();
49 it != protocol_handlers->end();
50 ++it) {
51 bool set_protocol = job_factory->SetProtocolHandler(
52 it->first, it->second.release());
53 DCHECK(set_protocol);
55 protocol_handlers->clear();
58 } // namespace
60 ShellURLRequestContextGetter::ShellURLRequestContextGetter(
61 bool ignore_certificate_errors,
62 const base::FilePath& base_path,
63 base::MessageLoop* io_loop,
64 base::MessageLoop* file_loop,
65 ProtocolHandlerMap* protocol_handlers,
66 net::NetLog* net_log)
67 : ignore_certificate_errors_(ignore_certificate_errors),
68 base_path_(base_path),
69 io_loop_(io_loop),
70 file_loop_(file_loop),
71 net_log_(net_log) {
72 // Must first be created on the UI thread.
73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
75 std::swap(protocol_handlers_, *protocol_handlers);
77 // We must create the proxy config service on the UI loop on Linux because it
78 // must synchronously run on the glib message loop. This will be passed to
79 // the URLRequestContextStorage on the IO thread in GetURLRequestContext().
80 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) {
81 proxy_config_service_.reset(
82 net::ProxyService::CreateSystemProxyConfigService(
83 io_loop_->message_loop_proxy().get(), file_loop_));
87 ShellURLRequestContextGetter::~ShellURLRequestContextGetter() {
90 net::URLRequestContext* ShellURLRequestContextGetter::GetURLRequestContext() {
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
93 if (!url_request_context_) {
94 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
96 url_request_context_.reset(new net::URLRequestContext());
97 url_request_context_->set_net_log(net_log_);
98 network_delegate_.reset(new ShellNetworkDelegate);
99 if (command_line.HasSwitch(switches::kDumpRenderTree))
100 ShellNetworkDelegate::SetAcceptAllCookies(false);
101 url_request_context_->set_network_delegate(network_delegate_.get());
102 storage_.reset(
103 new net::URLRequestContextStorage(url_request_context_.get()));
104 storage_->set_cookie_store(new net::CookieMonster(NULL, NULL));
105 storage_->set_server_bound_cert_service(new net::ServerBoundCertService(
106 new net::DefaultServerBoundCertStore(NULL),
107 base::WorkerPool::GetTaskRunner(true)));
108 storage_->set_http_user_agent_settings(
109 new net::StaticHttpUserAgentSettings("en-us,en", EmptyString()));
111 scoped_ptr<net::HostResolver> host_resolver(
112 net::HostResolver::CreateDefaultResolver(
113 url_request_context_->net_log()));
115 storage_->set_cert_verifier(net::CertVerifier::CreateDefault());
116 storage_->set_transport_security_state(new net::TransportSecurityState);
117 if (command_line.HasSwitch(switches::kDumpRenderTree)) {
118 storage_->set_proxy_service(net::ProxyService::CreateDirect());
119 } else {
120 // TODO(jam): use v8 if possible, look at chrome code.
121 storage_->set_proxy_service(
122 net::ProxyService::CreateUsingSystemProxyResolver(
123 proxy_config_service_.release(),
125 url_request_context_->net_log()));
127 storage_->set_ssl_config_service(new net::SSLConfigServiceDefaults);
128 storage_->set_http_auth_handler_factory(
129 net::HttpAuthHandlerFactory::CreateDefault(host_resolver.get()));
130 storage_->set_http_server_properties(
131 scoped_ptr<net::HttpServerProperties>(
132 new net::HttpServerPropertiesImpl()));
134 base::FilePath cache_path = base_path_.Append(FILE_PATH_LITERAL("Cache"));
135 net::HttpCache::DefaultBackend* main_backend =
136 new net::HttpCache::DefaultBackend(
137 net::DISK_CACHE,
138 #if defined(OS_ANDROID)
139 // TODO(rdsmith): Remove when default backend for Android is
140 // changed to simple cache.
141 net::CACHE_BACKEND_SIMPLE,
142 #else
143 net::CACHE_BACKEND_DEFAULT,
144 #endif
145 cache_path,
147 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)
148 .get());
150 net::HttpNetworkSession::Params network_session_params;
151 network_session_params.cert_verifier =
152 url_request_context_->cert_verifier();
153 network_session_params.transport_security_state =
154 url_request_context_->transport_security_state();
155 network_session_params.server_bound_cert_service =
156 url_request_context_->server_bound_cert_service();
157 network_session_params.proxy_service =
158 url_request_context_->proxy_service();
159 network_session_params.ssl_config_service =
160 url_request_context_->ssl_config_service();
161 network_session_params.http_auth_handler_factory =
162 url_request_context_->http_auth_handler_factory();
163 network_session_params.network_delegate =
164 network_delegate_.get();
165 network_session_params.http_server_properties =
166 url_request_context_->http_server_properties();
167 network_session_params.net_log =
168 url_request_context_->net_log();
169 network_session_params.ignore_certificate_errors =
170 ignore_certificate_errors_;
171 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
172 int value;
173 base::StringToInt(command_line.GetSwitchValueASCII(
174 switches::kTestingFixedHttpPort), &value);
175 network_session_params.testing_fixed_http_port = value;
177 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
178 int value;
179 base::StringToInt(command_line.GetSwitchValueASCII(
180 switches::kTestingFixedHttpsPort), &value);
181 network_session_params.testing_fixed_https_port = value;
183 if (command_line.HasSwitch(switches::kHostResolverRules)) {
184 scoped_ptr<net::MappedHostResolver> mapped_host_resolver(
185 new net::MappedHostResolver(host_resolver.Pass()));
186 mapped_host_resolver->SetRulesFromString(
187 command_line.GetSwitchValueASCII(switches::kHostResolverRules));
188 host_resolver = mapped_host_resolver.Pass();
191 // Give |storage_| ownership at the end in case it's |mapped_host_resolver|.
192 storage_->set_host_resolver(host_resolver.Pass());
193 network_session_params.host_resolver =
194 url_request_context_->host_resolver();
196 net::HttpCache* main_cache = new net::HttpCache(
197 network_session_params, main_backend);
198 storage_->set_http_transaction_factory(main_cache);
200 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
201 new net::URLRequestJobFactoryImpl());
202 // Keep ProtocolHandlers added in sync with
203 // ShellContentBrowserClient::IsHandledURL().
204 InstallProtocolHandlers(job_factory.get(), &protocol_handlers_);
205 bool set_protocol = job_factory->SetProtocolHandler(
206 chrome::kDataScheme,
207 new net::DataProtocolHandler);
208 DCHECK(set_protocol);
209 set_protocol = job_factory->SetProtocolHandler(
210 chrome::kFileScheme,
211 new net::FileProtocolHandler(
212 content::BrowserThread::GetBlockingPool()->
213 GetTaskRunnerWithShutdownBehavior(
214 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
215 DCHECK(set_protocol);
216 storage_->set_job_factory(job_factory.release());
219 return url_request_context_.get();
222 scoped_refptr<base::SingleThreadTaskRunner>
223 ShellURLRequestContextGetter::GetNetworkTaskRunner() const {
224 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
227 net::HostResolver* ShellURLRequestContextGetter::host_resolver() {
228 return url_request_context_->host_resolver();
231 } // namespace content