OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "components/variations/processed_study.h" | 5 #include "components/variations/processed_study.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/version.h" | 10 #include "base/version.h" |
11 #include "components/variations/proto/study.pb.h" | 11 #include "components/variations/proto/study.pb.h" |
12 | 12 |
13 namespace variations { | 13 namespace variations { |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 // Validates the sanity of |study| and computes the total probability and | 17 // Validates the sanity of |study| and computes the total probability and |
18 // whether all assignments are to a single group. | 18 // whether all assignments are to a single group. |
19 bool ValidateStudyAndComputeTotalProbability( | 19 bool ValidateStudyAndComputeTotalProbability( |
20 const Study& study, | 20 const Study& study, |
21 base::FieldTrial::Probability* total_probability, | 21 base::FieldTrial::Probability* total_probability, |
22 bool* all_assignments_to_one_group, | 22 bool* all_assignments_to_one_group, |
23 std::string* single_feature_name) { | 23 std::string* single_feature_name) { |
| 24 // At the moment, a missing default_experiment_name makes the study invalid. |
| 25 if (study.default_experiment_name().empty()) { |
| 26 DVLOG(1) << study.name() << " has no default experiment defined."; |
| 27 return false; |
| 28 } |
24 if (study.filter().has_min_version() && | 29 if (study.filter().has_min_version() && |
25 !base::Version::IsValidWildcardString(study.filter().min_version())) { | 30 !base::Version::IsValidWildcardString(study.filter().min_version())) { |
26 DVLOG(1) << study.name() << " has invalid min version: " | 31 DVLOG(1) << study.name() << " has invalid min version: " |
27 << study.filter().min_version(); | 32 << study.filter().min_version(); |
28 return false; | 33 return false; |
29 } | 34 } |
30 if (study.filter().has_max_version() && | 35 if (study.filter().has_max_version() && |
31 !base::Version::IsValidWildcardString(study.filter().max_version())) { | 36 !base::Version::IsValidWildcardString(study.filter().max_version())) { |
32 DVLOG(1) << study.name() << " has invalid max version: " | 37 DVLOG(1) << study.name() << " has invalid max version: " |
33 << study.filter().max_version(); | 38 << study.filter().max_version(); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 if (!experiment.has_forcing_flag() && experiment.probability_weight() > 0) { | 85 if (!experiment.has_forcing_flag() && experiment.probability_weight() > 0) { |
81 // If |divisor| is not 0, there was at least one prior non-zero group. | 86 // If |divisor| is not 0, there was at least one prior non-zero group. |
82 if (divisor != 0) | 87 if (divisor != 0) |
83 multiple_assigned_groups = true; | 88 multiple_assigned_groups = true; |
84 divisor += experiment.probability_weight(); | 89 divisor += experiment.probability_weight(); |
85 } | 90 } |
86 if (study.experiment(i).name() == default_group_name) | 91 if (study.experiment(i).name() == default_group_name) |
87 found_default_group = true; | 92 found_default_group = true; |
88 } | 93 } |
89 | 94 |
90 // Specifying a default experiment is optional, so finding it in the | 95 if (!found_default_group) { |
91 // experiment list is only required when it is specified. | 96 DVLOG(1) << study.name() << " is missing default experiment in its " |
92 if (!study.default_experiment_name().empty() && !found_default_group) { | 97 << "experiment list"; |
93 DVLOG(1) << study.name() << " is missing default experiment (" | |
94 << study.default_experiment_name() << ") in its experiment list"; | |
95 // The default group was not found in the list of groups. This study is not | 98 // The default group was not found in the list of groups. This study is not |
96 // valid. | 99 // valid. |
97 return false; | 100 return false; |
98 } | 101 } |
99 | 102 |
100 if (!has_multiple_features && !single_feature_name_seen.empty()) | 103 if (!has_multiple_features && !single_feature_name_seen.empty()) |
101 single_feature_name->swap(single_feature_name_seen); | 104 single_feature_name->swap(single_feature_name_seen); |
102 else | 105 else |
103 single_feature_name->clear(); | 106 single_feature_name->clear(); |
104 | 107 |
105 *total_probability = divisor; | 108 *total_probability = divisor; |
106 *all_assignments_to_one_group = !multiple_assigned_groups; | 109 *all_assignments_to_one_group = !multiple_assigned_groups; |
107 return true; | 110 return true; |
108 } | 111 } |
109 | 112 |
110 | 113 |
111 } // namespace | 114 } // namespace |
112 | 115 |
113 // static | |
114 const std::string ProcessedStudy::kGenericDefaultExperimentName = | |
115 "VariationsDefaultExperiment"; | |
116 | |
117 ProcessedStudy::ProcessedStudy() | 116 ProcessedStudy::ProcessedStudy() |
118 : study_(NULL), | 117 : study_(NULL), |
119 total_probability_(0), | 118 total_probability_(0), |
120 all_assignments_to_one_group_(false), | 119 all_assignments_to_one_group_(false), |
121 is_expired_(false) { | 120 is_expired_(false) { |
122 } | 121 } |
123 | 122 |
124 ProcessedStudy::~ProcessedStudy() { | 123 ProcessedStudy::~ProcessedStudy() { |
125 } | 124 } |
126 | 125 |
(...skipping 17 matching lines...) Expand all Loading... |
144 | 143 |
145 int ProcessedStudy::GetExperimentIndexByName(const std::string& name) const { | 144 int ProcessedStudy::GetExperimentIndexByName(const std::string& name) const { |
146 for (int i = 0; i < study_->experiment_size(); ++i) { | 145 for (int i = 0; i < study_->experiment_size(); ++i) { |
147 if (study_->experiment(i).name() == name) | 146 if (study_->experiment(i).name() == name) |
148 return i; | 147 return i; |
149 } | 148 } |
150 | 149 |
151 return -1; | 150 return -1; |
152 } | 151 } |
153 | 152 |
154 const std::string& ProcessedStudy::GetDefaultExperimentName() const { | |
155 if (study_->default_experiment_name().empty()) | |
156 return kGenericDefaultExperimentName; | |
157 | |
158 return study_->default_experiment_name(); | |
159 } | |
160 | |
161 // static | 153 // static |
162 bool ProcessedStudy::ValidateAndAppendStudy( | 154 bool ProcessedStudy::ValidateAndAppendStudy( |
163 const Study* study, | 155 const Study* study, |
164 bool is_expired, | 156 bool is_expired, |
165 std::vector<ProcessedStudy>* processed_studies) { | 157 std::vector<ProcessedStudy>* processed_studies) { |
166 ProcessedStudy processed_study; | 158 ProcessedStudy processed_study; |
167 if (processed_study.Init(study, is_expired)) { | 159 if (processed_study.Init(study, is_expired)) { |
168 processed_studies->push_back(processed_study); | 160 processed_studies->push_back(processed_study); |
169 return true; | 161 return true; |
170 } | 162 } |
171 return false; | 163 return false; |
172 } | 164 } |
173 | 165 |
174 } // namespace variations | 166 } // namespace variations |
OLD | NEW |