1 // Copyright (c) 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 NET_BASE_NETWORK_TIME_NOTIFIER_H_ 6 #define NET_BASE_NETWORK_TIME_NOTIFIER_H_ 7 8 #include <vector> 9 10 #include "base/callback.h" 11 #include "base/memory/ref_counted.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/threading/thread_checker.h" 15 #include "base/time/tick_clock.h" 16 #include "base/time/time.h" 17 #include "net/base/net_export.h" 18 19 namespace net { 20 21 // A class that receives updates for and maintains network time. Network time 22 // sources can pass updates through UpdateNetworkTime, and network time 23 // consumers can register as observers. This class is not thread-safe. 24 class NET_EXPORT NetworkTimeNotifier { 25 public: 26 // Callback for observers to receive network time updates. 27 // The parameters are: 28 // const base::Time& network_time - the new network time. 29 // const base::TimeTicks& network_time_ticks - the ticks time that corresponds 30 // with |network_time|. 31 // const base::TimeDelta& network_time_uncertainty - the uncertainty 32 // associated with the new network time. 33 typedef base::Callback<void(const base::Time&, 34 const base::TimeTicks&, 35 const base::TimeDelta&)> ObserverCallback; 36 37 // Takes ownership of |tick_clock|. 38 explicit NetworkTimeNotifier(scoped_ptr<base::TickClock> tick_clock); 39 ~NetworkTimeNotifier(); 40 41 // Calculates corresponding time ticks according to the given parameters and 42 // notifies observers. The provided |network_time| is precise at the given 43 // |resolution| and represent the time between now and up to |latency| + 44 // (now - |post_time|) ago. 45 void UpdateNetworkTime(const base::Time& network_time, 46 const base::TimeDelta& resolution, 47 const base::TimeDelta& latency, 48 const base::TimeTicks& post_time); 49 50 // |observer_callback| will invoked every time the network time is updated, or 51 // if a network time is already available when AddObserver is called. 52 void AddObserver(const ObserverCallback& observer_callback); 53 54 private: 55 base::ThreadChecker thread_checker_; 56 57 // For querying current time ticks. 58 scoped_ptr<base::TickClock> tick_clock_; 59 60 // The network time based on last call to UpdateNetworkTime(). 61 base::Time network_time_; 62 63 // The estimated local time from |tick_clock| that corresponds with 64 // |network_time|. Assumes the actual network time measurement was performed 65 // midway through the latency time, and does not account for suspect/resume 66 // events since the network time was measured. 67 // See UpdateNetworkTime(..) implementation for details. 68 base::TimeTicks network_time_ticks_; 69 70 // Uncertainty of |network_time_| based on added inaccuracies/resolution. 71 // See UpdateNetworkTime(..) implementation for details. 72 base::TimeDelta network_time_uncertainty_; 73 74 // List of network time update observers. 75 // A vector of callbacks is used, rather than an ObserverList, so that the 76 // lifetime of the observer can be bound to the callback. 77 std::vector<ObserverCallback> observers_; 78 79 DISALLOW_COPY_AND_ASSIGN(NetworkTimeNotifier); 80 }; 81 82 } // namespace net 83 84 #endif // NET_BASE_NETWORK_TIME_NOTIFIER_H_ 85