| 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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 if (data().length() + num_bytes > limit_) { | 150 if (data().length() + num_bytes > limit_) { |
| 151 return net::ERR_FILE_TOO_BIG; | 151 return net::ERR_FILE_TOO_BIG; |
| 152 } | 152 } |
| 153 return net::URLFetcherStringWriter::Write(buffer, num_bytes, callback); | 153 return net::URLFetcherStringWriter::Write(buffer, num_bytes, callback); |
| 154 } | 154 } |
| 155 | 155 |
| 156 private: | 156 private: |
| 157 size_t limit_; | 157 size_t limit_; |
| 158 }; | 158 }; |
| 159 | 159 |
| 160 bool BackgroundQueriesEnabled() { | |
| 161 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) { | |
| 162 return false; | |
| 163 } | |
| 164 | |
| 165 const std::string param = variations::GetVariationParamValueByFeature( | |
| 166 kNetworkTimeServiceQuerying, kVariationsServiceFetchBehavior); | |
| 167 return param == "background-only" || param == "background-and-on-demand"; | |
| 168 } | |
| 169 | |
| 170 bool OnDemandQueriesEnabled() { | |
| 171 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) { | |
| 172 return false; | |
| 173 } | |
| 174 | |
| 175 const std::string param = variations::GetVariationParamValueByFeature( | |
| 176 kNetworkTimeServiceQuerying, kVariationsServiceFetchBehavior); | |
| 177 return param == "on-demand-only" || param == "background-and-on-demand"; | |
| 178 } | |
| 179 | |
| 180 base::TimeDelta CheckTimeInterval() { | 160 base::TimeDelta CheckTimeInterval() { |
| 181 int64_t seconds; | 161 int64_t seconds; |
| 182 const std::string param = variations::GetVariationParamValueByFeature( | 162 const std::string param = variations::GetVariationParamValueByFeature( |
| 183 kNetworkTimeServiceQuerying, kVariationsServiceCheckTimeIntervalSeconds); | 163 kNetworkTimeServiceQuerying, kVariationsServiceCheckTimeIntervalSeconds); |
| 184 if (!param.empty() && base::StringToInt64(param, &seconds) && seconds > 0) { | 164 if (!param.empty() && base::StringToInt64(param, &seconds) && seconds > 0) { |
| 185 return base::TimeDelta::FromSeconds(seconds); | 165 return base::TimeDelta::FromSeconds(seconds); |
| 186 } | 166 } |
| 187 return base::TimeDelta::FromSeconds(kCheckTimeIntervalSeconds); | 167 return base::TimeDelta::FromSeconds(kCheckTimeIntervalSeconds); |
| 188 } | 168 } |
| 189 | 169 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 time_mapping.SetDouble(kPrefTime, time_at_last_measurement_.ToJsTime()); | 281 time_mapping.SetDouble(kPrefTime, time_at_last_measurement_.ToJsTime()); |
| 302 time_mapping.SetDouble(kPrefTicks, static_cast<double>( | 282 time_mapping.SetDouble(kPrefTicks, static_cast<double>( |
| 303 ticks_at_last_measurement_.ToInternalValue())); | 283 ticks_at_last_measurement_.ToInternalValue())); |
| 304 time_mapping.SetDouble(kPrefUncertainty, static_cast<double>( | 284 time_mapping.SetDouble(kPrefUncertainty, static_cast<double>( |
| 305 network_time_uncertainty_.ToInternalValue())); | 285 network_time_uncertainty_.ToInternalValue())); |
| 306 time_mapping.SetDouble(kPrefNetworkTime, | 286 time_mapping.SetDouble(kPrefNetworkTime, |
| 307 network_time_at_last_measurement_.ToJsTime()); | 287 network_time_at_last_measurement_.ToJsTime()); |
| 308 pref_service_->Set(prefs::kNetworkTimeMapping, time_mapping); | 288 pref_service_->Set(prefs::kNetworkTimeMapping, time_mapping); |
| 309 } | 289 } |
| 310 | 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 |
| 311 void NetworkTimeTracker::SetTimeServerURLForTesting(const GURL& url) { | 308 void NetworkTimeTracker::SetTimeServerURLForTesting(const GURL& url) { |
| 312 server_url_ = url; | 309 server_url_ = url; |
| 313 } | 310 } |
| 314 | 311 |
| 315 GURL NetworkTimeTracker::GetTimeServerURLForTesting() const { | 312 GURL NetworkTimeTracker::GetTimeServerURLForTesting() const { |
| 316 return server_url_; | 313 return server_url_; |
| 317 } | 314 } |
| 318 | 315 |
| 319 void NetworkTimeTracker::SetMaxResponseSizeForTesting(size_t limit) { | 316 void NetworkTimeTracker::SetMaxResponseSizeForTesting(size_t limit) { |
| 320 max_response_size_ = limit; | 317 max_response_size_ = limit; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 } | 402 } |
| 406 *network_time = network_time_at_last_measurement_ + tick_delta; | 403 *network_time = network_time_at_last_measurement_ + tick_delta; |
| 407 if (uncertainty) { | 404 if (uncertainty) { |
| 408 *uncertainty = network_time_uncertainty_ + divergence; | 405 *uncertainty = network_time_uncertainty_ + divergence; |
| 409 } | 406 } |
| 410 return NETWORK_TIME_AVAILABLE; | 407 return NETWORK_TIME_AVAILABLE; |
| 411 } | 408 } |
| 412 | 409 |
| 413 bool NetworkTimeTracker::StartTimeFetch(const base::Closure& closure) { | 410 bool NetworkTimeTracker::StartTimeFetch(const base::Closure& closure) { |
| 414 DCHECK(thread_checker_.CalledOnValidThread()); | 411 DCHECK(thread_checker_.CalledOnValidThread()); |
| 415 if (!OnDemandQueriesEnabled()) { | 412 FetchBehavior behavior = GetFetchBehavior(); |
| 413 if (behavior != FETCHES_ON_DEMAND_ONLY && |
| 414 behavior != FETCHES_IN_BACKGROUND_AND_ON_DEMAND) { |
| 416 return false; | 415 return false; |
| 417 } | 416 } |
| 418 | 417 |
| 419 // Enqueue the callback before calling CheckTime(), so that if | 418 // Enqueue the callback before calling CheckTime(), so that if |
| 420 // CheckTime() completes synchronously, the callback gets called. | 419 // CheckTime() completes synchronously, the callback gets called. |
| 421 fetch_completion_callbacks_.push_back(closure); | 420 fetch_completion_callbacks_.push_back(closure); |
| 422 | 421 |
| 423 // If a time query is already in progress, do not start another one. | 422 // If a time query is already in progress, do not start another one. |
| 424 if (time_fetcher_) { | 423 if (time_fetcher_) { |
| 425 return true; | 424 return true; |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 565 // callback. | 564 // callback. |
| 566 std::vector<base::Closure> callbacks = fetch_completion_callbacks_; | 565 std::vector<base::Closure> callbacks = fetch_completion_callbacks_; |
| 567 fetch_completion_callbacks_.clear(); | 566 fetch_completion_callbacks_.clear(); |
| 568 for (const auto& callback : callbacks) { | 567 for (const auto& callback : callbacks) { |
| 569 callback.Run(); | 568 callback.Run(); |
| 570 } | 569 } |
| 571 } | 570 } |
| 572 | 571 |
| 573 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) { | 572 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) { |
| 574 // Check if the user is opted in to background time fetches. | 573 // Check if the user is opted in to background time fetches. |
| 575 if (BackgroundQueriesEnabled()) { | 574 FetchBehavior behavior = GetFetchBehavior(); |
| 575 if (behavior == FETCHES_IN_BACKGROUND_ONLY || |
| 576 behavior == FETCHES_IN_BACKGROUND_AND_ON_DEMAND) { |
| 576 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime); | 577 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime); |
| 577 } | 578 } |
| 578 } | 579 } |
| 579 | 580 |
| 580 bool NetworkTimeTracker::ShouldIssueTimeQuery() { | 581 bool NetworkTimeTracker::ShouldIssueTimeQuery() { |
| 581 // Do not query the time service if not enabled via Variations Service. | 582 // Do not query the time service if not enabled via Variations Service. |
| 582 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) { | 583 if (!AreTimeFetchesEnabled()) { |
| 583 return false; | 584 return false; |
| 584 } | 585 } |
| 585 | 586 |
| 586 // If GetNetworkTime() does not return NETWORK_TIME_AVAILABLE, | 587 // If GetNetworkTime() does not return NETWORK_TIME_AVAILABLE, |
| 587 // synchronization has been lost and a query is needed. | 588 // synchronization has been lost and a query is needed. |
| 588 base::Time network_time; | 589 base::Time network_time; |
| 589 if (GetNetworkTime(&network_time, nullptr) != NETWORK_TIME_AVAILABLE) { | 590 if (GetNetworkTime(&network_time, nullptr) != NETWORK_TIME_AVAILABLE) { |
| 590 return true; | 591 return true; |
| 591 } | 592 } |
| 592 | 593 |
| 593 // Otherwise, make the decision at random. | 594 // Otherwise, make the decision at random. |
| 594 return base::RandDouble() < RandomQueryProbability(); | 595 return base::RandDouble() < RandomQueryProbability(); |
| 595 } | 596 } |
| 596 | 597 |
| 597 } // namespace network_time | 598 } // namespace network_time |
| OLD | NEW |