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 "chrome/common/variations/fieldtrial_testing_config.h" | 16 #include "chrome/common/variations/fieldtrial_testing_config.h" |
16 #include "components/variations/variations_associated_data.h" | 17 #include "components/variations/variations_associated_data.h" |
17 #include "net/base/escape.h" | 18 #include "net/base/escape.h" |
18 | 19 |
19 namespace chrome_variations { | 20 namespace chrome_variations { |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 std::string EscapeValue(const std::string& value) { | 24 std::string EscapeValue(const std::string& value) { |
24 return net::UnescapeURLComponent(value, net::UnescapeRule::URL_SPECIAL_CHARS); | 25 return net::UnescapeURLComponent(value, net::UnescapeRule::URL_SPECIAL_CHARS); |
25 } | 26 } |
26 | 27 |
27 } // namespace | 28 } // namespace |
28 | 29 |
29 bool AssociateParamsFromString(const std::string& varations_string) { | 30 bool AssociateParamsFromString(const std::string& varations_string) { |
30 // Format: Trial1.Group1:k1/v1/k2/v2,Trial2.Group2:k1/v1/k2/v2 | 31 // Format: Trial1.Group1:k1/v1/k2/v2,Trial2.Group2:k1/v1/k2/v2 |
| 32 std::set<std::pair<std::string, std::string>> trial_groups; |
31 for (const base::StringPiece& experiment_group : base::SplitStringPiece( | 33 for (const base::StringPiece& experiment_group : base::SplitStringPiece( |
32 varations_string, ",", | 34 varations_string, ",", |
33 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { | 35 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { |
34 std::vector<base::StringPiece> experiment = base::SplitStringPiece( | 36 std::vector<base::StringPiece> experiment = base::SplitStringPiece( |
35 experiment_group, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 37 experiment_group, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
36 if (experiment.size() != 2) { | 38 if (experiment.size() != 2) { |
37 DLOG(ERROR) << "Experiment and params should be separated by ':'"; | 39 DLOG(ERROR) << "Experiment and params should be separated by ':'"; |
38 return false; | 40 return false; |
39 } | 41 } |
40 | 42 |
41 std::vector<std::string> group_parts = base::SplitString( | 43 std::vector<std::string> group_parts = base::SplitString( |
42 experiment[0], ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 44 experiment[0], ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
43 if (group_parts.size() != 2) { | 45 if (group_parts.size() != 2) { |
44 DLOG(ERROR) << "Study and group name should be separated by '.'"; | 46 DLOG(ERROR) << "Study and group name should be separated by '.'"; |
45 return false; | 47 return false; |
46 } | 48 } |
47 | 49 |
48 std::vector<std::string> key_values = base::SplitString( | 50 std::vector<std::string> key_values = base::SplitString( |
49 experiment[1], "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 51 experiment[1], "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
50 if (key_values.size() % 2 != 0) { | 52 if (key_values.size() % 2 != 0) { |
51 DLOG(ERROR) << "Param name and param value should be separated by '/'"; | 53 DLOG(ERROR) << "Param name and param value should be separated by '/'"; |
52 return false; | 54 return false; |
53 } | 55 } |
54 | 56 std::string trial = EscapeValue(group_parts[0]); |
| 57 std::string group = EscapeValue(group_parts[1]); |
| 58 auto trial_group = std::make_pair(trial, group); |
| 59 if (trial_groups.find(trial_group) != trial_groups.end()) { |
| 60 DLOG(ERROR) << base::StringPrintf( |
| 61 "A (study, group) pair listed more than once. (%s, %s)", |
| 62 trial.c_str(), group.c_str()); |
| 63 return false; |
| 64 } |
| 65 trial_groups.insert(trial_group); |
55 std::map<std::string, std::string> params; | 66 std::map<std::string, std::string> params; |
56 for (size_t i = 0; i < key_values.size(); i += 2) { | 67 for (size_t i = 0; i < key_values.size(); i += 2) { |
57 std::string key = EscapeValue(key_values[i]); | 68 std::string key = EscapeValue(key_values[i]); |
58 std::string value = EscapeValue(key_values[i + 1]); | 69 std::string value = EscapeValue(key_values[i + 1]); |
59 params[key] = value; | 70 params[key] = value; |
60 } | 71 } |
61 std::string trial = EscapeValue(group_parts[0]); | |
62 std::string group = EscapeValue(group_parts[1]); | |
63 variations::AssociateVariationParams(trial, group, params); | 72 variations::AssociateVariationParams(trial, group, params); |
64 } | 73 } |
65 return true; | 74 return true; |
66 } | 75 } |
67 | 76 |
68 void AssociateParamsFromFieldTrialConfig(const FieldTrialTestingConfig& config, | 77 void AssociateParamsFromFieldTrialConfig(const FieldTrialTestingConfig& config, |
69 base::FeatureList* feature_list) { | 78 base::FeatureList* feature_list) { |
70 for (size_t i = 0; i < config.groups_size; ++i) { | 79 for (size_t i = 0; i < config.groups_size; ++i) { |
71 const FieldTrialTestingGroup& group = config.groups[i]; | 80 const FieldTrialTestingGroup& group = config.groups[i]; |
72 if (group.params_size != 0) { | 81 if (group.params_size != 0) { |
(...skipping 19 matching lines...) Expand all Loading... |
92 base::FeatureList::OVERRIDE_DISABLE_FEATURE, trial); | 101 base::FeatureList::OVERRIDE_DISABLE_FEATURE, trial); |
93 } | 102 } |
94 } | 103 } |
95 } | 104 } |
96 | 105 |
97 void AssociateDefaultFieldTrialConfig(base::FeatureList* feature_list) { | 106 void AssociateDefaultFieldTrialConfig(base::FeatureList* feature_list) { |
98 AssociateParamsFromFieldTrialConfig(kFieldTrialConfig, feature_list); | 107 AssociateParamsFromFieldTrialConfig(kFieldTrialConfig, feature_list); |
99 } | 108 } |
100 | 109 |
101 } // namespace chrome_variations | 110 } // namespace chrome_variations |
OLD | NEW |