[email protected] | 71011c168 | 2014-07-09 17:19:16 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 5 | #include "components/variations/variations_http_header_provider.h" |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 6 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
horo | e09b6c8 | 2014-11-01 02:08:28 | [diff] [blame] | 9 | #include <set> |
| 10 | #include <string> |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 13 | #include "base/base64.h" |
| 14 | #include "base/memory/singleton.h" |
asvitkine | 454600f | 2015-06-16 16:34:50 | [diff] [blame] | 15 | #include "base/metrics/histogram_macros.h" |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 16 | #include "base/strings/string_number_conversions.h" |
| 17 | #include "base/strings/string_split.h" |
[email protected] | 7f8a993 | 2013-07-26 20:43:34 | [diff] [blame] | 18 | #include "base/strings/string_util.h" |
fdoray | cddcf0e | 2016-06-23 21:20:30 | [diff] [blame] | 19 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | ea15bd5 | 2014-07-14 22:42:50 | [diff] [blame] | 20 | #include "components/variations/proto/client_variations.pb.h" |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 21 | |
[email protected] | 71011c168 | 2014-07-09 17:19:16 | [diff] [blame] | 22 | namespace variations { |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 23 | |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 24 | // static |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 25 | VariationsHttpHeaderProvider* VariationsHttpHeaderProvider::GetInstance() { |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 26 | return base::Singleton<VariationsHttpHeaderProvider>::get(); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 27 | } |
| 28 | |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 29 | std::string VariationsHttpHeaderProvider::GetClientDataHeader( |
| 30 | bool is_signed_in) { |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 31 | // Lazily initialize the header, if not already done, before attempting to |
| 32 | // transmit it. |
| 33 | InitVariationIDsCacheIfNeeded(); |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 34 | |
| 35 | std::string variation_ids_header_copy; |
| 36 | { |
| 37 | base::AutoLock scoped_lock(lock_); |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 38 | variation_ids_header_copy = is_signed_in |
| 39 | ? cached_variation_ids_header_signed_in_ |
| 40 | : cached_variation_ids_header_; |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 41 | } |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 42 | return variation_ids_header_copy; |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 43 | } |
| 44 | |
asvitkine | 35ba647 | 2015-12-18 23:52:33 | [diff] [blame] | 45 | std::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_); |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 54 | 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 | } |
asvitkine | 35ba647 | 2015-12-18 23:52:33 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | return ids_string; |
| 62 | } |
| 63 | |
Alexei Svitkine | 105f942e | 2018-02-17 02:53:48 | [diff] [blame^] | 64 | VariationsHttpHeaderProvider::ForceIdsResult |
| 65 | VariationsHttpHeaderProvider::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 | |
jkrcal | bf07337 | 2016-07-29 07:21:31 | [diff] [blame] | 73 | if (!command_line_variation_ids.empty()) { |
Alexei Svitkine | 105f942e | 2018-02-17 02:53:48 | [diff] [blame^] | 74 | std::vector<std::string> variation_ids_from_command_line = |
jkrcal | bf07337 | 2016-07-29 07:21:31 | [diff] [blame] | 75 | base::SplitString(command_line_variation_ids, ",", |
| 76 | base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Alexei Svitkine | 105f942e | 2018-02-17 02:53:48 | [diff] [blame^] | 77 | if (!AddDefaultVariationIds(variation_ids_from_command_line)) |
| 78 | return ForceIdsResult::INVALID_SWITCH_ENTRY; |
jkrcal | bf07337 | 2016-07-29 07:21:31 | [diff] [blame] | 79 | } |
Alexei Svitkine | 105f942e | 2018-02-17 02:53:48 | [diff] [blame^] | 80 | return ForceIdsResult::SUCCESS; |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 81 | } |
| 82 | |
asvitkine | b4ed7868 | 2015-03-12 18:18:54 | [diff] [blame] | 83 | void 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] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 92 | VariationsHttpHeaderProvider::VariationsHttpHeaderProvider() |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 93 | : variation_ids_cache_initialized_(false) {} |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 94 | |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 95 | VariationsHttpHeaderProvider::~VariationsHttpHeaderProvider() {} |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 96 | |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 97 | void VariationsHttpHeaderProvider::OnFieldTrialGroupFinalized( |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 98 | const std::string& trial_name, |
| 99 | const std::string& group_name) { |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 100 | base::AutoLock scoped_lock(lock_); |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 101 | 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] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 107 | } |
| 108 | |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 109 | void VariationsHttpHeaderProvider::OnSyntheticTrialsChanged( |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 110 | const std::vector<SyntheticTrialGroup>& groups) { |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 111 | base::AutoLock scoped_lock(lock_); |
| 112 | |
| 113 | synthetic_variation_ids_set_.clear(); |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 114 | for (const SyntheticTrialGroup& group : groups) { |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 115 | VariationID id = |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 116 | GetGoogleVariationIDFromHashes(GOOGLE_WEB_PROPERTIES, group.id); |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 117 | 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 | } |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 127 | } |
| 128 | UpdateVariationIDsHeaderValue(); |
| 129 | } |
| 130 | |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 131 | void VariationsHttpHeaderProvider::InitVariationIDsCacheIfNeeded() { |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 132 | 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. |
fdoray | cddcf0e | 2016-06-23 21:20:30 | [diff] [blame] | 138 | DCHECK(base::ThreadTaskRunnerHandle::IsSet()); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 139 | base::FieldTrialList::AddObserver(this); |
| 140 | |
[email protected] | 999f7b4 | 2013-02-04 16:14:25 | [diff] [blame] | 141 | base::TimeTicks before_time = base::TimeTicks::Now(); |
| 142 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 143 | base::FieldTrial::ActiveGroups initial_groups; |
| 144 | base::FieldTrialList::GetActiveFieldTrialGroups(&initial_groups); |
asvitkine | 35ba647 | 2015-12-18 23:52:33 | [diff] [blame] | 145 | |
| 146 | for (const auto& entry : initial_groups) { |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 147 | 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] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 153 | } |
| 154 | UpdateVariationIDsHeaderValue(); |
| 155 | |
[email protected] | 999f7b4 | 2013-02-04 16:14:25 | [diff] [blame] | 156 | UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 157 | "Variations.HeaderConstructionTime", |
drbasic | f0d1b26 | 2016-08-23 06:10:42 | [diff] [blame] | 158 | (base::TimeTicks::Now() - before_time).InMicroseconds(), 1, |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 159 | base::TimeDelta::FromSeconds(1).InMicroseconds(), 50); |
[email protected] | 999f7b4 | 2013-02-04 16:14:25 | [diff] [blame] | 160 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 161 | variation_ids_cache_initialized_ = true; |
| 162 | } |
| 163 | |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 164 | void 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] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 173 | void VariationsHttpHeaderProvider::UpdateVariationIDsHeaderValue() { |
| 174 | lock_.AssertAcquired(); |
| 175 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 176 | // The header value is a serialized protobuffer of Variation IDs which is |
| 177 | // base64 encoded before transmitting as a string. |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 178 | cached_variation_ids_header_.clear(); |
| 179 | cached_variation_ids_header_signed_in_.clear(); |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 180 | |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 181 | // 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 | |
| 189 | std::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 Malice | a846ad7d | 2017-12-05 00:44:42 | [diff] [blame] | 206 | case CHROME_SYNC_EVENT_LOGGER: |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 207 | 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] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 212 | } |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 213 | |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 214 | 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] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 220 | // 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] | e51dcb0c | 2014-05-06 16:56:10 | [diff] [blame] | 223 | DCHECK_LE(total_id_count, 10U); |
[email protected] | a27ae2a | 2014-08-01 16:17:52 | [diff] [blame] | 224 | UMA_HISTOGRAM_COUNTS_100("Variations.Headers.ExperimentCount", |
| 225 | total_id_count); |
[email protected] | e51dcb0c | 2014-05-06 16:56:10 | [diff] [blame] | 226 | if (total_id_count > 20) |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 227 | return std::string(); |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 228 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 229 | std::string serialized; |
| 230 | proto.SerializeToString(&serialized); |
| 231 | |
| 232 | std::string hashed; |
[email protected] | 33fca12 | 2013-12-11 01:48:50 | [diff] [blame] | 233 | base::Base64Encode(serialized, &hashed); |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 234 | return hashed; |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 235 | } |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 236 | |
Alexei Svitkine | 105f942e | 2018-02-17 02:53:48 | [diff] [blame^] | 237 | bool 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 | |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 261 | std::set<VariationsHttpHeaderProvider::VariationIDEntry> |
| 262 | VariationsHttpHeaderProvider::GetAllVariationIds() { |
asvitkine | 35ba647 | 2015-12-18 23:52:33 | [diff] [blame] | 263 | lock_.AssertAcquired(); |
| 264 | |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 265 | 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 | } |
asvitkine | 35ba647 | 2015-12-18 23:52:33 | [diff] [blame] | 272 | return all_variation_ids_set; |
| 273 | } |
| 274 | |
[email protected] | 71011c168 | 2014-07-09 17:19:16 | [diff] [blame] | 275 | } // namespace variations |