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

Side by Side Diff: net/base/registry_controlled_domains/registry_controlled_domain.cc

Issue 2562813003: Merge logic for SameDomainOrHost for GURLs and Origins (Closed)
Patch Set: Created 4 years 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
« no previous file with comments | « no previous file | url/gurl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // NB: Modelled after Mozilla's code (originally written by Pamela Greene, 5 // NB: Modelled after Mozilla's code (originally written by Pamela Greene,
6 // later modified by others), but almost entirely rewritten for Chrome. 6 // later modified by others), but almost entirely rewritten for Chrome.
7 // (netwerk/dns/src/nsEffectiveTLDService.cpp) 7 // (netwerk/dns/src/nsEffectiveTLDService.cpp)
8 /* ***** BEGIN LICENSE BLOCK ***** 8 /* ***** BEGIN LICENSE BLOCK *****
9 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 9 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
10 * 10 *
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 47
48 #include "base/logging.h" 48 #include "base/logging.h"
49 #include "base/strings/string_util.h" 49 #include "base/strings/string_util.h"
50 #include "base/strings/utf_string_conversions.h" 50 #include "base/strings/utf_string_conversions.h"
51 #include "net/base/lookup_string_in_fixed_set.h" 51 #include "net/base/lookup_string_in_fixed_set.h"
52 #include "net/base/net_module.h" 52 #include "net/base/net_module.h"
53 #include "net/base/url_util.h" 53 #include "net/base/url_util.h"
54 #include "url/gurl.h" 54 #include "url/gurl.h"
55 #include "url/origin.h" 55 #include "url/origin.h"
56 #include "url/third_party/mozilla/url_parse.h" 56 #include "url/third_party/mozilla/url_parse.h"
57 #include "url/url_util.h"
57 58
58 namespace net { 59 namespace net {
59 namespace registry_controlled_domains { 60 namespace registry_controlled_domains {
60 61
61 namespace { 62 namespace {
62 #include "net/base/registry_controlled_domains/effective_tld_names-inc.cc" 63 #include "net/base/registry_controlled_domains/effective_tld_names-inc.cc"
63 64
64 // See make_dafsa.py for documentation of the generated dafsa byte array. 65 // See make_dafsa.py for documentation of the generated dafsa byte array.
65 66
66 const unsigned char* g_graph = kDafsa; 67 const unsigned char* g_graph = kDafsa;
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 if (dot == std::string::npos) 184 if (dot == std::string::npos)
184 return host; 185 return host;
185 return host.substr(dot + 1); 186 return host.substr(dot + 1);
186 } 187 }
187 188
188 // Same as GetDomainAndRegistry, but returns the domain and registry as a 189 // Same as GetDomainAndRegistry, but returns the domain and registry as a
189 // StringPiece that references the underlying string of the passed-in |gurl|. 190 // StringPiece that references the underlying string of the passed-in |gurl|.
190 // TODO(pkalinnikov): Eliminate this helper by exposing StringPiece as the 191 // TODO(pkalinnikov): Eliminate this helper by exposing StringPiece as the
191 // interface type for all the APIs. 192 // interface type for all the APIs.
192 base::StringPiece GetDomainAndRegistryAsStringPiece( 193 base::StringPiece GetDomainAndRegistryAsStringPiece(
193 const GURL& gurl, 194 base::StringPiece host,
194 PrivateRegistryFilter filter) { 195 PrivateRegistryFilter filter) {
195 base::StringPiece host = gurl.host_piece(); 196 if (host.empty() || url::HostIsIPAddress(host))
196 if (host.empty() || gurl.HostIsIPAddress())
197 return base::StringPiece(); 197 return base::StringPiece();
198 return GetDomainAndRegistryImpl(host, filter); 198 return GetDomainAndRegistryImpl(host, filter);
199 } 199 }
200 200
201 // These two functions append the given string as-is to the given output, 201 // These two functions append the given string as-is to the given output,
202 // converting to UTF-8 if necessary. 202 // converting to UTF-8 if necessary.
203 void AppendInvalidString(base::StringPiece str, url::CanonOutput* output) { 203 void AppendInvalidString(base::StringPiece str, url::CanonOutput* output) {
204 output->Append(str.data(), static_cast<int>(str.length())); 204 output->Append(str.data(), static_cast<int>(str.length()));
205 } 205 }
206 void AppendInvalidString(base::StringPiece16 str, url::CanonOutput* output) { 206 void AppendInvalidString(base::StringPiece16 str, url::CanonOutput* output) {
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 try_output.Complete(); 302 try_output.Complete();
303 if (try_string == canonical_rcd) 303 if (try_string == canonical_rcd)
304 return host.length() - current_try; 304 return host.length() - current_try;
305 } 305 }
306 } 306 }
307 307
308 NOTREACHED(); 308 NOTREACHED();
309 return canonical_rcd_len; 309 return canonical_rcd_len;
310 } 310 }
311 311
312 bool SameDomainOrHost(base::StringPiece host1,
313 base::StringPiece host2,
314 PrivateRegistryFilter filter) {
315 // Quickly reject cases where either host is empty.
316 if (!host1.length() || !host2.length())
Peter Kasting 2016/12/09 00:17:59 Nit: Prefer .empty() to !.length()
Charlie Harrison 2016/12/09 01:14:01 Done.
317 return false;
318
319 // Check for exact host matches, which is faster than looking up the domain
320 // and registry.
321 if (host1 == host2)
322 return true;
323
324 // Check for a domain and registry match.
325 const base::StringPiece& domain1 =
326 GetDomainAndRegistryAsStringPiece(host1, filter);
327 return !domain1.empty() &&
328 (domain1 == GetDomainAndRegistryAsStringPiece(host2, filter));
329 }
330
312 } // namespace 331 } // namespace
313 332
314 std::string GetDomainAndRegistry(const GURL& gurl, 333 std::string GetDomainAndRegistry(const GURL& gurl,
315 PrivateRegistryFilter filter) { 334 PrivateRegistryFilter filter) {
316 return GetDomainAndRegistryAsStringPiece(gurl, filter).as_string(); 335 return GetDomainAndRegistryAsStringPiece(gurl.host_piece(), filter)
336 .as_string();
317 } 337 }
318 338
319 std::string GetDomainAndRegistry(base::StringPiece host, 339 std::string GetDomainAndRegistry(base::StringPiece host,
320 PrivateRegistryFilter filter) { 340 PrivateRegistryFilter filter) {
321 url::CanonHostInfo host_info; 341 url::CanonHostInfo host_info;
322 const std::string canon_host(CanonicalizeHost(host, &host_info)); 342 const std::string canon_host(CanonicalizeHost(host, &host_info));
323 if (canon_host.empty() || host_info.IsIPAddress()) 343 if (canon_host.empty() || host_info.IsIPAddress())
324 return std::string(); 344 return std::string();
325 return GetDomainAndRegistryImpl(canon_host, filter).as_string(); 345 return GetDomainAndRegistryImpl(canon_host, filter).as_string();
326 } 346 }
327 347
328 bool SameDomainOrHost( 348 bool SameDomainOrHost(
329 const GURL& gurl1, 349 const GURL& gurl1,
330 const GURL& gurl2, 350 const GURL& gurl2,
331 PrivateRegistryFilter filter) { 351 PrivateRegistryFilter filter) {
332 // Quickly reject cases where either host is empty. 352 return SameDomainOrHost(gurl1.host_piece(), gurl2.host_piece(), filter);
333 if (!gurl1.has_host() || !gurl2.has_host())
334 return false;
335
336 // Check for exact host matches, which is faster than looking up the domain
337 // and registry.
338 if (gurl1.host_piece() == gurl2.host_piece())
339 return true;
340
341 // Check for a domain and registry match.
342 const base::StringPiece& domain1 =
343 GetDomainAndRegistryAsStringPiece(gurl1, filter);
344 return !domain1.empty() &&
345 (domain1 == GetDomainAndRegistryAsStringPiece(gurl2, filter));
346 } 353 }
347 354
348 bool SameDomainOrHost(const url::Origin& origin1, 355 bool SameDomainOrHost(const url::Origin& origin1,
349 const url::Origin& origin2, 356 const url::Origin& origin2,
350 PrivateRegistryFilter filter) { 357 PrivateRegistryFilter filter) {
351 return SameDomainOrHost(origin1.GetURL(), origin2.GetURL(), filter); 358 return SameDomainOrHost(origin1.host(), origin2.host(), filter);
352 } 359 }
353 360
354 bool SameDomainOrHost(const url::Origin& origin1, 361 bool SameDomainOrHost(const url::Origin& origin1,
355 const base::Optional<url::Origin>& origin2, 362 const base::Optional<url::Origin>& origin2,
356 PrivateRegistryFilter filter) { 363 PrivateRegistryFilter filter) {
357 if (!origin2.has_value()) 364 return origin2.has_value() &&
358 return false; 365 SameDomainOrHost(origin1, origin2.value(), filter);
359 return SameDomainOrHost(origin1, origin2.value(), filter);
360 } 366 }
361 367
362 size_t GetRegistryLength( 368 size_t GetRegistryLength(
363 const GURL& gurl, 369 const GURL& gurl,
364 UnknownRegistryFilter unknown_filter, 370 UnknownRegistryFilter unknown_filter,
365 PrivateRegistryFilter private_filter) { 371 PrivateRegistryFilter private_filter) {
366 return GetRegistryLengthImpl(gurl.host_piece(), unknown_filter, 372 return GetRegistryLengthImpl(gurl.host_piece(), unknown_filter,
367 private_filter); 373 private_filter);
368 } 374 }
369 375
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 435
430 void SetFindDomainGraph(const unsigned char* domains, size_t length) { 436 void SetFindDomainGraph(const unsigned char* domains, size_t length) {
431 CHECK(domains); 437 CHECK(domains);
432 CHECK_NE(length, 0u); 438 CHECK_NE(length, 0u);
433 g_graph = domains; 439 g_graph = domains;
434 g_graph_length = length; 440 g_graph_length = length;
435 } 441 }
436 442
437 } // namespace registry_controlled_domains 443 } // namespace registry_controlled_domains
438 } // namespace net 444 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | url/gurl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698