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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 // be that badly wrong, but all the same it's included here to document the very | 93 // be that badly wrong, but all the same it's included here to document the very |
94 // rough nature of the time service provided by this class.) | 94 // rough nature of the time service provided by this class.) |
95 const uint32_t kTimeServerMaxSkewSeconds = 10; | 95 const uint32_t kTimeServerMaxSkewSeconds = 10; |
96 | 96 |
97 const char kTimeServiceURL[] = "http://clients2.google.com/time/1/current"; | 97 const char kTimeServiceURL[] = "http://clients2.google.com/time/1/current"; |
98 | 98 |
99 const char kVariationsServiceCheckTimeIntervalSeconds[] = | 99 const char kVariationsServiceCheckTimeIntervalSeconds[] = |
100 "CheckTimeIntervalSeconds"; | 100 "CheckTimeIntervalSeconds"; |
101 const char kVariationsServiceRandomQueryProbability[] = | 101 const char kVariationsServiceRandomQueryProbability[] = |
102 "RandomQueryProbability"; | 102 "RandomQueryProbability"; |
103 // This parameter must have the value "true" in order for | 103 |
104 // StartTimeFetch() to start time queries on demand. | 104 // This parameter can have three values: |
105 const char kVariationsServiceEnableFetchesOnDemand[] = "EnableFetchesOnDemand"; | 105 // |
106 // - "background-only": Time queries will be issued in the background as | |
107 // needed (when the clock loses sync), but on-demand time queries will | |
108 // not be issued (i.e. StartTimeFetch() will not start time queries.) | |
109 // | |
110 // - "on-demand-only": Time queries will not be issued except when | |
111 // StartTimeFetch() is called. | |
112 // | |
113 // - "background-and-on-demand": Time queries will be issued both in the | |
114 // background as needed and also on-demand. | |
115 const char kVariationsServiceFetchBehavior[] = "FetchBehavior"; | |
106 | 116 |
107 // This is an ECDSA prime256v1 named-curve key. | 117 // This is an ECDSA prime256v1 named-curve key. |
108 const int kKeyVersion = 1; | 118 const int kKeyVersion = 1; |
109 const uint8_t kKeyPubBytes[] = { | 119 const uint8_t kKeyPubBytes[] = { |
110 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, | 120 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, |
111 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, | 121 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, |
112 0x42, 0x00, 0x04, 0xeb, 0xd8, 0xad, 0x0b, 0x8f, 0x75, 0xe8, 0x84, 0x36, | 122 0x42, 0x00, 0x04, 0xeb, 0xd8, 0xad, 0x0b, 0x8f, 0x75, 0xe8, 0x84, 0x36, |
113 0x23, 0x48, 0x14, 0x24, 0xd3, 0x93, 0x42, 0x25, 0x43, 0xc1, 0xde, 0x36, | 123 0x23, 0x48, 0x14, 0x24, 0xd3, 0x93, 0x42, 0x25, 0x43, 0xc1, 0xde, 0x36, |
114 0x29, 0xc6, 0x95, 0xca, 0xeb, 0x28, 0x85, 0xff, 0x09, 0xdc, 0x08, 0xec, | 124 0x29, 0xc6, 0x95, 0xca, 0xeb, 0x28, 0x85, 0xff, 0x09, 0xdc, 0x08, 0xec, |
115 0x45, 0x74, 0x6e, 0x4b, 0xc3, 0xa5, 0xfd, 0x8a, 0x2f, 0x02, 0xa0, 0x4b, | 125 0x45, 0x74, 0x6e, 0x4b, 0xc3, 0xa5, 0xfd, 0x8a, 0x2f, 0x02, 0xa0, 0x4b, |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
295 return time_fetcher_ != nullptr; | 305 return time_fetcher_ != nullptr; |
296 } | 306 } |
297 | 307 |
298 void NetworkTimeTracker::WaitForFetchForTesting(uint32_t nonce) { | 308 void NetworkTimeTracker::WaitForFetchForTesting(uint32_t nonce) { |
299 query_signer_->OverrideNonceForTesting(kKeyVersion, nonce); | 309 query_signer_->OverrideNonceForTesting(kKeyVersion, nonce); |
300 base::RunLoop run_loop; | 310 base::RunLoop run_loop; |
301 fetch_completion_callbacks_.push_back(run_loop.QuitClosure()); | 311 fetch_completion_callbacks_.push_back(run_loop.QuitClosure()); |
302 run_loop.Run(); | 312 run_loop.Run(); |
303 } | 313 } |
304 | 314 |
315 void NetworkTimeTracker::OverrideNonceForTesting(uint32_t nonce) { | |
316 query_signer_->OverrideNonceForTesting(kKeyVersion, nonce); | |
317 } | |
318 | |
305 base::TimeDelta NetworkTimeTracker::GetTimerDelayForTesting() const { | 319 base::TimeDelta NetworkTimeTracker::GetTimerDelayForTesting() const { |
306 DCHECK(timer_.IsRunning()); | 320 DCHECK(timer_.IsRunning()); |
307 return timer_.GetCurrentDelay(); | 321 return timer_.GetCurrentDelay(); |
308 } | 322 } |
309 | 323 |
310 NetworkTimeTracker::NetworkTimeResult NetworkTimeTracker::GetNetworkTime( | 324 NetworkTimeTracker::NetworkTimeResult NetworkTimeTracker::GetNetworkTime( |
311 base::Time* network_time, | 325 base::Time* network_time, |
312 base::TimeDelta* uncertainty) const { | 326 base::TimeDelta* uncertainty) const { |
313 DCHECK(thread_checker_.CalledOnValidThread()); | 327 DCHECK(thread_checker_.CalledOnValidThread()); |
314 DCHECK(network_time); | 328 DCHECK(network_time); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
369 if (uncertainty) { | 383 if (uncertainty) { |
370 *uncertainty = network_time_uncertainty_ + divergence; | 384 *uncertainty = network_time_uncertainty_ + divergence; |
371 } | 385 } |
372 return NETWORK_TIME_AVAILABLE; | 386 return NETWORK_TIME_AVAILABLE; |
373 } | 387 } |
374 | 388 |
375 bool NetworkTimeTracker::StartTimeFetch(const base::Closure& closure) { | 389 bool NetworkTimeTracker::StartTimeFetch(const base::Closure& closure) { |
376 DCHECK(thread_checker_.CalledOnValidThread()); | 390 DCHECK(thread_checker_.CalledOnValidThread()); |
377 // Check if the user is opted in to on-demand time fetches. | 391 // Check if the user is opted in to on-demand time fetches. |
378 const std::string param = variations::GetVariationParamValueByFeature( | 392 const std::string param = variations::GetVariationParamValueByFeature( |
379 kNetworkTimeServiceQuerying, kVariationsServiceEnableFetchesOnDemand); | 393 kNetworkTimeServiceQuerying, kVariationsServiceFetchBehavior); |
380 if (param != "true") { | 394 if (param != "on-demand-only" && param != "background-and-on-demand") { |
381 return false; | 395 return false; |
382 } | 396 } |
383 | 397 |
384 // Enqueue the callback before calling CheckTime(), so that if | 398 // Enqueue the callback before calling CheckTime(), so that if |
385 // CheckTime() completes synchronously, the callback gets called. | 399 // CheckTime() completes synchronously, the callback gets called. |
386 fetch_completion_callbacks_.push_back(closure); | 400 fetch_completion_callbacks_.push_back(closure); |
387 | 401 |
388 // If a time query is already in progress, do not start another one. | 402 // If a time query is already in progress, do not start another one. |
389 if (time_fetcher_) { | 403 if (time_fetcher_) { |
390 return true; | 404 return true; |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
529 // because a callback could call StartTimeFetch() to enqueue another | 543 // because a callback could call StartTimeFetch() to enqueue another |
530 // callback. | 544 // callback. |
531 std::vector<base::Closure> callbacks = fetch_completion_callbacks_; | 545 std::vector<base::Closure> callbacks = fetch_completion_callbacks_; |
532 fetch_completion_callbacks_.clear(); | 546 fetch_completion_callbacks_.clear(); |
533 for (const auto& callback : callbacks) { | 547 for (const auto& callback : callbacks) { |
534 callback.Run(); | 548 callback.Run(); |
535 } | 549 } |
536 } | 550 } |
537 | 551 |
538 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) { | 552 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) { |
553 // Check if the user is opted in to background time fetches. | |
554 const std::string param = variations::GetVariationParamValueByFeature( | |
555 kNetworkTimeServiceQuerying, kVariationsServiceFetchBehavior); | |
556 if (param != "background-only" && param != "background-and-on-demand") { | |
557 return; | |
meacer
2016/11/01 22:20:18
nit: Maybe revert the condition? It's a bit more r
estark
2016/11/02 19:58:13
Done.
| |
558 } | |
559 | |
539 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime); | 560 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime); |
540 } | 561 } |
541 | 562 |
542 bool NetworkTimeTracker::ShouldIssueTimeQuery() { | 563 bool NetworkTimeTracker::ShouldIssueTimeQuery() { |
543 // Do not query the time service if not enabled via Variations Service. | 564 // Do not query the time service if not enabled via Variations Service. |
544 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) { | 565 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) { |
545 return false; | 566 return false; |
546 } | 567 } |
547 | 568 |
548 // If GetNetworkTime() does not return NETWORK_TIME_AVAILABLE, | 569 // If GetNetworkTime() does not return NETWORK_TIME_AVAILABLE, |
549 // synchronization has been lost and a query is needed. | 570 // synchronization has been lost and a query is needed. |
550 base::Time network_time; | 571 base::Time network_time; |
551 if (GetNetworkTime(&network_time, nullptr) != NETWORK_TIME_AVAILABLE) { | 572 if (GetNetworkTime(&network_time, nullptr) != NETWORK_TIME_AVAILABLE) { |
552 return true; | 573 return true; |
553 } | 574 } |
554 | 575 |
555 // Otherwise, make the decision at random. | 576 // Otherwise, make the decision at random. |
556 return base::RandDouble() < RandomQueryProbability(); | 577 return base::RandDouble() < RandomQueryProbability(); |
557 } | 578 } |
558 | 579 |
559 } // namespace network_time | 580 } // namespace network_time |
OLD | NEW |