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 |
11 #include "base/feature_list.h" | 11 #include "base/feature_list.h" |
12 #include "base/i18n/time_formatting.h" | 12 #include "base/i18n/time_formatting.h" |
13 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/message_loop/message_loop.h" | |
16 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
17 #include "base/metrics/sparse_histogram.h" | 16 #include "base/metrics/sparse_histogram.h" |
18 #include "base/rand_util.h" | 17 #include "base/rand_util.h" |
19 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
20 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
21 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
22 #include "base/time/tick_clock.h" | 21 #include "base/time/tick_clock.h" |
23 #include "build/build_config.h" | 22 #include "build/build_config.h" |
24 #include "components/client_update_protocol/ecdsa.h" | 23 #include "components/client_update_protocol/ecdsa.h" |
25 #include "components/network_time/network_time_pref_names.h" | 24 #include "components/network_time/network_time_pref_names.h" |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 | 181 |
183 NetworkTimeTracker::NetworkTimeTracker( | 182 NetworkTimeTracker::NetworkTimeTracker( |
184 std::unique_ptr<base::Clock> clock, | 183 std::unique_ptr<base::Clock> clock, |
185 std::unique_ptr<base::TickClock> tick_clock, | 184 std::unique_ptr<base::TickClock> tick_clock, |
186 PrefService* pref_service, | 185 PrefService* pref_service, |
187 scoped_refptr<net::URLRequestContextGetter> getter) | 186 scoped_refptr<net::URLRequestContextGetter> getter) |
188 : server_url_(kTimeServiceURL), | 187 : server_url_(kTimeServiceURL), |
189 max_response_size_(1024), | 188 max_response_size_(1024), |
190 backoff_(base::TimeDelta::FromMinutes(kBackoffMinutes)), | 189 backoff_(base::TimeDelta::FromMinutes(kBackoffMinutes)), |
191 getter_(std::move(getter)), | 190 getter_(std::move(getter)), |
192 loop_(nullptr), | |
193 clock_(std::move(clock)), | 191 clock_(std::move(clock)), |
194 tick_clock_(std::move(tick_clock)), | 192 tick_clock_(std::move(tick_clock)), |
195 pref_service_(pref_service) { | 193 pref_service_(pref_service) { |
196 const base::DictionaryValue* time_mapping = | 194 const base::DictionaryValue* time_mapping = |
197 pref_service_->GetDictionary(prefs::kNetworkTimeMapping); | 195 pref_service_->GetDictionary(prefs::kNetworkTimeMapping); |
198 double time_js = 0; | 196 double time_js = 0; |
199 double ticks_js = 0; | 197 double ticks_js = 0; |
200 double network_time_js = 0; | 198 double network_time_js = 0; |
201 double uncertainty_js = 0; | 199 double uncertainty_js = 0; |
202 if (time_mapping->GetDouble(kPrefTime, &time_js) && | 200 if (time_mapping->GetDouble(kPrefTime, &time_js) && |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 void NetworkTimeTracker::SetMaxResponseSizeForTesting(size_t limit) { | 283 void NetworkTimeTracker::SetMaxResponseSizeForTesting(size_t limit) { |
286 max_response_size_ = limit; | 284 max_response_size_ = limit; |
287 } | 285 } |
288 | 286 |
289 void NetworkTimeTracker::SetPublicKeyForTesting(const base::StringPiece& key) { | 287 void NetworkTimeTracker::SetPublicKeyForTesting(const base::StringPiece& key) { |
290 query_signer_ = client_update_protocol::Ecdsa::Create(kKeyVersion, key); | 288 query_signer_ = client_update_protocol::Ecdsa::Create(kKeyVersion, key); |
291 } | 289 } |
292 | 290 |
293 bool NetworkTimeTracker::QueryTimeServiceForTesting() { | 291 bool NetworkTimeTracker::QueryTimeServiceForTesting() { |
294 CheckTime(); | 292 CheckTime(); |
295 loop_ = base::MessageLoop::current(); // Gets Quit on completion. | |
296 return time_fetcher_ != nullptr; | 293 return time_fetcher_ != nullptr; |
297 } | 294 } |
298 | 295 |
299 void NetworkTimeTracker::WaitForFetchForTesting(uint32_t nonce) { | 296 void NetworkTimeTracker::WaitForFetchForTesting(uint32_t nonce) { |
300 query_signer_->OverrideNonceForTesting(kKeyVersion, nonce); | 297 query_signer_->OverrideNonceForTesting(kKeyVersion, nonce); |
301 base::RunLoop().Run(); | 298 base::RunLoop run_loop; |
| 299 run_loop_for_testing_ = &run_loop; |
| 300 run_loop.Run(); |
| 301 run_loop_for_testing_ = nullptr; |
302 } | 302 } |
303 | 303 |
304 base::TimeDelta NetworkTimeTracker::GetTimerDelayForTesting() const { | 304 base::TimeDelta NetworkTimeTracker::GetTimerDelayForTesting() const { |
305 DCHECK(timer_.IsRunning()); | 305 DCHECK(timer_.IsRunning()); |
306 return timer_.GetCurrentDelay(); | 306 return timer_.GetCurrentDelay(); |
307 } | 307 } |
308 | 308 |
309 NetworkTimeTracker::NetworkTimeResult NetworkTimeTracker::GetNetworkTime( | 309 NetworkTimeTracker::NetworkTimeResult NetworkTimeTracker::GetNetworkTime( |
310 base::Time* network_time, | 310 base::Time* network_time, |
311 base::TimeDelta* uncertainty) const { | 311 base::TimeDelta* uncertainty) const { |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 // long time. | 465 // long time. |
466 if (!UpdateTimeFromResponse()) { // On error, back off. | 466 if (!UpdateTimeFromResponse()) { // On error, back off. |
467 if (backoff_ < base::TimeDelta::FromDays(2)) { | 467 if (backoff_ < base::TimeDelta::FromDays(2)) { |
468 backoff_ *= 2; | 468 backoff_ *= 2; |
469 } | 469 } |
470 } else { | 470 } else { |
471 backoff_ = base::TimeDelta::FromMinutes(kBackoffMinutes); | 471 backoff_ = base::TimeDelta::FromMinutes(kBackoffMinutes); |
472 } | 472 } |
473 QueueCheckTime(backoff_); | 473 QueueCheckTime(backoff_); |
474 time_fetcher_.reset(); | 474 time_fetcher_.reset(); |
475 if (loop_ != nullptr) { | 475 if (run_loop_for_testing_ != nullptr) |
476 loop_->QuitWhenIdle(); | 476 run_loop_for_testing_->QuitWhenIdle(); |
477 loop_ = nullptr; | |
478 } | |
479 } | 477 } |
480 | 478 |
481 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) { | 479 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) { |
482 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime); | 480 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime); |
483 } | 481 } |
484 | 482 |
485 bool NetworkTimeTracker::ShouldIssueTimeQuery() { | 483 bool NetworkTimeTracker::ShouldIssueTimeQuery() { |
486 // Do not query the time service if not enabled via Variations Service. | 484 // Do not query the time service if not enabled via Variations Service. |
487 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) { | 485 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) { |
488 return false; | 486 return false; |
489 } | 487 } |
490 | 488 |
491 // If GetNetworkTime() does not return NETWORK_TIME_AVAILABLE, | 489 // If GetNetworkTime() does not return NETWORK_TIME_AVAILABLE, |
492 // synchronization has been lost and a query is needed. | 490 // synchronization has been lost and a query is needed. |
493 base::Time network_time; | 491 base::Time network_time; |
494 if (GetNetworkTime(&network_time, nullptr) != NETWORK_TIME_AVAILABLE) { | 492 if (GetNetworkTime(&network_time, nullptr) != NETWORK_TIME_AVAILABLE) { |
495 return true; | 493 return true; |
496 } | 494 } |
497 | 495 |
498 // Otherwise, make the decision at random. | 496 // Otherwise, make the decision at random. |
499 return base::RandDouble() < RandomQueryProbability(); | 497 return base::RandDouble() < RandomQueryProbability(); |
500 } | 498 } |
501 | 499 |
502 } // namespace network_time | 500 } // namespace network_time |
OLD | NEW |