OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/quic/chromium/quic_stream_factory.h" | 5 #include "net/quic/chromium/quic_stream_factory.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <tuple> | 8 #include <tuple> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 base::WeakPtrFactory<CertVerifierJob> weak_factory_; | 319 base::WeakPtrFactory<CertVerifierJob> weak_factory_; |
320 | 320 |
321 DISALLOW_COPY_AND_ASSIGN(CertVerifierJob); | 321 DISALLOW_COPY_AND_ASSIGN(CertVerifierJob); |
322 }; | 322 }; |
323 | 323 |
324 // Responsible for creating a new QUIC session to the specified server, and | 324 // Responsible for creating a new QUIC session to the specified server, and |
325 // for notifying any associated requests when complete. | 325 // for notifying any associated requests when complete. |
326 class QuicStreamFactory::Job { | 326 class QuicStreamFactory::Job { |
327 public: | 327 public: |
328 Job(QuicStreamFactory* factory, | 328 Job(QuicStreamFactory* factory, |
| 329 const QuicVersion& quic_version, |
329 HostResolver* host_resolver, | 330 HostResolver* host_resolver, |
330 const QuicSessionKey& key, | 331 const QuicSessionKey& key, |
331 bool was_alternative_service_recently_broken, | 332 bool was_alternative_service_recently_broken, |
332 int cert_verify_flags, | 333 int cert_verify_flags, |
333 const NetLogWithSource& net_log); | 334 const NetLogWithSource& net_log); |
334 | 335 |
335 ~Job(); | 336 ~Job(); |
336 | 337 |
337 int Run(const CompletionCallback& callback); | 338 int Run(const CompletionCallback& callback); |
338 | 339 |
(...skipping 20 matching lines...) Expand all Loading... |
359 enum IoState { | 360 enum IoState { |
360 STATE_NONE, | 361 STATE_NONE, |
361 STATE_RESOLVE_HOST, | 362 STATE_RESOLVE_HOST, |
362 STATE_RESOLVE_HOST_COMPLETE, | 363 STATE_RESOLVE_HOST_COMPLETE, |
363 STATE_CONNECT, | 364 STATE_CONNECT, |
364 STATE_CONNECT_COMPLETE, | 365 STATE_CONNECT_COMPLETE, |
365 }; | 366 }; |
366 IoState io_state_; | 367 IoState io_state_; |
367 | 368 |
368 QuicStreamFactory* factory_; | 369 QuicStreamFactory* factory_; |
| 370 QuicVersion quic_version_; |
369 HostResolver* host_resolver_; | 371 HostResolver* host_resolver_; |
370 std::unique_ptr<HostResolver::Request> request_; | 372 std::unique_ptr<HostResolver::Request> request_; |
371 const QuicSessionKey key_; | 373 const QuicSessionKey key_; |
372 const int cert_verify_flags_; | 374 const int cert_verify_flags_; |
373 const bool was_alternative_service_recently_broken_; | 375 const bool was_alternative_service_recently_broken_; |
374 const NetLogWithSource net_log_; | 376 const NetLogWithSource net_log_; |
375 int num_sent_client_hellos_; | 377 int num_sent_client_hellos_; |
376 QuicChromiumClientSession* session_; | 378 QuicChromiumClientSession* session_; |
377 CompletionCallback callback_; | 379 CompletionCallback callback_; |
378 AddressList address_list_; | 380 AddressList address_list_; |
379 base::TimeTicks dns_resolution_start_time_; | 381 base::TimeTicks dns_resolution_start_time_; |
380 base::TimeTicks dns_resolution_end_time_; | 382 base::TimeTicks dns_resolution_end_time_; |
381 base::WeakPtrFactory<Job> weak_factory_; | 383 base::WeakPtrFactory<Job> weak_factory_; |
382 DISALLOW_COPY_AND_ASSIGN(Job); | 384 DISALLOW_COPY_AND_ASSIGN(Job); |
383 }; | 385 }; |
384 | 386 |
385 QuicStreamFactory::Job::Job(QuicStreamFactory* factory, | 387 QuicStreamFactory::Job::Job(QuicStreamFactory* factory, |
| 388 const QuicVersion& quic_version, |
386 HostResolver* host_resolver, | 389 HostResolver* host_resolver, |
387 const QuicSessionKey& key, | 390 const QuicSessionKey& key, |
388 bool was_alternative_service_recently_broken, | 391 bool was_alternative_service_recently_broken, |
389 int cert_verify_flags, | 392 int cert_verify_flags, |
390 const NetLogWithSource& net_log) | 393 const NetLogWithSource& net_log) |
391 : io_state_(STATE_RESOLVE_HOST), | 394 : io_state_(STATE_RESOLVE_HOST), |
392 factory_(factory), | 395 factory_(factory), |
| 396 quic_version_(quic_version), |
393 host_resolver_(host_resolver), | 397 host_resolver_(host_resolver), |
394 key_(key), | 398 key_(key), |
395 cert_verify_flags_(cert_verify_flags), | 399 cert_verify_flags_(cert_verify_flags), |
396 was_alternative_service_recently_broken_( | 400 was_alternative_service_recently_broken_( |
397 was_alternative_service_recently_broken), | 401 was_alternative_service_recently_broken), |
398 net_log_( | 402 net_log_( |
399 NetLogWithSource::Make(net_log.net_log(), | 403 NetLogWithSource::Make(net_log.net_log(), |
400 NetLogSourceType::QUIC_STREAM_FACTORY_JOB)), | 404 NetLogSourceType::QUIC_STREAM_FACTORY_JOB)), |
401 num_sent_client_hellos_(0), | 405 num_sent_client_hellos_(0), |
402 session_(nullptr), | 406 session_(nullptr), |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 | 505 |
502 int QuicStreamFactory::Job::DoConnect() { | 506 int QuicStreamFactory::Job::DoConnect() { |
503 io_state_ = STATE_CONNECT_COMPLETE; | 507 io_state_ = STATE_CONNECT_COMPLETE; |
504 | 508 |
505 bool require_confirmation = factory_->require_confirmation() || | 509 bool require_confirmation = factory_->require_confirmation() || |
506 was_alternative_service_recently_broken_; | 510 was_alternative_service_recently_broken_; |
507 net_log_.BeginEvent( | 511 net_log_.BeginEvent( |
508 NetLogEventType::QUIC_STREAM_FACTORY_JOB_CONNECT, | 512 NetLogEventType::QUIC_STREAM_FACTORY_JOB_CONNECT, |
509 NetLog::BoolCallback("require_confirmation", require_confirmation)); | 513 NetLog::BoolCallback("require_confirmation", require_confirmation)); |
510 | 514 |
511 int rv = | 515 DCHECK_NE(quic_version_, QUIC_VERSION_UNSUPPORTED); |
512 factory_->CreateSession(key_, cert_verify_flags_, require_confirmation, | 516 int rv = factory_->CreateSession( |
513 address_list_, dns_resolution_start_time_, | 517 key_, quic_version_, cert_verify_flags_, require_confirmation, |
514 dns_resolution_end_time_, net_log_, &session_); | 518 address_list_, dns_resolution_start_time_, dns_resolution_end_time_, |
| 519 net_log_, &session_); |
515 if (rv != OK) { | 520 if (rv != OK) { |
516 DCHECK(rv != ERR_IO_PENDING); | 521 DCHECK(rv != ERR_IO_PENDING); |
517 DCHECK(!session_); | 522 DCHECK(!session_); |
518 return rv; | 523 return rv; |
519 } | 524 } |
520 | 525 |
521 if (!session_->connection()->connected()) | 526 if (!session_->connection()->connected()) |
522 return ERR_CONNECTION_CLOSED; | 527 return ERR_CONNECTION_CLOSED; |
523 | 528 |
524 session_->StartReading(); | 529 session_->StartReading(); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
574 | 579 |
575 QuicStreamRequest::QuicStreamRequest(QuicStreamFactory* factory) | 580 QuicStreamRequest::QuicStreamRequest(QuicStreamFactory* factory) |
576 : factory_(factory) {} | 581 : factory_(factory) {} |
577 | 582 |
578 QuicStreamRequest::~QuicStreamRequest() { | 583 QuicStreamRequest::~QuicStreamRequest() { |
579 if (factory_ && !callback_.is_null()) | 584 if (factory_ && !callback_.is_null()) |
580 factory_->CancelRequest(this); | 585 factory_->CancelRequest(this); |
581 } | 586 } |
582 | 587 |
583 int QuicStreamRequest::Request(const HostPortPair& destination, | 588 int QuicStreamRequest::Request(const HostPortPair& destination, |
| 589 const QuicVersionVector& advertised_versions, |
584 PrivacyMode privacy_mode, | 590 PrivacyMode privacy_mode, |
585 int cert_verify_flags, | 591 int cert_verify_flags, |
586 const GURL& url, | 592 const GURL& url, |
587 QuicStringPiece method, | 593 QuicStringPiece method, |
588 const NetLogWithSource& net_log, | 594 const NetLogWithSource& net_log, |
589 const CompletionCallback& callback) { | 595 const CompletionCallback& callback) { |
590 DCHECK(callback_.is_null()); | 596 DCHECK(callback_.is_null()); |
591 DCHECK(factory_); | 597 DCHECK(factory_); |
592 server_id_ = QuicServerId(HostPortPair::FromURL(url), privacy_mode); | 598 server_id_ = QuicServerId(HostPortPair::FromURL(url), privacy_mode); |
593 | 599 |
594 int rv = factory_->Create(server_id_, destination, cert_verify_flags, url, | 600 int rv = factory_->Create(server_id_, destination, advertised_versions, |
595 method, net_log, this); | 601 cert_verify_flags, url, method, net_log, this); |
596 if (rv == ERR_IO_PENDING) { | 602 if (rv == ERR_IO_PENDING) { |
597 net_log_ = net_log; | 603 net_log_ = net_log; |
598 callback_ = callback; | 604 callback_ = callback; |
599 } else { | 605 } else { |
600 factory_ = nullptr; | 606 factory_ = nullptr; |
601 } | 607 } |
602 if (rv == OK) | 608 if (rv == OK) |
603 DCHECK(session_); | 609 DCHECK(session_); |
604 return rv; | 610 return rv; |
605 } | 611 } |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
842 session->CanPool(server_id.host(), server_id.privacy_mode())) { | 848 session->CanPool(server_id.host(), server_id.privacy_mode())) { |
843 return true; | 849 return true; |
844 } | 850 } |
845 } | 851 } |
846 | 852 |
847 return false; | 853 return false; |
848 } | 854 } |
849 | 855 |
850 int QuicStreamFactory::Create(const QuicServerId& server_id, | 856 int QuicStreamFactory::Create(const QuicServerId& server_id, |
851 const HostPortPair& destination, | 857 const HostPortPair& destination, |
| 858 const QuicVersionVector& advertised_versions, |
852 int cert_verify_flags, | 859 int cert_verify_flags, |
853 const GURL& url, | 860 const GURL& url, |
854 QuicStringPiece method, | 861 QuicStringPiece method, |
855 const NetLogWithSource& net_log, | 862 const NetLogWithSource& net_log, |
856 QuicStreamRequest* request) { | 863 QuicStreamRequest* request) { |
857 if (clock_skew_detector_.ClockSkewDetected(base::TimeTicks::Now(), | 864 if (clock_skew_detector_.ClockSkewDetected(base::TimeTicks::Now(), |
858 base::Time::Now())) { | 865 base::Time::Now())) { |
859 while (!active_sessions_.empty()) { | 866 while (!active_sessions_.empty()) { |
860 QuicChromiumClientSession* session = active_sessions_.begin()->second; | 867 QuicChromiumClientSession* session = active_sessions_.begin()->second; |
861 OnSessionGoingAway(session); | 868 OnSessionGoingAway(session); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
919 } | 926 } |
920 | 927 |
921 // TODO(rtenneti): |task_runner_| is used by the Job. Initialize task_runner_ | 928 // TODO(rtenneti): |task_runner_| is used by the Job. Initialize task_runner_ |
922 // in the constructor after WebRequestActionWithThreadsTest.* tests are fixed. | 929 // in the constructor after WebRequestActionWithThreadsTest.* tests are fixed. |
923 if (!task_runner_) | 930 if (!task_runner_) |
924 task_runner_ = base::ThreadTaskRunnerHandle::Get().get(); | 931 task_runner_ = base::ThreadTaskRunnerHandle::Get().get(); |
925 | 932 |
926 ignore_result(StartCertVerifyJob(server_id, cert_verify_flags, net_log)); | 933 ignore_result(StartCertVerifyJob(server_id, cert_verify_flags, net_log)); |
927 | 934 |
928 QuicSessionKey key(destination, server_id); | 935 QuicSessionKey key(destination, server_id); |
| 936 QuicVersion quic_version = SelectQuicVersion(advertised_versions); |
| 937 DCHECK_NE(quic_version, QUIC_VERSION_UNSUPPORTED); |
929 std::unique_ptr<Job> job = base::MakeUnique<Job>( | 938 std::unique_ptr<Job> job = base::MakeUnique<Job>( |
930 this, host_resolver_, key, WasQuicRecentlyBroken(server_id), | 939 this, quic_version, host_resolver_, key, WasQuicRecentlyBroken(server_id), |
931 cert_verify_flags, net_log); | 940 cert_verify_flags, net_log); |
932 int rv = job->Run(base::Bind(&QuicStreamFactory::OnJobComplete, | 941 int rv = job->Run(base::Bind(&QuicStreamFactory::OnJobComplete, |
933 base::Unretained(this), job.get())); | 942 base::Unretained(this), job.get())); |
934 if (rv == ERR_IO_PENDING) { | 943 if (rv == ERR_IO_PENDING) { |
935 job_requests_map_[server_id].insert(request); | 944 job_requests_map_[server_id].insert(request); |
936 active_jobs_[server_id] = std::move(job); | 945 active_jobs_[server_id] = std::move(job); |
937 return rv; | 946 return rv; |
938 } | 947 } |
939 if (rv == OK) { | 948 if (rv == OK) { |
940 // TODO(rtenneti): crbug.com/498823 - revert active_sessions_.empty() | 949 // TODO(rtenneti): crbug.com/498823 - revert active_sessions_.empty() |
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1438 IPAddress last_address; | 1447 IPAddress last_address; |
1439 if (http_server_properties_->GetSupportsQuic(&last_address) && | 1448 if (http_server_properties_->GetSupportsQuic(&last_address) && |
1440 last_address == local_address_.address()) { | 1449 last_address == local_address_.address()) { |
1441 require_confirmation_ = false; | 1450 require_confirmation_ = false; |
1442 } | 1451 } |
1443 } | 1452 } |
1444 | 1453 |
1445 return OK; | 1454 return OK; |
1446 } | 1455 } |
1447 | 1456 |
1448 int QuicStreamFactory::CreateSession( | 1457 int QuicStreamFactory::CreateSession(const QuicSessionKey& key, |
1449 const QuicSessionKey& key, | 1458 const QuicVersion& quic_version, |
1450 int cert_verify_flags, | 1459 int cert_verify_flags, |
1451 bool require_confirmation, | 1460 bool require_confirmation, |
1452 const AddressList& address_list, | 1461 const AddressList& address_list, |
1453 base::TimeTicks dns_resolution_start_time, | 1462 base::TimeTicks dns_resolution_start_time, |
1454 base::TimeTicks dns_resolution_end_time, | 1463 base::TimeTicks dns_resolution_end_time, |
1455 const NetLogWithSource& net_log, | 1464 const NetLogWithSource& net_log, |
1456 QuicChromiumClientSession** session) { | 1465 QuicChromiumClientSession** session) { |
1457 TRACE_EVENT0(kNetTracingCategory, "QuicStreamFactory::CreateSession"); | 1466 TRACE_EVENT0(kNetTracingCategory, "QuicStreamFactory::CreateSession"); |
1458 IPEndPoint addr = *address_list.begin(); | 1467 IPEndPoint addr = *address_list.begin(); |
1459 const QuicServerId& server_id = key.server_id(); | 1468 const QuicServerId& server_id = key.server_id(); |
1460 DatagramSocket::BindType bind_type = DatagramSocket::DEFAULT_BIND; | 1469 DatagramSocket::BindType bind_type = DatagramSocket::DEFAULT_BIND; |
1461 std::unique_ptr<DatagramClientSocket> socket( | 1470 std::unique_ptr<DatagramClientSocket> socket( |
1462 client_socket_factory_->CreateDatagramClientSocket( | 1471 client_socket_factory_->CreateDatagramClientSocket( |
1463 bind_type, RandIntCallback(), net_log.net_log(), net_log.source())); | 1472 bind_type, RandIntCallback(), net_log.net_log(), net_log.source())); |
1464 | 1473 |
1465 // Passing in kInvalidNetworkHandle binds socket to default network. | 1474 // Passing in kInvalidNetworkHandle binds socket to default network. |
1466 int rv = ConfigureSocket(socket.get(), addr, | 1475 int rv = ConfigureSocket(socket.get(), addr, |
(...skipping 12 matching lines...) Expand all Loading... |
1479 | 1488 |
1480 QuicConnectionId connection_id = random_generator_->RandUint64(); | 1489 QuicConnectionId connection_id = random_generator_->RandUint64(); |
1481 std::unique_ptr<QuicServerInfo> server_info; | 1490 std::unique_ptr<QuicServerInfo> server_info; |
1482 if (store_server_configs_in_properties_) { | 1491 if (store_server_configs_in_properties_) { |
1483 server_info = base::MakeUnique<PropertiesBasedQuicServerInfo>( | 1492 server_info = base::MakeUnique<PropertiesBasedQuicServerInfo>( |
1484 server_id, http_server_properties_); | 1493 server_id, http_server_properties_); |
1485 } | 1494 } |
1486 InitializeCachedStateInCryptoConfig(server_id, server_info, &connection_id); | 1495 InitializeCachedStateInCryptoConfig(server_id, server_info, &connection_id); |
1487 | 1496 |
1488 QuicChromiumPacketWriter* writer = new QuicChromiumPacketWriter(socket.get()); | 1497 QuicChromiumPacketWriter* writer = new QuicChromiumPacketWriter(socket.get()); |
| 1498 // TODO(zhongyi): pass over |quic_version| to QuicConnection; |
1489 QuicConnection* connection = new QuicConnection( | 1499 QuicConnection* connection = new QuicConnection( |
1490 connection_id, QuicSocketAddress(QuicSocketAddressImpl(addr)), | 1500 connection_id, QuicSocketAddress(QuicSocketAddressImpl(addr)), |
1491 helper_.get(), alarm_factory_.get(), writer, true /* owns_writer */, | 1501 helper_.get(), alarm_factory_.get(), writer, true /* owns_writer */, |
1492 Perspective::IS_CLIENT, supported_versions_); | 1502 Perspective::IS_CLIENT, supported_versions_); |
1493 connection->set_ping_timeout(ping_timeout_); | 1503 connection->set_ping_timeout(ping_timeout_); |
1494 connection->SetMaxPacketLength(max_packet_length_); | 1504 connection->SetMaxPacketLength(max_packet_length_); |
1495 | 1505 |
1496 QuicConfig config = config_; | 1506 QuicConfig config = config_; |
1497 config.set_max_undecryptable_packets(kMaxUndecryptablePackets); | 1507 config.set_max_undecryptable_packets(kMaxUndecryptablePackets); |
1498 config.SetInitialSessionFlowControlWindowToSend( | 1508 config.SetInitialSessionFlowControlWindowToSend( |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1572 | 1582 |
1573 if (type == NetworkChangeNotifier::CONNECTION_3G) { | 1583 if (type == NetworkChangeNotifier::CONNECTION_3G) { |
1574 SetInitialRttEstimate(base::TimeDelta::FromMilliseconds(400), | 1584 SetInitialRttEstimate(base::TimeDelta::FromMilliseconds(400), |
1575 INITIAL_RTT_CACHED, config); | 1585 INITIAL_RTT_CACHED, config); |
1576 return; | 1586 return; |
1577 } | 1587 } |
1578 | 1588 |
1579 SetInitialRttEstimate(base::TimeDelta(), INITIAL_RTT_DEFAULT, config); | 1589 SetInitialRttEstimate(base::TimeDelta(), INITIAL_RTT_DEFAULT, config); |
1580 } | 1590 } |
1581 | 1591 |
| 1592 QuicVersion QuicStreamFactory::SelectQuicVersion( |
| 1593 const QuicVersionVector& advertised_versions) const { |
| 1594 for (const auto& supported : supported_versions_) { |
| 1595 for (const auto& advertised : advertised_versions) { |
| 1596 if (supported == advertised) |
| 1597 return supported; |
| 1598 } |
| 1599 } |
| 1600 |
| 1601 return supported_versions_[0]; |
| 1602 } |
| 1603 |
1582 const base::TimeDelta* QuicStreamFactory::GetServerNetworkStatsSmoothedRtt( | 1604 const base::TimeDelta* QuicStreamFactory::GetServerNetworkStatsSmoothedRtt( |
1583 const QuicServerId& server_id) const { | 1605 const QuicServerId& server_id) const { |
1584 url::SchemeHostPort server("https", server_id.host_port_pair().host(), | 1606 url::SchemeHostPort server("https", server_id.host_port_pair().host(), |
1585 server_id.host_port_pair().port()); | 1607 server_id.host_port_pair().port()); |
1586 const ServerNetworkStats* stats = | 1608 const ServerNetworkStats* stats = |
1587 http_server_properties_->GetServerNetworkStats(server); | 1609 http_server_properties_->GetServerNetworkStats(server); |
1588 if (stats == nullptr) | 1610 if (stats == nullptr) |
1589 return nullptr; | 1611 return nullptr; |
1590 return &(stats->srtt); | 1612 return &(stats->srtt); |
1591 } | 1613 } |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1700 // Since the session was active, there's no longer an | 1722 // Since the session was active, there's no longer an |
1701 // HttpStreamFactoryImpl::Job running which can mark it broken, unless the TCP | 1723 // HttpStreamFactoryImpl::Job running which can mark it broken, unless the TCP |
1702 // job also fails. So to avoid not using QUIC when we otherwise could, we mark | 1724 // job also fails. So to avoid not using QUIC when we otherwise could, we mark |
1703 // it as recently broken, which means that 0-RTT will be disabled but we'll | 1725 // it as recently broken, which means that 0-RTT will be disabled but we'll |
1704 // still race. | 1726 // still race. |
1705 http_server_properties_->MarkAlternativeServiceRecentlyBroken( | 1727 http_server_properties_->MarkAlternativeServiceRecentlyBroken( |
1706 alternative_service); | 1728 alternative_service); |
1707 } | 1729 } |
1708 | 1730 |
1709 } // namespace net | 1731 } // namespace net |
OLD | NEW |