1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/common/metrics/variations/variations_util.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.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"
19 namespace chrome_variations {
23 const char kVariationPrefix[] = "CrVar";
24 const char kExperimentLabelSep[] = ";";
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,
38 // 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
40 // headers can be transmitted, |count| is a number that is appended after the
41 // label key to differentiate the labels.
42 string16 CreateSingleExperimentLabel(int count, VariationID id) {
43 // Build the parts separately so they can be validated.
45 ASCIIToUTF16(kVariationPrefix) + base::IntToString16(count);
46 DCHECK_LE(key.size(), 8U);
47 const string16 value = base::IntToString16(id);
48 DCHECK_LE(value.size(), 8U);
50 label += ASCIIToUTF16("=");
52 label += ASCIIToUTF16("|");
53 label += installer::BuildExperimentDateString();
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,
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));
82 void SetChildProcessLoggingVariationList() {
83 std::vector<std::string> experiment_strings;
84 GetFieldTrialActiveGroupIdsAsStrings(&experiment_strings);
85 crash_keys::SetVariationsList(experiment_strings);
88 string16 BuildGoogleUpdateExperimentLabel(
89 const base::FieldTrial::ActiveGroups& active_groups) {
90 string16 experiment_labels;
93 // Find all currently active VariationIDs associated with Google Update.
94 for (base::FieldTrial::ActiveGroups::const_iterator it =
95 active_groups.begin(); it != active_groups.end(); ++it) {
96 const VariationID id = GetGoogleVariationID(GOOGLE_UPDATE_SERVICE,
97 it->trial_name, it->group_name);
102 if (!experiment_labels.empty())
103 experiment_labels += ASCIIToUTF16(kExperimentLabelSep);
104 experiment_labels += CreateSingleExperimentLabel(++counter, id);
107 return experiment_labels;
110 string16 ExtractNonVariationLabels(const string16& labels) {
111 const string16 separator = ASCIIToUTF16(kExperimentLabelSep);
112 string16 non_variation_labels;
114 // First, split everything by the label separator.
115 std::vector<string16> entries;
116 base::SplitStringUsingSubstr(labels, separator, &entries);
118 // For each label, keep the ones that do not look like a Variations label.
119 for (std::vector<string16>::const_iterator it = entries.begin();
120 it != entries.end(); ++it) {
121 if (it->empty() || StartsWith(*it, ASCIIToUTF16(kVariationPrefix), false))
124 // Dump the whole thing, including the timestamp.
125 if (!non_variation_labels.empty())
126 non_variation_labels += separator;
127 non_variation_labels += *it;
130 return non_variation_labels;
133 string16 CombineExperimentLabels(const string16& variation_labels,
134 const string16& other_labels) {
135 const string16 separator = ASCIIToUTF16(kExperimentLabelSep);
136 DCHECK(!StartsWith(variation_labels, separator, false));
137 DCHECK(!EndsWith(variation_labels, separator, false));
138 DCHECK(!StartsWith(other_labels, separator, false));
139 DCHECK(!EndsWith(other_labels, separator, false));
140 // Note that if either label is empty, a separator is not necessary.
141 string16 combined_labels = other_labels;
142 if (!other_labels.empty() && !variation_labels.empty())
143 combined_labels += separator;
144 combined_labels += variation_labels;
145 return combined_labels;
148 // Functions below are exposed for testing explicitly behind this namespace.
149 // They simply wrap existing functions in this file.
152 void TestGetFieldTrialActiveGroupIds(
153 const base::FieldTrial::ActiveGroups& active_groups,
154 std::vector<ActiveGroupId>* name_group_ids) {
155 GetFieldTrialActiveGroupIdsForActiveGroups(active_groups,
159 } // namespace testing
161 } // namespace chrome_variations