1 // Copyright 2014 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 "chromecast/browser/url_request_context_factory.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/macros.h"
10 #include "base/threading/worker_pool.h"
11 #include "chromecast/base/chromecast_switches.h"
12 #include "chromecast/browser/cast_http_user_agent_settings.h"
13 #include "chromecast/browser/cast_network_delegate.h"
14 #include "content/public/browser/browser_context.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/public/common/url_constants.h"
19 #include "net/cert/cert_verifier.h"
20 #include "net/cert_net/nss_ocsp.h"
21 #include "net/cookies/cookie_store.h"
22 #include "net/dns/host_resolver.h"
23 #include "net/http/http_auth_handler_factory.h"
24 #include "net/http/http_network_layer.h"
25 #include "net/http/http_server_properties_impl.h"
26 #include "net/http/http_stream_factory.h"
27 #include "net/proxy/proxy_service.h"
28 #include "net/socket/next_proto.h"
29 #include "net/ssl/channel_id_service.h"
30 #include "net/ssl/default_channel_id_store.h"
31 #include "net/ssl/ssl_config_service_defaults.h"
32 #include "net/url_request/data_protocol_handler.h"
33 #include "net/url_request/file_protocol_handler.h"
34 #include "net/url_request/url_request_context.h"
35 #include "net/url_request/url_request_context_getter.h"
36 #include "net/url_request/url_request_intercepting_job_factory.h"
37 #include "net/url_request/url_request_job_factory_impl.h"
39 namespace chromecast
{
44 const char kCookieStoreFile
[] = "Cookies";
48 // Private classes to expose URLRequestContextGetter that call back to the
49 // URLRequestContextFactory to create the URLRequestContext on demand.
51 // The URLRequestContextFactory::URLRequestContextGetter class is used for both
52 // the system and media URLRequestCotnexts.
53 class URLRequestContextFactory::URLRequestContextGetter
54 : public net::URLRequestContextGetter
{
56 URLRequestContextGetter(URLRequestContextFactory
* factory
, bool is_media
)
57 : is_media_(is_media
),
61 net::URLRequestContext
* GetURLRequestContext() override
{
62 if (!request_context_
) {
64 request_context_
.reset(factory_
->CreateMediaRequestContext());
66 request_context_
.reset(factory_
->CreateSystemRequestContext());
67 #if defined(USE_NSS_CERTS)
68 // Set request context used by NSS for Crl requests.
69 net::SetURLRequestContextForNSSHttpIO(request_context_
.get());
70 #endif // defined(USE_NSS_CERTS)
73 return request_context_
.get();
76 scoped_refptr
<base::SingleThreadTaskRunner
>
77 GetNetworkTaskRunner() const override
{
78 return content::BrowserThread::GetMessageLoopProxyForThread(
79 content::BrowserThread::IO
);
83 ~URLRequestContextGetter() override
{}
86 URLRequestContextFactory
* const factory_
;
87 scoped_ptr
<net::URLRequestContext
> request_context_
;
89 DISALLOW_COPY_AND_ASSIGN(URLRequestContextGetter
);
92 // The URLRequestContextFactory::MainURLRequestContextGetter class is used for
93 // the main URLRequestContext.
94 class URLRequestContextFactory::MainURLRequestContextGetter
95 : public net::URLRequestContextGetter
{
97 MainURLRequestContextGetter(
98 URLRequestContextFactory
* factory
,
99 content::BrowserContext
* browser_context
,
100 content::ProtocolHandlerMap
* protocol_handlers
,
101 content::URLRequestInterceptorScopedVector request_interceptors
)
102 : browser_context_(browser_context
),
104 request_interceptors_(request_interceptors
.Pass()) {
105 std::swap(protocol_handlers_
, *protocol_handlers
);
108 net::URLRequestContext
* GetURLRequestContext() override
{
109 if (!request_context_
) {
110 request_context_
.reset(factory_
->CreateMainRequestContext(
111 browser_context_
, &protocol_handlers_
, request_interceptors_
.Pass()));
112 protocol_handlers_
.clear();
114 return request_context_
.get();
117 scoped_refptr
<base::SingleThreadTaskRunner
>
118 GetNetworkTaskRunner() const override
{
119 return content::BrowserThread::GetMessageLoopProxyForThread(
120 content::BrowserThread::IO
);
124 ~MainURLRequestContextGetter() override
{}
126 content::BrowserContext
* const browser_context_
;
127 URLRequestContextFactory
* const factory_
;
128 content::ProtocolHandlerMap protocol_handlers_
;
129 content::URLRequestInterceptorScopedVector request_interceptors_
;
130 scoped_ptr
<net::URLRequestContext
> request_context_
;
132 DISALLOW_COPY_AND_ASSIGN(MainURLRequestContextGetter
);
135 URLRequestContextFactory::URLRequestContextFactory()
136 : app_network_delegate_(CastNetworkDelegate::Create()),
137 system_network_delegate_(CastNetworkDelegate::Create()),
138 system_dependencies_initialized_(false),
139 main_dependencies_initialized_(false),
140 media_dependencies_initialized_(false) {
143 URLRequestContextFactory::~URLRequestContextFactory() {
146 void URLRequestContextFactory::InitializeOnUIThread(net::NetLog
* net_log
) {
147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
148 // Cast http user agent settings must be initialized in UI thread
149 // because it registers itself to pref notification observer which is not
151 http_user_agent_settings_
.reset(new CastHttpUserAgentSettings());
153 // Proxy config service should be initialized in UI thread, since
154 // ProxyConfigServiceDelegate on Android expects UI thread.
155 proxy_config_service_
.reset(net::ProxyService::CreateSystemProxyConfigService(
156 content::BrowserThread::GetMessageLoopProxyForThread(
157 content::BrowserThread::IO
),
158 content::BrowserThread::GetMessageLoopProxyForThread(
159 content::BrowserThread::FILE)));
164 net::URLRequestContextGetter
* URLRequestContextFactory::CreateMainGetter(
165 content::BrowserContext
* browser_context
,
166 content::ProtocolHandlerMap
* protocol_handlers
,
167 content::URLRequestInterceptorScopedVector request_interceptors
) {
168 DCHECK(!main_getter_
.get())
169 << "Main URLRequestContextGetter already initialized";
170 main_getter_
= new MainURLRequestContextGetter(this,
173 request_interceptors
.Pass());
174 return main_getter_
.get();
177 net::URLRequestContextGetter
* URLRequestContextFactory::GetMainGetter() {
178 CHECK(main_getter_
.get());
179 return main_getter_
.get();
182 net::URLRequestContextGetter
* URLRequestContextFactory::GetSystemGetter() {
183 if (!system_getter_
.get()) {
184 system_getter_
= new URLRequestContextGetter(this, false);
186 return system_getter_
.get();
189 net::URLRequestContextGetter
* URLRequestContextFactory::GetMediaGetter() {
190 if (!media_getter_
.get()) {
191 media_getter_
= new URLRequestContextGetter(this, true);
193 return media_getter_
.get();
196 void URLRequestContextFactory::InitializeSystemContextDependencies() {
197 if (system_dependencies_initialized_
)
200 host_resolver_
= net::HostResolver::CreateDefaultResolver(NULL
);
202 // TODO(lcwu): http://crbug.com/392352. For performance and security reasons,
203 // a persistent (on-disk) HttpServerProperties and ChannelIDService might be
204 // desirable in the future.
205 channel_id_service_
.reset(
206 new net::ChannelIDService(new net::DefaultChannelIDStore(NULL
),
207 base::WorkerPool::GetTaskRunner(true)));
209 cert_verifier_
.reset(net::CertVerifier::CreateDefault());
211 ssl_config_service_
= new net::SSLConfigServiceDefaults
;
213 transport_security_state_
.reset(new net::TransportSecurityState());
214 http_auth_handler_factory_
.reset(
215 net::HttpAuthHandlerFactory::CreateDefault(host_resolver_
.get()));
217 http_server_properties_
.reset(new net::HttpServerPropertiesImpl
);
219 proxy_service_
.reset(net::ProxyService::CreateUsingSystemProxyResolver(
220 proxy_config_service_
.release(), 0, NULL
));
221 system_dependencies_initialized_
= true;
224 void URLRequestContextFactory::InitializeMainContextDependencies(
225 net::HttpTransactionFactory
* transaction_factory
,
226 content::ProtocolHandlerMap
* protocol_handlers
,
227 content::URLRequestInterceptorScopedVector request_interceptors
) {
228 if (main_dependencies_initialized_
)
231 main_transaction_factory_
.reset(transaction_factory
);
232 scoped_ptr
<net::URLRequestJobFactoryImpl
> job_factory(
233 new net::URLRequestJobFactoryImpl());
234 // Keep ProtocolHandlers added in sync with
235 // CastContentBrowserClient::IsHandledURL().
236 bool set_protocol
= false;
237 for (content::ProtocolHandlerMap::iterator it
= protocol_handlers
->begin();
238 it
!= protocol_handlers
->end();
240 set_protocol
= job_factory
->SetProtocolHandler(
241 it
->first
, it
->second
.release());
242 DCHECK(set_protocol
);
244 set_protocol
= job_factory
->SetProtocolHandler(
246 new net::DataProtocolHandler
);
247 DCHECK(set_protocol
);
249 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
250 switches::kEnableLocalFileAccesses
)) {
251 set_protocol
= job_factory
->SetProtocolHandler(
253 new net::FileProtocolHandler(
254 content::BrowserThread::GetBlockingPool()->
255 GetTaskRunnerWithShutdownBehavior(
256 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
)));
257 DCHECK(set_protocol
);
260 // Set up interceptors in the reverse order.
261 scoped_ptr
<net::URLRequestJobFactory
> top_job_factory
= job_factory
.Pass();
262 for (content::URLRequestInterceptorScopedVector::reverse_iterator i
=
263 request_interceptors
.rbegin();
264 i
!= request_interceptors
.rend();
266 top_job_factory
.reset(new net::URLRequestInterceptingJobFactory(
267 top_job_factory
.Pass(), make_scoped_ptr(*i
)));
269 request_interceptors
.weak_clear();
271 main_job_factory_
.reset(top_job_factory
.release());
273 main_dependencies_initialized_
= true;
276 void URLRequestContextFactory::InitializeMediaContextDependencies(
277 net::HttpTransactionFactory
* transaction_factory
) {
278 if (media_dependencies_initialized_
)
281 media_transaction_factory_
.reset(transaction_factory
);
282 media_dependencies_initialized_
= true;
285 void URLRequestContextFactory::PopulateNetworkSessionParams(
286 bool ignore_certificate_errors
,
287 net::HttpNetworkSession::Params
* params
) {
288 params
->host_resolver
= host_resolver_
.get();
289 params
->cert_verifier
= cert_verifier_
.get();
290 params
->channel_id_service
= channel_id_service_
.get();
291 params
->ssl_config_service
= ssl_config_service_
.get();
292 params
->transport_security_state
= transport_security_state_
.get();
293 params
->http_auth_handler_factory
= http_auth_handler_factory_
.get();
294 params
->http_server_properties
= http_server_properties_
->GetWeakPtr();
295 params
->ignore_certificate_errors
= ignore_certificate_errors
;
296 params
->proxy_service
= proxy_service_
.get();
298 // TODO(lcwu): http://crbug.com/329681. Remove this once spdy is enabled
299 // by default at the content level.
300 params
->next_protos
= net::NextProtosSpdy31();
301 params
->use_alternate_protocols
= true;
304 net::URLRequestContext
* URLRequestContextFactory::CreateSystemRequestContext() {
305 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
306 InitializeSystemContextDependencies();
307 net::HttpNetworkSession::Params system_params
;
308 PopulateNetworkSessionParams(false, &system_params
);
309 system_transaction_factory_
.reset(new net::HttpNetworkLayer(
310 new net::HttpNetworkSession(system_params
)));
311 system_job_factory_
.reset(new net::URLRequestJobFactoryImpl());
313 net::URLRequestContext
* system_context
= new net::URLRequestContext();
314 system_context
->set_host_resolver(host_resolver_
.get());
315 system_context
->set_channel_id_service(channel_id_service_
.get());
316 system_context
->set_cert_verifier(cert_verifier_
.get());
317 system_context
->set_proxy_service(proxy_service_
.get());
318 system_context
->set_ssl_config_service(ssl_config_service_
.get());
319 system_context
->set_transport_security_state(
320 transport_security_state_
.get());
321 system_context
->set_http_auth_handler_factory(
322 http_auth_handler_factory_
.get());
323 system_context
->set_http_server_properties(
324 http_server_properties_
->GetWeakPtr());
325 system_context
->set_http_transaction_factory(
326 system_transaction_factory_
.get());
327 system_context
->set_http_user_agent_settings(
328 http_user_agent_settings_
.get());
329 system_context
->set_job_factory(system_job_factory_
.get());
330 system_context
->set_cookie_store(
331 content::CreateCookieStore(content::CookieStoreConfig()));
332 system_context
->set_network_delegate(system_network_delegate_
.get());
333 system_context
->set_net_log(net_log_
);
334 return system_context
;
337 net::URLRequestContext
* URLRequestContextFactory::CreateMediaRequestContext() {
338 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
339 DCHECK(main_getter_
.get())
340 << "Getting MediaRequestContext before MainRequestContext";
341 net::URLRequestContext
* main_context
= main_getter_
->GetURLRequestContext();
343 // Set non caching backend.
344 net::HttpNetworkSession
* main_session
=
345 main_transaction_factory_
->GetSession();
346 InitializeMediaContextDependencies(
347 new net::HttpNetworkLayer(main_session
));
349 net::URLRequestContext
* media_context
= new net::URLRequestContext();
350 media_context
->CopyFrom(main_context
);
351 media_context
->set_http_transaction_factory(
352 media_transaction_factory_
.get());
353 media_context
->set_net_log(net_log_
);
354 return media_context
;
357 net::URLRequestContext
* URLRequestContextFactory::CreateMainRequestContext(
358 content::BrowserContext
* browser_context
,
359 content::ProtocolHandlerMap
* protocol_handlers
,
360 content::URLRequestInterceptorScopedVector request_interceptors
) {
361 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
362 InitializeSystemContextDependencies();
364 bool ignore_certificate_errors
= false;
365 base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
366 if (cmd_line
->HasSwitch(switches::kIgnoreCertificateErrors
)) {
367 ignore_certificate_errors
= true;
369 net::HttpNetworkSession::Params network_session_params
;
370 PopulateNetworkSessionParams(ignore_certificate_errors
,
371 &network_session_params
);
372 InitializeMainContextDependencies(
373 new net::HttpNetworkLayer(
374 new net::HttpNetworkSession(network_session_params
)),
376 request_interceptors
.Pass());
378 content::CookieStoreConfig
cookie_config(
379 browser_context
->GetPath().Append(kCookieStoreFile
),
380 content::CookieStoreConfig::PERSISTANT_SESSION_COOKIES
,
382 cookie_config
.background_task_runner
=
383 scoped_refptr
<base::SequencedTaskRunner
>();
384 scoped_refptr
<net::CookieStore
> cookie_store
=
385 content::CreateCookieStore(cookie_config
);
387 net::URLRequestContext
* main_context
= new net::URLRequestContext();
388 main_context
->set_host_resolver(host_resolver_
.get());
389 main_context
->set_channel_id_service(channel_id_service_
.get());
390 main_context
->set_cert_verifier(cert_verifier_
.get());
391 main_context
->set_proxy_service(proxy_service_
.get());
392 main_context
->set_ssl_config_service(ssl_config_service_
.get());
393 main_context
->set_transport_security_state(transport_security_state_
.get());
394 main_context
->set_http_auth_handler_factory(
395 http_auth_handler_factory_
.get());
396 main_context
->set_http_server_properties(
397 http_server_properties_
->GetWeakPtr());
398 main_context
->set_cookie_store(cookie_store
.get());
399 main_context
->set_http_user_agent_settings(
400 http_user_agent_settings_
.get());
402 main_context
->set_http_transaction_factory(
403 main_transaction_factory_
.get());
404 main_context
->set_job_factory(main_job_factory_
.get());
405 main_context
->set_network_delegate(app_network_delegate_
.get());
406 main_context
->set_net_log(net_log_
);
410 void URLRequestContextFactory::InitializeNetworkDelegates() {
411 app_network_delegate_
->Initialize(false);
412 LOG(INFO
) << "Initialized app network delegate.";
413 system_network_delegate_
->Initialize(false);
414 LOG(INFO
) << "Initialized system network delegate.";
418 } // namespace chromecast