Chromium Code Reviews| 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 "chrome/browser/metrics/variations/variations_service.h" | 5 #include "chrome/browser/metrics/variations/variations_service.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/build_time.h" | 10 #include "base/build_time.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/metrics/field_trial.h" | 13 #include "base/metrics/field_trial.h" |
| 14 #include "base/metrics/histogram.h" | |
| 15 #include "base/version.h" | 14 #include "base/version.h" |
| 16 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/metrics/proto/trials_seed.pb.h" | 16 #include "chrome/browser/metrics/proto/trials_seed.pb.h" |
| 18 #include "chrome/browser/prefs/pref_service.h" | 17 #include "chrome/browser/prefs/pref_service.h" |
| 19 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 20 #include "chrome/common/metrics/variations/variations_util.h" | 19 #include "chrome/common/metrics/variations/variations_util.h" |
| 21 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 22 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 23 #include "content/public/common/url_fetcher.h" | 22 #include "content/public/common/url_fetcher.h" |
| 24 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 server_url = kDefaultVariationsServerURL; | 91 server_url = kDefaultVariationsServerURL; |
| 93 GURL url_as_gurl = GURL(server_url); | 92 GURL url_as_gurl = GURL(server_url); |
| 94 DCHECK(url_as_gurl.is_valid()); | 93 DCHECK(url_as_gurl.is_valid()); |
| 95 return url_as_gurl; | 94 return url_as_gurl; |
| 96 } | 95 } |
| 97 | 96 |
| 98 } // namespace | 97 } // namespace |
| 99 | 98 |
| 100 VariationsService::VariationsService() | 99 VariationsService::VariationsService() |
| 101 : variations_server_url_(GetVariationsServerURL()), | 100 : variations_server_url_(GetVariationsServerURL()), |
| 102 create_trials_from_seed_called_(false), | 101 create_trials_from_seed_called_(false) { |
| 103 was_offline_during_last_request_attempt_(false) { | 102 resource_request_allowed_notifier_.AddObserver(this); |
| 104 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); | |
| 105 } | 103 } |
| 106 | 104 |
| 107 VariationsService::~VariationsService() { | 105 VariationsService::~VariationsService() { |
| 108 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | 106 resource_request_allowed_notifier_.RemoveObserver(this); |
| 109 } | 107 } |
| 110 | 108 |
| 111 bool VariationsService::CreateTrialsFromSeed(PrefService* local_prefs) { | 109 bool VariationsService::CreateTrialsFromSeed(PrefService* local_prefs) { |
| 112 create_trials_from_seed_called_ = true; | 110 create_trials_from_seed_called_ = true; |
| 113 | 111 |
| 114 TrialsSeed seed; | 112 TrialsSeed seed; |
| 115 if (!LoadTrialsSeedFromPref(local_prefs, &seed)) | 113 if (!LoadTrialsSeedFromPref(local_prefs, &seed)) |
| 116 return false; | 114 return false; |
| 117 | 115 |
| 118 const int64 date_value = local_prefs->GetInt64(prefs::kVariationsSeedDate); | 116 const int64 date_value = local_prefs->GetInt64(prefs::kVariationsSeedDate); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 147 FetchVariationsSeed(); | 145 FetchVariationsSeed(); |
| 148 | 146 |
| 149 // Repeat this periodically. | 147 // Repeat this periodically. |
| 150 timer_.Start(FROM_HERE, base::TimeDelta::FromHours(kSeedFetchPeriodHours), | 148 timer_.Start(FROM_HERE, base::TimeDelta::FromHours(kSeedFetchPeriodHours), |
| 151 this, &VariationsService::FetchVariationsSeed); | 149 this, &VariationsService::FetchVariationsSeed); |
| 152 } | 150 } |
| 153 | 151 |
| 154 void VariationsService::FetchVariationsSeed() { | 152 void VariationsService::FetchVariationsSeed() { |
| 155 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 153 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 156 | 154 |
| 157 was_offline_during_last_request_attempt_ = | 155 if (!resource_request_allowed_notifier_.ResourceRequestsAllowed()) { |
| 158 net::NetworkChangeNotifier::IsOffline(); | 156 DVLOG(1) << "Resource requests were not allowed. Waiting for notification."; |
| 159 UMA_HISTOGRAM_BOOLEAN("Variations.NetworkAvailability", | |
| 160 !was_offline_during_last_request_attempt_); | |
| 161 if (was_offline_during_last_request_attempt_) { | |
| 162 DVLOG(1) << "Network was offline."; | |
| 163 return; | 157 return; |
| 164 } | 158 } |
| 165 | 159 |
| 166 pending_seed_request_.reset(net::URLFetcher::Create( | 160 pending_seed_request_.reset(net::URLFetcher::Create( |
| 167 variations_server_url_, net::URLFetcher::GET, this)); | 161 variations_server_url_, net::URLFetcher::GET, this)); |
| 168 pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 162 pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 169 net::LOAD_DO_NOT_SAVE_COOKIES); | 163 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 170 pending_seed_request_->SetRequestContext( | 164 pending_seed_request_->SetRequestContext( |
| 171 g_browser_process->system_request_context()); | 165 g_browser_process->system_request_context()); |
| 172 pending_seed_request_->SetMaxRetries(kMaxRetrySeedFetch); | 166 pending_seed_request_->SetMaxRetries(kMaxRetrySeedFetch); |
| 173 if (!variations_serial_number_.empty()) { | 167 if (!variations_serial_number_.empty()) { |
| 174 pending_seed_request_->AddExtraRequestHeader("If-Match:" + | 168 pending_seed_request_->AddExtraRequestHeader("If-Match:" + |
| 175 variations_serial_number_); | 169 variations_serial_number_); |
| 176 } | 170 } |
| 177 pending_seed_request_->Start(); | 171 pending_seed_request_->Start(); |
| 178 } | 172 } |
| 179 | 173 |
| 180 void VariationsService::SetWasOfflineDuringLastRequestAttemptForTesting( | 174 void VariationsService::SetWasOfflineDuringLastRequestAttemptForTesting( |
|
Alexei Svitkine (slow)
2012/09/12 15:15:46
I don't see your tests calling this anymore.
SteveT
2012/09/14 20:25:09
Removed.
| |
| 181 bool offline) { | 175 bool offline) { |
| 182 was_offline_during_last_request_attempt_ = offline; | 176 resource_request_allowed_notifier_. |
| 177 SetWasOfflineDuringLastRequestAttemptForTesting(offline); | |
| 178 } | |
| 179 | |
| 180 void VariationsService::OnResourceRequestsAllowed() { | |
|
Alexei Svitkine (slow)
2012/09/12 15:15:46
Does this guarantee that we won't fetch more than
SteveT
2012/09/14 20:25:09
Done here, and already done in the header of the R
| |
| 181 VLOG(1) << "Retrying fetch."; | |
|
Ilya Sherman
2012/09/11 22:51:27
nit: DVLOG?
SteveT
2012/09/14 20:25:09
Done.
| |
| 182 FetchVariationsSeed(); | |
| 183 if (timer_.IsRunning()) | |
| 184 timer_.Reset(); | |
| 183 } | 185 } |
| 184 | 186 |
| 185 // static | 187 // static |
| 186 void VariationsService::RegisterPrefs(PrefService* prefs) { | 188 void VariationsService::RegisterPrefs(PrefService* prefs) { |
| 187 prefs->RegisterStringPref(prefs::kVariationsSeed, std::string()); | 189 prefs->RegisterStringPref(prefs::kVariationsSeed, std::string()); |
| 188 prefs->RegisterInt64Pref(prefs::kVariationsSeedDate, | 190 prefs->RegisterInt64Pref(prefs::kVariationsSeedDate, |
| 189 base::Time().ToInternalValue()); | 191 base::Time().ToInternalValue()); |
| 190 } | 192 } |
| 191 | 193 |
| 192 void VariationsService::OnURLFetchComplete(const net::URLFetcher* source) { | 194 void VariationsService::OnURLFetchComplete(const net::URLFetcher* source) { |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 209 bool success = request->GetResponseAsString(&seed_data); | 211 bool success = request->GetResponseAsString(&seed_data); |
| 210 DCHECK(success); | 212 DCHECK(success); |
| 211 | 213 |
| 212 base::Time response_date; | 214 base::Time response_date; |
| 213 success = request->GetResponseHeaders()->GetDateValue(&response_date); | 215 success = request->GetResponseHeaders()->GetDateValue(&response_date); |
| 214 DCHECK(success || response_date.is_null()); | 216 DCHECK(success || response_date.is_null()); |
| 215 | 217 |
| 216 StoreSeedData(seed_data, response_date, g_browser_process->local_state()); | 218 StoreSeedData(seed_data, response_date, g_browser_process->local_state()); |
| 217 } | 219 } |
| 218 | 220 |
| 219 void VariationsService::OnConnectionTypeChanged( | |
| 220 net::NetworkChangeNotifier::ConnectionType type) { | |
| 221 // If the connection type is back online, start a request if the last request | |
| 222 // failed due to being offline. | |
| 223 if (was_offline_during_last_request_attempt_ && | |
| 224 type != net::NetworkChangeNotifier::CONNECTION_NONE) { | |
| 225 VLOG(1) << "Retrying fetch due to network reconnect."; | |
| 226 FetchVariationsSeed(); | |
| 227 | |
| 228 // Since FetchVariationsSeed was explicitly called here, reset the timer to | |
| 229 // avoid retrying for a full period. | |
| 230 // net::NetworkChangeNotifier::IsOffline may be inconsistent with |type|, so | |
| 231 // we check if FetchVariationsSeed set | |
| 232 // |was_offline_during_last_request_attempt_| to true before we reset the | |
| 233 // timer. | |
| 234 if (!was_offline_during_last_request_attempt_ && timer_.IsRunning()) | |
| 235 timer_.Reset(); | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 bool VariationsService::StoreSeedData(const std::string& seed_data, | 221 bool VariationsService::StoreSeedData(const std::string& seed_data, |
| 240 const base::Time& seed_date, | 222 const base::Time& seed_date, |
| 241 PrefService* local_prefs) { | 223 PrefService* local_prefs) { |
| 242 // Only store the seed data if it parses correctly. | 224 // Only store the seed data if it parses correctly. |
| 243 TrialsSeed seed; | 225 TrialsSeed seed; |
| 244 if (!seed.ParseFromString(seed_data)) { | 226 if (!seed.ParseFromString(seed_data)) { |
| 245 VLOG(1) << "Variations Seed data from server is not in valid proto format, " | 227 VLOG(1) << "Variations Seed data from server is not in valid proto format, " |
| 246 << "rejecting the seed."; | 228 << "rejecting the seed."; |
| 247 return false; | 229 return false; |
| 248 } | 230 } |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 495 variation_id); | 477 variation_id); |
| 496 } | 478 } |
| 497 } | 479 } |
| 498 | 480 |
| 499 trial->SetForced(); | 481 trial->SetForced(); |
| 500 if (IsStudyExpired(study, reference_date)) | 482 if (IsStudyExpired(study, reference_date)) |
| 501 trial->Disable(); | 483 trial->Disable(); |
| 502 } | 484 } |
| 503 | 485 |
| 504 } // namespace chrome_variations | 486 } // namespace chrome_variations |
| OLD | NEW |