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