File: speedometer.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (56 lines) | stat: -rw-r--r-- 1,880 bytes parent folder | download | duplicates (8)
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
// 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.

#ifndef CHROMEOS_ASH_COMPONENTS_FILE_MANAGER_SPEEDOMETER_H_
#define CHROMEOS_ASH_COMPONENTS_FILE_MANAGER_SPEEDOMETER_H_

#include "base/component_export.h"
#include "base/containers/ring_buffer.h"
#include "base/time/time.h"

namespace file_manager {

// Calculates the remaining time for an operation based on the initial total
// bytes and the amount of bytes transferred on each `sample`.
//
// It estimates when the total bytes will be reached and exposes the "remaining
// time" from now until the projected end time.
class COMPONENT_EXPORT(FILE_MANAGER) Speedometer {
 public:
  // Sets the expected total number of bytes for the operation.
  void SetTotalBytes(int64_t total_bytes);

  // Gets the number of samples currently maintained.
  size_t GetSampleCount() const;

  // Gets the projected remaining time. It can be negative, or TimeDelta::Max()
  // if there aren't enough samples yet.
  base::TimeDelta GetRemainingTime() const;

  // Adds a sample with the current timestamp and the given number of bytes.
  // Does nothing if the previous sample was received less than 3 seconds ago.
  // Returns true if the sample was taken in account.
  // `total_processed_bytes`: Total bytes processed by the task so far.
  bool Update(int64_t bytes);

 private:
  struct Sample {
    // Time when the sample was created.
    base::TimeTicks time;

    // Total bytes processed up to this point in time.
    int64_t bytes;
  };

  // The expected total number of bytes, which will be reached when the task
  // finishes.
  int64_t total_bytes_ = 0;

  // Maintains the 20 most recent samples.
  base::RingBuffer<Sample, 20> samples_;
};

}  // namespace file_manager

#endif  // CHROMEOS_ASH_COMPONENTS_FILE_MANAGER_SPEEDOMETER_H_