• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef COMPONENTS_VARIATIONS_VARIATIONS_SEED_PROCESSOR_H_
6 #define COMPONENTS_VARIATIONS_VARIATIONS_SEED_PROCESSOR_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/compiler_specific.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/metrics/field_trial.h"
14 #include "base/time/time.h"
15 #include "base/version.h"
16 #include "components/variations/proto/study.pb.h"
17 #include "components/variations/proto/variations_seed.pb.h"
18 
19 namespace chrome_variations {
20 
21 class ProcessedStudy;
22 
23 // Helper class to instantiate field trials from a variations seed.
24 class VariationsSeedProcessor {
25  public:
26   VariationsSeedProcessor();
27   virtual ~VariationsSeedProcessor();
28 
29   // Creates field trials from the specified variations |seed|, based on the
30   // specified configuration (locale, current date, version and channel).
31   void CreateTrialsFromSeed(const VariationsSeed& seed,
32                             const std::string& locale,
33                             const base::Time& reference_date,
34                             const base::Version& version,
35                             Study_Channel channel,
36                             Study_FormFactor form_factor);
37 
38  private:
39   friend class VariationsSeedProcessorTest;
40   FRIEND_TEST_ALL_PREFIXES(VariationsSeedProcessorTest,
41                            AllowForceGroupAndVariationId);
42   FRIEND_TEST_ALL_PREFIXES(VariationsSeedProcessorTest,
43                            AllowVariationIdWithForcingFlag);
44   FRIEND_TEST_ALL_PREFIXES(VariationsSeedProcessorTest, CheckStudyChannel);
45   FRIEND_TEST_ALL_PREFIXES(VariationsSeedProcessorTest, CheckStudyFormFactor);
46   FRIEND_TEST_ALL_PREFIXES(VariationsSeedProcessorTest, CheckStudyLocale);
47   FRIEND_TEST_ALL_PREFIXES(VariationsSeedProcessorTest, CheckStudyPlatform);
48   FRIEND_TEST_ALL_PREFIXES(VariationsSeedProcessorTest, CheckStudyStartDate);
49   FRIEND_TEST_ALL_PREFIXES(VariationsSeedProcessorTest, CheckStudyVersion);
50   FRIEND_TEST_ALL_PREFIXES(VariationsSeedProcessorTest,
51                            FilterAndValidateStudies);
52   FRIEND_TEST_ALL_PREFIXES(VariationsSeedProcessorTest,
53                            ForbidForceGroupWithVariationId);
54   FRIEND_TEST_ALL_PREFIXES(VariationsSeedProcessorTest, ForceGroupWithFlag1);
55   FRIEND_TEST_ALL_PREFIXES(VariationsSeedProcessorTest, ForceGroupWithFlag2);
56   FRIEND_TEST_ALL_PREFIXES(VariationsSeedProcessorTest,
57                            ForceGroup_ChooseFirstGroupWithFlag);
58   FRIEND_TEST_ALL_PREFIXES(VariationsSeedProcessorTest,
59                            ForceGroup_DontChooseGroupWithFlag);
60   FRIEND_TEST_ALL_PREFIXES(VariationsSeedProcessorTest, IsStudyExpired);
61   FRIEND_TEST_ALL_PREFIXES(VariationsSeedProcessorTest, ValidateStudy);
62   FRIEND_TEST_ALL_PREFIXES(VariationsSeedProcessorTest, VariationParams);
63   FRIEND_TEST_ALL_PREFIXES(VariationsSeedProcessorTest,
64                            VariationParamsWithForcingFlag);
65 
66   // Check if the |study| is only associated with platform Android/iOS and
67   // channel dev/canary. If so, forcing flag and variation id can both be set.
68   // (Otherwise, forcing_flag and variation_id are mutually exclusive.)
69   bool AllowVariationIdWithForcingFlag(const Study& study);
70 
71   // Filters the list of studies in |seed| and validates and pre-processes them,
72   // adding any kept studies to |filtered_studies| list. Ensures that the
73   // resulting list will not have more than one study with the same name.
74   void FilterAndValidateStudies(const VariationsSeed& seed,
75                                 const std::string& locale,
76                                 const base::Time& reference_date,
77                                 const base::Version& version,
78                                 Study_Channel channel,
79                                 Study_FormFactor form_factor,
80                                 std::vector<ProcessedStudy>* filtered_studies);
81 
82   // Validates |study| and if valid, adds it to |filtered_studies| as a
83   // ProcessedStudy object.
84   void ValidateAndAddStudy(const Study& study,
85                            bool is_expired,
86                            std::vector<ProcessedStudy>* filtered_studies);
87 
88   // Checks whether a study is applicable for the given |channel| per |filter|.
89   bool CheckStudyChannel(const Study_Filter& filter, Study_Channel channel);
90 
91   // Checks whether a study is applicable for the given |form_factor| per
92   // |filter|.
93   bool CheckStudyFormFactor(const Study_Filter& filter,
94                             Study_FormFactor form_factor);
95 
96   // Checks whether a study is applicable for the given |locale| per |filter|.
97   bool CheckStudyLocale(const Study_Filter& filter, const std::string& locale);
98 
99   // Checks whether a study is applicable for the given |platform| per |filter|.
100   bool CheckStudyPlatform(const Study_Filter& filter, Study_Platform platform);
101 
102   // Checks whether a study is applicable for the given date/time per |filter|.
103   bool CheckStudyStartDate(const Study_Filter& filter,
104                            const base::Time& date_time);
105 
106   // Checks whether a study is applicable for the given version per |filter|.
107   bool CheckStudyVersion(const Study_Filter& filter,
108                          const base::Version& version);
109 
110   // Creates and registers a field trial from the |processed_study| data.
111   // Disables the trial if |processed_study.is_expired| is true.
112   void CreateTrialFromStudy(const ProcessedStudy& processed_study);
113 
114   // Checks whether |study| is expired using the given date/time.
115   bool IsStudyExpired(const Study& study, const base::Time& date_time);
116 
117   // Returns whether |study| should be disabled according to its restriction
118   // parameters. Uses |version_info| for min / max version checks,
119   // |reference_date| for the start date check and |channel| for channel
120   // checks.
121   bool ShouldAddStudy(const Study& study,
122                       const std::string& locale,
123                       const base::Time& reference_date,
124                       const base::Version& version,
125                       Study_Channel channel,
126                       Study_FormFactor form_factor);
127 
128   // Validates the sanity of |study| and computes the total probability.
129   bool ValidateStudyAndComputeTotalProbability(
130       const Study& study,
131       base::FieldTrial::Probability* total_probability);
132 
133   DISALLOW_COPY_AND_ASSIGN(VariationsSeedProcessor);
134 };
135 
136 }  // namespace chrome_variations
137 
138 #endif  // COMPONENTS_VARIATIONS_VARIATIONS_SEED_PROCESSOR_H_
139