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