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