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