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_service.h" | 5 #include "chrome/browser/metrics/variations_service.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/build_time.h" | 8 #include "base/build_time.h" |
9 #include "base/version.h" | 9 #include "base/version.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 case chrome_variations::Study_Channel_BETA: | 37 case chrome_variations::Study_Channel_BETA: |
38 return chrome::VersionInfo::CHANNEL_BETA; | 38 return chrome::VersionInfo::CHANNEL_BETA; |
39 case chrome_variations::Study_Channel_STABLE: | 39 case chrome_variations::Study_Channel_STABLE: |
40 return chrome::VersionInfo::CHANNEL_STABLE; | 40 return chrome::VersionInfo::CHANNEL_STABLE; |
41 } | 41 } |
42 // All enum values of |study_channel| were handled above. | 42 // All enum values of |study_channel| were handled above. |
43 NOTREACHED(); | 43 NOTREACHED(); |
44 return chrome::VersionInfo::CHANNEL_UNKNOWN; | 44 return chrome::VersionInfo::CHANNEL_UNKNOWN; |
45 } | 45 } |
46 | 46 |
| 47 // Converts |date_time| in chrome_variations::Study date format to base::Time. |
| 48 base::Time ConvertStudyDateToBaseTime(int64 date_time) { |
| 49 return base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(date_time); |
| 50 } |
| 51 |
47 } // namespace | 52 } // namespace |
48 | 53 |
49 // Static | 54 // Static |
50 VariationsService* VariationsService::GetInstance() { | 55 VariationsService* VariationsService::GetInstance() { |
51 return Singleton<VariationsService>::get(); | 56 return Singleton<VariationsService>::get(); |
52 } | 57 } |
53 | 58 |
54 bool VariationsService::CreateTrialsFromSeed(PrefService* local_prefs) { | 59 bool VariationsService::CreateTrialsFromSeed(PrefService* local_prefs) { |
55 chrome_variations::TrialsSeed seed; | 60 chrome_variations::TrialsSeed seed; |
56 if (!LoadTrialsSeedFromPref(local_prefs, &seed)) | 61 if (!LoadTrialsSeedFromPref(local_prefs, &seed)) |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 return true; | 160 return true; |
156 } | 161 } |
157 return false; | 162 return false; |
158 } | 163 } |
159 | 164 |
160 // static | 165 // static |
161 bool VariationsService::CheckStudyVersion(const chrome_variations::Study& study, | 166 bool VariationsService::CheckStudyVersion(const chrome_variations::Study& study, |
162 const std::string& version_string) { | 167 const std::string& version_string) { |
163 const Version current_version(version_string); | 168 const Version current_version(version_string); |
164 if (!current_version.IsValid()) { | 169 if (!current_version.IsValid()) { |
165 DCHECK(false); | 170 NOTREACHED(); |
166 return false; | 171 return false; |
167 } | 172 } |
168 | 173 |
169 if (study.has_min_version()) { | 174 if (study.has_min_version()) { |
170 const Version min_version(study.min_version()); | 175 const Version min_version(study.min_version()); |
171 if (!min_version.IsValid()) | 176 if (!min_version.IsValid()) |
172 return false; | 177 return false; |
173 if (current_version.CompareTo(min_version) < 0) | 178 if (current_version.CompareTo(min_version) < 0) |
174 return false; | 179 return false; |
175 } | 180 } |
176 | 181 |
177 if (study.has_max_version()) { | 182 if (study.has_max_version()) { |
178 const Version max_version(study.max_version()); | 183 const Version max_version(study.max_version()); |
179 if (!max_version.IsValid()) | 184 if (!max_version.IsValid()) |
180 return false; | 185 return false; |
181 if (current_version.CompareTo(max_version) > 0) | 186 if (current_version.CompareTo(max_version) > 0) |
182 return false; | 187 return false; |
183 } | 188 } |
184 | 189 |
185 return true; | 190 return true; |
186 } | 191 } |
187 | 192 |
188 // static | 193 // static |
189 bool VariationsService::CheckStudyDate(const chrome_variations::Study& study, | 194 bool VariationsService::CheckStudyDate(const chrome_variations::Study& study, |
190 const base::Time& date_time) { | 195 const base::Time& date_time) { |
191 const base::Time epoch = base::Time::UnixEpoch(); | |
192 | |
193 if (study.has_start_date()) { | 196 if (study.has_start_date()) { |
194 const base::Time start_date = | 197 const base::Time start_date = |
195 epoch + base::TimeDelta::FromSeconds(study.start_date()); | 198 ConvertStudyDateToBaseTime(study.start_date()); |
196 if (date_time < start_date) | 199 if (date_time < start_date) |
197 return false; | 200 return false; |
198 } | 201 } |
199 | 202 |
200 if (study.has_expiry_date()) { | 203 if (study.has_expiry_date()) { |
201 const base::Time expiry_date = | 204 const base::Time expiry_date = |
202 epoch + base::TimeDelta::FromSeconds(study.expiry_date()); | 205 ConvertStudyDateToBaseTime(study.expiry_date()); |
203 if (date_time >= expiry_date) | 206 if (date_time >= expiry_date) |
204 return false; | 207 return false; |
205 } | 208 } |
206 | 209 |
207 return true; | 210 return true; |
208 } | 211 } |
209 | 212 |
210 bool VariationsService::LoadTrialsSeedFromPref( | 213 bool VariationsService::LoadTrialsSeedFromPref( |
211 PrefService* local_prefs, | 214 PrefService* local_prefs, |
212 chrome_variations::TrialsSeed* seed) { | 215 chrome_variations::TrialsSeed* seed) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 found_default_group = true; | 249 found_default_group = true; |
247 } | 250 } |
248 if (!found_default_group) { | 251 if (!found_default_group) { |
249 DVLOG(1) << study.name() << " is missing default experiment in it's " | 252 DVLOG(1) << study.name() << " is missing default experiment in it's " |
250 << "experiment list"; | 253 << "experiment list"; |
251 // The default group was not found in the list of groups. This study is not | 254 // The default group was not found in the list of groups. This study is not |
252 // valid. | 255 // valid. |
253 return; | 256 return; |
254 } | 257 } |
255 | 258 |
256 const base::Time epoch = base::Time::UnixEpoch(); | |
257 const base::Time expiry_date = | 259 const base::Time expiry_date = |
258 epoch + base::TimeDelta::FromSeconds(study.expiry_date()); | 260 ConvertStudyDateToBaseTime(study.expiry_date()); |
259 base::Time::Exploded exploded_end_date; | 261 base::Time::Exploded exploded_end_date; |
260 expiry_date.UTCExplode(&exploded_end_date); | 262 expiry_date.UTCExplode(&exploded_end_date); |
261 | 263 |
262 scoped_refptr<base::FieldTrial> trial( | 264 scoped_refptr<base::FieldTrial> trial( |
263 base::FieldTrialList::FactoryGetFieldTrial( | 265 base::FieldTrialList::FactoryGetFieldTrial( |
264 study.name(), divisor, default_group_name, exploded_end_date.year, | 266 study.name(), divisor, default_group_name, exploded_end_date.year, |
265 exploded_end_date.month, exploded_end_date.day_of_month, NULL)); | 267 exploded_end_date.month, exploded_end_date.day_of_month, NULL)); |
266 | 268 |
267 if (study.has_consistency() && | 269 if (study.has_consistency() && |
268 study.consistency() == chrome_variations::Study_Consistency_PERMANENT) { | 270 study.consistency() == chrome_variations::Study_Consistency_PERMANENT) { |
269 trial->UseOneTimeRandomization(); | 271 trial->UseOneTimeRandomization(); |
270 } | 272 } |
271 | 273 |
272 for (int i = 0; i < study.experiment_size(); ++i) { | 274 for (int i = 0; i < study.experiment_size(); ++i) { |
273 if (study.experiment(i).name() != default_group_name) { | 275 if (study.experiment(i).name() != default_group_name) { |
274 trial->AppendGroup(study.experiment(i).name(), | 276 trial->AppendGroup(study.experiment(i).name(), |
275 study.experiment(i).probability_weight()); | 277 study.experiment(i).probability_weight()); |
276 } | 278 } |
277 } | 279 } |
278 | 280 |
279 // TODO(jwd): Add experiment_id association code. | 281 // TODO(jwd): Add experiment_id association code. |
280 trial->SetForced(); | 282 trial->SetForced(); |
281 } | 283 } |
OLD | NEW |