Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / shell / browser / shell_url_request_context_getter.cc
blob01ca90e8a5a8e6a61a0a250fc243e98abdd646d0
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/memory/scoped_ptr.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
14 #include "base/threading/sequenced_worker_pool.h"
15 #include "base/threading/worker_pool.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/cookie_store_factory.h"
18 #include "content/public/common/content_switches.h"
19 #include "content/shell/browser/shell_network_delegate.h"
20 #include "content/shell/common/shell_content_client.h"
21 #include "content/shell/common/shell_switches.h"
22 #include "net/base/cache_type.h"
23 #include "net/cert/cert_verifier.h"
24 #include "net/cookies/cookie_monster.h"
25 #include "net/dns/host_resolver.h"
26 #include "net/dns/mapped_host_resolver.h"
27 #include "net/http/http_auth_handler_factory.h"
28 #include "net/http/http_cache.h"
29 #include "net/http/http_network_session.h"
30 #include "net/http/http_server_properties_impl.h"
31 #include "net/http/transport_security_state.h"
32 #include "net/proxy/proxy_service.h"
33 #include "net/ssl/channel_id_service.h"
34 #include "net/ssl/default_channel_id_store.h"
35 #include "net/ssl/ssl_config_service_defaults.h"
36 #include "net/url_request/data_protocol_handler.h"
37 #include "net/url_request/file_protocol_handler.h"
38 #include "net/url_request/static_http_user_agent_settings.h"
39 #include "net/url_request/url_request_context.h"
40 #include "net/url_request/url_request_context_storage.h"
41 #include "net/url_request/url_request_intercepting_job_factory.h"
42 #include "net/url_request/url_request_job_factory_impl.h"
43 #include "url/url_constants.h"
45 namespace content {
47 namespace {
49 void InstallProtocolHandlers(net::URLRequestJobFactoryImpl* job_factory,
50 ProtocolHandlerMap* protocol_handlers) {
51 for (ProtocolHandlerMap::iterator it =
52 protocol_handlers->begin();
53 it != protocol_handlers->end();
54 ++it) {
55 bool set_protocol = job_factory->SetProtocolHandler(
56 it->first, make_scoped_ptr(it->second.release()));
57 DCHECK(set_protocol);
59 protocol_handlers->clear();
62 } // namespace
64 ShellURLRequestContextGetter::ShellURLRequestContextGetter(
65 bool ignore_certificate_errors,
66 const base::FilePath& base_path,
67 base::MessageLoop* io_loop,
68 base::MessageLoop* file_loop,
69 ProtocolHandlerMap* protocol_handlers,
70 URLRequestInterceptorScopedVector request_interceptors,
71 net::NetLog* net_log)
72 : ignore_certificate_errors_(ignore_certificate_errors),
73 base_path_(base_path),
74 io_loop_(io_loop),
75 file_loop_(file_loop),
76 net_log_(net_log),
77 request_interceptors_(request_interceptors.Pass()) {
78 // Must first be created on the UI thread.
79 DCHECK_CURRENTLY_ON(BrowserThread::UI);
81 std::swap(protocol_handlers_, *protocol_handlers);
83 // We must create the proxy config service on the UI loop on Linux because it
84 // must synchronously run on the glib message loop. This will be passed to
85 // the URLRequestContextStorage on the IO thread in GetURLRequestContext().
86 proxy_config_service_ = GetProxyConfigService();
89 ShellURLRequestContextGetter::~ShellURLRequestContextGetter() {
92 scoped_ptr<net::NetworkDelegate>
93 ShellURLRequestContextGetter::CreateNetworkDelegate() {
94 return make_scoped_ptr(new ShellNetworkDelegate).Pass();
97 scoped_ptr<net::ProxyConfigService>
98 ShellURLRequestContextGetter::GetProxyConfigService() {
99 return make_scoped_ptr(net::ProxyService::CreateSystemProxyConfigService(
100 io_loop_->task_runner(), file_loop_->task_runner()));
103 scoped_ptr<net::ProxyService> ShellURLRequestContextGetter::GetProxyService() {
104 // TODO(jam): use v8 if possible, look at chrome code.
105 return net::ProxyService::CreateUsingSystemProxyResolver(
106 proxy_config_service_.release(), 0, url_request_context_->net_log());
109 net::URLRequestContext* ShellURLRequestContextGetter::GetURLRequestContext() {
110 DCHECK_CURRENTLY_ON(BrowserThread::IO);
112 if (!url_request_context_) {
113 const base::CommandLine& command_line =
114 *base::CommandLine::ForCurrentProcess();
116 url_request_context_.reset(new net::URLRequestContext());
117 url_request_context_->set_net_log(net_log_);
118 network_delegate_ = CreateNetworkDelegate();
119 url_request_context_->set_network_delegate(network_delegate_.get());
120 storage_.reset(
121 new net::URLRequestContextStorage(url_request_context_.get()));
122 storage_->set_cookie_store(CreateCookieStore(CookieStoreConfig()));
123 storage_->set_channel_id_service(make_scoped_ptr(
124 new net::ChannelIDService(new net::DefaultChannelIDStore(NULL),
125 base::WorkerPool::GetTaskRunner(true))));
126 storage_->set_http_user_agent_settings(make_scoped_ptr(
127 new net::StaticHttpUserAgentSettings("en-us,en", GetShellUserAgent())));
129 scoped_ptr<net::HostResolver> host_resolver(
130 net::HostResolver::CreateDefaultResolver(
131 url_request_context_->net_log()));
133 storage_->set_cert_verifier(net::CertVerifier::CreateDefault());
134 storage_->set_transport_security_state(
135 make_scoped_ptr(new net::TransportSecurityState));
136 storage_->set_proxy_service(GetProxyService());
137 storage_->set_ssl_config_service(new net::SSLConfigServiceDefaults);
138 storage_->set_http_auth_handler_factory(
139 net::HttpAuthHandlerFactory::CreateDefault(host_resolver.get()));
140 storage_->set_http_server_properties(
141 make_scoped_ptr(new net::HttpServerPropertiesImpl()));
143 base::FilePath cache_path = base_path_.Append(FILE_PATH_LITERAL("Cache"));
144 net::HttpCache::DefaultBackend* main_backend =
145 new net::HttpCache::DefaultBackend(
146 net::DISK_CACHE,
147 #if defined(OS_ANDROID)
148 // TODO(rdsmith): Remove when default backend for Android is
149 // changed to simple cache.
150 net::CACHE_BACKEND_SIMPLE,
151 #else
152 net::CACHE_BACKEND_DEFAULT,
153 #endif
154 cache_path,
156 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
158 net::HttpNetworkSession::Params network_session_params;
159 network_session_params.cert_verifier =
160 url_request_context_->cert_verifier();
161 network_session_params.transport_security_state =
162 url_request_context_->transport_security_state();
163 network_session_params.channel_id_service =
164 url_request_context_->channel_id_service();
165 network_session_params.proxy_service =
166 url_request_context_->proxy_service();
167 network_session_params.ssl_config_service =
168 url_request_context_->ssl_config_service();
169 network_session_params.http_auth_handler_factory =
170 url_request_context_->http_auth_handler_factory();
171 network_session_params.network_delegate =
172 network_delegate_.get();
173 network_session_params.http_server_properties =
174 url_request_context_->http_server_properties();
175 network_session_params.net_log =
176 url_request_context_->net_log();
177 network_session_params.ignore_certificate_errors =
178 ignore_certificate_errors_;
179 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
180 int value;
181 base::StringToInt(command_line.GetSwitchValueASCII(
182 switches::kTestingFixedHttpPort), &value);
183 network_session_params.testing_fixed_http_port = value;
185 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
186 int value;
187 base::StringToInt(command_line.GetSwitchValueASCII(
188 switches::kTestingFixedHttpsPort), &value);
189 network_session_params.testing_fixed_https_port = value;
191 if (command_line.HasSwitch(switches::kHostResolverRules)) {
192 scoped_ptr<net::MappedHostResolver> mapped_host_resolver(
193 new net::MappedHostResolver(host_resolver.Pass()));
194 mapped_host_resolver->SetRulesFromString(
195 command_line.GetSwitchValueASCII(switches::kHostResolverRules));
196 host_resolver = mapped_host_resolver.Pass();
199 // Give |storage_| ownership at the end in case it's |mapped_host_resolver|.
200 storage_->set_host_resolver(host_resolver.Pass());
201 network_session_params.host_resolver =
202 url_request_context_->host_resolver();
204 storage_->set_http_transaction_factory(
205 make_scoped_ptr(
206 new net::HttpCache(network_session_params, main_backend))
207 .Pass());
209 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
210 new net::URLRequestJobFactoryImpl());
211 // Keep ProtocolHandlers added in sync with
212 // ShellContentBrowserClient::IsHandledURL().
213 InstallProtocolHandlers(job_factory.get(), &protocol_handlers_);
214 bool set_protocol = job_factory->SetProtocolHandler(
215 url::kDataScheme, make_scoped_ptr(new net::DataProtocolHandler));
216 DCHECK(set_protocol);
217 #if !defined(DISABLE_FILE_SUPPORT)
218 set_protocol = job_factory->SetProtocolHandler(
219 url::kFileScheme,
220 make_scoped_ptr(new net::FileProtocolHandler(
221 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
222 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN))));
223 DCHECK(set_protocol);
224 #endif
226 // Set up interceptors in the reverse order.
227 scoped_ptr<net::URLRequestJobFactory> top_job_factory =
228 job_factory.Pass();
229 for (URLRequestInterceptorScopedVector::reverse_iterator i =
230 request_interceptors_.rbegin();
231 i != request_interceptors_.rend();
232 ++i) {
233 top_job_factory.reset(new net::URLRequestInterceptingJobFactory(
234 top_job_factory.Pass(), make_scoped_ptr(*i)));
236 request_interceptors_.weak_clear();
238 storage_->set_job_factory(top_job_factory.Pass());
241 return url_request_context_.get();
244 scoped_refptr<base::SingleThreadTaskRunner>
245 ShellURLRequestContextGetter::GetNetworkTaskRunner() const {
246 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
249 net::HostResolver* ShellURLRequestContextGetter::host_resolver() {
250 return url_request_context_->host_resolver();
253 } // namespace content