| 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/callback.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/memory/weak_ptr.h" | |
| 10 #include "base/threading/thread_checker.h" | 9 #include "base/threading/thread_checker.h" |
| 11 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 12 #include "net/base/network_time_notifier.h" | |
| 13 | 11 |
| 14 class IOThread; | 12 namespace base { |
| 15 class NetworkTimeTrackerTest; | 13 class TickClock; |
| 14 } |
| 16 | 15 |
| 17 // A class that receives network time updates and can provide the network time | 16 // A class that receives network time updates and can provide the network time |
| 18 // for a corresponding local time. This class is not thread safe, but may live | 17 // for a corresponding local time. This class is not thread safe. |
| 19 // on any thread. | |
| 20 // Note that all NetworkTimeTracker instances interact with a | |
| 21 // NetworkTimeNotifier singleton that lives on the IO thread. This class | |
| 22 // enables arbitrary threads to maintain their own network time source without | |
| 23 // having to deal with the details of the Notifier's implementation. | |
| 24 class NetworkTimeTracker { | 18 class NetworkTimeTracker { |
| 25 public: | 19 public: |
| 26 // Callback for updating network time based on http responses. This callback | 20 explicit NetworkTimeTracker(scoped_ptr<base::TickClock> tick_clock); |
| 27 // is thread safe and should be called directly without posting to any thread. | |
| 28 // The parameters are: | |
| 29 // const base::Time& network_time - the actual time. | |
| 30 // const base::TimeDelta& resolution - how precise the reading is. | |
| 31 // const base::TimeDelta& latency - the http request's latency. | |
| 32 typedef base::Callback<void(const base::Time&, | |
| 33 const base::TimeDelta&, | |
| 34 const base::TimeDelta&)> UpdateCallback; | |
| 35 | |
| 36 NetworkTimeTracker(); | |
| 37 ~NetworkTimeTracker(); | 21 ~NetworkTimeTracker(); |
| 38 | 22 |
| 39 // Starts tracking network time. | |
| 40 void Start(); | |
| 41 | |
| 42 struct TimeMapping { | 23 struct TimeMapping { |
| 43 TimeMapping(base::Time local_time, base::Time network_time); | 24 TimeMapping(const base::Time& local_time, const base::Time& network_time); |
| 44 base::Time local_time; | 25 base::Time local_time; |
| 45 base::Time network_time; | 26 base::Time network_time; |
| 46 }; | 27 }; |
| 47 | 28 |
| 48 // Initializes from saved times to be able to compute network time before | 29 // Initializes from saved times to be able to compute network time before |
| 49 // receiving accurate time from HTTP response. | 30 // receiving accurate time from HTTP response. |
| 50 void InitFromSavedTime(const TimeMapping& saved); | 31 void InitFromSavedTime(const TimeMapping& saved); |
| 51 | 32 |
| 52 // Returns the network time corresponding to |time_ticks| if network time | 33 // Returns the network time corresponding to |time_ticks| if network time |
| 53 // is available. Returns false if no network time is available yet. Can also | 34 // is available. Returns false if no network time is available yet. Can also |
| 54 // return the error range if |uncertainty| isn't NULL. | 35 // return the error range if |uncertainty| isn't NULL. |
| 55 bool GetNetworkTime(const base::TimeTicks& time_ticks, | 36 bool GetNetworkTime(const base::TimeTicks& time_ticks, |
| 56 base::Time* network_time, | 37 base::Time* network_time, |
| 57 base::TimeDelta* uncertainty) const; | 38 base::TimeDelta* uncertainty) const; |
| 58 | 39 |
| 59 // Build a callback for use in updating the network time notifier. | 40 // Calculates corresponding time ticks according to the given parameters. |
| 60 // This method must be called on the UI thread, while the callback can be | 41 // The provided |network_time| is precise at the given |resolution| and |
| 61 // called from any thread. | 42 // represent the time between now and up to |latency| + (now - |post_time|) |
| 62 static UpdateCallback BuildNotifierUpdateCallback(); | 43 // ago. |
| 44 void UpdateNetworkTime(const base::Time& network_time, |
| 45 const base::TimeDelta& resolution, |
| 46 const base::TimeDelta& latency, |
| 47 const base::TimeTicks& post_time); |
| 63 | 48 |
| 64 bool received_network_time() const { | 49 bool received_network_time() const { |
| 65 return received_network_time_; | 50 return received_network_time_; |
| 66 } | 51 } |
| 67 | 52 |
| 68 private: | 53 private: |
| 69 friend class NetworkTimeTrackerTest; | 54 // For querying current time ticks. |
| 70 | 55 scoped_ptr<base::TickClock> tick_clock_; |
| 71 // Returns the callback used to register for time updates from the network | |
| 72 // time notifier on the IO thread. | |
| 73 net::NetworkTimeNotifier::ObserverCallback BuildObserverCallback(); | |
| 74 | |
| 75 // net::NetworkTimeNotifier::ObserverCallback implementation. | |
| 76 void OnNetworkTimeUpdate( | |
| 77 const base::Time& network_time, | |
| 78 const base::TimeTicks& network_time_ticks, | |
| 79 const base::TimeDelta& network_time_uncertainty); | |
| 80 | 56 |
| 81 // Network time based on last call to UpdateNetworkTime(). | 57 // Network time based on last call to UpdateNetworkTime(). |
| 82 base::Time network_time_; | 58 base::Time network_time_; |
| 83 | 59 |
| 84 // The estimated ticks count from when |network_time_| was set based on the | 60 // The estimated local time from |tick_clock| that corresponds with |
| 85 // system's tick count. | 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. |
| 86 base::TimeTicks network_time_ticks_; | 65 base::TimeTicks network_time_ticks_; |
| 87 | 66 |
| 88 // Uncertainty of |network_time_| based on added inaccuracies/resolution. | 67 // Uncertainty of |network_time_| based on added inaccuracies/resolution. |
| 68 // See UpdateNetworkTime(..) implementation for details. |
| 89 base::TimeDelta network_time_uncertainty_; | 69 base::TimeDelta network_time_uncertainty_; |
| 90 | 70 |
| 91 base::WeakPtrFactory<NetworkTimeTracker> weak_ptr_factory_; | |
| 92 | |
| 93 base::ThreadChecker thread_checker_; | 71 base::ThreadChecker thread_checker_; |
| 94 | 72 |
| 95 bool received_network_time_; | 73 bool received_network_time_; |
| 96 | 74 |
| 97 DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker); | 75 DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker); |
| 98 }; | 76 }; |
| 99 | 77 |
| 100 #endif // CHROME_BROWSER_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ | 78 #endif // CHROME_BROWSER_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ |
| OLD | NEW |