- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / metrics / variations / variations_util.cc
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.
4
5 #include "chrome/common/metrics/variations/variations_util.h"
6
7 #include <vector>
8
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"
18
19 namespace chrome_variations {
20
21 namespace {
22
23 const char kVariationPrefix[] = "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
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.
44   const string16 key =
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);
49   string16 label(key);
50   label += ASCIIToUTF16("=");
51   label += value;
52   label += ASCIIToUTF16("|");
53   label += installer::BuildExperimentDateString();
54   return label;
55 }
56
57 }  // namespace
58
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(
89     const base::FieldTrial::ActiveGroups& active_groups) {
90   string16 experiment_labels;
91   int counter = 0;
92
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);
98
99     if (id == EMPTY_ID)
100       continue;
101
102     if (!experiment_labels.empty())
103       experiment_labels += ASCIIToUTF16(kExperimentLabelSep);
104     experiment_labels += CreateSingleExperimentLabel(++counter, id);
105   }
106
107   return experiment_labels;
108 }
109
110 string16 ExtractNonVariationLabels(const string16& labels) {
111   const string16 separator = ASCIIToUTF16(kExperimentLabelSep);
112   string16 non_variation_labels;
113
114   // First, split everything by the label separator.
115   std::vector<string16> entries;
116   base::SplitStringUsingSubstr(labels, separator, &entries);
117
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))
122       continue;
123
124     // Dump the whole thing, including the timestamp.
125     if (!non_variation_labels.empty())
126       non_variation_labels += separator;
127     non_variation_labels += *it;
128   }
129
130   return non_variation_labels;
131 }
132
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;
146 }
147
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