[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 | |
jkrcal | bf07337 | 2016-07-29 07:21:31 | [diff] [blame] | 64 | bool VariationsHttpHeaderProvider::ForceVariationIds( |
| 65 | const std::string& command_line_variation_ids, |
| 66 | std::vector<std::string>* variation_ids) { |
| 67 | if (!command_line_variation_ids.empty()) { |
| 68 | // Combine |variation_ids| with |command_line_variation_ids|. |
| 69 | std::vector<std::string> variation_ids_flags = |
| 70 | base::SplitString(command_line_variation_ids, ",", |
| 71 | base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 72 | variation_ids->insert(variation_ids->end(), variation_ids_flags.begin(), |
| 73 | variation_ids_flags.end()); |
| 74 | } |
| 75 | |
| 76 | if (!variation_ids->empty()) { |
| 77 | // Create default variation ids which will always be included in the |
| 78 | // X-Client-Data request header. |
| 79 | return SetDefaultVariationIds(*variation_ids); |
| 80 | } |
| 81 | return true; |
| 82 | } |
| 83 | |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 84 | bool VariationsHttpHeaderProvider::SetDefaultVariationIds( |
jkrcal | bf07337 | 2016-07-29 07:21:31 | [diff] [blame] | 85 | const std::vector<std::string>& variation_ids) { |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 86 | default_variation_ids_set_.clear(); |
jkrcal | bf07337 | 2016-07-29 07:21:31 | [diff] [blame] | 87 | for (const std::string& entry : variation_ids) { |
brettw | 8be197d1 | 2015-07-23 23:23:31 | [diff] [blame] | 88 | if (entry.empty()) { |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 89 | default_variation_ids_set_.clear(); |
| 90 | return false; |
| 91 | } |
brettw | 8be197d1 | 2015-07-23 23:23:31 | [diff] [blame] | 92 | bool trigger_id = |
| 93 | base::StartsWith(entry, "t", base::CompareCase::SENSITIVE); |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 94 | // Remove the "t" prefix if it's there. |
jkrcal | bf07337 | 2016-07-29 07:21:31 | [diff] [blame] | 95 | std::string trimmed_entry = trigger_id ? entry.substr(1) : entry; |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 96 | |
| 97 | int variation_id = 0; |
brettw | 8be197d1 | 2015-07-23 23:23:31 | [diff] [blame] | 98 | if (!base::StringToInt(trimmed_entry, &variation_id)) { |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 99 | default_variation_ids_set_.clear(); |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 100 | return false; |
| 101 | } |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 102 | default_variation_ids_set_.insert(VariationIDEntry( |
| 103 | variation_id, |
| 104 | trigger_id ? GOOGLE_WEB_PROPERTIES_TRIGGER : GOOGLE_WEB_PROPERTIES)); |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 105 | } |
| 106 | return true; |
| 107 | } |
| 108 | |
asvitkine | b4ed7868 | 2015-03-12 18:18:54 | [diff] [blame] | 109 | void VariationsHttpHeaderProvider::ResetForTesting() { |
| 110 | base::AutoLock scoped_lock(lock_); |
| 111 | |
| 112 | // Stop observing field trials so that it can be restarted when this is |
| 113 | // re-inited. Note: This is a no-op if this is not currently observing. |
| 114 | base::FieldTrialList::RemoveObserver(this); |
| 115 | variation_ids_cache_initialized_ = false; |
| 116 | } |
| 117 | |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 118 | VariationsHttpHeaderProvider::VariationsHttpHeaderProvider() |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 119 | : variation_ids_cache_initialized_(false) {} |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 120 | |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 121 | VariationsHttpHeaderProvider::~VariationsHttpHeaderProvider() {} |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 122 | |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 123 | void VariationsHttpHeaderProvider::OnFieldTrialGroupFinalized( |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 124 | const std::string& trial_name, |
| 125 | const std::string& group_name) { |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 126 | base::AutoLock scoped_lock(lock_); |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 127 | const size_t old_size = variation_ids_set_.size(); |
| 128 | CacheVariationsId(trial_name, group_name, GOOGLE_WEB_PROPERTIES); |
| 129 | CacheVariationsId(trial_name, group_name, GOOGLE_WEB_PROPERTIES_SIGNED_IN); |
| 130 | CacheVariationsId(trial_name, group_name, GOOGLE_WEB_PROPERTIES_TRIGGER); |
| 131 | if (variation_ids_set_.size() != old_size) |
| 132 | UpdateVariationIDsHeaderValue(); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 133 | } |
| 134 | |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 135 | void VariationsHttpHeaderProvider::OnSyntheticTrialsChanged( |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 136 | const std::vector<SyntheticTrialGroup>& groups) { |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 137 | base::AutoLock scoped_lock(lock_); |
| 138 | |
| 139 | synthetic_variation_ids_set_.clear(); |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 140 | for (const SyntheticTrialGroup& group : groups) { |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 141 | VariationID id = |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 142 | GetGoogleVariationIDFromHashes(GOOGLE_WEB_PROPERTIES, group.id); |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 143 | if (id != EMPTY_ID) { |
| 144 | synthetic_variation_ids_set_.insert( |
| 145 | VariationIDEntry(id, GOOGLE_WEB_PROPERTIES)); |
| 146 | } |
| 147 | id = GetGoogleVariationIDFromHashes(GOOGLE_WEB_PROPERTIES_SIGNED_IN, |
| 148 | group.id); |
| 149 | if (id != EMPTY_ID) { |
| 150 | synthetic_variation_ids_set_.insert( |
| 151 | VariationIDEntry(id, GOOGLE_WEB_PROPERTIES_SIGNED_IN)); |
| 152 | } |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 153 | } |
| 154 | UpdateVariationIDsHeaderValue(); |
| 155 | } |
| 156 | |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 157 | void VariationsHttpHeaderProvider::InitVariationIDsCacheIfNeeded() { |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 158 | base::AutoLock scoped_lock(lock_); |
| 159 | if (variation_ids_cache_initialized_) |
| 160 | return; |
| 161 | |
| 162 | // Register for additional cache updates. This is done first to avoid a race |
| 163 | // that could cause registered FieldTrials to be missed. |
fdoray | cddcf0e | 2016-06-23 21:20:30 | [diff] [blame] | 164 | DCHECK(base::ThreadTaskRunnerHandle::IsSet()); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 165 | base::FieldTrialList::AddObserver(this); |
| 166 | |
[email protected] | 999f7b4 | 2013-02-04 16:14:25 | [diff] [blame] | 167 | base::TimeTicks before_time = base::TimeTicks::Now(); |
| 168 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 169 | base::FieldTrial::ActiveGroups initial_groups; |
| 170 | base::FieldTrialList::GetActiveFieldTrialGroups(&initial_groups); |
asvitkine | 35ba647 | 2015-12-18 23:52:33 | [diff] [blame] | 171 | |
| 172 | for (const auto& entry : initial_groups) { |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 173 | CacheVariationsId(entry.trial_name, entry.group_name, |
| 174 | GOOGLE_WEB_PROPERTIES); |
| 175 | CacheVariationsId(entry.trial_name, entry.group_name, |
| 176 | GOOGLE_WEB_PROPERTIES_SIGNED_IN); |
| 177 | CacheVariationsId(entry.trial_name, entry.group_name, |
| 178 | GOOGLE_WEB_PROPERTIES_TRIGGER); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 179 | } |
| 180 | UpdateVariationIDsHeaderValue(); |
| 181 | |
[email protected] | 999f7b4 | 2013-02-04 16:14:25 | [diff] [blame] | 182 | UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 183 | "Variations.HeaderConstructionTime", |
drbasic | f0d1b26 | 2016-08-23 06:10:42 | [diff] [blame] | 184 | (base::TimeTicks::Now() - before_time).InMicroseconds(), 1, |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 185 | base::TimeDelta::FromSeconds(1).InMicroseconds(), 50); |
[email protected] | 999f7b4 | 2013-02-04 16:14:25 | [diff] [blame] | 186 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 187 | variation_ids_cache_initialized_ = true; |
| 188 | } |
| 189 | |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 190 | void VariationsHttpHeaderProvider::CacheVariationsId( |
| 191 | const std::string& trial_name, |
| 192 | const std::string& group_name, |
| 193 | IDCollectionKey key) { |
| 194 | const VariationID id = GetGoogleVariationID(key, trial_name, group_name); |
| 195 | if (id != EMPTY_ID) |
| 196 | variation_ids_set_.insert(VariationIDEntry(id, key)); |
| 197 | } |
| 198 | |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 199 | void VariationsHttpHeaderProvider::UpdateVariationIDsHeaderValue() { |
| 200 | lock_.AssertAcquired(); |
| 201 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 202 | // The header value is a serialized protobuffer of Variation IDs which is |
| 203 | // base64 encoded before transmitting as a string. |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 204 | cached_variation_ids_header_.clear(); |
| 205 | cached_variation_ids_header_signed_in_.clear(); |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 206 | |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 207 | // If successful, swap the header value with the new one. |
| 208 | // Note that the list of IDs and the header could be temporarily out of sync |
| 209 | // if IDs are added as the header is recreated. The receiving servers are OK |
| 210 | // with such discrepancies. |
| 211 | cached_variation_ids_header_ = GenerateBase64EncodedProto(false); |
| 212 | cached_variation_ids_header_signed_in_ = GenerateBase64EncodedProto(true); |
| 213 | } |
| 214 | |
| 215 | std::string VariationsHttpHeaderProvider::GenerateBase64EncodedProto( |
| 216 | bool is_signed_in) { |
| 217 | std::set<VariationIDEntry> all_variation_ids_set = GetAllVariationIds(); |
| 218 | |
| 219 | ClientVariations proto; |
| 220 | for (const VariationIDEntry& entry : all_variation_ids_set) { |
| 221 | switch (entry.second) { |
| 222 | case GOOGLE_WEB_PROPERTIES_SIGNED_IN: |
| 223 | if (is_signed_in) |
| 224 | proto.add_variation_id(entry.first); |
| 225 | break; |
| 226 | case GOOGLE_WEB_PROPERTIES: |
| 227 | proto.add_variation_id(entry.first); |
| 228 | break; |
| 229 | case GOOGLE_WEB_PROPERTIES_TRIGGER: |
| 230 | proto.add_trigger_variation_id(entry.first); |
| 231 | break; |
Sky Malice | a846ad7d | 2017-12-05 00:44:42 | [diff] [blame^] | 232 | case CHROME_SYNC_EVENT_LOGGER: |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 233 | case ID_COLLECTION_COUNT: |
| 234 | // These cases included to get full enum coverage for switch, so that |
| 235 | // new enums introduce compiler warnings. Nothing to do for these. |
| 236 | break; |
| 237 | } |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 238 | } |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 239 | |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 240 | const size_t total_id_count = |
| 241 | proto.variation_id_size() + proto.trigger_variation_id_size(); |
| 242 | |
| 243 | if (total_id_count == 0) |
| 244 | return std::string(); |
| 245 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 246 | // This is the bottleneck for the creation of the header, so validate the size |
| 247 | // here. Force a hard maximum on the ID count in case the Variations server |
| 248 | // returns too many IDs and DOSs receiving servers with large requests. |
[email protected] | e51dcb0c | 2014-05-06 16:56:10 | [diff] [blame] | 249 | DCHECK_LE(total_id_count, 10U); |
[email protected] | a27ae2a | 2014-08-01 16:17:52 | [diff] [blame] | 250 | UMA_HISTOGRAM_COUNTS_100("Variations.Headers.ExperimentCount", |
| 251 | total_id_count); |
[email protected] | e51dcb0c | 2014-05-06 16:56:10 | [diff] [blame] | 252 | if (total_id_count > 20) |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 253 | return std::string(); |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 254 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 255 | std::string serialized; |
| 256 | proto.SerializeToString(&serialized); |
| 257 | |
| 258 | std::string hashed; |
[email protected] | 33fca12 | 2013-12-11 01:48:50 | [diff] [blame] | 259 | base::Base64Encode(serialized, &hashed); |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 260 | return hashed; |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 261 | } |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 262 | |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 263 | std::set<VariationsHttpHeaderProvider::VariationIDEntry> |
| 264 | VariationsHttpHeaderProvider::GetAllVariationIds() { |
asvitkine | 35ba647 | 2015-12-18 23:52:33 | [diff] [blame] | 265 | lock_.AssertAcquired(); |
| 266 | |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 267 | std::set<VariationIDEntry> all_variation_ids_set = default_variation_ids_set_; |
| 268 | for (const VariationIDEntry& entry : variation_ids_set_) { |
| 269 | all_variation_ids_set.insert(entry); |
| 270 | } |
| 271 | for (const VariationIDEntry& entry : synthetic_variation_ids_set_) { |
| 272 | all_variation_ids_set.insert(entry); |
| 273 | } |
asvitkine | 35ba647 | 2015-12-18 23:52:33 | [diff] [blame] | 274 | return all_variation_ids_set; |
| 275 | } |
| 276 | |
[email protected] | 71011c168 | 2014-07-09 17:19:16 | [diff] [blame] | 277 | } // namespace variations |