| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/google/core/browser/google_util.h" | 5 #include "components/google/core/browser/google_util.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 // Only use Link Doctor on official builds. It uses an API key, too, but | 26 // Only use Link Doctor on official builds. It uses an API key, too, but |
| 27 // seems best to just disable it, for more responsive error pages and to reduce | 27 // seems best to just disable it, for more responsive error pages and to reduce |
| 28 // server load. | 28 // server load. |
| 29 #if defined(GOOGLE_CHROME_BUILD) | 29 #if defined(GOOGLE_CHROME_BUILD) |
| 30 #define LINKDOCTOR_SERVER_REQUEST_URL "https://www.googleapis.com/rpc" | 30 #define LINKDOCTOR_SERVER_REQUEST_URL "https://www.googleapis.com/rpc" |
| 31 #else | 31 #else |
| 32 #define LINKDOCTOR_SERVER_REQUEST_URL "" | 32 #define LINKDOCTOR_SERVER_REQUEST_URL "" |
| 33 #endif | 33 #endif |
| 34 | 34 |
| 35 namespace google_util { | |
| 36 | 35 |
| 37 // Helpers -------------------------------------------------------------------- | 36 // Helpers -------------------------------------------------------------------- |
| 38 | 37 |
| 39 namespace { | 38 namespace { |
| 40 | 39 |
| 41 bool gUseMockLinkDoctorBaseURLForTesting = false; | 40 bool gUseMockLinkDoctorBaseURLForTesting = false; |
| 42 | 41 |
| 43 bool IsPathHomePageBase(base::StringPiece path) { | 42 bool IsPathHomePageBase(base::StringPiece path) { |
| 44 return (path == "/") || (path == "/webhp"); | 43 return (path == "/") || (path == "/webhp"); |
| 45 } | 44 } |
| 46 | 45 |
| 47 // True if the given canonical |host| is "[www.]<domain_in_lower_case>.<TLD>" | 46 // True if |host| is "[www.]<domain_in_lower_case>.<TLD>" with a valid TLD. If |
| 48 // with a valid TLD. If |subdomain_permission| is ALLOW_SUBDOMAIN, we check | 47 // |subdomain_permission| is ALLOW_SUBDOMAIN, we check against host |
| 49 // against host "*.<domain_in_lower_case>.<TLD>" instead. | 48 // "*.<domain_in_lower_case>.<TLD>" instead. |
| 50 bool IsValidHostName(base::StringPiece host, | 49 bool IsValidHostName(base::StringPiece host, |
| 51 base::StringPiece domain_in_lower_case, | 50 base::StringPiece domain_in_lower_case, |
| 52 SubdomainPermission subdomain_permission) { | 51 google_util::SubdomainPermission subdomain_permission) { |
| 53 size_t tld_length = | 52 size_t tld_length = net::registry_controlled_domains::GetRegistryLength( |
| 54 net::registry_controlled_domains::GetCanonicalHostRegistryLength( | 53 host, |
| 55 host, net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, | 54 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, |
| 56 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); | 55 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); |
| 57 if ((tld_length == 0) || (tld_length == std::string::npos)) | 56 if ((tld_length == 0) || (tld_length == std::string::npos)) |
| 58 return false; | 57 return false; |
| 59 | 58 |
| 60 // Removes the tld and the preceding dot. | 59 // Removes the tld and the preceding dot. |
| 61 base::StringPiece host_minus_tld = | 60 base::StringPiece host_minus_tld = |
| 62 host.substr(0, host.length() - tld_length - 1); | 61 host.substr(0, host.length() - tld_length - 1); |
| 63 if (base::LowerCaseEqualsASCII(host_minus_tld, domain_in_lower_case)) | 62 if (base::LowerCaseEqualsASCII(host_minus_tld, domain_in_lower_case)) |
| 64 return true; | 63 return true; |
| 65 | 64 |
| 66 if (subdomain_permission == ALLOW_SUBDOMAIN) { | 65 if (subdomain_permission == google_util::ALLOW_SUBDOMAIN) { |
| 67 std::string dot_domain("."); | 66 std::string dot_domain("."); |
| 68 domain_in_lower_case.AppendToString(&dot_domain); | 67 domain_in_lower_case.AppendToString(&dot_domain); |
| 69 return base::EndsWith(host_minus_tld, dot_domain, | 68 return base::EndsWith(host_minus_tld, dot_domain, |
| 70 base::CompareCase::INSENSITIVE_ASCII); | 69 base::CompareCase::INSENSITIVE_ASCII); |
| 71 } | 70 } |
| 72 | 71 |
| 73 std::string www_domain("www."); | 72 std::string www_domain("www."); |
| 74 domain_in_lower_case.AppendToString(&www_domain); | 73 domain_in_lower_case.AppendToString(&www_domain); |
| 75 return base::LowerCaseEqualsASCII(host_minus_tld, www_domain); | 74 return base::LowerCaseEqualsASCII(host_minus_tld, www_domain); |
| 76 } | 75 } |
| 77 | 76 |
| 78 // True if |url| is a valid URL with HTTP or HTTPS scheme. If |port_permission| | 77 // True if |url| is a valid URL with HTTP or HTTPS scheme. If |port_permission| |
| 79 // is DISALLOW_NON_STANDARD_PORTS, this also requires |url| to use the standard | 78 // is DISALLOW_NON_STANDARD_PORTS, this also requires |url| to use the standard |
| 80 // port for its scheme (80 for HTTP, 443 for HTTPS). | 79 // port for its scheme (80 for HTTP, 443 for HTTPS). |
| 81 bool IsValidURL(const GURL& url, PortPermission port_permission) { | 80 bool IsValidURL(const GURL& url, google_util::PortPermission port_permission) { |
| 82 return url.is_valid() && url.SchemeIsHTTPOrHTTPS() && | 81 return url.is_valid() && url.SchemeIsHTTPOrHTTPS() && |
| 83 (url.port().empty() || (port_permission == ALLOW_NON_STANDARD_PORTS)); | 82 (url.port().empty() || |
| 84 } | 83 (port_permission == google_util::ALLOW_NON_STANDARD_PORTS)); |
| 85 | |
| 86 bool IsCanonicalHostGoogleHostname(base::StringPiece canonical_host, | |
| 87 SubdomainPermission subdomain_permission) { | |
| 88 const GURL& base_url(CommandLineGoogleBaseURL()); | |
| 89 if (base_url.is_valid() && (canonical_host == base_url.host_piece())) | |
| 90 return true; | |
| 91 | |
| 92 return IsValidHostName(canonical_host, "google", subdomain_permission); | |
| 93 } | 84 } |
| 94 | 85 |
| 95 } // namespace | 86 } // namespace |
| 96 | 87 |
| 88 |
| 89 namespace google_util { |
| 90 |
| 97 // Global functions ----------------------------------------------------------- | 91 // Global functions ----------------------------------------------------------- |
| 98 | 92 |
| 99 bool HasGoogleSearchQueryParam(base::StringPiece str) { | 93 bool HasGoogleSearchQueryParam(base::StringPiece str) { |
| 100 url::Component query(0, static_cast<int>(str.length())), key, value; | 94 url::Component query(0, static_cast<int>(str.length())), key, value; |
| 101 while (url::ExtractQueryKeyValue(str.data(), &query, &key, &value)) { | 95 while (url::ExtractQueryKeyValue(str.data(), &query, &key, &value)) { |
| 102 if (value.is_nonempty()) { | 96 if (value.is_nonempty()) { |
| 103 base::StringPiece key_str = str.substr(key.begin, key.len); | 97 base::StringPiece key_str = str.substr(key.begin, key.len); |
| 104 if (key_str == "q" || key_str == "as_q") | 98 if (key_str == "q" || key_str == "as_q") |
| 105 return true; | 99 return true; |
| 106 } | 100 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 | 173 |
| 180 bool StartsWithCommandLineGoogleBaseURL(const GURL& url) { | 174 bool StartsWithCommandLineGoogleBaseURL(const GURL& url) { |
| 181 const GURL& base_url(CommandLineGoogleBaseURL()); | 175 const GURL& base_url(CommandLineGoogleBaseURL()); |
| 182 return base_url.is_valid() && | 176 return base_url.is_valid() && |
| 183 base::StartsWith(url.possibly_invalid_spec(), base_url.spec(), | 177 base::StartsWith(url.possibly_invalid_spec(), base_url.spec(), |
| 184 base::CompareCase::SENSITIVE); | 178 base::CompareCase::SENSITIVE); |
| 185 } | 179 } |
| 186 | 180 |
| 187 bool IsGoogleHostname(base::StringPiece host, | 181 bool IsGoogleHostname(base::StringPiece host, |
| 188 SubdomainPermission subdomain_permission) { | 182 SubdomainPermission subdomain_permission) { |
| 189 url::CanonHostInfo host_info; | 183 const GURL& base_url(CommandLineGoogleBaseURL()); |
| 190 return IsCanonicalHostGoogleHostname(net::CanonicalizeHost(host, &host_info), | 184 if (base_url.is_valid() && (host == base_url.host_piece())) |
| 191 subdomain_permission); | 185 return true; |
| 186 |
| 187 return IsValidHostName(host, "google", subdomain_permission); |
| 192 } | 188 } |
| 193 | 189 |
| 194 bool IsGoogleDomainUrl(const GURL& url, | 190 bool IsGoogleDomainUrl(const GURL& url, |
| 195 SubdomainPermission subdomain_permission, | 191 SubdomainPermission subdomain_permission, |
| 196 PortPermission port_permission) { | 192 PortPermission port_permission) { |
| 197 return IsValidURL(url, port_permission) && | 193 return IsValidURL(url, port_permission) && |
| 198 IsCanonicalHostGoogleHostname(url.host_piece(), subdomain_permission); | 194 IsGoogleHostname(url.host(), subdomain_permission); |
| 199 } | 195 } |
| 200 | 196 |
| 201 bool IsGoogleHomePageUrl(const GURL& url) { | 197 bool IsGoogleHomePageUrl(const GURL& url) { |
| 202 // First check to see if this has a Google domain. | 198 // First check to see if this has a Google domain. |
| 203 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN, DISALLOW_NON_STANDARD_PORTS)) | 199 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN, DISALLOW_NON_STANDARD_PORTS)) |
| 204 return false; | 200 return false; |
| 205 | 201 |
| 206 // Make sure the path is a known home page path. | 202 // Make sure the path is a known home page path. |
| 207 base::StringPiece path(url.path_piece()); | 203 base::StringPiece path(url.path_piece()); |
| 208 return IsPathHomePageBase(path) || | 204 return IsPathHomePageBase(path) || |
| (...skipping 18 matching lines...) Expand all Loading... |
| 227 } | 223 } |
| 228 | 224 |
| 229 bool IsYoutubeDomainUrl(const GURL& url, | 225 bool IsYoutubeDomainUrl(const GURL& url, |
| 230 SubdomainPermission subdomain_permission, | 226 SubdomainPermission subdomain_permission, |
| 231 PortPermission port_permission) { | 227 PortPermission port_permission) { |
| 232 return IsValidURL(url, port_permission) && | 228 return IsValidURL(url, port_permission) && |
| 233 IsValidHostName(url.host_piece(), "youtube", subdomain_permission); | 229 IsValidHostName(url.host_piece(), "youtube", subdomain_permission); |
| 234 } | 230 } |
| 235 | 231 |
| 236 } // namespace google_util | 232 } // namespace google_util |
| OLD | NEW |