job_details.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2021 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 COMPONENTS_BACKGROUND_FETCH_JOB_DETAILS_H_
  5. #define COMPONENTS_BACKGROUND_FETCH_JOB_DETAILS_H_
  6. #include "base/callback.h"
  7. #include "base/memory/weak_ptr.h"
  8. #include "content/public/browser/background_fetch_delegate.h"
  9. namespace content {
  10. struct BackgroundFetchDescription;
  11. }
  12. namespace background_fetch {
  13. struct JobDetails {
  14. enum class State {
  15. kPendingWillStartPaused,
  16. kPendingWillStartDownloading,
  17. kStartedButPaused,
  18. kStartedAndDownloading,
  19. // The job was aborted.
  20. kCancelled,
  21. // All requests were processed (either succeeded or failed).
  22. kDownloadsComplete,
  23. // The appropriate completion event (success, fail, abort) has been
  24. // dispatched.
  25. kJobComplete,
  26. };
  27. JobDetails();
  28. JobDetails(const JobDetails& other) = delete;
  29. JobDetails& operator=(const JobDetails& other) = delete;
  30. ~JobDetails();
  31. void MarkJobAsStarted();
  32. void UpdateJobOnDownloadComplete(const std::string& download_guid);
  33. // Gets the total number of bytes that need to be processed, or -1 if unknown.
  34. uint64_t GetTotalBytes() const;
  35. // Returns how many bytes have been processed by the Download Service so
  36. // far.
  37. uint64_t GetProcessedBytes() const;
  38. // Returns the number of downloaded bytes, including for the in progress
  39. // requests.
  40. uint64_t GetDownloadedBytes() const;
  41. // Whether the job has finished successfully (not aborted).
  42. bool IsComplete() const;
  43. void UpdateInProgressBytes(const std::string& download_guid,
  44. uint64_t bytes_uploaded,
  45. uint64_t bytes_downloaded);
  46. // Whether we should report progress of the job in terms of size of
  47. // downloads or in terms of the number of files being downloaded.
  48. bool ShouldReportProgressBySize() const;
  49. // Returns the number of bytes processed by in-progress requests.
  50. uint64_t GetInProgressBytes() const;
  51. struct RequestData {
  52. enum class Status {
  53. kAbsent,
  54. kIncluded,
  55. };
  56. explicit RequestData(bool has_upload_data);
  57. ~RequestData();
  58. Status status;
  59. uint64_t body_size_bytes = 0u;
  60. uint64_t in_progress_uploaded_bytes = 0u;
  61. uint64_t in_progress_downloaded_bytes = 0u;
  62. };
  63. // The client to report the Background Fetch updates to.
  64. base::WeakPtr<content::BackgroundFetchDelegate::Client> client;
  65. // Set of DownloadService GUIDs that are currently processed. They are
  66. // added by DownloadUrl and are removed when the fetch completes, fails,
  67. // or is cancelled.
  68. std::map<std::string, RequestData> current_fetch_guids;
  69. State job_state;
  70. std::unique_ptr<content::BackgroundFetchDescription> fetch_description;
  71. bool cancelled_from_ui = false;
  72. base::OnceClosure on_resume;
  73. };
  74. } // namespace background_fetch
  75. #endif // COMPONENTS_BACKGROUND_FETCH_JOB_DETAILS_H_