• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 
5 #ifndef COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_
6 #define COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_
7 
8 #include "base/memory/scoped_ptr.h"
9 #include "base/threading/thread_checker.h"
10 #include "base/time/time.h"
11 
12 class PrefRegistrySimple;
13 class PrefService;
14 
15 namespace base {
16 class TickClock;
17 }
18 
19 namespace network_time {
20 
21 // A class that receives network time updates and can provide the network time
22 // for a corresponding local time. This class is not thread safe.
23 class NetworkTimeTracker {
24  public:
25   static void RegisterPrefs(PrefRegistrySimple* registry);
26 
27   NetworkTimeTracker(scoped_ptr<base::TickClock> tick_clock,
28                      PrefService* pref_service);
29   ~NetworkTimeTracker();
30 
31   // Returns the network time corresponding to |time_ticks| if network time
32   // is available. Returns false if no network time is available yet. Can also
33   // return the error range if |uncertainty| isn't NULL.
34   bool GetNetworkTime(base::TimeTicks time_ticks,
35                       base::Time* network_time,
36                       base::TimeDelta* uncertainty) const;
37 
38   // Calculates corresponding time ticks according to the given parameters.
39   // The provided |network_time| is precise at the given |resolution| and
40   // represent the time between now and up to |latency| + (now - |post_time|)
41   // ago.
42   void UpdateNetworkTime(base::Time network_time,
43                          base::TimeDelta resolution,
44                          base::TimeDelta latency,
45                          base::TimeTicks post_time);
46 
received_network_time()47   bool received_network_time() const {
48     return received_network_time_;
49   }
50 
51  private:
52   // For querying current time ticks.
53   scoped_ptr<base::TickClock> tick_clock_;
54 
55   PrefService* pref_service_;
56 
57   // Network time based on last call to UpdateNetworkTime().
58   base::Time network_time_;
59 
60   // The estimated local time from |tick_clock| that corresponds with
61   // |network_time|. Assumes the actual network time measurement was performed
62   // midway through the latency time, and does not account for suspect/resume
63   // events since the network time was measured.
64   // See UpdateNetworkTime(..) implementation for details.
65   base::TimeTicks network_time_ticks_;
66 
67   // Uncertainty of |network_time_| based on added inaccuracies/resolution.
68   // See UpdateNetworkTime(..) implementation for details.
69   base::TimeDelta network_time_uncertainty_;
70 
71   base::ThreadChecker thread_checker_;
72 
73   bool received_network_time_;
74 
75   DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker);
76 };
77 
78 }  // namespace network_time
79 
80 #endif  // COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_
81