1 // Copyright (c) 2012 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 "net/url_request/url_request_context_builder.h"
9 #include "base/basictypes.h"
10 #include "base/compiler_specific.h"
11 #include "base/logging.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/strings/string_util.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "base/threading/thread.h"
16 #include "net/base/cache_type.h"
17 #include "net/base/net_errors.h"
18 #include "net/base/network_delegate_impl.h"
19 #include "net/base/sdch_manager.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/ftp/ftp_network_layer.h"
24 #include "net/http/http_auth_handler_factory.h"
25 #include "net/http/http_cache.h"
26 #include "net/http/http_network_layer.h"
27 #include "net/http/http_network_session.h"
28 #include "net/http/http_server_properties_impl.h"
29 #include "net/http/transport_security_persister.h"
30 #include "net/http/transport_security_state.h"
31 #include "net/ssl/channel_id_service.h"
32 #include "net/ssl/default_channel_id_store.h"
33 #include "net/ssl/ssl_config_service_defaults.h"
34 #include "net/url_request/data_protocol_handler.h"
35 #include "net/url_request/static_http_user_agent_settings.h"
36 #include "net/url_request/url_request_context.h"
37 #include "net/url_request/url_request_context_storage.h"
38 #include "net/url_request/url_request_intercepting_job_factory.h"
39 #include "net/url_request/url_request_interceptor.h"
40 #include "net/url_request/url_request_job_factory_impl.h"
41 #include "net/url_request/url_request_throttler_manager.h"
43 #if !defined(DISABLE_FILE_SUPPORT)
44 #include "net/url_request/file_protocol_handler.h"
47 #if !defined(DISABLE_FTP_SUPPORT)
48 #include "net/url_request/ftp_protocol_handler.h"
55 class BasicNetworkDelegate
: public NetworkDelegateImpl
{
57 BasicNetworkDelegate() {}
58 ~BasicNetworkDelegate() override
{}
61 int OnBeforeURLRequest(URLRequest
* request
,
62 const CompletionCallback
& callback
,
63 GURL
* new_url
) override
{
67 int OnBeforeSendHeaders(URLRequest
* request
,
68 const CompletionCallback
& callback
,
69 HttpRequestHeaders
* headers
) override
{
73 void OnSendHeaders(URLRequest
* request
,
74 const HttpRequestHeaders
& headers
) override
{}
76 int OnHeadersReceived(
78 const CompletionCallback
& callback
,
79 const HttpResponseHeaders
* original_response_headers
,
80 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
81 GURL
* allowed_unsafe_redirect_url
) override
{
85 void OnBeforeRedirect(URLRequest
* request
,
86 const GURL
& new_location
) override
{}
88 void OnResponseStarted(URLRequest
* request
) override
{}
90 void OnRawBytesRead(const URLRequest
& request
, int bytes_read
) override
{}
92 void OnCompleted(URLRequest
* request
, bool started
) override
{}
94 void OnURLRequestDestroyed(URLRequest
* request
) override
{}
96 void OnPACScriptError(int line_number
, const base::string16
& error
) override
{
99 NetworkDelegate::AuthRequiredResponse
OnAuthRequired(
101 const AuthChallengeInfo
& auth_info
,
102 const AuthCallback
& callback
,
103 AuthCredentials
* credentials
) override
{
104 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION
;
107 bool OnCanGetCookies(const URLRequest
& request
,
108 const CookieList
& cookie_list
) override
{
112 bool OnCanSetCookie(const URLRequest
& request
,
113 const std::string
& cookie_line
,
114 CookieOptions
* options
) override
{
118 bool OnCanAccessFile(const URLRequest
& request
,
119 const base::FilePath
& path
) const override
{
123 bool OnCanThrottleRequest(const URLRequest
& request
) const override
{
124 // Returning true will only enable throttling if there's also a
125 // URLRequestThrottlerManager, which there isn't, by default.
129 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate
);
132 class BasicURLRequestContext
: public URLRequestContext
{
134 explicit BasicURLRequestContext(
135 const scoped_refptr
<base::SingleThreadTaskRunner
>& file_task_runner
)
136 : file_task_runner_(file_task_runner
), storage_(this) {}
138 URLRequestContextStorage
* storage() {
142 scoped_refptr
<base::SingleThreadTaskRunner
>& GetFileTaskRunner() {
143 // Create a new thread to run file tasks, if needed.
144 if (!file_task_runner_
) {
145 DCHECK(!file_thread_
);
146 file_thread_
.reset(new base::Thread("Network File Thread"));
147 file_thread_
->StartWithOptions(
148 base::Thread::Options(base::MessageLoop::TYPE_DEFAULT
, 0));
149 file_task_runner_
= file_thread_
->task_runner();
151 return file_task_runner_
;
154 void set_transport_security_persister(
155 scoped_ptr
<TransportSecurityPersister
> transport_security_persister
) {
156 transport_security_persister
= transport_security_persister
.Pass();
160 ~BasicURLRequestContext() override
{ AssertNoURLRequests(); }
163 // The thread should be torn down last.
164 scoped_ptr
<base::Thread
> file_thread_
;
165 scoped_refptr
<base::SingleThreadTaskRunner
> file_task_runner_
;
167 URLRequestContextStorage storage_
;
168 scoped_ptr
<TransportSecurityPersister
> transport_security_persister_
;
170 DISALLOW_COPY_AND_ASSIGN(BasicURLRequestContext
);
175 URLRequestContextBuilder::HttpCacheParams::HttpCacheParams()
178 URLRequestContextBuilder::HttpCacheParams::~HttpCacheParams() {}
180 URLRequestContextBuilder::HttpNetworkSessionParams::HttpNetworkSessionParams()
181 : ignore_certificate_errors(false),
182 host_mapping_rules(NULL
),
183 testing_fixed_http_port(0),
184 testing_fixed_https_port(0),
185 next_protos(NextProtosDefaults()),
186 use_alternate_protocols(true),
190 URLRequestContextBuilder::HttpNetworkSessionParams::~HttpNetworkSessionParams()
193 URLRequestContextBuilder::SchemeFactory::SchemeFactory(
194 const std::string
& auth_scheme
,
195 HttpAuthHandlerFactory
* auth_handler_factory
)
196 : scheme(auth_scheme
), factory(auth_handler_factory
) {
199 URLRequestContextBuilder::SchemeFactory::~SchemeFactory() {
202 URLRequestContextBuilder::URLRequestContextBuilder()
203 : data_enabled_(false),
204 #if !defined(DISABLE_FILE_SUPPORT)
205 file_enabled_(false),
207 #if !defined(DISABLE_FTP_SUPPORT)
210 http_cache_enabled_(true),
211 throttling_enabled_(false),
212 sdch_enabled_(false) {
215 URLRequestContextBuilder::~URLRequestContextBuilder() {}
217 void URLRequestContextBuilder::EnableHttpCache(const HttpCacheParams
& params
) {
218 http_cache_enabled_
= true;
219 http_cache_params_
= params
;
222 void URLRequestContextBuilder::DisableHttpCache() {
223 http_cache_enabled_
= false;
224 http_cache_params_
= HttpCacheParams();
227 void URLRequestContextBuilder::SetSpdyAndQuicEnabled(bool spdy_enabled
,
229 http_network_session_params_
.next_protos
=
230 NextProtosWithSpdyAndQuic(spdy_enabled
, quic_enabled
);
231 http_network_session_params_
.enable_quic
= quic_enabled
;
234 void URLRequestContextBuilder::SetInterceptors(
235 ScopedVector
<URLRequestInterceptor
> url_request_interceptors
) {
236 url_request_interceptors_
= url_request_interceptors
.Pass();
239 void URLRequestContextBuilder::SetCookieAndChannelIdStores(
240 const scoped_refptr
<CookieStore
>& cookie_store
,
241 scoped_ptr
<ChannelIDService
> channel_id_service
) {
242 DCHECK(cookie_store
);
243 cookie_store_
= cookie_store
;
244 channel_id_service_
= channel_id_service
.Pass();
247 void URLRequestContextBuilder::SetFileTaskRunner(
248 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
) {
249 file_task_runner_
= task_runner
;
252 URLRequestContext
* URLRequestContextBuilder::Build() {
253 BasicURLRequestContext
* context
=
254 new BasicURLRequestContext(file_task_runner_
);
255 URLRequestContextStorage
* storage
= context
->storage();
257 storage
->set_http_user_agent_settings(new StaticHttpUserAgentSettings(
258 accept_language_
, user_agent_
));
260 if (!network_delegate_
)
261 network_delegate_
.reset(new BasicNetworkDelegate
);
262 NetworkDelegate
* network_delegate
= network_delegate_
.release();
263 storage
->set_network_delegate(network_delegate
);
266 storage
->set_net_log(net_log_
.release());
268 storage
->set_net_log(new NetLog
);
271 if (!host_resolver_
) {
272 host_resolver_
= HostResolver::CreateDefaultResolver(context
->net_log());
274 storage
->set_host_resolver(host_resolver_
.Pass());
276 if (!proxy_service_
) {
277 // TODO(willchan): Switch to using this code when
278 // ProxyService::CreateSystemProxyConfigService()'s signature doesn't suck.
279 #if defined(OS_LINUX) || defined(OS_ANDROID)
280 ProxyConfigService
* proxy_config_service
= proxy_config_service_
.release();
282 ProxyConfigService
* proxy_config_service
= NULL
;
283 if (proxy_config_service_
) {
284 proxy_config_service
= proxy_config_service_
.release();
286 proxy_config_service
= ProxyService::CreateSystemProxyConfigService(
287 base::ThreadTaskRunnerHandle::Get().get(),
288 context
->GetFileTaskRunner());
290 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
291 proxy_service_
.reset(
292 ProxyService::CreateUsingSystemProxyResolver(
293 proxy_config_service
,
294 0, // This results in using the default value.
295 context
->net_log()));
297 storage
->set_proxy_service(proxy_service_
.release());
299 storage
->set_ssl_config_service(new SSLConfigServiceDefaults
);
300 HttpAuthHandlerRegistryFactory
* http_auth_handler_registry_factory
=
301 HttpAuthHandlerRegistryFactory::CreateDefault(context
->host_resolver());
302 for (size_t i
= 0; i
< extra_http_auth_handlers_
.size(); ++i
) {
303 http_auth_handler_registry_factory
->RegisterSchemeFactory(
304 extra_http_auth_handlers_
[i
].scheme
,
305 extra_http_auth_handlers_
[i
].factory
);
307 storage
->set_http_auth_handler_factory(http_auth_handler_registry_factory
);
310 storage
->set_cookie_store(cookie_store_
.get());
311 storage
->set_channel_id_service(channel_id_service_
.Pass());
313 storage
->set_cookie_store(new CookieMonster(NULL
, NULL
));
314 // TODO(mmenke): This always creates a file thread, even when it ends up
315 // not being used. Consider lazily creating the thread.
316 storage
->set_channel_id_service(make_scoped_ptr(new ChannelIDService(
317 new DefaultChannelIDStore(NULL
), context
->GetFileTaskRunner())));
321 storage
->set_sdch_manager(
322 scoped_ptr
<net::SdchManager
>(new SdchManager()).Pass());
325 storage
->set_transport_security_state(new TransportSecurityState());
326 if (!transport_security_persister_path_
.empty()) {
327 context
->set_transport_security_persister(
328 make_scoped_ptr
<TransportSecurityPersister
>(
329 new TransportSecurityPersister(context
->transport_security_state(),
330 transport_security_persister_path_
,
331 context
->GetFileTaskRunner(),
335 storage
->set_http_server_properties(
336 scoped_ptr
<HttpServerProperties
>(new HttpServerPropertiesImpl()));
337 storage
->set_cert_verifier(CertVerifier::CreateDefault());
339 if (throttling_enabled_
)
340 storage
->set_throttler_manager(new URLRequestThrottlerManager());
342 HttpNetworkSession::Params network_session_params
;
343 network_session_params
.host_resolver
= context
->host_resolver();
344 network_session_params
.cert_verifier
= context
->cert_verifier();
345 network_session_params
.transport_security_state
=
346 context
->transport_security_state();
347 network_session_params
.proxy_service
= context
->proxy_service();
348 network_session_params
.ssl_config_service
=
349 context
->ssl_config_service();
350 network_session_params
.http_auth_handler_factory
=
351 context
->http_auth_handler_factory();
352 network_session_params
.network_delegate
= network_delegate
;
353 network_session_params
.http_server_properties
=
354 context
->http_server_properties();
355 network_session_params
.net_log
= context
->net_log();
357 network_session_params
.ignore_certificate_errors
=
358 http_network_session_params_
.ignore_certificate_errors
;
359 network_session_params
.host_mapping_rules
=
360 http_network_session_params_
.host_mapping_rules
;
361 network_session_params
.testing_fixed_http_port
=
362 http_network_session_params_
.testing_fixed_http_port
;
363 network_session_params
.testing_fixed_https_port
=
364 http_network_session_params_
.testing_fixed_https_port
;
365 network_session_params
.use_alternate_protocols
=
366 http_network_session_params_
.use_alternate_protocols
;
367 network_session_params
.trusted_spdy_proxy
=
368 http_network_session_params_
.trusted_spdy_proxy
;
369 network_session_params
.next_protos
= http_network_session_params_
.next_protos
;
370 network_session_params
.enable_quic
= http_network_session_params_
.enable_quic
;
371 network_session_params
.quic_connection_options
=
372 http_network_session_params_
.quic_connection_options
;
374 HttpTransactionFactory
* http_transaction_factory
= NULL
;
375 if (http_cache_enabled_
) {
376 network_session_params
.channel_id_service
=
377 context
->channel_id_service();
378 HttpCache::BackendFactory
* http_cache_backend
= NULL
;
379 if (http_cache_params_
.type
== HttpCacheParams::DISK
) {
380 http_cache_backend
= new HttpCache::DefaultBackend(
381 DISK_CACHE
, CACHE_BACKEND_DEFAULT
, http_cache_params_
.path
,
382 http_cache_params_
.max_size
, context
->GetFileTaskRunner());
385 HttpCache::DefaultBackend::InMemory(http_cache_params_
.max_size
);
388 http_transaction_factory
= new HttpCache(
389 network_session_params
, http_cache_backend
);
391 scoped_refptr
<HttpNetworkSession
> network_session(
392 new HttpNetworkSession(network_session_params
));
394 http_transaction_factory
= new HttpNetworkLayer(network_session
.get());
396 storage
->set_http_transaction_factory(http_transaction_factory
);
398 URLRequestJobFactoryImpl
* job_factory
= new URLRequestJobFactoryImpl
;
400 job_factory
->SetProtocolHandler("data", new DataProtocolHandler
);
402 #if !defined(DISABLE_FILE_SUPPORT)
404 job_factory
->SetProtocolHandler(
405 "file", new FileProtocolHandler(context
->GetFileTaskRunner()));
407 #endif // !defined(DISABLE_FILE_SUPPORT)
409 #if !defined(DISABLE_FTP_SUPPORT)
411 ftp_transaction_factory_
.reset(
412 new FtpNetworkLayer(context
->host_resolver()));
413 job_factory
->SetProtocolHandler("ftp",
414 new FtpProtocolHandler(ftp_transaction_factory_
.get()));
416 #endif // !defined(DISABLE_FTP_SUPPORT)
418 scoped_ptr
<net::URLRequestJobFactory
> top_job_factory(job_factory
);
419 if (!url_request_interceptors_
.empty()) {
420 // Set up interceptors in the reverse order.
422 for (ScopedVector
<net::URLRequestInterceptor
>::reverse_iterator i
=
423 url_request_interceptors_
.rbegin();
424 i
!= url_request_interceptors_
.rend(); ++i
) {
425 top_job_factory
.reset(new net::URLRequestInterceptingJobFactory(
426 top_job_factory
.Pass(), make_scoped_ptr(*i
)));
428 url_request_interceptors_
.weak_clear();
430 storage
->set_job_factory(top_job_factory
.release());
431 // TODO(willchan): Support sdch.