Chromium Code Reviews| Index: net/base/net_util_icu.cc |
| diff --git a/net/base/net_util_icu.cc b/net/base/net_util_icu.cc |
| index 4094feeff74a1313e900940ea4d6648c42f0e2a2..650d68d0e032146f2478516da6c3fe1da594b5fd 100644 |
| --- a/net/base/net_util_icu.cc |
| +++ b/net/base/net_util_icu.cc |
| @@ -574,6 +574,8 @@ const FormatUrlType kFormatUrlOmitNothing = 0; |
| const FormatUrlType kFormatUrlOmitUsernamePassword = 1 << 0; |
| const FormatUrlType kFormatUrlOmitHTTP = 1 << 1; |
| const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname = 1 << 2; |
| +const FormatUrlType kFormatUrlOmitScheme = 1 << 3; |
| +const FormatUrlType kFormatUrlOmitPort = 1 << 4; |
| const FormatUrlType kFormatUrlOmitAll = kFormatUrlOmitUsernamePassword | |
| kFormatUrlOmitHTTP | kFormatUrlOmitTrailingSlashOnBareHostname; |
| @@ -686,21 +688,11 @@ base::string16 FormatUrlWithAdjustments( |
| const url::Parsed& parsed = url.parsed_for_possibly_invalid_spec(); |
| // Scheme & separators. These are ASCII. |
| + // Scheme removal occurs at the end. |
| base::string16 url_string; |
| url_string.insert( |
| url_string.end(), spec.begin(), |
| spec.begin() + parsed.CountCharactersBefore(url::Parsed::USERNAME, true)); |
| - const char kHTTP[] = "http://"; |
| - const char kFTP[] = "ftp."; |
| - // url_fixer::FixupURL() treats "ftp.foo.com" as ftp://ftp.foo.com. This |
| - // means that if we trim "http://" off a URL whose host starts with "ftp." and |
| - // the user inputs this into any field subject to fixup (which is basically |
| - // all input fields), the meaning would be changed. (In fact, often the |
| - // formatted URL is directly pre-filled into an input field.) For this reason |
| - // we avoid stripping "http://" in this case. |
| - bool omit_http = (format_types & kFormatUrlOmitHTTP) && |
| - EqualsASCII(url_string, kHTTP) && |
| - !StartsWithASCII(url.host(), kFTP, true); |
| new_parsed->scheme = parsed.scheme; |
| // Username & password. |
| @@ -750,7 +742,7 @@ base::string16 FormatUrlWithAdjustments( |
| &url_string, &new_parsed->host, adjustments); |
| // Port. |
| - if (parsed.port.is_nonempty()) { |
| + if (parsed.port.is_nonempty() && !(format_types & kFormatUrlOmitPort)) { |
| url_string.push_back(':'); |
| new_parsed->port.begin = url_string.length(); |
| url_string.insert(url_string.end(), |
| @@ -786,25 +778,34 @@ base::string16 FormatUrlWithAdjustments( |
| NonHostComponentTransform(UnescapeRule::NONE), |
| &url_string, &new_parsed->ref, adjustments); |
| - // If we need to strip out http do it after the fact. |
| - if (omit_http && StartsWith(url_string, base::ASCIIToUTF16(kHTTP), true)) { |
| - const size_t kHTTPSize = arraysize(kHTTP) - 1; |
| - url_string = url_string.substr(kHTTPSize); |
| + // Strip out the scheme, after the fact. |
| + bool omit_all_schemes = format_types & kFormatUrlOmitScheme; |
| + // url_fixer::FixupURL() treats "ftp.foo.com" as ftp://ftp.foo.com. This |
| + // means that if we trim "http://" off a URL whose host starts with "ftp." and |
| + // the user inputs this into any field subject to fixup (which is basically |
| + // all input fields), the meaning would be changed. (In fact, often the |
| + // formatted URL is directly pre-filled into an input field.) For this reason |
| + // we avoid stripping "http://" in this case. |
| + bool omit_http = (format_types & kFormatUrlOmitHTTP) && |
| + url.SchemeIs("http") && !StartsWithASCII(url.host(), "ftp.", true); |
| + if (omit_all_schemes || omit_http) { |
| + const int scheme_size = |
| + parsed.CountCharactersBefore(url::Parsed::USERNAME, true); |
| + url_string = url_string.substr(scheme_size); |
| // Because offsets in the |adjustments| are already calculated with respect |
| - // to the string with the http:// prefix in it, those offsets remain correct |
| + // to the string with the scheme prefix in it, those offsets remain correct |
| // after stripping the prefix. The only thing necessary is to add an |
| // adjustment to reflect the stripped prefix. |
| adjustments->insert(adjustments->begin(), |
| - base::OffsetAdjuster::Adjustment(0, kHTTPSize, 0)); |
| + base::OffsetAdjuster::Adjustment(0, scheme_size, 0)); |
| if (prefix_end) |
| - *prefix_end -= kHTTPSize; |
| + *prefix_end -= scheme_size; |
| - // Adjust new_parsed. |
| DCHECK(new_parsed->scheme.is_valid()); |
| - int delta = -(new_parsed->scheme.len + 3); // +3 for ://. |
| + DCHECK_EQ(scheme_size, new_parsed->scheme.len + 3); |
| new_parsed->scheme.reset(); |
| - AdjustAllComponentsButScheme(delta, new_parsed); |
| + AdjustAllComponentsButScheme(-scheme_size, new_parsed); |
| } |
| return url_string; |
| @@ -827,4 +828,27 @@ base::string16 FormatUrl(const GURL& url, |
| return result; |
| } |
| +base::string16 FormatOriginForDisplay(const GURL& url, |
| + const std::string& languages, |
| + bool always_show_scheme) { |
| + if (url.SchemeIsFile() || url.SchemeIsFileSystem() || !url.IsStandard()) |
| + return FormatUrl(url, languages); |
| + GURL display_origin = url.GetOrigin(); |
| + |
| + FormatUrlTypes format_types = kFormatUrlOmitUsernamePassword | |
| + kFormatUrlOmitTrailingSlashOnBareHostname; |
| + if (!always_show_scheme && display_origin.SchemeIsSecure()) |
| + format_types |= kFormatUrlOmitScheme; |
| + |
| + if ((display_origin.SchemeIs("http") && display_origin.port() == "80") || |
| + (display_origin.SchemeIs("https") && display_origin.port() == "443")) |
|
Ryan Sleevi
2014/09/02 19:26:17
We already have a //net concept of "default port f
|
| + format_types |= kFormatUrlOmitPort; |
| + |
| + return FormatUrl(display_origin, |
| + languages, |
| + format_types, |
| + UnescapeRule::SPACES, |
| + NULL, NULL, NULL); |
| +} |
| + |
| } // namespace net |