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.
5 #ifndef COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_
6 #define COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/threading/thread_checker.h"
10 #include "base/time/time.h"
12 class PrefRegistrySimple
;
19 namespace network_time
{
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
{
25 static void RegisterPrefs(PrefRegistrySimple
* registry
);
27 NetworkTimeTracker(scoped_ptr
<base::TickClock
> tick_clock
,
28 PrefService
* pref_service
);
29 ~NetworkTimeTracker();
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;
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|)
42 void UpdateNetworkTime(base::Time network_time
,
43 base::TimeDelta resolution
,
44 base::TimeDelta latency
,
45 base::TimeTicks post_time
);
47 bool received_network_time() const {
48 return received_network_time_
;
52 // For querying current time ticks.
53 scoped_ptr
<base::TickClock
> tick_clock_
;
55 PrefService
* pref_service_
;
57 // Network time based on last call to UpdateNetworkTime().
58 base::Time network_time_
;
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_
;
67 // Uncertainty of |network_time_| based on added inaccuracies/resolution.
68 // See UpdateNetworkTime(..) implementation for details.
69 base::TimeDelta network_time_uncertainty_
;
71 base::ThreadChecker thread_checker_
;
73 bool received_network_time_
;
75 DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker
);
78 } // namespace network_time
80 #endif // COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_