| OLD | NEW |
| 1 // Copyright 2013 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/experiment_labels.h" | 5 #include "chrome/common/metrics/variations/experiment_labels.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #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" | 13 #include "components/variations/variations_associated_data.h" |
| 14 | 14 |
| 15 namespace chrome_variations { | 15 namespace chrome_variations { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 const char kVariationPrefix[] = "CrVar"; | 19 const char kVariationPrefix[] = "CrVar"; |
| 20 | 20 |
| 21 // This method builds a single experiment label for a Chrome Variation, | 21 // This method builds a single experiment label for a Chrome Variation, |
| 22 // including a timestamp that is a year in the future from |current_time|. Since | 22 // including a timestamp that is a year in the future from |current_time|. Since |
| 23 // multiple headers can be transmitted, |count| is a number that is appended | 23 // multiple headers can be transmitted, |count| is a number that is appended |
| 24 // after the label key to differentiate the labels. | 24 // after the label key to differentiate the labels. |
| 25 base::string16 CreateSingleExperimentLabel(int count, VariationID id, | 25 base::string16 CreateSingleExperimentLabel(int count, VariationID id, |
| 26 const base::Time& current_time) { | 26 const base::Time& current_time) { |
| 27 // Build the parts separately so they can be validated. | 27 // Build the parts separately so they can be validated. |
| 28 const base::string16 key = | 28 const base::string16 key = |
| 29 ASCIIToUTF16(kVariationPrefix) + base::IntToString16(count); | 29 base::ASCIIToUTF16(kVariationPrefix) + base::IntToString16(count); |
| 30 DCHECK_LE(key.size(), 8U); | 30 DCHECK_LE(key.size(), 8U); |
| 31 const base::string16 value = base::IntToString16(id); | 31 const base::string16 value = base::IntToString16(id); |
| 32 DCHECK_LE(value.size(), 8U); | 32 DCHECK_LE(value.size(), 8U); |
| 33 base::string16 label(key); | 33 base::string16 label(key); |
| 34 label += ASCIIToUTF16("="); | 34 label += base::ASCIIToUTF16("="); |
| 35 label += value; | 35 label += value; |
| 36 label += ASCIIToUTF16("|"); | 36 label += base::ASCIIToUTF16("|"); |
| 37 label += installer::BuildExperimentDateString(current_time); | 37 label += installer::BuildExperimentDateString(current_time); |
| 38 return label; | 38 return label; |
| 39 } | 39 } |
| 40 | 40 |
| 41 } // namespace | 41 } // namespace |
| 42 | 42 |
| 43 base::string16 BuildGoogleUpdateExperimentLabel( | 43 base::string16 BuildGoogleUpdateExperimentLabel( |
| 44 const base::FieldTrial::ActiveGroups& active_groups) { | 44 const base::FieldTrial::ActiveGroups& active_groups) { |
| 45 base::string16 experiment_labels; | 45 base::string16 experiment_labels; |
| 46 int counter = 0; | 46 int counter = 0; |
| 47 | 47 |
| 48 const base::Time current_time(base::Time::Now()); | 48 const base::Time current_time(base::Time::Now()); |
| 49 | 49 |
| 50 // Find all currently active VariationIDs associated with Google Update. | 50 // Find all currently active VariationIDs associated with Google Update. |
| 51 for (base::FieldTrial::ActiveGroups::const_iterator it = | 51 for (base::FieldTrial::ActiveGroups::const_iterator it = |
| 52 active_groups.begin(); it != active_groups.end(); ++it) { | 52 active_groups.begin(); it != active_groups.end(); ++it) { |
| 53 const VariationID id = GetGoogleVariationID(GOOGLE_UPDATE_SERVICE, | 53 const VariationID id = GetGoogleVariationID(GOOGLE_UPDATE_SERVICE, |
| 54 it->trial_name, it->group_name); | 54 it->trial_name, it->group_name); |
| 55 | 55 |
| 56 if (id == EMPTY_ID) | 56 if (id == EMPTY_ID) |
| 57 continue; | 57 continue; |
| 58 | 58 |
| 59 if (!experiment_labels.empty()) | 59 if (!experiment_labels.empty()) { |
| 60 experiment_labels += ASCIIToUTF16(google_update::kExperimentLabelSep); | 60 experiment_labels += |
| 61 base::ASCIIToUTF16(google_update::kExperimentLabelSep); |
| 62 } |
| 61 experiment_labels += CreateSingleExperimentLabel(++counter, id, | 63 experiment_labels += CreateSingleExperimentLabel(++counter, id, |
| 62 current_time); | 64 current_time); |
| 63 } | 65 } |
| 64 | 66 |
| 65 return experiment_labels; | 67 return experiment_labels; |
| 66 } | 68 } |
| 67 | 69 |
| 68 base::string16 ExtractNonVariationLabels(const base::string16& labels) { | 70 base::string16 ExtractNonVariationLabels(const base::string16& labels) { |
| 69 const base::string16 separator = | 71 const base::string16 separator = |
| 70 ASCIIToUTF16(google_update::kExperimentLabelSep); | 72 base::ASCIIToUTF16(google_update::kExperimentLabelSep); |
| 71 base::string16 non_variation_labels; | 73 base::string16 non_variation_labels; |
| 72 | 74 |
| 73 // First, split everything by the label separator. | 75 // First, split everything by the label separator. |
| 74 std::vector<base::string16> entries; | 76 std::vector<base::string16> entries; |
| 75 base::SplitStringUsingSubstr(labels, separator, &entries); | 77 base::SplitStringUsingSubstr(labels, separator, &entries); |
| 76 | 78 |
| 77 // For each label, keep the ones that do not look like a Variations label. | 79 // For each label, keep the ones that do not look like a Variations label. |
| 78 for (std::vector<base::string16>::const_iterator it = entries.begin(); | 80 for (std::vector<base::string16>::const_iterator it = entries.begin(); |
| 79 it != entries.end(); ++it) { | 81 it != entries.end(); ++it) { |
| 80 if (it->empty() || StartsWith(*it, ASCIIToUTF16(kVariationPrefix), false)) | 82 if (it->empty() || |
| 83 StartsWith(*it, base::ASCIIToUTF16(kVariationPrefix), false)) { |
| 81 continue; | 84 continue; |
| 85 } |
| 82 | 86 |
| 83 // Dump the whole thing, including the timestamp. | 87 // Dump the whole thing, including the timestamp. |
| 84 if (!non_variation_labels.empty()) | 88 if (!non_variation_labels.empty()) |
| 85 non_variation_labels += separator; | 89 non_variation_labels += separator; |
| 86 non_variation_labels += *it; | 90 non_variation_labels += *it; |
| 87 } | 91 } |
| 88 | 92 |
| 89 return non_variation_labels; | 93 return non_variation_labels; |
| 90 } | 94 } |
| 91 | 95 |
| 92 base::string16 CombineExperimentLabels(const base::string16& variation_labels, | 96 base::string16 CombineExperimentLabels(const base::string16& variation_labels, |
| 93 const base::string16& other_labels) { | 97 const base::string16& other_labels) { |
| 94 const base::string16 separator = | 98 const base::string16 separator = |
| 95 ASCIIToUTF16(google_update::kExperimentLabelSep); | 99 base::ASCIIToUTF16(google_update::kExperimentLabelSep); |
| 96 DCHECK(!StartsWith(variation_labels, separator, false)); | 100 DCHECK(!StartsWith(variation_labels, separator, false)); |
| 97 DCHECK(!EndsWith(variation_labels, separator, false)); | 101 DCHECK(!EndsWith(variation_labels, separator, false)); |
| 98 DCHECK(!StartsWith(other_labels, separator, false)); | 102 DCHECK(!StartsWith(other_labels, separator, false)); |
| 99 DCHECK(!EndsWith(other_labels, separator, false)); | 103 DCHECK(!EndsWith(other_labels, separator, false)); |
| 100 // Note that if either label is empty, a separator is not necessary. | 104 // Note that if either label is empty, a separator is not necessary. |
| 101 base::string16 combined_labels = other_labels; | 105 base::string16 combined_labels = other_labels; |
| 102 if (!other_labels.empty() && !variation_labels.empty()) | 106 if (!other_labels.empty() && !variation_labels.empty()) |
| 103 combined_labels += separator; | 107 combined_labels += separator; |
| 104 combined_labels += variation_labels; | 108 combined_labels += variation_labels; |
| 105 return combined_labels; | 109 return combined_labels; |
| 106 } | 110 } |
| 107 | 111 |
| 108 } // namespace chrome_variations | 112 } // namespace chrome_variations |
| OLD | NEW |