OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ | 5 #ifndef COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ |
6 #define COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ | 6 #define COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
(...skipping 30 matching lines...) Expand all Loading... |
41 #if defined(OS_WIN) | 41 #if defined(OS_WIN) |
42 const int64_t kTicksResolutionMs = base::Time::kMinLowResolutionThresholdMs; | 42 const int64_t kTicksResolutionMs = base::Time::kMinLowResolutionThresholdMs; |
43 #else | 43 #else |
44 const int64_t kTicksResolutionMs = 1; // Assume 1ms for non-windows platforms. | 44 const int64_t kTicksResolutionMs = 1; // Assume 1ms for non-windows platforms. |
45 #endif | 45 #endif |
46 | 46 |
47 // A class that receives network time updates and can provide the network time | 47 // A class that receives network time updates and can provide the network time |
48 // for a corresponding local time. This class is not thread safe. | 48 // for a corresponding local time. This class is not thread safe. |
49 class NetworkTimeTracker : public net::URLFetcherDelegate { | 49 class NetworkTimeTracker : public net::URLFetcherDelegate { |
50 public: | 50 public: |
| 51 // Describes the result of a GetNetworkTime() call, describing whether |
| 52 // network time was available and if not, why not. |
| 53 enum NetworkTimeResult { |
| 54 // Network time is available. |
| 55 NETWORK_TIME_AVAILABLE, |
| 56 // A time has been retrieved from the network in the past, but |
| 57 // network time is no longer available because the tracker fell out |
| 58 // of sync due to, for example, a suspend/resume. |
| 59 NETWORK_TIME_SYNC_LOST, |
| 60 // Network time is unavailable because the tracker has not yet |
| 61 // retrieved a time from the network. |
| 62 NETWORK_TIME_NO_SYNC, |
| 63 }; |
| 64 |
51 static void RegisterPrefs(PrefRegistrySimple* registry); | 65 static void RegisterPrefs(PrefRegistrySimple* registry); |
52 | 66 |
53 // Constructor. Arguments may be stubbed out for tests. |getter|, if not | 67 // Constructor. Arguments may be stubbed out for tests. |getter|, if not |
54 // null, will cause automatic queries to a time server. Otherwise, time is | 68 // null, will cause automatic queries to a time server. Otherwise, time is |
55 // available only if |UpdateNetworkTime| is called. | 69 // available only if |UpdateNetworkTime| is called. |
56 NetworkTimeTracker(std::unique_ptr<base::Clock> clock, | 70 NetworkTimeTracker(std::unique_ptr<base::Clock> clock, |
57 std::unique_ptr<base::TickClock> tick_clock, | 71 std::unique_ptr<base::TickClock> tick_clock, |
58 PrefService* pref_service, | 72 PrefService* pref_service, |
59 scoped_refptr<net::URLRequestContextGetter> getter); | 73 scoped_refptr<net::URLRequestContextGetter> getter); |
60 ~NetworkTimeTracker() override; | 74 ~NetworkTimeTracker() override; |
61 | 75 |
62 // Sets |network_time| to an estimate of the true time. Returns true if time | 76 // Sets |network_time| to an estimate of the true time. Returns |
63 // is available, and false otherwise. If |uncertainty| is non-NULL, it will | 77 // NETWORK_TIME_AVAILABLE if time is available. If |uncertainty| is |
64 // be set to an estimate of the error range. | 78 // non-NULL, it will be set to an estimate of the error range. |
| 79 // |
| 80 // If network time is unavailable, this method returns |
| 81 // NETWORK_TIME_SYNC_LOST or NETWORK_TIME_NO_SYNC to indicate the |
| 82 // reason. |
65 // | 83 // |
66 // Network time may be available on startup if deserialized from a pref. | 84 // Network time may be available on startup if deserialized from a pref. |
67 // Failing that, a call to |UpdateNetworkTime| is required to make time | 85 // Failing that, a call to |UpdateNetworkTime| is required to make time |
68 // available to callers of |GetNetworkTime|. Subsequently, network time may | 86 // available to callers of |GetNetworkTime|. Subsequently, network time may |
69 // become unavailable if |NetworkTimeTracker| has reason to believe it is no | 87 // become unavailable if |NetworkTimeTracker| has reason to believe it is no |
70 // longer accurate. Consumers should even be prepared to handle the case | 88 // longer accurate. Consumers should even be prepared to handle the case |
71 // where calls to |GetNetworkTime| never once succeeds. | 89 // where calls to |GetNetworkTime| never once succeeds. |
72 bool GetNetworkTime(base::Time* network_time, | 90 NetworkTimeResult GetNetworkTime(base::Time* network_time, |
73 base::TimeDelta* uncertainty) const; | 91 base::TimeDelta* uncertainty) const; |
74 | 92 |
75 // Calculates corresponding time ticks according to the given parameters. | 93 // Calculates corresponding time ticks according to the given parameters. |
76 // The provided |network_time| is precise at the given |resolution| and | 94 // The provided |network_time| is precise at the given |resolution| and |
77 // represent the time between now and up to |latency| + (now - |post_time|) | 95 // represent the time between now and up to |latency| + (now - |post_time|) |
78 // ago. | 96 // ago. |
79 void UpdateNetworkTime(base::Time network_time, | 97 void UpdateNetworkTime(base::Time network_time, |
80 base::TimeDelta resolution, | 98 base::TimeDelta resolution, |
81 base::TimeDelta latency, | 99 base::TimeDelta latency, |
82 base::TimeTicks post_time); | 100 base::TimeTicks post_time); |
83 | 101 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 base::TimeDelta network_time_uncertainty_; | 170 base::TimeDelta network_time_uncertainty_; |
153 | 171 |
154 base::ThreadChecker thread_checker_; | 172 base::ThreadChecker thread_checker_; |
155 | 173 |
156 DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker); | 174 DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker); |
157 }; | 175 }; |
158 | 176 |
159 } // namespace network_time | 177 } // namespace network_time |
160 | 178 |
161 #endif // COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ | 179 #endif // COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ |
OLD | NEW |