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 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
298 run_loop_for_testing_ = &run_loop; | 298 run_loop_for_testing_ = &run_loop; |
299 run_loop.Run(); | 299 run_loop.Run(); |
300 run_loop_for_testing_ = nullptr; | 300 run_loop_for_testing_ = nullptr; |
301 } | 301 } |
302 | 302 |
303 base::TimeDelta NetworkTimeTracker::GetTimerDelayForTesting() const { | 303 base::TimeDelta NetworkTimeTracker::GetTimerDelayForTesting() const { |
304 DCHECK(timer_.IsRunning()); | 304 DCHECK(timer_.IsRunning()); |
305 return timer_.GetCurrentDelay(); | 305 return timer_.GetCurrentDelay(); |
306 } | 306 } |
307 | 307 |
308 void NetworkTimeTracker::EnableTimeQueriesForTesting() { | |
309 enable_time_queries_for_testing_ = true; | |
meacer
2016/10/17 23:45:48
If you want to keep the bool as is, I feel like fo
| |
310 } | |
311 | |
308 NetworkTimeTracker::NetworkTimeResult NetworkTimeTracker::GetNetworkTime( | 312 NetworkTimeTracker::NetworkTimeResult NetworkTimeTracker::GetNetworkTime( |
309 base::Time* network_time, | 313 base::Time* network_time, |
310 base::TimeDelta* uncertainty) const { | 314 base::TimeDelta* uncertainty) const { |
311 DCHECK(thread_checker_.CalledOnValidThread()); | 315 DCHECK(thread_checker_.CalledOnValidThread()); |
312 DCHECK(network_time); | 316 DCHECK(network_time); |
313 if (network_time_at_last_measurement_.is_null()) { | 317 if (network_time_at_last_measurement_.is_null()) { |
314 if (time_query_completed_) { | 318 if (time_query_completed_) { |
315 // Time query attempts have been made in the past and failed. | 319 // Time query attempts have been made in the past and failed. |
316 if (time_fetcher_) { | 320 if (time_fetcher_) { |
317 // A fetch (not the first attempt) is in progress. | 321 // A fetch (not the first attempt) is in progress. |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
493 if (run_loop_for_testing_ != nullptr) | 497 if (run_loop_for_testing_ != nullptr) |
494 run_loop_for_testing_->QuitWhenIdle(); | 498 run_loop_for_testing_->QuitWhenIdle(); |
495 } | 499 } |
496 | 500 |
497 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) { | 501 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) { |
498 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime); | 502 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime); |
499 } | 503 } |
500 | 504 |
501 bool NetworkTimeTracker::ShouldIssueTimeQuery() { | 505 bool NetworkTimeTracker::ShouldIssueTimeQuery() { |
502 // Do not query the time service if not enabled via Variations Service. | 506 // Do not query the time service if not enabled via Variations Service. |
503 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) { | 507 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying) && |
508 !enable_time_queries_for_testing_) { | |
meacer
2016/10/17 21:35:20
Instead of adding a bool, can the test force enabl
estark
2016/10/17 21:45:37
It's possible, but it's takes an annoying large am
meacer
2016/10/17 23:45:48
Unless you want to refactor that code, I think thi
estark
2016/10/18 03:58:37
I like that idea, done.
| |
504 return false; | 509 return false; |
505 } | 510 } |
506 | 511 |
507 // If GetNetworkTime() does not return NETWORK_TIME_AVAILABLE, | 512 // If GetNetworkTime() does not return NETWORK_TIME_AVAILABLE, |
508 // synchronization has been lost and a query is needed. | 513 // synchronization has been lost and a query is needed. |
509 base::Time network_time; | 514 base::Time network_time; |
510 if (GetNetworkTime(&network_time, nullptr) != NETWORK_TIME_AVAILABLE) { | 515 if (GetNetworkTime(&network_time, nullptr) != NETWORK_TIME_AVAILABLE) { |
511 return true; | 516 return true; |
512 } | 517 } |
513 | 518 |
514 // Otherwise, make the decision at random. | 519 // Otherwise, make the decision at random. |
515 return base::RandDouble() < RandomQueryProbability(); | 520 return base::RandDouble() < RandomQueryProbability(); |
516 } | 521 } |
517 | 522 |
518 } // namespace network_time | 523 } // namespace network_time |
OLD | NEW |