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

Unified 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 side-by-side diff with in-line comments
Download patch
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..4413eb0392c59733a0d13d42443ffedb0e97c277 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,30 @@ base::string16 StripWWW(const base::string16& text) {
? text.substr(www.length()) : text;
}
-base::string16 StripWWWFromHost(const GURL& url) {
+base::StringPiece 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;
+
+ std::string subdomain_piece =
+ host_piece.substr(0, host_piece.length() - domain_and_registry.length())
+ .as_string();
+
+ if ((subdomains & kStripWWW) != 0)
Peter Kasting 2017/06/19 23:42:32 Nit: Personally I find omitting the "!= 0" more re
tommycli 2017/06/19 23:48:51 Done. Cool. Me too. I was just following precedent
+ base::ReplaceSubstringsAfterOffset(&subdomain_piece, 0, "www.", "");
+
+ if ((subdomains & kStripM) != 0)
+ base::ReplaceSubstringsAfterOffset(&subdomain_piece, 0, "m.", "");
+
+ return subdomain_piece + domain_and_registry;
}
} // namespace url_formatter

Powered by Google App Engine
This is Rietveld 408576698