Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(691)

Side by Side Diff: chromecast/shell/browser/url_request_context_factory.cc

Issue 223143003: Initial checkin of chromecast content embedder (cast_shell) and related build scripts. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove ScreenCast code and stale comments. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 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.
4
5 #include "chromecast/shell/browser/url_request_context_factory.h"
6
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/path_service.h"
10 #include "base/threading/worker_pool.h"
11 #include "chromecast/shell/browser/cast_http_user_agent_settings.h"
12 #include "content/public/browser/browser_context.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/cookie_store_factory.h"
15 #include "content/public/common/content_switches.h"
16 #include "content/public/common/url_constants.h"
17 #include "net/cert/cert_verifier.h"
18 #include "net/cookies/cookie_store.h"
19 #include "net/dns/host_resolver.h"
20 #include "net/http/http_auth_handler_factory.h"
21 #include "net/http/http_cache.h"
22 #include "net/http/http_network_layer.h"
23 #include "net/http/http_server_properties_impl.h"
24 #include "net/http/http_stream_factory.h"
25 #include "net/ocsp/nss_ocsp.h"
26 #include "net/proxy/proxy_service.h"
27 #include "net/socket/next_proto.h"
28 #include "net/ssl/default_server_bound_cert_store.h"
29 #include "net/ssl/server_bound_cert_service.h"
30 #include "net/ssl/ssl_config_service_defaults.h"
31 #include "net/url_request/data_protocol_handler.h"
32 #include "net/url_request/url_request_context.h"
33 #include "net/url_request/url_request_context_getter.h"
34 #include "net/url_request/url_request_job_factory_impl.h"
35
36 namespace chromecast {
37 namespace shell {
38
39 namespace {
40
41 const char kCookieStoreFile[] = "Cookies";
42
43 } // namespace
44
45 // Private classes to expose URLRequestContextGetter that call back to the
46 // URLRequestContextFactory to create the URLRequestContext on demand.
47 //
48 // The URLRequestContextFactory::URLRequestContextGetter class is used for both
49 // the system and media URLRequestCotnexts.
50 class URLRequestContextFactory::URLRequestContextGetter
51 : public net::URLRequestContextGetter {
52 public:
53 URLRequestContextGetter(URLRequestContextFactory* factory, bool is_media)
54 : is_media_(is_media),
55 factory_(factory) {
56 }
57
58 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE {
59 if (!request_context_) {
60 if (is_media_) {
61 request_context_.reset(factory_->CreateMediaRequestContext());
62 } else {
63 request_context_.reset(factory_->CreateSystemRequestContext());
64 // Set request context used by NSS for Crl requests.
65 net::SetURLRequestContextForNSSHttpIO(request_context_.get());
66 }
67 }
68 return request_context_.get();
69 }
70
71 virtual scoped_refptr<base::SingleThreadTaskRunner>
72 GetNetworkTaskRunner() const OVERRIDE {
73 return content::BrowserThread::GetMessageLoopProxyForThread(
74 content::BrowserThread::IO);
75 }
76
77 private:
78 virtual ~URLRequestContextGetter() {}
79
80 const bool is_media_;
81 URLRequestContextFactory* const factory_;
82 scoped_ptr<net::URLRequestContext> request_context_;
83
84 DISALLOW_COPY_AND_ASSIGN(URLRequestContextGetter);
85 };
86
87 // The URLRequestContextFactory::MainURLRequestContextGetter class is used for
88 // the main URLRequestContext.
89 class URLRequestContextFactory::MainURLRequestContextGetter
90 : public net::URLRequestContextGetter {
91 public:
92 MainURLRequestContextGetter(URLRequestContextFactory* factory,
93 content::BrowserContext* browser_context,
94 content::ProtocolHandlerMap* protocol_handlers)
95 : browser_context_(browser_context),
96 factory_(factory) {
97 std::swap(protocol_handlers_, *protocol_handlers);
98 }
99
100 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE {
101 if (!request_context_) {
102 request_context_.reset(factory_->CreateMainRequestContext(
103 browser_context_, &protocol_handlers_));
104 protocol_handlers_.clear();
105 }
106 return request_context_.get();
107 }
108
109 virtual scoped_refptr<base::SingleThreadTaskRunner>
110 GetNetworkTaskRunner() const OVERRIDE {
111 return content::BrowserThread::GetMessageLoopProxyForThread(
112 content::BrowserThread::IO);
113 }
114
115 private:
116 virtual ~MainURLRequestContextGetter() {}
117
118 content::BrowserContext* const browser_context_;
119 URLRequestContextFactory* const factory_;
120 content::ProtocolHandlerMap protocol_handlers_;
121 scoped_ptr<net::URLRequestContext> request_context_;
122
123 DISALLOW_COPY_AND_ASSIGN(MainURLRequestContextGetter);
124 };
125
126 URLRequestContextFactory::URLRequestContextFactory()
127 : system_dependencies_initialized_(false),
128 main_dependencies_initialized_(false),
129 media_dependencies_initialized_(false) {
130 }
131
132 URLRequestContextFactory::~URLRequestContextFactory() {
133 }
134
135 void URLRequestContextFactory::InitializeOnUIThread() {
136 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
137 // Cast http user agent settings must be initialized in UI thread
138 // because it registers itself to pref notification observer which is not
139 // thread safe.
140 http_user_agent_settings_.reset(new CastHttpUserAgentSettings());
141 }
142
143 net::URLRequestContextGetter* URLRequestContextFactory::CreateMainGetter(
144 content::BrowserContext* browser_context,
145 content::ProtocolHandlerMap* protocol_handlers) {
146 DCHECK(!main_getter_) << "Main URLRequestContextGetter already initialized";
147 main_getter_ = new MainURLRequestContextGetter(this,
148 browser_context,
149 protocol_handlers);
150 return main_getter_.get();
151 }
152
153 net::URLRequestContextGetter* URLRequestContextFactory::GetMainGetter() {
154 CHECK(main_getter_);
155 return main_getter_.get();
156 }
157
158 net::URLRequestContextGetter* URLRequestContextFactory::GetSystemGetter() {
159 if (!system_getter_) {
160 system_getter_ = new URLRequestContextGetter(this, false);
161 }
162 return system_getter_.get();
163 }
164
165 net::URLRequestContextGetter* URLRequestContextFactory::GetMediaGetter() {
166 if (!media_getter_) {
167 media_getter_ = new URLRequestContextGetter(this, true);
168 }
169 return media_getter_.get();
170 }
171
172 void URLRequestContextFactory::InitializeSystemContextDependencies() {
173 if (!system_dependencies_initialized_) {
174 host_resolver_ = net::HostResolver::CreateDefaultResolver(NULL);
175
176 server_bound_cert_service_.reset(new net::ServerBoundCertService(
177 new net::DefaultServerBoundCertStore(NULL),
178 base::WorkerPool::GetTaskRunner(true)));
179
180 cert_verifier_.reset(net::CertVerifier::CreateDefault());
181
182 ssl_config_service_ = new net::SSLConfigServiceDefaults;
183
184 transport_security_state_.reset(new net::TransportSecurityState());
185 http_auth_handler_factory_.reset(
186 net::HttpAuthHandlerFactory::CreateDefault(host_resolver_.get()));
187
188 http_server_properties_.reset(new net::HttpServerPropertiesImpl);
189
190 proxy_service_.reset(net::ProxyService::CreateUsingSystemProxyResolver(
191 net::ProxyService::CreateSystemProxyConfigService(
192 content::BrowserThread::GetMessageLoopProxyForThread(
193 content::BrowserThread::IO).get(),
194 content::BrowserThread::UnsafeGetMessageLoopForThread(
195 content::BrowserThread::FILE)),
196 0,
197 NULL));
198 system_dependencies_initialized_ = true;
199 }
200 }
201
202 void URLRequestContextFactory::InitializeMainContextDependencies(
203 net::HttpTransactionFactory* transaction_factory,
204 content::ProtocolHandlerMap* protocol_handlers) {
205 if (!main_dependencies_initialized_) {
206 main_transaction_factory_.reset(transaction_factory);
207 main_job_factory_.reset(new net::URLRequestJobFactoryImpl());
208 // Keep ProtocolHandlers added in sync with
209 // ShellContentBrowserClient::IsHandledURL().
210 bool set_protocol = false;
211 for (content::ProtocolHandlerMap::iterator it =
212 protocol_handlers->begin();
213 it != protocol_handlers->end();
214 ++it) {
215 set_protocol = main_job_factory_->SetProtocolHandler(
216 it->first, it->second.release());
217 DCHECK(set_protocol);
218 }
219 set_protocol = main_job_factory_->SetProtocolHandler(
220 url::kDataScheme,
221 new net::DataProtocolHandler);
222 DCHECK(set_protocol);
223 main_dependencies_initialized_ = true;
224 }
225 }
226
227 void URLRequestContextFactory::InitializeMediaContextDependencies(
228 net::HttpTransactionFactory* transaction_factory) {
229 if (!media_dependencies_initialized_) {
230 media_transaction_factory_.reset(transaction_factory);
231 media_dependencies_initialized_ = true;
232 }
233 }
234
235 void URLRequestContextFactory::PopulateNetworkSessionParams(
236 bool ignore_certificate_errors,
237 net::HttpNetworkSession::Params* params) {
238 params->host_resolver = host_resolver_.get();
239 params->cert_verifier = cert_verifier_.get();
240 params->server_bound_cert_service = server_bound_cert_service_.get();
241 params->ssl_config_service = ssl_config_service_.get();
242 params->transport_security_state = transport_security_state_.get();
243 params->http_auth_handler_factory = http_auth_handler_factory_.get();
244 params->http_server_properties = http_server_properties_->GetWeakPtr();
245 params->ignore_certificate_errors = ignore_certificate_errors;
246 params->proxy_service = proxy_service_.get();
247
248 // TODO(lcwu): Remove this once spdy is enabled by deafult at the content
249 // level. See crbug.com/329681.
250 params->next_protos = net::NextProtosSpdy31();
251 params->use_alternate_protocols = true;
252 }
253
254 net::URLRequestContext* URLRequestContextFactory::CreateSystemRequestContext() {
255 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
256 InitializeSystemContextDependencies();
257 net::HttpNetworkSession::Params system_params;
258 PopulateNetworkSessionParams(false, &system_params);
259 system_transaction_factory_.reset(new net::HttpNetworkLayer(
260 new net::HttpNetworkSession(system_params)));
261
262 net::URLRequestContext* system_context = new net::URLRequestContext();
263 system_context->set_host_resolver(host_resolver_.get());
264 system_context->set_server_bound_cert_service(
265 server_bound_cert_service_.get());
266 system_context->set_cert_verifier(cert_verifier_.get());
267 system_context->set_proxy_service(proxy_service_.get());
268 system_context->set_ssl_config_service(ssl_config_service_.get());
269 system_context->set_transport_security_state(
270 transport_security_state_.get());
271 system_context->set_http_auth_handler_factory(
272 http_auth_handler_factory_.get());
273 system_context->set_http_server_properties(
274 http_server_properties_->GetWeakPtr());
275 system_context->set_http_transaction_factory(
276 system_transaction_factory_.get());
277 system_context->set_http_user_agent_settings(
278 http_user_agent_settings_.get());
279 return system_context;
280 }
281
282 net::URLRequestContext* URLRequestContextFactory::CreateMediaRequestContext() {
283 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
284 DCHECK(main_getter_)
285 << "Getting MediaRequestContext before MainRequestContext";
286 net::URLRequestContext* main_context = main_getter_->GetURLRequestContext();
287
288 // Set non caching backend.
289 net::HttpNetworkSession* main_session =
290 main_transaction_factory_->GetSession();
291 InitializeMediaContextDependencies(
292 new net::HttpNetworkLayer(main_session));
293
294 net::URLRequestContext* media_context = new net::URLRequestContext();
295 media_context->CopyFrom(main_context);
296 media_context->set_http_transaction_factory(
297 media_transaction_factory_.get());
298 return media_context;
299 }
300
301 net::URLRequestContext* URLRequestContextFactory::CreateMainRequestContext(
302 content::BrowserContext* browser_context,
303 content::ProtocolHandlerMap* protocol_handlers) {
304 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
305 InitializeSystemContextDependencies();
306
307 // TODO(lcwu): Look into use block file backend
308 net::HttpCache::BackendFactory* main_backend =
309 net::HttpCache::DefaultBackend::InMemory(16 * 1024 * 1024);
310
311 bool ignore_certificate_errors = false;
312 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
313 if (cmd_line->HasSwitch(switches::kIgnoreCertificateErrors)) {
314 ignore_certificate_errors = true;
315 }
316 net::HttpNetworkSession::Params network_session_params;
317 PopulateNetworkSessionParams(ignore_certificate_errors,
318 &network_session_params);
319 InitializeMainContextDependencies(
320 new net::HttpCache(network_session_params, main_backend),
321 protocol_handlers);
322
323 content::CookieStoreConfig cookie_config(
324 browser_context->GetPath().Append(kCookieStoreFile),
325 content::CookieStoreConfig::PERSISTANT_SESSION_COOKIES,
326 NULL, NULL);
327 cookie_config.background_task_runner =
328 scoped_refptr<base::SequencedTaskRunner>();
329 scoped_refptr<net::CookieStore> cookie_store =
330 content::CreateCookieStore(cookie_config);
331
332 net::URLRequestContext* main_context = new net::URLRequestContext();
333 main_context->set_host_resolver(host_resolver_.get());
334 main_context->set_server_bound_cert_service(
335 server_bound_cert_service_.get());
336 main_context->set_cert_verifier(cert_verifier_.get());
337 main_context->set_proxy_service(proxy_service_.get());
338 main_context->set_ssl_config_service(ssl_config_service_.get());
339 main_context->set_transport_security_state(transport_security_state_.get());
340 main_context->set_http_auth_handler_factory(
341 http_auth_handler_factory_.get());
342 main_context->set_http_server_properties(
343 http_server_properties_->GetWeakPtr());
344 main_context->set_cookie_store(cookie_store.get());
345 main_context->set_http_user_agent_settings(
346 http_user_agent_settings_.get());
347
348 main_context->set_http_transaction_factory(
349 main_transaction_factory_.get());
350 main_context->set_job_factory(main_job_factory_.get());
351 return main_context;
352 }
353
354 } // namespace shell
355 } // namespace chromecast
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698