| 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. | 17 // Validates the sanity of |study| and computes the total probability and |
| 18 // whether all assignments are to a single group. |
| 18 bool ValidateStudyAndComputeTotalProbability( | 19 bool ValidateStudyAndComputeTotalProbability( |
| 19 const Study& study, | 20 const Study& study, |
| 20 base::FieldTrial::Probability* total_probability) { | 21 base::FieldTrial::Probability* total_probability, |
| 22 bool* all_assignments_to_one_group) { |
| 21 // At the moment, a missing default_experiment_name makes the study invalid. | 23 // At the moment, a missing default_experiment_name makes the study invalid. |
| 22 if (study.default_experiment_name().empty()) { | 24 if (study.default_experiment_name().empty()) { |
| 23 DVLOG(1) << study.name() << " has no default experiment defined."; | 25 DVLOG(1) << study.name() << " has no default experiment defined."; |
| 24 return false; | 26 return false; |
| 25 } | 27 } |
| 26 if (study.filter().has_min_version() && | 28 if (study.filter().has_min_version() && |
| 27 !Version::IsValidWildcardString(study.filter().min_version())) { | 29 !Version::IsValidWildcardString(study.filter().min_version())) { |
| 28 DVLOG(1) << study.name() << " has invalid min version: " | 30 DVLOG(1) << study.name() << " has invalid min version: " |
| 29 << study.filter().min_version(); | 31 << study.filter().min_version(); |
| 30 return false; | 32 return false; |
| 31 } | 33 } |
| 32 if (study.filter().has_max_version() && | 34 if (study.filter().has_max_version() && |
| 33 !Version::IsValidWildcardString(study.filter().max_version())) { | 35 !Version::IsValidWildcardString(study.filter().max_version())) { |
| 34 DVLOG(1) << study.name() << " has invalid max version: " | 36 DVLOG(1) << study.name() << " has invalid max version: " |
| 35 << study.filter().max_version(); | 37 << study.filter().max_version(); |
| 36 return false; | 38 return false; |
| 37 } | 39 } |
| 38 | 40 |
| 39 const std::string& default_group_name = study.default_experiment_name(); | 41 const std::string& default_group_name = study.default_experiment_name(); |
| 40 base::FieldTrial::Probability divisor = 0; | 42 base::FieldTrial::Probability divisor = 0; |
| 41 | 43 |
| 44 bool multiple_assigned_groups = false; |
| 42 bool found_default_group = false; | 45 bool found_default_group = false; |
| 43 std::set<std::string> experiment_names; | 46 std::set<std::string> experiment_names; |
| 44 for (int i = 0; i < study.experiment_size(); ++i) { | 47 for (int i = 0; i < study.experiment_size(); ++i) { |
| 45 if (study.experiment(i).name().empty()) { | 48 const Study_Experiment& experiment = study.experiment(i); |
| 49 if (experiment.name().empty()) { |
| 46 DVLOG(1) << study.name() << " is missing experiment " << i << " name"; | 50 DVLOG(1) << study.name() << " is missing experiment " << i << " name"; |
| 47 return false; | 51 return false; |
| 48 } | 52 } |
| 49 if (!experiment_names.insert(study.experiment(i).name()).second) { | 53 if (!experiment_names.insert(experiment.name()).second) { |
| 50 DVLOG(1) << study.name() << " has a repeated experiment name " | 54 DVLOG(1) << study.name() << " has a repeated experiment name " |
| 51 << study.experiment(i).name(); | 55 << study.experiment(i).name(); |
| 52 return false; | 56 return false; |
| 53 } | 57 } |
| 54 | 58 |
| 55 if (!study.experiment(i).has_forcing_flag()) | 59 if (!experiment.has_forcing_flag() && experiment.probability_weight() > 0) { |
| 56 divisor += study.experiment(i).probability_weight(); | 60 // If |divisor| is not 0, there was at least one prior non-zero group. |
| 61 if (divisor != 0) |
| 62 multiple_assigned_groups = true; |
| 63 divisor += experiment.probability_weight(); |
| 64 } |
| 57 if (study.experiment(i).name() == default_group_name) | 65 if (study.experiment(i).name() == default_group_name) |
| 58 found_default_group = true; | 66 found_default_group = true; |
| 59 } | 67 } |
| 60 | 68 |
| 61 if (!found_default_group) { | 69 if (!found_default_group) { |
| 62 DVLOG(1) << study.name() << " is missing default experiment in its " | 70 DVLOG(1) << study.name() << " is missing default experiment in its " |
| 63 << "experiment list"; | 71 << "experiment list"; |
| 64 // The default group was not found in the list of groups. This study is not | 72 // The default group was not found in the list of groups. This study is not |
| 65 // valid. | 73 // valid. |
| 66 return false; | 74 return false; |
| 67 } | 75 } |
| 68 | 76 |
| 69 *total_probability = divisor; | 77 *total_probability = divisor; |
| 78 *all_assignments_to_one_group = !multiple_assigned_groups; |
| 70 return true; | 79 return true; |
| 71 } | 80 } |
| 72 | 81 |
| 73 | 82 |
| 74 } // namespace | 83 } // namespace |
| 75 | 84 |
| 76 | |
| 77 ProcessedStudy::ProcessedStudy() | 85 ProcessedStudy::ProcessedStudy() |
| 78 : study_(NULL), total_probability_(0), is_expired_(false) { | 86 : study_(NULL), |
| 87 total_probability_(0), |
| 88 all_assignments_to_one_group_(false), |
| 89 is_expired_(false) { |
| 79 } | 90 } |
| 80 | 91 |
| 81 ProcessedStudy::~ProcessedStudy() { | 92 ProcessedStudy::~ProcessedStudy() { |
| 82 } | 93 } |
| 83 | 94 |
| 84 bool ProcessedStudy::Init(const Study* study, bool is_expired) { | 95 bool ProcessedStudy::Init(const Study* study, bool is_expired) { |
| 85 base::FieldTrial::Probability total_probability = 0; | 96 base::FieldTrial::Probability total_probability = 0; |
| 86 if (!ValidateStudyAndComputeTotalProbability(*study, &total_probability)) | 97 bool all_assignments_to_one_group = false; |
| 98 if (!ValidateStudyAndComputeTotalProbability(*study, &total_probability, |
| 99 &all_assignments_to_one_group)) { |
| 87 return false; | 100 return false; |
| 101 } |
| 88 | 102 |
| 89 study_ = study; | 103 study_ = study; |
| 90 is_expired_ = is_expired; | 104 is_expired_ = is_expired; |
| 91 total_probability_ = total_probability; | 105 total_probability_ = total_probability; |
| 106 all_assignments_to_one_group_ = all_assignments_to_one_group; |
| 92 return true; | 107 return true; |
| 93 } | 108 } |
| 94 | 109 |
| 95 int ProcessedStudy::GetExperimentIndexByName(const std::string& name) const { | 110 int ProcessedStudy::GetExperimentIndexByName(const std::string& name) const { |
| 96 for (int i = 0; i < study_->experiment_size(); ++i) { | 111 for (int i = 0; i < study_->experiment_size(); ++i) { |
| 97 if (study_->experiment(i).name() == name) | 112 if (study_->experiment(i).name() == name) |
| 98 return i; | 113 return i; |
| 99 } | 114 } |
| 100 | 115 |
| 101 return -1; | 116 return -1; |
| 102 } | 117 } |
| 103 | 118 |
| 104 // static | 119 // static |
| 105 bool ProcessedStudy::ValidateAndAppendStudy( | 120 bool ProcessedStudy::ValidateAndAppendStudy( |
| 106 const Study* study, | 121 const Study* study, |
| 107 bool is_expired, | 122 bool is_expired, |
| 108 std::vector<ProcessedStudy>* processed_studies) { | 123 std::vector<ProcessedStudy>* processed_studies) { |
| 109 ProcessedStudy processed_study; | 124 ProcessedStudy processed_study; |
| 110 if (processed_study.Init(study, is_expired)) { | 125 if (processed_study.Init(study, is_expired)) { |
| 111 processed_studies->push_back(processed_study); | 126 processed_studies->push_back(processed_study); |
| 112 return true; | 127 return true; |
| 113 } | 128 } |
| 114 return false; | 129 return false; |
| 115 } | 130 } |
| 116 | 131 |
| 117 } // namespace variations | 132 } // namespace variations |
| OLD | NEW |