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 std::set<base::StringPiece> gGoogleSearchSubdomains{"ipv4.google.com", | |
|
Andrew T Wilson (Slow)
2017/05/09 13:36:47
This is a relatively complicated static which I th
igorcov
2017/05/10 13:25:20
Done.
| |
| 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./ | |
|
Andrew T Wilson (Slow)
2017/05/09 13:36:47
Log a bug for this and add a link to the bug with
igorcov
2017/05/10 13:25:20
Done.
| |
| 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); | |
| 236 | |
| 237 return gGoogleSearchSubdomains.find(host) != gGoogleSearchSubdomains.end(); | |
| 238 } | |
| 239 | |
| 218 bool IsGoogleHomePageUrl(const GURL& url) { | 240 bool IsGoogleHomePageUrl(const GURL& url) { |
| 219 // First check to see if this has a Google domain. | 241 // First check to see if this has a Google domain. |
| 220 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN, DISALLOW_NON_STANDARD_PORTS)) | 242 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN, |
| 243 DISALLOW_NON_STANDARD_PORTS) && | |
| 244 !IsGoogleSearchSubdomainUrl(url, DISALLOW_NON_STANDARD_PORTS)) | |
|
Andrew T Wilson (Slow)
2017/05/09 13:36:47
Multi-line if statements require braces.
igorcov
2017/05/10 13:25:20
Done.
| |
| 221 return false; | 245 return false; |
| 222 | 246 |
| 223 // Make sure the path is a known home page path. | 247 // Make sure the path is a known home page path. |
| 224 base::StringPiece path(url.path_piece()); | 248 base::StringPiece path(url.path_piece()); |
| 225 return IsPathHomePageBase(path) || | 249 return IsPathHomePageBase(path) || |
| 226 base::StartsWith(path, "/ig", base::CompareCase::INSENSITIVE_ASCII); | 250 base::StartsWith(path, "/ig", base::CompareCase::INSENSITIVE_ASCII); |
| 227 } | 251 } |
| 228 | 252 |
| 229 bool IsGoogleSearchUrl(const GURL& url) { | 253 bool IsGoogleSearchUrl(const GURL& url) { |
| 230 // First check to see if this has a Google domain. | 254 // First check to see if this has a Google domain. |
| 231 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN, DISALLOW_NON_STANDARD_PORTS)) | 255 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN, |
| 256 DISALLOW_NON_STANDARD_PORTS) && | |
| 257 !IsGoogleSearchSubdomainUrl(url, DISALLOW_NON_STANDARD_PORTS)) | |
|
Andrew T Wilson (Slow)
2017/05/09 13:36:47
For a multi-line if statement, you need to use bra
igorcov
2017/05/10 13:25:21
Done.
| |
| 232 return false; | 258 return false; |
| 233 | 259 |
| 234 // Make sure the path is a known search path. | 260 // Make sure the path is a known search path. |
| 235 base::StringPiece path(url.path_piece()); | 261 base::StringPiece path(url.path_piece()); |
| 236 bool is_home_page_base = IsPathHomePageBase(path); | 262 bool is_home_page_base = IsPathHomePageBase(path); |
| 237 if (!is_home_page_base && (path != "/search")) | 263 if (!is_home_page_base && (path != "/search")) |
| 238 return false; | 264 return false; |
| 239 | 265 |
| 240 // Check for query parameter in URL parameter and hash fragment, depending on | 266 // Check for query parameter in URL parameter and hash fragment, depending on |
| 241 // the path type. | 267 // the path type. |
| 242 return HasGoogleSearchQueryParam(url.ref_piece()) || | 268 return HasGoogleSearchQueryParam(url.ref_piece()) || |
| 243 (!is_home_page_base && HasGoogleSearchQueryParam(url.query_piece())); | 269 (!is_home_page_base && HasGoogleSearchQueryParam(url.query_piece())); |
| 244 } | 270 } |
| 245 | 271 |
| 246 bool IsYoutubeDomainUrl(const GURL& url, | 272 bool IsYoutubeDomainUrl(const GURL& url, |
| 247 SubdomainPermission subdomain_permission, | 273 SubdomainPermission subdomain_permission, |
| 248 PortPermission port_permission) { | 274 PortPermission port_permission) { |
| 249 return IsValidURL(url, port_permission) && | 275 return IsValidURL(url, port_permission) && |
| 250 IsValidHostName(url.host_piece(), "youtube", subdomain_permission, | 276 IsValidHostName(url.host_piece(), "youtube", subdomain_permission, |
| 251 nullptr); | 277 nullptr); |
| 252 } | 278 } |
| 253 | 279 |
| 254 } // namespace google_util | 280 } // namespace google_util |
| OLD | NEW |