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