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