Update ASan/Android runtime and setup script to LLVM r200682.
[chromium-blink-merge.git] / content / shell / browser / shell_url_request_context_getter.cc
blobbd5c9374328d26e24be3704afb8d69cfd88a2862
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/browser/cookie_store_factory.h"
16 #include "content/public/common/content_switches.h"
17 #include "content/public/common/url_constants.h"
18 #include "content/shell/browser/shell_network_delegate.h"
19 #include "content/shell/common/shell_switches.h"
20 #include "net/base/cache_type.h"
21 #include "net/cert/cert_verifier.h"
22 #include "net/cookies/cookie_monster.h"
23 #include "net/dns/host_resolver.h"
24 #include "net/dns/mapped_host_resolver.h"
25 #include "net/http/http_auth_handler_factory.h"
26 #include "net/http/http_cache.h"
27 #include "net/http/http_network_session.h"
28 #include "net/http/http_server_properties_impl.h"
29 #include "net/http/transport_security_state.h"
30 #include "net/proxy/proxy_service.h"
31 #include "net/ssl/default_server_bound_cert_store.h"
32 #include "net/ssl/server_bound_cert_service.h"
33 #include "net/ssl/ssl_config_service_defaults.h"
34 #include "net/url_request/data_protocol_handler.h"
35 #include "net/url_request/file_protocol_handler.h"
36 #include "net/url_request/protocol_intercept_job_factory.h"
37 #include "net/url_request/static_http_user_agent_settings.h"
38 #include "net/url_request/url_request_context.h"
39 #include "net/url_request/url_request_context_storage.h"
40 #include "net/url_request/url_request_job_factory_impl.h"
42 namespace content {
44 namespace {
46 void InstallProtocolHandlers(net::URLRequestJobFactoryImpl* job_factory,
47 ProtocolHandlerMap* protocol_handlers) {
48 for (ProtocolHandlerMap::iterator it =
49 protocol_handlers->begin();
50 it != protocol_handlers->end();
51 ++it) {
52 bool set_protocol = job_factory->SetProtocolHandler(
53 it->first, it->second.release());
54 DCHECK(set_protocol);
56 protocol_handlers->clear();
59 } // namespace
61 ShellURLRequestContextGetter::ShellURLRequestContextGetter(
62 bool ignore_certificate_errors,
63 const base::FilePath& base_path,
64 base::MessageLoop* io_loop,
65 base::MessageLoop* file_loop,
66 ProtocolHandlerMap* protocol_handlers,
67 net::NetLog* net_log)
68 : ignore_certificate_errors_(ignore_certificate_errors),
69 base_path_(base_path),
70 io_loop_(io_loop),
71 file_loop_(file_loop),
72 net_log_(net_log) {
73 // Must first be created on the UI thread.
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
76 std::swap(protocol_handlers_, *protocol_handlers);
78 // We must create the proxy config service on the UI loop on Linux because it
79 // must synchronously run on the glib message loop. This will be passed to
80 // the URLRequestContextStorage on the IO thread in GetURLRequestContext().
81 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) {
82 proxy_config_service_.reset(
83 net::ProxyService::CreateSystemProxyConfigService(
84 io_loop_->message_loop_proxy().get(), file_loop_));
88 ShellURLRequestContextGetter::~ShellURLRequestContextGetter() {
91 net::URLRequestContext* ShellURLRequestContextGetter::GetURLRequestContext() {
92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
94 if (!url_request_context_) {
95 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
97 url_request_context_.reset(new net::URLRequestContext());
98 url_request_context_->set_net_log(net_log_);
99 network_delegate_.reset(new ShellNetworkDelegate);
100 if (command_line.HasSwitch(switches::kDumpRenderTree))
101 ShellNetworkDelegate::SetAcceptAllCookies(false);
102 url_request_context_->set_network_delegate(network_delegate_.get());
103 storage_.reset(
104 new net::URLRequestContextStorage(url_request_context_.get()));
105 storage_->set_cookie_store(
106 content::CreateCookieStore(content::CookieStoreConfig()));
107 storage_->set_server_bound_cert_service(new net::ServerBoundCertService(
108 new net::DefaultServerBoundCertStore(NULL),
109 base::WorkerPool::GetTaskRunner(true)));
110 storage_->set_http_user_agent_settings(
111 new net::StaticHttpUserAgentSettings("en-us,en", std::string()));
113 scoped_ptr<net::HostResolver> host_resolver(
114 net::HostResolver::CreateDefaultResolver(
115 url_request_context_->net_log()));
117 storage_->set_cert_verifier(net::CertVerifier::CreateDefault());
118 storage_->set_transport_security_state(new net::TransportSecurityState);
119 if (command_line.HasSwitch(switches::kDumpRenderTree)) {
120 storage_->set_proxy_service(net::ProxyService::CreateDirect());
121 } else {
122 // TODO(jam): use v8 if possible, look at chrome code.
123 storage_->set_proxy_service(
124 net::ProxyService::CreateUsingSystemProxyResolver(
125 proxy_config_service_.release(),
127 url_request_context_->net_log()));
129 storage_->set_ssl_config_service(new net::SSLConfigServiceDefaults);
130 storage_->set_http_auth_handler_factory(
131 net::HttpAuthHandlerFactory::CreateDefault(host_resolver.get()));
132 storage_->set_http_server_properties(
133 scoped_ptr<net::HttpServerProperties>(
134 new net::HttpServerPropertiesImpl()));
136 base::FilePath cache_path = base_path_.Append(FILE_PATH_LITERAL("Cache"));
137 net::HttpCache::DefaultBackend* main_backend =
138 new net::HttpCache::DefaultBackend(
139 net::DISK_CACHE,
140 #if defined(OS_ANDROID)
141 // TODO(rdsmith): Remove when default backend for Android is
142 // changed to simple cache.
143 net::CACHE_BACKEND_SIMPLE,
144 #else
145 net::CACHE_BACKEND_DEFAULT,
146 #endif
147 cache_path,
149 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)
150 .get());
152 net::HttpNetworkSession::Params network_session_params;
153 network_session_params.cert_verifier =
154 url_request_context_->cert_verifier();
155 network_session_params.transport_security_state =
156 url_request_context_->transport_security_state();
157 network_session_params.server_bound_cert_service =
158 url_request_context_->server_bound_cert_service();
159 network_session_params.proxy_service =
160 url_request_context_->proxy_service();
161 network_session_params.ssl_config_service =
162 url_request_context_->ssl_config_service();
163 network_session_params.http_auth_handler_factory =
164 url_request_context_->http_auth_handler_factory();
165 network_session_params.network_delegate =
166 network_delegate_.get();
167 network_session_params.http_server_properties =
168 url_request_context_->http_server_properties();
169 network_session_params.net_log =
170 url_request_context_->net_log();
171 network_session_params.ignore_certificate_errors =
172 ignore_certificate_errors_;
173 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
174 int value;
175 base::StringToInt(command_line.GetSwitchValueASCII(
176 switches::kTestingFixedHttpPort), &value);
177 network_session_params.testing_fixed_http_port = value;
179 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
180 int value;
181 base::StringToInt(command_line.GetSwitchValueASCII(
182 switches::kTestingFixedHttpsPort), &value);
183 network_session_params.testing_fixed_https_port = value;
185 if (command_line.HasSwitch(switches::kHostResolverRules)) {
186 scoped_ptr<net::MappedHostResolver> mapped_host_resolver(
187 new net::MappedHostResolver(host_resolver.Pass()));
188 mapped_host_resolver->SetRulesFromString(
189 command_line.GetSwitchValueASCII(switches::kHostResolverRules));
190 host_resolver = mapped_host_resolver.Pass();
193 // Give |storage_| ownership at the end in case it's |mapped_host_resolver|.
194 storage_->set_host_resolver(host_resolver.Pass());
195 network_session_params.host_resolver =
196 url_request_context_->host_resolver();
198 net::HttpCache* main_cache = new net::HttpCache(
199 network_session_params, main_backend);
200 storage_->set_http_transaction_factory(main_cache);
202 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
203 new net::URLRequestJobFactoryImpl());
204 // Keep ProtocolHandlers added in sync with
205 // ShellContentBrowserClient::IsHandledURL().
206 InstallProtocolHandlers(job_factory.get(), &protocol_handlers_);
207 bool set_protocol = job_factory->SetProtocolHandler(
208 kDataScheme, new net::DataProtocolHandler);
209 DCHECK(set_protocol);
210 set_protocol = job_factory->SetProtocolHandler(
211 kFileScheme,
212 new net::FileProtocolHandler(
213 content::BrowserThread::GetBlockingPool()->
214 GetTaskRunnerWithShutdownBehavior(
215 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
216 DCHECK(set_protocol);
217 storage_->set_job_factory(job_factory.release());
220 return url_request_context_.get();
223 scoped_refptr<base::SingleThreadTaskRunner>
224 ShellURLRequestContextGetter::GetNetworkTaskRunner() const {
225 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
228 net::HostResolver* ShellURLRequestContextGetter::host_resolver() {
229 return url_request_context_->host_resolver();
232 } // namespace content