OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "net/base/network_time_notifier.h" | 5 #include "net/base/network_time_notifier.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/i18n/time_formatting.h" | 9 #include "base/i18n/time_formatting.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 | 26 |
27 } // namespace | 27 } // namespace |
28 | 28 |
29 namespace net { | 29 namespace net { |
30 | 30 |
31 NetworkTimeNotifier::NetworkTimeNotifier( | 31 NetworkTimeNotifier::NetworkTimeNotifier( |
32 scoped_ptr<base::TickClock> tick_clock) { | 32 scoped_ptr<base::TickClock> tick_clock) { |
33 tick_clock_ = tick_clock.Pass(); | 33 tick_clock_ = tick_clock.Pass(); |
34 } | 34 } |
35 | 35 |
36 NetworkTimeNotifier::~NetworkTimeNotifier() {} | 36 NetworkTimeNotifier::~NetworkTimeNotifier() { |
| 37 } |
37 | 38 |
38 void NetworkTimeNotifier::UpdateNetworkTime(const base::Time& network_time, | 39 void NetworkTimeNotifier::UpdateNetworkTime(const base::Time& network_time, |
39 const base::TimeDelta& resolution, | 40 const base::TimeDelta& resolution, |
40 const base::TimeDelta& latency, | 41 const base::TimeDelta& latency, |
41 const base::TimeTicks& post_time) { | 42 const base::TimeTicks& post_time) { |
42 DCHECK(thread_checker_.CalledOnValidThread()); | 43 DCHECK(thread_checker_.CalledOnValidThread()); |
43 DVLOG(1) << "Network time updating to " | 44 DVLOG(1) << "Network time updating to " |
44 << base::UTF16ToUTF8( | 45 << base::UTF16ToUTF8( |
45 base::TimeFormatFriendlyDateAndTime(network_time)); | 46 base::TimeFormatFriendlyDateAndTime(network_time)); |
46 // Update network time on every request to limit dependency on ticks lag. | 47 // Update network time on every request to limit dependency on ticks lag. |
47 // TODO(mad): Find a heuristic to avoid augmenting the | 48 // TODO(mad): Find a heuristic to avoid augmenting the |
48 // network_time_uncertainty_ too much by a particularly long latency. | 49 // network_time_uncertainty_ too much by a particularly long latency. |
49 // Maybe only update when the the new time either improves in accuracy or | 50 // Maybe only update when the the new time either improves in accuracy or |
50 // drifts too far from |network_time_|. | 51 // drifts too far from |network_time_|. |
51 network_time_ = network_time; | 52 network_time_ = network_time; |
52 | 53 |
53 // Calculate the delay since the network time was received. | 54 // Calculate the delay since the network time was received. |
54 base::TimeTicks now = tick_clock_->NowTicks(); | 55 base::TimeTicks now = tick_clock_->NowTicks(); |
55 base::TimeDelta task_delay = now - post_time; | 56 base::TimeDelta task_delay = now - post_time; |
56 // Estimate that the time was set midway through the latency time. | 57 // Estimate that the time was set midway through the latency time. |
57 network_time_ticks_ = now - task_delay - latency / 2; | 58 network_time_ticks_ = now - task_delay - latency / 2; |
58 | 59 |
59 // Can't assume a better time than the resolution of the given time | 60 // Can't assume a better time than the resolution of the given time |
60 // and 5 ticks measurements are involved, each with their own uncertainty. | 61 // and 5 ticks measurements are involved, each with their own uncertainty. |
61 // 1 & 2 are the ones used to compute the latency, 3 is the Now() from when | 62 // 1 & 2 are the ones used to compute the latency, 3 is the Now() from when |
62 // this task was posted, 4 is the Now() above and 5 will be the Now() used in | 63 // this task was posted, 4 is the Now() above and 5 will be the Now() used in |
63 // GetNetworkTime(). | 64 // GetNetworkTime(). |
64 network_time_uncertainty_ = | 65 network_time_uncertainty_ = |
65 resolution + latency + kNumTimeMeasurements * | 66 resolution + latency + |
66 base::TimeDelta::FromMilliseconds(kTicksResolutionMs); | 67 kNumTimeMeasurements * |
| 68 base::TimeDelta::FromMilliseconds(kTicksResolutionMs); |
67 | 69 |
68 for (size_t i = 0; i < observers_.size(); ++i) { | 70 for (size_t i = 0; i < observers_.size(); ++i) { |
69 base::MessageLoop::current()->PostTask( | 71 base::MessageLoop::current()->PostTask( |
70 FROM_HERE, | 72 FROM_HERE, |
71 base::Bind(observers_[i], | 73 base::Bind(observers_[i], |
72 network_time_, | 74 network_time_, |
73 network_time_ticks_, | 75 network_time_ticks_, |
74 network_time_uncertainty_)); | 76 network_time_uncertainty_)); |
75 } | 77 } |
76 } | 78 } |
77 | 79 |
78 void NetworkTimeNotifier::AddObserver( | 80 void NetworkTimeNotifier::AddObserver( |
79 const ObserverCallback& observer_callback) { | 81 const ObserverCallback& observer_callback) { |
80 DCHECK(thread_checker_.CalledOnValidThread()); | 82 DCHECK(thread_checker_.CalledOnValidThread()); |
81 observers_.push_back(observer_callback); | 83 observers_.push_back(observer_callback); |
82 if (!network_time_.is_null()) { | 84 if (!network_time_.is_null()) { |
83 base::MessageLoop::current()->PostTask( | 85 base::MessageLoop::current()->PostTask( |
84 FROM_HERE, | 86 FROM_HERE, |
85 base::Bind(observer_callback, | 87 base::Bind(observer_callback, |
86 network_time_, | 88 network_time_, |
87 network_time_ticks_, | 89 network_time_ticks_, |
88 network_time_uncertainty_)); | 90 network_time_uncertainty_)); |
89 } | 91 } |
90 } | 92 } |
91 | 93 |
92 } // namespace net | 94 } // namespace net |
OLD | NEW |