Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ | 5 #ifndef COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ |
| 6 #define COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ | 6 #define COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/threading/thread_checker.h" | 10 #include "base/threading/thread_checker.h" |
| 11 #include "base/time/clock.h" | 11 #include "base/time/clock.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "base/timer/timer.h" | |
| 14 #include "net/url_request/url_fetcher_delegate.h" | |
| 13 | 15 |
| 14 class PrefRegistrySimple; | 16 class PrefRegistrySimple; |
| 15 class PrefService; | 17 class PrefService; |
| 16 | 18 |
| 17 namespace base { | 19 namespace base { |
| 18 class TickClock; | 20 class TickClock; |
| 19 } | 21 } |
| 20 | 22 |
| 23 namespace client_update_protocol { | |
| 24 class Ecdsa; | |
| 25 } | |
| 26 | |
| 27 namespace net { | |
| 28 class URLFetcher; | |
| 29 class URLRequestContextGetter; | |
| 30 } | |
| 31 | |
| 21 namespace network_time { | 32 namespace network_time { |
| 22 | 33 |
| 23 // Clock resolution is platform dependent. | 34 // Clock resolution is platform dependent. |
| 24 #if defined(OS_WIN) | 35 #if defined(OS_WIN) |
| 25 const int64_t kTicksResolutionMs = base::Time::kMinLowResolutionThresholdMs; | 36 const int64_t kTicksResolutionMs = base::Time::kMinLowResolutionThresholdMs; |
| 26 #else | 37 #else |
| 27 const int64_t kTicksResolutionMs = 1; // Assume 1ms for non-windows platforms. | 38 const int64_t kTicksResolutionMs = 1; // Assume 1ms for non-windows platforms. |
| 28 #endif | 39 #endif |
| 29 | 40 |
| 41 struct NetworkTimeTrackerPeer; | |
| 42 | |
| 30 // A class that receives network time updates and can provide the network time | 43 // A class that receives network time updates and can provide the network time |
| 31 // for a corresponding local time. This class is not thread safe. | 44 // for a corresponding local time. This class is not thread safe. |
| 32 class NetworkTimeTracker { | 45 class NetworkTimeTracker : public net::URLFetcherDelegate { |
| 33 public: | 46 public: |
| 34 static void RegisterPrefs(PrefRegistrySimple* registry); | 47 static void RegisterPrefs(PrefRegistrySimple* registry); |
| 35 | 48 |
| 49 // Constructor. Arguments may be stubbed out for tests. |getter|, if not | |
| 50 // null, will cause automatic queries to a time server. Otherwise, time is | |
| 51 // available only if |UpdateNetworkTime| is called. | |
| 36 NetworkTimeTracker(scoped_ptr<base::Clock> clock, | 52 NetworkTimeTracker(scoped_ptr<base::Clock> clock, |
| 37 scoped_ptr<base::TickClock> tick_clock, | 53 scoped_ptr<base::TickClock> tick_clock, |
| 38 PrefService* pref_service); | 54 PrefService* pref_service, |
| 39 ~NetworkTimeTracker(); | 55 net::URLRequestContextGetter* getter = nullptr); |
| 56 ~NetworkTimeTracker() override; | |
| 40 | 57 |
| 41 // Sets |network_time| to an estimate of the true time. Returns true if time | 58 // Sets |network_time| to an estimate of the true time. Returns true if time |
| 42 // is available, and false otherwise. If |uncertainty| is non-NULL, it will | 59 // is available, and false otherwise. If |uncertainty| is non-NULL, it will |
| 43 // be set to an estimate of the error range. | 60 // be set to an estimate of the error range. |
| 44 // | 61 // |
| 45 // Network time may be available on startup if deserialized from a pref. | 62 // Network time may be available on startup if deserialized from a pref. |
| 46 // Failing that, a call to |UpdateNetworkTime| is required to make time | 63 // Failing that, a call to |UpdateNetworkTime| is required to make time |
| 47 // available to callers of |GetNetworkTime|. Subsequently, network time may | 64 // available to callers of |GetNetworkTime|. Subsequently, network time may |
| 48 // become unavailable if |NetworkTimeTracker| has reason to believe it is no | 65 // become unavailable if |NetworkTimeTracker| has reason to believe it is no |
| 49 // longer accurate. Consumers should even be prepared to handle the case | 66 // longer accurate. Consumers should even be prepared to handle the case |
| 50 // where calls to |GetNetworkTime| never once succeeds. | 67 // where calls to |GetNetworkTime| never once succeeds. |
| 51 bool GetNetworkTime(base::Time* network_time, | 68 bool GetNetworkTime(base::Time* network_time, |
| 52 base::TimeDelta* uncertainty) const; | 69 base::TimeDelta* uncertainty) const; |
| 53 | 70 |
| 54 // Calculates corresponding time ticks according to the given parameters. | 71 // Calculates corresponding time ticks according to the given parameters. |
| 55 // The provided |network_time| is precise at the given |resolution| and | 72 // The provided |network_time| is precise at the given |resolution| and |
| 56 // represent the time between now and up to |latency| + (now - |post_time|) | 73 // represent the time between now and up to |latency| + (now - |post_time|) |
| 57 // ago. | 74 // ago. |
| 58 void UpdateNetworkTime(base::Time network_time, | 75 void UpdateNetworkTime(base::Time network_time, |
| 59 base::TimeDelta resolution, | 76 base::TimeDelta resolution, |
| 60 base::TimeDelta latency, | 77 base::TimeDelta latency, |
| 61 base::TimeTicks post_time); | 78 base::TimeTicks post_time); |
| 62 | 79 |
| 63 private: | 80 private: |
| 81 friend struct NetworkTimeTrackerPeer; | |
| 82 | |
| 83 // Sends a query to the secure time service. Upon response, execution resumes | |
| 84 // in |OnURLFetchComplete|. | |
| 85 void QueryTimeService(); | |
| 86 | |
| 87 // Called to process responses from the secure time service. | |
|
estark
2016/03/28 17:35:10
Preface this section with
// net::URLFetcherDeleg
mab
2016/03/28 19:56:26
Done.
| |
| 88 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
| 89 | |
| 90 // State variables for internally-managed secure time service queries. | |
| 91 base::RepeatingTimer query_timer_; | |
| 92 net::URLRequestContextGetter* getter_; | |
| 93 scoped_ptr<net::URLFetcher> time_fetcher_; | |
| 94 base::TimeTicks fetch_started_; | |
| 95 scoped_ptr<client_update_protocol::Ecdsa> query_signer_; | |
| 96 | |
| 64 // The |Clock| and |TickClock| are used to sanity-check one another, allowing | 97 // The |Clock| and |TickClock| are used to sanity-check one another, allowing |
| 65 // the NetworkTimeTracker to notice e.g. suspend/resume events and clock | 98 // the NetworkTimeTracker to notice e.g. suspend/resume events and clock |
| 66 // resets. | 99 // resets. |
| 67 scoped_ptr<base::Clock> clock_; | 100 scoped_ptr<base::Clock> clock_; |
| 68 scoped_ptr<base::TickClock> tick_clock_; | 101 scoped_ptr<base::TickClock> tick_clock_; |
| 69 | 102 |
| 70 PrefService* pref_service_; | 103 PrefService* pref_service_; |
| 71 | 104 |
| 72 // Network time based on last call to UpdateNetworkTime(). | 105 // Network time based on last call to UpdateNetworkTime(). |
| 73 mutable base::Time network_time_at_last_measurement_; | 106 mutable base::Time network_time_at_last_measurement_; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 87 base::ThreadChecker thread_checker_; | 120 base::ThreadChecker thread_checker_; |
| 88 | 121 |
| 89 bool received_network_time_; | 122 bool received_network_time_; |
| 90 | 123 |
| 91 DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker); | 124 DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker); |
| 92 }; | 125 }; |
| 93 | 126 |
| 94 } // namespace network_time | 127 } // namespace network_time |
| 95 | 128 |
| 96 #endif // COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ | 129 #endif // COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ |
| OLD | NEW |