| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/variations/service/variations_service.h" | 5 #include "components/variations/service/variations_service.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 |
| 9 #include <utility> | 10 #include <utility> |
| 10 | 11 |
| 11 #include "base/build_time.h" | 12 #include "base/build_time.h" |
| 12 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/memory/ptr_util.h" |
| 13 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 14 #include "base/metrics/sparse_histogram.h" | 16 #include "base/metrics/sparse_histogram.h" |
| 15 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 16 #include "base/sys_info.h" | 18 #include "base/sys_info.h" |
| 17 #include "base/task_runner_util.h" | 19 #include "base/task_runner_util.h" |
| 18 #include "base/timer/elapsed_timer.h" | 20 #include "base/timer/elapsed_timer.h" |
| 19 #include "base/values.h" | 21 #include "base/values.h" |
| 20 #include "base/version.h" | 22 #include "base/version.h" |
| 21 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 22 #include "components/data_use_measurement/core/data_use_user_data.h" | 24 #include "components/data_use_measurement/core/data_use_user_data.h" |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 << "requested x-bm,gzip but received gzip,x-bm"; | 264 << "requested x-bm,gzip but received gzip,x-bm"; |
| 263 return false; | 265 return false; |
| 264 } | 266 } |
| 265 | 267 |
| 266 return true; | 268 return true; |
| 267 } | 269 } |
| 268 | 270 |
| 269 } // namespace | 271 } // namespace |
| 270 | 272 |
| 271 VariationsService::VariationsService( | 273 VariationsService::VariationsService( |
| 272 scoped_ptr<VariationsServiceClient> client, | 274 std::unique_ptr<VariationsServiceClient> client, |
| 273 scoped_ptr<web_resource::ResourceRequestAllowedNotifier> notifier, | 275 std::unique_ptr<web_resource::ResourceRequestAllowedNotifier> notifier, |
| 274 PrefService* local_state, | 276 PrefService* local_state, |
| 275 metrics::MetricsStateManager* state_manager, | 277 metrics::MetricsStateManager* state_manager, |
| 276 const UIStringOverrider& ui_string_overrider) | 278 const UIStringOverrider& ui_string_overrider) |
| 277 : client_(std::move(client)), | 279 : client_(std::move(client)), |
| 278 ui_string_overrider_(ui_string_overrider), | 280 ui_string_overrider_(ui_string_overrider), |
| 279 local_state_(local_state), | 281 local_state_(local_state), |
| 280 state_manager_(state_manager), | 282 state_manager_(state_manager), |
| 281 policy_pref_service_(local_state), | 283 policy_pref_service_(local_state), |
| 282 seed_store_(local_state), | 284 seed_store_(local_state), |
| 283 create_trials_from_seed_called_(false), | 285 create_trials_from_seed_called_(false), |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 // static | 473 // static |
| 472 void VariationsService::RegisterProfilePrefs( | 474 void VariationsService::RegisterProfilePrefs( |
| 473 user_prefs::PrefRegistrySyncable* registry) { | 475 user_prefs::PrefRegistrySyncable* registry) { |
| 474 // This preference will only be written by the policy service, which will fill | 476 // This preference will only be written by the policy service, which will fill |
| 475 // it according to a value stored in the User Policy. | 477 // it according to a value stored in the User Policy. |
| 476 registry->RegisterStringPref(prefs::kVariationsRestrictParameter, | 478 registry->RegisterStringPref(prefs::kVariationsRestrictParameter, |
| 477 std::string()); | 479 std::string()); |
| 478 } | 480 } |
| 479 | 481 |
| 480 // static | 482 // static |
| 481 scoped_ptr<VariationsService> VariationsService::Create( | 483 std::unique_ptr<VariationsService> VariationsService::Create( |
| 482 scoped_ptr<VariationsServiceClient> client, | 484 std::unique_ptr<VariationsServiceClient> client, |
| 483 PrefService* local_state, | 485 PrefService* local_state, |
| 484 metrics::MetricsStateManager* state_manager, | 486 metrics::MetricsStateManager* state_manager, |
| 485 const char* disable_network_switch, | 487 const char* disable_network_switch, |
| 486 const UIStringOverrider& ui_string_overrider) { | 488 const UIStringOverrider& ui_string_overrider) { |
| 487 scoped_ptr<VariationsService> result; | 489 std::unique_ptr<VariationsService> result; |
| 488 #if !defined(GOOGLE_CHROME_BUILD) | 490 #if !defined(GOOGLE_CHROME_BUILD) |
| 489 // Unless the URL was provided, unsupported builds should return NULL to | 491 // Unless the URL was provided, unsupported builds should return NULL to |
| 490 // indicate that the service should not be used. | 492 // indicate that the service should not be used. |
| 491 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | 493 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 492 switches::kVariationsServerURL)) { | 494 switches::kVariationsServerURL)) { |
| 493 DVLOG(1) << "Not creating VariationsService in unofficial build without --" | 495 DVLOG(1) << "Not creating VariationsService in unofficial build without --" |
| 494 << switches::kVariationsServerURL << " specified."; | 496 << switches::kVariationsServerURL << " specified."; |
| 495 return result; | 497 return result; |
| 496 } | 498 } |
| 497 #endif | 499 #endif |
| 498 result.reset(new VariationsService( | 500 result.reset(new VariationsService( |
| 499 std::move(client), | 501 std::move(client), |
| 500 make_scoped_ptr(new web_resource::ResourceRequestAllowedNotifier( | 502 base::WrapUnique(new web_resource::ResourceRequestAllowedNotifier( |
| 501 local_state, disable_network_switch)), | 503 local_state, disable_network_switch)), |
| 502 local_state, state_manager, ui_string_overrider)); | 504 local_state, state_manager, ui_string_overrider)); |
| 503 return result; | 505 return result; |
| 504 } | 506 } |
| 505 | 507 |
| 506 // static | 508 // static |
| 507 scoped_ptr<VariationsService> VariationsService::CreateForTesting( | 509 std::unique_ptr<VariationsService> VariationsService::CreateForTesting( |
| 508 scoped_ptr<VariationsServiceClient> client, | 510 std::unique_ptr<VariationsServiceClient> client, |
| 509 PrefService* local_state) { | 511 PrefService* local_state) { |
| 510 return make_scoped_ptr(new VariationsService( | 512 return base::WrapUnique(new VariationsService( |
| 511 std::move(client), | 513 std::move(client), |
| 512 make_scoped_ptr(new web_resource::ResourceRequestAllowedNotifier( | 514 base::WrapUnique(new web_resource::ResourceRequestAllowedNotifier( |
| 513 local_state, nullptr)), | 515 local_state, nullptr)), |
| 514 local_state, nullptr, UIStringOverrider())); | 516 local_state, nullptr, UIStringOverrider())); |
| 515 } | 517 } |
| 516 | 518 |
| 517 void VariationsService::DoActualFetch() { | 519 void VariationsService::DoActualFetch() { |
| 518 DCHECK(thread_checker_.CalledOnValidThread()); | 520 DCHECK(thread_checker_.CalledOnValidThread()); |
| 519 DCHECK(!pending_seed_request_); | 521 DCHECK(!pending_seed_request_); |
| 520 | 522 |
| 521 pending_seed_request_ = net::URLFetcher::Create(0, variations_server_url_, | 523 pending_seed_request_ = net::URLFetcher::Create(0, variations_server_url_, |
| 522 net::URLFetcher::GET, this); | 524 net::URLFetcher::GET, this); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 564 } | 566 } |
| 565 | 567 |
| 566 bool VariationsService::StoreSeed(const std::string& seed_data, | 568 bool VariationsService::StoreSeed(const std::string& seed_data, |
| 567 const std::string& seed_signature, | 569 const std::string& seed_signature, |
| 568 const std::string& country_code, | 570 const std::string& country_code, |
| 569 const base::Time& date_fetched, | 571 const base::Time& date_fetched, |
| 570 bool is_delta_compressed, | 572 bool is_delta_compressed, |
| 571 bool is_gzip_compressed) { | 573 bool is_gzip_compressed) { |
| 572 DCHECK(thread_checker_.CalledOnValidThread()); | 574 DCHECK(thread_checker_.CalledOnValidThread()); |
| 573 | 575 |
| 574 scoped_ptr<variations::VariationsSeed> seed(new variations::VariationsSeed); | 576 std::unique_ptr<variations::VariationsSeed> seed( |
| 577 new variations::VariationsSeed); |
| 575 if (!seed_store_.StoreSeedData(seed_data, seed_signature, country_code, | 578 if (!seed_store_.StoreSeedData(seed_data, seed_signature, country_code, |
| 576 date_fetched, is_delta_compressed, | 579 date_fetched, is_delta_compressed, |
| 577 is_gzip_compressed, seed.get())) { | 580 is_gzip_compressed, seed.get())) { |
| 578 return false; | 581 return false; |
| 579 } | 582 } |
| 580 RecordLastFetchTime(); | 583 RecordLastFetchTime(); |
| 581 | 584 |
| 582 // Perform seed simulation only if |state_manager_| is not-NULL. The state | 585 // Perform seed simulation only if |state_manager_| is not-NULL. The state |
| 583 // manager may be NULL for some unit tests. | 586 // manager may be NULL for some unit tests. |
| 584 if (!state_manager_) | 587 if (!state_manager_) |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 } | 627 } |
| 625 | 628 |
| 626 void VariationsService::OnURLFetchComplete(const net::URLFetcher* source) { | 629 void VariationsService::OnURLFetchComplete(const net::URLFetcher* source) { |
| 627 DCHECK(thread_checker_.CalledOnValidThread()); | 630 DCHECK(thread_checker_.CalledOnValidThread()); |
| 628 DCHECK_EQ(pending_seed_request_.get(), source); | 631 DCHECK_EQ(pending_seed_request_.get(), source); |
| 629 | 632 |
| 630 const bool is_first_request = !initial_request_completed_; | 633 const bool is_first_request = !initial_request_completed_; |
| 631 initial_request_completed_ = true; | 634 initial_request_completed_ = true; |
| 632 | 635 |
| 633 // The fetcher will be deleted when the request is handled. | 636 // The fetcher will be deleted when the request is handled. |
| 634 scoped_ptr<const net::URLFetcher> request(pending_seed_request_.release()); | 637 std::unique_ptr<const net::URLFetcher> request( |
| 638 pending_seed_request_.release()); |
| 635 const net::URLRequestStatus& request_status = request->GetStatus(); | 639 const net::URLRequestStatus& request_status = request->GetStatus(); |
| 636 if (request_status.status() != net::URLRequestStatus::SUCCESS) { | 640 if (request_status.status() != net::URLRequestStatus::SUCCESS) { |
| 637 UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.FailedRequestErrorCode", | 641 UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.FailedRequestErrorCode", |
| 638 -request_status.error()); | 642 -request_status.error()); |
| 639 DVLOG(1) << "Variations server request failed with error: " | 643 DVLOG(1) << "Variations server request failed with error: " |
| 640 << request_status.error() << ": " | 644 << request_status.error() << ": " |
| 641 << net::ErrorToString(request_status.error()); | 645 << net::ErrorToString(request_status.error()); |
| 642 // It's common for the very first fetch attempt to fail (e.g. the network | 646 // It's common for the very first fetch attempt to fail (e.g. the network |
| 643 // may not yet be available). In such a case, try again soon, rather than | 647 // may not yet be available). In such a case, try again soon, rather than |
| 644 // waiting the full time interval. | 648 // waiting the full time interval. |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 719 RecordRequestsAllowedHistogram(RESOURCE_REQUESTS_ALLOWED_NOTIFIED); | 723 RecordRequestsAllowedHistogram(RESOURCE_REQUESTS_ALLOWED_NOTIFIED); |
| 720 DVLOG(1) << "Retrying fetch."; | 724 DVLOG(1) << "Retrying fetch."; |
| 721 DoActualFetch(); | 725 DoActualFetch(); |
| 722 | 726 |
| 723 // This service must have created a scheduler in order for this to be called. | 727 // This service must have created a scheduler in order for this to be called. |
| 724 DCHECK(request_scheduler_.get()); | 728 DCHECK(request_scheduler_.get()); |
| 725 request_scheduler_->Reset(); | 729 request_scheduler_->Reset(); |
| 726 } | 730 } |
| 727 | 731 |
| 728 void VariationsService::PerformSimulationWithVersion( | 732 void VariationsService::PerformSimulationWithVersion( |
| 729 scoped_ptr<variations::VariationsSeed> seed, | 733 std::unique_ptr<variations::VariationsSeed> seed, |
| 730 const base::Version& version) { | 734 const base::Version& version) { |
| 731 DCHECK(thread_checker_.CalledOnValidThread()); | 735 DCHECK(thread_checker_.CalledOnValidThread()); |
| 732 | 736 |
| 733 if (!version.IsValid()) | 737 if (!version.IsValid()) |
| 734 return; | 738 return; |
| 735 | 739 |
| 736 const base::ElapsedTimer timer; | 740 const base::ElapsedTimer timer; |
| 737 | 741 |
| 738 scoped_ptr<const base::FieldTrial::EntropyProvider> entropy_provider = | 742 std::unique_ptr<const base::FieldTrial::EntropyProvider> entropy_provider = |
| 739 state_manager_->CreateEntropyProvider(); | 743 state_manager_->CreateEntropyProvider(); |
| 740 variations::VariationsSeedSimulator seed_simulator(*entropy_provider); | 744 variations::VariationsSeedSimulator seed_simulator(*entropy_provider); |
| 741 | 745 |
| 742 const std::string latest_country = | 746 const std::string latest_country = |
| 743 local_state_->GetString(prefs::kVariationsCountry); | 747 local_state_->GetString(prefs::kVariationsCountry); |
| 744 const variations::VariationsSeedSimulator::Result result = | 748 const variations::VariationsSeedSimulator::Result result = |
| 745 seed_simulator.SimulateSeedStudies( | 749 seed_simulator.SimulateSeedStudies( |
| 746 *seed, client_->GetApplicationLocale(), | 750 *seed, client_->GetApplicationLocale(), |
| 747 GetReferenceDateForExpiryChecks(local_state_), version, | 751 GetReferenceDateForExpiryChecks(local_state_), version, |
| 748 GetChannelForVariations(client_->GetChannel()), | 752 GetChannelForVariations(client_->GetChannel()), |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 882 | 886 |
| 883 if (got_stored_country && stored_country == country_override) | 887 if (got_stored_country && stored_country == country_override) |
| 884 return false; | 888 return false; |
| 885 | 889 |
| 886 base::Version version(version_info::GetVersionNumber()); | 890 base::Version version(version_info::GetVersionNumber()); |
| 887 StorePermanentCountry(version, country_override); | 891 StorePermanentCountry(version, country_override); |
| 888 return true; | 892 return true; |
| 889 } | 893 } |
| 890 | 894 |
| 891 } // namespace variations | 895 } // namespace variations |
| OLD | NEW |