OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/network_time/network_time_tracker.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/i18n/time_formatting.h" | |
9 #include "base/logging.h" | |
10 #include "base/prefs/pref_registry_simple.h" | |
11 #include "base/prefs/pref_service.h" | |
12 #include "base/strings/utf_string_conversions.h" | |
13 #include "base/time/tick_clock.h" | |
14 #include "chrome/common/pref_names.h" | |
15 | |
16 namespace { | |
17 | |
18 // Clock resolution is platform dependent. | |
19 #if defined(OS_WIN) | |
20 const int64 kTicksResolutionMs = base::Time::kMinLowResolutionThresholdMs; | |
21 #else | |
22 const int64 kTicksResolutionMs = 1; // Assume 1ms for non-windows platforms. | |
23 #endif | |
24 | |
25 // Number of time measurements performed in a given network time calculation. | |
26 const int kNumTimeMeasurements = 5; | |
27 | |
28 } // namespace | |
29 | |
30 // static | |
31 void NetworkTimeTracker::RegisterPrefs(PrefRegistrySimple* registry) { | |
32 registry->RegisterDictionaryPref(prefs::kNetworkTimeMapping, | |
33 new base::DictionaryValue()); | |
34 } | |
35 | |
36 NetworkTimeTracker::NetworkTimeTracker(scoped_ptr<base::TickClock> tick_clock, | |
37 PrefService* pref_service) | |
38 : tick_clock_(tick_clock.Pass()), | |
39 pref_service_(pref_service), | |
40 received_network_time_(false) { | |
41 const base::DictionaryValue* time_mapping = | |
42 pref_service_->GetDictionary(prefs::kNetworkTimeMapping); | |
43 double local_time_js = 0; | |
44 double network_time_js = 0; | |
45 if (time_mapping->GetDouble("local", &local_time_js) && | |
46 time_mapping->GetDouble("network", &network_time_js)) { | |
47 base::Time local_time_saved = base::Time::FromJsTime(local_time_js); | |
48 base::Time local_time_now = base::Time::Now(); | |
49 if (local_time_saved > local_time_now || | |
50 local_time_now - local_time_saved > base::TimeDelta::FromDays(7)) { | |
51 // Drop saved mapping if clock skew has changed or the data is too old. | |
52 pref_service_->ClearPref(prefs::kNetworkTimeMapping); | |
53 } else { | |
54 network_time_ = base::Time::FromJsTime(network_time_js) + | |
55 (local_time_now - local_time_saved); | |
56 network_time_ticks_ = base::TimeTicks::Now(); | |
57 } | |
58 } | |
59 } | |
60 | |
61 NetworkTimeTracker::~NetworkTimeTracker() { | |
62 DCHECK(thread_checker_.CalledOnValidThread()); | |
63 } | |
64 | |
65 void NetworkTimeTracker::UpdateNetworkTime(base::Time network_time, | |
66 base::TimeDelta resolution, | |
67 base::TimeDelta latency, | |
68 base::TimeTicks post_time) { | |
69 DCHECK(thread_checker_.CalledOnValidThread()); | |
70 DVLOG(1) << "Network time updating to " | |
71 << base::UTF16ToUTF8( | |
72 base::TimeFormatFriendlyDateAndTime(network_time)); | |
73 // Update network time on every request to limit dependency on ticks lag. | |
74 // TODO(mad): Find a heuristic to avoid augmenting the | |
75 // network_time_uncertainty_ too much by a particularly long latency. | |
76 // Maybe only update when the the new time either improves in accuracy or | |
77 // drifts too far from |network_time_|. | |
78 network_time_ = network_time; | |
79 | |
80 // Calculate the delay since the network time was received. | |
81 base::TimeTicks now = tick_clock_->NowTicks(); | |
82 base::TimeDelta task_delay = now - post_time; | |
83 // Estimate that the time was set midway through the latency time. | |
84 network_time_ticks_ = now - task_delay - latency / 2; | |
85 | |
86 // Can't assume a better time than the resolution of the given time | |
87 // and 5 ticks measurements are involved, each with their own uncertainty. | |
88 // 1 & 2 are the ones used to compute the latency, 3 is the Now() from when | |
89 // this task was posted, 4 is the Now() above and 5 will be the Now() used in | |
90 // GetNetworkTime(). | |
91 network_time_uncertainty_ = | |
92 resolution + latency + kNumTimeMeasurements * | |
93 base::TimeDelta::FromMilliseconds(kTicksResolutionMs); | |
94 | |
95 received_network_time_ = true; | |
96 | |
97 base::Time network_time_now; | |
98 if (GetNetworkTime(base::TimeTicks::Now(), &network_time_now, NULL)) { | |
99 // Update time mapping if tracker received time update from server, i.e. | |
100 // mapping is accurate. | |
101 base::Time local_now = base::Time::Now(); | |
102 base::DictionaryValue time_mapping; | |
103 time_mapping.SetDouble("local", local_now.ToJsTime()); | |
104 time_mapping.SetDouble("network", network_time_now.ToJsTime()); | |
105 pref_service_->Set(prefs::kNetworkTimeMapping, time_mapping); | |
106 } | |
107 } | |
108 | |
109 bool NetworkTimeTracker::GetNetworkTime(base::TimeTicks time_ticks, | |
110 base::Time* network_time, | |
111 base::TimeDelta* uncertainty) const { | |
112 DCHECK(thread_checker_.CalledOnValidThread()); | |
113 DCHECK(network_time); | |
114 if (network_time_.is_null()) | |
115 return false; | |
116 DCHECK(!network_time_ticks_.is_null()); | |
117 *network_time = network_time_ + (time_ticks - network_time_ticks_); | |
118 if (uncertainty) | |
119 *uncertainty = network_time_uncertainty_; | |
120 return true; | |
121 } | |
OLD | NEW |