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 #ifndef SERVICES_NETWORK_UPLOAD_PROGRESS_TRACKER_H_ 6 #define SERVICES_NETWORK_UPLOAD_PROGRESS_TRACKER_H_ 7 8 #include <stdint.h> 9 10 #include "base/callback.h" 11 #include "base/component_export.h" 12 #include "base/macros.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/sequenced_task_runner.h" 15 #include "base/threading/sequenced_task_runner_handle.h" 16 #include "base/time/time.h" 17 #include "base/timer/timer.h" 18 #include "net/base/upload_progress.h" 19 20 namespace base { 21 class Location; 22 } 23 24 namespace net { 25 class URLRequest; 26 } 27 28 namespace network { 29 30 // UploadProgressTracker watches the upload progress of a URL loading, and sends 31 // the progress to the client in a suitable granularity and frequency. COMPONENT_EXPORT(NETWORK_SERVICE)32class COMPONENT_EXPORT(NETWORK_SERVICE) UploadProgressTracker { 33 public: 34 using UploadProgressReportCallback = 35 base::RepeatingCallback<void(const net::UploadProgress&)>; 36 37 UploadProgressTracker(const base::Location& location, 38 UploadProgressReportCallback report_progress, 39 net::URLRequest* request, 40 scoped_refptr<base::SequencedTaskRunner> task_runner = 41 base::SequencedTaskRunnerHandle::Get()); 42 virtual ~UploadProgressTracker(); 43 44 void OnAckReceived(); 45 void OnUploadCompleted(); 46 47 static base::TimeDelta GetUploadProgressIntervalForTesting(); 48 49 private: 50 // Overridden by tests to use a fake time and progress. 51 virtual base::TimeTicks GetCurrentTime() const; 52 virtual net::UploadProgress GetUploadProgress() const; 53 54 void ReportUploadProgressIfNeeded(); 55 56 net::URLRequest* request_; // Not owned. 57 58 uint64_t last_upload_position_ = 0; 59 bool waiting_for_upload_progress_ack_ = false; 60 base::TimeTicks last_upload_ticks_; 61 base::RepeatingTimer progress_timer_; 62 63 UploadProgressReportCallback report_progress_; 64 65 DISALLOW_COPY_AND_ASSIGN(UploadProgressTracker); 66 }; 67 68 } // namespace network 69 70 #endif // SERVICES_NETWORK_UPLOAD_PROGRESS_TRACKER_H_ 71