upload_progress_tracker.h 2.2 KB

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