Chromium Code Reviews| 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"; |
| 324 UMA_HISTOGRAM_SPARSE_SLOWLY("NetworkTimeTracker.WallClockRanBackwards", | |
| 325 time_delta.InSeconds()); | |
| 323 network_time_at_last_measurement_ = base::Time(); | 326 network_time_at_last_measurement_ = base::Time(); |
| 324 return false; | 327 return NETWORK_TIME_SYNC_LOST; |
| 325 } | 328 } |
| 326 // Now we know that both |tick_delta| and |time_delta| are positive. | 329 // Now we know that both |tick_delta| and |time_delta| are positive. |
| 327 base::TimeDelta divergence = (tick_delta - time_delta).magnitude(); | 330 base::TimeDelta divergence = tick_delta - time_delta; |
| 328 if (divergence > base::TimeDelta::FromSeconds(kClockDivergenceSeconds)) { | 331 if (divergence.magnitude() > |
| 332 base::TimeDelta::FromSeconds(kClockDivergenceSeconds)) { | |
| 329 // Most likely either the machine has suspended, or the wall clock has been | 333 // Most likely either the machine has suspended, or the wall clock has been |
| 330 // reset. | 334 // reset. |
| 331 DVLOG(1) << "Discarding network time due to clocks diverging"; | 335 DVLOG(1) << "Discarding network time due to clocks diverging"; |
| 336 UMA_HISTOGRAM_SPARSE_SLOWLY("NetworkTimeTracker.ClockDivergence", | |
|
Steven Holte
2016/08/19 19:24:52
Using sparse histogram here seems a little surpris
| |
| 337 divergence.InSeconds()); | |
| 332 network_time_at_last_measurement_ = base::Time(); | 338 network_time_at_last_measurement_ = base::Time(); |
| 333 return false; | 339 return NETWORK_TIME_SYNC_LOST; |
| 334 } | 340 } |
| 335 *network_time = network_time_at_last_measurement_ + tick_delta; | 341 *network_time = network_time_at_last_measurement_ + tick_delta; |
| 336 if (uncertainty) { | 342 if (uncertainty) { |
| 337 *uncertainty = network_time_uncertainty_ + divergence; | 343 *uncertainty = network_time_uncertainty_ + divergence; |
| 338 } | 344 } |
| 339 return true; | 345 return NETWORK_TIME_AVAILABLE; |
| 340 } | 346 } |
| 341 | 347 |
| 342 void NetworkTimeTracker::CheckTime() { | 348 void NetworkTimeTracker::CheckTime() { |
| 343 DCHECK(thread_checker_.CalledOnValidThread()); | 349 DCHECK(thread_checker_.CalledOnValidThread()); |
| 344 | 350 |
| 345 // If NetworkTimeTracker is waking up after a backoff, this will reset the | 351 // If NetworkTimeTracker is waking up after a backoff, this will reset the |
| 346 // timer to its default faster frequency. | 352 // timer to its default faster frequency. |
| 347 QueueCheckTime(CheckTimeInterval()); | 353 QueueCheckTime(CheckTimeInterval()); |
| 348 | 354 |
| 349 if (!ShouldIssueTimeQuery()) { | 355 if (!ShouldIssueTimeQuery()) { |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 462 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) { | 468 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) { |
| 463 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime); | 469 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime); |
| 464 } | 470 } |
| 465 | 471 |
| 466 bool NetworkTimeTracker::ShouldIssueTimeQuery() { | 472 bool NetworkTimeTracker::ShouldIssueTimeQuery() { |
| 467 // Do not query the time service if not enabled via Variations Service. | 473 // Do not query the time service if not enabled via Variations Service. |
| 468 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) { | 474 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) { |
| 469 return false; | 475 return false; |
| 470 } | 476 } |
| 471 | 477 |
| 472 // If GetNetworkTime() returns false, synchronization has been lost | 478 // If GetNetworkTime() does not return NETWORK_TIME_AVAILABLE, |
| 473 // and a query is needed. | 479 // synchronization has been lost and a query is needed. |
| 474 base::Time network_time; | 480 base::Time network_time; |
| 475 if (!GetNetworkTime(&network_time, nullptr)) { | 481 if (GetNetworkTime(&network_time, nullptr) != NETWORK_TIME_AVAILABLE) { |
| 476 return true; | 482 return true; |
| 477 } | 483 } |
| 478 | 484 |
| 479 // Otherwise, make the decision at random. | 485 // Otherwise, make the decision at random. |
| 480 return base::RandDouble() < RandomQueryProbability(); | 486 return base::RandDouble() < RandomQueryProbability(); |
| 481 } | 487 } |
| 482 | 488 |
| 483 } // namespace network_time | 489 } // namespace network_time |
| OLD | NEW |