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

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: lint 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
« no previous file with comments | « components/network_time/DEPS ('k') | components/network_time/network_time_tracker.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <stdint.h>
8 #include <memory> 9 #include <memory>
9 10
11 #include "base/gtest_prod_util.h"
10 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
11 #include "base/threading/thread_checker.h" 14 #include "base/threading/thread_checker.h"
12 #include "base/time/clock.h" 15 #include "base/time/clock.h"
13 #include "base/time/time.h" 16 #include "base/time/time.h"
17 #include "base/timer/timer.h"
18 #include "net/url_request/url_fetcher_delegate.h"
19 #include "url/gurl.h"
14 20
15 class PrefRegistrySimple; 21 class PrefRegistrySimple;
16 class PrefService; 22 class PrefService;
17 23
18 namespace base { 24 namespace base {
25 class MessageLoop;
19 class TickClock; 26 class TickClock;
20 } 27 } // namespace base
28
29 namespace client_update_protocol {
30 class Ecdsa;
31 } // namespace client_update_protocol
32
33 namespace net {
34 class URLFetcher;
35 class URLRequestContextGetter;
36 } // namespace net
21 37
22 namespace network_time { 38 namespace network_time {
23 39
24 // Clock resolution is platform dependent. 40 // Clock resolution is platform dependent.
25 #if defined(OS_WIN) 41 #if defined(OS_WIN)
26 const int64_t kTicksResolutionMs = base::Time::kMinLowResolutionThresholdMs; 42 const int64_t kTicksResolutionMs = base::Time::kMinLowResolutionThresholdMs;
27 #else 43 #else
28 const int64_t kTicksResolutionMs = 1; // Assume 1ms for non-windows platforms. 44 const int64_t kTicksResolutionMs = 1; // Assume 1ms for non-windows platforms.
29 #endif 45 #endif
30 46
31 // A class that receives network time updates and can provide the network time 47 // 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. 48 // for a corresponding local time. This class is not thread safe.
33 class NetworkTimeTracker { 49 class NetworkTimeTracker : public net::URLFetcherDelegate {
34 public: 50 public:
35 static void RegisterPrefs(PrefRegistrySimple* registry); 51 static void RegisterPrefs(PrefRegistrySimple* registry);
36 52
53 // Constructor. Arguments may be stubbed out for tests. |getter|, if not
54 // null, will cause automatic queries to a time server. Otherwise, time is
55 // available only if |UpdateNetworkTime| is called.
37 NetworkTimeTracker(std::unique_ptr<base::Clock> clock, 56 NetworkTimeTracker(std::unique_ptr<base::Clock> clock,
38 std::unique_ptr<base::TickClock> tick_clock, 57 std::unique_ptr<base::TickClock> tick_clock,
39 PrefService* pref_service); 58 PrefService* pref_service,
40 ~NetworkTimeTracker(); 59 scoped_refptr<net::URLRequestContextGetter> getter);
60 ~NetworkTimeTracker() override;
41 61
42 // Sets |network_time| to an estimate of the true time. Returns true if time 62 // 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 63 // is available, and false otherwise. If |uncertainty| is non-NULL, it will
44 // be set to an estimate of the error range. 64 // be set to an estimate of the error range.
45 // 65 //
46 // Network time may be available on startup if deserialized from a pref. 66 // Network time may be available on startup if deserialized from a pref.
47 // Failing that, a call to |UpdateNetworkTime| is required to make time 67 // Failing that, a call to |UpdateNetworkTime| is required to make time
48 // available to callers of |GetNetworkTime|. Subsequently, network time may 68 // available to callers of |GetNetworkTime|. Subsequently, network time may
49 // become unavailable if |NetworkTimeTracker| has reason to believe it is no 69 // become unavailable if |NetworkTimeTracker| has reason to believe it is no
50 // longer accurate. Consumers should even be prepared to handle the case 70 // longer accurate. Consumers should even be prepared to handle the case
51 // where calls to |GetNetworkTime| never once succeeds. 71 // where calls to |GetNetworkTime| never once succeeds.
52 bool GetNetworkTime(base::Time* network_time, 72 bool GetNetworkTime(base::Time* network_time,
53 base::TimeDelta* uncertainty) const; 73 base::TimeDelta* uncertainty) const;
54 74
55 // Calculates corresponding time ticks according to the given parameters. 75 // Calculates corresponding time ticks according to the given parameters.
56 // The provided |network_time| is precise at the given |resolution| and 76 // The provided |network_time| is precise at the given |resolution| and
57 // represent the time between now and up to |latency| + (now - |post_time|) 77 // represent the time between now and up to |latency| + (now - |post_time|)
58 // ago. 78 // ago.
59 void UpdateNetworkTime(base::Time network_time, 79 void UpdateNetworkTime(base::Time network_time,
60 base::TimeDelta resolution, 80 base::TimeDelta resolution,
61 base::TimeDelta latency, 81 base::TimeDelta latency,
62 base::TimeTicks post_time); 82 base::TimeTicks post_time);
63 83
84 void SetMaxResponseSizeForTesting(size_t limit);
85
86 void SetPublicKeyForTesting(const base::StringPiece& key);
87
88 void SetTimeServerURLForTesting(const GURL& url);
89
90 bool QueryTimeServiceForTesting();
91
92 void WaitForFetchForTesting(uint32_t nonce);
93
94 base::TimeDelta GetTimerDelayForTesting() const;
95
64 private: 96 private:
97 // If synchronization has been lost, sends a query to the secure time service.
98 // Upon response, execution resumes in |OnURLFetchComplete|.
99 void QueryTimeService();
100
101 // Updates network time from a time server response, returning true
102 // if successful.
103 bool UpdateTimeFromResponse();
104
105 // net::URLFetcherDelegate:
106 // Called to process responses from the secure time service.
107 void OnURLFetchComplete(const net::URLFetcher* source) override;
108
109 // Sets the next time query to be run at the specified time.
110 void QueueTimeQuery(base::TimeDelta delay);
111
112 // State variables for internally-managed secure time service queries.
113 GURL server_url_;
114 size_t max_response_size_;
115 base::RepeatingTimer query_timer_;
116 scoped_refptr<net::URLRequestContextGetter> getter_;
117 std::unique_ptr<net::URLFetcher> time_fetcher_;
118 base::TimeTicks fetch_started_;
119 std::unique_ptr<client_update_protocol::Ecdsa> query_signer_;
120 base::MessageLoop* loop_; // For testing; quit on fetch complete.
121
65 // The |Clock| and |TickClock| are used to sanity-check one another, allowing 122 // The |Clock| and |TickClock| are used to sanity-check one another, allowing
66 // the NetworkTimeTracker to notice e.g. suspend/resume events and clock 123 // the NetworkTimeTracker to notice e.g. suspend/resume events and clock
67 // resets. 124 // resets.
68 std::unique_ptr<base::Clock> clock_; 125 std::unique_ptr<base::Clock> clock_;
69 std::unique_ptr<base::TickClock> tick_clock_; 126 std::unique_ptr<base::TickClock> tick_clock_;
70 127
71 PrefService* pref_service_; 128 PrefService* pref_service_;
72 129
73 // Network time based on last call to UpdateNetworkTime(). 130 // Network time based on last call to UpdateNetworkTime().
74 mutable base::Time network_time_at_last_measurement_; 131 mutable base::Time network_time_at_last_measurement_;
(...skipping 13 matching lines...) Expand all
88 base::ThreadChecker thread_checker_; 145 base::ThreadChecker thread_checker_;
89 146
90 bool received_network_time_; 147 bool received_network_time_;
91 148
92 DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker); 149 DISALLOW_COPY_AND_ASSIGN(NetworkTimeTracker);
93 }; 150 };
94 151
95 } // namespace network_time 152 } // namespace network_time
96 153
97 #endif // COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_ 154 #endif // COMPONENTS_NETWORK_TIME_NETWORK_TIME_TRACKER_H_
OLDNEW
« no previous file with comments | « components/network_time/DEPS ('k') | components/network_time/network_time_tracker.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698