| 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 <stdint.h> | |
| 9 #include <memory> | 8 #include <memory> |
| 10 | 9 |
| 11 #include "base/gtest_prod_util.h" | |
| 12 #include "base/macros.h" | 10 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "base/threading/thread_checker.h" | 11 #include "base/threading/thread_checker.h" |
| 15 #include "base/time/clock.h" | 12 #include "base/time/clock.h" |
| 16 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 17 #include "base/timer/timer.h" | |
| 18 #include "net/url_request/url_fetcher_delegate.h" | |
| 19 #include "url/gurl.h" | |
| 20 | 14 |
| 21 class PrefRegistrySimple; | 15 class PrefRegistrySimple; |
| 22 class PrefService; | 16 class PrefService; |
| 23 | 17 |
| 24 namespace base { | 18 namespace base { |
| 25 class MessageLoop; | |
| 26 class TickClock; | 19 class TickClock; |
| 27 } // namespace base | 20 } |
| 28 | |
| 29 namespace client_update_protocol { | |
| 30 class Ecdsa; | |
| 31 } // namespace client_udpate_protocol | |
| 32 | |
| 33 namespace net { | |
| 34 class URLFetcher; | |
| 35 class URLRequestContextGetter; | |
| 36 } // namespace net | |
| 37 | 21 |
| 38 namespace network_time { | 22 namespace network_time { |
| 39 | 23 |
| 40 // Clock resolution is platform dependent. | 24 // Clock resolution is platform dependent. |
| 41 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
| 42 const int64_t kTicksResolutionMs = base::Time::kMinLowResolutionThresholdMs; | 26 const int64_t kTicksResolutionMs = base::Time::kMinLowResolutionThresholdMs; |
| 43 #else | 27 #else |
| 44 const int64_t kTicksResolutionMs = 1; // Assume 1ms for non-windows platforms. | 28 const int64_t kTicksResolutionMs = 1; // Assume 1ms for non-windows platforms. |
| 45 #endif | 29 #endif |
| 46 | 30 |
| 47 // A class that receives network time updates and can provide the network time | 31 // A class that receives network time updates and can provide the network time |
| 48 // for a corresponding local time. This class is not thread safe. | 32 // for a corresponding local time. This class is not thread safe. |
| 49 class NetworkTimeTracker : public net::URLFetcherDelegate { | 33 class NetworkTimeTracker { |
| 50 public: | 34 public: |
| 51 static void RegisterPrefs(PrefRegistrySimple* registry); | 35 static void RegisterPrefs(PrefRegistrySimple* registry); |
| 52 | 36 |
| 53 // Constructor. Arguments may be stubbed out for tests. |getter|, if not | |
| 54 // null, will cause automatic queries to a time server. Otherwise, time is | |
| 55 // available only if |UpdateNetworkTime| is called. | |
| 56 NetworkTimeTracker(std::unique_ptr<base::Clock> clock, | 37 NetworkTimeTracker(std::unique_ptr<base::Clock> clock, |
| 57 std::unique_ptr<base::TickClock> tick_clock, | 38 std::unique_ptr<base::TickClock> tick_clock, |
| 58 PrefService* pref_service, | 39 PrefService* pref_service); |
| 59 scoped_refptr<net::URLRequestContextGetter> getter); | 40 ~NetworkTimeTracker(); |
| 60 ~NetworkTimeTracker() override; | |
| 61 | 41 |
| 62 // Sets |network_time| to an estimate of the true time. Returns true if time | 42 // Sets |network_time| to an estimate of the true time. Returns true if time |
| 63 // is available, and false otherwise. If |uncertainty| is non-NULL, it will | 43 // is available, and false otherwise. If |uncertainty| is non-NULL, it will |
| 64 // be set to an estimate of the error range. | 44 // be set to an estimate of the error range. |
| 65 // | 45 // |
| 66 // Network time may be available on startup if deserialized from a pref. | 46 // Network time may be available on startup if deserialized from a pref. |
| 67 // Failing that, a call to |UpdateNetworkTime| is required to make time | 47 // Failing that, a call to |UpdateNetworkTime| is required to make time |
| 68 // available to callers of |GetNetworkTime|. Subsequently, network time may | 48 // available to callers of |GetNetworkTime|. Subsequently, network time may |
| 69 // become unavailable if |NetworkTimeTracker| has reason to believe it is no | 49 // become unavailable if |NetworkTimeTracker| has reason to believe it is no |
| 70 // longer accurate. Consumers should even be prepared to handle the case | 50 // longer accurate. Consumers should even be prepared to handle the case |
| 71 // where calls to |GetNetworkTime| never once succeeds. | 51 // where calls to |GetNetworkTime| never once succeeds. |
| 72 bool GetNetworkTime(base::Time* network_time, | 52 bool GetNetworkTime(base::Time* network_time, |
| 73 base::TimeDelta* uncertainty) const; | 53 base::TimeDelta* uncertainty) const; |
| 74 | 54 |
| 75 // Calculates corresponding time ticks according to the given parameters. | 55 // Calculates corresponding time ticks according to the given parameters. |
| 76 // The provided |network_time| is precise at the given |resolution| and | 56 // The provided |network_time| is precise at the given |resolution| and |
| 77 // represent the time between now and up to |latency| + (now - |post_time|) | 57 // represent the time between now and up to |latency| + (now - |post_time|) |
| 78 // ago. | 58 // ago. |
| 79 void UpdateNetworkTime(base::Time network_time, | 59 void UpdateNetworkTime(base::Time network_time, |
| 80 base::TimeDelta resolution, | 60 base::TimeDelta resolution, |
| 81 base::TimeDelta latency, | 61 base::TimeDelta latency, |
| 82 base::TimeTicks post_time); | 62 base::TimeTicks post_time); |
| 83 | 63 |
| 84 void SetMaxResponseSizeForTesting(size_t limit); | |
| 85 | |
| 86 void SetPublicKeyForTesting(const base::StringPiece& key); | |
| 87 | |
| 88 void SetTimeServerURLForTesting(const GURL& url); | |
| 89 | |
| 90 bool QueryTimeServiceForTesting(); | |
| 91 | |
| 92 void WaitForFetchForTesting(uint32_t nonce); | |
| 93 | |
| 94 base::TimeDelta GetTimerDelayForTesting() const; | |
| 95 | |
| 96 private: | 64 private: |
| 97 // If synchronization has been lost, sends a query to the secure time service. | |
| 98 // Upon response, execution resumes in |OnURLFetchComplete|. | |
| 99 void QueryTimeService(); | |
| 100 | |
| 101 // Updates network time from a time server response, returning true | |
| 102 // if successful. | |
| 103 bool UpdateTimeFromResponse(); | |
| 104 | |
| 105 // net::URLFetcherDelegate: | |
| 106 // Called to process responses from the secure time service. | |
| 107 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
| 108 | |
| 109 // Sets the next time query to be run at the specified time. | |
| 110 void QueueTimeQuery(base::TimeDelta delay); | |
| 111 | |
| 112 // State variables for internally-managed secure time service queries. | |
| 113 GURL server_url_; | |
| 114 size_t max_response_size_; | |
| 115 base::RepeatingTimer query_timer_; | |
| 116 scoped_refptr<net::URLRequestContextGetter> getter_; | |
| 117 std::unique_ptr<net::URLFetcher> time_fetcher_; | |
| 118 base::TimeTicks fetch_started_; | |
| 119 std::unique_ptr<client_update_protocol::Ecdsa> query_signer_; | |
| 120 base::MessageLoop* loop_; // For testing; quit on fetch complete. | |
| 121 | |
| 122 // The |Clock| and |TickClock| are used to sanity-check one another, allowing | 65 // The |Clock| and |TickClock| are used to sanity-check one another, allowing |
| 123 // the NetworkTimeTracker to notice e.g. suspend/resume events and clock | 66 // the NetworkTimeTracker to notice e.g. suspend/resume events and clock |
| 124 // resets. | 67 // resets. |
| 125 std::unique_ptr<base::Clock> clock_; | 68 std::unique_ptr<base::Clock> clock_; |
| 126 std::unique_ptr<base::TickClock> tick_clock_; | 69 std::unique_ptr<base::TickClock> tick_clock_; |
| 127 | 70 |
| 128 PrefService* pref_service_; | 71 PrefService* pref_service_; |
| 129 | 72 |
| 130 // Network time based on last call to UpdateNetworkTime(). | 73 // Network time based on last call to UpdateNetworkTime(). |
| 131 mutable base::Time network_time_at_last_measurement_; | 74 mutable base::Time network_time_at_last_measurement_; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 145 base::ThreadChecker thread_checker_; | 88 base::ThreadChecker thread_checker_; |
| 146 | 89 |
| 147 bool received_network_time_; | 90 bool received_network_time_; |
| 148 | 91 |
| 149 DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker); | 92 DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker); |
| 150 }; | 93 }; |
| 151 | 94 |
| 152 } // namespace network_time | 95 } // namespace network_time |
| 153 | 96 |
| 154 #endif // COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ | 97 #endif // COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ |
| OLD | NEW |