OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "chrome/common/variations/variations_util.h" | 5 #include "chrome/common/variations/variations_util.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/feature_list.h" | 12 #include "base/feature_list.h" |
13 #include "base/metrics/field_trial.h" | 13 #include "base/metrics/field_trial.h" |
14 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
16 #include "chrome/common/variations/fieldtrial_testing_config.h" | 16 #include "chrome/common/variations/fieldtrial_testing_config.h" |
17 #include "components/variations/variations_associated_data.h" | 17 #include "components/variations/variations_associated_data.h" |
18 #include "net/base/escape.h" | 18 #include "net/base/escape.h" |
19 | 19 |
20 namespace chrome_variations { | 20 namespace chrome_variations { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 std::string EscapeValue(const std::string& value) { | 24 std::string EscapeValue(const std::string& value) { |
25 return net::UnescapeURLComponent( | 25 return net::UnescapeURLComponent( |
26 value, net::UnescapeRule::PATH_SEPARATORS | | 26 value, net::UnescapeRule::PATH_SEPARATORS | |
27 net::UnescapeRule::URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS); | 27 net::UnescapeRule::URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS); |
28 } | 28 } |
29 | 29 |
| 30 void AssociateParamsFromGroup(const std::string& study_name, |
| 31 const FieldTrialTestingStudyGroup& group, |
| 32 base::FeatureList* feature_list) { |
| 33 if (group.params_size != 0) { |
| 34 std::map<std::string, std::string> params; |
| 35 for (size_t i = 0; i < group.params_size; ++i) { |
| 36 const FieldTrialTestingStudyGroupsParams& param = group.params[i]; |
| 37 params[param.key] = param.value; |
| 38 } |
| 39 variations::AssociateVariationParams(study_name, group.name, |
| 40 params); |
| 41 } |
| 42 base::FieldTrial* trial = |
| 43 base::FieldTrialList::CreateFieldTrial(study_name, group.name); |
| 44 |
| 45 if (!trial) { |
| 46 DLOG(WARNING) << "Field trial config study skipped: " << study_name |
| 47 << "." << group.name |
| 48 << " (it is overridden from chrome://flags)"; |
| 49 return; |
| 50 } |
| 51 |
| 52 for (size_t i = 0; i < group.enable_features_size; ++i) { |
| 53 feature_list->RegisterFieldTrialOverride( |
| 54 group.enable_features[i], base::FeatureList::OVERRIDE_ENABLE_FEATURE, |
| 55 trial); |
| 56 } |
| 57 for (size_t i = 0; i < group.disable_features_size; ++i) { |
| 58 feature_list->RegisterFieldTrialOverride( |
| 59 group.disable_features[i], |
| 60 base::FeatureList::OVERRIDE_DISABLE_FEATURE, trial); |
| 61 } |
| 62 } |
| 63 |
30 } // namespace | 64 } // namespace |
31 | 65 |
32 bool AssociateParamsFromString(const std::string& varations_string) { | 66 bool AssociateParamsFromString(const std::string& varations_string) { |
33 // Format: Trial1.Group1:k1/v1/k2/v2,Trial2.Group2:k1/v1/k2/v2 | 67 // Format: Trial1.Group1:k1/v1/k2/v2,Trial2.Group2:k1/v1/k2/v2 |
34 std::set<std::pair<std::string, std::string>> trial_groups; | 68 std::set<std::pair<std::string, std::string>> trial_groups; |
35 for (const base::StringPiece& experiment_group : base::SplitStringPiece( | 69 for (const base::StringPiece& experiment_group : base::SplitStringPiece( |
36 varations_string, ",", | 70 varations_string, ",", |
37 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { | 71 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { |
38 std::vector<base::StringPiece> experiment = base::SplitStringPiece( | 72 std::vector<base::StringPiece> experiment = base::SplitStringPiece( |
39 experiment_group, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 73 experiment_group, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 std::string value = EscapeValue(key_values[i + 1]); | 105 std::string value = EscapeValue(key_values[i + 1]); |
72 params[key] = value; | 106 params[key] = value; |
73 } | 107 } |
74 variations::AssociateVariationParams(trial, group, params); | 108 variations::AssociateVariationParams(trial, group, params); |
75 } | 109 } |
76 return true; | 110 return true; |
77 } | 111 } |
78 | 112 |
79 void AssociateParamsFromFieldTrialConfig(const FieldTrialTestingConfig& config, | 113 void AssociateParamsFromFieldTrialConfig(const FieldTrialTestingConfig& config, |
80 base::FeatureList* feature_list) { | 114 base::FeatureList* feature_list) { |
81 for (size_t i = 0; i < config.groups_size; ++i) { | 115 for (size_t i = 0; i < config.studies_size; ++i) { |
82 const FieldTrialTestingGroup& group = config.groups[i]; | 116 const FieldTrialTestingStudy& study = config.studies[i]; |
83 if (group.params_size != 0) { | 117 if (study.groups_size > 0) { |
84 std::map<std::string, std::string> params; | 118 AssociateParamsFromGroup(study.name, study.groups[0], feature_list); |
85 for (size_t j = 0; j < group.params_size; ++j) { | 119 } else { |
86 const FieldTrialGroupParams& param = group.params[j]; | 120 NOTREACHED() << "Unexpected empty study: " << study.name; |
87 params[param.key] = param.value; | |
88 } | |
89 variations::AssociateVariationParams(group.study, group.group_name, | |
90 params); | |
91 } | |
92 base::FieldTrial* trial = | |
93 base::FieldTrialList::CreateFieldTrial(group.study, group.group_name); | |
94 | |
95 if (!trial) { | |
96 DLOG(WARNING) << "Field trial config study skipped: " << group.study | |
97 << "." << group.group_name | |
98 << " (it is overridden from chrome://flags)"; | |
99 continue; | |
100 } | |
101 | |
102 for (size_t j = 0; j < group.enable_features_size; ++j) { | |
103 feature_list->RegisterFieldTrialOverride( | |
104 group.enable_features[j], base::FeatureList::OVERRIDE_ENABLE_FEATURE, | |
105 trial); | |
106 } | |
107 for (size_t j = 0; j < group.disable_features_size; ++j) { | |
108 feature_list->RegisterFieldTrialOverride( | |
109 group.disable_features[j], | |
110 base::FeatureList::OVERRIDE_DISABLE_FEATURE, trial); | |
111 } | 121 } |
112 } | 122 } |
113 } | 123 } |
114 | 124 |
115 void AssociateDefaultFieldTrialConfig(base::FeatureList* feature_list) { | 125 void AssociateDefaultFieldTrialConfig(base::FeatureList* feature_list) { |
116 AssociateParamsFromFieldTrialConfig(kFieldTrialConfig, feature_list); | 126 AssociateParamsFromFieldTrialConfig(kFieldTrialConfig, feature_list); |
117 } | 127 } |
118 | 128 |
119 } // namespace chrome_variations | 129 } // namespace chrome_variations |
OLD | NEW |