Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: components/network_time/network_time_tracker.h

Issue 1835823002: network_time_tracker: add temporary time protocol. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add test of signed bad data Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
12 #include "base/memory/ref_counted.h"
11 #include "base/threading/thread_checker.h" 13 #include "base/threading/thread_checker.h"
12 #include "base/time/clock.h" 14 #include "base/time/clock.h"
13 #include "base/time/time.h" 15 #include "base/time/time.h"
16 #include "base/timer/timer.h"
17 #include "net/url_request/url_fetcher_delegate.h"
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
82 void SetMaxResponseSizeForTesting(size_t limit);
83
84 void SetPublicKeyForTesting(base::StringPiece);
mmenke 2016/05/06 15:00:37 Looks to me like StringPieces are generally passed
mmenke 2016/05/06 15:00:37 nit: +key (argument name).
mab 2016/05/06 18:12:37 Done.
mab 2016/05/06 18:12:37 Done.
85
86 void SetTimeServerURLForTesting(const GURL& url);
87
88 bool QueryTimeServiceForTesting();
89
90 void WaitForFetchForTesting(uint32_t nonce);
mmenke 2016/05/06 15:00:37 include stdint.h
mab 2016/05/06 18:12:37 Done.
91
92 base::TimeDelta GetTimerDelayForTesting() const;
93
64 private: 94 private:
95 // If synchronization has been lost, sends a query to the secure time service.
96 // Upon response, execution resumes in |OnURLFetchComplete|.
97 void QueryTimeService();
98
99 // Updates network time from a time server response, returning true
100 // if successful.
101 bool UpdateTimeFromResponse();
102
103 // net::URLFetcherDelegate:
104 // Called to process responses from the secure time service.
105 void OnURLFetchComplete(const net::URLFetcher* source) override;
106
107 // Sets the next time query to be run at the specified time.
108 void QueueTimeQuery(base::TimeDelta delay);
109
110 // State variables for internally-managed secure time service queries.
111 GURL server_url_;
112 size_t max_response_size_;
113 base::RepeatingTimer query_timer_;
114 scoped_refptr<net::URLRequestContextGetter> getter_;
115 std::unique_ptr<net::URLFetcher> time_fetcher_;
116 base::TimeTicks fetch_started_;
117 std::unique_ptr<client_update_protocol::Ecdsa> query_signer_;
118
65 // The |Clock| and |TickClock| are used to sanity-check one another, allowing 119 // The |Clock| and |TickClock| are used to sanity-check one another, allowing
66 // the NetworkTimeTracker to notice e.g. suspend/resume events and clock 120 // the NetworkTimeTracker to notice e.g. suspend/resume events and clock
67 // resets. 121 // resets.
68 std::unique_ptr<base::Clock> clock_; 122 std::unique_ptr<base::Clock> clock_;
69 std::unique_ptr<base::TickClock> tick_clock_; 123 std::unique_ptr<base::TickClock> tick_clock_;
70 124
71 PrefService* pref_service_; 125 PrefService* pref_service_;
72 126
73 // Network time based on last call to UpdateNetworkTime(). 127 // Network time based on last call to UpdateNetworkTime().
74 mutable base::Time network_time_at_last_measurement_; 128 mutable base::Time network_time_at_last_measurement_;
(...skipping 13 matching lines...) Expand all
88 base::ThreadChecker thread_checker_; 142 base::ThreadChecker thread_checker_;
89 143
90 bool received_network_time_; 144 bool received_network_time_;
91 145
92 DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker); 146 DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker);
93 }; 147 };
94 148
95 } // namespace network_time 149 } // namespace network_time
96 150
97 #endif // COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ 151 #endif // COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698