| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/network_time/network_time_tracker.h" | 5 #include "chrome/browser/network_time/network_time_tracker.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/i18n/time_formatting.h" | 8 #include "base/i18n/time_formatting.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/prefs/pref_registry_simple.h" |
| 11 #include "base/prefs/pref_service.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/time/tick_clock.h" | 13 #include "base/time/tick_clock.h" |
| 14 #include "chrome/common/pref_names.h" |
| 12 | 15 |
| 13 namespace { | 16 namespace { |
| 14 | 17 |
| 15 // Clock resolution is platform dependent. | 18 // Clock resolution is platform dependent. |
| 16 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
| 17 const int64 kTicksResolutionMs = base::Time::kMinLowResolutionThresholdMs; | 20 const int64 kTicksResolutionMs = base::Time::kMinLowResolutionThresholdMs; |
| 18 #else | 21 #else |
| 19 const int64 kTicksResolutionMs = 1; // Assume 1ms for non-windows platforms. | 22 const int64 kTicksResolutionMs = 1; // Assume 1ms for non-windows platforms. |
| 20 #endif | 23 #endif |
| 21 | 24 |
| 22 // Number of time measurements performed in a given network time calculation. | 25 // Number of time measurements performed in a given network time calculation. |
| 23 const int kNumTimeMeasurements = 5; | 26 const int kNumTimeMeasurements = 5; |
| 24 | 27 |
| 25 } // namespace | 28 } // namespace |
| 26 | 29 |
| 27 NetworkTimeTracker::TimeMapping::TimeMapping(base::Time local_time, | 30 // static |
| 28 base::Time network_time) | 31 void NetworkTimeTracker::RegisterPrefs(PrefRegistrySimple* registry) { |
| 29 : local_time(local_time), | 32 registry->RegisterDictionaryPref(prefs::kNetworkTimeMapping, |
| 30 network_time(network_time) {} | 33 new base::DictionaryValue()); |
| 34 } |
| 31 | 35 |
| 32 NetworkTimeTracker::NetworkTimeTracker(scoped_ptr<base::TickClock> tick_clock) | 36 NetworkTimeTracker::NetworkTimeTracker(scoped_ptr<base::TickClock> tick_clock, |
| 37 PrefService* pref_service) |
| 33 : tick_clock_(tick_clock.Pass()), | 38 : tick_clock_(tick_clock.Pass()), |
| 39 pref_service_(pref_service), |
| 34 received_network_time_(false) { | 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 } |
| 35 } | 59 } |
| 36 | 60 |
| 37 NetworkTimeTracker::~NetworkTimeTracker() { | 61 NetworkTimeTracker::~NetworkTimeTracker() { |
| 38 DCHECK(thread_checker_.CalledOnValidThread()); | 62 DCHECK(thread_checker_.CalledOnValidThread()); |
| 39 } | 63 } |
| 40 | 64 |
| 41 void NetworkTimeTracker::InitFromSavedTime(const TimeMapping& saved) { | |
| 42 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 43 if (!network_time_.is_null() || saved.local_time.is_null() || | |
| 44 saved.network_time.is_null()) | |
| 45 return; | |
| 46 | |
| 47 base::Time local_time_now = base::Time::Now(); | |
| 48 if (local_time_now < saved.local_time) { | |
| 49 DLOG(WARNING) << "Can't initialize because clock skew has changed."; | |
| 50 return; | |
| 51 } | |
| 52 | |
| 53 network_time_ = saved.network_time + (local_time_now - saved.local_time); | |
| 54 network_time_ticks_ = base::TimeTicks::Now(); | |
| 55 } | |
| 56 | |
| 57 void NetworkTimeTracker::UpdateNetworkTime(base::Time network_time, | 65 void NetworkTimeTracker::UpdateNetworkTime(base::Time network_time, |
| 58 base::TimeDelta resolution, | 66 base::TimeDelta resolution, |
| 59 base::TimeDelta latency, | 67 base::TimeDelta latency, |
| 60 base::TimeTicks post_time) { | 68 base::TimeTicks post_time) { |
| 61 DCHECK(thread_checker_.CalledOnValidThread()); | 69 DCHECK(thread_checker_.CalledOnValidThread()); |
| 62 DVLOG(1) << "Network time updating to " | 70 DVLOG(1) << "Network time updating to " |
| 63 << base::UTF16ToUTF8( | 71 << base::UTF16ToUTF8( |
| 64 base::TimeFormatFriendlyDateAndTime(network_time)); | 72 base::TimeFormatFriendlyDateAndTime(network_time)); |
| 65 // Update network time on every request to limit dependency on ticks lag. | 73 // Update network time on every request to limit dependency on ticks lag. |
| 66 // TODO(mad): Find a heuristic to avoid augmenting the | 74 // TODO(mad): Find a heuristic to avoid augmenting the |
| (...skipping 11 matching lines...) Expand all Loading... |
| 78 // Can't assume a better time than the resolution of the given time | 86 // Can't assume a better time than the resolution of the given time |
| 79 // and 5 ticks measurements are involved, each with their own uncertainty. | 87 // and 5 ticks measurements are involved, each with their own uncertainty. |
| 80 // 1 & 2 are the ones used to compute the latency, 3 is the Now() from when | 88 // 1 & 2 are the ones used to compute the latency, 3 is the Now() from when |
| 81 // this task was posted, 4 is the Now() above and 5 will be the Now() used in | 89 // this task was posted, 4 is the Now() above and 5 will be the Now() used in |
| 82 // GetNetworkTime(). | 90 // GetNetworkTime(). |
| 83 network_time_uncertainty_ = | 91 network_time_uncertainty_ = |
| 84 resolution + latency + kNumTimeMeasurements * | 92 resolution + latency + kNumTimeMeasurements * |
| 85 base::TimeDelta::FromMilliseconds(kTicksResolutionMs); | 93 base::TimeDelta::FromMilliseconds(kTicksResolutionMs); |
| 86 | 94 |
| 87 received_network_time_ = true; | 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 } |
| 88 } | 107 } |
| 89 | 108 |
| 90 bool NetworkTimeTracker::GetNetworkTime(base::TimeTicks time_ticks, | 109 bool NetworkTimeTracker::GetNetworkTime(base::TimeTicks time_ticks, |
| 91 base::Time* network_time, | 110 base::Time* network_time, |
| 92 base::TimeDelta* uncertainty) const { | 111 base::TimeDelta* uncertainty) const { |
| 93 DCHECK(thread_checker_.CalledOnValidThread()); | 112 DCHECK(thread_checker_.CalledOnValidThread()); |
| 94 DCHECK(network_time); | 113 DCHECK(network_time); |
| 95 if (network_time_.is_null()) | 114 if (network_time_.is_null()) |
| 96 return false; | 115 return false; |
| 97 DCHECK(!network_time_ticks_.is_null()); | 116 DCHECK(!network_time_ticks_.is_null()); |
| 98 *network_time = network_time_ + (time_ticks - network_time_ticks_); | 117 *network_time = network_time_ + (time_ticks - network_time_ticks_); |
| 99 if (uncertainty) | 118 if (uncertainty) |
| 100 *uncertainty = network_time_uncertainty_; | 119 *uncertainty = network_time_uncertainty_; |
| 101 return true; | 120 return true; |
| 102 } | 121 } |
| OLD | NEW |