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