| 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" |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 GURL url_as_gurl = GURL(server_url); | 93 GURL url_as_gurl = GURL(server_url); |
| 94 DCHECK(url_as_gurl.is_valid()); | 94 DCHECK(url_as_gurl.is_valid()); |
| 95 return url_as_gurl; | 95 return url_as_gurl; |
| 96 } | 96 } |
| 97 | 97 |
| 98 } // namespace | 98 } // namespace |
| 99 | 99 |
| 100 VariationsService::VariationsService() | 100 VariationsService::VariationsService() |
| 101 : variations_server_url_(GetVariationsServerURL()), | 101 : variations_server_url_(GetVariationsServerURL()), |
| 102 create_trials_from_seed_called_(false), | 102 create_trials_from_seed_called_(false), |
| 103 #if defined(OS_CHROMEOS) |
| 104 waiting_for_user_to_accept_eula_(false), |
| 105 #endif |
| 103 was_offline_during_last_request_attempt_(false) { | 106 was_offline_during_last_request_attempt_(false) { |
| 104 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); | 107 resource_request_allowed_notifier_.AddObserver(this); |
| 105 } | 108 } |
| 106 | 109 |
| 107 VariationsService::~VariationsService() { | 110 VariationsService::~VariationsService() { |
| 108 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | 111 resource_request_allowed_notifier_.RemoveObserver(this); |
| 109 } | 112 } |
| 110 | 113 |
| 111 bool VariationsService::CreateTrialsFromSeed(PrefService* local_prefs) { | 114 bool VariationsService::CreateTrialsFromSeed(PrefService* local_prefs) { |
| 112 create_trials_from_seed_called_ = true; | 115 create_trials_from_seed_called_ = true; |
| 113 | 116 |
| 114 TrialsSeed seed; | 117 TrialsSeed seed; |
| 115 if (!LoadTrialsSeedFromPref(local_prefs, &seed)) | 118 if (!LoadTrialsSeedFromPref(local_prefs, &seed)) |
| 116 return false; | 119 return false; |
| 117 | 120 |
| 118 const int64 date_value = local_prefs->GetInt64(prefs::kVariationsSeedDate); | 121 const int64 date_value = local_prefs->GetInt64(prefs::kVariationsSeedDate); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 | 159 |
| 157 was_offline_during_last_request_attempt_ = | 160 was_offline_during_last_request_attempt_ = |
| 158 net::NetworkChangeNotifier::IsOffline(); | 161 net::NetworkChangeNotifier::IsOffline(); |
| 159 UMA_HISTOGRAM_BOOLEAN("Variations.NetworkAvailability", | 162 UMA_HISTOGRAM_BOOLEAN("Variations.NetworkAvailability", |
| 160 !was_offline_during_last_request_attempt_); | 163 !was_offline_during_last_request_attempt_); |
| 161 if (was_offline_during_last_request_attempt_) { | 164 if (was_offline_during_last_request_attempt_) { |
| 162 DVLOG(1) << "Network was offline."; | 165 DVLOG(1) << "Network was offline."; |
| 163 return; | 166 return; |
| 164 } | 167 } |
| 165 | 168 |
| 169 #if defined(OS_CHROMEOS) |
| 170 if (!ResourceRequestAllowedNotifier::IsEulaAccepted()) { |
| 171 // The ResourceRequestAllowedNotifier should notify this class when the EULA |
| 172 // has been accepted. |
| 173 VLOG(1) << "EULA was not accepted."; |
| 174 waiting_for_user_to_accept_eula_ = true; |
| 175 return; |
| 176 } |
| 177 #endif |
| 178 |
| 166 pending_seed_request_.reset(net::URLFetcher::Create( | 179 pending_seed_request_.reset(net::URLFetcher::Create( |
| 167 variations_server_url_, net::URLFetcher::GET, this)); | 180 variations_server_url_, net::URLFetcher::GET, this)); |
| 168 pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 181 pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 169 net::LOAD_DO_NOT_SAVE_COOKIES); | 182 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 170 pending_seed_request_->SetRequestContext( | 183 pending_seed_request_->SetRequestContext( |
| 171 g_browser_process->system_request_context()); | 184 g_browser_process->system_request_context()); |
| 172 pending_seed_request_->SetMaxRetries(kMaxRetrySeedFetch); | 185 pending_seed_request_->SetMaxRetries(kMaxRetrySeedFetch); |
| 173 if (!variations_serial_number_.empty()) { | 186 if (!variations_serial_number_.empty()) { |
| 174 pending_seed_request_->AddExtraRequestHeader("If-Match:" + | 187 pending_seed_request_->AddExtraRequestHeader("If-Match:" + |
| 175 variations_serial_number_); | 188 variations_serial_number_); |
| 176 } | 189 } |
| 177 pending_seed_request_->Start(); | 190 pending_seed_request_->Start(); |
| 178 } | 191 } |
| 179 | 192 |
| 180 void VariationsService::SetWasOfflineDuringLastRequestAttemptForTesting( | 193 void VariationsService::SetWasOfflineDuringLastRequestAttemptForTesting( |
| 181 bool offline) { | 194 bool offline) { |
| 182 was_offline_during_last_request_attempt_ = offline; | 195 was_offline_during_last_request_attempt_ = offline; |
| 183 } | 196 } |
| 184 | 197 |
| 198 void VariationsService::OnNetworkChangedToActiveConnection() { |
| 199 // If the connection type is back online, start a request if the last request |
| 200 // failed due to being offline. |
| 201 if (was_offline_during_last_request_attempt_) { |
| 202 VLOG(1) << "Retrying fetch due to network reconnect."; |
| 203 FetchVariationsSeed(); |
| 204 |
| 205 // Since FetchVariationsSeed was explicitly called here, reset the timer to |
| 206 // avoid retrying for a full period. |
| 207 // ResourceRequestAllowedNotifier::IsNetworkOffline may be inconsistent with |
| 208 // calls to OnNetworkChangedToActiveConnection, so we check if |
| 209 // FetchVariationsSeed set |was_offline_during_last_request_attempt_| to |
| 210 // true before we reset the timer. |
| 211 if (!was_offline_during_last_request_attempt_ && timer_.IsRunning()) |
| 212 timer_.Reset(); |
| 213 } |
| 214 } |
| 215 |
| 216 #if defined(OS_CHROMEOS) |
| 217 void VariationsService::OnEulaAccepted() { |
| 218 // If an earlier request attempt was aborted because the EULA was not |
| 219 // accepted, retry the request now. |
| 220 // Note that if the EULA check succeeded earlier, this just returns. |
| 221 if (!waiting_for_user_to_accept_eula_) |
| 222 return; |
| 223 DCHECK(ResourceRequestAllowedNotifier::IsEulaAccepted()); |
| 224 waiting_for_user_to_accept_eula_ = false; |
| 225 |
| 226 VLOG(1) << "Starting ping because the EULA was accepted."; |
| 227 FetchVariationsSeed(); |
| 228 |
| 229 // Since FetchVariationsSeed was explicitly called here, reset the timer to |
| 230 // avoid retrying for a full period. |
| 231 if (timer_.IsRunning()) |
| 232 timer_.Reset(); |
| 233 } |
| 234 #endif |
| 235 |
| 185 // static | 236 // static |
| 186 void VariationsService::RegisterPrefs(PrefService* prefs) { | 237 void VariationsService::RegisterPrefs(PrefService* prefs) { |
| 187 prefs->RegisterStringPref(prefs::kVariationsSeed, std::string()); | 238 prefs->RegisterStringPref(prefs::kVariationsSeed, std::string()); |
| 188 prefs->RegisterInt64Pref(prefs::kVariationsSeedDate, | 239 prefs->RegisterInt64Pref(prefs::kVariationsSeedDate, |
| 189 base::Time().ToInternalValue()); | 240 base::Time().ToInternalValue()); |
| 190 } | 241 } |
| 191 | 242 |
| 192 void VariationsService::OnURLFetchComplete(const net::URLFetcher* source) { | 243 void VariationsService::OnURLFetchComplete(const net::URLFetcher* source) { |
| 193 DCHECK_EQ(pending_seed_request_.get(), source); | 244 DCHECK_EQ(pending_seed_request_.get(), source); |
| 194 // The fetcher will be deleted when the request is handled. | 245 // The fetcher will be deleted when the request is handled. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 209 bool success = request->GetResponseAsString(&seed_data); | 260 bool success = request->GetResponseAsString(&seed_data); |
| 210 DCHECK(success); | 261 DCHECK(success); |
| 211 | 262 |
| 212 base::Time response_date; | 263 base::Time response_date; |
| 213 success = request->GetResponseHeaders()->GetDateValue(&response_date); | 264 success = request->GetResponseHeaders()->GetDateValue(&response_date); |
| 214 DCHECK(success || response_date.is_null()); | 265 DCHECK(success || response_date.is_null()); |
| 215 | 266 |
| 216 StoreSeedData(seed_data, response_date, g_browser_process->local_state()); | 267 StoreSeedData(seed_data, response_date, g_browser_process->local_state()); |
| 217 } | 268 } |
| 218 | 269 |
| 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, | 270 bool VariationsService::StoreSeedData(const std::string& seed_data, |
| 240 const base::Time& seed_date, | 271 const base::Time& seed_date, |
| 241 PrefService* local_prefs) { | 272 PrefService* local_prefs) { |
| 242 // Only store the seed data if it parses correctly. | 273 // Only store the seed data if it parses correctly. |
| 243 TrialsSeed seed; | 274 TrialsSeed seed; |
| 244 if (!seed.ParseFromString(seed_data)) { | 275 if (!seed.ParseFromString(seed_data)) { |
| 245 VLOG(1) << "Variations Seed data from server is not in valid proto format, " | 276 VLOG(1) << "Variations Seed data from server is not in valid proto format, " |
| 246 << "rejecting the seed."; | 277 << "rejecting the seed."; |
| 247 return false; | 278 return false; |
| 248 } | 279 } |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 variation_id); | 526 variation_id); |
| 496 } | 527 } |
| 497 } | 528 } |
| 498 | 529 |
| 499 trial->SetForced(); | 530 trial->SetForced(); |
| 500 if (IsStudyExpired(study, reference_date)) | 531 if (IsStudyExpired(study, reference_date)) |
| 501 trial->Disable(); | 532 trial->Disable(); |
| 502 } | 533 } |
| 503 | 534 |
| 504 } // namespace chrome_variations | 535 } // namespace chrome_variations |
| OLD | NEW |