OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/metrics/variations/variations_http_header_provider.h" | 5 #include "chrome/browser/metrics/variations/variations_http_header_provider.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
15 #include "chrome/browser/google/google_util.h" | 15 #include "chrome/browser/google/google_util.h" |
16 #include "chrome/common/metrics/proto/chrome_experiments.pb.h" | 16 #include "chrome/common/metrics/proto/chrome_experiments.pb.h" |
17 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 17 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
18 #include "net/http/http_request_headers.h" | 18 #include "net/http/http_request_headers.h" |
19 #include "url/gurl.h" | 19 #include "url/gurl.h" |
20 | 20 |
21 namespace chrome_variations { | 21 namespace chrome_variations { |
22 | 22 |
23 namespace { | |
24 | |
25 const char* kSuffixesToSetHeadersFor[] = { | |
26 "android.com", | |
27 "doubleclick.com", | |
28 "doubleclick.net", | |
29 "ggpht.com", | |
30 "googleadservices.com", | |
31 "googleapis.com", | |
32 "googlesyndication.com", | |
33 "googleusercontent.com", | |
34 "googlevideo.com", | |
35 "gstatic.com", | |
36 "ytimg.com", | |
37 }; | |
38 | |
39 } // namespace | |
40 | |
23 VariationsHttpHeaderProvider* VariationsHttpHeaderProvider::GetInstance() { | 41 VariationsHttpHeaderProvider* VariationsHttpHeaderProvider::GetInstance() { |
24 return Singleton<VariationsHttpHeaderProvider>::get(); | 42 return Singleton<VariationsHttpHeaderProvider>::get(); |
25 } | 43 } |
26 | 44 |
27 void VariationsHttpHeaderProvider::AppendHeaders( | 45 void VariationsHttpHeaderProvider::AppendHeaders( |
28 const GURL& url, | 46 const GURL& url, |
29 bool incognito, | 47 bool incognito, |
30 bool uma_enabled, | 48 bool uma_enabled, |
31 net::HttpRequestHeaders* headers) { | 49 net::HttpRequestHeaders* headers) { |
32 // Note the criteria for attaching Chrome experiment headers: | 50 // Note the criteria for attaching Chrome experiment headers: |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 if (trigger_id) | 106 if (trigger_id) |
89 default_trigger_id_set_.insert(variation_id); | 107 default_trigger_id_set_.insert(variation_id); |
90 else | 108 else |
91 default_variation_ids_set_.insert(variation_id); | 109 default_variation_ids_set_.insert(variation_id); |
92 } | 110 } |
93 return true; | 111 return true; |
94 } | 112 } |
95 | 113 |
96 VariationsHttpHeaderProvider::VariationsHttpHeaderProvider() | 114 VariationsHttpHeaderProvider::VariationsHttpHeaderProvider() |
97 : variation_ids_cache_initialized_(false) { | 115 : variation_ids_cache_initialized_(false) { |
116 for (size_t i = 0; i < arraysize(kSuffixesToSetHeadersFor); ++i) { | |
117 suffixes_to_set_headers_for_.insert(kSuffixesToSetHeadersFor[i]); | |
118 } | |
98 } | 119 } |
99 | 120 |
100 VariationsHttpHeaderProvider::~VariationsHttpHeaderProvider() { | 121 VariationsHttpHeaderProvider::~VariationsHttpHeaderProvider() { |
101 } | 122 } |
102 | 123 |
103 void VariationsHttpHeaderProvider::OnFieldTrialGroupFinalized( | 124 void VariationsHttpHeaderProvider::OnFieldTrialGroupFinalized( |
104 const std::string& trial_name, | 125 const std::string& trial_name, |
105 const std::string& group_name) { | 126 const std::string& group_name) { |
106 VariationID new_id = | 127 VariationID new_id = |
107 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial_name, group_name); | 128 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial_name, group_name); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
209 | 230 |
210 std::string hashed; | 231 std::string hashed; |
211 base::Base64Encode(serialized, &hashed); | 232 base::Base64Encode(serialized, &hashed); |
212 // If successful, swap the header value with the new one. | 233 // 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 | 234 // 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 | 235 // if IDs are added as the header is recreated. The receiving servers are OK |
215 // with such discrepancies. | 236 // with such discrepancies. |
216 variation_ids_header_ = hashed; | 237 variation_ids_header_ = hashed; |
217 } | 238 } |
218 | 239 |
219 // static | |
220 bool VariationsHttpHeaderProvider::ShouldAppendHeaders(const GURL& url) { | 240 bool VariationsHttpHeaderProvider::ShouldAppendHeaders(const GURL& url) { |
221 if (google_util::IsGoogleDomainUrl(url, google_util::ALLOW_SUBDOMAIN, | 241 if (google_util::IsGoogleDomainUrl(url, google_util::ALLOW_SUBDOMAIN, |
222 google_util::ALLOW_NON_STANDARD_PORTS)) { | 242 google_util::ALLOW_NON_STANDARD_PORTS)) { |
223 return true; | 243 return true; |
224 } | 244 } |
225 | 245 |
226 if (!url.is_valid() || !url.SchemeIsHTTPOrHTTPS()) | 246 if (!url.is_valid() || !url.SchemeIsHTTPOrHTTPS()) |
227 return false; | 247 return false; |
228 | 248 |
229 // Some domains don't have international TLD extensions, so testing for them | 249 // Some domains don't have international TLD extensions, so testing for them |
230 // is very straight forward. | 250 // is very straight forward. |
231 const std::string host = url.host(); | 251 const std::string host = url.host(); |
232 if (EndsWith(host, ".doubleclick.net", false) || | 252 size_t suffix_start = host.find_last_of(".", host.length(), 2); |
Alexei Svitkine (slow)
2014/05/30 20:36:12
Remove unnecessary last arg. Same below.
| |
233 EndsWith(host, ".googlesyndication.com", false) || | 253 if (suffix_start != std::string::npos && suffix_start > 0) { |
234 LowerCaseEqualsASCII(host, "www.googleadservices.com")) { | 254 suffix_start = host.find_last_of(".", suffix_start - 1, 2); |
235 return true; | 255 if (suffix_start != std::string::npos) { |
256 std::string suffix = StringToLowerASCII(host.substr(suffix_start + 1)); | |
257 if (ContainsKey(suffixes_to_set_headers_for_, suffix)) | |
258 return true; | |
259 } | |
236 } | 260 } |
237 | 261 |
238 // The below mirrors logic in IsGoogleDomainUrl(), but for youtube.<TLD>. | 262 // The below mirrors logic in IsGoogleDomainUrl(), but for youtube.<TLD>. |
239 const size_t tld_length = net::registry_controlled_domains::GetRegistryLength( | 263 const size_t tld_length = net::registry_controlled_domains::GetRegistryLength( |
240 host, | 264 host, |
241 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, | 265 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, |
242 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); | 266 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); |
243 if ((tld_length == 0) || (tld_length == std::string::npos)) | 267 if ((tld_length == 0) || (tld_length == std::string::npos)) |
244 return false; | 268 return false; |
245 | 269 |
246 const std::string host_minus_tld(host, 0, host.length() - tld_length); | 270 const std::string host_minus_tld(host, 0, host.length() - tld_length); |
247 return LowerCaseEqualsASCII(host_minus_tld, "youtube.") || | 271 return LowerCaseEqualsASCII(host_minus_tld, "youtube.") || |
248 EndsWith(host_minus_tld, ".youtube.", false); | 272 EndsWith(host_minus_tld, ".youtube.", false); |
249 } | 273 } |
250 | 274 |
251 } // namespace chrome_variations | 275 } // namespace chrome_variations |
OLD | NEW |