OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/cronet/ios/cronet_environment.h" | 5 #include "components/cronet/ios/cronet_environment.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
10 #include "base/atomicops.h" | 10 #include "base/atomicops.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "base/path_service.h" | 22 #include "base/path_service.h" |
23 #include "base/single_thread_task_runner.h" | 23 #include "base/single_thread_task_runner.h" |
24 #include "base/synchronization/waitable_event.h" | 24 #include "base/synchronization/waitable_event.h" |
25 #include "base/threading/worker_pool.h" | 25 #include "base/threading/worker_pool.h" |
26 #include "components/cronet/histogram_manager.h" | 26 #include "components/cronet/histogram_manager.h" |
27 #include "components/cronet/ios/version.h" | 27 #include "components/cronet/ios/version.h" |
28 #include "components/prefs/json_pref_store.h" | 28 #include "components/prefs/json_pref_store.h" |
29 #include "components/prefs/pref_filter.h" | 29 #include "components/prefs/pref_filter.h" |
30 #include "ios/net/cookies/cookie_store_ios.h" | 30 #include "ios/net/cookies/cookie_store_ios.h" |
31 #include "ios/web/public/user_agent.h" | 31 #include "ios/web/public/user_agent.h" |
32 #include "net/base/net_errors.h" | |
33 #include "net/base/network_change_notifier.h" | 32 #include "net/base/network_change_notifier.h" |
34 #include "net/cert/cert_verifier.h" | 33 #include "net/cert/cert_verifier.h" |
35 #include "net/cert/ct_known_logs.h" | |
36 #include "net/cert/ct_log_verifier.h" | |
37 #include "net/cert/ct_policy_enforcer.h" | |
38 #include "net/cert/ct_verifier.h" | |
39 #include "net/cert/multi_log_ct_verifier.h" | |
40 #include "net/cookies/cookie_store.h" | |
41 #include "net/dns/host_resolver.h" | 34 #include "net/dns/host_resolver.h" |
42 #include "net/dns/mapped_host_resolver.h" | 35 #include "net/dns/mapped_host_resolver.h" |
43 #include "net/http/http_auth_handler_factory.h" | |
44 #include "net/http/http_cache.h" | |
45 #include "net/http/http_response_headers.h" | |
46 #include "net/http/http_server_properties_impl.h" | 36 #include "net/http/http_server_properties_impl.h" |
47 #include "net/http/http_stream_factory.h" | 37 #include "net/http/http_stream_factory.h" |
| 38 #include "net/http/http_transaction_factory.h" |
48 #include "net/http/http_util.h" | 39 #include "net/http/http_util.h" |
49 #include "net/log/net_log.h" | 40 #include "net/log/net_log.h" |
50 #include "net/log/net_log_capture_mode.h" | 41 #include "net/log/net_log_capture_mode.h" |
51 #include "net/log/write_to_file_net_log_observer.h" | 42 #include "net/log/write_to_file_net_log_observer.h" |
52 #include "net/proxy/proxy_service.h" | 43 #include "net/proxy/proxy_service.h" |
53 #include "net/socket/ssl_client_socket.h" | 44 #include "net/socket/ssl_client_socket.h" |
54 #include "net/ssl/channel_id_service.h" | 45 #include "net/url_request/http_user_agent_settings.h" |
55 #include "net/ssl/default_channel_id_store.h" | 46 #include "net/url_request/url_request_context.h" |
56 #include "net/ssl/ssl_config_service_defaults.h" | 47 #include "net/url_request/url_request_context_builder.h" |
57 #include "net/url_request/static_http_user_agent_settings.h" | |
58 #include "net/url_request/url_request_context_storage.h" | 48 #include "net/url_request/url_request_context_storage.h" |
59 #include "net/url_request/url_request_job_factory_impl.h" | 49 #include "net/url_request/url_request_job_factory_impl.h" |
60 #include "url/scheme_host_port.h" | 50 #include "url/scheme_host_port.h" |
61 #include "url/url_util.h" | 51 #include "url/url_util.h" |
62 | 52 |
63 namespace { | 53 namespace { |
64 | 54 |
65 base::AtExitManager* g_at_exit_ = nullptr; | 55 base::AtExitManager* g_at_exit_ = nullptr; |
66 net::NetworkChangeNotifier* g_network_change_notifier = nullptr; | 56 net::NetworkChangeNotifier* g_network_change_notifier = nullptr; |
67 // MessageLoop on the main thread. | 57 // MessageLoop on the main thread. |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 if (!ssl_key_log_file_set && !ssl_key_log_file_name_.empty()) { | 237 if (!ssl_key_log_file_set && !ssl_key_log_file_name_.empty()) { |
248 ssl_key_log_file_set = true; | 238 ssl_key_log_file_set = true; |
249 base::FilePath ssl_key_log_file; | 239 base::FilePath ssl_key_log_file; |
250 if (!PathService::Get(base::DIR_HOME, &ssl_key_log_file)) | 240 if (!PathService::Get(base::DIR_HOME, &ssl_key_log_file)) |
251 return; | 241 return; |
252 net::SSLClientSocket::SetSSLKeyLogFile( | 242 net::SSLClientSocket::SetSSLKeyLogFile( |
253 ssl_key_log_file.Append(ssl_key_log_file_name_), | 243 ssl_key_log_file.Append(ssl_key_log_file_name_), |
254 file_thread_->task_runner()); | 244 file_thread_->task_runner()); |
255 } | 245 } |
256 | 246 |
257 proxy_config_service_ = net::ProxyService::CreateSystemProxyConfigService( | |
258 network_io_thread_->task_runner(), nullptr); | |
259 main_context_getter_ = new CronetURLRequestContextGetter( | 247 main_context_getter_ = new CronetURLRequestContextGetter( |
260 this, network_io_thread_->task_runner()); | 248 this, network_io_thread_->task_runner()); |
261 base::subtle::MemoryBarrier(); | 249 base::subtle::MemoryBarrier(); |
262 PostToNetworkThread(FROM_HERE, | 250 PostToNetworkThread(FROM_HERE, |
263 base::Bind(&CronetEnvironment::InitializeOnNetworkThread, | 251 base::Bind(&CronetEnvironment::InitializeOnNetworkThread, |
264 base::Unretained(this))); | 252 base::Unretained(this))); |
265 } | 253 } |
266 | 254 |
267 CronetEnvironment::~CronetEnvironment() { | 255 CronetEnvironment::~CronetEnvironment() { |
268 // net::HTTPProtocolHandlerDelegate::SetInstance(nullptr); | 256 // net::HTTPProtocolHandlerDelegate::SetInstance(nullptr); |
269 } | 257 } |
270 | 258 |
271 void CronetEnvironment::InitializeOnNetworkThread() { | 259 void CronetEnvironment::InitializeOnNetworkThread() { |
272 DCHECK(network_io_thread_->task_runner()->BelongsToCurrentThread()); | 260 DCHECK(network_io_thread_->task_runner()->BelongsToCurrentThread()); |
273 base::FeatureList::InitializeInstance(std::string(), std::string()); | 261 base::FeatureList::InitializeInstance(std::string(), std::string()); |
274 // TODO(mef): Use net:UrlRequestContextBuilder instead of manual build. | |
275 main_context_.reset(new net::URLRequestContext); | |
276 main_context_->set_net_log(net_log_.get()); | |
277 | 262 |
278 if (user_agent_partial_) | 263 if (user_agent_partial_) |
279 user_agent_ = web::BuildUserAgentFromProduct(user_agent_); | 264 user_agent_ = web::BuildUserAgentFromProduct(user_agent_); |
280 | 265 |
281 main_context_->set_http_user_agent_settings( | |
282 new net::StaticHttpUserAgentSettings(accept_language_, user_agent_)); | |
283 | |
284 main_context_->set_ssl_config_service(new net::SSLConfigServiceDefaults); | |
285 main_context_->set_transport_security_state( | |
286 new net::TransportSecurityState()); | |
287 http_server_properties_.reset(new net::HttpServerPropertiesImpl()); | |
288 main_context_->set_http_server_properties(http_server_properties_.get()); | |
289 | |
290 // TODO(rdsmith): Note that the ".release()" calls below are leaking | |
291 // the objects in question; this should be fixed by having an object | |
292 // corresponding to URLRequestContextStorage that actually owns those | |
293 // objects. See http://crbug.com/523858. | |
294 std::unique_ptr<net::MappedHostResolver> mapped_host_resolver( | |
295 new net::MappedHostResolver( | |
296 net::HostResolver::CreateDefaultResolver(nullptr))); | |
297 | |
298 main_context_->set_host_resolver(mapped_host_resolver.release()); | |
299 | |
300 if (!cert_verifier_) | |
301 cert_verifier_ = net::CertVerifier::CreateDefault(); | |
302 main_context_->set_cert_verifier(cert_verifier_.get()); | |
303 | |
304 std::unique_ptr<net::MultiLogCTVerifier> ct_verifier = | |
305 base::MakeUnique<net::MultiLogCTVerifier>(); | |
306 ct_verifier->AddLogs(net::ct::CreateLogVerifiersForKnownLogs()); | |
307 main_context_->set_cert_transparency_verifier(ct_verifier.release()); | |
308 main_context_->set_ct_policy_enforcer(new net::CTPolicyEnforcer()); | |
309 | |
310 main_context_->set_http_auth_handler_factory( | |
311 net::HttpAuthHandlerRegistryFactory::CreateDefault( | |
312 main_context_->host_resolver()) | |
313 .release()); | |
314 main_context_->set_proxy_service( | |
315 net::ProxyService::CreateUsingSystemProxyResolver( | |
316 std::move(proxy_config_service_), 0, nullptr) | |
317 .release()); | |
318 | |
319 // Cache | 266 // Cache |
320 base::FilePath cache_path; | 267 base::FilePath cache_path; |
321 if (!PathService::Get(base::DIR_CACHE, &cache_path)) | 268 if (!PathService::Get(base::DIR_CACHE, &cache_path)) |
322 return; | 269 return; |
323 cache_path = cache_path.Append(FILE_PATH_LITERAL("cronet")); | 270 cache_path = cache_path.Append(FILE_PATH_LITERAL("cronet")); |
324 std::unique_ptr<net::HttpCache::DefaultBackend> main_backend( | |
325 new net::HttpCache::DefaultBackend(net::DISK_CACHE, | |
326 net::CACHE_BACKEND_SIMPLE, cache_path, | |
327 0, // Default cache size. | |
328 network_cache_thread_->task_runner())); | |
329 | 271 |
330 net::HttpNetworkSession::Params params; | 272 std::unique_ptr<URLRequestContextConfig> config(new URLRequestContextConfig( |
| 273 quic_enabled_, // Enable QUIC. |
| 274 "", // QUIC User Agent ID. |
| 275 http2_enabled_, // Enable SPDY. |
| 276 false, // Enable SDCH |
| 277 URLRequestContextConfig::DISK, // Type of http cache. |
| 278 0, // Max size of http cache in bytes. |
| 279 false, // Disable caching for HTTP responses. |
| 280 cache_path.value(), // Storage path for http cache and cookie storage. |
| 281 user_agent_, // User-Agent request header field. |
| 282 "{}", // JSON encoded experimental options. |
| 283 "", // Data reduction proxy key. |
| 284 "", // Data reduction proxy. |
| 285 "", // Fallback data reduction proxy. |
| 286 "", // Data reduction proxy secure proxy check URL. |
| 287 std::move(mock_cert_verifier_), // MockCertVerifier to use for testing |
| 288 // purposes. |
| 289 false, // Enable network quality estimator. |
| 290 true, // Enable bypassing of public key pinning for local trust anchors |
| 291 "")); // Certificate verifier cache data. |
331 | 292 |
332 params.host_resolver = main_context_->host_resolver(); | 293 net::URLRequestContextBuilder context_builder; |
333 params.cert_verifier = main_context_->cert_verifier(); | |
334 params.cert_transparency_verifier = | |
335 main_context_->cert_transparency_verifier(); | |
336 params.ct_policy_enforcer = main_context_->ct_policy_enforcer(); | |
337 params.channel_id_service = main_context_->channel_id_service(); | |
338 params.transport_security_state = main_context_->transport_security_state(); | |
339 params.proxy_service = main_context_->proxy_service(); | |
340 params.ssl_config_service = main_context_->ssl_config_service(); | |
341 params.http_auth_handler_factory = main_context_->http_auth_handler_factory(); | |
342 params.http_server_properties = main_context_->http_server_properties(); | |
343 params.net_log = main_context_->net_log(); | |
344 params.enable_http2 = http2_enabled(); | |
345 params.enable_quic = quic_enabled(); | |
346 | 294 |
| 295 context_builder.set_accept_language(accept_language_); |
| 296 |
| 297 config->ConfigureURLRequestContextBuilder(&context_builder, net_log_.get(), |
| 298 file_thread_.get()->task_runner()); |
| 299 |
| 300 std::unique_ptr<net::MappedHostResolver> mapped_host_resolver( |
| 301 new net::MappedHostResolver( |
| 302 net::HostResolver::CreateDefaultResolver(nullptr))); |
| 303 |
| 304 context_builder.set_host_resolver(std::move(mapped_host_resolver)); |
| 305 |
| 306 std::unordered_set<std::string> quic_host_whitelist; |
| 307 std::unique_ptr<net::HttpServerProperties> http_server_properties( |
| 308 new net::HttpServerPropertiesImpl()); |
347 for (const auto& quic_hint : quic_hints_) { | 309 for (const auto& quic_hint : quic_hints_) { |
348 net::AlternativeService alternative_service(net::kProtoQUIC, "", | 310 net::AlternativeService alternative_service(net::kProtoQUIC, "", |
349 quic_hint.port()); | 311 quic_hint.port()); |
350 url::SchemeHostPort quic_hint_server("https", quic_hint.host(), | 312 url::SchemeHostPort quic_hint_server("https", quic_hint.host(), |
351 quic_hint.port()); | 313 quic_hint.port()); |
352 main_context_->http_server_properties()->SetAlternativeService( | 314 http_server_properties->SetAlternativeService( |
353 quic_hint_server, alternative_service, base::Time::Max()); | 315 quic_hint_server, alternative_service, base::Time::Max()); |
354 params.quic_host_whitelist.insert(quic_hint.host()); | 316 quic_host_whitelist.insert(quic_hint.host()); |
355 } | 317 } |
356 | 318 |
357 if (!params.channel_id_service) { | 319 context_builder.SetHttpServerProperties(std::move(http_server_properties)); |
358 // The main context may not have a ChannelIDService, since it is lazily | 320 context_builder.set_quic_host_whitelist(quic_host_whitelist); |
359 // constructed. If not, build an ephemeral ChannelIDService with no backing | |
360 // disk store. | |
361 // TODO(ellyjones): support persisting ChannelID. | |
362 params.channel_id_service = | |
363 new net::ChannelIDService(new net::DefaultChannelIDStore(NULL), | |
364 base::WorkerPool::GetTaskRunner(true)); | |
365 } | |
366 | 321 |
367 // TODO(mmenke): These really shouldn't be leaked. | 322 main_context_ = context_builder.Build(); |
368 // See https://crbug.com/523858. | |
369 net::HttpNetworkSession* http_network_session = | |
370 new net::HttpNetworkSession(params); | |
371 net::HttpCache* main_cache = | |
372 new net::HttpCache(http_network_session, std::move(main_backend), | |
373 true /* set_up_quic_server_info */); | |
374 main_context_->set_http_transaction_factory(main_cache); | |
375 // Cookies | |
376 cookie_store_ = net::CookieStoreIOS::CreateCookieStore( | |
377 [NSHTTPCookieStorage sharedHTTPCookieStorage]); | |
378 main_context_->set_cookie_store(cookie_store_.get()); | |
379 | |
380 net::URLRequestJobFactoryImpl* job_factory = | |
381 new net::URLRequestJobFactoryImpl; | |
382 main_context_->set_job_factory(job_factory); | |
383 main_context_->set_net_log(net_log_.get()); | |
384 } | 323 } |
385 | 324 |
386 std::string CronetEnvironment::user_agent() { | 325 std::string CronetEnvironment::user_agent() { |
387 const net::HttpUserAgentSettings* user_agent_settings = | 326 const net::HttpUserAgentSettings* user_agent_settings = |
388 main_context_->http_user_agent_settings(); | 327 main_context_->http_user_agent_settings(); |
389 if (!user_agent_settings) { | 328 if (!user_agent_settings) { |
390 return nullptr; | 329 return nullptr; |
391 } | 330 } |
392 | 331 |
393 return user_agent_settings->GetUserAgent(); | 332 return user_agent_settings->GetUserAgent(); |
(...skipping 19 matching lines...) Expand all Loading... |
413 | 352 |
414 void CronetEnvironment::SetHostResolverRulesOnNetworkThread( | 353 void CronetEnvironment::SetHostResolverRulesOnNetworkThread( |
415 const std::string& rules, | 354 const std::string& rules, |
416 base::WaitableEvent* event) { | 355 base::WaitableEvent* event) { |
417 static_cast<net::MappedHostResolver*>(main_context_->host_resolver()) | 356 static_cast<net::MappedHostResolver*>(main_context_->host_resolver()) |
418 ->SetRulesFromString(rules); | 357 ->SetRulesFromString(rules); |
419 event->Signal(); | 358 event->Signal(); |
420 } | 359 } |
421 | 360 |
422 } // namespace cronet | 361 } // namespace cronet |
OLD | NEW |