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..713275899f7d0f65217640a8629efe40fc54742a 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; |
felt
2015/05/13 22:50:14
nit: spaces -- should this line up with the others
palmer
2015/05/14 18:18:01
This is what "git cl format" gives me. (Sigh.) I'l
|
+const FormatUrlType kFormatUrlOmitPort = 1 << 4; |
const FormatUrlType kFormatUrlOmitAll = kFormatUrlOmitUsernamePassword | |
kFormatUrlOmitHTTP | kFormatUrlOmitTrailingSlashOnBareHostname; |
@@ -687,22 +689,11 @@ base::string16 FormatUrlWithAdjustments( |
const std::string& spec = url.possibly_invalid_spec(); |
const url::Parsed& parsed = url.parsed_for_possibly_invalid_spec(); |
- // Scheme & separators. These are ASCII. |
+ // 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. |
@@ -753,12 +744,18 @@ base::string16 FormatUrlWithAdjustments( |
// Port. |
if (parsed.port.is_nonempty()) { |
- url_string.push_back(':'); |
- new_parsed->port.begin = url_string.length(); |
- url_string.insert(url_string.end(), |
- spec.begin() + parsed.port.begin, |
- spec.begin() + parsed.port.end()); |
- new_parsed->port.len = url_string.length() - new_parsed->port.begin; |
+ if (!(format_types & kFormatUrlOmitPort)) { |
+ url_string.push_back(':'); |
+ new_parsed->port.begin = url_string.length(); |
+ url_string.insert(url_string.end(), spec.begin() + parsed.port.begin, |
+ spec.begin() + parsed.port.end()); |
+ new_parsed->port.len = url_string.length() - new_parsed->port.begin; |
+ } else { |
+ if (parsed.port.len > 0) { |
+ adjustments->push_back(base::OffsetAdjuster::Adjustment( |
+ parsed.port.begin, parsed.port.len, 0)); |
asanka
2015/05/14 03:42:47
We are also dropping the colon.
palmer
2015/05/14 18:18:01
I'm going to change this code; it's triggering a D
|
+ } |
+ } |
} else { |
new_parsed->port.reset(); |
} |
@@ -788,25 +785,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 +836,40 @@ 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()); |
+ } |
+ |
+ if (url.SchemeIsFileSystem()) { |
+ // TODO(palmer): Determine whether to encode this policy in GURL::GetOrigin. |
+ const GURL inner_url(url.spec().substr(strlen("filesystem:"))); |
+ return base::ASCIIToUTF16("filesystem:") + |
felt
2015/05/13 22:50:14
will this sometimes yield filesystem:url instead o
palmer
2015/05/14 18:10:05
It yields filesystem:url, as it should. (See tests
|
+ FormatOriginForDisplay(inner_url, languages, omit_scheme); |
+ } |
+ |
+ 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.IntPort() == default_port) |
+ format_types |= kFormatUrlOmitPort; |
+ |
+ return FormatUrl(display_origin, languages, format_types, |
+ UnescapeRule::SPACES, nullptr, nullptr, nullptr); |
+} |
+ |
} // namespace net |