| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/variations_util.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 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" | 9 #include "base/strings/stringprintf.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 15 #include "chrome/common/child_process_logging.h" | 11 #include "chrome/common/child_process_logging.h" |
| 16 #include "chrome/common/crash_keys.h" | 12 #include "chrome/common/crash_keys.h" |
| 17 #include "chrome/installer/util/google_update_experiment_util.h" | |
| 18 | 13 |
| 19 namespace chrome_variations { | 14 namespace chrome_variations { |
| 20 | 15 |
| 21 namespace { | 16 namespace { |
| 22 | 17 |
| 23 const char kVariationPrefix[] = "CrVar"; | |
| 24 const char kExperimentLabelSep[] = ";"; | |
| 25 | |
| 26 // Populates |name_group_ids| based on |active_groups|. | 18 // Populates |name_group_ids| based on |active_groups|. |
| 27 void GetFieldTrialActiveGroupIdsForActiveGroups( | 19 void GetFieldTrialActiveGroupIdsForActiveGroups( |
| 28 const base::FieldTrial::ActiveGroups& active_groups, | 20 const base::FieldTrial::ActiveGroups& active_groups, |
| 29 std::vector<ActiveGroupId>* name_group_ids) { | 21 std::vector<ActiveGroupId>* name_group_ids) { |
| 30 DCHECK(name_group_ids->empty()); | 22 DCHECK(name_group_ids->empty()); |
| 31 for (base::FieldTrial::ActiveGroups::const_iterator it = | 23 for (base::FieldTrial::ActiveGroups::const_iterator it = |
| 32 active_groups.begin(); it != active_groups.end(); ++it) { | 24 active_groups.begin(); it != active_groups.end(); ++it) { |
| 33 name_group_ids->push_back(MakeActiveGroupId(it->trial_name, | 25 name_group_ids->push_back(MakeActiveGroupId(it->trial_name, |
| 34 it->group_name)); | 26 it->group_name)); |
| 35 } | 27 } |
| 36 } | 28 } |
| 37 | 29 |
| 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 | 30 } // namespace |
| 58 | 31 |
| 59 void GetFieldTrialActiveGroupIds( | 32 void GetFieldTrialActiveGroupIds( |
| 60 std::vector<ActiveGroupId>* name_group_ids) { | 33 std::vector<ActiveGroupId>* name_group_ids) { |
| 61 DCHECK(name_group_ids->empty()); | 34 DCHECK(name_group_ids->empty()); |
| 62 // A note on thread safety: Since GetActiveFieldTrialGroups() is thread | 35 // A note on thread safety: Since GetActiveFieldTrialGroups() is thread |
| 63 // safe, and we operate on a separate list of that data, this function is | 36 // 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. | 37 // technically thread safe as well, with respect to the FieldTrialList data. |
| 65 base::FieldTrial::ActiveGroups active_groups; | 38 base::FieldTrial::ActiveGroups active_groups; |
| 66 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups); | 39 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 78 "%x-%x", name_group_ids[i].name, name_group_ids[i].group)); | 51 "%x-%x", name_group_ids[i].name, name_group_ids[i].group)); |
| 79 } | 52 } |
| 80 } | 53 } |
| 81 | 54 |
| 82 void SetChildProcessLoggingVariationList() { | 55 void SetChildProcessLoggingVariationList() { |
| 83 std::vector<std::string> experiment_strings; | 56 std::vector<std::string> experiment_strings; |
| 84 GetFieldTrialActiveGroupIdsAsStrings(&experiment_strings); | 57 GetFieldTrialActiveGroupIdsAsStrings(&experiment_strings); |
| 85 crash_keys::SetVariationsList(experiment_strings); | 58 crash_keys::SetVariationsList(experiment_strings); |
| 86 } | 59 } |
| 87 | 60 |
| 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. | 61 // Functions below are exposed for testing explicitly behind this namespace. |
| 149 // They simply wrap existing functions in this file. | 62 // They simply wrap existing functions in this file. |
| 150 namespace testing { | 63 namespace testing { |
| 151 | 64 |
| 152 void TestGetFieldTrialActiveGroupIds( | 65 void TestGetFieldTrialActiveGroupIds( |
| 153 const base::FieldTrial::ActiveGroups& active_groups, | 66 const base::FieldTrial::ActiveGroups& active_groups, |
| 154 std::vector<ActiveGroupId>* name_group_ids) { | 67 std::vector<ActiveGroupId>* name_group_ids) { |
| 155 GetFieldTrialActiveGroupIdsForActiveGroups(active_groups, | 68 GetFieldTrialActiveGroupIdsForActiveGroups(active_groups, |
| 156 name_group_ids); | 69 name_group_ids); |
| 157 } | 70 } |
| 158 | 71 |
| 159 } // namespace testing | 72 } // namespace testing |
| 160 | 73 |
| 161 } // namespace chrome_variations | 74 } // namespace chrome_variations |
| OLD | NEW |