• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #ifndef CHROME_BROWSER_NETWORK_TIME_NETWORK_TIME_TRACKER_H_
6 #define CHROME_BROWSER_NETWORK_TIME_NETWORK_TIME_TRACKER_H_
7 
8 #include "base/callback.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/threading/thread_checker.h"
11 #include "base/time/time.h"
12 #include "net/base/network_time_notifier.h"
13 
14 class IOThread;
15 class NetworkTimeTrackerTest;
16 
17 // 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
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 {
25  public:
26   // Callback for updating network time based on http responses. This callback
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();
38 
39   // Starts tracking network time.
40   void Start();
41 
42   struct TimeMapping {
43     TimeMapping(base::Time local_time, base::Time network_time);
44     base::Time local_time;
45     base::Time network_time;
46   };
47 
48   // Initializes from saved times to be able to compute network time before
49   // receiving accurate time from HTTP response.
50   void InitFromSavedTime(const TimeMapping& saved);
51 
52   // 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
54   // return the error range if |uncertainty| isn't NULL.
55   bool GetNetworkTime(const base::TimeTicks& time_ticks,
56                       base::Time* network_time,
57                       base::TimeDelta* uncertainty) const;
58 
59   // Build a callback for use in updating the network time notifier.
60   // This method must be called on the UI thread, while the callback can be
61   // called from any thread.
62   static UpdateCallback BuildNotifierUpdateCallback();
63 
received_network_time()64   bool received_network_time() const {
65     return received_network_time_;
66   }
67 
68  private:
69   friend class NetworkTimeTrackerTest;
70 
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 
81   // Network time based on last call to UpdateNetworkTime().
82   base::Time network_time_;
83 
84   // The estimated ticks count from when |network_time_| was set based on the
85   // system's tick count.
86   base::TimeTicks network_time_ticks_;
87 
88   // Uncertainty of |network_time_| based on added inaccuracies/resolution.
89   base::TimeDelta network_time_uncertainty_;
90 
91   base::WeakPtrFactory<NetworkTimeTracker> weak_ptr_factory_;
92 
93   base::ThreadChecker thread_checker_;
94 
95   bool received_network_time_;
96 
97   DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker);
98 };
99 
100 #endif  // CHROME_BROWSER_NETWORK_TIME_NETWORK_TIME_TRACKER_H_
101