| 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 <memory> |
| 9 |
| 8 #include "base/macros.h" | 10 #include "base/macros.h" |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/threading/thread_checker.h" | 11 #include "base/threading/thread_checker.h" |
| 11 #include "base/time/clock.h" | 12 #include "base/time/clock.h" |
| 12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 13 | 14 |
| 14 class PrefRegistrySimple; | 15 class PrefRegistrySimple; |
| 15 class PrefService; | 16 class PrefService; |
| 16 | 17 |
| 17 namespace base { | 18 namespace base { |
| 18 class TickClock; | 19 class TickClock; |
| 19 } | 20 } |
| 20 | 21 |
| 21 namespace network_time { | 22 namespace network_time { |
| 22 | 23 |
| 23 // Clock resolution is platform dependent. | 24 // Clock resolution is platform dependent. |
| 24 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
| 25 const int64_t kTicksResolutionMs = base::Time::kMinLowResolutionThresholdMs; | 26 const int64_t kTicksResolutionMs = base::Time::kMinLowResolutionThresholdMs; |
| 26 #else | 27 #else |
| 27 const int64_t kTicksResolutionMs = 1; // Assume 1ms for non-windows platforms. | 28 const int64_t kTicksResolutionMs = 1; // Assume 1ms for non-windows platforms. |
| 28 #endif | 29 #endif |
| 29 | 30 |
| 30 // A class that receives network time updates and can provide the network time | 31 // A class that receives network time updates and can provide the network time |
| 31 // for a corresponding local time. This class is not thread safe. | 32 // for a corresponding local time. This class is not thread safe. |
| 32 class NetworkTimeTracker { | 33 class NetworkTimeTracker { |
| 33 public: | 34 public: |
| 34 static void RegisterPrefs(PrefRegistrySimple* registry); | 35 static void RegisterPrefs(PrefRegistrySimple* registry); |
| 35 | 36 |
| 36 NetworkTimeTracker(scoped_ptr<base::Clock> clock, | 37 NetworkTimeTracker(std::unique_ptr<base::Clock> clock, |
| 37 scoped_ptr<base::TickClock> tick_clock, | 38 std::unique_ptr<base::TickClock> tick_clock, |
| 38 PrefService* pref_service); | 39 PrefService* pref_service); |
| 39 ~NetworkTimeTracker(); | 40 ~NetworkTimeTracker(); |
| 40 | 41 |
| 41 // Sets |network_time| to an estimate of the true time. Returns true if time | 42 // Sets |network_time| to an estimate of the true time. Returns true if time |
| 42 // is available, and false otherwise. If |uncertainty| is non-NULL, it will | 43 // is available, and false otherwise. If |uncertainty| is non-NULL, it will |
| 43 // be set to an estimate of the error range. | 44 // be set to an estimate of the error range. |
| 44 // | 45 // |
| 45 // Network time may be available on startup if deserialized from a pref. | 46 // Network time may be available on startup if deserialized from a pref. |
| 46 // Failing that, a call to |UpdateNetworkTime| is required to make time | 47 // Failing that, a call to |UpdateNetworkTime| is required to make time |
| 47 // available to callers of |GetNetworkTime|. Subsequently, network time may | 48 // available to callers of |GetNetworkTime|. Subsequently, network time may |
| 48 // become unavailable if |NetworkTimeTracker| has reason to believe it is no | 49 // become unavailable if |NetworkTimeTracker| has reason to believe it is no |
| 49 // longer accurate. Consumers should even be prepared to handle the case | 50 // longer accurate. Consumers should even be prepared to handle the case |
| 50 // where calls to |GetNetworkTime| never once succeeds. | 51 // where calls to |GetNetworkTime| never once succeeds. |
| 51 bool GetNetworkTime(base::Time* network_time, | 52 bool GetNetworkTime(base::Time* network_time, |
| 52 base::TimeDelta* uncertainty) const; | 53 base::TimeDelta* uncertainty) const; |
| 53 | 54 |
| 54 // Calculates corresponding time ticks according to the given parameters. | 55 // Calculates corresponding time ticks according to the given parameters. |
| 55 // The provided |network_time| is precise at the given |resolution| and | 56 // The provided |network_time| is precise at the given |resolution| and |
| 56 // represent the time between now and up to |latency| + (now - |post_time|) | 57 // represent the time between now and up to |latency| + (now - |post_time|) |
| 57 // ago. | 58 // ago. |
| 58 void UpdateNetworkTime(base::Time network_time, | 59 void UpdateNetworkTime(base::Time network_time, |
| 59 base::TimeDelta resolution, | 60 base::TimeDelta resolution, |
| 60 base::TimeDelta latency, | 61 base::TimeDelta latency, |
| 61 base::TimeTicks post_time); | 62 base::TimeTicks post_time); |
| 62 | 63 |
| 63 private: | 64 private: |
| 64 // The |Clock| and |TickClock| are used to sanity-check one another, allowing | 65 // The |Clock| and |TickClock| are used to sanity-check one another, allowing |
| 65 // the NetworkTimeTracker to notice e.g. suspend/resume events and clock | 66 // the NetworkTimeTracker to notice e.g. suspend/resume events and clock |
| 66 // resets. | 67 // resets. |
| 67 scoped_ptr<base::Clock> clock_; | 68 std::unique_ptr<base::Clock> clock_; |
| 68 scoped_ptr<base::TickClock> tick_clock_; | 69 std::unique_ptr<base::TickClock> tick_clock_; |
| 69 | 70 |
| 70 PrefService* pref_service_; | 71 PrefService* pref_service_; |
| 71 | 72 |
| 72 // Network time based on last call to UpdateNetworkTime(). | 73 // Network time based on last call to UpdateNetworkTime(). |
| 73 mutable base::Time network_time_at_last_measurement_; | 74 mutable base::Time network_time_at_last_measurement_; |
| 74 | 75 |
| 75 // The estimated local times that correspond with |network_time_|. Assumes | 76 // The estimated local times that correspond with |network_time_|. Assumes |
| 76 // the actual network time measurement was performed midway through the | 77 // the actual network time measurement was performed midway through the |
| 77 // latency time. See UpdateNetworkTime(...) implementation for details. The | 78 // latency time. See UpdateNetworkTime(...) implementation for details. The |
| 78 // tick clock is the one actually used to return values to callers, but both | 79 // tick clock is the one actually used to return values to callers, but both |
| 79 // clocks must agree to within some tolerance. | 80 // clocks must agree to within some tolerance. |
| 80 base::Time time_at_last_measurement_; | 81 base::Time time_at_last_measurement_; |
| 81 base::TimeTicks ticks_at_last_measurement_; | 82 base::TimeTicks ticks_at_last_measurement_; |
| 82 | 83 |
| 83 // Uncertainty of |network_time_| based on added inaccuracies/resolution. See | 84 // Uncertainty of |network_time_| based on added inaccuracies/resolution. See |
| 84 // UpdateNetworkTime(...) implementation for details. | 85 // UpdateNetworkTime(...) implementation for details. |
| 85 base::TimeDelta network_time_uncertainty_; | 86 base::TimeDelta network_time_uncertainty_; |
| 86 | 87 |
| 87 base::ThreadChecker thread_checker_; | 88 base::ThreadChecker thread_checker_; |
| 88 | 89 |
| 89 bool received_network_time_; | 90 bool received_network_time_; |
| 90 | 91 |
| 91 DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker); | 92 DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker); |
| 92 }; | 93 }; |
| 93 | 94 |
| 94 } // namespace network_time | 95 } // namespace network_time |
| 95 | 96 |
| 96 #endif // COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ | 97 #endif // COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ |
| OLD | NEW |