[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 | |
isherman | 3be67db | 2014-10-24 05:57:44 | [diff] [blame] | 5 | #include "components/variations/net/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] | 8e44a5b0 | 2014-06-19 19:03:24 | [diff] [blame] | 17 | #include "components/google/core/browser/google_util.h" |
[email protected] | ea15bd5 | 2014-07-14 22:42:50 | [diff] [blame] | 18 | #include "components/variations/proto/client_variations.pb.h" |
[email protected] | 7f8a993 | 2013-07-26 20:43:34 | [diff] [blame] | 19 | #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 20 | #include "net/http/http_request_headers.h" |
[email protected] | 7f8a993 | 2013-07-26 20:43:34 | [diff] [blame] | 21 | #include "url/gurl.h" |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 22 | |
[email protected] | 71011c168 | 2014-07-09 17:19:16 | [diff] [blame] | 23 | namespace variations { |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 24 | |
[email protected] | 64d617e | 2014-05-31 04:37:54 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | const char* kSuffixesToSetHeadersFor[] = { |
| 28 | ".android.com", |
| 29 | ".doubleclick.com", |
| 30 | ".doubleclick.net", |
| 31 | ".ggpht.com", |
| 32 | ".googleadservices.com", |
| 33 | ".googleapis.com", |
| 34 | ".googlesyndication.com", |
| 35 | ".googleusercontent.com", |
| 36 | ".googlevideo.com", |
| 37 | ".gstatic.com", |
| 38 | ".ytimg.com", |
| 39 | }; |
| 40 | |
horo | e09b6c8 | 2014-11-01 02:08:28 | [diff] [blame] | 41 | const char kChromeUMAEnabled[] = "X-Chrome-UMA-Enabled"; |
| 42 | const char kClientData[] = "X-Client-Data"; |
| 43 | |
[email protected] | 64d617e | 2014-05-31 04:37:54 | [diff] [blame] | 44 | } // namespace |
| 45 | |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 46 | VariationsHttpHeaderProvider* VariationsHttpHeaderProvider::GetInstance() { |
| 47 | return Singleton<VariationsHttpHeaderProvider>::get(); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 48 | } |
| 49 | |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 50 | void VariationsHttpHeaderProvider::AppendHeaders( |
| 51 | const GURL& url, |
| 52 | bool incognito, |
| 53 | bool uma_enabled, |
| 54 | net::HttpRequestHeaders* headers) { |
[email protected] | ea15bd5 | 2014-07-14 22:42:50 | [diff] [blame] | 55 | // Note the criteria for attaching client experiment headers: |
[email protected] | 0eea1cf7 | 2014-04-01 19:39:19 | [diff] [blame] | 56 | // 1. We only transmit to Google owned domains which can evaluate experiments. |
| 57 | // 1a. These include hosts which have a standard postfix such as: |
| 58 | // *.doubleclick.net or *.googlesyndication.com or |
| 59 | // exactly www.googleadservices.com or |
| 60 | // international TLD domains *.google.<TLD> or *.youtube.<TLD>. |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 61 | // 2. Only transmit for non-Incognito profiles. |
| 62 | // 3. For the X-Chrome-UMA-Enabled bit, only set it if UMA is in fact enabled |
| 63 | // for this install of Chrome. |
[email protected] | 05ef1f2 | 2013-12-05 22:24:34 | [diff] [blame] | 64 | // 4. For the X-Client-Data header, only include non-empty variation IDs. |
[email protected] | 7f8a993 | 2013-07-26 20:43:34 | [diff] [blame] | 65 | if (incognito || !ShouldAppendHeaders(url)) |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 66 | return; |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 67 | |
| 68 | if (uma_enabled) |
horo | e09b6c8 | 2014-11-01 02:08:28 | [diff] [blame] | 69 | headers->SetHeaderIfMissing(kChromeUMAEnabled, "1"); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 70 | |
| 71 | // Lazily initialize the header, if not already done, before attempting to |
| 72 | // transmit it. |
| 73 | InitVariationIDsCacheIfNeeded(); |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 74 | |
| 75 | std::string variation_ids_header_copy; |
| 76 | { |
| 77 | base::AutoLock scoped_lock(lock_); |
| 78 | variation_ids_header_copy = variation_ids_header_; |
| 79 | } |
| 80 | |
| 81 | if (!variation_ids_header_copy.empty()) { |
[email protected] | 05ef1f2 | 2013-12-05 22:24:34 | [diff] [blame] | 82 | // Note that prior to M33 this header was named X-Chrome-Variations. |
horo | e09b6c8 | 2014-11-01 02:08:28 | [diff] [blame] | 83 | headers->SetHeaderIfMissing(kClientData, variation_ids_header_copy); |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 84 | } |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 85 | } |
| 86 | |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 87 | bool VariationsHttpHeaderProvider::SetDefaultVariationIds( |
| 88 | const std::string& variation_ids) { |
| 89 | default_variation_ids_set_.clear(); |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 90 | default_trigger_id_set_.clear(); |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 91 | std::vector<std::string> entries; |
| 92 | base::SplitString(variation_ids, ',', &entries); |
| 93 | for (std::vector<std::string>::const_iterator it = entries.begin(); |
| 94 | it != entries.end(); ++it) { |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 95 | if (it->empty()) { |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 96 | default_variation_ids_set_.clear(); |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 97 | default_trigger_id_set_.clear(); |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 98 | return false; |
| 99 | } |
brettw | 44ce0ec5 | 2015-06-12 01:57:57 | [diff] [blame] | 100 | bool trigger_id = base::StartsWithASCII(*it, "t", true); |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 101 | // Remove the "t" prefix if it's there. |
| 102 | std::string entry = trigger_id ? it->substr(1) : *it; |
| 103 | |
| 104 | int variation_id = 0; |
| 105 | if (!base::StringToInt(entry, &variation_id)) { |
| 106 | default_variation_ids_set_.clear(); |
| 107 | default_trigger_id_set_.clear(); |
| 108 | return false; |
| 109 | } |
| 110 | if (trigger_id) |
| 111 | default_trigger_id_set_.insert(variation_id); |
| 112 | else |
| 113 | default_variation_ids_set_.insert(variation_id); |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | |
asvitkine | b4ed7868 | 2015-03-12 18:18:54 | [diff] [blame] | 118 | std::set<std::string> VariationsHttpHeaderProvider::GetVariationHeaderNames() |
| 119 | const { |
| 120 | std::set<std::string> headers; |
| 121 | headers.insert(kChromeUMAEnabled); |
| 122 | headers.insert(kClientData); |
| 123 | return headers; |
| 124 | } |
| 125 | |
| 126 | void VariationsHttpHeaderProvider::ResetForTesting() { |
| 127 | base::AutoLock scoped_lock(lock_); |
| 128 | |
| 129 | // Stop observing field trials so that it can be restarted when this is |
| 130 | // re-inited. Note: This is a no-op if this is not currently observing. |
| 131 | base::FieldTrialList::RemoveObserver(this); |
| 132 | variation_ids_cache_initialized_ = false; |
| 133 | } |
| 134 | |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 135 | VariationsHttpHeaderProvider::VariationsHttpHeaderProvider() |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 136 | : variation_ids_cache_initialized_(false) { |
| 137 | } |
| 138 | |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 139 | VariationsHttpHeaderProvider::~VariationsHttpHeaderProvider() { |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 142 | void VariationsHttpHeaderProvider::OnFieldTrialGroupFinalized( |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 143 | const std::string& trial_name, |
| 144 | const std::string& group_name) { |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 145 | VariationID new_id = |
| 146 | GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial_name, group_name); |
[email protected] | e51dcb0c | 2014-05-06 16:56:10 | [diff] [blame] | 147 | VariationID new_trigger_id = GetGoogleVariationID( |
| 148 | GOOGLE_WEB_PROPERTIES_TRIGGER, trial_name, group_name); |
| 149 | if (new_id == EMPTY_ID && new_trigger_id == EMPTY_ID) |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 150 | return; |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 151 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 152 | base::AutoLock scoped_lock(lock_); |
[email protected] | e51dcb0c | 2014-05-06 16:56:10 | [diff] [blame] | 153 | if (new_id != EMPTY_ID) |
| 154 | variation_ids_set_.insert(new_id); |
| 155 | if (new_trigger_id != EMPTY_ID) |
| 156 | variation_trigger_ids_set_.insert(new_trigger_id); |
| 157 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 158 | UpdateVariationIDsHeaderValue(); |
| 159 | } |
| 160 | |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 161 | void VariationsHttpHeaderProvider::OnSyntheticTrialsChanged( |
| 162 | const std::vector<metrics::SyntheticTrialGroup>& groups) { |
| 163 | base::AutoLock scoped_lock(lock_); |
| 164 | |
| 165 | synthetic_variation_ids_set_.clear(); |
| 166 | for (const metrics::SyntheticTrialGroup& group : groups) { |
| 167 | const VariationID id = |
| 168 | GetGoogleVariationIDFromHashes(GOOGLE_WEB_PROPERTIES, group.id); |
| 169 | if (id != EMPTY_ID) |
| 170 | synthetic_variation_ids_set_.insert(id); |
| 171 | } |
| 172 | UpdateVariationIDsHeaderValue(); |
| 173 | } |
| 174 | |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 175 | void VariationsHttpHeaderProvider::InitVariationIDsCacheIfNeeded() { |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 176 | base::AutoLock scoped_lock(lock_); |
| 177 | if (variation_ids_cache_initialized_) |
| 178 | return; |
| 179 | |
| 180 | // Register for additional cache updates. This is done first to avoid a race |
| 181 | // that could cause registered FieldTrials to be missed. |
[email protected] | b3a2509 | 2013-05-28 22:08:16 | [diff] [blame] | 182 | DCHECK(base::MessageLoop::current()); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 183 | base::FieldTrialList::AddObserver(this); |
| 184 | |
[email protected] | 999f7b4 | 2013-02-04 16:14:25 | [diff] [blame] | 185 | base::TimeTicks before_time = base::TimeTicks::Now(); |
| 186 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 187 | base::FieldTrial::ActiveGroups initial_groups; |
| 188 | base::FieldTrialList::GetActiveFieldTrialGroups(&initial_groups); |
| 189 | for (base::FieldTrial::ActiveGroups::const_iterator it = |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 190 | initial_groups.begin(); |
| 191 | it != initial_groups.end(); ++it) { |
| 192 | const VariationID id = |
| 193 | GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, it->trial_name, |
| 194 | it->group_name); |
[email protected] | 90acad0 | 2013-01-16 17:17:54 | [diff] [blame] | 195 | if (id != EMPTY_ID) |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 196 | variation_ids_set_.insert(id); |
[email protected] | e51dcb0c | 2014-05-06 16:56:10 | [diff] [blame] | 197 | |
| 198 | const VariationID trigger_id = |
| 199 | GetGoogleVariationID(GOOGLE_WEB_PROPERTIES_TRIGGER, it->trial_name, |
| 200 | it->group_name); |
| 201 | if (trigger_id != EMPTY_ID) |
| 202 | variation_trigger_ids_set_.insert(trigger_id); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 203 | } |
| 204 | UpdateVariationIDsHeaderValue(); |
| 205 | |
[email protected] | 999f7b4 | 2013-02-04 16:14:25 | [diff] [blame] | 206 | UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 207 | "Variations.HeaderConstructionTime", |
| 208 | (base::TimeTicks::Now() - before_time).InMicroseconds(), |
| 209 | 0, |
| 210 | base::TimeDelta::FromSeconds(1).InMicroseconds(), |
| 211 | 50); |
| 212 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 213 | variation_ids_cache_initialized_ = true; |
| 214 | } |
| 215 | |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 216 | void VariationsHttpHeaderProvider::UpdateVariationIDsHeaderValue() { |
| 217 | lock_.AssertAcquired(); |
| 218 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 219 | // The header value is a serialized protobuffer of Variation IDs which is |
| 220 | // base64 encoded before transmitting as a string. |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 221 | variation_ids_header_.clear(); |
| 222 | |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 223 | if (variation_ids_set_.empty() && default_variation_ids_set_.empty() && |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 224 | variation_trigger_ids_set_.empty() && default_trigger_id_set_.empty() && |
| 225 | synthetic_variation_ids_set_.empty()) { |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 226 | return; |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 227 | } |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 228 | |
| 229 | // This is the bottleneck for the creation of the header, so validate the size |
| 230 | // here. Force a hard maximum on the ID count in case the Variations server |
| 231 | // returns too many IDs and DOSs receiving servers with large requests. |
[email protected] | e51dcb0c | 2014-05-06 16:56:10 | [diff] [blame] | 232 | const size_t total_id_count = |
| 233 | variation_ids_set_.size() + variation_trigger_ids_set_.size(); |
| 234 | DCHECK_LE(total_id_count, 10U); |
[email protected] | a27ae2a | 2014-08-01 16:17:52 | [diff] [blame] | 235 | UMA_HISTOGRAM_COUNTS_100("Variations.Headers.ExperimentCount", |
| 236 | total_id_count); |
[email protected] | e51dcb0c | 2014-05-06 16:56:10 | [diff] [blame] | 237 | if (total_id_count > 20) |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 238 | return; |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 239 | |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 240 | // Merge the two sets of experiment ids. |
| 241 | std::set<VariationID> all_variation_ids_set = default_variation_ids_set_; |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 242 | for (VariationID id : variation_ids_set_) |
| 243 | all_variation_ids_set.insert(id); |
| 244 | for (VariationID id : synthetic_variation_ids_set_) |
| 245 | all_variation_ids_set.insert(id); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 246 | |
[email protected] | e51dcb0c | 2014-05-06 16:56:10 | [diff] [blame] | 247 | std::set<VariationID> all_trigger_ids_set = default_trigger_id_set_; |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 248 | for (VariationID id : variation_trigger_ids_set_) |
| 249 | all_trigger_ids_set.insert(id); |
| 250 | |
| 251 | ClientVariations proto; |
| 252 | for (VariationID id : all_variation_ids_set) |
| 253 | proto.add_variation_id(id); |
| 254 | for (VariationID id : all_trigger_ids_set) |
| 255 | proto.add_trigger_variation_id(id); |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 256 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 257 | std::string serialized; |
| 258 | proto.SerializeToString(&serialized); |
| 259 | |
| 260 | std::string hashed; |
[email protected] | 33fca12 | 2013-12-11 01:48:50 | [diff] [blame] | 261 | base::Base64Encode(serialized, &hashed); |
| 262 | // If successful, swap the header value with the new one. |
| 263 | // Note that the list of IDs and the header could be temporarily out of sync |
| 264 | // if IDs are added as the header is recreated. The receiving servers are OK |
| 265 | // with such discrepancies. |
| 266 | variation_ids_header_ = hashed; |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 267 | } |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 268 | |
[email protected] | 7f8a993 | 2013-07-26 20:43:34 | [diff] [blame] | 269 | // static |
| 270 | bool VariationsHttpHeaderProvider::ShouldAppendHeaders(const GURL& url) { |
| 271 | if (google_util::IsGoogleDomainUrl(url, google_util::ALLOW_SUBDOMAIN, |
| 272 | google_util::ALLOW_NON_STANDARD_PORTS)) { |
| 273 | return true; |
| 274 | } |
| 275 | |
[email protected] | 91f568903 | 2013-08-22 01:43:33 | [diff] [blame] | 276 | if (!url.is_valid() || !url.SchemeIsHTTPOrHTTPS()) |
[email protected] | 7f8a993 | 2013-07-26 20:43:34 | [diff] [blame] | 277 | return false; |
| 278 | |
[email protected] | 0eea1cf7 | 2014-04-01 19:39:19 | [diff] [blame] | 279 | // Some domains don't have international TLD extensions, so testing for them |
| 280 | // is very straight forward. |
[email protected] | 7f8a993 | 2013-07-26 20:43:34 | [diff] [blame] | 281 | const std::string host = url.host(); |
[email protected] | 64d617e | 2014-05-31 04:37:54 | [diff] [blame] | 282 | for (size_t i = 0; i < arraysize(kSuffixesToSetHeadersFor); ++i) { |
brettw | 17b32ad | 2015-06-12 23:46:41 | [diff] [blame] | 283 | if (base::EndsWith(host, kSuffixesToSetHeadersFor[i], false)) |
[email protected] | 64d617e | 2014-05-31 04:37:54 | [diff] [blame] | 284 | return true; |
[email protected] | 0eea1cf7 | 2014-04-01 19:39:19 | [diff] [blame] | 285 | } |
| 286 | |
[email protected] | 87abe9ea | 2014-06-17 03:29:54 | [diff] [blame] | 287 | return google_util::IsYoutubeDomainUrl(url, google_util::ALLOW_SUBDOMAIN, |
| 288 | google_util::ALLOW_NON_STANDARD_PORTS); |
[email protected] | 7f8a993 | 2013-07-26 20:43:34 | [diff] [blame] | 289 | } |
| 290 | |
[email protected] | 71011c168 | 2014-07-09 17:19:16 | [diff] [blame] | 291 | } // namespace variations |