1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// found in the LICENSE file.
4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chromecast/shell/browser/url_request_context_factory.h"
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/command_line.h"
8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/files/file_path.h"
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/macros.h"
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/path_service.h"
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/threading/worker_pool.h"
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chromecast/shell/browser/cast_http_user_agent_settings.h"
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/public/browser/browser_context.h"
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/public/browser/browser_thread.h"
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/public/browser/cookie_store_factory.h"
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/public/common/content_switches.h"
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/public/common/url_constants.h"
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/cert/cert_verifier.h"
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/cookies/cookie_store.h"
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/dns/host_resolver.h"
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/http/http_auth_handler_factory.h"
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/http/http_cache.h"
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/http/http_network_layer.h"
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/http/http_server_properties_impl.h"
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/http/http_stream_factory.h"
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/ocsp/nss_ocsp.h"
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/proxy/proxy_service.h"
28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/socket/next_proto.h"
295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "net/ssl/channel_id_service.h"
305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "net/ssl/default_channel_id_store.h"
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/ssl/ssl_config_service_defaults.h"
32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/url_request/data_protocol_handler.h"
331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "net/url_request/file_protocol_handler.h"
34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/url_request/url_request_context.h"
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/url_request/url_request_context_getter.h"
36116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/url_request/url_request_intercepting_job_factory.h"
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/url_request/url_request_job_factory_impl.h"
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
39116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace chromecast {
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace shell {
41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
42116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace {
43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdochconst char kCookieStoreFile[] = "Cookies";
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Private classes to expose URLRequestContextGetter that call back to the
49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// URLRequestContextFactory to create the URLRequestContext on demand.
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch//
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// The URLRequestContextFactory::URLRequestContextGetter class is used for both
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// the system and media URLRequestCotnexts.
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass URLRequestContextFactory::URLRequestContextGetter
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    : public net::URLRequestContextGetter {
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch public:
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  URLRequestContextGetter(URLRequestContextFactory* factory, bool is_media)
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      : is_media_(is_media),
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        factory_(factory) {
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE {
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (!request_context_) {
63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      if (is_media_) {
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        request_context_.reset(factory_->CreateMediaRequestContext());
65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      } else {
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        request_context_.reset(factory_->CreateSystemRequestContext());
671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(USE_NSS)
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        // Set request context used by NSS for Crl requests.
69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        net::SetURLRequestContextForNSSHttpIO(request_context_.get());
701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif  // defined(USE_NSS)
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      }
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    }
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return request_context_.get();
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual scoped_refptr<base::SingleThreadTaskRunner>
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      GetNetworkTaskRunner() const OVERRIDE {
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return content::BrowserThread::GetMessageLoopProxyForThread(
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        content::BrowserThread::IO);
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch private:
83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual ~URLRequestContextGetter() {}
84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const bool is_media_;
86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  URLRequestContextFactory* const factory_;
87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<net::URLRequestContext> request_context_;
88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DISALLOW_COPY_AND_ASSIGN(URLRequestContextGetter);
90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch};
91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// The URLRequestContextFactory::MainURLRequestContextGetter class is used for
93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// the main URLRequestContext.
94116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass URLRequestContextFactory::MainURLRequestContextGetter
95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    : public net::URLRequestContextGetter {
96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch public:
97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  MainURLRequestContextGetter(
98116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      URLRequestContextFactory* factory,
99116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      content::BrowserContext* browser_context,
100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      content::ProtocolHandlerMap* protocol_handlers,
101116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      content::URLRequestInterceptorScopedVector request_interceptors)
102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      : browser_context_(browser_context),
103116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        factory_(factory),
104116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        request_interceptors_(request_interceptors.Pass()) {
105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    std::swap(protocol_handlers_, *protocol_handlers);
106116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
108116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE {
109116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (!request_context_) {
110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      request_context_.reset(factory_->CreateMainRequestContext(
111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          browser_context_, &protocol_handlers_, request_interceptors_.Pass()));
112116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      protocol_handlers_.clear();
113116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    }
114116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return request_context_.get();
115116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
116116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
117116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual scoped_refptr<base::SingleThreadTaskRunner>
118116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      GetNetworkTaskRunner() const OVERRIDE {
119116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return content::BrowserThread::GetMessageLoopProxyForThread(
120116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        content::BrowserThread::IO);
121116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
122116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
123116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch private:
124116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual ~MainURLRequestContextGetter() {}
125116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
126116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  content::BrowserContext* const browser_context_;
127116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  URLRequestContextFactory* const factory_;
128116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  content::ProtocolHandlerMap protocol_handlers_;
129116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  content::URLRequestInterceptorScopedVector request_interceptors_;
130116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<net::URLRequestContext> request_context_;
131116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
132116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DISALLOW_COPY_AND_ASSIGN(MainURLRequestContextGetter);
133116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch};
134116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
135116680a4aac90f2aa7413d9095a592090648e557Ben MurdochURLRequestContextFactory::URLRequestContextFactory()
136116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    : system_dependencies_initialized_(false),
137116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      main_dependencies_initialized_(false),
138116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      media_dependencies_initialized_(false) {
139116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
140116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
141116680a4aac90f2aa7413d9095a592090648e557Ben MurdochURLRequestContextFactory::~URLRequestContextFactory() {
142116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
143116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
144116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid URLRequestContextFactory::InitializeOnUIThread() {
145116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
146116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Cast http user agent settings must be initialized in UI thread
147116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // because it registers itself to pref notification observer which is not
148116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // thread safe.
149116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  http_user_agent_settings_.reset(new CastHttpUserAgentSettings());
1501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Proxy config service should be initialized in UI thread, since
1521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // ProxyConfigServiceDelegate on Android expects UI thread.
1531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService(
1541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      content::BrowserThread::GetMessageLoopProxyForThread(
1551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          content::BrowserThread::IO),
1561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      content::BrowserThread::GetMessageLoopProxyForThread(
1571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          content::BrowserThread::FILE)));
158116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
159116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
160116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnet::URLRequestContextGetter* URLRequestContextFactory::CreateMainGetter(
161116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    content::BrowserContext* browser_context,
162116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    content::ProtocolHandlerMap* protocol_handlers,
163116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    content::URLRequestInterceptorScopedVector request_interceptors) {
1641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK(!main_getter_.get())
1651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      << "Main URLRequestContextGetter already initialized";
166116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  main_getter_ = new MainURLRequestContextGetter(this,
167116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                                 browser_context,
168116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                                 protocol_handlers,
169116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                                 request_interceptors.Pass());
170116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return main_getter_.get();
171116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
172116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
173116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnet::URLRequestContextGetter* URLRequestContextFactory::GetMainGetter() {
1741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  CHECK(main_getter_.get());
175116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return main_getter_.get();
176116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
177116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
178116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnet::URLRequestContextGetter* URLRequestContextFactory::GetSystemGetter() {
1791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!system_getter_.get()) {
180116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    system_getter_ = new URLRequestContextGetter(this, false);
181116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
182116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return system_getter_.get();
183116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
184116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
185116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnet::URLRequestContextGetter* URLRequestContextFactory::GetMediaGetter() {
1861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!media_getter_.get()) {
187116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    media_getter_ = new URLRequestContextGetter(this, true);
188116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
189116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return media_getter_.get();
190116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
191116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
192116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid URLRequestContextFactory::InitializeSystemContextDependencies() {
193116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (system_dependencies_initialized_)
194116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return;
195116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
196116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  host_resolver_ = net::HostResolver::CreateDefaultResolver(NULL);
197116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
198116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // TODO(lcwu): http://crbug.com/392352. For performance and security reasons,
1995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // a persistent (on-disk) HttpServerProperties and ChannelIDService might be
2005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // desirable in the future.
2015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  channel_id_service_.reset(
2025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      new net::ChannelIDService(new net::DefaultChannelIDStore(NULL),
2035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                base::WorkerPool::GetTaskRunner(true)));
204116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
205116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  cert_verifier_.reset(net::CertVerifier::CreateDefault());
206116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
207116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ssl_config_service_ = new net::SSLConfigServiceDefaults;
208116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
209116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  transport_security_state_.reset(new net::TransportSecurityState());
210116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  http_auth_handler_factory_.reset(
211116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      net::HttpAuthHandlerFactory::CreateDefault(host_resolver_.get()));
212116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
213116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  http_server_properties_.reset(new net::HttpServerPropertiesImpl);
214116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
215116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  proxy_service_.reset(net::ProxyService::CreateUsingSystemProxyResolver(
2161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      proxy_config_service_.release(), 0, NULL));
217116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  system_dependencies_initialized_ = true;
218116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
219116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
220116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid URLRequestContextFactory::InitializeMainContextDependencies(
221116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    net::HttpTransactionFactory* transaction_factory,
222116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    content::ProtocolHandlerMap* protocol_handlers,
223116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    content::URLRequestInterceptorScopedVector request_interceptors) {
224116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (main_dependencies_initialized_)
225116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return;
226116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
227116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  main_transaction_factory_.reset(transaction_factory);
228116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
229116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      new net::URLRequestJobFactoryImpl());
230116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Keep ProtocolHandlers added in sync with
231116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // CastContentBrowserClient::IsHandledURL().
232116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool set_protocol = false;
233116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  for (content::ProtocolHandlerMap::iterator it = protocol_handlers->begin();
234116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch       it != protocol_handlers->end();
235116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch       ++it) {
236116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    set_protocol = job_factory->SetProtocolHandler(
237116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        it->first, it->second.release());
238116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    DCHECK(set_protocol);
239116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
240116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  set_protocol = job_factory->SetProtocolHandler(
241116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      url::kDataScheme,
242116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      new net::DataProtocolHandler);
243116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DCHECK(set_protocol);
2441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(OS_ANDROID)
2451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  set_protocol = job_factory->SetProtocolHandler(
2461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      url::kFileScheme,
2471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      new net::FileProtocolHandler(
2481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          content::BrowserThread::GetBlockingPool()->
2491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci              GetTaskRunnerWithShutdownBehavior(
2501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
2511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK(set_protocol);
2521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif  // defined(OS_ANDROID)
253116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
254116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Set up interceptors in the reverse order.
255116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<net::URLRequestJobFactory> top_job_factory =
256116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      job_factory.PassAs<net::URLRequestJobFactory>();
257116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  for (content::URLRequestInterceptorScopedVector::reverse_iterator i =
258116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch           request_interceptors.rbegin();
259116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch       i != request_interceptors.rend();
260116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch       ++i) {
261116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    top_job_factory.reset(new net::URLRequestInterceptingJobFactory(
262116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        top_job_factory.Pass(), make_scoped_ptr(*i)));
263116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
264116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  request_interceptors.weak_clear();
265116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
266116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  main_job_factory_.reset(top_job_factory.release());
267116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
268116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  main_dependencies_initialized_ = true;
269116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
270116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
271116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid URLRequestContextFactory::InitializeMediaContextDependencies(
272116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    net::HttpTransactionFactory* transaction_factory) {
273116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (media_dependencies_initialized_)
274116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return;
275116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
276116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  media_transaction_factory_.reset(transaction_factory);
277116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  media_dependencies_initialized_ = true;
278116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
279116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
280116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid URLRequestContextFactory::PopulateNetworkSessionParams(
281116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    bool ignore_certificate_errors,
282116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    net::HttpNetworkSession::Params* params) {
283116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  params->host_resolver = host_resolver_.get();
284116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  params->cert_verifier = cert_verifier_.get();
2855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  params->channel_id_service = channel_id_service_.get();
286116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  params->ssl_config_service = ssl_config_service_.get();
287116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  params->transport_security_state = transport_security_state_.get();
288116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  params->http_auth_handler_factory = http_auth_handler_factory_.get();
289116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  params->http_server_properties = http_server_properties_->GetWeakPtr();
290116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  params->ignore_certificate_errors = ignore_certificate_errors;
291116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  params->proxy_service = proxy_service_.get();
292116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
293116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // TODO(lcwu): http://crbug.com/329681. Remove this once spdy is enabled
294116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // by default at the content level.
295116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  params->next_protos = net::NextProtosSpdy31();
296116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  params->use_alternate_protocols = true;
297116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
298116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
299116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnet::URLRequestContext* URLRequestContextFactory::CreateSystemRequestContext() {
300116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
301116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  InitializeSystemContextDependencies();
302116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  net::HttpNetworkSession::Params system_params;
303116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  PopulateNetworkSessionParams(false, &system_params);
304116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  system_transaction_factory_.reset(new net::HttpNetworkLayer(
305116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      new net::HttpNetworkSession(system_params)));
3061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  system_job_factory_.reset(new net::URLRequestJobFactoryImpl());
307116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
308116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  net::URLRequestContext* system_context = new net::URLRequestContext();
309116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  system_context->set_host_resolver(host_resolver_.get());
3105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  system_context->set_channel_id_service(channel_id_service_.get());
311116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  system_context->set_cert_verifier(cert_verifier_.get());
312116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  system_context->set_proxy_service(proxy_service_.get());
313116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  system_context->set_ssl_config_service(ssl_config_service_.get());
314116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  system_context->set_transport_security_state(
315116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      transport_security_state_.get());
316116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  system_context->set_http_auth_handler_factory(
317116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      http_auth_handler_factory_.get());
318116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  system_context->set_http_server_properties(
319116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      http_server_properties_->GetWeakPtr());
320116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  system_context->set_http_transaction_factory(
321116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      system_transaction_factory_.get());
322116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  system_context->set_http_user_agent_settings(
323116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      http_user_agent_settings_.get());
3241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  system_context->set_job_factory(system_job_factory_.get());
325116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  system_context->set_cookie_store(
326116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      content::CreateCookieStore(content::CookieStoreConfig()));
327116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return system_context;
328116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
329116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
330116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnet::URLRequestContext* URLRequestContextFactory::CreateMediaRequestContext() {
331116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
3321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK(main_getter_.get())
333116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      << "Getting MediaRequestContext before MainRequestContext";
334116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  net::URLRequestContext* main_context = main_getter_->GetURLRequestContext();
335116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
336116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Set non caching backend.
337116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  net::HttpNetworkSession* main_session =
338116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      main_transaction_factory_->GetSession();
339116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  InitializeMediaContextDependencies(
340116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      new net::HttpNetworkLayer(main_session));
341116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
342116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  net::URLRequestContext* media_context = new net::URLRequestContext();
343116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  media_context->CopyFrom(main_context);
344116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  media_context->set_http_transaction_factory(
345116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      media_transaction_factory_.get());
346116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return media_context;
347116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
348116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
349116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnet::URLRequestContext* URLRequestContextFactory::CreateMainRequestContext(
350116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    content::BrowserContext* browser_context,
351116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    content::ProtocolHandlerMap* protocol_handlers,
352116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    content::URLRequestInterceptorScopedVector request_interceptors) {
353116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
354116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  InitializeSystemContextDependencies();
355116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
356116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  net::HttpCache::BackendFactory* main_backend =
357116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      net::HttpCache::DefaultBackend::InMemory(16 * 1024 * 1024);
358116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
359116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool ignore_certificate_errors = false;
360116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  CommandLine* cmd_line = CommandLine::ForCurrentProcess();
361116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (cmd_line->HasSwitch(switches::kIgnoreCertificateErrors)) {
362116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    ignore_certificate_errors = true;
363116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
364116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  net::HttpNetworkSession::Params network_session_params;
365116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  PopulateNetworkSessionParams(ignore_certificate_errors,
366116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                               &network_session_params);
367116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  InitializeMainContextDependencies(
368116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      new net::HttpCache(network_session_params, main_backend),
369116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      protocol_handlers,
370116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      request_interceptors.Pass());
371116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
372116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  content::CookieStoreConfig cookie_config(
373116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      browser_context->GetPath().Append(kCookieStoreFile),
374116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      content::CookieStoreConfig::PERSISTANT_SESSION_COOKIES,
375116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      NULL, NULL);
376116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  cookie_config.background_task_runner =
377116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      scoped_refptr<base::SequencedTaskRunner>();
378116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_refptr<net::CookieStore> cookie_store =
379116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      content::CreateCookieStore(cookie_config);
380116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
381116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  net::URLRequestContext* main_context = new net::URLRequestContext();
382116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  main_context->set_host_resolver(host_resolver_.get());
3835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  main_context->set_channel_id_service(channel_id_service_.get());
384116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  main_context->set_cert_verifier(cert_verifier_.get());
385116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  main_context->set_proxy_service(proxy_service_.get());
386116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  main_context->set_ssl_config_service(ssl_config_service_.get());
387116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  main_context->set_transport_security_state(transport_security_state_.get());
388116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  main_context->set_http_auth_handler_factory(
389116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      http_auth_handler_factory_.get());
390116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  main_context->set_http_server_properties(
391116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      http_server_properties_->GetWeakPtr());
392116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  main_context->set_cookie_store(cookie_store.get());
393116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  main_context->set_http_user_agent_settings(
394116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      http_user_agent_settings_.get());
395116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
396116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  main_context->set_http_transaction_factory(
397116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      main_transaction_factory_.get());
398116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  main_context->set_job_factory(main_job_factory_.get());
399116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return main_context;
400116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
401116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
402116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace shell
403116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace chromecast
404