| 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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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_( |
| 104 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); | 103 new ResourceRequestAllowedNotifier) { |
| 104 resource_request_allowed_notifier_->Init(this); |
| 105 } |
| 106 |
| 107 VariationsService::VariationsService(ResourceRequestAllowedNotifier* notifier) |
| 108 : variations_server_url_(GetVariationsServerURL()), |
| 109 create_trials_from_seed_called_(false), |
| 110 resource_request_allowed_notifier_(notifier) { |
| 111 resource_request_allowed_notifier_->Init(this); |
| 105 } | 112 } |
| 106 | 113 |
| 107 VariationsService::~VariationsService() { | 114 VariationsService::~VariationsService() { |
| 108 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | |
| 109 } | 115 } |
| 110 | 116 |
| 111 bool VariationsService::CreateTrialsFromSeed(PrefService* local_prefs) { | 117 bool VariationsService::CreateTrialsFromSeed(PrefService* local_prefs) { |
| 112 create_trials_from_seed_called_ = true; | 118 create_trials_from_seed_called_ = true; |
| 113 | 119 |
| 114 TrialsSeed seed; | 120 TrialsSeed seed; |
| 115 if (!LoadTrialsSeedFromPref(local_prefs, &seed)) | 121 if (!LoadTrialsSeedFromPref(local_prefs, &seed)) |
| 116 return false; | 122 return false; |
| 117 | 123 |
| 118 const int64 date_value = local_prefs->GetInt64(prefs::kVariationsSeedDate); | 124 const int64 date_value = local_prefs->GetInt64(prefs::kVariationsSeedDate); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 144 DCHECK(create_trials_from_seed_called_); | 150 DCHECK(create_trials_from_seed_called_); |
| 145 | 151 |
| 146 // Perform the first fetch. | 152 // Perform the first fetch. |
| 147 FetchVariationsSeed(); | 153 FetchVariationsSeed(); |
| 148 | 154 |
| 149 // Repeat this periodically. | 155 // Repeat this periodically. |
| 150 timer_.Start(FROM_HERE, base::TimeDelta::FromHours(kSeedFetchPeriodHours), | 156 timer_.Start(FROM_HERE, base::TimeDelta::FromHours(kSeedFetchPeriodHours), |
| 151 this, &VariationsService::FetchVariationsSeed); | 157 this, &VariationsService::FetchVariationsSeed); |
| 152 } | 158 } |
| 153 | 159 |
| 154 void VariationsService::FetchVariationsSeed() { | 160 // static |
| 155 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 161 void VariationsService::RegisterPrefs(PrefService* prefs) { |
| 162 prefs->RegisterStringPref(prefs::kVariationsSeed, std::string()); |
| 163 prefs->RegisterInt64Pref(prefs::kVariationsSeedDate, |
| 164 base::Time().ToInternalValue()); |
| 165 } |
| 156 | 166 |
| 157 was_offline_during_last_request_attempt_ = | 167 void VariationsService::SetCreateTrialsFromSeedCalledForTesting(bool called) { |
| 158 net::NetworkChangeNotifier::IsOffline(); | 168 create_trials_from_seed_called_ = called; |
| 159 UMA_HISTOGRAM_BOOLEAN("Variations.NetworkAvailability", | 169 } |
| 160 !was_offline_during_last_request_attempt_); | |
| 161 if (was_offline_during_last_request_attempt_) { | |
| 162 DVLOG(1) << "Network was offline."; | |
| 163 return; | |
| 164 } | |
| 165 | 170 |
| 171 void VariationsService::DoActualFetch() { |
| 166 pending_seed_request_.reset(net::URLFetcher::Create( | 172 pending_seed_request_.reset(net::URLFetcher::Create( |
| 167 variations_server_url_, net::URLFetcher::GET, this)); | 173 variations_server_url_, net::URLFetcher::GET, this)); |
| 168 pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 174 pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 169 net::LOAD_DO_NOT_SAVE_COOKIES); | 175 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 170 pending_seed_request_->SetRequestContext( | 176 pending_seed_request_->SetRequestContext( |
| 171 g_browser_process->system_request_context()); | 177 g_browser_process->system_request_context()); |
| 172 pending_seed_request_->SetMaxRetries(kMaxRetrySeedFetch); | 178 pending_seed_request_->SetMaxRetries(kMaxRetrySeedFetch); |
| 173 if (!variations_serial_number_.empty()) { | 179 if (!variations_serial_number_.empty()) { |
| 174 pending_seed_request_->AddExtraRequestHeader("If-Match:" + | 180 pending_seed_request_->AddExtraRequestHeader("If-Match:" + |
| 175 variations_serial_number_); | 181 variations_serial_number_); |
| 176 } | 182 } |
| 177 pending_seed_request_->Start(); | 183 pending_seed_request_->Start(); |
| 178 } | 184 } |
| 179 | 185 |
| 180 void VariationsService::SetWasOfflineDuringLastRequestAttemptForTesting( | 186 void VariationsService::FetchVariationsSeed() { |
| 181 bool offline) { | 187 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 182 was_offline_during_last_request_attempt_ = offline; | |
| 183 } | |
| 184 | 188 |
| 185 // static | 189 if (!resource_request_allowed_notifier_->ResourceRequestsAllowed()) { |
| 186 void VariationsService::RegisterPrefs(PrefService* prefs) { | 190 DVLOG(1) << "Resource requests were not allowed. Waiting for notification."; |
| 187 prefs->RegisterStringPref(prefs::kVariationsSeed, std::string()); | 191 return; |
| 188 prefs->RegisterInt64Pref(prefs::kVariationsSeedDate, | 192 } |
| 189 base::Time().ToInternalValue()); | 193 |
| 194 DoActualFetch(); |
| 190 } | 195 } |
| 191 | 196 |
| 192 void VariationsService::OnURLFetchComplete(const net::URLFetcher* source) { | 197 void VariationsService::OnURLFetchComplete(const net::URLFetcher* source) { |
| 193 DCHECK_EQ(pending_seed_request_.get(), source); | 198 DCHECK_EQ(pending_seed_request_.get(), source); |
| 194 // The fetcher will be deleted when the request is handled. | 199 // The fetcher will be deleted when the request is handled. |
| 195 scoped_ptr<const net::URLFetcher> request( | 200 scoped_ptr<const net::URLFetcher> request( |
| 196 pending_seed_request_.release()); | 201 pending_seed_request_.release()); |
| 197 if (request->GetStatus().status() != net::URLRequestStatus::SUCCESS) { | 202 if (request->GetStatus().status() != net::URLRequestStatus::SUCCESS) { |
| 198 DVLOG(1) << "Variations server request failed."; | 203 DVLOG(1) << "Variations server request failed."; |
| 199 return; | 204 return; |
| 200 } | 205 } |
| 201 | 206 |
| 202 if (request->GetResponseCode() != 200) { | 207 if (request->GetResponseCode() != 200) { |
| 203 DVLOG(1) << "Variations server request returned non-200 response code: " | 208 DVLOG(1) << "Variations server request returned non-200 response code: " |
| 204 << request->GetResponseCode(); | 209 << request->GetResponseCode(); |
| 205 return; | 210 return; |
| 206 } | 211 } |
| 207 | 212 |
| 208 std::string seed_data; | 213 std::string seed_data; |
| 209 bool success = request->GetResponseAsString(&seed_data); | 214 bool success = request->GetResponseAsString(&seed_data); |
| 210 DCHECK(success); | 215 DCHECK(success); |
| 211 | 216 |
| 212 base::Time response_date; | 217 base::Time response_date; |
| 213 success = request->GetResponseHeaders()->GetDateValue(&response_date); | 218 success = request->GetResponseHeaders()->GetDateValue(&response_date); |
| 214 DCHECK(success || response_date.is_null()); | 219 DCHECK(success || response_date.is_null()); |
| 215 | 220 |
| 216 StoreSeedData(seed_data, response_date, g_browser_process->local_state()); | 221 StoreSeedData(seed_data, response_date, g_browser_process->local_state()); |
| 217 } | 222 } |
| 218 | 223 |
| 219 void VariationsService::OnConnectionTypeChanged( | 224 void VariationsService::OnResourceRequestsAllowed() { |
| 220 net::NetworkChangeNotifier::ConnectionType type) { | 225 // Note that this only attempts to fetch the seed at most once per period |
| 221 // If the connection type is back online, start a request if the last request | 226 // (kSeedFetchPeriodHours). This works because |
| 222 // failed due to being offline. | 227 // |resource_request_allowed_notifier_| only calls this method if an |
| 223 if (was_offline_during_last_request_attempt_ && | 228 // attempt was made earlier that fails (which implies that the period had |
| 224 type != net::NetworkChangeNotifier::CONNECTION_NONE) { | 229 // elapsed). After a successful attempt is made, the notifier will know not |
| 225 VLOG(1) << "Retrying fetch due to network reconnect."; | 230 // to call this method again until another failed attempt occurs. |
| 226 FetchVariationsSeed(); | 231 DVLOG(1) << "Retrying fetch."; |
| 227 | 232 DoActualFetch(); |
| 228 // Since FetchVariationsSeed was explicitly called here, reset the timer to | 233 if (timer_.IsRunning()) |
| 229 // avoid retrying for a full period. | 234 timer_.Reset(); |
| 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 } | 235 } |
| 238 | 236 |
| 239 bool VariationsService::StoreSeedData(const std::string& seed_data, | 237 bool VariationsService::StoreSeedData(const std::string& seed_data, |
| 240 const base::Time& seed_date, | 238 const base::Time& seed_date, |
| 241 PrefService* local_prefs) { | 239 PrefService* local_prefs) { |
| 242 // Only store the seed data if it parses correctly. | 240 // Only store the seed data if it parses correctly. |
| 243 TrialsSeed seed; | 241 TrialsSeed seed; |
| 244 if (!seed.ParseFromString(seed_data)) { | 242 if (!seed.ParseFromString(seed_data)) { |
| 245 VLOG(1) << "Variations Seed data from server is not in valid proto format, " | 243 VLOG(1) << "Variations Seed data from server is not in valid proto format, " |
| 246 << "rejecting the seed."; | 244 << "rejecting the seed."; |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 variation_id); | 493 variation_id); |
| 496 } | 494 } |
| 497 } | 495 } |
| 498 | 496 |
| 499 trial->SetForced(); | 497 trial->SetForced(); |
| 500 if (IsStudyExpired(study, reference_date)) | 498 if (IsStudyExpired(study, reference_date)) |
| 501 trial->Disable(); | 499 trial->Disable(); |
| 502 } | 500 } |
| 503 | 501 |
| 504 } // namespace chrome_variations | 502 } // namespace chrome_variations |
| OLD | NEW |