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