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

Side by Side Diff: components/url_formatter/url_formatter.cc

Issue 2939423003: URL Formatter: Add StripSubdomain method that preserves eTLD + 1. (Closed)
Patch Set: fix Created 3 years, 6 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/url_formatter/url_formatter.h" 5 #include "components/url_formatter/url_formatter.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/lazy_instance.h" 11 #include "base/lazy_instance.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/numerics/safe_conversions.h" 13 #include "base/numerics/safe_conversions.h"
14 #include "base/strings/string_piece.h" 14 #include "base/strings/string_piece.h"
15 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_offset_string_conversions.h" 16 #include "base/strings/utf_offset_string_conversions.h"
17 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
18 #include "base/threading/thread_local_storage.h" 18 #include "base/threading/thread_local_storage.h"
19 #include "components/url_formatter/idn_spoof_checker.h" 19 #include "components/url_formatter/idn_spoof_checker.h"
20 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
20 #include "third_party/icu/source/common/unicode/uidna.h" 21 #include "third_party/icu/source/common/unicode/uidna.h"
21 #include "third_party/icu/source/common/unicode/utypes.h" 22 #include "third_party/icu/source/common/unicode/utypes.h"
22 #include "url/gurl.h" 23 #include "url/gurl.h"
23 #include "url/third_party/mozilla/url_parse.h" 24 #include "url/third_party/mozilla/url_parse.h"
24 25
25 namespace url_formatter { 26 namespace url_formatter {
26 27
27 namespace { 28 namespace {
28 29
29 base::string16 IDNToUnicodeWithAdjustments( 30 base::string16 IDNToUnicodeWithAdjustments(
(...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 base::string16 IDNToUnicode(base::StringPiece host) { 572 base::string16 IDNToUnicode(base::StringPiece host) {
572 return IDNToUnicodeWithAdjustments(host, nullptr); 573 return IDNToUnicodeWithAdjustments(host, nullptr);
573 } 574 }
574 575
575 base::string16 StripWWW(const base::string16& text) { 576 base::string16 StripWWW(const base::string16& text) {
576 const base::string16 www(base::ASCIIToUTF16("www.")); 577 const base::string16 www(base::ASCIIToUTF16("www."));
577 return base::StartsWith(text, www, base::CompareCase::SENSITIVE) 578 return base::StartsWith(text, www, base::CompareCase::SENSITIVE)
578 ? text.substr(www.length()) : text; 579 ? text.substr(www.length()) : text;
579 } 580 }
580 581
581 base::string16 StripWWWFromHost(const GURL& url) { 582 base::StringPiece StripSubdomains(const GURL& url,
583 StripSubdomainTypes subdomains) {
582 DCHECK(url.is_valid()); 584 DCHECK(url.is_valid());
583 return StripWWW(base::ASCIIToUTF16(url.host_piece())); 585
586 base::StringPiece host_piece = url.host_piece();
587 std::string etld_plus_one =
Peter Kasting 2017/06/17 02:46:55 Nit: Prefer to use the same terminology as the RCD
tommycli 2017/06/19 23:03:07 Done.
588 net::registry_controlled_domains::GetDomainAndRegistry(
589 host_piece,
590 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
591
592 if (host_piece == etld_plus_one)
593 return host_piece;
594
595 if ((subdomains & kStripWWW) != 0 &&
596 base::StartsWith(host_piece, "www.", base::CompareCase::SENSITIVE)) {
597 return host_piece.substr(4);
Peter Kasting 2017/06/17 02:46:55 This early-returns if the host is www.m.foo.com, w
tommycli 2017/06/19 23:03:08 Done.
598 }
599
600 if ((subdomains & kStripM) != 0 &&
601 base::StartsWith(host_piece, "m.", base::CompareCase::SENSITIVE)) {
Peter Kasting 2017/06/17 02:46:55 This fails to fix up the most common mobile host I
tommycli 2017/06/19 23:03:07 Done.
602 return host_piece.substr(2);
603 }
604
605 return host_piece;
584 } 606 }
585 607
586 } // namespace url_formatter 608 } // namespace url_formatter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698