blob: 5055b9e30508c7f8ec2a96c3ce3e7671c0c53366 [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
Jun Caif3aba7f92018-07-12 16:52:4024// The following documents how adding/removing http headers for web content
25// requests are implemented when Network Service is enabled or not enabled.
26//
27// When Network Service is not enabled, adding headers is implemented in
28// ChromeResourceDispatcherHostDelegate::RequestBeginning() by calling
29// variations::AppendVariationHeaders(), and removing headers is implemented in
30// ChromeNetworkDelegate::OnBeforeRedirect() by calling
31// variations::StripVariationHeaderIfNeeded().
32//
33// When Network Service is enabled, adding/removing headers is implemented by
34// request consumers, and how it is implemented depends on the request type.
35// There are three cases:
36// 1. Subresources request in renderer, it is implemented
37// in URLLoaderThrottleProviderImpl::CreateThrottles() by adding a
John Abd-El-Malek9fb60492018-08-02 04:28:5038// GoogleURLLoaderThrottle to a content::URLLoaderThrottle vector.
Jun Caif3aba7f92018-07-12 16:52:4039// 2. Navigations/Downloads request in browser, it is implemented in
40// ChromeContentBrowserClient::CreateURLLoaderThrottles() by also adding a
John Abd-El-Malek9fb60492018-08-02 04:28:5041// GoogleURLLoaderThrottle to a content::URLLoaderThrottle vector.
Jun Caif3aba7f92018-07-12 16:52:4042// 3. SimpleURLLoader in browser, it is implemented in a SimpleURLLoader wrapper
43// function variations::CreateSimpleURLLoaderWithVariationsHeaders().
44
asvitkine9a279832015-12-18 02:35:5045// static
[email protected]ab7780792013-01-10 01:26:0946VariationsHttpHeaderProvider* VariationsHttpHeaderProvider::GetInstance() {
olli.raula36aa8be2015-09-10 11:14:2247 return base::Singleton<VariationsHttpHeaderProvider>::get();
[email protected]bd3b4712012-12-18 17:01:3048}
49
yutak3305f49d2016-12-13 10:32:3150std::string VariationsHttpHeaderProvider::GetClientDataHeader(
51 bool is_signed_in) {
[email protected]bd3b4712012-12-18 17:01:3052 // Lazily initialize the header, if not already done, before attempting to
53 // transmit it.
54 InitVariationIDsCacheIfNeeded();
[email protected]ab7780792013-01-10 01:26:0955
56 std::string variation_ids_header_copy;
57 {
58 base::AutoLock scoped_lock(lock_);
yutak3305f49d2016-12-13 10:32:3159 variation_ids_header_copy = is_signed_in
60 ? cached_variation_ids_header_signed_in_
61 : cached_variation_ids_header_;
[email protected]ab7780792013-01-10 01:26:0962 }
asvitkine9a279832015-12-18 02:35:5063 return variation_ids_header_copy;
[email protected]bd3b4712012-12-18 17:01:3064}
65
asvitkine35ba6472015-12-18 23:52:3366std::string VariationsHttpHeaderProvider::GetVariationsString() {
67 InitVariationIDsCacheIfNeeded();
68
69 // Construct a space-separated string with leading and trailing spaces from
70 // the variations set. Note: The ids in it will be in sorted order per the
71 // std::set contract.
72 std::string ids_string = " ";
73 {
74 base::AutoLock scoped_lock(lock_);
yutak3305f49d2016-12-13 10:32:3175 for (const VariationIDEntry& entry : GetAllVariationIds()) {
76 if (entry.second == GOOGLE_WEB_PROPERTIES) {
77 ids_string.append(base::IntToString(entry.first));
78 ids_string.push_back(' ');
79 }
asvitkine35ba6472015-12-18 23:52:3380 }
81 }
82 return ids_string;
83}
84
Alexei Svitkine105f942e2018-02-17 02:53:4885VariationsHttpHeaderProvider::ForceIdsResult
86VariationsHttpHeaderProvider::ForceVariationIds(
87 const std::vector<std::string>& variation_ids,
88 const std::string& command_line_variation_ids) {
89 default_variation_ids_set_.clear();
90
91 if (!AddDefaultVariationIds(variation_ids))
92 return ForceIdsResult::INVALID_VECTOR_ENTRY;
93
jkrcalbf073372016-07-29 07:21:3194 if (!command_line_variation_ids.empty()) {
Alexei Svitkine105f942e2018-02-17 02:53:4895 std::vector<std::string> variation_ids_from_command_line =
jkrcalbf073372016-07-29 07:21:3196 base::SplitString(command_line_variation_ids, ",",
97 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
Alexei Svitkine105f942e2018-02-17 02:53:4898 if (!AddDefaultVariationIds(variation_ids_from_command_line))
99 return ForceIdsResult::INVALID_SWITCH_ENTRY;
jkrcalbf073372016-07-29 07:21:31100 }
Alexei Svitkine105f942e2018-02-17 02:53:48101 return ForceIdsResult::SUCCESS;
[email protected]1bd918d2013-10-13 18:23:09102}
103
asvitkineb4ed78682015-03-12 18:18:54104void VariationsHttpHeaderProvider::ResetForTesting() {
105 base::AutoLock scoped_lock(lock_);
106
107 // Stop observing field trials so that it can be restarted when this is
108 // re-inited. Note: This is a no-op if this is not currently observing.
109 base::FieldTrialList::RemoveObserver(this);
110 variation_ids_cache_initialized_ = false;
111}
112
[email protected]ab7780792013-01-10 01:26:09113VariationsHttpHeaderProvider::VariationsHttpHeaderProvider()
asvitkine9a279832015-12-18 02:35:50114 : variation_ids_cache_initialized_(false) {}
[email protected]bd3b4712012-12-18 17:01:30115
asvitkine9a279832015-12-18 02:35:50116VariationsHttpHeaderProvider::~VariationsHttpHeaderProvider() {}
[email protected]bd3b4712012-12-18 17:01:30117
[email protected]ab7780792013-01-10 01:26:09118void VariationsHttpHeaderProvider::OnFieldTrialGroupFinalized(
[email protected]bd3b4712012-12-18 17:01:30119 const std::string& trial_name,
120 const std::string& group_name) {
[email protected]bd3b4712012-12-18 17:01:30121 base::AutoLock scoped_lock(lock_);
yutak3305f49d2016-12-13 10:32:31122 const size_t old_size = variation_ids_set_.size();
123 CacheVariationsId(trial_name, group_name, GOOGLE_WEB_PROPERTIES);
124 CacheVariationsId(trial_name, group_name, GOOGLE_WEB_PROPERTIES_SIGNED_IN);
125 CacheVariationsId(trial_name, group_name, GOOGLE_WEB_PROPERTIES_TRIGGER);
126 if (variation_ids_set_.size() != old_size)
127 UpdateVariationIDsHeaderValue();
[email protected]bd3b4712012-12-18 17:01:30128}
129
asvitkinee0dbdbe2014-10-31 21:59:57130void VariationsHttpHeaderProvider::OnSyntheticTrialsChanged(
asvitkine9a279832015-12-18 02:35:50131 const std::vector<SyntheticTrialGroup>& groups) {
asvitkinee0dbdbe2014-10-31 21:59:57132 base::AutoLock scoped_lock(lock_);
133
134 synthetic_variation_ids_set_.clear();
asvitkine9a279832015-12-18 02:35:50135 for (const SyntheticTrialGroup& group : groups) {
yutak3305f49d2016-12-13 10:32:31136 VariationID id =
asvitkinee0dbdbe2014-10-31 21:59:57137 GetGoogleVariationIDFromHashes(GOOGLE_WEB_PROPERTIES, group.id);
yutak3305f49d2016-12-13 10:32:31138 if (id != EMPTY_ID) {
139 synthetic_variation_ids_set_.insert(
140 VariationIDEntry(id, GOOGLE_WEB_PROPERTIES));
141 }
142 id = GetGoogleVariationIDFromHashes(GOOGLE_WEB_PROPERTIES_SIGNED_IN,
143 group.id);
144 if (id != EMPTY_ID) {
145 synthetic_variation_ids_set_.insert(
146 VariationIDEntry(id, GOOGLE_WEB_PROPERTIES_SIGNED_IN));
147 }
asvitkinee0dbdbe2014-10-31 21:59:57148 }
149 UpdateVariationIDsHeaderValue();
150}
151
[email protected]ab7780792013-01-10 01:26:09152void VariationsHttpHeaderProvider::InitVariationIDsCacheIfNeeded() {
[email protected]bd3b4712012-12-18 17:01:30153 base::AutoLock scoped_lock(lock_);
154 if (variation_ids_cache_initialized_)
155 return;
156
157 // Register for additional cache updates. This is done first to avoid a race
158 // that could cause registered FieldTrials to be missed.
fdoraycddcf0e2016-06-23 21:20:30159 DCHECK(base::ThreadTaskRunnerHandle::IsSet());
[email protected]bd3b4712012-12-18 17:01:30160 base::FieldTrialList::AddObserver(this);
161
[email protected]999f7b42013-02-04 16:14:25162 base::TimeTicks before_time = base::TimeTicks::Now();
163
[email protected]bd3b4712012-12-18 17:01:30164 base::FieldTrial::ActiveGroups initial_groups;
165 base::FieldTrialList::GetActiveFieldTrialGroups(&initial_groups);
asvitkine35ba6472015-12-18 23:52:33166
167 for (const auto& entry : initial_groups) {
yutak3305f49d2016-12-13 10:32:31168 CacheVariationsId(entry.trial_name, entry.group_name,
169 GOOGLE_WEB_PROPERTIES);
170 CacheVariationsId(entry.trial_name, entry.group_name,
171 GOOGLE_WEB_PROPERTIES_SIGNED_IN);
172 CacheVariationsId(entry.trial_name, entry.group_name,
173 GOOGLE_WEB_PROPERTIES_TRIGGER);
[email protected]bd3b4712012-12-18 17:01:30174 }
175 UpdateVariationIDsHeaderValue();
176
[email protected]999f7b42013-02-04 16:14:25177 UMA_HISTOGRAM_CUSTOM_COUNTS(
178 "Variations.HeaderConstructionTime",
drbasicf0d1b262016-08-23 06:10:42179 (base::TimeTicks::Now() - before_time).InMicroseconds(), 1,
asvitkine9a279832015-12-18 02:35:50180 base::TimeDelta::FromSeconds(1).InMicroseconds(), 50);
[email protected]999f7b42013-02-04 16:14:25181
[email protected]bd3b4712012-12-18 17:01:30182 variation_ids_cache_initialized_ = true;
183}
184
yutak3305f49d2016-12-13 10:32:31185void VariationsHttpHeaderProvider::CacheVariationsId(
186 const std::string& trial_name,
187 const std::string& group_name,
188 IDCollectionKey key) {
189 const VariationID id = GetGoogleVariationID(key, trial_name, group_name);
190 if (id != EMPTY_ID)
191 variation_ids_set_.insert(VariationIDEntry(id, key));
192}
193
[email protected]ab7780792013-01-10 01:26:09194void VariationsHttpHeaderProvider::UpdateVariationIDsHeaderValue() {
195 lock_.AssertAcquired();
196
[email protected]bd3b4712012-12-18 17:01:30197 // The header value is a serialized protobuffer of Variation IDs which is
198 // base64 encoded before transmitting as a string.
yutak3305f49d2016-12-13 10:32:31199 cached_variation_ids_header_.clear();
200 cached_variation_ids_header_signed_in_.clear();
[email protected]1bd918d2013-10-13 18:23:09201
yutak3305f49d2016-12-13 10:32:31202 // If successful, swap the header value with the new one.
203 // Note that the list of IDs and the header could be temporarily out of sync
204 // if IDs are added as the header is recreated. The receiving servers are OK
205 // with such discrepancies.
206 cached_variation_ids_header_ = GenerateBase64EncodedProto(false);
207 cached_variation_ids_header_signed_in_ = GenerateBase64EncodedProto(true);
208}
209
210std::string VariationsHttpHeaderProvider::GenerateBase64EncodedProto(
211 bool is_signed_in) {
212 std::set<VariationIDEntry> all_variation_ids_set = GetAllVariationIds();
213
214 ClientVariations proto;
215 for (const VariationIDEntry& entry : all_variation_ids_set) {
216 switch (entry.second) {
217 case GOOGLE_WEB_PROPERTIES_SIGNED_IN:
218 if (is_signed_in)
219 proto.add_variation_id(entry.first);
220 break;
221 case GOOGLE_WEB_PROPERTIES:
222 proto.add_variation_id(entry.first);
223 break;
224 case GOOGLE_WEB_PROPERTIES_TRIGGER:
225 proto.add_trigger_variation_id(entry.first);
226 break;
Sky Malicea846ad7d2017-12-05 00:44:42227 case CHROME_SYNC_EVENT_LOGGER:
yutak3305f49d2016-12-13 10:32:31228 case ID_COLLECTION_COUNT:
229 // These cases included to get full enum coverage for switch, so that
230 // new enums introduce compiler warnings. Nothing to do for these.
231 break;
232 }
[email protected]8c2c5442014-04-04 18:55:29233 }
[email protected]bd3b4712012-12-18 17:01:30234
yutak3305f49d2016-12-13 10:32:31235 const size_t total_id_count =
236 proto.variation_id_size() + proto.trigger_variation_id_size();
237
238 if (total_id_count == 0)
239 return std::string();
240
[email protected]bd3b4712012-12-18 17:01:30241 // This is the bottleneck for the creation of the header, so validate the size
242 // here. Force a hard maximum on the ID count in case the Variations server
243 // returns too many IDs and DOSs receiving servers with large requests.
Alexei Svitkine93c85fc2018-07-20 20:21:05244 DCHECK_LE(total_id_count, 20U);
[email protected]a27ae2a2014-08-01 16:17:52245 UMA_HISTOGRAM_COUNTS_100("Variations.Headers.ExperimentCount",
246 total_id_count);
Alexei Svitkine93c85fc2018-07-20 20:21:05247 if (total_id_count > 30)
yutak3305f49d2016-12-13 10:32:31248 return std::string();
[email protected]8c2c5442014-04-04 18:55:29249
[email protected]bd3b4712012-12-18 17:01:30250 std::string serialized;
251 proto.SerializeToString(&serialized);
252
253 std::string hashed;
[email protected]33fca122013-12-11 01:48:50254 base::Base64Encode(serialized, &hashed);
yutak3305f49d2016-12-13 10:32:31255 return hashed;
[email protected]bd3b4712012-12-18 17:01:30256}
[email protected]ab7780792013-01-10 01:26:09257
Alexei Svitkine105f942e2018-02-17 02:53:48258bool VariationsHttpHeaderProvider::AddDefaultVariationIds(
259 const std::vector<std::string>& variation_ids) {
260 for (const std::string& entry : variation_ids) {
261 if (entry.empty()) {
262 default_variation_ids_set_.clear();
263 return false;
264 }
265 bool trigger_id =
266 base::StartsWith(entry, "t", base::CompareCase::SENSITIVE);
267 // Remove the "t" prefix if it's there.
268 std::string trimmed_entry = trigger_id ? entry.substr(1) : entry;
269
270 int variation_id = 0;
271 if (!base::StringToInt(trimmed_entry, &variation_id)) {
272 default_variation_ids_set_.clear();
273 return false;
274 }
275 default_variation_ids_set_.insert(VariationIDEntry(
276 variation_id,
277 trigger_id ? GOOGLE_WEB_PROPERTIES_TRIGGER : GOOGLE_WEB_PROPERTIES));
278 }
279 return true;
280}
281
yutak3305f49d2016-12-13 10:32:31282std::set<VariationsHttpHeaderProvider::VariationIDEntry>
283VariationsHttpHeaderProvider::GetAllVariationIds() {
asvitkine35ba6472015-12-18 23:52:33284 lock_.AssertAcquired();
285
yutak3305f49d2016-12-13 10:32:31286 std::set<VariationIDEntry> all_variation_ids_set = default_variation_ids_set_;
287 for (const VariationIDEntry& entry : variation_ids_set_) {
288 all_variation_ids_set.insert(entry);
289 }
290 for (const VariationIDEntry& entry : synthetic_variation_ids_set_) {
291 all_variation_ids_set.insert(entry);
292 }
asvitkine35ba6472015-12-18 23:52:33293 return all_variation_ids_set;
294}
295
[email protected]71011c1682014-07-09 17:19:16296} // namespace variations