Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(140)

Side by Side Diff: chrome/browser/metrics/variations/variations_service.cc

Issue 10917120: Activate the VariationsService for ChromeOS and ensure that it does not ping the server until the E… (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Comments addressed. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 28 matching lines...) Expand all
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 void VariationsService::FetchVariationsSeed() {
155 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 161 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
156 162
157 was_offline_during_last_request_attempt_ = 163 if (!resource_request_allowed_notifier_->ResourceRequestsAllowed()) {
158 net::NetworkChangeNotifier::IsOffline(); 164 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; 165 return;
164 } 166 }
165 167
168 DoActualFetch();
169 }
170
171 // static
172 void VariationsService::RegisterPrefs(PrefService* prefs) {
173 prefs->RegisterStringPref(prefs::kVariationsSeed, std::string());
174 prefs->RegisterInt64Pref(prefs::kVariationsSeedDate,
175 base::Time().ToInternalValue());
176 }
177
178 ResourceRequestAllowedNotifier*
179 VariationsService::GetResourceRequestAllowedNotifierForTesting() {
Alexei Svitkine (slow) 2012/09/20 21:39:03 Nit: Indent 4.
SteveT 2012/09/21 15:16:17 Removed.
180 return resource_request_allowed_notifier_.get();
181 }
182
183 void VariationsService::SetCreateTrialsFromSeedCalledForTesting(bool called) {
184 create_trials_from_seed_called_ = called;
185 }
186
187 void VariationsService::DoActualFetch() {
166 pending_seed_request_.reset(net::URLFetcher::Create( 188 pending_seed_request_.reset(net::URLFetcher::Create(
167 variations_server_url_, net::URLFetcher::GET, this)); 189 variations_server_url_, net::URLFetcher::GET, this));
168 pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 190 pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
169 net::LOAD_DO_NOT_SAVE_COOKIES); 191 net::LOAD_DO_NOT_SAVE_COOKIES);
170 pending_seed_request_->SetRequestContext( 192 pending_seed_request_->SetRequestContext(
171 g_browser_process->system_request_context()); 193 g_browser_process->system_request_context());
172 pending_seed_request_->SetMaxRetries(kMaxRetrySeedFetch); 194 pending_seed_request_->SetMaxRetries(kMaxRetrySeedFetch);
173 if (!variations_serial_number_.empty()) { 195 if (!variations_serial_number_.empty()) {
174 pending_seed_request_->AddExtraRequestHeader("If-Match:" + 196 pending_seed_request_->AddExtraRequestHeader("If-Match:" +
175 variations_serial_number_); 197 variations_serial_number_);
176 } 198 }
177 pending_seed_request_->Start(); 199 pending_seed_request_->Start();
178 } 200 }
179 201
180 void VariationsService::SetWasOfflineDuringLastRequestAttemptForTesting(
181 bool offline) {
182 was_offline_during_last_request_attempt_ = offline;
183 }
184
185 // static
186 void VariationsService::RegisterPrefs(PrefService* prefs) {
187 prefs->RegisterStringPref(prefs::kVariationsSeed, std::string());
188 prefs->RegisterInt64Pref(prefs::kVariationsSeedDate,
189 base::Time().ToInternalValue());
190 }
191
192 void VariationsService::OnURLFetchComplete(const net::URLFetcher* source) { 202 void VariationsService::OnURLFetchComplete(const net::URLFetcher* source) {
193 DCHECK_EQ(pending_seed_request_.get(), source); 203 DCHECK_EQ(pending_seed_request_.get(), source);
194 // The fetcher will be deleted when the request is handled. 204 // The fetcher will be deleted when the request is handled.
195 scoped_ptr<const net::URLFetcher> request( 205 scoped_ptr<const net::URLFetcher> request(
196 pending_seed_request_.release()); 206 pending_seed_request_.release());
197 if (request->GetStatus().status() != net::URLRequestStatus::SUCCESS) { 207 if (request->GetStatus().status() != net::URLRequestStatus::SUCCESS) {
198 DVLOG(1) << "Variations server request failed."; 208 DVLOG(1) << "Variations server request failed.";
199 return; 209 return;
200 } 210 }
201 211
202 if (request->GetResponseCode() != 200) { 212 if (request->GetResponseCode() != 200) {
203 DVLOG(1) << "Variations server request returned non-200 response code: " 213 DVLOG(1) << "Variations server request returned non-200 response code: "
204 << request->GetResponseCode(); 214 << request->GetResponseCode();
205 return; 215 return;
206 } 216 }
207 217
208 std::string seed_data; 218 std::string seed_data;
209 bool success = request->GetResponseAsString(&seed_data); 219 bool success = request->GetResponseAsString(&seed_data);
210 DCHECK(success); 220 DCHECK(success);
211 221
212 base::Time response_date; 222 base::Time response_date;
213 success = request->GetResponseHeaders()->GetDateValue(&response_date); 223 success = request->GetResponseHeaders()->GetDateValue(&response_date);
214 DCHECK(success || response_date.is_null()); 224 DCHECK(success || response_date.is_null());
215 225
216 StoreSeedData(seed_data, response_date, g_browser_process->local_state()); 226 StoreSeedData(seed_data, response_date, g_browser_process->local_state());
217 } 227 }
218 228
219 void VariationsService::OnConnectionTypeChanged( 229 void VariationsService::OnResourceRequestsAllowed() {
220 net::NetworkChangeNotifier::ConnectionType type) { 230 // 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 231 // (kSeedFetchPeriodHours). This works because
222 // failed due to being offline. 232 // |resource_request_allowed_notifier_| only calls this method if an
223 if (was_offline_during_last_request_attempt_ && 233 // attempt was made earlier that fails (which implies that the period had
224 type != net::NetworkChangeNotifier::CONNECTION_NONE) { 234 // elapsed). After a successful attempt is made, the notifier will know not
225 VLOG(1) << "Retrying fetch due to network reconnect."; 235 // to call this method again until another failed attempt occurs.
226 FetchVariationsSeed(); 236 DVLOG(1) << "Retrying fetch.";
227 237 FetchVariationsSeed();
228 // Since FetchVariationsSeed was explicitly called here, reset the timer to 238 if (timer_.IsRunning())
229 // avoid retrying for a full period. 239 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 } 240 }
238 241
239 bool VariationsService::StoreSeedData(const std::string& seed_data, 242 bool VariationsService::StoreSeedData(const std::string& seed_data,
240 const base::Time& seed_date, 243 const base::Time& seed_date,
241 PrefService* local_prefs) { 244 PrefService* local_prefs) {
242 // Only store the seed data if it parses correctly. 245 // Only store the seed data if it parses correctly.
243 TrialsSeed seed; 246 TrialsSeed seed;
244 if (!seed.ParseFromString(seed_data)) { 247 if (!seed.ParseFromString(seed_data)) {
245 VLOG(1) << "Variations Seed data from server is not in valid proto format, " 248 VLOG(1) << "Variations Seed data from server is not in valid proto format, "
246 << "rejecting the seed."; 249 << "rejecting the seed.";
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 variation_id); 498 variation_id);
496 } 499 }
497 } 500 }
498 501
499 trial->SetForced(); 502 trial->SetForced();
500 if (IsStudyExpired(study, reference_date)) 503 if (IsStudyExpired(study, reference_date))
501 trial->Disable(); 504 trial->Disable();
502 } 505 }
503 506
504 } // namespace chrome_variations 507 } // namespace chrome_variations
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698