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

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

Powered by Google App Engine
This is Rietveld 408576698