OLD | NEW |
1 // Copyright (c) 2012 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 #include "chrome/common/metrics/variations/variations_util.h" | 5 #include "base/logging.h" |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/strings/string16.h" | |
10 #include "base/strings/string_number_conversions.h" | 6 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/string_split.h" | 7 #include "base/strings/string_split.h" |
12 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
13 #include "base/strings/stringprintf.h" | 9 #include "chrome/installer/util/google_update_constants.h" |
14 #include "base/strings/utf_string_conversions.h" | |
15 #include "chrome/common/child_process_logging.h" | |
16 #include "chrome/installer/util/google_update_experiment_util.h" | 10 #include "chrome/installer/util/google_update_experiment_util.h" |
| 11 #include "components/variations/variations_associated_data.h" |
17 | 12 |
18 namespace chrome_variations { | 13 namespace chrome_variations { |
19 | 14 |
20 namespace { | 15 namespace { |
21 | 16 |
22 const char kVariationPrefix[] = "CrVar"; | 17 const wchar_t kVariationPrefix[] = L"CrVar"; |
23 const char kExperimentLabelSep[] = ";"; | |
24 | |
25 // Populates |name_group_ids| based on |active_groups|. | |
26 void GetFieldTrialActiveGroupIdsForActiveGroups( | |
27 const base::FieldTrial::ActiveGroups& active_groups, | |
28 std::vector<ActiveGroupId>* name_group_ids) { | |
29 DCHECK(name_group_ids->empty()); | |
30 for (base::FieldTrial::ActiveGroups::const_iterator it = | |
31 active_groups.begin(); it != active_groups.end(); ++it) { | |
32 name_group_ids->push_back(MakeActiveGroupId(it->trial_name, | |
33 it->group_name)); | |
34 } | |
35 } | |
36 | 18 |
37 // This method builds a single experiment label for a Chrome Variation, | 19 // This method builds a single experiment label for a Chrome Variation, |
38 // including a timestamp that is a year in the future from now. Since multiple | 20 // including a timestamp that is a year in the future from now. Since multiple |
39 // headers can be transmitted, |count| is a number that is appended after the | 21 // headers can be transmitted, |count| is a number that is appended after the |
40 // label key to differentiate the labels. | 22 // label key to differentiate the labels. |
41 string16 CreateSingleExperimentLabel(int count, VariationID id) { | 23 string16 CreateSingleExperimentLabel(int count, VariationID id) { |
42 // Build the parts separately so they can be validated. | 24 // Build the parts separately so they can be validated. |
43 const string16 key = | 25 const string16 key = kVariationPrefix + base::IntToString16(count); |
44 ASCIIToUTF16(kVariationPrefix) + base::IntToString16(count); | |
45 DCHECK_LE(key.size(), 8U); | 26 DCHECK_LE(key.size(), 8U); |
46 const string16 value = base::IntToString16(id); | 27 const string16 value = base::IntToString16(id); |
47 DCHECK_LE(value.size(), 8U); | 28 DCHECK_LE(value.size(), 8U); |
48 string16 label(key); | 29 string16 label(key); |
49 label += ASCIIToUTF16("="); | 30 label += L'='; |
50 label += value; | 31 label += value; |
51 label += ASCIIToUTF16("|"); | 32 label += L'|'; |
52 label += installer::BuildExperimentDateString(); | 33 label += installer::BuildExperimentDateString(); |
53 return label; | 34 return label; |
54 } | 35 } |
55 | 36 |
56 } // namespace | 37 } // namespace |
57 | 38 |
58 void GetFieldTrialActiveGroupIds( | |
59 std::vector<ActiveGroupId>* name_group_ids) { | |
60 DCHECK(name_group_ids->empty()); | |
61 // A note on thread safety: Since GetActiveFieldTrialGroups() is thread | |
62 // safe, and we operate on a separate list of that data, this function is | |
63 // technically thread safe as well, with respect to the FieldTriaList data. | |
64 base::FieldTrial::ActiveGroups active_groups; | |
65 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups); | |
66 GetFieldTrialActiveGroupIdsForActiveGroups(active_groups, | |
67 name_group_ids); | |
68 } | |
69 | |
70 void GetFieldTrialActiveGroupIdsAsStrings( | |
71 std::vector<string16>* output) { | |
72 DCHECK(output->empty()); | |
73 std::vector<ActiveGroupId> name_group_ids; | |
74 GetFieldTrialActiveGroupIds(&name_group_ids); | |
75 for (size_t i = 0; i < name_group_ids.size(); ++i) { | |
76 output->push_back(UTF8ToUTF16(base::StringPrintf( | |
77 "%x-%x", name_group_ids[i].name, name_group_ids[i].group))); | |
78 } | |
79 } | |
80 | |
81 void GenerateVariationChunks(const std::vector<string16>& experiments, | |
82 std::vector<string16>* chunks) { | |
83 string16 current_chunk; | |
84 for (size_t i = 0; i < experiments.size(); ++i) { | |
85 const size_t needed_length = | |
86 (current_chunk.empty() ? 1 : 0) + experiments[i].length(); | |
87 if (current_chunk.length() + needed_length > kMaxVariationChunkSize) { | |
88 chunks->push_back(current_chunk); | |
89 current_chunk = experiments[i]; | |
90 } else { | |
91 if (!current_chunk.empty()) | |
92 current_chunk.push_back(','); | |
93 current_chunk += experiments[i]; | |
94 } | |
95 } | |
96 if (!current_chunk.empty()) | |
97 chunks->push_back(current_chunk); | |
98 } | |
99 | |
100 void SetChildProcessLoggingVariationList() { | |
101 std::vector<string16> experiment_strings; | |
102 GetFieldTrialActiveGroupIdsAsStrings(&experiment_strings); | |
103 child_process_logging::SetExperimentList(experiment_strings); | |
104 } | |
105 | |
106 string16 BuildGoogleUpdateExperimentLabel( | 39 string16 BuildGoogleUpdateExperimentLabel( |
107 const base::FieldTrial::ActiveGroups& active_groups) { | 40 const base::FieldTrial::ActiveGroups& active_groups) { |
108 string16 experiment_labels; | 41 string16 experiment_labels; |
109 int counter = 0; | 42 int counter = 0; |
110 | 43 |
111 // Find all currently active VariationIDs associated with Google Update. | 44 // Find all currently active VariationIDs associated with Google Update. |
112 for (base::FieldTrial::ActiveGroups::const_iterator it = | 45 for (base::FieldTrial::ActiveGroups::const_iterator it = |
113 active_groups.begin(); it != active_groups.end(); ++it) { | 46 active_groups.begin(); it != active_groups.end(); ++it) { |
114 const VariationID id = GetGoogleVariationID(GOOGLE_UPDATE_SERVICE, | 47 const VariationID id = GetGoogleVariationID(GOOGLE_UPDATE_SERVICE, |
115 it->trial_name, it->group_name); | 48 it->trial_name, it->group_name); |
116 | 49 |
117 if (id == EMPTY_ID) | 50 if (id == EMPTY_ID) |
118 continue; | 51 continue; |
119 | 52 |
120 if (!experiment_labels.empty()) | 53 if (!experiment_labels.empty()) |
121 experiment_labels += ASCIIToUTF16(kExperimentLabelSep); | 54 experiment_labels += google_update::kExperimentLabelSep; |
122 experiment_labels += CreateSingleExperimentLabel(++counter, id); | 55 experiment_labels += CreateSingleExperimentLabel(++counter, id); |
123 } | 56 } |
124 | 57 |
125 return experiment_labels; | 58 return experiment_labels; |
126 } | 59 } |
127 | 60 |
128 string16 ExtractNonVariationLabels(const string16& labels) { | 61 string16 ExtractNonVariationLabels(const string16& labels) { |
129 const string16 separator = ASCIIToUTF16(kExperimentLabelSep); | |
130 string16 non_variation_labels; | 62 string16 non_variation_labels; |
131 | 63 |
132 // First, split everything by the label separator. | 64 // First, split everything by the label separator. |
133 std::vector<string16> entries; | 65 std::vector<string16> entries; |
134 base::SplitStringUsingSubstr(labels, separator, &entries); | 66 base::SplitStringUsingSubstr(labels, google_update::kExperimentLabelSep, |
| 67 &entries); |
135 | 68 |
136 // For each label, keep the ones that do not look like a Variations label. | 69 // For each label, keep the ones that do not look like a Variations label. |
137 for (std::vector<string16>::const_iterator it = entries.begin(); | 70 for (std::vector<string16>::const_iterator it = entries.begin(); |
138 it != entries.end(); ++it) { | 71 it != entries.end(); ++it) { |
139 if (it->empty() || StartsWith(*it, ASCIIToUTF16(kVariationPrefix), false)) | 72 if (it->empty() || StartsWith(*it, kVariationPrefix, false)) |
140 continue; | 73 continue; |
141 | 74 |
142 // Dump the whole thing, including the timestamp. | 75 // Dump the whole thing, including the timestamp. |
143 if (!non_variation_labels.empty()) | 76 if (!non_variation_labels.empty()) |
144 non_variation_labels += separator; | 77 non_variation_labels += google_update::kExperimentLabelSep; |
145 non_variation_labels += *it; | 78 non_variation_labels += *it; |
146 } | 79 } |
147 | 80 |
148 return non_variation_labels; | 81 return non_variation_labels; |
149 } | 82 } |
150 | 83 |
151 string16 CombineExperimentLabels(const string16& variation_labels, | 84 string16 CombineExperimentLabels(const string16& variation_labels, |
152 const string16& other_labels) { | 85 const string16& other_labels) { |
153 const string16 separator = ASCIIToUTF16(kExperimentLabelSep); | 86 DCHECK(!StartsWith(variation_labels, google_update::kExperimentLabelSep, |
154 DCHECK(!StartsWith(variation_labels, separator, false)); | 87 false)); |
155 DCHECK(!EndsWith(variation_labels, separator, false)); | 88 DCHECK(!EndsWith(variation_labels, google_update::kExperimentLabelSep, |
156 DCHECK(!StartsWith(other_labels, separator, false)); | 89 false)); |
157 DCHECK(!EndsWith(other_labels, separator, false)); | 90 DCHECK(!StartsWith(other_labels, google_update::kExperimentLabelSep, false)); |
| 91 DCHECK(!EndsWith(other_labels, google_update::kExperimentLabelSep, false)); |
158 // Note that if either label is empty, a separator is not necessary. | 92 // Note that if either label is empty, a separator is not necessary. |
159 string16 combined_labels = other_labels; | 93 string16 combined_labels = other_labels; |
160 if (!other_labels.empty() && !variation_labels.empty()) | 94 if (!other_labels.empty() && !variation_labels.empty()) |
161 combined_labels += separator; | 95 combined_labels += google_update::kExperimentLabelSep; |
162 combined_labels += variation_labels; | 96 combined_labels += variation_labels; |
163 return combined_labels; | 97 return combined_labels; |
164 } | 98 } |
165 | 99 |
166 // Functions below are exposed for testing explicitly behind this namespace. | |
167 // They simply wrap existing functions in this file. | |
168 namespace testing { | |
169 | |
170 void TestGetFieldTrialActiveGroupIds( | |
171 const base::FieldTrial::ActiveGroups& active_groups, | |
172 std::vector<ActiveGroupId>* name_group_ids) { | |
173 GetFieldTrialActiveGroupIdsForActiveGroups(active_groups, | |
174 name_group_ids); | |
175 } | |
176 | |
177 } // namespace testing | |
178 | |
179 } // namespace chrome_variations | 100 } // namespace chrome_variations |
OLD | NEW |