File: job_details.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (110 lines) | stat: -rw-r--r-- 3,664 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/background_fetch/job_details.h"

#include "content/public/browser/background_fetch_description.h"

namespace background_fetch {

JobDetails::RequestData::RequestData(bool has_upload_data)
    : status(has_upload_data ? Status::kIncluded : Status::kAbsent) {}

JobDetails::RequestData::~RequestData() = default;

JobDetails::JobDetails() = default;
JobDetails::~JobDetails() = default;

void JobDetails::MarkJobAsStarted() {
  if (job_state == State::kPendingWillStartDownloading) {
    job_state = State::kStartedAndDownloading;
  } else if (job_state == State::kPendingWillStartPaused) {
    job_state = State::kStartedButPaused;
  }
}

void JobDetails::UpdateJobOnDownloadComplete(const std::string& download_guid) {
  fetch_description->completed_requests++;
  if (fetch_description->completed_requests ==
      fetch_description->total_requests) {
    job_state = State::kDownloadsComplete;
  }

  current_fetch_guids.erase(download_guid);
}

uint64_t JobDetails::GetTotalBytes() const {
  if (!ShouldReportProgressBySize()) {
    return -1;
  }

  // If we have completed all downloads, update progress max to the processed
  // bytes in case the provided totals were set too high. This avoids
  // unnecessary jumping in the progress bar.
  uint64_t completed_bytes =
      fetch_description->downloaded_bytes + fetch_description->uploaded_bytes;
  uint64_t total_bytes = fetch_description->download_total_bytes +
                         fetch_description->upload_total_bytes;
  return job_state == State::kDownloadsComplete ? completed_bytes : total_bytes;
}

uint64_t JobDetails::GetProcessedBytes() const {
  return fetch_description->downloaded_bytes +
         fetch_description->uploaded_bytes + GetInProgressBytes();
}

uint64_t JobDetails::GetDownloadedBytes() const {
  uint64_t bytes = fetch_description->downloaded_bytes;
  for (const auto& current_fetch : current_fetch_guids)
    bytes += current_fetch.second.in_progress_downloaded_bytes;
  return bytes;
}

uint64_t JobDetails::GetInProgressBytes() const {
  uint64_t bytes = 0u;
  for (const auto& current_fetch : current_fetch_guids) {
    bytes += current_fetch.second.in_progress_downloaded_bytes +
             current_fetch.second.in_progress_uploaded_bytes;
  }
  return bytes;
}

bool JobDetails::IsComplete() const {
  return job_state == State::kJobComplete;
}

void JobDetails::UpdateInProgressBytes(const std::string& download_guid,
                                       uint64_t bytes_uploaded,
                                       uint64_t bytes_downloaded) {
  DCHECK(current_fetch_guids.count(download_guid));
  auto& request_data = current_fetch_guids.find(download_guid)->second;

  // If we started receiving download bytes then the upload was complete and is
  // accounted for in |uploaded_bytes|.
  if (bytes_downloaded > 0u) {
    request_data.in_progress_uploaded_bytes = 0u;
  } else {
    request_data.in_progress_uploaded_bytes = bytes_uploaded;
  }

  request_data.in_progress_downloaded_bytes = bytes_downloaded;
}

bool JobDetails::ShouldReportProgressBySize() const {
  if (!fetch_description->download_total_bytes) {
    // |download_total_bytes| was not set. Cannot report by size.
    return false;
  }

  if (fetch_description->completed_requests <
          fetch_description->total_requests &&
      GetDownloadedBytes() > fetch_description->download_total_bytes) {
    // |download_total_bytes| was set too low.
    return false;
  }

  return true;
}

}  // namespace background_fetch