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 <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/gtest_prod_util.h" | |
| 10 #include "base/macros.h" | 11 #include "base/macros.h" |
| 11 #include "base/threading/thread_checker.h" | 12 #include "base/threading/thread_checker.h" |
| 12 #include "base/time/clock.h" | 13 #include "base/time/clock.h" |
| 13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "base/timer/timer.h" | |
| 16 #include "net/url_request/url_fetcher_delegate.h" | |
| 14 | 17 |
| 15 class PrefRegistrySimple; | 18 class PrefRegistrySimple; |
| 16 class PrefService; | 19 class PrefService; |
| 17 | 20 |
| 18 namespace base { | 21 namespace base { |
| 19 class TickClock; | 22 class TickClock; |
| 20 } | 23 } // namespace base |
| 24 | |
| 25 namespace client_update_protocol { | |
| 26 class Ecdsa; | |
| 27 } // namespace client_udpate_protocol | |
| 28 | |
| 29 namespace net { | |
| 30 class URLFetcher; | |
| 31 class URLRequestContextGetter; | |
| 32 } // namespace net | |
| 21 | 33 |
| 22 namespace network_time { | 34 namespace network_time { |
| 23 | 35 |
| 24 // Clock resolution is platform dependent. | 36 // Clock resolution is platform dependent. |
| 25 #if defined(OS_WIN) | 37 #if defined(OS_WIN) |
| 26 const int64_t kTicksResolutionMs = base::Time::kMinLowResolutionThresholdMs; | 38 const int64_t kTicksResolutionMs = base::Time::kMinLowResolutionThresholdMs; |
| 27 #else | 39 #else |
| 28 const int64_t kTicksResolutionMs = 1; // Assume 1ms for non-windows platforms. | 40 const int64_t kTicksResolutionMs = 1; // Assume 1ms for non-windows platforms. |
| 29 #endif | 41 #endif |
| 30 | 42 |
| 31 // 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 |
| 32 // for a corresponding local time. This class is not thread safe. | 44 // for a corresponding local time. This class is not thread safe. |
| 33 class NetworkTimeTracker { | 45 class NetworkTimeTracker : public net::URLFetcherDelegate { |
| 34 public: | 46 public: |
| 35 static void RegisterPrefs(PrefRegistrySimple* registry); | 47 static void RegisterPrefs(PrefRegistrySimple* registry); |
| 36 | 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. | |
| 37 NetworkTimeTracker(std::unique_ptr<base::Clock> clock, | 52 NetworkTimeTracker(std::unique_ptr<base::Clock> clock, |
| 38 std::unique_ptr<base::TickClock> tick_clock, | 53 std::unique_ptr<base::TickClock> tick_clock, |
| 39 PrefService* pref_service); | 54 PrefService* pref_service, |
| 40 ~NetworkTimeTracker(); | 55 net::URLRequestContextGetter* getter = nullptr); |
| 56 ~NetworkTimeTracker() override; | |
| 41 | 57 |
| 42 // 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 |
| 43 // 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 |
| 44 // be set to an estimate of the error range. | 60 // be set to an estimate of the error range. |
| 45 // | 61 // |
| 46 // 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. |
| 47 // Failing that, a call to |UpdateNetworkTime| is required to make time | 63 // Failing that, a call to |UpdateNetworkTime| is required to make time |
| 48 // available to callers of |GetNetworkTime|. Subsequently, network time may | 64 // available to callers of |GetNetworkTime|. Subsequently, network time may |
| 49 // become unavailable if |NetworkTimeTracker| has reason to believe it is no | 65 // become unavailable if |NetworkTimeTracker| has reason to believe it is no |
| 50 // longer accurate. Consumers should even be prepared to handle the case | 66 // longer accurate. Consumers should even be prepared to handle the case |
| 51 // where calls to |GetNetworkTime| never once succeeds. | 67 // where calls to |GetNetworkTime| never once succeeds. |
| 52 bool GetNetworkTime(base::Time* network_time, | 68 bool GetNetworkTime(base::Time* network_time, |
| 53 base::TimeDelta* uncertainty) const; | 69 base::TimeDelta* uncertainty) const; |
| 54 | 70 |
| 55 // Calculates corresponding time ticks according to the given parameters. | 71 // Calculates corresponding time ticks according to the given parameters. |
| 56 // The provided |network_time| is precise at the given |resolution| and | 72 // The provided |network_time| is precise at the given |resolution| and |
| 57 // 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|) |
| 58 // ago. | 74 // ago. |
| 59 void UpdateNetworkTime(base::Time network_time, | 75 void UpdateNetworkTime(base::Time network_time, |
| 60 base::TimeDelta resolution, | 76 base::TimeDelta resolution, |
| 61 base::TimeDelta latency, | 77 base::TimeDelta latency, |
| 62 base::TimeTicks post_time); | 78 base::TimeTicks post_time); |
| 63 | 79 |
| 64 private: | 80 private: |
| 81 friend class NetworkTimeTrackerTest; | |
| 82 FRIEND_TEST_ALL_PREFIXES(NetworkTimeTrackerTest, UpdateFromNetwork); | |
| 83 FRIEND_TEST_ALL_PREFIXES(NetworkTimeTrackerTest, NoNetworkQueryWhileSynced); | |
| 84 FRIEND_TEST_ALL_PREFIXES(NetworkTimeTrackerTest, | |
| 85 UpdateFromNetworkBadSignature); | |
| 86 | |
| 87 // If syncrhonization has been lost, sends a query to the secure time service. | |
|
Nicolas Zea
2016/04/27 20:44:06
nit: synchronization
| |
| 88 // Upon response, execution resumes in |OnURLFetchComplete|. | |
| 89 void QueryTimeService(); | |
| 90 | |
| 91 // net::URLFetcherDelegate: | |
| 92 // Called to process responses from the secure time service. | |
| 93 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
| 94 | |
| 95 // State variables for internally-managed secure time service queries. | |
| 96 base::RepeatingTimer query_timer_; | |
| 97 net::URLRequestContextGetter* getter_; | |
| 98 std::unique_ptr<net::URLFetcher> time_fetcher_; | |
| 99 base::TimeTicks fetch_started_; | |
| 100 std::unique_ptr<client_update_protocol::Ecdsa> query_signer_; | |
| 101 | |
| 65 // The |Clock| and |TickClock| are used to sanity-check one another, allowing | 102 // The |Clock| and |TickClock| are used to sanity-check one another, allowing |
| 66 // the NetworkTimeTracker to notice e.g. suspend/resume events and clock | 103 // the NetworkTimeTracker to notice e.g. suspend/resume events and clock |
| 67 // resets. | 104 // resets. |
| 68 std::unique_ptr<base::Clock> clock_; | 105 std::unique_ptr<base::Clock> clock_; |
| 69 std::unique_ptr<base::TickClock> tick_clock_; | 106 std::unique_ptr<base::TickClock> tick_clock_; |
| 70 | 107 |
| 71 PrefService* pref_service_; | 108 PrefService* pref_service_; |
| 72 | 109 |
| 73 // Network time based on last call to UpdateNetworkTime(). | 110 // Network time based on last call to UpdateNetworkTime(). |
| 74 mutable base::Time network_time_at_last_measurement_; | 111 mutable base::Time network_time_at_last_measurement_; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 88 base::ThreadChecker thread_checker_; | 125 base::ThreadChecker thread_checker_; |
| 89 | 126 |
| 90 bool received_network_time_; | 127 bool received_network_time_; |
| 91 | 128 |
| 92 DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker); | 129 DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker); |
| 93 }; | 130 }; |
| 94 | 131 |
| 95 } // namespace network_time | 132 } // namespace network_time |
| 96 | 133 |
| 97 #endif // COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ | 134 #endif // COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ |
| OLD | NEW |