Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(110)

Side by Side Diff: components/network_time/network_time_tracker.cc

Issue 2449193002: Attempt an on-demand time fetch when encountering a date invalid error (Closed)
Patch Set: meacer comments Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 network_time_uncertainty_.ToInternalValue())); 285 network_time_uncertainty_.ToInternalValue()));
276 time_mapping.SetDouble(kPrefNetworkTime, 286 time_mapping.SetDouble(kPrefNetworkTime,
277 network_time_at_last_measurement_.ToJsTime()); 287 network_time_at_last_measurement_.ToJsTime());
278 pref_service_->Set(prefs::kNetworkTimeMapping, time_mapping); 288 pref_service_->Set(prefs::kNetworkTimeMapping, time_mapping);
279 } 289 }
280 290
281 void NetworkTimeTracker::SetTimeServerURLForTesting(const GURL& url) { 291 void NetworkTimeTracker::SetTimeServerURLForTesting(const GURL& url) {
282 server_url_ = url; 292 server_url_ = url;
283 } 293 }
284 294
295 GURL NetworkTimeTracker::GetTimeServerURLForTesting() const {
296 return server_url_;
297 }
298
285 void NetworkTimeTracker::SetMaxResponseSizeForTesting(size_t limit) { 299 void NetworkTimeTracker::SetMaxResponseSizeForTesting(size_t limit) {
286 max_response_size_ = limit; 300 max_response_size_ = limit;
287 } 301 }
288 302
289 void NetworkTimeTracker::SetPublicKeyForTesting(const base::StringPiece& key) { 303 void NetworkTimeTracker::SetPublicKeyForTesting(const base::StringPiece& key) {
290 query_signer_ = client_update_protocol::Ecdsa::Create(kKeyVersion, key); 304 query_signer_ = client_update_protocol::Ecdsa::Create(kKeyVersion, key);
291 } 305 }
292 306
293 bool NetworkTimeTracker::QueryTimeServiceForTesting() { 307 bool NetworkTimeTracker::QueryTimeServiceForTesting() {
294 CheckTime(); 308 CheckTime();
295 return time_fetcher_ != nullptr; 309 return time_fetcher_ != nullptr;
296 } 310 }
297 311
298 void NetworkTimeTracker::WaitForFetchForTesting(uint32_t nonce) { 312 void NetworkTimeTracker::WaitForFetchForTesting(uint32_t nonce) {
299 query_signer_->OverrideNonceForTesting(kKeyVersion, nonce); 313 query_signer_->OverrideNonceForTesting(kKeyVersion, nonce);
300 base::RunLoop run_loop; 314 base::RunLoop run_loop;
301 fetch_completion_callbacks_.push_back(run_loop.QuitClosure()); 315 fetch_completion_callbacks_.push_back(run_loop.QuitClosure());
302 run_loop.Run(); 316 run_loop.Run();
303 } 317 }
304 318
319 void NetworkTimeTracker::OverrideNonceForTesting(uint32_t nonce) {
320 query_signer_->OverrideNonceForTesting(kKeyVersion, nonce);
321 }
322
305 base::TimeDelta NetworkTimeTracker::GetTimerDelayForTesting() const { 323 base::TimeDelta NetworkTimeTracker::GetTimerDelayForTesting() const {
306 DCHECK(timer_.IsRunning()); 324 DCHECK(timer_.IsRunning());
307 return timer_.GetCurrentDelay(); 325 return timer_.GetCurrentDelay();
308 } 326 }
309 327
310 NetworkTimeTracker::NetworkTimeResult NetworkTimeTracker::GetNetworkTime( 328 NetworkTimeTracker::NetworkTimeResult NetworkTimeTracker::GetNetworkTime(
311 base::Time* network_time, 329 base::Time* network_time,
312 base::TimeDelta* uncertainty) const { 330 base::TimeDelta* uncertainty) const {
313 DCHECK(thread_checker_.CalledOnValidThread()); 331 DCHECK(thread_checker_.CalledOnValidThread());
314 DCHECK(network_time); 332 DCHECK(network_time);
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 if (uncertainty) { 387 if (uncertainty) {
370 *uncertainty = network_time_uncertainty_ + divergence; 388 *uncertainty = network_time_uncertainty_ + divergence;
371 } 389 }
372 return NETWORK_TIME_AVAILABLE; 390 return NETWORK_TIME_AVAILABLE;
373 } 391 }
374 392
375 bool NetworkTimeTracker::StartTimeFetch(const base::Closure& closure) { 393 bool NetworkTimeTracker::StartTimeFetch(const base::Closure& closure) {
376 DCHECK(thread_checker_.CalledOnValidThread()); 394 DCHECK(thread_checker_.CalledOnValidThread());
377 // Check if the user is opted in to on-demand time fetches. 395 // Check if the user is opted in to on-demand time fetches.
378 const std::string param = variations::GetVariationParamValueByFeature( 396 const std::string param = variations::GetVariationParamValueByFeature(
379 kNetworkTimeServiceQuerying, kVariationsServiceEnableFetchesOnDemand); 397 kNetworkTimeServiceQuerying, kVariationsServiceFetchBehavior);
380 if (param != "true") { 398 if (param == "background-only") {
381 return false; 399 return false;
382 } 400 }
383 401
384 // Enqueue the callback before calling CheckTime(), so that if 402 // Enqueue the callback before calling CheckTime(), so that if
385 // CheckTime() completes synchronously, the callback gets called. 403 // CheckTime() completes synchronously, the callback gets called.
386 fetch_completion_callbacks_.push_back(closure); 404 fetch_completion_callbacks_.push_back(closure);
387 405
388 // If a time query is already in progress, do not start another one. 406 // If a time query is already in progress, do not start another one.
389 if (time_fetcher_) { 407 if (time_fetcher_) {
390 return true; 408 return true;
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
529 // because a callback could call StartTimeFetch() to enqueue another 547 // because a callback could call StartTimeFetch() to enqueue another
530 // callback. 548 // callback.
531 std::vector<base::Closure> callbacks = fetch_completion_callbacks_; 549 std::vector<base::Closure> callbacks = fetch_completion_callbacks_;
532 fetch_completion_callbacks_.clear(); 550 fetch_completion_callbacks_.clear();
533 for (const auto& callback : callbacks) { 551 for (const auto& callback : callbacks) {
534 callback.Run(); 552 callback.Run();
535 } 553 }
536 } 554 }
537 555
538 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) { 556 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) {
539 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime); 557 // Check if the user is opted in to background time fetches.
558 const std::string param = variations::GetVariationParamValueByFeature(
559 kNetworkTimeServiceQuerying, kVariationsServiceFetchBehavior);
560 if (base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying) &&
561 param != "on-demand-only") {
562 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime);
563 }
540 } 564 }
541 565
542 bool NetworkTimeTracker::ShouldIssueTimeQuery() { 566 bool NetworkTimeTracker::ShouldIssueTimeQuery() {
543 // Do not query the time service if not enabled via Variations Service. 567 // Do not query the time service if not enabled via Variations Service.
544 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) { 568 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) {
545 return false; 569 return false;
546 } 570 }
547 571
548 // If GetNetworkTime() does not return NETWORK_TIME_AVAILABLE, 572 // If GetNetworkTime() does not return NETWORK_TIME_AVAILABLE,
549 // synchronization has been lost and a query is needed. 573 // synchronization has been lost and a query is needed.
550 base::Time network_time; 574 base::Time network_time;
551 if (GetNetworkTime(&network_time, nullptr) != NETWORK_TIME_AVAILABLE) { 575 if (GetNetworkTime(&network_time, nullptr) != NETWORK_TIME_AVAILABLE) {
552 return true; 576 return true;
553 } 577 }
554 578
555 // Otherwise, make the decision at random. 579 // Otherwise, make the decision at random.
556 return base::RandDouble() < RandomQueryProbability(); 580 return base::RandDouble() < RandomQueryProbability();
557 } 581 }
558 582
559 } // namespace network_time 583 } // namespace network_time
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698