[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 | |
horo | e09b6c8 | 2014-11-01 02:08:28 | [diff] [blame] | 7 | #include <set> |
| 8 | #include <string> |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 11 | #include "base/base64.h" |
| 12 | #include "base/memory/singleton.h" |
asvitkine | 454600f | 2015-06-16 16:34:50 | [diff] [blame] | 13 | #include "base/metrics/histogram_macros.h" |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 14 | #include "base/strings/string_number_conversions.h" |
| 15 | #include "base/strings/string_split.h" |
[email protected] | 7f8a993 | 2013-07-26 20:43:34 | [diff] [blame] | 16 | #include "base/strings/string_util.h" |
[email protected] | ea15bd5 | 2014-07-14 22:42:50 | [diff] [blame] | 17 | #include "components/variations/proto/client_variations.pb.h" |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 18 | |
[email protected] | 71011c168 | 2014-07-09 17:19:16 | [diff] [blame] | 19 | namespace variations { |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 20 | |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 21 | // static |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 22 | VariationsHttpHeaderProvider* VariationsHttpHeaderProvider::GetInstance() { |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 23 | return base::Singleton<VariationsHttpHeaderProvider>::get(); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 24 | } |
| 25 | |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 26 | std::string VariationsHttpHeaderProvider::GetClientDataHeader() { |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 27 | // Lazily initialize the header, if not already done, before attempting to |
| 28 | // transmit it. |
| 29 | InitVariationIDsCacheIfNeeded(); |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 30 | |
| 31 | std::string variation_ids_header_copy; |
| 32 | { |
| 33 | base::AutoLock scoped_lock(lock_); |
| 34 | variation_ids_header_copy = variation_ids_header_; |
| 35 | } |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 36 | return variation_ids_header_copy; |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 37 | } |
| 38 | |
asvitkine | 35ba647 | 2015-12-18 23:52:33 | [diff] [blame^] | 39 | std::string VariationsHttpHeaderProvider::GetVariationsString() { |
| 40 | InitVariationIDsCacheIfNeeded(); |
| 41 | |
| 42 | // Construct a space-separated string with leading and trailing spaces from |
| 43 | // the variations set. Note: The ids in it will be in sorted order per the |
| 44 | // std::set contract. |
| 45 | std::string ids_string = " "; |
| 46 | { |
| 47 | base::AutoLock scoped_lock(lock_); |
| 48 | for (VariationID id : GetAllVariationIds()) { |
| 49 | ids_string.append(base::IntToString(id)); |
| 50 | ids_string.push_back(' '); |
| 51 | } |
| 52 | } |
| 53 | return ids_string; |
| 54 | } |
| 55 | |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 56 | bool VariationsHttpHeaderProvider::SetDefaultVariationIds( |
| 57 | const std::string& variation_ids) { |
| 58 | default_variation_ids_set_.clear(); |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 59 | default_trigger_id_set_.clear(); |
brettw | 8be197d1 | 2015-07-23 23:23:31 | [diff] [blame] | 60 | for (const base::StringPiece& entry : base::SplitStringPiece( |
| 61 | variation_ids, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { |
| 62 | if (entry.empty()) { |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 63 | default_variation_ids_set_.clear(); |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 64 | default_trigger_id_set_.clear(); |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 65 | return false; |
| 66 | } |
brettw | 8be197d1 | 2015-07-23 23:23:31 | [diff] [blame] | 67 | bool trigger_id = |
| 68 | base::StartsWith(entry, "t", base::CompareCase::SENSITIVE); |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 69 | // Remove the "t" prefix if it's there. |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 70 | base::StringPiece trimmed_entry = trigger_id ? entry.substr(1) : entry; |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 71 | |
| 72 | int variation_id = 0; |
brettw | 8be197d1 | 2015-07-23 23:23:31 | [diff] [blame] | 73 | if (!base::StringToInt(trimmed_entry, &variation_id)) { |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 74 | default_variation_ids_set_.clear(); |
| 75 | default_trigger_id_set_.clear(); |
| 76 | return false; |
| 77 | } |
| 78 | if (trigger_id) |
| 79 | default_trigger_id_set_.insert(variation_id); |
| 80 | else |
| 81 | default_variation_ids_set_.insert(variation_id); |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 82 | } |
| 83 | return true; |
| 84 | } |
| 85 | |
asvitkine | b4ed7868 | 2015-03-12 18:18:54 | [diff] [blame] | 86 | void VariationsHttpHeaderProvider::ResetForTesting() { |
| 87 | base::AutoLock scoped_lock(lock_); |
| 88 | |
| 89 | // Stop observing field trials so that it can be restarted when this is |
| 90 | // re-inited. Note: This is a no-op if this is not currently observing. |
| 91 | base::FieldTrialList::RemoveObserver(this); |
| 92 | variation_ids_cache_initialized_ = false; |
| 93 | } |
| 94 | |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 95 | VariationsHttpHeaderProvider::VariationsHttpHeaderProvider() |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 96 | : variation_ids_cache_initialized_(false) {} |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 97 | |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 98 | VariationsHttpHeaderProvider::~VariationsHttpHeaderProvider() {} |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 99 | |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 100 | void VariationsHttpHeaderProvider::OnFieldTrialGroupFinalized( |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 101 | const std::string& trial_name, |
| 102 | const std::string& group_name) { |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 103 | VariationID new_id = |
| 104 | GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial_name, group_name); |
[email protected] | e51dcb0c | 2014-05-06 16:56:10 | [diff] [blame] | 105 | VariationID new_trigger_id = GetGoogleVariationID( |
| 106 | GOOGLE_WEB_PROPERTIES_TRIGGER, trial_name, group_name); |
| 107 | if (new_id == EMPTY_ID && new_trigger_id == EMPTY_ID) |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 108 | return; |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 109 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 110 | base::AutoLock scoped_lock(lock_); |
[email protected] | e51dcb0c | 2014-05-06 16:56:10 | [diff] [blame] | 111 | if (new_id != EMPTY_ID) |
| 112 | variation_ids_set_.insert(new_id); |
| 113 | if (new_trigger_id != EMPTY_ID) |
| 114 | variation_trigger_ids_set_.insert(new_trigger_id); |
| 115 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 116 | UpdateVariationIDsHeaderValue(); |
| 117 | } |
| 118 | |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 119 | void VariationsHttpHeaderProvider::OnSyntheticTrialsChanged( |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 120 | const std::vector<SyntheticTrialGroup>& groups) { |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 121 | base::AutoLock scoped_lock(lock_); |
| 122 | |
| 123 | synthetic_variation_ids_set_.clear(); |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 124 | for (const SyntheticTrialGroup& group : groups) { |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 125 | const VariationID id = |
| 126 | GetGoogleVariationIDFromHashes(GOOGLE_WEB_PROPERTIES, group.id); |
| 127 | if (id != EMPTY_ID) |
| 128 | synthetic_variation_ids_set_.insert(id); |
| 129 | } |
| 130 | UpdateVariationIDsHeaderValue(); |
| 131 | } |
| 132 | |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 133 | void VariationsHttpHeaderProvider::InitVariationIDsCacheIfNeeded() { |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 134 | base::AutoLock scoped_lock(lock_); |
| 135 | if (variation_ids_cache_initialized_) |
| 136 | return; |
| 137 | |
| 138 | // Register for additional cache updates. This is done first to avoid a race |
| 139 | // that could cause registered FieldTrials to be missed. |
[email protected] | b3a2509 | 2013-05-28 22:08:16 | [diff] [blame] | 140 | DCHECK(base::MessageLoop::current()); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 141 | base::FieldTrialList::AddObserver(this); |
| 142 | |
[email protected] | 999f7b4 | 2013-02-04 16:14:25 | [diff] [blame] | 143 | base::TimeTicks before_time = base::TimeTicks::Now(); |
| 144 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 145 | base::FieldTrial::ActiveGroups initial_groups; |
| 146 | base::FieldTrialList::GetActiveFieldTrialGroups(&initial_groups); |
asvitkine | 35ba647 | 2015-12-18 23:52:33 | [diff] [blame^] | 147 | |
| 148 | for (const auto& entry : initial_groups) { |
| 149 | const VariationID id = |
| 150 | GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, entry.trial_name, |
| 151 | entry.group_name); |
[email protected] | 90acad0 | 2013-01-16 17:17:54 | [diff] [blame] | 152 | if (id != EMPTY_ID) |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 153 | variation_ids_set_.insert(id); |
[email protected] | e51dcb0c | 2014-05-06 16:56:10 | [diff] [blame] | 154 | |
asvitkine | 35ba647 | 2015-12-18 23:52:33 | [diff] [blame^] | 155 | const VariationID trigger_id = |
| 156 | GetGoogleVariationID(GOOGLE_WEB_PROPERTIES_TRIGGER, entry.trial_name, |
| 157 | entry.group_name); |
| 158 | |
[email protected] | e51dcb0c | 2014-05-06 16:56:10 | [diff] [blame] | 159 | if (trigger_id != EMPTY_ID) |
| 160 | variation_trigger_ids_set_.insert(trigger_id); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 161 | } |
| 162 | UpdateVariationIDsHeaderValue(); |
| 163 | |
[email protected] | 999f7b4 | 2013-02-04 16:14:25 | [diff] [blame] | 164 | UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 165 | "Variations.HeaderConstructionTime", |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 166 | (base::TimeTicks::Now() - before_time).InMicroseconds(), 0, |
| 167 | base::TimeDelta::FromSeconds(1).InMicroseconds(), 50); |
[email protected] | 999f7b4 | 2013-02-04 16:14:25 | [diff] [blame] | 168 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 169 | variation_ids_cache_initialized_ = true; |
| 170 | } |
| 171 | |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 172 | void VariationsHttpHeaderProvider::UpdateVariationIDsHeaderValue() { |
| 173 | lock_.AssertAcquired(); |
| 174 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 175 | // The header value is a serialized protobuffer of Variation IDs which is |
| 176 | // base64 encoded before transmitting as a string. |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 177 | variation_ids_header_.clear(); |
| 178 | |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 179 | if (variation_ids_set_.empty() && default_variation_ids_set_.empty() && |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 180 | variation_trigger_ids_set_.empty() && default_trigger_id_set_.empty() && |
| 181 | synthetic_variation_ids_set_.empty()) { |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 182 | return; |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 183 | } |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 184 | |
| 185 | // This is the bottleneck for the creation of the header, so validate the size |
| 186 | // here. Force a hard maximum on the ID count in case the Variations server |
| 187 | // returns too many IDs and DOSs receiving servers with large requests. |
[email protected] | e51dcb0c | 2014-05-06 16:56:10 | [diff] [blame] | 188 | const size_t total_id_count = |
| 189 | variation_ids_set_.size() + variation_trigger_ids_set_.size(); |
| 190 | DCHECK_LE(total_id_count, 10U); |
[email protected] | a27ae2a | 2014-08-01 16:17:52 | [diff] [blame] | 191 | UMA_HISTOGRAM_COUNTS_100("Variations.Headers.ExperimentCount", |
| 192 | total_id_count); |
[email protected] | e51dcb0c | 2014-05-06 16:56:10 | [diff] [blame] | 193 | if (total_id_count > 20) |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 194 | return; |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 195 | |
asvitkine | 35ba647 | 2015-12-18 23:52:33 | [diff] [blame^] | 196 | std::set<VariationID> all_variation_ids_set = GetAllVariationIds(); |
[email protected] | e51dcb0c | 2014-05-06 16:56:10 | [diff] [blame] | 197 | std::set<VariationID> all_trigger_ids_set = default_trigger_id_set_; |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 198 | for (VariationID id : variation_trigger_ids_set_) |
| 199 | all_trigger_ids_set.insert(id); |
| 200 | |
| 201 | ClientVariations proto; |
| 202 | for (VariationID id : all_variation_ids_set) |
| 203 | proto.add_variation_id(id); |
| 204 | for (VariationID id : all_trigger_ids_set) |
| 205 | proto.add_trigger_variation_id(id); |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 206 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 207 | std::string serialized; |
| 208 | proto.SerializeToString(&serialized); |
| 209 | |
| 210 | std::string hashed; |
[email protected] | 33fca12 | 2013-12-11 01:48:50 | [diff] [blame] | 211 | base::Base64Encode(serialized, &hashed); |
| 212 | // If successful, swap the header value with the new one. |
| 213 | // Note that the list of IDs and the header could be temporarily out of sync |
| 214 | // if IDs are added as the header is recreated. The receiving servers are OK |
| 215 | // with such discrepancies. |
| 216 | variation_ids_header_ = hashed; |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 217 | } |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 218 | |
asvitkine | 35ba647 | 2015-12-18 23:52:33 | [diff] [blame^] | 219 | std::set<VariationID> VariationsHttpHeaderProvider::GetAllVariationIds() { |
| 220 | lock_.AssertAcquired(); |
| 221 | |
| 222 | std::set<VariationID> all_variation_ids_set = default_variation_ids_set_; |
| 223 | for (VariationID id : variation_ids_set_) |
| 224 | all_variation_ids_set.insert(id); |
| 225 | for (VariationID id : synthetic_variation_ids_set_) |
| 226 | all_variation_ids_set.insert(id); |
| 227 | return all_variation_ids_set; |
| 228 | } |
| 229 | |
[email protected] | 71011c168 | 2014-07-09 17:19:16 | [diff] [blame] | 230 | } // namespace variations |