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 94e1a0d7232a65a7c517e714fa128fc571b47aa2..cbcbbbc34717a75dbba43a1788addd0df74e65cd 100644 |
| --- a/net/base/net_util_icu.cc |
| +++ b/net/base/net_util_icu.cc |
| @@ -576,6 +576,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; |
| @@ -688,21 +690,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. |
| @@ -752,7 +744,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(), |
| @@ -788,25 +780,35 @@ 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; |
| @@ -829,4 +831,45 @@ base::string16 FormatUrl(const GURL& url, |
| return result; |
| } |
| +base::string16 FormatOriginForDisplay(const GURL& url, |
| + const std::string& languages, |
| + bool omit_scheme) { |
| + if (!url.IsStandard()) |
| + return FormatUrl(url, languages); |
| + |
| + if (url.SchemeIsFile()) { |
| + // TODO(palmer): Determine whether to encode this policy in GURL::GetOrigin. |
| + return (omit_scheme ? base::ASCIIToUTF16("") |
| + : base::ASCIIToUTF16("file://")) + |
| + base::UTF8ToUTF16(url.path()); |
|
asanka
2015/05/11 23:39:44
It is (or should be) possible for url.host() to be
palmer
2015/05/12 22:20:53
For file: URLs, the path is the origin, so that's
|
| + } |
| + |
| + if (url.SchemeIsFileSystem()) { |
| + // TODO(palmer): Determine whether to encode this policy in GURL::GetOrigin. |
| + // |
| + // TODO(palmer): Determine whether GURL::IsStandard should return false for |
|
asanka
2015/05/11 23:39:44
This is a good point. You should file a bug for it
palmer
2015/05/12 22:20:53
Done.
|
| + // filesystem: URLs. Per |
| + // http://www.html5rocks.com/en/tutorials/file/filesystem/, they are not |
| + // standard. |
| + const GURL* inner_url = url.inner_url(); |
| + return base::ASCIIToUTF16("filesystem:") + |
| + FormatOriginForDisplay(*inner_url, languages, omit_scheme); |
|
asanka
2015/05/11 23:39:44
Perhaps add a comment that this method deals corre
palmer
2015/05/12 22:20:53
Yeah, wow. Either GURL::inner_url is broken, or my
|
| + } |
| + |
| + GURL display_origin = url.GetOrigin(); |
| + |
| + FormatUrlTypes format_types = kFormatUrlOmitUsernamePassword | |
| + kFormatUrlOmitTrailingSlashOnBareHostname; |
| + if (omit_scheme) |
| + format_types |= kFormatUrlOmitScheme; |
| + |
| + const int default_port = url::DefaultPortForScheme( |
| + display_origin.scheme().c_str(), display_origin.scheme().length()); |
| + if (display_origin.EffectiveIntPort() == default_port) |
|
asanka
2015/05/11 23:39:44
Nit: use IntPort().
palmer
2015/05/12 22:20:53
Done.
|
| + format_types |= kFormatUrlOmitPort; |
| + |
| + return FormatUrl(display_origin, languages, format_types, |
| + UnescapeRule::SPACES, NULL, NULL, NULL); |
|
asanka
2015/05/11 23:39:44
NULL -> nullptr
palmer
2015/05/12 22:20:53
Done.
|
| +} |
| + |
| } // namespace net |