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