diff options
author | palmer <palmer@chromium.org> | 2015-05-15 17:53:03 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-05-16 00:53:51 +0000 |
commit | f39e5aff5aae8451a7ef190a2fc822bf80c4e30d (patch) | |
tree | 4e9c638513f026c47a3836e77acd5b1582162561 /net/base/net_util_icu.cc | |
parent | 68208a4cb9def359c3054f0769853f31602968bf (diff) | |
download | chromium_src-f39e5aff5aae8451a7ef190a2fc822bf80c4e30d.zip chromium_src-f39e5aff5aae8451a7ef190a2fc822bf80c4e30d.tar.gz chromium_src-f39e5aff5aae8451a7ef190a2fc822bf80c4e30d.tar.bz2 |
Create net::FormatOriginForDisplay helper function.
net::FormatOriginForDisplay provides a canonical, concise, human-friendly
display for origins. It supports callers in any higher-layer module.
Based heavily on earlier work by dewittj and felt. Thanks!
BUG=402698
TBR=thakis,brettw
Review URL: https://codereview.chromium.org/1133943002
Cr-Commit-Position: refs/heads/master@{#330254}
Diffstat (limited to 'net/base/net_util_icu.cc')
-rw-r--r-- | net/base/net_util_icu.cc | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/net/base/net_util_icu.cc b/net/base/net_util_icu.cc index 94e1a0d..bdbe270 100644 --- a/net/base/net_util_icu.cc +++ b/net/base/net_util_icu.cc @@ -797,7 +797,7 @@ base::string16 FormatUrlWithAdjustments( // 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, kHTTPSize, 0)); if (prefix_end) *prefix_end -= kHTTPSize; @@ -829,4 +829,46 @@ 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:") + + FormatOriginForDisplay(inner_url, languages, omit_scheme); + } + + const GURL origin = url.GetOrigin(); + const std::string& scheme = origin.scheme(); + const std::string& host = origin.host(); + if (scheme.empty() || host.empty()) + return FormatUrl(url, languages); + + base::string16 result; + + if (!omit_scheme) + result = base::UTF8ToUTF16(scheme) + base::ASCIIToUTF16("://"); + + result += base::UTF8ToUTF16(host); + + const int port = origin.IntPort(); + const int default_port = url::DefaultPortForScheme(origin.scheme().c_str(), + origin.scheme().length()); + if (origin.port().length() > 0 && port != 0 && port != default_port) + result += base::ASCIIToUTF16(":") + base::UTF8ToUTF16(origin.port()); + + return result; +} + } // namespace net |