| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "net/http/http_stream_factory_impl_job_controller.h" | 5 #include "net/http/http_stream_factory_impl_job_controller.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "net/base/host_mapping_rules.h" | 16 #include "net/base/host_mapping_rules.h" |
| 17 #include "net/base/proxy_delegate.h" | 17 #include "net/base/proxy_delegate.h" |
| 18 #include "net/http/bidirectional_stream_impl.h" | 18 #include "net/http/bidirectional_stream_impl.h" |
| 19 #include "net/http/transport_security_state.h" | 19 #include "net/http/transport_security_state.h" |
| 20 #include "net/log/net_log_capture_mode.h" | 20 #include "net/log/net_log_capture_mode.h" |
| 21 #include "net/log/net_log_event_type.h" | 21 #include "net/log/net_log_event_type.h" |
| 22 #include "net/log/net_log_source.h" | 22 #include "net/log/net_log_source.h" |
| 23 #include "net/log/net_log_with_source.h" | 23 #include "net/log/net_log_with_source.h" |
| 24 #include "net/proxy/proxy_server.h" | 24 #include "net/proxy/proxy_server.h" |
| 25 #include "net/spdy/spdy_session.h" | 25 #include "net/spdy/spdy_session.h" |
| 26 #include "url/url_constants.h" | 26 #include "url/url_constants.h" |
| 27 | 27 |
| 28 namespace net { | 28 namespace net { |
| 29 | 29 |
| 30 // Returns parameters associated with the delay of the HTTP stream job. | |
| 31 std::unique_ptr<base::Value> NetLogHttpStreamJobDelayCallback( | |
| 32 base::TimeDelta delay, | |
| 33 NetLogCaptureMode /* capture_mode */) { | |
| 34 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
| 35 dict->SetInteger("resume_after_ms", static_cast<int>(delay.InMilliseconds())); | |
| 36 return std::move(dict); | |
| 37 } | |
| 38 | |
| 39 HttpStreamFactoryImpl::JobController::JobController( | 30 HttpStreamFactoryImpl::JobController::JobController( |
| 40 HttpStreamFactoryImpl* factory, | 31 HttpStreamFactoryImpl* factory, |
| 41 HttpStreamRequest::Delegate* delegate, | 32 HttpStreamRequest::Delegate* delegate, |
| 42 HttpNetworkSession* session, | 33 HttpNetworkSession* session, |
| 43 JobFactory* job_factory) | 34 JobFactory* job_factory) |
| 44 : factory_(factory), | 35 : factory_(factory), |
| 45 session_(session), | 36 session_(session), |
| 46 job_factory_(job_factory), | 37 job_factory_(job_factory), |
| 47 request_(nullptr), | 38 request_(nullptr), |
| 48 delegate_(delegate), | 39 delegate_(delegate), |
| (...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 void HttpStreamFactoryImpl::JobController::AddConnectionAttemptsToRequest( | 499 void HttpStreamFactoryImpl::JobController::AddConnectionAttemptsToRequest( |
| 509 Job* job, | 500 Job* job, |
| 510 const ConnectionAttempts& attempts) { | 501 const ConnectionAttempts& attempts) { |
| 511 if (is_preconnect_ || (job_bound_ && bound_job_ != job)) | 502 if (is_preconnect_ || (job_bound_ && bound_job_ != job)) |
| 512 return; | 503 return; |
| 513 | 504 |
| 514 DCHECK(request_); | 505 DCHECK(request_); |
| 515 request_->AddConnectionAttempts(attempts); | 506 request_->AddConnectionAttempts(attempts); |
| 516 } | 507 } |
| 517 | 508 |
| 509 void HttpStreamFactoryImpl::JobController::ResumeMainJobLater( |
| 510 const base::TimeDelta& delay) { |
| 511 net_log_.AddEvent(NetLogEventType::HTTP_STREAM_JOB_DELAYED, |
| 512 NetLog::Int64Callback("delay", delay.InMilliseconds())); |
| 513 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 514 FROM_HERE, |
| 515 base::Bind(&HttpStreamFactoryImpl::JobController::ResumeMainJob, |
| 516 ptr_factory_.GetWeakPtr()), |
| 517 delay); |
| 518 } |
| 519 |
| 518 void HttpStreamFactoryImpl::JobController::ResumeMainJob() { | 520 void HttpStreamFactoryImpl::JobController::ResumeMainJob() { |
| 519 main_job_->net_log().AddEvent( | 521 main_job_->net_log().AddEvent( |
| 520 NetLogEventType::HTTP_STREAM_JOB_DELAYED, | 522 NetLogEventType::HTTP_STREAM_JOB_RESUMED, |
| 521 base::Bind(&NetLogHttpStreamJobDelayCallback, main_job_wait_time_)); | 523 NetLog::Int64Callback("delay", main_job_wait_time_.InMilliseconds())); |
| 522 | 524 |
| 523 main_job_->Resume(); | 525 main_job_->Resume(); |
| 524 main_job_wait_time_ = base::TimeDelta(); | 526 main_job_wait_time_ = base::TimeDelta(); |
| 525 } | 527 } |
| 526 | 528 |
| 527 void HttpStreamFactoryImpl::JobController::MaybeResumeMainJob( | 529 void HttpStreamFactoryImpl::JobController::MaybeResumeMainJob( |
| 528 Job* job, | 530 Job* job, |
| 529 const base::TimeDelta& delay) { | 531 const base::TimeDelta& delay) { |
| 530 DCHECK(job == main_job_.get() || job == alternative_job_.get()); | 532 DCHECK(job == main_job_.get() || job == alternative_job_.get()); |
| 531 | 533 |
| 532 if (!main_job_is_blocked_ || job != alternative_job_.get() || !main_job_) | 534 if (!main_job_is_blocked_ || job != alternative_job_.get() || !main_job_) |
| 533 return; | 535 return; |
| 534 | 536 |
| 535 main_job_is_blocked_ = false; | 537 main_job_is_blocked_ = false; |
| 536 | 538 |
| 537 if (!main_job_->is_waiting()) | 539 if (!main_job_->is_waiting()) |
| 538 return; | 540 return; |
| 539 | 541 |
| 540 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 542 ResumeMainJobLater(main_job_wait_time_); |
| 541 FROM_HERE, | |
| 542 base::Bind(&HttpStreamFactoryImpl::JobController::ResumeMainJob, | |
| 543 ptr_factory_.GetWeakPtr()), | |
| 544 main_job_wait_time_); | |
| 545 } | 543 } |
| 546 | 544 |
| 547 void HttpStreamFactoryImpl::JobController::OnConnectionInitialized(Job* job, | 545 void HttpStreamFactoryImpl::JobController::OnConnectionInitialized(Job* job, |
| 548 int rv) { | 546 int rv) { |
| 549 if (rv != OK) { | 547 if (rv != OK) { |
| 550 // Resume the main job as there's an error raised in connection | 548 // Resume the main job as there's an error raised in connection |
| 551 // initiation. | 549 // initiation. |
| 552 return MaybeResumeMainJob(job, main_job_wait_time_); | 550 return MaybeResumeMainJob(job, main_job_wait_time_); |
| 553 } | 551 } |
| 554 } | 552 } |
| 555 | 553 |
| 556 bool HttpStreamFactoryImpl::JobController::ShouldWait(Job* job) { | 554 bool HttpStreamFactoryImpl::JobController::ShouldWait(Job* job) { |
| 557 // The alternative job never waits. | 555 // The alternative job never waits. |
| 558 if (job == alternative_job_.get()) | 556 if (job == alternative_job_.get()) |
| 559 return false; | 557 return false; |
| 560 | 558 |
| 561 if (main_job_is_blocked_) | 559 if (main_job_is_blocked_) |
| 562 return true; | 560 return true; |
| 563 | 561 |
| 564 if (main_job_wait_time_.is_zero()) | 562 if (main_job_wait_time_.is_zero()) |
| 565 return false; | 563 return false; |
| 566 | 564 |
| 567 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 565 ResumeMainJobLater(main_job_wait_time_); |
| 568 FROM_HERE, | |
| 569 base::Bind(&HttpStreamFactoryImpl::JobController::ResumeMainJob, | |
| 570 ptr_factory_.GetWeakPtr()), | |
| 571 main_job_wait_time_); | |
| 572 | |
| 573 return true; | 566 return true; |
| 574 } | 567 } |
| 575 | 568 |
| 576 void HttpStreamFactoryImpl::JobController::SetSpdySessionKey( | 569 void HttpStreamFactoryImpl::JobController::SetSpdySessionKey( |
| 577 Job* job, | 570 Job* job, |
| 578 const SpdySessionKey& spdy_session_key) { | 571 const SpdySessionKey& spdy_session_key) { |
| 579 if (is_preconnect_ || (job_bound_ && bound_job_ != job)) | 572 if (is_preconnect_ || (job_bound_ && bound_job_ != job)) |
| 580 return; | 573 return; |
| 581 | 574 |
| 582 DCHECK(request_); | 575 DCHECK(request_); |
| (...skipping 514 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1097 return; | 1090 return; |
| 1098 DCHECK(alternative_job_->alternative_proxy_server().is_valid()); | 1091 DCHECK(alternative_job_->alternative_proxy_server().is_valid()); |
| 1099 alternative_job_->Start(request_->stream_type()); | 1092 alternative_job_->Start(request_->stream_type()); |
| 1100 } | 1093 } |
| 1101 | 1094 |
| 1102 bool HttpStreamFactoryImpl::JobController::IsJobOrphaned(Job* job) const { | 1095 bool HttpStreamFactoryImpl::JobController::IsJobOrphaned(Job* job) const { |
| 1103 return !request_ || (job_bound_ && bound_job_ != job); | 1096 return !request_ || (job_bound_ && bound_job_ != job); |
| 1104 } | 1097 } |
| 1105 | 1098 |
| 1106 } // namespace net | 1099 } // namespace net |
| OLD | NEW |