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 "content/browser/loader/upload_progress_tracker.h" | 5 #include "content/browser/loader/upload_progress_tracker.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/single_thread_task_runner.h" | |
| 8 #include "net/base/upload_progress.h" | 9 #include "net/base/upload_progress.h" |
| 9 #include "net/url_request/url_request.h" | |
| 10 | 10 |
| 11 namespace content { | 11 namespace content { |
| 12 namespace { | 12 namespace { |
| 13 // The interval for calls to ReportUploadProgress. | 13 // The interval for calls to ReportUploadProgress. |
| 14 constexpr base::TimeDelta kUploadProgressInterval = | 14 constexpr base::TimeDelta kUploadProgressInterval = |
| 15 base::TimeDelta::FromMilliseconds(100); | 15 base::TimeDelta::FromMilliseconds(100); |
| 16 } // namespace | 16 } // namespace |
| 17 | 17 |
| 18 UploadProgressTracker::Client::Client() = default; | |
| 19 UploadProgressTracker::Client::~Client() = default; | |
| 20 | |
| 18 UploadProgressTracker::UploadProgressTracker( | 21 UploadProgressTracker::UploadProgressTracker( |
| 19 const tracked_objects::Location& location, | 22 const tracked_objects::Location& location, |
| 20 UploadProgressReportCallback report_progress, | 23 Client* client, |
| 21 net::URLRequest* request) | 24 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 22 : request_(request), report_progress_(std::move(report_progress)) { | 25 : client_(client) { |
| 23 DCHECK(request_); | 26 DCHECK(client_); |
| 24 DCHECK(report_progress_); | |
| 25 | 27 |
| 28 progress_timer_.SetTaskRunner(std::move(task_runner)); | |
|
mmenke
2017/01/06 15:54:53
Why are we not just using the current thread? You
tzik
2017/01/10 06:54:41
That is for using TestSimpleTaskRunner::RunPending
| |
| 26 progress_timer_.Start(location, kUploadProgressInterval, this, | 29 progress_timer_.Start(location, kUploadProgressInterval, this, |
| 27 &UploadProgressTracker::ReportUploadProgressIfNeeded); | 30 &UploadProgressTracker::ReportUploadProgressIfNeeded); |
| 28 } | 31 } |
| 29 | 32 |
| 30 UploadProgressTracker::~UploadProgressTracker() {} | 33 UploadProgressTracker::~UploadProgressTracker() {} |
| 31 | 34 |
| 32 void UploadProgressTracker::OnAckReceived() { | 35 void UploadProgressTracker::OnAckReceived() { |
| 33 waiting_for_upload_progress_ack_ = false; | 36 waiting_for_upload_progress_ack_ = false; |
| 34 } | 37 } |
| 35 | 38 |
| 36 void UploadProgressTracker::OnUploadCompleted() { | 39 void UploadProgressTracker::OnUploadCompleted() { |
| 37 waiting_for_upload_progress_ack_ = false; | 40 waiting_for_upload_progress_ack_ = false; |
| 38 ReportUploadProgressIfNeeded(); | 41 ReportUploadProgressIfNeeded(); |
| 39 progress_timer_.Stop(); | 42 progress_timer_.Stop(); |
| 40 } | 43 } |
| 41 | 44 |
| 42 void UploadProgressTracker::ReportUploadProgressIfNeeded() { | 45 void UploadProgressTracker::ReportUploadProgressIfNeeded() { |
| 43 if (waiting_for_upload_progress_ack_) | 46 if (waiting_for_upload_progress_ack_) |
| 44 return; | 47 return; |
| 45 | 48 |
| 46 net::UploadProgress progress = request_->GetUploadProgress(); | 49 net::UploadProgress progress = client_->GetUploadProgress(); |
| 47 if (!progress.size()) | 50 if (!progress.size()) |
| 48 return; // Nothing to upload. | 51 return; // Nothing to upload. |
| 49 | 52 |
| 50 if (progress.position() == last_upload_position_) | 53 if (progress.position() == last_upload_position_) |
| 51 return; // No progress made since last time. | 54 return; // No progress made since last time. |
| 52 | 55 |
| 53 const uint64_t kHalfPercentIncrements = 200; | 56 const uint64_t kHalfPercentIncrements = 200; |
| 54 const base::TimeDelta kOneSecond = base::TimeDelta::FromMilliseconds(1000); | 57 const base::TimeDelta kOneSecond = base::TimeDelta::FromMilliseconds(1000); |
| 55 | 58 |
| 56 uint64_t amt_since_last = progress.position() - last_upload_position_; | 59 uint64_t amt_since_last = progress.position() - last_upload_position_; |
| 57 base::TimeDelta time_since_last = base::TimeTicks::Now() - last_upload_ticks_; | 60 base::TimeDelta time_since_last = base::TimeTicks::Now() - last_upload_ticks_; |
| 58 | 61 |
| 59 bool is_finished = (progress.size() == progress.position()); | 62 bool is_finished = (progress.size() == progress.position()); |
| 60 bool enough_new_progress = | 63 bool enough_new_progress = |
| 61 (amt_since_last > (progress.size() / kHalfPercentIncrements)); | 64 (amt_since_last > (progress.size() / kHalfPercentIncrements)); |
| 62 bool too_much_time_passed = time_since_last > kOneSecond; | 65 bool too_much_time_passed = time_since_last > kOneSecond; |
| 63 | 66 |
| 64 if (is_finished || enough_new_progress || too_much_time_passed) { | 67 if (is_finished || enough_new_progress || too_much_time_passed) { |
| 65 report_progress_.Run(progress.position(), progress.size()); | 68 client_->ReportUploadProgress(progress.position(), progress.size()); |
| 66 waiting_for_upload_progress_ack_ = true; | 69 waiting_for_upload_progress_ack_ = true; |
| 67 last_upload_ticks_ = base::TimeTicks::Now(); | 70 last_upload_ticks_ = base::TimeTicks::Now(); |
| 68 last_upload_position_ = progress.position(); | 71 last_upload_position_ = progress.position(); |
| 69 } | 72 } |
| 70 } | 73 } |
| 71 | 74 |
| 72 } // namespace content | 75 } // namespace content |
| OLD | NEW |