Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Unified Diff: components/google/core/browser/google_util.cc

Issue 2861183002: Google search subdomains included for Safesearch (Closed)
Patch Set: Fixed review comments Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/google/core/browser/google_util.cc
diff --git a/components/google/core/browser/google_util.cc b/components/google/core/browser/google_util.cc
index a16d55eaafdd0daa4d89de8523ad121be7cc5f84..2c779ee13023b55bb4028b95bdca7e545518cf29 100644
--- a/components/google/core/browser/google_util.cc
+++ b/components/google/core/browser/google_util.cc
@@ -41,10 +41,19 @@ namespace {
bool gUseMockLinkDoctorBaseURLForTesting = false;
+const char* const gGoogleSearchSubdomains[] = {"ipv4.google.com",
Ilya Sherman 2017/05/11 00:33:35 nit: s/g/k, since this is a constant.
Peter Kasting 2017/05/11 00:48:08 Also, prefer to define this locally in the functio
igorcov 2017/05/11 11:30:05 Included all this locally inside a static set.
+ "ipv6.google.com"};
+
bool IsPathHomePageBase(base::StringPiece path) {
return (path == "/") || (path == "/webhp");
}
+// Removes a single trailing dot if present in |host|.
+void StripTrailingDot(base::StringPiece* host) {
+ if (host->ends_with("."))
+ host->remove_suffix(1);
+}
+
// True if the given canonical |host| is "[www.]<domain_in_lower_case>.<TLD>"
// with a valid TLD. If |subdomain_permission| is ALLOW_SUBDOMAIN, we check
// against host "*.<domain_in_lower_case>.<TLD>" instead. Will return the TLD
@@ -104,6 +113,10 @@ bool IsCanonicalHostGoogleHostname(base::StringPiece canonical_host,
if (!IsValidHostName(canonical_host, "google", subdomain_permission, &tld))
return false;
+ // Remove the trailing dot from tld if present, as for google domain it's the
+ // same page.
+ StripTrailingDot(&tld);
+
CR_DEFINE_STATIC_LOCAL(std::set<std::string>, google_tlds,
({GOOGLE_TLD_LIST}));
return base::ContainsKey(google_tlds, tld.as_string());
@@ -148,6 +161,8 @@ GURL AppendGoogleLocaleParam(const GURL& url,
std::string GetGoogleCountryCode(const GURL& google_homepage_url) {
base::StringPiece google_hostname = google_homepage_url.host_piece();
+ // TODO(igorcov): This needs a fix for case when the host has a trailing dot,
+ // like "google.com./". https://crbug.com/720295.
const size_t last_dot = google_hostname.find_last_of('.');
if (last_dot == std::string::npos) {
NOTREACHED();
@@ -215,10 +230,32 @@ bool IsGoogleDomainUrl(const GURL& url,
IsCanonicalHostGoogleHostname(url.host_piece(), subdomain_permission);
}
+bool IsGoogleSearchSubdomainUrl(const GURL& url,
+ PortPermission port_permission) {
Peter Kasting 2017/05/11 00:48:08 Nit: Callers all seem to pass DISALLOW_NON_STANDAR
igorcov 2017/05/11 11:30:05 Done.
+ if (!IsValidURL(url, port_permission))
+ return false;
+
+ base::StringPiece host(url.host_piece());
+ StripTrailingDot(&host);
+
+ // If the size of |gGoogleSearchSubdomains| becomes larger, it might make
+ // sense to std::binary_search this.
+ for (const char* subdomain : gGoogleSearchSubdomains) {
Peter Kasting 2017/05/11 00:48:08 Nit: It might make sense to CR_DEFINE_STATIC_LOCAL
igorcov 2017/05/11 11:30:05 Done.
+ if (host == subdomain) {
Peter Kasting 2017/05/11 00:48:08 Nit: {} not necessary
+ return true;
+ }
+ }
+
+ return false;
+}
+
bool IsGoogleHomePageUrl(const GURL& url) {
// First check to see if this has a Google domain.
- if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN, DISALLOW_NON_STANDARD_PORTS))
+ if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN,
+ DISALLOW_NON_STANDARD_PORTS) &&
+ !IsGoogleSearchSubdomainUrl(url, DISALLOW_NON_STANDARD_PORTS)) {
return false;
+ }
// Make sure the path is a known home page path.
base::StringPiece path(url.path_piece());
@@ -228,8 +265,11 @@ bool IsGoogleHomePageUrl(const GURL& url) {
bool IsGoogleSearchUrl(const GURL& url) {
// First check to see if this has a Google domain.
- if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN, DISALLOW_NON_STANDARD_PORTS))
+ if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN,
+ DISALLOW_NON_STANDARD_PORTS) &&
+ !IsGoogleSearchSubdomainUrl(url, DISALLOW_NON_STANDARD_PORTS)) {
return false;
+ }
// Make sure the path is a known search path.
base::StringPiece path(url.path_piece());

Powered by Google App Engine
This is Rietveld 408576698