job_details.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include "components/background_fetch/job_details.h"
  5. #include "content/public/browser/background_fetch_description.h"
  6. namespace background_fetch {
  7. JobDetails::RequestData::RequestData(bool has_upload_data)
  8. : status(has_upload_data ? Status::kIncluded : Status::kAbsent) {}
  9. JobDetails::RequestData::~RequestData() = default;
  10. JobDetails::JobDetails() = default;
  11. JobDetails::~JobDetails() = default;
  12. void JobDetails::MarkJobAsStarted() {
  13. if (job_state == State::kPendingWillStartDownloading)
  14. job_state = State::kStartedAndDownloading;
  15. else if (job_state == State::kPendingWillStartPaused)
  16. job_state = State::kStartedButPaused;
  17. }
  18. void JobDetails::UpdateJobOnDownloadComplete(const std::string& download_guid) {
  19. fetch_description->completed_requests++;
  20. if (fetch_description->completed_requests ==
  21. fetch_description->total_requests) {
  22. job_state = State::kDownloadsComplete;
  23. }
  24. current_fetch_guids.erase(download_guid);
  25. }
  26. uint64_t JobDetails::GetTotalBytes() const {
  27. if (!ShouldReportProgressBySize())
  28. return -1;
  29. // If we have completed all downloads, update progress max to the processed
  30. // bytes in case the provided totals were set too high. This avoids
  31. // unnecessary jumping in the progress bar.
  32. uint64_t completed_bytes =
  33. fetch_description->downloaded_bytes + fetch_description->uploaded_bytes;
  34. uint64_t total_bytes = fetch_description->download_total_bytes +
  35. fetch_description->upload_total_bytes;
  36. return job_state == State::kDownloadsComplete ? completed_bytes : total_bytes;
  37. }
  38. uint64_t JobDetails::GetProcessedBytes() const {
  39. return fetch_description->downloaded_bytes +
  40. fetch_description->uploaded_bytes + GetInProgressBytes();
  41. }
  42. uint64_t JobDetails::GetDownloadedBytes() const {
  43. uint64_t bytes = fetch_description->downloaded_bytes;
  44. for (const auto& current_fetch : current_fetch_guids)
  45. bytes += current_fetch.second.in_progress_downloaded_bytes;
  46. return bytes;
  47. }
  48. uint64_t JobDetails::GetInProgressBytes() const {
  49. uint64_t bytes = 0u;
  50. for (const auto& current_fetch : current_fetch_guids) {
  51. bytes += current_fetch.second.in_progress_downloaded_bytes +
  52. current_fetch.second.in_progress_uploaded_bytes;
  53. }
  54. return bytes;
  55. }
  56. bool JobDetails::IsComplete() const {
  57. return job_state == State::kJobComplete;
  58. }
  59. void JobDetails::UpdateInProgressBytes(const std::string& download_guid,
  60. uint64_t bytes_uploaded,
  61. uint64_t bytes_downloaded) {
  62. DCHECK(current_fetch_guids.count(download_guid));
  63. auto& request_data = current_fetch_guids.find(download_guid)->second;
  64. // If we started receiving download bytes then the upload was complete and is
  65. // accounted for in |uploaded_bytes|.
  66. if (bytes_downloaded > 0u)
  67. request_data.in_progress_uploaded_bytes = 0u;
  68. else
  69. request_data.in_progress_uploaded_bytes = bytes_uploaded;
  70. request_data.in_progress_downloaded_bytes = bytes_downloaded;
  71. }
  72. bool JobDetails::ShouldReportProgressBySize() const {
  73. if (!fetch_description->download_total_bytes) {
  74. // |download_total_bytes| was not set. Cannot report by size.
  75. return false;
  76. }
  77. if (fetch_description->completed_requests <
  78. fetch_description->total_requests &&
  79. GetDownloadedBytes() > fetch_description->download_total_bytes) {
  80. // |download_total_bytes| was set too low.
  81. return false;
  82. }
  83. return true;
  84. }
  85. } // namespace background_fetch