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

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

Issue 2939423003: URL Formatter: Add StripSubdomain method that preserves eTLD + 1. (Closed)
Patch Set: Return std::string instead of base::StringPiece (which makes no sense) 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 std::string StripSubdomains(const GURL& url, StripSubdomainTypes subdomains) {
582 DCHECK(url.is_valid()); 583 DCHECK(url.is_valid());
583 return StripWWW(base::ASCIIToUTF16(url.host_piece())); 584
585 base::StringPiece host_piece = url.host_piece();
586 std::string domain_and_registry =
587 net::registry_controlled_domains::GetDomainAndRegistry(
588 host_piece,
589 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
590
591 if (host_piece == domain_and_registry)
592 return host_piece.as_string();
593
594 std::string subdomain_piece =
595 host_piece.substr(0, host_piece.length() - domain_and_registry.length())
596 .as_string();
597
598 if (subdomains & kStripWWW)
599 base::ReplaceSubstringsAfterOffset(&subdomain_piece, 0, "www.", "");
600
601 if (subdomains & kStripM)
602 base::ReplaceSubstringsAfterOffset(&subdomain_piece, 0, "m.", "");
603
604 return subdomain_piece + domain_and_registry;
584 } 605 }
585 606
586 } // namespace url_formatter 607 } // namespace url_formatter
OLDNEW
« no previous file with comments | « components/url_formatter/url_formatter.h ('k') | components/url_formatter/url_formatter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698