| 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 URLValidationResult { |
| 47 INVALID_URL, |
| 48 NOT_HTTPS, |
| 49 NOT_GOOGLE_DOMAIN, |
| 50 SHOULD_APPEND, |
| 51 URL_VALIDATION_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 IsGoogleDomain(const GURL& url) { |
| 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 |
| 82 void LogUrlValidationHistogram(URLValidationResult result) { |
| 83 UMA_HISTOGRAM_ENUMERATION("Variations.Headers.URLValidationResult", result, |
| 84 URL_VALIDATION_RESULT_SIZE); |
| 85 } |
| 86 |
| 42 } // namespace | 87 } // namespace |
| 43 | 88 |
| 44 void AppendVariationHeaders(const GURL& url, | 89 void AppendVariationHeaders(const GURL& url, |
| 45 bool incognito, | 90 bool incognito, |
| 46 bool uma_enabled, | 91 bool uma_enabled, |
| 47 net::HttpRequestHeaders* headers) { | 92 net::HttpRequestHeaders* headers) { |
| 48 // Note the criteria for attaching client experiment headers: | 93 // Note the criteria for attaching client experiment headers: |
| 49 // 1. We only transmit to Google owned domains which can evaluate experiments. | 94 // 1. We only transmit to Google owned domains which can evaluate experiments. |
| 50 // 1a. These include hosts which have a standard postfix such as: | 95 // 1a. These include hosts which have a standard postfix such as: |
| 51 // *.doubleclick.net or *.googlesyndication.com or | 96 // *.doubleclick.net or *.googlesyndication.com or |
| (...skipping 21 matching lines...) Expand all Loading... |
| 73 std::set<std::string> headers; | 118 std::set<std::string> headers; |
| 74 headers.insert(kChromeUMAEnabled); | 119 headers.insert(kChromeUMAEnabled); |
| 75 headers.insert(kClientData); | 120 headers.insert(kClientData); |
| 76 return headers; | 121 return headers; |
| 77 } | 122 } |
| 78 | 123 |
| 79 namespace internal { | 124 namespace internal { |
| 80 | 125 |
| 81 // static | 126 // static |
| 82 bool ShouldAppendVariationHeaders(const GURL& url) { | 127 bool ShouldAppendVariationHeaders(const GURL& url) { |
| 83 if (google_util::IsGoogleDomainUrl(url, google_util::ALLOW_SUBDOMAIN, | 128 if (!url.is_valid()) { |
| 84 google_util::ALLOW_NON_STANDARD_PORTS)) { | 129 LogUrlValidationHistogram(INVALID_URL); |
| 85 return true; | 130 return false; |
| 131 } |
| 132 if (!url.SchemeIs("https")) { |
| 133 LogUrlValidationHistogram(NOT_HTTPS); |
| 134 return false; |
| 135 } |
| 136 if (!IsGoogleDomain(url)) { |
| 137 LogUrlValidationHistogram(NOT_GOOGLE_DOMAIN); |
| 138 return false; |
| 86 } | 139 } |
| 87 | 140 |
| 88 if (!url.is_valid() || !url.SchemeIsHTTPOrHTTPS()) | 141 LogUrlValidationHistogram(SHOULD_APPEND); |
| 89 return false; | 142 return true; |
| 90 | |
| 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 } | 143 } |
| 107 | 144 |
| 108 } // namespace internal | 145 } // namespace internal |
| 109 | 146 |
| 110 } // namespace variations | 147 } // namespace variations |
| OLD | NEW |