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"
10 #include "base/version.h"
11 #include "components/variations/proto/study.pb.h"
13 namespace variations
{
17 // Validates the sanity of |study| and computes the total probability and
18 // whether all assignments are to a single group.
19 bool ValidateStudyAndComputeTotalProbability(
21 base::FieldTrial::Probability
* total_probability
,
22 bool* all_assignments_to_one_group
) {
23 // At the moment, a missing default_experiment_name makes the study invalid.
24 if (study
.default_experiment_name().empty()) {
25 DVLOG(1) << study
.name() << " has no default experiment defined.";
28 if (study
.filter().has_min_version() &&
29 !Version::IsValidWildcardString(study
.filter().min_version())) {
30 DVLOG(1) << study
.name() << " has invalid min version: "
31 << study
.filter().min_version();
34 if (study
.filter().has_max_version() &&
35 !Version::IsValidWildcardString(study
.filter().max_version())) {
36 DVLOG(1) << study
.name() << " has invalid max version: "
37 << study
.filter().max_version();
41 const std::string
& default_group_name
= study
.default_experiment_name();
42 base::FieldTrial::Probability divisor
= 0;
44 bool multiple_assigned_groups
= false;
45 bool found_default_group
= false;
46 std::set
<std::string
> experiment_names
;
47 for (int i
= 0; i
< study
.experiment_size(); ++i
) {
48 const Study_Experiment
& experiment
= study
.experiment(i
);
49 if (experiment
.name().empty()) {
50 DVLOG(1) << study
.name() << " is missing experiment " << i
<< " name";
53 if (!experiment_names
.insert(experiment
.name()).second
) {
54 DVLOG(1) << study
.name() << " has a repeated experiment name "
55 << study
.experiment(i
).name();
59 if (!experiment
.has_forcing_flag() && experiment
.probability_weight() > 0) {
60 // If |divisor| is not 0, there was at least one prior non-zero group.
62 multiple_assigned_groups
= true;
63 divisor
+= experiment
.probability_weight();
65 if (study
.experiment(i
).name() == default_group_name
)
66 found_default_group
= true;
69 if (!found_default_group
) {
70 DVLOG(1) << study
.name() << " is missing default experiment in its "
72 // The default group was not found in the list of groups. This study is not
77 *total_probability
= divisor
;
78 *all_assignments_to_one_group
= !multiple_assigned_groups
;
85 ProcessedStudy::ProcessedStudy()
87 total_probability_(0),
88 all_assignments_to_one_group_(false),
92 ProcessedStudy::~ProcessedStudy() {
95 bool ProcessedStudy::Init(const Study
* study
, bool is_expired
) {
96 base::FieldTrial::Probability total_probability
= 0;
97 bool all_assignments_to_one_group
= false;
98 if (!ValidateStudyAndComputeTotalProbability(*study
, &total_probability
,
99 &all_assignments_to_one_group
)) {
104 is_expired_
= is_expired
;
105 total_probability_
= total_probability
;
106 all_assignments_to_one_group_
= all_assignments_to_one_group
;
110 int ProcessedStudy::GetExperimentIndexByName(const std::string
& name
) const {
111 for (int i
= 0; i
< study_
->experiment_size(); ++i
) {
112 if (study_
->experiment(i
).name() == name
)
120 bool ProcessedStudy::ValidateAndAppendStudy(
123 std::vector
<ProcessedStudy
>* processed_studies
) {
124 ProcessedStudy processed_study
;
125 if (processed_study
.Init(study
, is_expired
)) {
126 processed_studies
->push_back(processed_study
);
132 } // namespace variations