Fix infinite recursion on hiding panel when created during fullscreen mode.
[chromium-blink-merge.git] / components / variations / processed_study.cc
blob9d0e4f14d512dbda40a6fda36a672a178e1257b0
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>
9 #include "base/version.h"
10 #include "components/variations/proto/study.pb.h"
12 namespace chrome_variations {
14 namespace {
16 // Validates the sanity of |study| and computes the total probability.
17 bool ValidateStudyAndComputeTotalProbability(
18 const Study& study,
19 base::FieldTrial::Probability* total_probability) {
20 // At the moment, a missing default_experiment_name makes the study invalid.
21 if (study.default_experiment_name().empty()) {
22 DVLOG(1) << study.name() << " has no default experiment defined.";
23 return false;
25 if (study.filter().has_min_version() &&
26 !Version::IsValidWildcardString(study.filter().min_version())) {
27 DVLOG(1) << study.name() << " has invalid min version: "
28 << study.filter().min_version();
29 return false;
31 if (study.filter().has_max_version() &&
32 !Version::IsValidWildcardString(study.filter().max_version())) {
33 DVLOG(1) << study.name() << " has invalid max version: "
34 << study.filter().max_version();
35 return false;
38 const std::string& default_group_name = study.default_experiment_name();
39 base::FieldTrial::Probability divisor = 0;
41 bool found_default_group = false;
42 std::set<std::string> experiment_names;
43 for (int i = 0; i < study.experiment_size(); ++i) {
44 if (study.experiment(i).name().empty()) {
45 DVLOG(1) << study.name() << " is missing experiment " << i << " name";
46 return false;
48 if (!experiment_names.insert(study.experiment(i).name()).second) {
49 DVLOG(1) << study.name() << " has a repeated experiment name "
50 << study.experiment(i).name();
51 return false;
54 if (!study.experiment(i).has_forcing_flag())
55 divisor += study.experiment(i).probability_weight();
56 if (study.experiment(i).name() == default_group_name)
57 found_default_group = true;
60 if (!found_default_group) {
61 DVLOG(1) << study.name() << " is missing default experiment in its "
62 << "experiment list";
63 // The default group was not found in the list of groups. This study is not
64 // valid.
65 return false;
68 *total_probability = divisor;
69 return true;
73 } // namespace
76 ProcessedStudy::ProcessedStudy()
77 : study_(NULL), total_probability_(0), is_expired_(false) {
80 ProcessedStudy::~ProcessedStudy() {
83 bool ProcessedStudy::Init(const Study* study, bool is_expired) {
84 base::FieldTrial::Probability total_probability = 0;
85 if (!ValidateStudyAndComputeTotalProbability(*study, &total_probability))
86 return false;
88 study_ = study;
89 is_expired_ = is_expired;
90 total_probability_ = total_probability;
91 return true;
94 // static
95 bool ProcessedStudy::ValidateAndAppendStudy(
96 const Study* study,
97 bool is_expired,
98 std::vector<ProcessedStudy>* processed_studies) {
99 ProcessedStudy processed_study;
100 if (processed_study.Init(study, is_expired)) {
101 processed_studies->push_back(processed_study);
102 return true;
104 return false;
107 } // namespace chrome_variations