| 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 // At the moment, a missing default_experiment_name makes the study invalid. | 24 // At the moment, a missing default_experiment_name makes the study invalid. |
| 24 if (study.default_experiment_name().empty()) { | 25 if (study.default_experiment_name().empty()) { |
| 25 DVLOG(1) << study.name() << " has no default experiment defined."; | 26 DVLOG(1) << study.name() << " has no default experiment defined."; |
| 26 return false; | 27 return false; |
| 27 } | 28 } |
| 28 if (study.filter().has_min_version() && | 29 if (study.filter().has_min_version() && |
| 29 !Version::IsValidWildcardString(study.filter().min_version())) { | 30 !Version::IsValidWildcardString(study.filter().min_version())) { |
| 30 DVLOG(1) << study.name() << " has invalid min version: " | 31 DVLOG(1) << study.name() << " has invalid min version: " |
| 31 << study.filter().min_version(); | 32 << study.filter().min_version(); |
| 32 return false; | 33 return false; |
| 33 } | 34 } |
| 34 if (study.filter().has_max_version() && | 35 if (study.filter().has_max_version() && |
| 35 !Version::IsValidWildcardString(study.filter().max_version())) { | 36 !Version::IsValidWildcardString(study.filter().max_version())) { |
| 36 DVLOG(1) << study.name() << " has invalid max version: " | 37 DVLOG(1) << study.name() << " has invalid max version: " |
| 37 << study.filter().max_version(); | 38 << study.filter().max_version(); |
| 38 return false; | 39 return false; |
| 39 } | 40 } |
| 40 | 41 |
| 41 const std::string& default_group_name = study.default_experiment_name(); | 42 const std::string& default_group_name = study.default_experiment_name(); |
| 42 base::FieldTrial::Probability divisor = 0; | 43 base::FieldTrial::Probability divisor = 0; |
| 43 | 44 |
| 44 bool multiple_assigned_groups = false; | 45 bool multiple_assigned_groups = false; |
| 45 bool found_default_group = false; | 46 bool found_default_group = false; |
| 47 std::string single_feature_name_seen; |
| 48 bool has_multiple_features = false; |
| 49 |
| 46 std::set<std::string> experiment_names; | 50 std::set<std::string> experiment_names; |
| 47 for (int i = 0; i < study.experiment_size(); ++i) { | 51 for (int i = 0; i < study.experiment_size(); ++i) { |
| 48 const Study_Experiment& experiment = study.experiment(i); | 52 const Study_Experiment& experiment = study.experiment(i); |
| 49 if (experiment.name().empty()) { | 53 if (experiment.name().empty()) { |
| 50 DVLOG(1) << study.name() << " is missing experiment " << i << " name"; | 54 DVLOG(1) << study.name() << " is missing experiment " << i << " name"; |
| 51 return false; | 55 return false; |
| 52 } | 56 } |
| 53 if (!experiment_names.insert(experiment.name()).second) { | 57 if (!experiment_names.insert(experiment.name()).second) { |
| 54 DVLOG(1) << study.name() << " has a repeated experiment name " | 58 DVLOG(1) << study.name() << " has a repeated experiment name " |
| 55 << study.experiment(i).name(); | 59 << study.experiment(i).name(); |
| 56 return false; | 60 return false; |
| 57 } | 61 } |
| 58 | 62 |
| 63 if (!has_multiple_features) { |
| 64 const auto& features = experiment.feature_association(); |
| 65 for (int i = 0; i < features.enable_feature_size(); ++i) { |
| 66 const std::string& feature_name = features.enable_feature(i); |
| 67 if (single_feature_name_seen.empty()) { |
| 68 single_feature_name_seen = feature_name; |
| 69 } else if (feature_name != single_feature_name_seen) { |
| 70 has_multiple_features = true; |
| 71 break; |
| 72 } |
| 73 } |
| 74 for (int i = 0; i < features.disable_feature_size(); ++i) { |
| 75 const std::string& feature_name = features.disable_feature(i); |
| 76 if (single_feature_name_seen.empty()) { |
| 77 single_feature_name_seen = feature_name; |
| 78 } else if (feature_name != single_feature_name_seen) { |
| 79 has_multiple_features = true; |
| 80 break; |
| 81 } |
| 82 } |
| 83 } |
| 84 |
| 59 if (!experiment.has_forcing_flag() && experiment.probability_weight() > 0) { | 85 if (!experiment.has_forcing_flag() && experiment.probability_weight() > 0) { |
| 60 // 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. |
| 61 if (divisor != 0) | 87 if (divisor != 0) |
| 62 multiple_assigned_groups = true; | 88 multiple_assigned_groups = true; |
| 63 divisor += experiment.probability_weight(); | 89 divisor += experiment.probability_weight(); |
| 64 } | 90 } |
| 65 if (study.experiment(i).name() == default_group_name) | 91 if (study.experiment(i).name() == default_group_name) |
| 66 found_default_group = true; | 92 found_default_group = true; |
| 67 } | 93 } |
| 68 | 94 |
| 69 if (!found_default_group) { | 95 if (!found_default_group) { |
| 70 DVLOG(1) << study.name() << " is missing default experiment in its " | 96 DVLOG(1) << study.name() << " is missing default experiment in its " |
| 71 << "experiment list"; | 97 << "experiment list"; |
| 72 // 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 |
| 73 // valid. | 99 // valid. |
| 74 return false; | 100 return false; |
| 75 } | 101 } |
| 76 | 102 |
| 103 if (!has_multiple_features && !single_feature_name_seen.empty()) |
| 104 single_feature_name->swap(single_feature_name_seen); |
| 105 else |
| 106 single_feature_name->clear(); |
| 107 |
| 77 *total_probability = divisor; | 108 *total_probability = divisor; |
| 78 *all_assignments_to_one_group = !multiple_assigned_groups; | 109 *all_assignments_to_one_group = !multiple_assigned_groups; |
| 79 return true; | 110 return true; |
| 80 } | 111 } |
| 81 | 112 |
| 82 | 113 |
| 83 } // namespace | 114 } // namespace |
| 84 | 115 |
| 85 ProcessedStudy::ProcessedStudy() | 116 ProcessedStudy::ProcessedStudy() |
| 86 : study_(NULL), | 117 : study_(NULL), |
| 87 total_probability_(0), | 118 total_probability_(0), |
| 88 all_assignments_to_one_group_(false), | 119 all_assignments_to_one_group_(false), |
| 89 is_expired_(false) { | 120 is_expired_(false) { |
| 90 } | 121 } |
| 91 | 122 |
| 92 ProcessedStudy::~ProcessedStudy() { | 123 ProcessedStudy::~ProcessedStudy() { |
| 93 } | 124 } |
| 94 | 125 |
| 95 bool ProcessedStudy::Init(const Study* study, bool is_expired) { | 126 bool ProcessedStudy::Init(const Study* study, bool is_expired) { |
| 96 base::FieldTrial::Probability total_probability = 0; | 127 base::FieldTrial::Probability total_probability = 0; |
| 97 bool all_assignments_to_one_group = false; | 128 bool all_assignments_to_one_group = false; |
| 129 std::string single_feature_name; |
| 98 if (!ValidateStudyAndComputeTotalProbability(*study, &total_probability, | 130 if (!ValidateStudyAndComputeTotalProbability(*study, &total_probability, |
| 99 &all_assignments_to_one_group)) { | 131 &all_assignments_to_one_group, |
| 132 &single_feature_name)) { |
| 100 return false; | 133 return false; |
| 101 } | 134 } |
| 102 | 135 |
| 103 study_ = study; | 136 study_ = study; |
| 104 is_expired_ = is_expired; | 137 is_expired_ = is_expired; |
| 105 total_probability_ = total_probability; | 138 total_probability_ = total_probability; |
| 106 all_assignments_to_one_group_ = all_assignments_to_one_group; | 139 all_assignments_to_one_group_ = all_assignments_to_one_group; |
| 140 single_feature_name_.swap(single_feature_name); |
| 107 return true; | 141 return true; |
| 108 } | 142 } |
| 109 | 143 |
| 110 int ProcessedStudy::GetExperimentIndexByName(const std::string& name) const { | 144 int ProcessedStudy::GetExperimentIndexByName(const std::string& name) const { |
| 111 for (int i = 0; i < study_->experiment_size(); ++i) { | 145 for (int i = 0; i < study_->experiment_size(); ++i) { |
| 112 if (study_->experiment(i).name() == name) | 146 if (study_->experiment(i).name() == name) |
| 113 return i; | 147 return i; |
| 114 } | 148 } |
| 115 | 149 |
| 116 return -1; | 150 return -1; |
| 117 } | 151 } |
| 118 | 152 |
| 119 // static | 153 // static |
| 120 bool ProcessedStudy::ValidateAndAppendStudy( | 154 bool ProcessedStudy::ValidateAndAppendStudy( |
| 121 const Study* study, | 155 const Study* study, |
| 122 bool is_expired, | 156 bool is_expired, |
| 123 std::vector<ProcessedStudy>* processed_studies) { | 157 std::vector<ProcessedStudy>* processed_studies) { |
| 124 ProcessedStudy processed_study; | 158 ProcessedStudy processed_study; |
| 125 if (processed_study.Init(study, is_expired)) { | 159 if (processed_study.Init(study, is_expired)) { |
| 126 processed_studies->push_back(processed_study); | 160 processed_studies->push_back(processed_study); |
| 127 return true; | 161 return true; |
| 128 } | 162 } |
| 129 return false; | 163 return false; |
| 130 } | 164 } |
| 131 | 165 |
| 132 } // namespace variations | 166 } // namespace variations |
| OLD | NEW |