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

Side by Side 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 unified diff | Download patch
OLDNEW
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
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",
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.
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
51 // Removes a single trailing dot if present in |host|.
52 void StripTrailingDot(base::StringPiece* host) {
53 if (host->ends_with("."))
54 host->remove_suffix(1);
55 }
56
48 // True if the given canonical |host| is "[www.]<domain_in_lower_case>.<TLD>" 57 // 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 58 // 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 59 // against host "*.<domain_in_lower_case>.<TLD>" instead. Will return the TLD
51 // string in |tld|, if specified and the |host| can be parsed. 60 // string in |tld|, if specified and the |host| can be parsed.
52 bool IsValidHostName(base::StringPiece host, 61 bool IsValidHostName(base::StringPiece host,
53 base::StringPiece domain_in_lower_case, 62 base::StringPiece domain_in_lower_case,
54 SubdomainPermission subdomain_permission, 63 SubdomainPermission subdomain_permission,
55 base::StringPiece* tld) { 64 base::StringPiece* tld) {
56 // Fast path to avoid searching the registry set. 65 // Fast path to avoid searching the registry set.
57 if (host.find(domain_in_lower_case) == base::StringPiece::npos) 66 if (host.find(domain_in_lower_case) == base::StringPiece::npos)
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 bool IsCanonicalHostGoogleHostname(base::StringPiece canonical_host, 106 bool IsCanonicalHostGoogleHostname(base::StringPiece canonical_host,
98 SubdomainPermission subdomain_permission) { 107 SubdomainPermission subdomain_permission) {
99 const GURL& base_url(CommandLineGoogleBaseURL()); 108 const GURL& base_url(CommandLineGoogleBaseURL());
100 if (base_url.is_valid() && (canonical_host == base_url.host_piece())) 109 if (base_url.is_valid() && (canonical_host == base_url.host_piece()))
101 return true; 110 return true;
102 111
103 base::StringPiece tld; 112 base::StringPiece tld;
104 if (!IsValidHostName(canonical_host, "google", subdomain_permission, &tld)) 113 if (!IsValidHostName(canonical_host, "google", subdomain_permission, &tld))
105 return false; 114 return false;
106 115
116 // Remove the trailing dot from tld if present, as for google domain it's the
117 // same page.
118 StripTrailingDot(&tld);
119
107 CR_DEFINE_STATIC_LOCAL(std::set<std::string>, google_tlds, 120 CR_DEFINE_STATIC_LOCAL(std::set<std::string>, google_tlds,
108 ({GOOGLE_TLD_LIST})); 121 ({GOOGLE_TLD_LIST}));
109 return base::ContainsKey(google_tlds, tld.as_string()); 122 return base::ContainsKey(google_tlds, tld.as_string());
110 } 123 }
111 124
112 } // namespace 125 } // namespace
113 126
114 // Global functions ----------------------------------------------------------- 127 // Global functions -----------------------------------------------------------
115 128
116 bool HasGoogleSearchQueryParam(base::StringPiece str) { 129 bool HasGoogleSearchQueryParam(base::StringPiece str) {
(...skipping 24 matching lines...) Expand all
141 } 154 }
142 155
143 GURL AppendGoogleLocaleParam(const GURL& url, 156 GURL AppendGoogleLocaleParam(const GURL& url,
144 const std::string& application_locale) { 157 const std::string& application_locale) {
145 return net::AppendQueryParameter( 158 return net::AppendQueryParameter(
146 url, "hl", GetGoogleLocale(application_locale)); 159 url, "hl", GetGoogleLocale(application_locale));
147 } 160 }
148 161
149 std::string GetGoogleCountryCode(const GURL& google_homepage_url) { 162 std::string GetGoogleCountryCode(const GURL& google_homepage_url) {
150 base::StringPiece google_hostname = google_homepage_url.host_piece(); 163 base::StringPiece google_hostname = google_homepage_url.host_piece();
164 // TODO(igorcov): This needs a fix for case when the host has a trailing dot,
165 // like "google.com./". https://crbug.com/720295.
151 const size_t last_dot = google_hostname.find_last_of('.'); 166 const size_t last_dot = google_hostname.find_last_of('.');
152 if (last_dot == std::string::npos) { 167 if (last_dot == std::string::npos) {
153 NOTREACHED(); 168 NOTREACHED();
154 } 169 }
155 base::StringPiece country_code = google_hostname.substr(last_dot + 1); 170 base::StringPiece country_code = google_hostname.substr(last_dot + 1);
156 // Assume the com TLD implies the US. 171 // Assume the com TLD implies the US.
157 if (country_code == "com") 172 if (country_code == "com")
158 return "us"; 173 return "us";
159 // Google uses the Unicode Common Locale Data Repository (CLDR), and the CLDR 174 // Google uses the Unicode Common Locale Data Repository (CLDR), and the CLDR
160 // code for the UK is "gb". 175 // code for the UK is "gb".
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 subdomain_permission); 223 subdomain_permission);
209 } 224 }
210 225
211 bool IsGoogleDomainUrl(const GURL& url, 226 bool IsGoogleDomainUrl(const GURL& url,
212 SubdomainPermission subdomain_permission, 227 SubdomainPermission subdomain_permission,
213 PortPermission port_permission) { 228 PortPermission port_permission) {
214 return IsValidURL(url, port_permission) && 229 return IsValidURL(url, port_permission) &&
215 IsCanonicalHostGoogleHostname(url.host_piece(), subdomain_permission); 230 IsCanonicalHostGoogleHostname(url.host_piece(), subdomain_permission);
216 } 231 }
217 232
233 bool IsGoogleSearchSubdomainUrl(const GURL& url,
234 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.
235 if (!IsValidURL(url, port_permission))
236 return false;
237
238 base::StringPiece host(url.host_piece());
239 StripTrailingDot(&host);
240
241 // If the size of |gGoogleSearchSubdomains| becomes larger, it might make
242 // sense to std::binary_search this.
243 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.
244 if (host == subdomain) {
Peter Kasting 2017/05/11 00:48:08 Nit: {} not necessary
245 return true;
246 }
247 }
248
249 return false;
250 }
251
218 bool IsGoogleHomePageUrl(const GURL& url) { 252 bool IsGoogleHomePageUrl(const GURL& url) {
219 // First check to see if this has a Google domain. 253 // First check to see if this has a Google domain.
220 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN, DISALLOW_NON_STANDARD_PORTS)) 254 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN,
255 DISALLOW_NON_STANDARD_PORTS) &&
256 !IsGoogleSearchSubdomainUrl(url, DISALLOW_NON_STANDARD_PORTS)) {
221 return false; 257 return false;
258 }
222 259
223 // Make sure the path is a known home page path. 260 // Make sure the path is a known home page path.
224 base::StringPiece path(url.path_piece()); 261 base::StringPiece path(url.path_piece());
225 return IsPathHomePageBase(path) || 262 return IsPathHomePageBase(path) ||
226 base::StartsWith(path, "/ig", base::CompareCase::INSENSITIVE_ASCII); 263 base::StartsWith(path, "/ig", base::CompareCase::INSENSITIVE_ASCII);
227 } 264 }
228 265
229 bool IsGoogleSearchUrl(const GURL& url) { 266 bool IsGoogleSearchUrl(const GURL& url) {
230 // First check to see if this has a Google domain. 267 // First check to see if this has a Google domain.
231 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN, DISALLOW_NON_STANDARD_PORTS)) 268 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN,
269 DISALLOW_NON_STANDARD_PORTS) &&
270 !IsGoogleSearchSubdomainUrl(url, DISALLOW_NON_STANDARD_PORTS)) {
232 return false; 271 return false;
272 }
233 273
234 // Make sure the path is a known search path. 274 // Make sure the path is a known search path.
235 base::StringPiece path(url.path_piece()); 275 base::StringPiece path(url.path_piece());
236 bool is_home_page_base = IsPathHomePageBase(path); 276 bool is_home_page_base = IsPathHomePageBase(path);
237 if (!is_home_page_base && (path != "/search")) 277 if (!is_home_page_base && (path != "/search"))
238 return false; 278 return false;
239 279
240 // Check for query parameter in URL parameter and hash fragment, depending on 280 // Check for query parameter in URL parameter and hash fragment, depending on
241 // the path type. 281 // the path type.
242 return HasGoogleSearchQueryParam(url.ref_piece()) || 282 return HasGoogleSearchQueryParam(url.ref_piece()) ||
243 (!is_home_page_base && HasGoogleSearchQueryParam(url.query_piece())); 283 (!is_home_page_base && HasGoogleSearchQueryParam(url.query_piece()));
244 } 284 }
245 285
246 bool IsYoutubeDomainUrl(const GURL& url, 286 bool IsYoutubeDomainUrl(const GURL& url,
247 SubdomainPermission subdomain_permission, 287 SubdomainPermission subdomain_permission,
248 PortPermission port_permission) { 288 PortPermission port_permission) {
249 return IsValidURL(url, port_permission) && 289 return IsValidURL(url, port_permission) &&
250 IsValidHostName(url.host_piece(), "youtube", subdomain_permission, 290 IsValidHostName(url.host_piece(), "youtube", subdomain_permission,
251 nullptr); 291 nullptr);
252 } 292 }
253 293
254 } // namespace google_util 294 } // namespace google_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698