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