Chromium Code Reviews| Index: chrome/browser/metrics/variations/variations_service.cc |
| diff --git a/chrome/browser/metrics/variations/variations_service.cc b/chrome/browser/metrics/variations/variations_service.cc |
| index 2b95bf6e4aa1b2b0389b7f241fe50b541692edfa..ba1305403cf65c0f931036b06e17eb8defa1171d 100644 |
| --- a/chrome/browser/metrics/variations/variations_service.cc |
| +++ b/chrome/browser/metrics/variations/variations_service.cc |
| @@ -11,7 +11,6 @@ |
| #include "base/command_line.h" |
| #include "base/memory/scoped_ptr.h" |
| #include "base/metrics/field_trial.h" |
| -#include "base/metrics/histogram.h" |
| #include "base/version.h" |
| #include "chrome/browser/browser_process.h" |
| #include "chrome/browser/metrics/proto/trials_seed.pb.h" |
| @@ -100,12 +99,19 @@ GURL GetVariationsServerURL() { |
| VariationsService::VariationsService() |
| : variations_server_url_(GetVariationsServerURL()), |
| create_trials_from_seed_called_(false), |
| - was_offline_during_last_request_attempt_(false) { |
| - net::NetworkChangeNotifier::AddConnectionTypeObserver(this); |
| + resource_request_allowed_notifier_( |
| + new ResourceRequestAllowedNotifier) { |
| + resource_request_allowed_notifier_->Init(this); |
| +} |
| + |
| +VariationsService::VariationsService(ResourceRequestAllowedNotifier* notifier) |
| + : variations_server_url_(GetVariationsServerURL()), |
| + create_trials_from_seed_called_(false), |
| + resource_request_allowed_notifier_(notifier) { |
| + resource_request_allowed_notifier_->Init(this); |
| } |
| VariationsService::~VariationsService() { |
| - net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
| } |
| bool VariationsService::CreateTrialsFromSeed(PrefService* local_prefs) { |
| @@ -154,15 +160,31 @@ void VariationsService::StartRepeatedVariationsSeedFetch() { |
| void VariationsService::FetchVariationsSeed() { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| - was_offline_during_last_request_attempt_ = |
| - net::NetworkChangeNotifier::IsOffline(); |
| - UMA_HISTOGRAM_BOOLEAN("Variations.NetworkAvailability", |
| - !was_offline_during_last_request_attempt_); |
| - if (was_offline_during_last_request_attempt_) { |
| - DVLOG(1) << "Network was offline."; |
| + if (!resource_request_allowed_notifier_->ResourceRequestsAllowed()) { |
| + DVLOG(1) << "Resource requests were not allowed. Waiting for notification."; |
| return; |
| } |
| + DoActualFetch(); |
| +} |
| + |
| +// static |
| +void VariationsService::RegisterPrefs(PrefService* prefs) { |
| + prefs->RegisterStringPref(prefs::kVariationsSeed, std::string()); |
| + prefs->RegisterInt64Pref(prefs::kVariationsSeedDate, |
| + base::Time().ToInternalValue()); |
| +} |
| + |
| +ResourceRequestAllowedNotifier* |
| +VariationsService::GetResourceRequestAllowedNotifierForTesting() { |
|
Alexei Svitkine (slow)
2012/09/20 21:39:03
Nit: Indent 4.
SteveT
2012/09/21 15:16:17
Removed.
|
| + return resource_request_allowed_notifier_.get(); |
| +} |
| + |
| +void VariationsService::SetCreateTrialsFromSeedCalledForTesting(bool called) { |
| + create_trials_from_seed_called_ = called; |
| +} |
| + |
| +void VariationsService::DoActualFetch() { |
| pending_seed_request_.reset(net::URLFetcher::Create( |
| variations_server_url_, net::URLFetcher::GET, this)); |
| pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| @@ -177,18 +199,6 @@ void VariationsService::FetchVariationsSeed() { |
| pending_seed_request_->Start(); |
| } |
| -void VariationsService::SetWasOfflineDuringLastRequestAttemptForTesting( |
| - bool offline) { |
| - was_offline_during_last_request_attempt_ = offline; |
| -} |
| - |
| -// static |
| -void VariationsService::RegisterPrefs(PrefService* prefs) { |
| - prefs->RegisterStringPref(prefs::kVariationsSeed, std::string()); |
| - prefs->RegisterInt64Pref(prefs::kVariationsSeedDate, |
| - base::Time().ToInternalValue()); |
| -} |
| - |
| void VariationsService::OnURLFetchComplete(const net::URLFetcher* source) { |
| DCHECK_EQ(pending_seed_request_.get(), source); |
| // The fetcher will be deleted when the request is handled. |
| @@ -216,24 +226,17 @@ void VariationsService::OnURLFetchComplete(const net::URLFetcher* source) { |
| StoreSeedData(seed_data, response_date, g_browser_process->local_state()); |
| } |
| -void VariationsService::OnConnectionTypeChanged( |
| - net::NetworkChangeNotifier::ConnectionType type) { |
| - // If the connection type is back online, start a request if the last request |
| - // failed due to being offline. |
| - if (was_offline_during_last_request_attempt_ && |
| - type != net::NetworkChangeNotifier::CONNECTION_NONE) { |
| - VLOG(1) << "Retrying fetch due to network reconnect."; |
| - FetchVariationsSeed(); |
| - |
| - // Since FetchVariationsSeed was explicitly called here, reset the timer to |
| - // avoid retrying for a full period. |
| - // net::NetworkChangeNotifier::IsOffline may be inconsistent with |type|, so |
| - // we check if FetchVariationsSeed set |
| - // |was_offline_during_last_request_attempt_| to true before we reset the |
| - // timer. |
| - if (!was_offline_during_last_request_attempt_ && timer_.IsRunning()) |
| - timer_.Reset(); |
| - } |
| +void VariationsService::OnResourceRequestsAllowed() { |
| + // Note that this only attempts to fetch the seed at most once per period |
| + // (kSeedFetchPeriodHours). This works because |
| + // |resource_request_allowed_notifier_| only calls this method if an |
| + // attempt was made earlier that fails (which implies that the period had |
| + // elapsed). After a successful attempt is made, the notifier will know not |
| + // to call this method again until another failed attempt occurs. |
| + DVLOG(1) << "Retrying fetch."; |
| + FetchVariationsSeed(); |
| + if (timer_.IsRunning()) |
| + timer_.Reset(); |
| } |
| bool VariationsService::StoreSeedData(const std::string& seed_data, |