Fix building NaCl in Linux component GN builds.
[chromium-blink-merge.git] / chromecast / browser / url_request_context_factory.cc
blob9143071fb8e65bb687c9422799d5ac6cd34f3fc4
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/browser/cast_http_user_agent_settings.h"
12 #include "chromecast/browser/cast_network_delegate.h"
13 #include "chromecast/common/chromecast_switches.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 {
40 namespace shell {
42 namespace {
44 const char kCookieStoreFile[] = "Cookies";
46 } // namespace
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 {
55 public:
56 URLRequestContextGetter(URLRequestContextFactory* factory, bool is_media)
57 : is_media_(is_media),
58 factory_(factory) {
61 net::URLRequestContext* GetURLRequestContext() override {
62 if (!request_context_) {
63 if (is_media_) {
64 request_context_.reset(factory_->CreateMediaRequestContext());
65 } else {
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);
82 private:
83 ~URLRequestContextGetter() override {}
85 const bool is_media_;
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 {
96 public:
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),
103 factory_(factory),
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);
123 private:
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() {
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
150 // thread safe.
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)));
162 net::URLRequestContextGetter* URLRequestContextFactory::CreateMainGetter(
163 content::BrowserContext* browser_context,
164 content::ProtocolHandlerMap* protocol_handlers,
165 content::URLRequestInterceptorScopedVector request_interceptors) {
166 DCHECK(!main_getter_.get())
167 << "Main URLRequestContextGetter already initialized";
168 main_getter_ = new MainURLRequestContextGetter(this,
169 browser_context,
170 protocol_handlers,
171 request_interceptors.Pass());
172 return main_getter_.get();
175 net::URLRequestContextGetter* URLRequestContextFactory::GetMainGetter() {
176 CHECK(main_getter_.get());
177 return main_getter_.get();
180 net::URLRequestContextGetter* URLRequestContextFactory::GetSystemGetter() {
181 if (!system_getter_.get()) {
182 system_getter_ = new URLRequestContextGetter(this, false);
184 return system_getter_.get();
187 net::URLRequestContextGetter* URLRequestContextFactory::GetMediaGetter() {
188 if (!media_getter_.get()) {
189 media_getter_ = new URLRequestContextGetter(this, true);
191 return media_getter_.get();
194 void URLRequestContextFactory::InitializeSystemContextDependencies() {
195 if (system_dependencies_initialized_)
196 return;
198 host_resolver_ = net::HostResolver::CreateDefaultResolver(NULL);
200 // TODO(lcwu): http://crbug.com/392352. For performance and security reasons,
201 // a persistent (on-disk) HttpServerProperties and ChannelIDService might be
202 // desirable in the future.
203 channel_id_service_.reset(
204 new net::ChannelIDService(new net::DefaultChannelIDStore(NULL),
205 base::WorkerPool::GetTaskRunner(true)));
207 cert_verifier_.reset(net::CertVerifier::CreateDefault());
209 ssl_config_service_ = new net::SSLConfigServiceDefaults;
211 transport_security_state_.reset(new net::TransportSecurityState());
212 http_auth_handler_factory_.reset(
213 net::HttpAuthHandlerFactory::CreateDefault(host_resolver_.get()));
215 http_server_properties_.reset(new net::HttpServerPropertiesImpl);
217 proxy_service_.reset(net::ProxyService::CreateUsingSystemProxyResolver(
218 proxy_config_service_.release(), 0, NULL));
219 system_dependencies_initialized_ = true;
222 void URLRequestContextFactory::InitializeMainContextDependencies(
223 net::HttpTransactionFactory* transaction_factory,
224 content::ProtocolHandlerMap* protocol_handlers,
225 content::URLRequestInterceptorScopedVector request_interceptors) {
226 if (main_dependencies_initialized_)
227 return;
229 main_transaction_factory_.reset(transaction_factory);
230 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
231 new net::URLRequestJobFactoryImpl());
232 // Keep ProtocolHandlers added in sync with
233 // CastContentBrowserClient::IsHandledURL().
234 bool set_protocol = false;
235 for (content::ProtocolHandlerMap::iterator it = protocol_handlers->begin();
236 it != protocol_handlers->end();
237 ++it) {
238 set_protocol = job_factory->SetProtocolHandler(
239 it->first, it->second.release());
240 DCHECK(set_protocol);
242 set_protocol = job_factory->SetProtocolHandler(
243 url::kDataScheme,
244 new net::DataProtocolHandler);
245 DCHECK(set_protocol);
247 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
248 switches::kEnableLocalFileAccesses)) {
249 set_protocol = job_factory->SetProtocolHandler(
250 url::kFileScheme,
251 new net::FileProtocolHandler(
252 content::BrowserThread::GetBlockingPool()->
253 GetTaskRunnerWithShutdownBehavior(
254 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
255 DCHECK(set_protocol);
258 // Set up interceptors in the reverse order.
259 scoped_ptr<net::URLRequestJobFactory> top_job_factory = job_factory.Pass();
260 for (content::URLRequestInterceptorScopedVector::reverse_iterator i =
261 request_interceptors.rbegin();
262 i != request_interceptors.rend();
263 ++i) {
264 top_job_factory.reset(new net::URLRequestInterceptingJobFactory(
265 top_job_factory.Pass(), make_scoped_ptr(*i)));
267 request_interceptors.weak_clear();
269 main_job_factory_.reset(top_job_factory.release());
271 main_dependencies_initialized_ = true;
274 void URLRequestContextFactory::InitializeMediaContextDependencies(
275 net::HttpTransactionFactory* transaction_factory) {
276 if (media_dependencies_initialized_)
277 return;
279 media_transaction_factory_.reset(transaction_factory);
280 media_dependencies_initialized_ = true;
283 void URLRequestContextFactory::PopulateNetworkSessionParams(
284 bool ignore_certificate_errors,
285 net::HttpNetworkSession::Params* params) {
286 params->host_resolver = host_resolver_.get();
287 params->cert_verifier = cert_verifier_.get();
288 params->channel_id_service = channel_id_service_.get();
289 params->ssl_config_service = ssl_config_service_.get();
290 params->transport_security_state = transport_security_state_.get();
291 params->http_auth_handler_factory = http_auth_handler_factory_.get();
292 params->http_server_properties = http_server_properties_->GetWeakPtr();
293 params->ignore_certificate_errors = ignore_certificate_errors;
294 params->proxy_service = proxy_service_.get();
296 // TODO(lcwu): http://crbug.com/329681. Remove this once spdy is enabled
297 // by default at the content level.
298 params->next_protos = net::NextProtosSpdy31();
299 params->use_alternate_protocols = true;
302 net::URLRequestContext* URLRequestContextFactory::CreateSystemRequestContext() {
303 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
304 InitializeSystemContextDependencies();
305 net::HttpNetworkSession::Params system_params;
306 PopulateNetworkSessionParams(false, &system_params);
307 system_transaction_factory_.reset(new net::HttpNetworkLayer(
308 new net::HttpNetworkSession(system_params)));
309 system_job_factory_.reset(new net::URLRequestJobFactoryImpl());
311 net::URLRequestContext* system_context = new net::URLRequestContext();
312 system_context->set_host_resolver(host_resolver_.get());
313 system_context->set_channel_id_service(channel_id_service_.get());
314 system_context->set_cert_verifier(cert_verifier_.get());
315 system_context->set_proxy_service(proxy_service_.get());
316 system_context->set_ssl_config_service(ssl_config_service_.get());
317 system_context->set_transport_security_state(
318 transport_security_state_.get());
319 system_context->set_http_auth_handler_factory(
320 http_auth_handler_factory_.get());
321 system_context->set_http_server_properties(
322 http_server_properties_->GetWeakPtr());
323 system_context->set_http_transaction_factory(
324 system_transaction_factory_.get());
325 system_context->set_http_user_agent_settings(
326 http_user_agent_settings_.get());
327 system_context->set_job_factory(system_job_factory_.get());
328 system_context->set_cookie_store(
329 content::CreateCookieStore(content::CookieStoreConfig()));
330 system_context->set_network_delegate(system_network_delegate_.get());
331 return system_context;
334 net::URLRequestContext* URLRequestContextFactory::CreateMediaRequestContext() {
335 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
336 DCHECK(main_getter_.get())
337 << "Getting MediaRequestContext before MainRequestContext";
338 net::URLRequestContext* main_context = main_getter_->GetURLRequestContext();
340 // Set non caching backend.
341 net::HttpNetworkSession* main_session =
342 main_transaction_factory_->GetSession();
343 InitializeMediaContextDependencies(
344 new net::HttpNetworkLayer(main_session));
346 net::URLRequestContext* media_context = new net::URLRequestContext();
347 media_context->CopyFrom(main_context);
348 media_context->set_http_transaction_factory(
349 media_transaction_factory_.get());
350 return media_context;
353 net::URLRequestContext* URLRequestContextFactory::CreateMainRequestContext(
354 content::BrowserContext* browser_context,
355 content::ProtocolHandlerMap* protocol_handlers,
356 content::URLRequestInterceptorScopedVector request_interceptors) {
357 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
358 InitializeSystemContextDependencies();
360 bool ignore_certificate_errors = false;
361 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
362 if (cmd_line->HasSwitch(switches::kIgnoreCertificateErrors)) {
363 ignore_certificate_errors = true;
365 net::HttpNetworkSession::Params network_session_params;
366 PopulateNetworkSessionParams(ignore_certificate_errors,
367 &network_session_params);
368 InitializeMainContextDependencies(
369 new net::HttpNetworkLayer(
370 new net::HttpNetworkSession(network_session_params)),
371 protocol_handlers,
372 request_interceptors.Pass());
374 content::CookieStoreConfig cookie_config(
375 browser_context->GetPath().Append(kCookieStoreFile),
376 content::CookieStoreConfig::PERSISTANT_SESSION_COOKIES,
377 NULL, NULL);
378 cookie_config.background_task_runner =
379 scoped_refptr<base::SequencedTaskRunner>();
380 scoped_refptr<net::CookieStore> cookie_store =
381 content::CreateCookieStore(cookie_config);
383 net::URLRequestContext* main_context = new net::URLRequestContext();
384 main_context->set_host_resolver(host_resolver_.get());
385 main_context->set_channel_id_service(channel_id_service_.get());
386 main_context->set_cert_verifier(cert_verifier_.get());
387 main_context->set_proxy_service(proxy_service_.get());
388 main_context->set_ssl_config_service(ssl_config_service_.get());
389 main_context->set_transport_security_state(transport_security_state_.get());
390 main_context->set_http_auth_handler_factory(
391 http_auth_handler_factory_.get());
392 main_context->set_http_server_properties(
393 http_server_properties_->GetWeakPtr());
394 main_context->set_cookie_store(cookie_store.get());
395 main_context->set_http_user_agent_settings(
396 http_user_agent_settings_.get());
398 main_context->set_http_transaction_factory(
399 main_transaction_factory_.get());
400 main_context->set_job_factory(main_job_factory_.get());
401 main_context->set_network_delegate(app_network_delegate_.get());
402 return main_context;
405 void URLRequestContextFactory::InitializeNetworkDelegates() {
406 app_network_delegate_->Initialize(false);
407 LOG(INFO) << "Initialized app network delegate.";
408 system_network_delegate_->Initialize(false);
409 LOG(INFO) << "Initialized system network delegate.";
412 } // namespace shell
413 } // namespace chromecast