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