Home | History | Annotate | Download | only in variations
      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.
      4 
      5 #include "components/variations/processed_study.h"
      6 
      7 #include <set>
      8 
      9 #include "base/version.h"
     10 #include "components/variations/proto/study.pb.h"
     11 
     12 namespace chrome_variations {
     13 
     14 namespace {
     15 
     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;
     24   }
     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;
     30   }
     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;
     36   }
     37 
     38   const std::string& default_group_name = study.default_experiment_name();
     39   base::FieldTrial::Probability divisor = 0;
     40 
     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;
     47     }
     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;
     52     }
     53 
     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;
     58   }
     59 
     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;
     66   }
     67 
     68   *total_probability = divisor;
     69   return true;
     70 }
     71 
     72 
     73 }  // namespace
     74 
     75 
     76 ProcessedStudy::ProcessedStudy()
     77     : study_(NULL), total_probability_(0), is_expired_(false) {
     78 }
     79 
     80 ProcessedStudy::~ProcessedStudy() {
     81 }
     82 
     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;
     87 
     88   study_ = study;
     89   is_expired_ = is_expired;
     90   total_probability_ = total_probability;
     91   return true;
     92 }
     93 
     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;
    103   }
    104   return false;
    105 }
    106 
    107 }  // namespace chrome_variations
    108