Home | History | Annotate | Download | only in net
      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 CHROME_BROWSER_NET_NETWORK_TIME_TRACKER_H_
      6 #define CHROME_BROWSER_NET_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   // Returns the network time corresponding to |time_ticks| if network time
     43   // is available. Returns false if no network time is available yet. Can also
     44   // return the error range if |uncertainty| isn't NULL.
     45   bool GetNetworkTime(const base::TimeTicks& time_ticks,
     46                       base::Time* network_time,
     47                       base::TimeDelta* uncertainty) const;
     48 
     49   // Build a callback for use in updating the network time notifier.
     50   // This method must be called on the UI thread, while the callback can be
     51   // called from any thread.
     52   static UpdateCallback BuildNotifierUpdateCallback();
     53 
     54  private:
     55   friend class NetworkTimeTrackerTest;
     56 
     57   // Returns the callback used to register for time updates from the network
     58   // time notifier on the IO thread.
     59   net::NetworkTimeNotifier::ObserverCallback BuildObserverCallback();
     60 
     61   // net::NetworkTimeNotifier::ObserverCallback implementation.
     62   void OnNetworkTimeUpdate(
     63       const base::Time& network_time,
     64       const base::TimeTicks& network_time_ticks,
     65       const base::TimeDelta& network_time_uncertainty);
     66 
     67   // Network time based on last call to UpdateNetworkTime().
     68   base::Time network_time_;
     69 
     70   // The estimated ticks count from when |network_time_| was set based on the
     71   // system's tick count.
     72   base::TimeTicks network_time_ticks_;
     73 
     74   // Uncertainty of |network_time_| based on added inaccuracies/resolution.
     75   base::TimeDelta network_time_uncertainty_;
     76 
     77   base::WeakPtrFactory<NetworkTimeTracker> weak_ptr_factory_;
     78 
     79   base::ThreadChecker thread_checker_;
     80 
     81   DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker);
     82 };
     83 
     84 #endif  // CHROME_BROWSER_NET_NETWORK_TIME_TRACKER_H_
     85