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_CUSTOM_TIMES( | |
| 325 "NetworkTimeTracker.WallClockRanBackwards", time_delta.magnitude(), | |
| 326 base::TimeDelta::FromSeconds(0), base::TimeDelta::FromDays(30), 50); | |
| 323 network_time_at_last_measurement_ = base::Time(); | 327 network_time_at_last_measurement_ = base::Time(); |
| 324 return false; | 328 return NETWORK_TIME_SYNC_LOST; |
| 325 } | 329 } |
| 326 // Now we know that both |tick_delta| and |time_delta| are positive. | 330 // Now we know that both |tick_delta| and |time_delta| are positive. |
| 327 base::TimeDelta divergence = (tick_delta - time_delta).magnitude(); | 331 base::TimeDelta divergence = tick_delta - time_delta; |
| 328 if (divergence > base::TimeDelta::FromSeconds(kClockDivergenceSeconds)) { | 332 if (divergence.magnitude() > |
| 333 base::TimeDelta::FromSeconds(kClockDivergenceSeconds)) { | |
| 329 // Most likely either the machine has suspended, or the wall clock has been | 334 // Most likely either the machine has suspended, or the wall clock has been |
| 330 // reset. | 335 // reset. |
| 331 DVLOG(1) << "Discarding network time due to clocks diverging"; | 336 DVLOG(1) << "Discarding network time due to clocks diverging"; |
| 337 if (divergence.InMilliseconds() < 0) { | |
| 338 UMA_HISTOGRAM_CUSTOM_TIMES( | |
| 339 "NetworkTimeTracker.ClockDivergence.Negative", divergence.magnitude(), | |
| 340 base::TimeDelta::FromSeconds(kClockDivergenceSeconds), | |
|
Steven Holte
2016/08/29 22:22:59
You may want to be a bit careful here, since even
estark
2016/08/29 22:37:05
Ah, thanks. I think I'll just change this hard-cod
| |
| 341 base::TimeDelta::FromDays(30), 50); | |
| 342 } else { | |
| 343 UMA_HISTOGRAM_CUSTOM_TIMES( | |
| 344 "NetworkTimeTracker.ClockDivergence.Positive", divergence.magnitude(), | |
| 345 base::TimeDelta::FromSeconds(kClockDivergenceSeconds), | |
| 346 base::TimeDelta::FromDays(30), 50); | |
| 347 } | |
| 332 network_time_at_last_measurement_ = base::Time(); | 348 network_time_at_last_measurement_ = base::Time(); |
| 333 return false; | 349 return NETWORK_TIME_SYNC_LOST; |
| 334 } | 350 } |
| 335 *network_time = network_time_at_last_measurement_ + tick_delta; | 351 *network_time = network_time_at_last_measurement_ + tick_delta; |
| 336 if (uncertainty) { | 352 if (uncertainty) { |
| 337 *uncertainty = network_time_uncertainty_ + divergence; | 353 *uncertainty = network_time_uncertainty_ + divergence; |
| 338 } | 354 } |
| 339 return true; | 355 return NETWORK_TIME_AVAILABLE; |
| 340 } | 356 } |
| 341 | 357 |
| 342 void NetworkTimeTracker::CheckTime() { | 358 void NetworkTimeTracker::CheckTime() { |
| 343 DCHECK(thread_checker_.CalledOnValidThread()); | 359 DCHECK(thread_checker_.CalledOnValidThread()); |
| 344 | 360 |
| 345 // If NetworkTimeTracker is waking up after a backoff, this will reset the | 361 // If NetworkTimeTracker is waking up after a backoff, this will reset the |
| 346 // timer to its default faster frequency. | 362 // timer to its default faster frequency. |
| 347 QueueCheckTime(CheckTimeInterval()); | 363 QueueCheckTime(CheckTimeInterval()); |
| 348 | 364 |
| 349 if (!ShouldIssueTimeQuery()) { | 365 if (!ShouldIssueTimeQuery()) { |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 462 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) { | 478 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) { |
| 463 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime); | 479 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime); |
| 464 } | 480 } |
| 465 | 481 |
| 466 bool NetworkTimeTracker::ShouldIssueTimeQuery() { | 482 bool NetworkTimeTracker::ShouldIssueTimeQuery() { |
| 467 // Do not query the time service if not enabled via Variations Service. | 483 // Do not query the time service if not enabled via Variations Service. |
| 468 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) { | 484 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) { |
| 469 return false; | 485 return false; |
| 470 } | 486 } |
| 471 | 487 |
| 472 // If GetNetworkTime() returns false, synchronization has been lost | 488 // If GetNetworkTime() does not return NETWORK_TIME_AVAILABLE, |
| 473 // and a query is needed. | 489 // synchronization has been lost and a query is needed. |
| 474 base::Time network_time; | 490 base::Time network_time; |
| 475 if (!GetNetworkTime(&network_time, nullptr)) { | 491 if (GetNetworkTime(&network_time, nullptr) != NETWORK_TIME_AVAILABLE) { |
| 476 return true; | 492 return true; |
| 477 } | 493 } |
| 478 | 494 |
| 479 // Otherwise, make the decision at random. | 495 // Otherwise, make the decision at random. |
| 480 return base::RandDouble() < RandomQueryProbability(); | 496 return base::RandDouble() < RandomQueryProbability(); |
| 481 } | 497 } |
| 482 | 498 |
| 483 } // namespace network_time | 499 } // namespace network_time |
| OLD | NEW |