Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/loader/upload_progress_tracker.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "net/base/upload_progress.h" | |
| 9 #include "net/url_request/url_request.h" | |
| 10 | |
| 11 namespace content { | |
| 12 namespace { | |
| 13 // The interval for calls to ReportUploadProgress. | |
| 14 constexpr base::TimeDelta kUploadProgressInterval = | |
| 15 base::TimeDelta::FromMilliseconds(100); | |
| 16 } // namespace | |
| 17 | |
| 18 UploadProgressTracker::UploadProgressTracker( | |
| 19 const tracked_objects::Location& location, | |
|
mmenke
2016/12/13 18:29:46
Do we really get anything from taking this as an a
tzik
2016/12/14 06:50:30
Taking it from a param will leave slightly better
| |
| 20 UploadProgressReportCallback report_progress, | |
| 21 net::URLRequest* request) | |
| 22 : request_(request), report_progress_(std::move(report_progress)) { | |
| 23 DCHECK(request_); | |
| 24 DCHECK(report_progress_); | |
| 25 | |
| 26 progress_timer_.Start(location, kUploadProgressInterval, this, | |
| 27 &UploadProgressTracker::ReportUploadProgressIfNeeded); | |
| 28 } | |
| 29 | |
| 30 UploadProgressTracker::~UploadProgressTracker() {} | |
| 31 | |
| 32 void UploadProgressTracker::OnAckReceived() { | |
| 33 waiting_for_upload_progress_ack_ = false; | |
| 34 } | |
| 35 | |
| 36 void UploadProgressTracker::OnUploadCompleted() { | |
| 37 waiting_for_upload_progress_ack_ = false; | |
| 38 ReportUploadProgressIfNeeded(); | |
| 39 progress_timer_.Stop(); | |
| 40 } | |
| 41 | |
| 42 void UploadProgressTracker::ReportUploadProgressIfNeeded() { | |
| 43 if (waiting_for_upload_progress_ack_) | |
| 44 return; | |
| 45 | |
| 46 net::UploadProgress progress = request_->GetUploadProgress(); | |
| 47 if (!progress.size()) | |
| 48 return; // Nothing to upload. | |
| 49 | |
| 50 if (progress.position() == last_upload_position_) | |
| 51 return; // No progress made since last time. | |
| 52 | |
| 53 const uint64_t kHalfPercentIncrements = 200; | |
| 54 const base::TimeDelta kOneSecond = base::TimeDelta::FromMilliseconds(1000); | |
| 55 | |
| 56 uint64_t amt_since_last = progress.position() - last_upload_position_; | |
| 57 base::TimeDelta time_since_last = base::TimeTicks::Now() - last_upload_ticks_; | |
| 58 | |
| 59 bool is_finished = (progress.size() == progress.position()); | |
| 60 bool enough_new_progress = | |
| 61 (amt_since_last > (progress.size() / kHalfPercentIncrements)); | |
| 62 bool too_much_time_passed = time_since_last > kOneSecond; | |
| 63 | |
| 64 if (is_finished || enough_new_progress || too_much_time_passed) { | |
| 65 report_progress_.Run(progress.position(), progress.size()); | |
| 66 waiting_for_upload_progress_ack_ = true; | |
| 67 last_upload_ticks_ = base::TimeTicks::Now(); | |
| 68 last_upload_position_ = progress.position(); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 } // namespace content | |
| OLD | NEW |