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