Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / variations / processed_study.cc
blob5f065d8e1e88ac19a6034581eb49780a775d132e
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"
7 #include <set>
8 #include <string>
10 #include "base/version.h"
11 #include "components/variations/proto/study.pb.h"
13 namespace variations {
15 namespace {
17 // Validates the sanity of |study| and computes the total probability and
18 // whether all assignments are to a single group.
19 bool ValidateStudyAndComputeTotalProbability(
20 const Study& study,
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.";
26 return false;
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();
32 return false;
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();
38 return false;
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";
51 return false;
53 if (!experiment_names.insert(experiment.name()).second) {
54 DVLOG(1) << study.name() << " has a repeated experiment name "
55 << study.experiment(i).name();
56 return false;
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.
61 if (divisor != 0)
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 "
71 << "experiment list";
72 // The default group was not found in the list of groups. This study is not
73 // valid.
74 return false;
77 *total_probability = divisor;
78 *all_assignments_to_one_group = !multiple_assigned_groups;
79 return true;
83 } // namespace
85 ProcessedStudy::ProcessedStudy()
86 : study_(NULL),
87 total_probability_(0),
88 all_assignments_to_one_group_(false),
89 is_expired_(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)) {
100 return false;
103 study_ = study;
104 is_expired_ = is_expired;
105 total_probability_ = total_probability;
106 all_assignments_to_one_group_ = all_assignments_to_one_group;
107 return true;
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)
113 return i;
116 return -1;
119 // static
120 bool ProcessedStudy::ValidateAndAppendStudy(
121 const Study* study,
122 bool is_expired,
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);
127 return true;
129 return false;
132 } // namespace variations