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 "mojo/shell/url_request_context_getter.h"
7 #include "net/cert/cert_verifier.h"
8 #include "net/cookies/cookie_monster.h"
9 #include "net/http/http_cache.h"
10 #include "net/http/http_network_session.h"
11 #include "net/http/http_server_properties_impl.h"
12 #include "net/http/transport_security_state.h"
13 #include "net/proxy/proxy_service.h"
14 #include "net/ssl/default_server_bound_cert_store.h"
15 #include "net/ssl/server_bound_cert_service.h"
16 #include "net/ssl/ssl_config_service_defaults.h"
17 #include "net/url_request/file_protocol_handler.h"
18 #include "net/url_request/static_http_user_agent_settings.h"
19 #include "net/url_request/url_request_context.h"
20 #include "net/url_request/url_request_job_factory_impl.h"
27 const bool kTransportSecurityPersisterIsReadOnly
= false;
31 URLRequestContextGetter::URLRequestContextGetter(
32 base::FilePath base_path
,
33 base::SingleThreadTaskRunner
* network_task_runner
,
34 base::SingleThreadTaskRunner
* file_task_runner
,
35 base::MessageLoopProxy
* cache_task_runner
,
36 scoped_ptr
<net::NetworkDelegate
> network_delegate
)
37 : base_path_(base_path
),
38 file_task_runner_(file_task_runner
),
39 network_task_runner_(network_task_runner
),
40 cache_task_runner_(cache_task_runner
),
41 network_delegate_(network_delegate
.Pass()),
42 net_log_(new net::NetLog()) {
45 URLRequestContextGetter::~URLRequestContextGetter() {
48 net::URLRequestContext
* URLRequestContextGetter::GetURLRequestContext() {
49 if (!url_request_context_
) {
50 url_request_context_
.reset(new net::URLRequestContext());
51 url_request_context_
->set_net_log(net_log_
.get());
52 url_request_context_
->set_network_delegate(network_delegate_
.get());
55 new net::URLRequestContextStorage(url_request_context_
.get()));
57 storage_
->set_cookie_store(new net::CookieMonster(NULL
, NULL
));
58 storage_
->set_http_user_agent_settings(
59 new net::StaticHttpUserAgentSettings("en-us,en", "Mojo/0.1"));
61 storage_
->set_proxy_service(net::ProxyService::CreateDirect());
62 storage_
->set_ssl_config_service(new net::SSLConfigServiceDefaults
);
63 storage_
->set_cert_verifier(net::CertVerifier::CreateDefault());
65 net::TransportSecurityState
* transport_security_state
=
66 new net::TransportSecurityState();
67 storage_
->set_transport_security_state(transport_security_state
);
69 transport_security_persister_
.reset(
70 new net::TransportSecurityPersister(
71 transport_security_state
,
74 kTransportSecurityPersisterIsReadOnly
));
76 storage_
->set_server_bound_cert_service(new net::ServerBoundCertService(
77 new net::DefaultServerBoundCertStore(NULL
), file_task_runner_
));
78 storage_
->set_http_server_properties(
79 scoped_ptr
<net::HttpServerProperties
>(
80 new net::HttpServerPropertiesImpl()));
81 storage_
->set_host_resolver(net::HostResolver::CreateDefaultResolver(
82 url_request_context_
->net_log()));
84 net::HttpNetworkSession::Params network_session_params
;
85 network_session_params
.cert_verifier
=
86 url_request_context_
->cert_verifier();
87 network_session_params
.transport_security_state
=
88 url_request_context_
->transport_security_state();
89 network_session_params
.server_bound_cert_service
=
90 url_request_context_
->server_bound_cert_service();
91 network_session_params
.net_log
=
92 url_request_context_
->net_log();
93 network_session_params
.proxy_service
=
94 url_request_context_
->proxy_service();
95 network_session_params
.ssl_config_service
=
96 url_request_context_
->ssl_config_service();
97 network_session_params
.http_server_properties
=
98 url_request_context_
->http_server_properties();
99 network_session_params
.host_resolver
=
100 url_request_context_
->host_resolver();
102 base::FilePath cache_path
= base_path_
.Append(FILE_PATH_LITERAL("Cache"));
104 net::HttpCache::DefaultBackend
* main_backend
=
105 new net::HttpCache::DefaultBackend(
107 net::CACHE_BACKEND_DEFAULT
,
110 cache_task_runner_
.get());
112 net::HttpCache
* main_cache
= new net::HttpCache(
113 network_session_params
, main_backend
);
114 storage_
->set_http_transaction_factory(main_cache
);
116 scoped_ptr
<net::URLRequestJobFactoryImpl
> job_factory(
117 new net::URLRequestJobFactoryImpl());
118 job_factory
->SetProtocolHandler(
120 new net::FileProtocolHandler(file_task_runner_
));
121 storage_
->set_job_factory(job_factory
.release());
124 return url_request_context_
.get();
127 scoped_refptr
<base::SingleThreadTaskRunner
>
128 URLRequestContextGetter::GetNetworkTaskRunner() const {
129 return network_task_runner_
;