Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: content/browser/loader/upload_progress_tracker.cc

Issue 2546653003: Factor out upload progress handling from AsyncResourceHandler (Closed)
Patch Set: . Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "net/base/upload_progress.h"
8 #include "net/url_request/url_request.h"
9
10 namespace content {
11 namespace {
12 // The interval for calls to ReportUploadProgress.
13 constexpr base::TimeDelta kUploadProgressInterval =
14 base::TimeDelta::FromMilliseconds(100);
15 } // namespace
16
17 UploadProgressTracker::UploadProgressTracker(
18 const tracked_objects::Location& location,
19 UploadProgressReportCallback report_progress,
20 net::URLRequest* request)
21 : request_(request), report_progress_(std::move(report_progress)) {
22 DCHECK(request_);
23 DCHECK(report_progress_);
24 progress_timer_.Start(location, kUploadProgressInterval, this,
25 &UploadProgressTracker::ReportUploadProgressIfNeeded);
26 }
27
28 UploadProgressTracker::~UploadProgressTracker() {}
29
30 void UploadProgressTracker::OnAckReceived() {
31 waiting_for_upload_progress_ack_ = true;
yhirano 2016/12/02 09:17:52 false?
tzik 2016/12/05 08:00:06 Done.
32 }
33
34 void UploadProgressTracker::ForceReportUploadProgress() {
35 waiting_for_upload_progress_ack_ = false;
36 ReportUploadProgressIfNeeded();
37 }
38
39 void UploadProgressTracker::ReportUploadProgressIfNeeded() {
40 net::UploadProgress progress = request_->GetUploadProgress();
41 if (!progress.size())
42 return; // Nothing to upload.
43
44 if (progress.position() == last_upload_position_)
45 return; // No progress made since last time.
46
47 const uint64_t kHalfPercentIncrements = 200;
48 const base::TimeDelta kOneSecond = base::TimeDelta::FromMilliseconds(1000);
49
50 uint64_t amt_since_last = progress.position() - last_upload_position_;
51 base::TimeDelta time_since_last = base::TimeTicks::Now() - last_upload_ticks_;
52
53 bool is_finished = (progress.size() == progress.position());
54 bool enough_new_progress =
55 (amt_since_last > (progress.size() / kHalfPercentIncrements));
56 bool too_much_time_passed = time_since_last > kOneSecond;
57
58 if (is_finished || enough_new_progress || too_much_time_passed) {
59 report_progress_.Run(progress.position(), progress.size());
60 waiting_for_upload_progress_ack_ = true;
61 last_upload_ticks_ = base::TimeTicks::Now();
62 last_upload_position_ = progress.position();
63 }
64 }
65
66 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698