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