• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_NETWORK_TIME_TIME_TRACKER_TIME_TRACKER_H_
6 #define COMPONENTS_NETWORK_TIME_TIME_TRACKER_TIME_TRACKER_H_
7 
8 #include "base/time/time.h"
9 
10 namespace network_time {
11 
12 // A class that's created with a known good time, and provides an estimate of
13 // the current time by adding the system clock seconds that have elapsed since
14 // it was created.
15 class TimeTracker {
16  public:
17   struct TimeTrackerState {
18     base::Time system_time;
19     base::TimeTicks system_ticks;
20     base::Time known_time;
21     base::TimeDelta uncertainty;
22   };
23   TimeTracker(const base::Time& system_time,
24               const base::TimeTicks& system_ticks,
25               const base::Time& time,
26               const base::TimeDelta& uncertainty);
27   ~TimeTracker() = default;
28 
29   // Returns true if the time is available, false otherwise (e.g. if sync was
30   // lost). Sets |estimated_ time| to an estimate of the true time. If
31   // |uncertainty| is non-NULL, it will be set to an estimate of the error
32   // range. |system_time| and |system_ticks| should come from the same clocks
33   // used to retrieve the system time on creation.
34   bool GetTime(const base::Time& system_time,
35                const base::TimeTicks& system_ticks,
36                base::Time* time,
37                base::TimeDelta* uncertainty) const;
38 
GetStateAtCreation()39   TimeTrackerState GetStateAtCreation() const { return state_; }
40 
41  private:
42   TimeTrackerState state_;
43 };
44 
45 }  // namespace network_time
46 
47 #endif  // COMPONENTS_NETWORK_TIME_TIME_TRACKER_TIME_TRACKER_H_
48