Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/variations/net/variations_http_headers.h" | 5 #include "components/variations/net/variations_http_headers.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/metrics/histogram_macros.h" | |
| 10 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 11 #include "components/google/core/browser/google_util.h" | 12 #include "components/google/core/browser/google_util.h" |
| 12 #include "components/variations/variations_http_header_provider.h" | 13 #include "components/variations/variations_http_header_provider.h" |
| 13 #include "net/http/http_request_headers.h" | 14 #include "net/http/http_request_headers.h" |
| 14 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 15 | 16 |
| 16 namespace variations { | 17 namespace variations { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 32 }; | 33 }; |
| 33 | 34 |
| 34 // Exact hostnames in lowercase to set headers for. | 35 // Exact hostnames in lowercase to set headers for. |
| 35 const char* kHostsToSetHeadersFor[] = { | 36 const char* kHostsToSetHeadersFor[] = { |
| 36 "googleweblight.com", | 37 "googleweblight.com", |
| 37 }; | 38 }; |
| 38 | 39 |
| 39 const char kChromeUMAEnabled[] = "X-Chrome-UMA-Enabled"; | 40 const char kChromeUMAEnabled[] = "X-Chrome-UMA-Enabled"; |
| 40 const char kClientData[] = "X-Client-Data"; | 41 const char kClientData[] = "X-Client-Data"; |
| 41 | 42 |
| 43 // The result of checking if a URL should have variations headers appended. | |
| 44 // This enum is used to record UMA histogram values, and should not be | |
| 45 // reordered. | |
| 46 enum ShouldAppendHeadersResult { | |
| 47 INVALID_URL, | |
| 48 NOT_HTTPS, | |
| 49 NOT_GOOGLE_DOMAIN, | |
| 50 SHOULD_APPEND, | |
| 51 SHOULD_APPEND_HEADERS_RESULT_SIZE, | |
| 52 }; | |
| 53 | |
| 54 // Checks whether headers should be appended to the |url|, based on the domain | |
| 55 // of |url|. |url| is assumed to be valid, and to have the https scheme. | |
| 56 bool ShouldAppendForDomain(const GURL& url) { | |
|
rkaplow
2016/12/12 19:31:17
actually don't super like this name, kind of prefe
jwd
2016/12/12 19:52:54
Done.
| |
| 57 if (google_util::IsGoogleDomainUrl(url, google_util::ALLOW_SUBDOMAIN, | |
| 58 google_util::ALLOW_NON_STANDARD_PORTS)) { | |
| 59 return true; | |
| 60 } | |
| 61 if (google_util::IsYoutubeDomainUrl(url, google_util::ALLOW_SUBDOMAIN, | |
| 62 google_util::ALLOW_NON_STANDARD_PORTS)) { | |
| 63 return true; | |
| 64 } | |
| 65 | |
| 66 // Some domains don't have international TLD extensions, so testing for them | |
| 67 // is very straight forward. | |
| 68 const std::string host = url.host(); | |
| 69 for (size_t i = 0; i < arraysize(kSuffixesToSetHeadersFor); ++i) { | |
| 70 if (base::EndsWith(host, kSuffixesToSetHeadersFor[i], | |
| 71 base::CompareCase::INSENSITIVE_ASCII)) | |
| 72 return true; | |
| 73 } | |
| 74 for (size_t i = 0; i < arraysize(kHostsToSetHeadersFor); ++i) { | |
| 75 if (base::LowerCaseEqualsASCII(host, kHostsToSetHeadersFor[i])) | |
| 76 return true; | |
| 77 } | |
| 78 | |
| 79 return false; | |
| 80 } | |
| 81 | |
| 42 } // namespace | 82 } // namespace |
| 43 | 83 |
| 44 void AppendVariationHeaders(const GURL& url, | 84 void AppendVariationHeaders(const GURL& url, |
| 45 bool incognito, | 85 bool incognito, |
| 46 bool uma_enabled, | 86 bool uma_enabled, |
| 47 net::HttpRequestHeaders* headers) { | 87 net::HttpRequestHeaders* headers) { |
| 48 // Note the criteria for attaching client experiment headers: | 88 // Note the criteria for attaching client experiment headers: |
| 49 // 1. We only transmit to Google owned domains which can evaluate experiments. | 89 // 1. We only transmit to Google owned domains which can evaluate experiments. |
| 50 // 1a. These include hosts which have a standard postfix such as: | 90 // 1a. These include hosts which have a standard postfix such as: |
| 51 // *.doubleclick.net or *.googlesyndication.com or | 91 // *.doubleclick.net or *.googlesyndication.com or |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 73 std::set<std::string> headers; | 113 std::set<std::string> headers; |
| 74 headers.insert(kChromeUMAEnabled); | 114 headers.insert(kChromeUMAEnabled); |
| 75 headers.insert(kClientData); | 115 headers.insert(kClientData); |
| 76 return headers; | 116 return headers; |
| 77 } | 117 } |
| 78 | 118 |
| 79 namespace internal { | 119 namespace internal { |
| 80 | 120 |
| 81 // static | 121 // static |
| 82 bool ShouldAppendVariationHeaders(const GURL& url) { | 122 bool ShouldAppendVariationHeaders(const GURL& url) { |
| 83 if (google_util::IsGoogleDomainUrl(url, google_util::ALLOW_SUBDOMAIN, | 123 const char kShouldAppendResultHistogram[] = |
|
rkaplow
2016/12/12 19:31:18
nit - could rename this
jwd
2016/12/12 19:52:54
Done.
| |
| 84 google_util::ALLOW_NON_STANDARD_PORTS)) { | 124 "Variations.Headers.URLValidationResult"; |
| 85 return true; | 125 if (!url.is_valid()) { |
| 126 UMA_HISTOGRAM_ENUMERATION(kShouldAppendResultHistogram, INVALID_URL); | |
| 127 return false; | |
| 128 } | |
| 129 if (url.SchemeIs("https")) { | |
|
rkaplow
2016/12/12 19:31:18
!
jwd
2016/12/12 19:52:54
Done.
| |
| 130 UMA_HISTOGRAM_ENUMERATION(kShouldAppendResultHistogram, NOT_HTTPS); | |
| 131 return false; | |
| 132 } | |
| 133 if (!ShouldAppendForDomain(url)) { | |
| 134 UMA_HISTOGRAM_ENUMERATION(kShouldAppendResultHistogram, NOT_GOOGLE_DOMAIN, | |
| 135 SHOULD_APPEND_HEADERS_RESULT_SIZE); | |
| 136 return false; | |
| 86 } | 137 } |
| 87 | 138 |
| 88 if (!url.is_valid() || !url.SchemeIsHTTPOrHTTPS()) | 139 UMA_HISTOGRAM_ENUMERATION(kShouldAppendResultHistogram, SHOULD_APPEND, |
| 89 return false; | 140 SHOULD_APPEND_HEADERS_RESULT_SIZE); |
| 90 | 141 return true; |
| 91 // Some domains don't have international TLD extensions, so testing for them | |
| 92 // is very straight forward. | |
| 93 const std::string host = url.host(); | |
| 94 for (size_t i = 0; i < arraysize(kSuffixesToSetHeadersFor); ++i) { | |
| 95 if (base::EndsWith(host, kSuffixesToSetHeadersFor[i], | |
| 96 base::CompareCase::INSENSITIVE_ASCII)) | |
| 97 return true; | |
| 98 } | |
| 99 for (size_t i = 0; i < arraysize(kHostsToSetHeadersFor); ++i) { | |
| 100 if (base::LowerCaseEqualsASCII(host, kHostsToSetHeadersFor[i])) | |
| 101 return true; | |
| 102 } | |
| 103 | |
| 104 return google_util::IsYoutubeDomainUrl(url, google_util::ALLOW_SUBDOMAIN, | |
| 105 google_util::ALLOW_NON_STANDARD_PORTS); | |
| 106 } | 142 } |
| 107 | 143 |
| 108 } // namespace internal | 144 } // namespace internal |
| 109 | 145 |
| 110 } // namespace variations | 146 } // namespace variations |
| OLD | NEW |