| 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> | 8 #include <stdint.h> |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/feature_list.h" | 11 #include "base/feature_list.h" |
| 12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
| 16 #include "base/time/clock.h" | 16 #include "base/time/clock.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "base/timer/timer.h" | 18 #include "base/timer/timer.h" |
| 19 #include "net/url_request/url_fetcher_delegate.h" | 19 #include "net/url_request/url_fetcher_delegate.h" |
| 20 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 21 | 21 |
| 22 class PrefRegistrySimple; | 22 class PrefRegistrySimple; |
| 23 class PrefService; | 23 class PrefService; |
| 24 | 24 |
| 25 namespace base { | 25 namespace base { |
| 26 class RunLoop; | |
| 27 class TickClock; | 26 class TickClock; |
| 28 } // namespace base | 27 } // namespace base |
| 29 | 28 |
| 30 namespace client_update_protocol { | 29 namespace client_update_protocol { |
| 31 class Ecdsa; | 30 class Ecdsa; |
| 32 } // namespace client_update_protocol | 31 } // namespace client_update_protocol |
| 33 | 32 |
| 34 namespace net { | 33 namespace net { |
| 35 class URLFetcher; | 34 class URLFetcher; |
| 36 class URLRequestContextGetter; | 35 class URLRequestContextGetter; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 // | 98 // |
| 100 // Network time may be available on startup if deserialized from a pref. | 99 // Network time may be available on startup if deserialized from a pref. |
| 101 // Failing that, a call to |UpdateNetworkTime| is required to make time | 100 // Failing that, a call to |UpdateNetworkTime| is required to make time |
| 102 // available to callers of |GetNetworkTime|. Subsequently, network time may | 101 // available to callers of |GetNetworkTime|. Subsequently, network time may |
| 103 // become unavailable if |NetworkTimeTracker| has reason to believe it is no | 102 // become unavailable if |NetworkTimeTracker| has reason to believe it is no |
| 104 // longer accurate. Consumers should even be prepared to handle the case | 103 // longer accurate. Consumers should even be prepared to handle the case |
| 105 // where calls to |GetNetworkTime| never once succeeds. | 104 // where calls to |GetNetworkTime| never once succeeds. |
| 106 NetworkTimeResult GetNetworkTime(base::Time* network_time, | 105 NetworkTimeResult GetNetworkTime(base::Time* network_time, |
| 107 base::TimeDelta* uncertainty) const; | 106 base::TimeDelta* uncertainty) const; |
| 108 | 107 |
| 108 // Starts a network time query if network time isn't already available |
| 109 // and if there isn't already a time query in progress. If a new query |
| 110 // is started or if there is one already in progress, |callback| will |
| 111 // run when the query completes. |
| 112 // |
| 113 // Returns true if a time query is started or was already in progress, |
| 114 // and false otherwise. For example, this method may return false if |
| 115 // time queries are disabled or if network time is already available. |
| 116 bool StartTimeFetch(const base::Closure& callback); |
| 117 |
| 109 // Calculates corresponding time ticks according to the given parameters. | 118 // Calculates corresponding time ticks according to the given parameters. |
| 110 // The provided |network_time| is precise at the given |resolution| and | 119 // The provided |network_time| is precise at the given |resolution| and |
| 111 // represent the time between now and up to |latency| + (now - |post_time|) | 120 // represent the time between now and up to |latency| + (now - |post_time|) |
| 112 // ago. | 121 // ago. |
| 113 void UpdateNetworkTime(base::Time network_time, | 122 void UpdateNetworkTime(base::Time network_time, |
| 114 base::TimeDelta resolution, | 123 base::TimeDelta resolution, |
| 115 base::TimeDelta latency, | 124 base::TimeDelta latency, |
| 116 base::TimeTicks post_time); | 125 base::TimeTicks post_time); |
| 117 | 126 |
| 118 void SetMaxResponseSizeForTesting(size_t limit); | 127 void SetMaxResponseSizeForTesting(size_t limit); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 base::TimeDelta backoff_; | 163 base::TimeDelta backoff_; |
| 155 // Timer that runs CheckTime(). All backoff and delay is implemented by | 164 // Timer that runs CheckTime(). All backoff and delay is implemented by |
| 156 // changing the delay of this timer, with the result that CheckTime() may | 165 // changing the delay of this timer, with the result that CheckTime() may |
| 157 // assume that if it runs, it is eligible to issue a time query. | 166 // assume that if it runs, it is eligible to issue a time query. |
| 158 base::RepeatingTimer timer_; | 167 base::RepeatingTimer timer_; |
| 159 scoped_refptr<net::URLRequestContextGetter> getter_; | 168 scoped_refptr<net::URLRequestContextGetter> getter_; |
| 160 std::unique_ptr<net::URLFetcher> time_fetcher_; | 169 std::unique_ptr<net::URLFetcher> time_fetcher_; |
| 161 base::TimeTicks fetch_started_; | 170 base::TimeTicks fetch_started_; |
| 162 std::unique_ptr<client_update_protocol::Ecdsa> query_signer_; | 171 std::unique_ptr<client_update_protocol::Ecdsa> query_signer_; |
| 163 | 172 |
| 164 // Run by WaitForFetchForTesting() and quit by OnURLFetchComplete(). | |
| 165 base::RunLoop* run_loop_for_testing_ = nullptr; | |
| 166 | |
| 167 // The |Clock| and |TickClock| are used to sanity-check one another, allowing | 173 // The |Clock| and |TickClock| are used to sanity-check one another, allowing |
| 168 // the NetworkTimeTracker to notice e.g. suspend/resume events and clock | 174 // the NetworkTimeTracker to notice e.g. suspend/resume events and clock |
| 169 // resets. | 175 // resets. |
| 170 std::unique_ptr<base::Clock> clock_; | 176 std::unique_ptr<base::Clock> clock_; |
| 171 std::unique_ptr<base::TickClock> tick_clock_; | 177 std::unique_ptr<base::TickClock> tick_clock_; |
| 172 | 178 |
| 173 PrefService* pref_service_; | 179 PrefService* pref_service_; |
| 174 | 180 |
| 175 // Network time based on last call to UpdateNetworkTime(). | 181 // Network time based on last call to UpdateNetworkTime(). |
| 176 mutable base::Time network_time_at_last_measurement_; | 182 mutable base::Time network_time_at_last_measurement_; |
| 177 | 183 |
| 178 // The estimated local times that correspond with |network_time_|. Assumes | 184 // The estimated local times that correspond with |network_time_|. Assumes |
| 179 // the actual network time measurement was performed midway through the | 185 // the actual network time measurement was performed midway through the |
| 180 // latency time. See UpdateNetworkTime(...) implementation for details. The | 186 // latency time. See UpdateNetworkTime(...) implementation for details. The |
| 181 // tick clock is the one actually used to return values to callers, but both | 187 // tick clock is the one actually used to return values to callers, but both |
| 182 // clocks must agree to within some tolerance. | 188 // clocks must agree to within some tolerance. |
| 183 base::Time time_at_last_measurement_; | 189 base::Time time_at_last_measurement_; |
| 184 base::TimeTicks ticks_at_last_measurement_; | 190 base::TimeTicks ticks_at_last_measurement_; |
| 185 | 191 |
| 186 // Uncertainty of |network_time_| based on added inaccuracies/resolution. See | 192 // Uncertainty of |network_time_| based on added inaccuracies/resolution. See |
| 187 // UpdateNetworkTime(...) implementation for details. | 193 // UpdateNetworkTime(...) implementation for details. |
| 188 base::TimeDelta network_time_uncertainty_; | 194 base::TimeDelta network_time_uncertainty_; |
| 189 | 195 |
| 190 // True if any time query has completed (but not necessarily succeeded) in | 196 // True if any time query has completed (but not necessarily succeeded) in |
| 191 // this NetworkTimeTracker's lifetime. | 197 // this NetworkTimeTracker's lifetime. |
| 192 bool time_query_completed_; | 198 bool time_query_completed_; |
| 193 | 199 |
| 200 // Callbacks to run when the in-progress time fetch completes. |
| 201 std::vector<base::Closure> fetch_completion_callbacks_; |
| 202 |
| 194 base::ThreadChecker thread_checker_; | 203 base::ThreadChecker thread_checker_; |
| 195 | 204 |
| 196 DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker); | 205 DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker); |
| 197 }; | 206 }; |
| 198 | 207 |
| 199 } // namespace network_time | 208 } // namespace network_time |
| 200 | 209 |
| 201 #endif // COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ | 210 #endif // COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ |
| OLD | NEW |