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 |current_time|. Since |
40 // headers can be transmitted, |count| is a number that is appended after the | 23 // multiple headers can be transmitted, |count| is a number that is appended |
41 // label key to differentiate the labels. | 24 // after the label key to differentiate the labels. |
42 string16 CreateSingleExperimentLabel(int count, VariationID id) { | 25 string16 CreateSingleExperimentLabel(int count, VariationID id, |
| 26 const base::Time& current_time) { |
43 // Build the parts separately so they can be validated. | 27 // Build the parts separately so they can be validated. |
44 const string16 key = | 28 const string16 key = kVariationPrefix + base::IntToString16(count); |
45 ASCIIToUTF16(kVariationPrefix) + base::IntToString16(count); | |
46 DCHECK_LE(key.size(), 8U); | 29 DCHECK_LE(key.size(), 8U); |
47 const string16 value = base::IntToString16(id); | 30 const string16 value = base::IntToString16(id); |
48 DCHECK_LE(value.size(), 8U); | 31 DCHECK_LE(value.size(), 8U); |
49 string16 label(key); | 32 string16 label(key); |
50 label += ASCIIToUTF16("="); | 33 label += L'='; |
51 label += value; | 34 label += value; |
52 label += ASCIIToUTF16("|"); | 35 label += L'|'; |
53 label += installer::BuildExperimentDateString(); | 36 label += installer::BuildExperimentDateString(current_time); |
54 return label; | 37 return label; |
55 } | 38 } |
56 | 39 |
57 } // namespace | 40 } // namespace |
58 | 41 |
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( | 42 string16 BuildGoogleUpdateExperimentLabel( |
89 const base::FieldTrial::ActiveGroups& active_groups) { | 43 const base::FieldTrial::ActiveGroups& active_groups) { |
90 string16 experiment_labels; | 44 string16 experiment_labels; |
91 int counter = 0; | 45 int counter = 0; |
92 | 46 |
| 47 const base::Time current_time(base::Time::Now()); |
| 48 |
93 // Find all currently active VariationIDs associated with Google Update. | 49 // Find all currently active VariationIDs associated with Google Update. |
94 for (base::FieldTrial::ActiveGroups::const_iterator it = | 50 for (base::FieldTrial::ActiveGroups::const_iterator it = |
95 active_groups.begin(); it != active_groups.end(); ++it) { | 51 active_groups.begin(); it != active_groups.end(); ++it) { |
96 const VariationID id = GetGoogleVariationID(GOOGLE_UPDATE_SERVICE, | 52 const VariationID id = GetGoogleVariationID(GOOGLE_UPDATE_SERVICE, |
97 it->trial_name, it->group_name); | 53 it->trial_name, it->group_name); |
98 | 54 |
99 if (id == EMPTY_ID) | 55 if (id == EMPTY_ID) |
100 continue; | 56 continue; |
101 | 57 |
102 if (!experiment_labels.empty()) | 58 if (!experiment_labels.empty()) |
103 experiment_labels += ASCIIToUTF16(kExperimentLabelSep); | 59 experiment_labels += google_update::kExperimentLabelSep; |
104 experiment_labels += CreateSingleExperimentLabel(++counter, id); | 60 experiment_labels += CreateSingleExperimentLabel(++counter, id, |
| 61 current_time); |
105 } | 62 } |
106 | 63 |
107 return experiment_labels; | 64 return experiment_labels; |
108 } | 65 } |
109 | 66 |
110 string16 ExtractNonVariationLabels(const string16& labels) { | 67 string16 ExtractNonVariationLabels(const string16& labels) { |
111 const string16 separator = ASCIIToUTF16(kExperimentLabelSep); | |
112 string16 non_variation_labels; | 68 string16 non_variation_labels; |
113 | 69 |
114 // First, split everything by the label separator. | 70 // First, split everything by the label separator. |
115 std::vector<string16> entries; | 71 std::vector<string16> entries; |
116 base::SplitStringUsingSubstr(labels, separator, &entries); | 72 base::SplitStringUsingSubstr(labels, google_update::kExperimentLabelSep, |
| 73 &entries); |
117 | 74 |
118 // For each label, keep the ones that do not look like a Variations label. | 75 // For each label, keep the ones that do not look like a Variations label. |
119 for (std::vector<string16>::const_iterator it = entries.begin(); | 76 for (std::vector<string16>::const_iterator it = entries.begin(); |
120 it != entries.end(); ++it) { | 77 it != entries.end(); ++it) { |
121 if (it->empty() || StartsWith(*it, ASCIIToUTF16(kVariationPrefix), false)) | 78 if (it->empty() || StartsWith(*it, kVariationPrefix, false)) |
122 continue; | 79 continue; |
123 | 80 |
124 // Dump the whole thing, including the timestamp. | 81 // Dump the whole thing, including the timestamp. |
125 if (!non_variation_labels.empty()) | 82 if (!non_variation_labels.empty()) |
126 non_variation_labels += separator; | 83 non_variation_labels += google_update::kExperimentLabelSep; |
127 non_variation_labels += *it; | 84 non_variation_labels += *it; |
128 } | 85 } |
129 | 86 |
130 return non_variation_labels; | 87 return non_variation_labels; |
131 } | 88 } |
132 | 89 |
133 string16 CombineExperimentLabels(const string16& variation_labels, | 90 string16 CombineExperimentLabels(const string16& variation_labels, |
134 const string16& other_labels) { | 91 const string16& other_labels) { |
135 const string16 separator = ASCIIToUTF16(kExperimentLabelSep); | 92 DCHECK(!StartsWith(variation_labels, google_update::kExperimentLabelSep, |
136 DCHECK(!StartsWith(variation_labels, separator, false)); | 93 false)); |
137 DCHECK(!EndsWith(variation_labels, separator, false)); | 94 DCHECK(!EndsWith(variation_labels, google_update::kExperimentLabelSep, |
138 DCHECK(!StartsWith(other_labels, separator, false)); | 95 false)); |
139 DCHECK(!EndsWith(other_labels, separator, false)); | 96 DCHECK(!StartsWith(other_labels, google_update::kExperimentLabelSep, false)); |
| 97 DCHECK(!EndsWith(other_labels, google_update::kExperimentLabelSep, false)); |
140 // Note that if either label is empty, a separator is not necessary. | 98 // Note that if either label is empty, a separator is not necessary. |
141 string16 combined_labels = other_labels; | 99 string16 combined_labels = other_labels; |
142 if (!other_labels.empty() && !variation_labels.empty()) | 100 if (!other_labels.empty() && !variation_labels.empty()) |
143 combined_labels += separator; | 101 combined_labels += google_update::kExperimentLabelSep; |
144 combined_labels += variation_labels; | 102 combined_labels += variation_labels; |
145 return combined_labels; | 103 return combined_labels; |
146 } | 104 } |
147 | 105 |
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 | 106 } // namespace chrome_variations |
OLD | NEW |