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