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