OLD | NEW |
---|---|
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 #include "components/network_time/network_time_tracker.h" | 5 #include "components/network_time/network_time_tracker.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
299 void NetworkTimeTracker::WaitForFetchForTesting(uint32_t nonce) { | 299 void NetworkTimeTracker::WaitForFetchForTesting(uint32_t nonce) { |
300 query_signer_->OverrideNonceForTesting(kKeyVersion, nonce); | 300 query_signer_->OverrideNonceForTesting(kKeyVersion, nonce); |
301 base::RunLoop().Run(); | 301 base::RunLoop().Run(); |
302 } | 302 } |
303 | 303 |
304 base::TimeDelta NetworkTimeTracker::GetTimerDelayForTesting() const { | 304 base::TimeDelta NetworkTimeTracker::GetTimerDelayForTesting() const { |
305 DCHECK(timer_.IsRunning()); | 305 DCHECK(timer_.IsRunning()); |
306 return timer_.GetCurrentDelay(); | 306 return timer_.GetCurrentDelay(); |
307 } | 307 } |
308 | 308 |
309 bool NetworkTimeTracker::GetNetworkTime(base::Time* network_time, | 309 NetworkTimeTracker::NetworkTimeResult NetworkTimeTracker::GetNetworkTime( |
310 base::TimeDelta* uncertainty) const { | 310 base::Time* network_time, |
311 base::TimeDelta* uncertainty) const { | |
311 DCHECK(thread_checker_.CalledOnValidThread()); | 312 DCHECK(thread_checker_.CalledOnValidThread()); |
312 DCHECK(network_time); | 313 DCHECK(network_time); |
313 if (network_time_at_last_measurement_.is_null()) { | 314 if (network_time_at_last_measurement_.is_null()) { |
314 return false; | 315 return NETWORK_TIME_NO_SYNC; |
315 } | 316 } |
316 DCHECK(!ticks_at_last_measurement_.is_null()); | 317 DCHECK(!ticks_at_last_measurement_.is_null()); |
317 DCHECK(!time_at_last_measurement_.is_null()); | 318 DCHECK(!time_at_last_measurement_.is_null()); |
318 base::TimeDelta tick_delta = | 319 base::TimeDelta tick_delta = |
319 tick_clock_->NowTicks() - ticks_at_last_measurement_; | 320 tick_clock_->NowTicks() - ticks_at_last_measurement_; |
320 base::TimeDelta time_delta = clock_->Now() - time_at_last_measurement_; | 321 base::TimeDelta time_delta = clock_->Now() - time_at_last_measurement_; |
321 if (time_delta.InMilliseconds() < 0) { // Has wall clock run backward? | 322 if (time_delta.InMilliseconds() < 0) { // Has wall clock run backward? |
322 DVLOG(1) << "Discarding network time due to wall clock running backward"; | 323 DVLOG(1) << "Discarding network time due to wall clock running backward"; |
323 network_time_at_last_measurement_ = base::Time(); | 324 network_time_at_last_measurement_ = base::Time(); |
324 return false; | 325 return NETWORK_TIME_SYNC_LOST; |
mab
2016/08/19 01:51:08
Do you not want to record the new histogram in thi
estark
2016/08/19 11:51:08
I think it would be clearest to have this be yet a
mab
2016/08/20 00:11:05
Your comment makes me wonder if it's worth disting
| |
325 } | 326 } |
326 // Now we know that both |tick_delta| and |time_delta| are positive. | 327 // Now we know that both |tick_delta| and |time_delta| are positive. |
327 base::TimeDelta divergence = (tick_delta - time_delta).magnitude(); | 328 base::TimeDelta divergence = tick_delta - time_delta; |
328 if (divergence > base::TimeDelta::FromSeconds(kClockDivergenceSeconds)) { | 329 if (divergence.magnitude() > |
330 base::TimeDelta::FromSeconds(kClockDivergenceSeconds)) { | |
329 // Most likely either the machine has suspended, or the wall clock has been | 331 // Most likely either the machine has suspended, or the wall clock has been |
330 // reset. | 332 // reset. |
331 DVLOG(1) << "Discarding network time due to clocks diverging"; | 333 DVLOG(1) << "Discarding network time due to clocks diverging"; |
334 UMA_HISTOGRAM_SPARSE_SLOWLY("NetworkTimeTracker.ClockDivergence", | |
335 divergence.InSeconds()); | |
mab
2016/08/19 01:51:07
Is this always positive? It would be nice to get
estark
2016/08/19 11:51:08
This should be signed. My bad for not writing a te
| |
332 network_time_at_last_measurement_ = base::Time(); | 336 network_time_at_last_measurement_ = base::Time(); |
333 return false; | 337 return NETWORK_TIME_SYNC_LOST; |
334 } | 338 } |
335 *network_time = network_time_at_last_measurement_ + tick_delta; | 339 *network_time = network_time_at_last_measurement_ + tick_delta; |
336 if (uncertainty) { | 340 if (uncertainty) { |
337 *uncertainty = network_time_uncertainty_ + divergence; | 341 *uncertainty = network_time_uncertainty_ + divergence; |
338 } | 342 } |
339 return true; | 343 return NETWORK_TIME_AVAILABLE; |
340 } | 344 } |
341 | 345 |
342 void NetworkTimeTracker::CheckTime() { | 346 void NetworkTimeTracker::CheckTime() { |
343 DCHECK(thread_checker_.CalledOnValidThread()); | 347 DCHECK(thread_checker_.CalledOnValidThread()); |
344 | 348 |
345 // If NetworkTimeTracker is waking up after a backoff, this will reset the | 349 // If NetworkTimeTracker is waking up after a backoff, this will reset the |
346 // timer to its default faster frequency. | 350 // timer to its default faster frequency. |
347 QueueCheckTime(CheckTimeInterval()); | 351 QueueCheckTime(CheckTimeInterval()); |
348 | 352 |
349 if (!ShouldIssueTimeQuery()) { | 353 if (!ShouldIssueTimeQuery()) { |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
462 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) { | 466 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) { |
463 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime); | 467 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime); |
464 } | 468 } |
465 | 469 |
466 bool NetworkTimeTracker::ShouldIssueTimeQuery() { | 470 bool NetworkTimeTracker::ShouldIssueTimeQuery() { |
467 // Do not query the time service if not enabled via Variations Service. | 471 // Do not query the time service if not enabled via Variations Service. |
468 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) { | 472 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) { |
469 return false; | 473 return false; |
470 } | 474 } |
471 | 475 |
472 // If GetNetworkTime() returns false, synchronization has been lost | 476 // If GetNetworkTime() does not return NETWORK_TIME_AVAILABLE, |
473 // and a query is needed. | 477 // synchronization has been lost and a query is needed. |
474 base::Time network_time; | 478 base::Time network_time; |
475 if (!GetNetworkTime(&network_time, nullptr)) { | 479 if (GetNetworkTime(&network_time, nullptr) != NETWORK_TIME_AVAILABLE) { |
476 return true; | 480 return true; |
477 } | 481 } |
478 | 482 |
479 // Otherwise, make the decision at random. | 483 // Otherwise, make the decision at random. |
480 return base::RandDouble() < RandomQueryProbability(); | 484 return base::RandDouble() < RandomQueryProbability(); |
481 } | 485 } |
482 | 486 |
483 } // namespace network_time | 487 } // namespace network_time |
OLD | NEW |