blob: a473a40e2883fdef6bbdf99b9552f81e621f0c5c [file] [log] [blame]
[email protected]71011c1682014-07-09 17:19:161// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]bd3b4712012-12-18 17:01:302// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
asvitkine9a279832015-12-18 02:35:505#include "components/variations/variations_http_header_provider.h"
[email protected]bd3b4712012-12-18 17:01:306
avi5dd91f82015-12-25 22:30:467#include <stddef.h>
8
horoe09b6c82014-11-01 02:08:289#include <set>
10#include <string>
[email protected]1bd918d2013-10-13 18:23:0911#include <vector>
12
[email protected]bd3b4712012-12-18 17:01:3013#include "base/base64.h"
14#include "base/memory/singleton.h"
asvitkine454600f2015-06-16 16:34:5015#include "base/metrics/histogram_macros.h"
[email protected]1bd918d2013-10-13 18:23:0916#include "base/strings/string_number_conversions.h"
17#include "base/strings/string_split.h"
[email protected]7f8a9932013-07-26 20:43:3418#include "base/strings/string_util.h"
fdoraycddcf0e2016-06-23 21:20:3019#include "base/threading/thread_task_runner_handle.h"
[email protected]ea15bd52014-07-14 22:42:5020#include "components/variations/proto/client_variations.pb.h"
[email protected]bd3b4712012-12-18 17:01:3021
[email protected]71011c1682014-07-09 17:19:1622namespace variations {
[email protected]ab7780792013-01-10 01:26:0923
asvitkine9a279832015-12-18 02:35:5024// static
[email protected]ab7780792013-01-10 01:26:0925VariationsHttpHeaderProvider* VariationsHttpHeaderProvider::GetInstance() {
olli.raula36aa8be2015-09-10 11:14:2226 return base::Singleton<VariationsHttpHeaderProvider>::get();
[email protected]bd3b4712012-12-18 17:01:3027}
28
yutak3305f49d2016-12-13 10:32:3129std::string VariationsHttpHeaderProvider::GetClientDataHeader(
30 bool is_signed_in) {
[email protected]bd3b4712012-12-18 17:01:3031 // Lazily initialize the header, if not already done, before attempting to
32 // transmit it.
33 InitVariationIDsCacheIfNeeded();
[email protected]ab7780792013-01-10 01:26:0934
35 std::string variation_ids_header_copy;
36 {
37 base::AutoLock scoped_lock(lock_);
yutak3305f49d2016-12-13 10:32:3138 variation_ids_header_copy = is_signed_in
39 ? cached_variation_ids_header_signed_in_
40 : cached_variation_ids_header_;
[email protected]ab7780792013-01-10 01:26:0941 }
asvitkine9a279832015-12-18 02:35:5042 return variation_ids_header_copy;
[email protected]bd3b4712012-12-18 17:01:3043}
44
asvitkine35ba6472015-12-18 23:52:3345std::string VariationsHttpHeaderProvider::GetVariationsString() {
46 InitVariationIDsCacheIfNeeded();
47
48 // Construct a space-separated string with leading and trailing spaces from
49 // the variations set. Note: The ids in it will be in sorted order per the
50 // std::set contract.
51 std::string ids_string = " ";
52 {
53 base::AutoLock scoped_lock(lock_);
yutak3305f49d2016-12-13 10:32:3154 for (const VariationIDEntry& entry : GetAllVariationIds()) {
55 if (entry.second == GOOGLE_WEB_PROPERTIES) {
56 ids_string.append(base::IntToString(entry.first));
57 ids_string.push_back(' ');
58 }
asvitkine35ba6472015-12-18 23:52:3359 }
60 }
61 return ids_string;
62}
63
Alexei Svitkine105f942e2018-02-17 02:53:4864VariationsHttpHeaderProvider::ForceIdsResult
65VariationsHttpHeaderProvider::ForceVariationIds(
66 const std::vector<std::string>& variation_ids,
67 const std::string& command_line_variation_ids) {
68 default_variation_ids_set_.clear();
69
70 if (!AddDefaultVariationIds(variation_ids))
71 return ForceIdsResult::INVALID_VECTOR_ENTRY;
72
jkrcalbf073372016-07-29 07:21:3173 if (!command_line_variation_ids.empty()) {
Alexei Svitkine105f942e2018-02-17 02:53:4874 std::vector<std::string> variation_ids_from_command_line =
jkrcalbf073372016-07-29 07:21:3175 base::SplitString(command_line_variation_ids, ",",
76 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
Alexei Svitkine105f942e2018-02-17 02:53:4877 if (!AddDefaultVariationIds(variation_ids_from_command_line))
78 return ForceIdsResult::INVALID_SWITCH_ENTRY;
jkrcalbf073372016-07-29 07:21:3179 }
Alexei Svitkine105f942e2018-02-17 02:53:4880 return ForceIdsResult::SUCCESS;
[email protected]1bd918d2013-10-13 18:23:0981}
82
asvitkineb4ed78682015-03-12 18:18:5483void VariationsHttpHeaderProvider::ResetForTesting() {
84 base::AutoLock scoped_lock(lock_);
85
86 // Stop observing field trials so that it can be restarted when this is
87 // re-inited. Note: This is a no-op if this is not currently observing.
88 base::FieldTrialList::RemoveObserver(this);
89 variation_ids_cache_initialized_ = false;
90}
91
[email protected]ab7780792013-01-10 01:26:0992VariationsHttpHeaderProvider::VariationsHttpHeaderProvider()
asvitkine9a279832015-12-18 02:35:5093 : variation_ids_cache_initialized_(false) {}
[email protected]bd3b4712012-12-18 17:01:3094
asvitkine9a279832015-12-18 02:35:5095VariationsHttpHeaderProvider::~VariationsHttpHeaderProvider() {}
[email protected]bd3b4712012-12-18 17:01:3096
[email protected]ab7780792013-01-10 01:26:0997void VariationsHttpHeaderProvider::OnFieldTrialGroupFinalized(
[email protected]bd3b4712012-12-18 17:01:3098 const std::string& trial_name,
99 const std::string& group_name) {
[email protected]bd3b4712012-12-18 17:01:30100 base::AutoLock scoped_lock(lock_);
yutak3305f49d2016-12-13 10:32:31101 const size_t old_size = variation_ids_set_.size();
102 CacheVariationsId(trial_name, group_name, GOOGLE_WEB_PROPERTIES);
103 CacheVariationsId(trial_name, group_name, GOOGLE_WEB_PROPERTIES_SIGNED_IN);
104 CacheVariationsId(trial_name, group_name, GOOGLE_WEB_PROPERTIES_TRIGGER);
105 if (variation_ids_set_.size() != old_size)
106 UpdateVariationIDsHeaderValue();
[email protected]bd3b4712012-12-18 17:01:30107}
108
asvitkinee0dbdbe2014-10-31 21:59:57109void VariationsHttpHeaderProvider::OnSyntheticTrialsChanged(
asvitkine9a279832015-12-18 02:35:50110 const std::vector<SyntheticTrialGroup>& groups) {
asvitkinee0dbdbe2014-10-31 21:59:57111 base::AutoLock scoped_lock(lock_);
112
113 synthetic_variation_ids_set_.clear();
asvitkine9a279832015-12-18 02:35:50114 for (const SyntheticTrialGroup& group : groups) {
yutak3305f49d2016-12-13 10:32:31115 VariationID id =
asvitkinee0dbdbe2014-10-31 21:59:57116 GetGoogleVariationIDFromHashes(GOOGLE_WEB_PROPERTIES, group.id);
yutak3305f49d2016-12-13 10:32:31117 if (id != EMPTY_ID) {
118 synthetic_variation_ids_set_.insert(
119 VariationIDEntry(id, GOOGLE_WEB_PROPERTIES));
120 }
121 id = GetGoogleVariationIDFromHashes(GOOGLE_WEB_PROPERTIES_SIGNED_IN,
122 group.id);
123 if (id != EMPTY_ID) {
124 synthetic_variation_ids_set_.insert(
125 VariationIDEntry(id, GOOGLE_WEB_PROPERTIES_SIGNED_IN));
126 }
asvitkinee0dbdbe2014-10-31 21:59:57127 }
128 UpdateVariationIDsHeaderValue();
129}
130
[email protected]ab7780792013-01-10 01:26:09131void VariationsHttpHeaderProvider::InitVariationIDsCacheIfNeeded() {
[email protected]bd3b4712012-12-18 17:01:30132 base::AutoLock scoped_lock(lock_);
133 if (variation_ids_cache_initialized_)
134 return;
135
136 // Register for additional cache updates. This is done first to avoid a race
137 // that could cause registered FieldTrials to be missed.
fdoraycddcf0e2016-06-23 21:20:30138 DCHECK(base::ThreadTaskRunnerHandle::IsSet());
[email protected]bd3b4712012-12-18 17:01:30139 base::FieldTrialList::AddObserver(this);
140
[email protected]999f7b42013-02-04 16:14:25141 base::TimeTicks before_time = base::TimeTicks::Now();
142
[email protected]bd3b4712012-12-18 17:01:30143 base::FieldTrial::ActiveGroups initial_groups;
144 base::FieldTrialList::GetActiveFieldTrialGroups(&initial_groups);
asvitkine35ba6472015-12-18 23:52:33145
146 for (const auto& entry : initial_groups) {
yutak3305f49d2016-12-13 10:32:31147 CacheVariationsId(entry.trial_name, entry.group_name,
148 GOOGLE_WEB_PROPERTIES);
149 CacheVariationsId(entry.trial_name, entry.group_name,
150 GOOGLE_WEB_PROPERTIES_SIGNED_IN);
151 CacheVariationsId(entry.trial_name, entry.group_name,
152 GOOGLE_WEB_PROPERTIES_TRIGGER);
[email protected]bd3b4712012-12-18 17:01:30153 }
154 UpdateVariationIDsHeaderValue();
155
[email protected]999f7b42013-02-04 16:14:25156 UMA_HISTOGRAM_CUSTOM_COUNTS(
157 "Variations.HeaderConstructionTime",
drbasicf0d1b262016-08-23 06:10:42158 (base::TimeTicks::Now() - before_time).InMicroseconds(), 1,
asvitkine9a279832015-12-18 02:35:50159 base::TimeDelta::FromSeconds(1).InMicroseconds(), 50);
[email protected]999f7b42013-02-04 16:14:25160
[email protected]bd3b4712012-12-18 17:01:30161 variation_ids_cache_initialized_ = true;
162}
163
yutak3305f49d2016-12-13 10:32:31164void VariationsHttpHeaderProvider::CacheVariationsId(
165 const std::string& trial_name,
166 const std::string& group_name,
167 IDCollectionKey key) {
168 const VariationID id = GetGoogleVariationID(key, trial_name, group_name);
169 if (id != EMPTY_ID)
170 variation_ids_set_.insert(VariationIDEntry(id, key));
171}
172
[email protected]ab7780792013-01-10 01:26:09173void VariationsHttpHeaderProvider::UpdateVariationIDsHeaderValue() {
174 lock_.AssertAcquired();
175
[email protected]bd3b4712012-12-18 17:01:30176 // The header value is a serialized protobuffer of Variation IDs which is
177 // base64 encoded before transmitting as a string.
yutak3305f49d2016-12-13 10:32:31178 cached_variation_ids_header_.clear();
179 cached_variation_ids_header_signed_in_.clear();
[email protected]1bd918d2013-10-13 18:23:09180
yutak3305f49d2016-12-13 10:32:31181 // If successful, swap the header value with the new one.
182 // Note that the list of IDs and the header could be temporarily out of sync
183 // if IDs are added as the header is recreated. The receiving servers are OK
184 // with such discrepancies.
185 cached_variation_ids_header_ = GenerateBase64EncodedProto(false);
186 cached_variation_ids_header_signed_in_ = GenerateBase64EncodedProto(true);
187}
188
189std::string VariationsHttpHeaderProvider::GenerateBase64EncodedProto(
190 bool is_signed_in) {
191 std::set<VariationIDEntry> all_variation_ids_set = GetAllVariationIds();
192
193 ClientVariations proto;
194 for (const VariationIDEntry& entry : all_variation_ids_set) {
195 switch (entry.second) {
196 case GOOGLE_WEB_PROPERTIES_SIGNED_IN:
197 if (is_signed_in)
198 proto.add_variation_id(entry.first);
199 break;
200 case GOOGLE_WEB_PROPERTIES:
201 proto.add_variation_id(entry.first);
202 break;
203 case GOOGLE_WEB_PROPERTIES_TRIGGER:
204 proto.add_trigger_variation_id(entry.first);
205 break;
Sky Malicea846ad7d2017-12-05 00:44:42206 case CHROME_SYNC_EVENT_LOGGER:
yutak3305f49d2016-12-13 10:32:31207 case ID_COLLECTION_COUNT:
208 // These cases included to get full enum coverage for switch, so that
209 // new enums introduce compiler warnings. Nothing to do for these.
210 break;
211 }
[email protected]8c2c5442014-04-04 18:55:29212 }
[email protected]bd3b4712012-12-18 17:01:30213
yutak3305f49d2016-12-13 10:32:31214 const size_t total_id_count =
215 proto.variation_id_size() + proto.trigger_variation_id_size();
216
217 if (total_id_count == 0)
218 return std::string();
219
[email protected]bd3b4712012-12-18 17:01:30220 // This is the bottleneck for the creation of the header, so validate the size
221 // here. Force a hard maximum on the ID count in case the Variations server
222 // returns too many IDs and DOSs receiving servers with large requests.
[email protected]e51dcb0c2014-05-06 16:56:10223 DCHECK_LE(total_id_count, 10U);
[email protected]a27ae2a2014-08-01 16:17:52224 UMA_HISTOGRAM_COUNTS_100("Variations.Headers.ExperimentCount",
225 total_id_count);
[email protected]e51dcb0c2014-05-06 16:56:10226 if (total_id_count > 20)
yutak3305f49d2016-12-13 10:32:31227 return std::string();
[email protected]8c2c5442014-04-04 18:55:29228
[email protected]bd3b4712012-12-18 17:01:30229 std::string serialized;
230 proto.SerializeToString(&serialized);
231
232 std::string hashed;
[email protected]33fca122013-12-11 01:48:50233 base::Base64Encode(serialized, &hashed);
yutak3305f49d2016-12-13 10:32:31234 return hashed;
[email protected]bd3b4712012-12-18 17:01:30235}
[email protected]ab7780792013-01-10 01:26:09236
Alexei Svitkine105f942e2018-02-17 02:53:48237bool VariationsHttpHeaderProvider::AddDefaultVariationIds(
238 const std::vector<std::string>& variation_ids) {
239 for (const std::string& entry : variation_ids) {
240 if (entry.empty()) {
241 default_variation_ids_set_.clear();
242 return false;
243 }
244 bool trigger_id =
245 base::StartsWith(entry, "t", base::CompareCase::SENSITIVE);
246 // Remove the "t" prefix if it's there.
247 std::string trimmed_entry = trigger_id ? entry.substr(1) : entry;
248
249 int variation_id = 0;
250 if (!base::StringToInt(trimmed_entry, &variation_id)) {
251 default_variation_ids_set_.clear();
252 return false;
253 }
254 default_variation_ids_set_.insert(VariationIDEntry(
255 variation_id,
256 trigger_id ? GOOGLE_WEB_PROPERTIES_TRIGGER : GOOGLE_WEB_PROPERTIES));
257 }
258 return true;
259}
260
yutak3305f49d2016-12-13 10:32:31261std::set<VariationsHttpHeaderProvider::VariationIDEntry>
262VariationsHttpHeaderProvider::GetAllVariationIds() {
asvitkine35ba6472015-12-18 23:52:33263 lock_.AssertAcquired();
264
yutak3305f49d2016-12-13 10:32:31265 std::set<VariationIDEntry> all_variation_ids_set = default_variation_ids_set_;
266 for (const VariationIDEntry& entry : variation_ids_set_) {
267 all_variation_ids_set.insert(entry);
268 }
269 for (const VariationIDEntry& entry : synthetic_variation_ids_set_) {
270 all_variation_ids_set.insert(entry);
271 }
asvitkine35ba6472015-12-18 23:52:33272 return all_variation_ids_set;
273}
274
[email protected]71011c1682014-07-09 17:19:16275} // namespace variations