Chromium Code Reviews| Index: content/browser/loader/upload_progress_tracker.cc |
| diff --git a/content/browser/loader/upload_progress_tracker.cc b/content/browser/loader/upload_progress_tracker.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8d3f41e68505dc8b3d39ef4e67b691286078b3e3 |
| --- /dev/null |
| +++ b/content/browser/loader/upload_progress_tracker.cc |
| @@ -0,0 +1,73 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/loader/upload_progress_tracker.h" |
| + |
| +#include "net/base/upload_progress.h" |
| +#include "net/url_request/url_request.h" |
| + |
| +namespace content { |
| +namespace { |
| +// The interval for calls to ReportUploadProgress. |
| +constexpr base::TimeDelta kUploadProgressInterval = |
| + base::TimeDelta::FromMilliseconds(100); |
| +} // namespace |
| + |
| +UploadProgressTracker::UploadProgressTracker( |
| + UploadProgressReportCallback report_progress, |
| + net::URLRequest* request) |
| + : request_(request), report_progress_(std::move(report_progress)) { |
| + DCHECK(request_); |
| + DCHECK(report_progress_); |
|
mmenke
2016/12/08 15:37:08
include base/logging.h
tzik
2016/12/12 07:28:50
Done.
|
| +} |
| + |
| +UploadProgressTracker::~UploadProgressTracker() {} |
| + |
| +void UploadProgressTracker::OnAckReceived() { |
| + waiting_for_upload_progress_ack_ = false; |
| +} |
| + |
| +void UploadProgressTracker::Start(const tracked_objects::Location& location) { |
| + progress_timer_.Start(location, kUploadProgressInterval, this, |
| + &UploadProgressTracker::ReportUploadProgressIfNeeded); |
|
mmenke
2016/12/08 15:37:08
Just do this in the constructor?
tzik
2016/12/12 07:28:50
Done.
|
| + UploadProgressTracker::ReportUploadProgressIfNeeded(); |
|
mmenke
2016/12/08 15:37:08
Why do we call this on start? We haven't uploaded
tzik
2016/12/12 07:28:50
It's just to preserve previous behavior. Seems lik
|
| +} |
| + |
| +void UploadProgressTracker::OnUploadCompleted() { |
| + waiting_for_upload_progress_ack_ = false; |
| + ReportUploadProgressIfNeeded(); |
| + progress_timer_.Stop(); |
| +} |
| + |
| +void UploadProgressTracker::ReportUploadProgressIfNeeded() { |
| + if (waiting_for_upload_progress_ack_) |
| + return; |
| + |
| + net::UploadProgress progress = request_->GetUploadProgress(); |
| + if (!progress.size()) |
| + return; // Nothing to upload. |
| + |
| + if (progress.position() == last_upload_position_) |
| + return; // No progress made since last time. |
| + |
| + const uint64_t kHalfPercentIncrements = 200; |
| + const base::TimeDelta kOneSecond = base::TimeDelta::FromMilliseconds(1000); |
| + |
| + uint64_t amt_since_last = progress.position() - last_upload_position_; |
| + base::TimeDelta time_since_last = base::TimeTicks::Now() - last_upload_ticks_; |
| + |
| + bool is_finished = (progress.size() == progress.position()); |
| + bool enough_new_progress = |
| + (amt_since_last > (progress.size() / kHalfPercentIncrements)); |
| + bool too_much_time_passed = time_since_last > kOneSecond; |
| + |
| + if (is_finished || enough_new_progress || too_much_time_passed) { |
| + report_progress_.Run(progress.position(), progress.size()); |
| + waiting_for_upload_progress_ack_ = true; |
| + last_upload_ticks_ = base::TimeTicks::Now(); |
| + last_upload_position_ = progress.position(); |
| + } |
| +} |
| + |
| +} // namespace content |