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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/url_formatter/url_formatter.cc
diff --git a/components/url_formatter/url_formatter.cc b/components/url_formatter/url_formatter.cc
index 298b1c18ff42bfae9102433a16200dd4a65d642b..ee32aaa893ccdb49db2de16fc3cb7ca81f6b1392 100644
--- a/components/url_formatter/url_formatter.cc
+++ b/components/url_formatter/url_formatter.cc
@@ -17,6 +17,7 @@
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_local_storage.h"
#include "components/url_formatter/idn_spoof_checker.h"
+#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "third_party/icu/source/common/unicode/uidna.h"
#include "third_party/icu/source/common/unicode/utypes.h"
#include "url/gurl.h"
@@ -578,9 +579,29 @@ base::string16 StripWWW(const base::string16& text) {
? text.substr(www.length()) : text;
}
-base::string16 StripWWWFromHost(const GURL& url) {
+std::string StripSubdomains(const GURL& url, StripSubdomainTypes subdomains) {
DCHECK(url.is_valid());
- return StripWWW(base::ASCIIToUTF16(url.host_piece()));
+
+ base::StringPiece host_piece = url.host_piece();
+ std::string domain_and_registry =
+ net::registry_controlled_domains::GetDomainAndRegistry(
+ host_piece,
+ net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
+
+ if (host_piece == domain_and_registry)
+ return host_piece.as_string();
+
+ std::string subdomain_piece =
+ host_piece.substr(0, host_piece.length() - domain_and_registry.length())
+ .as_string();
+
+ if (subdomains & kStripWWW)
+ base::ReplaceSubstringsAfterOffset(&subdomain_piece, 0, "www.", "");
+
+ if (subdomains & kStripM)
+ base::ReplaceSubstringsAfterOffset(&subdomain_piece, 0, "m.", "");
+
+ return subdomain_piece + domain_and_registry;
}
} // namespace url_formatter
« 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