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 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 time_mapping.SetDouble(kPrefTime, time_at_last_measurement_.ToJsTime()); | 281 time_mapping.SetDouble(kPrefTime, time_at_last_measurement_.ToJsTime()); |
282 time_mapping.SetDouble(kPrefTicks, static_cast<double>( | 282 time_mapping.SetDouble(kPrefTicks, static_cast<double>( |
283 ticks_at_last_measurement_.ToInternalValue())); | 283 ticks_at_last_measurement_.ToInternalValue())); |
284 time_mapping.SetDouble(kPrefUncertainty, static_cast<double>( | 284 time_mapping.SetDouble(kPrefUncertainty, static_cast<double>( |
285 network_time_uncertainty_.ToInternalValue())); | 285 network_time_uncertainty_.ToInternalValue())); |
286 time_mapping.SetDouble(kPrefNetworkTime, | 286 time_mapping.SetDouble(kPrefNetworkTime, |
287 network_time_at_last_measurement_.ToJsTime()); | 287 network_time_at_last_measurement_.ToJsTime()); |
288 pref_service_->Set(prefs::kNetworkTimeMapping, time_mapping); | 288 pref_service_->Set(prefs::kNetworkTimeMapping, time_mapping); |
289 } | 289 } |
290 | 290 |
| 291 bool NetworkTimeTracker::AreTimeFetchesEnabled() const { |
| 292 return base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying); |
| 293 } |
| 294 |
| 295 NetworkTimeTracker::FetchBehavior NetworkTimeTracker::GetFetchBehavior() const { |
| 296 const std::string param = variations::GetVariationParamValueByFeature( |
| 297 kNetworkTimeServiceQuerying, kVariationsServiceFetchBehavior); |
| 298 if (param == "background-only") { |
| 299 return FETCHES_IN_BACKGROUND_ONLY; |
| 300 } else if (param == "on-demand-only") { |
| 301 return FETCHES_ON_DEMAND_ONLY; |
| 302 } else if (param == "background-and-on-demand") { |
| 303 return FETCHES_IN_BACKGROUND_AND_ON_DEMAND; |
| 304 } |
| 305 return FETCH_BEHAVIOR_UNKNOWN; |
| 306 } |
| 307 |
291 void NetworkTimeTracker::SetTimeServerURLForTesting(const GURL& url) { | 308 void NetworkTimeTracker::SetTimeServerURLForTesting(const GURL& url) { |
292 server_url_ = url; | 309 server_url_ = url; |
293 } | 310 } |
294 | 311 |
295 GURL NetworkTimeTracker::GetTimeServerURLForTesting() const { | 312 GURL NetworkTimeTracker::GetTimeServerURLForTesting() const { |
296 return server_url_; | 313 return server_url_; |
297 } | 314 } |
298 | 315 |
299 void NetworkTimeTracker::SetMaxResponseSizeForTesting(size_t limit) { | 316 void NetworkTimeTracker::SetMaxResponseSizeForTesting(size_t limit) { |
300 max_response_size_ = limit; | 317 max_response_size_ = limit; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 *uncertainty = network_time_uncertainty_ + divergence; | 405 *uncertainty = network_time_uncertainty_ + divergence; |
389 } | 406 } |
390 return NETWORK_TIME_AVAILABLE; | 407 return NETWORK_TIME_AVAILABLE; |
391 } | 408 } |
392 | 409 |
393 bool NetworkTimeTracker::StartTimeFetch(const base::Closure& closure) { | 410 bool NetworkTimeTracker::StartTimeFetch(const base::Closure& closure) { |
394 DCHECK(thread_checker_.CalledOnValidThread()); | 411 DCHECK(thread_checker_.CalledOnValidThread()); |
395 // Check if the user is opted in to on-demand time fetches. | 412 // Check if the user is opted in to on-demand time fetches. |
396 const std::string param = variations::GetVariationParamValueByFeature( | 413 const std::string param = variations::GetVariationParamValueByFeature( |
397 kNetworkTimeServiceQuerying, kVariationsServiceFetchBehavior); | 414 kNetworkTimeServiceQuerying, kVariationsServiceFetchBehavior); |
398 if (param == "background-only") { | 415 if (!AreTimeFetchesEnabled() || |
| 416 GetFetchBehavior() == FETCHES_IN_BACKGROUND_ONLY) { |
399 return false; | 417 return false; |
400 } | 418 } |
401 | 419 |
402 // Enqueue the callback before calling CheckTime(), so that if | 420 // Enqueue the callback before calling CheckTime(), so that if |
403 // CheckTime() completes synchronously, the callback gets called. | 421 // CheckTime() completes synchronously, the callback gets called. |
404 fetch_completion_callbacks_.push_back(closure); | 422 fetch_completion_callbacks_.push_back(closure); |
405 | 423 |
406 // If a time query is already in progress, do not start another one. | 424 // If a time query is already in progress, do not start another one. |
407 if (time_fetcher_) { | 425 if (time_fetcher_) { |
408 return true; | 426 return true; |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
547 // because a callback could call StartTimeFetch() to enqueue another | 565 // because a callback could call StartTimeFetch() to enqueue another |
548 // callback. | 566 // callback. |
549 std::vector<base::Closure> callbacks = fetch_completion_callbacks_; | 567 std::vector<base::Closure> callbacks = fetch_completion_callbacks_; |
550 fetch_completion_callbacks_.clear(); | 568 fetch_completion_callbacks_.clear(); |
551 for (const auto& callback : callbacks) { | 569 for (const auto& callback : callbacks) { |
552 callback.Run(); | 570 callback.Run(); |
553 } | 571 } |
554 } | 572 } |
555 | 573 |
556 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) { | 574 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) { |
557 // Check if the user is opted in to background time fetches. | 575 if (AreTimeFetchesEnabled() && GetFetchBehavior() != FETCHES_ON_DEMAND_ONLY) { |
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); | 576 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime); |
563 } | 577 } |
564 } | 578 } |
565 | 579 |
566 bool NetworkTimeTracker::ShouldIssueTimeQuery() { | 580 bool NetworkTimeTracker::ShouldIssueTimeQuery() { |
567 // Do not query the time service if not enabled via Variations Service. | 581 // Do not query the time service if not enabled via Variations Service. |
568 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) { | 582 if (!AreTimeFetchesEnabled()) { |
569 return false; | 583 return false; |
570 } | 584 } |
571 | 585 |
572 // If GetNetworkTime() does not return NETWORK_TIME_AVAILABLE, | 586 // If GetNetworkTime() does not return NETWORK_TIME_AVAILABLE, |
573 // synchronization has been lost and a query is needed. | 587 // synchronization has been lost and a query is needed. |
574 base::Time network_time; | 588 base::Time network_time; |
575 if (GetNetworkTime(&network_time, nullptr) != NETWORK_TIME_AVAILABLE) { | 589 if (GetNetworkTime(&network_time, nullptr) != NETWORK_TIME_AVAILABLE) { |
576 return true; | 590 return true; |
577 } | 591 } |
578 | 592 |
579 // Otherwise, make the decision at random. | 593 // Otherwise, make the decision at random. |
580 return base::RandDouble() < RandomQueryProbability(); | 594 return base::RandDouble() < RandomQueryProbability(); |
581 } | 595 } |
582 | 596 |
583 } // namespace network_time | 597 } // namespace network_time |
OLD | NEW |