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

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..752fd1efb876e2dfa48b903d8a065db7e34e80a1 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 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.
+ net::registry_controlled_domains::GetDomainAndRegistry(
+ host_piece,
+ net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
+
+ if (host_piece == etld_plus_one)
+ return host_piece;
+
+ if ((subdomains & kStripWWW) != 0 &&
+ base::StartsWith(host_piece, "www.", base::CompareCase::SENSITIVE)) {
+ 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.
+ }
+
+ if ((subdomains & kStripM) != 0 &&
+ 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.
+ return host_piece.substr(2);
+ }
+
+ return host_piece;
}
} // namespace url_formatter

Powered by Google App Engine
This is Rietveld 408576698