blob: 29084615b683d3095354e7902cf0e69fc84d3230 [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
Jun Cai0e568632018-08-09 02:05:33104void VariationsHttpHeaderProvider::AddObserver(Observer* observer) {
105 observer_list_.AddObserver(observer);
106}
107
108void VariationsHttpHeaderProvider::RemoveObserver(Observer* observer) {
109 observer_list_.RemoveObserver(observer);
110}
111
asvitkineb4ed78682015-03-12 18:18:54112void VariationsHttpHeaderProvider::ResetForTesting() {
113 base::AutoLock scoped_lock(lock_);
114
115 // Stop observing field trials so that it can be restarted when this is
116 // re-inited. Note: This is a no-op if this is not currently observing.
117 base::FieldTrialList::RemoveObserver(this);
118 variation_ids_cache_initialized_ = false;
Olivier Robin9edc20f2018-10-03 09:51:42119 variation_ids_set_.clear();
120 default_variation_ids_set_.clear();
121 synthetic_variation_ids_set_.clear();
122 cached_variation_ids_header_.clear();
123 cached_variation_ids_header_signed_in_.clear();
asvitkineb4ed78682015-03-12 18:18:54124}
125
[email protected]ab7780792013-01-10 01:26:09126VariationsHttpHeaderProvider::VariationsHttpHeaderProvider()
asvitkine9a279832015-12-18 02:35:50127 : variation_ids_cache_initialized_(false) {}
[email protected]bd3b4712012-12-18 17:01:30128
asvitkine9a279832015-12-18 02:35:50129VariationsHttpHeaderProvider::~VariationsHttpHeaderProvider() {}
[email protected]bd3b4712012-12-18 17:01:30130
[email protected]ab7780792013-01-10 01:26:09131void VariationsHttpHeaderProvider::OnFieldTrialGroupFinalized(
[email protected]bd3b4712012-12-18 17:01:30132 const std::string& trial_name,
133 const std::string& group_name) {
[email protected]bd3b4712012-12-18 17:01:30134 base::AutoLock scoped_lock(lock_);
yutak3305f49d2016-12-13 10:32:31135 const size_t old_size = variation_ids_set_.size();
136 CacheVariationsId(trial_name, group_name, GOOGLE_WEB_PROPERTIES);
137 CacheVariationsId(trial_name, group_name, GOOGLE_WEB_PROPERTIES_SIGNED_IN);
138 CacheVariationsId(trial_name, group_name, GOOGLE_WEB_PROPERTIES_TRIGGER);
139 if (variation_ids_set_.size() != old_size)
140 UpdateVariationIDsHeaderValue();
[email protected]bd3b4712012-12-18 17:01:30141}
142
asvitkinee0dbdbe2014-10-31 21:59:57143void VariationsHttpHeaderProvider::OnSyntheticTrialsChanged(
asvitkine9a279832015-12-18 02:35:50144 const std::vector<SyntheticTrialGroup>& groups) {
asvitkinee0dbdbe2014-10-31 21:59:57145 base::AutoLock scoped_lock(lock_);
146
147 synthetic_variation_ids_set_.clear();
asvitkine9a279832015-12-18 02:35:50148 for (const SyntheticTrialGroup& group : groups) {
yutak3305f49d2016-12-13 10:32:31149 VariationID id =
asvitkinee0dbdbe2014-10-31 21:59:57150 GetGoogleVariationIDFromHashes(GOOGLE_WEB_PROPERTIES, group.id);
yutak3305f49d2016-12-13 10:32:31151 if (id != EMPTY_ID) {
152 synthetic_variation_ids_set_.insert(
153 VariationIDEntry(id, GOOGLE_WEB_PROPERTIES));
154 }
155 id = GetGoogleVariationIDFromHashes(GOOGLE_WEB_PROPERTIES_SIGNED_IN,
156 group.id);
157 if (id != EMPTY_ID) {
158 synthetic_variation_ids_set_.insert(
159 VariationIDEntry(id, GOOGLE_WEB_PROPERTIES_SIGNED_IN));
160 }
asvitkinee0dbdbe2014-10-31 21:59:57161 }
162 UpdateVariationIDsHeaderValue();
163}
164
[email protected]ab7780792013-01-10 01:26:09165void VariationsHttpHeaderProvider::InitVariationIDsCacheIfNeeded() {
[email protected]bd3b4712012-12-18 17:01:30166 base::AutoLock scoped_lock(lock_);
167 if (variation_ids_cache_initialized_)
168 return;
169
170 // Register for additional cache updates. This is done first to avoid a race
171 // that could cause registered FieldTrials to be missed.
fdoraycddcf0e2016-06-23 21:20:30172 DCHECK(base::ThreadTaskRunnerHandle::IsSet());
[email protected]bd3b4712012-12-18 17:01:30173 base::FieldTrialList::AddObserver(this);
174
[email protected]999f7b42013-02-04 16:14:25175 base::TimeTicks before_time = base::TimeTicks::Now();
176
[email protected]bd3b4712012-12-18 17:01:30177 base::FieldTrial::ActiveGroups initial_groups;
178 base::FieldTrialList::GetActiveFieldTrialGroups(&initial_groups);
asvitkine35ba6472015-12-18 23:52:33179
180 for (const auto& entry : initial_groups) {
yutak3305f49d2016-12-13 10:32:31181 CacheVariationsId(entry.trial_name, entry.group_name,
182 GOOGLE_WEB_PROPERTIES);
183 CacheVariationsId(entry.trial_name, entry.group_name,
184 GOOGLE_WEB_PROPERTIES_SIGNED_IN);
185 CacheVariationsId(entry.trial_name, entry.group_name,
186 GOOGLE_WEB_PROPERTIES_TRIGGER);
[email protected]bd3b4712012-12-18 17:01:30187 }
188 UpdateVariationIDsHeaderValue();
189
[email protected]999f7b42013-02-04 16:14:25190 UMA_HISTOGRAM_CUSTOM_COUNTS(
191 "Variations.HeaderConstructionTime",
drbasicf0d1b262016-08-23 06:10:42192 (base::TimeTicks::Now() - before_time).InMicroseconds(), 1,
asvitkine9a279832015-12-18 02:35:50193 base::TimeDelta::FromSeconds(1).InMicroseconds(), 50);
[email protected]999f7b42013-02-04 16:14:25194
[email protected]bd3b4712012-12-18 17:01:30195 variation_ids_cache_initialized_ = true;
196}
197
yutak3305f49d2016-12-13 10:32:31198void VariationsHttpHeaderProvider::CacheVariationsId(
199 const std::string& trial_name,
200 const std::string& group_name,
201 IDCollectionKey key) {
202 const VariationID id = GetGoogleVariationID(key, trial_name, group_name);
203 if (id != EMPTY_ID)
204 variation_ids_set_.insert(VariationIDEntry(id, key));
205}
206
[email protected]ab7780792013-01-10 01:26:09207void VariationsHttpHeaderProvider::UpdateVariationIDsHeaderValue() {
208 lock_.AssertAcquired();
209
[email protected]bd3b4712012-12-18 17:01:30210 // The header value is a serialized protobuffer of Variation IDs which is
211 // base64 encoded before transmitting as a string.
yutak3305f49d2016-12-13 10:32:31212 cached_variation_ids_header_.clear();
213 cached_variation_ids_header_signed_in_.clear();
[email protected]1bd918d2013-10-13 18:23:09214
yutak3305f49d2016-12-13 10:32:31215 // If successful, swap the header value with the new one.
216 // Note that the list of IDs and the header could be temporarily out of sync
217 // if IDs are added as the header is recreated. The receiving servers are OK
218 // with such discrepancies.
219 cached_variation_ids_header_ = GenerateBase64EncodedProto(false);
220 cached_variation_ids_header_signed_in_ = GenerateBase64EncodedProto(true);
Jun Cai0e568632018-08-09 02:05:33221
222 for (auto& observer : observer_list_) {
223 observer.VariationIdsHeaderUpdated(cached_variation_ids_header_,
224 cached_variation_ids_header_signed_in_);
225 }
yutak3305f49d2016-12-13 10:32:31226}
227
228std::string VariationsHttpHeaderProvider::GenerateBase64EncodedProto(
229 bool is_signed_in) {
230 std::set<VariationIDEntry> all_variation_ids_set = GetAllVariationIds();
231
232 ClientVariations proto;
233 for (const VariationIDEntry& entry : all_variation_ids_set) {
234 switch (entry.second) {
235 case GOOGLE_WEB_PROPERTIES_SIGNED_IN:
236 if (is_signed_in)
237 proto.add_variation_id(entry.first);
238 break;
239 case GOOGLE_WEB_PROPERTIES:
240 proto.add_variation_id(entry.first);
241 break;
242 case GOOGLE_WEB_PROPERTIES_TRIGGER:
243 proto.add_trigger_variation_id(entry.first);
244 break;
Sky Malicea846ad7d2017-12-05 00:44:42245 case CHROME_SYNC_EVENT_LOGGER:
yutak3305f49d2016-12-13 10:32:31246 case ID_COLLECTION_COUNT:
247 // These cases included to get full enum coverage for switch, so that
248 // new enums introduce compiler warnings. Nothing to do for these.
249 break;
250 }
[email protected]8c2c5442014-04-04 18:55:29251 }
[email protected]bd3b4712012-12-18 17:01:30252
yutak3305f49d2016-12-13 10:32:31253 const size_t total_id_count =
254 proto.variation_id_size() + proto.trigger_variation_id_size();
255
256 if (total_id_count == 0)
257 return std::string();
258
[email protected]bd3b4712012-12-18 17:01:30259 // This is the bottleneck for the creation of the header, so validate the size
260 // here. Force a hard maximum on the ID count in case the Variations server
261 // returns too many IDs and DOSs receiving servers with large requests.
Alexei Svitkine93c85fc2018-07-20 20:21:05262 DCHECK_LE(total_id_count, 20U);
[email protected]a27ae2a2014-08-01 16:17:52263 UMA_HISTOGRAM_COUNTS_100("Variations.Headers.ExperimentCount",
264 total_id_count);
Alexei Svitkine93c85fc2018-07-20 20:21:05265 if (total_id_count > 30)
yutak3305f49d2016-12-13 10:32:31266 return std::string();
[email protected]8c2c5442014-04-04 18:55:29267
[email protected]bd3b4712012-12-18 17:01:30268 std::string serialized;
269 proto.SerializeToString(&serialized);
270
271 std::string hashed;
[email protected]33fca122013-12-11 01:48:50272 base::Base64Encode(serialized, &hashed);
yutak3305f49d2016-12-13 10:32:31273 return hashed;
[email protected]bd3b4712012-12-18 17:01:30274}
[email protected]ab7780792013-01-10 01:26:09275
Alexei Svitkine105f942e2018-02-17 02:53:48276bool VariationsHttpHeaderProvider::AddDefaultVariationIds(
277 const std::vector<std::string>& variation_ids) {
278 for (const std::string& entry : variation_ids) {
279 if (entry.empty()) {
280 default_variation_ids_set_.clear();
281 return false;
282 }
283 bool trigger_id =
284 base::StartsWith(entry, "t", base::CompareCase::SENSITIVE);
285 // Remove the "t" prefix if it's there.
286 std::string trimmed_entry = trigger_id ? entry.substr(1) : entry;
287
288 int variation_id = 0;
289 if (!base::StringToInt(trimmed_entry, &variation_id)) {
290 default_variation_ids_set_.clear();
291 return false;
292 }
293 default_variation_ids_set_.insert(VariationIDEntry(
294 variation_id,
295 trigger_id ? GOOGLE_WEB_PROPERTIES_TRIGGER : GOOGLE_WEB_PROPERTIES));
296 }
297 return true;
298}
299
yutak3305f49d2016-12-13 10:32:31300std::set<VariationsHttpHeaderProvider::VariationIDEntry>
301VariationsHttpHeaderProvider::GetAllVariationIds() {
asvitkine35ba6472015-12-18 23:52:33302 lock_.AssertAcquired();
303
yutak3305f49d2016-12-13 10:32:31304 std::set<VariationIDEntry> all_variation_ids_set = default_variation_ids_set_;
305 for (const VariationIDEntry& entry : variation_ids_set_) {
306 all_variation_ids_set.insert(entry);
307 }
308 for (const VariationIDEntry& entry : synthetic_variation_ids_set_) {
309 all_variation_ids_set.insert(entry);
310 }
asvitkine35ba6472015-12-18 23:52:33311 return all_variation_ids_set;
312}
313
[email protected]71011c1682014-07-09 17:19:16314} // namespace variations