Chromium Code Reviews| 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 23 matching lines...) Expand all Loading... | |
| 34 #endif | 34 #endif |
| 35 | 35 |
| 36 namespace google_util { | 36 namespace google_util { |
| 37 | 37 |
| 38 // Helpers -------------------------------------------------------------------- | 38 // Helpers -------------------------------------------------------------------- |
| 39 | 39 |
| 40 namespace { | 40 namespace { |
| 41 | 41 |
| 42 bool gUseMockLinkDoctorBaseURLForTesting = false; | 42 bool gUseMockLinkDoctorBaseURLForTesting = false; |
| 43 | 43 |
| 44 const char* const gGoogleSearchSubdomains[] = {"ipv4.google.com", | |
| 45 "ipv6.google.com"}; | |
| 46 | |
| 44 bool IsPathHomePageBase(base::StringPiece path) { | 47 bool IsPathHomePageBase(base::StringPiece path) { |
| 45 return (path == "/") || (path == "/webhp"); | 48 return (path == "/") || (path == "/webhp"); |
| 46 } | 49 } |
| 47 | 50 |
| 48 // True if the given canonical |host| is "[www.]<domain_in_lower_case>.<TLD>" | 51 // True if the given canonical |host| is "[www.]<domain_in_lower_case>.<TLD>" |
| 49 // with a valid TLD. If |subdomain_permission| is ALLOW_SUBDOMAIN, we check | 52 // with a valid TLD. If |subdomain_permission| is ALLOW_SUBDOMAIN, we check |
| 50 // against host "*.<domain_in_lower_case>.<TLD>" instead. Will return the TLD | 53 // against host "*.<domain_in_lower_case>.<TLD>" instead. Will return the TLD |
| 51 // string in |tld|, if specified and the |host| can be parsed. | 54 // string in |tld|, if specified and the |host| can be parsed. |
| 52 bool IsValidHostName(base::StringPiece host, | 55 bool IsValidHostName(base::StringPiece host, |
| 53 base::StringPiece domain_in_lower_case, | 56 base::StringPiece domain_in_lower_case, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 97 bool IsCanonicalHostGoogleHostname(base::StringPiece canonical_host, | 100 bool IsCanonicalHostGoogleHostname(base::StringPiece canonical_host, |
| 98 SubdomainPermission subdomain_permission) { | 101 SubdomainPermission subdomain_permission) { |
| 99 const GURL& base_url(CommandLineGoogleBaseURL()); | 102 const GURL& base_url(CommandLineGoogleBaseURL()); |
| 100 if (base_url.is_valid() && (canonical_host == base_url.host_piece())) | 103 if (base_url.is_valid() && (canonical_host == base_url.host_piece())) |
| 101 return true; | 104 return true; |
| 102 | 105 |
| 103 base::StringPiece tld; | 106 base::StringPiece tld; |
| 104 if (!IsValidHostName(canonical_host, "google", subdomain_permission, &tld)) | 107 if (!IsValidHostName(canonical_host, "google", subdomain_permission, &tld)) |
| 105 return false; | 108 return false; |
| 106 | 109 |
| 110 // Remove the trailing dot from tld if present, as for google domain it's the | |
| 111 // same page. | |
| 112 if (tld.ends_with(".")) | |
| 113 tld = tld.substr(0, tld.length() - 1); | |
| 114 | |
| 107 CR_DEFINE_STATIC_LOCAL(std::set<std::string>, google_tlds, | 115 CR_DEFINE_STATIC_LOCAL(std::set<std::string>, google_tlds, |
| 108 ({GOOGLE_TLD_LIST})); | 116 ({GOOGLE_TLD_LIST})); |
| 109 return base::ContainsKey(google_tlds, tld.as_string()); | 117 return base::ContainsKey(google_tlds, tld.as_string()); |
| 110 } | 118 } |
| 111 | 119 |
| 112 } // namespace | 120 } // namespace |
| 113 | 121 |
| 114 // Global functions ----------------------------------------------------------- | 122 // Global functions ----------------------------------------------------------- |
| 115 | 123 |
| 116 bool HasGoogleSearchQueryParam(base::StringPiece str) { | 124 bool HasGoogleSearchQueryParam(base::StringPiece str) { |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 141 } | 149 } |
| 142 | 150 |
| 143 GURL AppendGoogleLocaleParam(const GURL& url, | 151 GURL AppendGoogleLocaleParam(const GURL& url, |
| 144 const std::string& application_locale) { | 152 const std::string& application_locale) { |
| 145 return net::AppendQueryParameter( | 153 return net::AppendQueryParameter( |
| 146 url, "hl", GetGoogleLocale(application_locale)); | 154 url, "hl", GetGoogleLocale(application_locale)); |
| 147 } | 155 } |
| 148 | 156 |
| 149 std::string GetGoogleCountryCode(const GURL& google_homepage_url) { | 157 std::string GetGoogleCountryCode(const GURL& google_homepage_url) { |
| 150 base::StringPiece google_hostname = google_homepage_url.host_piece(); | 158 base::StringPiece google_hostname = google_homepage_url.host_piece(); |
| 159 // TODO(igorcov): This needs a fix for case when the host has a trailing dot, | |
| 160 // like "google.com./". https://crbug.com/720295. | |
| 151 const size_t last_dot = google_hostname.find_last_of('.'); | 161 const size_t last_dot = google_hostname.find_last_of('.'); |
| 152 if (last_dot == std::string::npos) { | 162 if (last_dot == std::string::npos) { |
| 153 NOTREACHED(); | 163 NOTREACHED(); |
| 154 } | 164 } |
| 155 base::StringPiece country_code = google_hostname.substr(last_dot + 1); | 165 base::StringPiece country_code = google_hostname.substr(last_dot + 1); |
| 156 // Assume the com TLD implies the US. | 166 // Assume the com TLD implies the US. |
| 157 if (country_code == "com") | 167 if (country_code == "com") |
| 158 return "us"; | 168 return "us"; |
| 159 // Google uses the Unicode Common Locale Data Repository (CLDR), and the CLDR | 169 // Google uses the Unicode Common Locale Data Repository (CLDR), and the CLDR |
| 160 // code for the UK is "gb". | 170 // code for the UK is "gb". |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 subdomain_permission); | 218 subdomain_permission); |
| 209 } | 219 } |
| 210 | 220 |
| 211 bool IsGoogleDomainUrl(const GURL& url, | 221 bool IsGoogleDomainUrl(const GURL& url, |
| 212 SubdomainPermission subdomain_permission, | 222 SubdomainPermission subdomain_permission, |
| 213 PortPermission port_permission) { | 223 PortPermission port_permission) { |
| 214 return IsValidURL(url, port_permission) && | 224 return IsValidURL(url, port_permission) && |
| 215 IsCanonicalHostGoogleHostname(url.host_piece(), subdomain_permission); | 225 IsCanonicalHostGoogleHostname(url.host_piece(), subdomain_permission); |
| 216 } | 226 } |
| 217 | 227 |
| 228 bool IsGoogleSearchSubdomainUrl(const GURL& url, | |
| 229 PortPermission port_permission) { | |
| 230 if (!IsValidURL(url, port_permission)) | |
| 231 return false; | |
| 232 | |
| 233 base::StringPiece host(url.host_piece()); | |
| 234 if (host.ends_with(".")) | |
| 235 host = host.substr(0, host.length() - 1); | |
|
Andrew T Wilson (Slow)
2017/05/10 14:54:17
This is fine, but if we do this anywhere else we m
igorcov
2017/05/10 16:19:56
Done, thanks.
| |
| 236 | |
| 237 return std::binary_search( | |
| 238 gGoogleSearchSubdomains, | |
|
Andrew T Wilson (Slow)
2017/05/10 14:54:17
Binary search is probably overkill for a two-item
igorcov
2017/05/10 16:19:56
Right, forgot to put the comment. Don't know which
| |
| 239 gGoogleSearchSubdomains + arraysize(gGoogleSearchSubdomains), host); | |
| 240 } | |
| 241 | |
| 218 bool IsGoogleHomePageUrl(const GURL& url) { | 242 bool IsGoogleHomePageUrl(const GURL& url) { |
| 219 // First check to see if this has a Google domain. | 243 // First check to see if this has a Google domain. |
| 220 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN, DISALLOW_NON_STANDARD_PORTS)) | 244 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN, |
| 245 DISALLOW_NON_STANDARD_PORTS) && | |
| 246 !IsGoogleSearchSubdomainUrl(url, DISALLOW_NON_STANDARD_PORTS)) { | |
| 221 return false; | 247 return false; |
| 248 } | |
| 222 | 249 |
| 223 // Make sure the path is a known home page path. | 250 // Make sure the path is a known home page path. |
| 224 base::StringPiece path(url.path_piece()); | 251 base::StringPiece path(url.path_piece()); |
| 225 return IsPathHomePageBase(path) || | 252 return IsPathHomePageBase(path) || |
| 226 base::StartsWith(path, "/ig", base::CompareCase::INSENSITIVE_ASCII); | 253 base::StartsWith(path, "/ig", base::CompareCase::INSENSITIVE_ASCII); |
| 227 } | 254 } |
| 228 | 255 |
| 229 bool IsGoogleSearchUrl(const GURL& url) { | 256 bool IsGoogleSearchUrl(const GURL& url) { |
| 230 // First check to see if this has a Google domain. | 257 // First check to see if this has a Google domain. |
| 231 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN, DISALLOW_NON_STANDARD_PORTS)) | 258 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN, |
| 259 DISALLOW_NON_STANDARD_PORTS) && | |
| 260 !IsGoogleSearchSubdomainUrl(url, DISALLOW_NON_STANDARD_PORTS)) { | |
| 232 return false; | 261 return false; |
| 262 } | |
| 233 | 263 |
| 234 // Make sure the path is a known search path. | 264 // Make sure the path is a known search path. |
| 235 base::StringPiece path(url.path_piece()); | 265 base::StringPiece path(url.path_piece()); |
| 236 bool is_home_page_base = IsPathHomePageBase(path); | 266 bool is_home_page_base = IsPathHomePageBase(path); |
| 237 if (!is_home_page_base && (path != "/search")) | 267 if (!is_home_page_base && (path != "/search")) |
| 238 return false; | 268 return false; |
| 239 | 269 |
| 240 // Check for query parameter in URL parameter and hash fragment, depending on | 270 // Check for query parameter in URL parameter and hash fragment, depending on |
| 241 // the path type. | 271 // the path type. |
| 242 return HasGoogleSearchQueryParam(url.ref_piece()) || | 272 return HasGoogleSearchQueryParam(url.ref_piece()) || |
| 243 (!is_home_page_base && HasGoogleSearchQueryParam(url.query_piece())); | 273 (!is_home_page_base && HasGoogleSearchQueryParam(url.query_piece())); |
| 244 } | 274 } |
| 245 | 275 |
| 246 bool IsYoutubeDomainUrl(const GURL& url, | 276 bool IsYoutubeDomainUrl(const GURL& url, |
| 247 SubdomainPermission subdomain_permission, | 277 SubdomainPermission subdomain_permission, |
| 248 PortPermission port_permission) { | 278 PortPermission port_permission) { |
| 249 return IsValidURL(url, port_permission) && | 279 return IsValidURL(url, port_permission) && |
| 250 IsValidHostName(url.host_piece(), "youtube", subdomain_permission, | 280 IsValidHostName(url.host_piece(), "youtube", subdomain_permission, |
| 251 nullptr); | 281 nullptr); |
| 252 } | 282 } |
| 253 | 283 |
| 254 } // namespace google_util | 284 } // namespace google_util |
| OLD | NEW |