1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/variations/processed_study.h"
9 #include "base/version.h"
10 #include "components/variations/proto/study.pb.h"
12 namespace chrome_variations
{
16 // Validates the sanity of |study| and computes the total probability.
17 bool ValidateStudyAndComputeTotalProbability(
19 base::FieldTrial::Probability
* total_probability
) {
20 // At the moment, a missing default_experiment_name makes the study invalid.
21 if (study
.default_experiment_name().empty()) {
22 DVLOG(1) << study
.name() << " has no default experiment defined.";
25 if (study
.filter().has_min_version() &&
26 !Version::IsValidWildcardString(study
.filter().min_version())) {
27 DVLOG(1) << study
.name() << " has invalid min version: "
28 << study
.filter().min_version();
31 if (study
.filter().has_max_version() &&
32 !Version::IsValidWildcardString(study
.filter().max_version())) {
33 DVLOG(1) << study
.name() << " has invalid max version: "
34 << study
.filter().max_version();
38 const std::string
& default_group_name
= study
.default_experiment_name();
39 base::FieldTrial::Probability divisor
= 0;
41 bool found_default_group
= false;
42 std::set
<std::string
> experiment_names
;
43 for (int i
= 0; i
< study
.experiment_size(); ++i
) {
44 if (study
.experiment(i
).name().empty()) {
45 DVLOG(1) << study
.name() << " is missing experiment " << i
<< " name";
48 if (!experiment_names
.insert(study
.experiment(i
).name()).second
) {
49 DVLOG(1) << study
.name() << " has a repeated experiment name "
50 << study
.experiment(i
).name();
54 if (!study
.experiment(i
).has_forcing_flag())
55 divisor
+= study
.experiment(i
).probability_weight();
56 if (study
.experiment(i
).name() == default_group_name
)
57 found_default_group
= true;
60 if (!found_default_group
) {
61 DVLOG(1) << study
.name() << " is missing default experiment in its "
63 // The default group was not found in the list of groups. This study is not
68 *total_probability
= divisor
;
76 ProcessedStudy::ProcessedStudy()
77 : study_(NULL
), total_probability_(0), is_expired_(false) {
80 ProcessedStudy::~ProcessedStudy() {
83 bool ProcessedStudy::Init(const Study
* study
, bool is_expired
) {
84 base::FieldTrial::Probability total_probability
= 0;
85 if (!ValidateStudyAndComputeTotalProbability(*study
, &total_probability
))
89 is_expired_
= is_expired
;
90 total_probability_
= total_probability
;
95 bool ProcessedStudy::ValidateAndAppendStudy(
98 std::vector
<ProcessedStudy
>* processed_studies
) {
99 ProcessedStudy processed_study
;
100 if (processed_study
.Init(study
, is_expired
)) {
101 processed_studies
->push_back(processed_study
);
107 } // namespace chrome_variations