Bug 1623222 - Remove the charset parameter from nsITextToSubURI::UnEscapeURIForUI. r=hsivonen

Only 3 callers are using a non-UTF-8 charset as the first parameter.
* MediaDocument.cpp: This does not make sense because the "filename" part of
  URLs will always be encoded with UTF-8.
* nsContextMenu.js: This is wrong because "mailto:" URLs don't care about the
  document charset.
* Finder.jsm: This caused bug 1623222.

Differential Revision: https://phabricator.services.mozilla.com/D67386
This commit is contained in:
Masatoshi Kimura
2020-03-19 10:45:28 +00:00
parent f4007c7525
commit 7355e130aa
16 changed files with 21 additions and 54 deletions

View File

@@ -5018,7 +5018,7 @@ var XULBrowserWindow = {
setOverLink(url) {
if (url) {
url = Services.textToSubURI.unEscapeURIForUI("UTF-8", url);
url = Services.textToSubURI.unEscapeURIForUI(url);
// Encode bidirectional formatting characters.
// (RFC 3987 sections 3.2 and 4.1 paragraph 6)

View File

@@ -1686,10 +1686,7 @@ class nsContextMenu {
// Let's try to unescape it using a character set
// in case the address is not ASCII.
try {
addresses = Services.textToSubURI.unEscapeURIForUI(
this.contentData.charSet,
addresses
);
addresses = Services.textToSubURI.unEscapeURIForUI(addresses);
} catch (ex) {
// Do nothing.
}

View File

@@ -223,10 +223,7 @@ function makeUrlbarResult(tokens, info) {
if (!title) {
// If the url doesn't have an host (e.g. javascript urls), comment
// will be empty, and we can't build the usual title. Thus use the url.
title = Services.textToSubURI.unEscapeURIForUI(
"UTF-8",
action.params.url
);
title = Services.textToSubURI.unEscapeURIForUI(action.params.url);
} else if (tokens && tokens.length > 1) {
title = UrlbarUtils.strings.formatStringFromName(
"bookmarkKeywordSearch",

View File

@@ -213,10 +213,7 @@ class UrlbarResult {
url = BrowserUtils.trimURL(url);
}
}
payloadInfo.displayUrl[0] = Services.textToSubURI.unEscapeURIForUI(
"UTF-8",
url
);
payloadInfo.displayUrl[0] = Services.textToSubURI.unEscapeURIForUI(url);
}
// For performance reasons limit excessive string lengths, to reduce the

View File

@@ -3939,8 +3939,7 @@ nsDocShell::DisplayLoadError(nsresult aError, nsIURI* aURI,
nsCOMPtr<nsITextToSubURI> textToSubURI(
do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv));
if (NS_SUCCEEDED(rv)) {
rv = textToSubURI->UnEscapeURIForUI(NS_LITERAL_CSTRING("UTF-8"), spec,
nextFormatStr);
rv = textToSubURI->UnEscapeURIForUI(spec, nextFormatStr);
}
} else {
spec.Assign('?');

View File

@@ -258,15 +258,12 @@ void MediaDocument::GetFileName(nsAString& aResult, nsIChannel* aChannel) {
url->GetFileName(fileName);
if (fileName.IsEmpty()) return;
nsAutoCString docCharset;
// Now that the charset is set in |StartDocumentLoad| to the charset of
// the document viewer instead of a bogus value ("windows-1252" set in
// |Document|'s ctor), the priority is given to the current charset.
// This is necessary to deal with a media document being opened in a new
// window or a new tab.
if (mCharacterSetSource != kCharsetUninitialized) {
mCharacterSet->Name(docCharset);
} else {
if (mCharacterSetSource == kCharsetUninitialized) {
// resort to UTF-8
SetDocumentCharacterSet(UTF_8_ENCODING);
}
@@ -276,7 +273,7 @@ void MediaDocument::GetFileName(nsAString& aResult, nsIChannel* aChannel) {
do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv)) {
// UnEscapeURIForUI always succeeds
textToSubURI->UnEscapeURIForUI(docCharset, fileName, aResult);
textToSubURI->UnEscapeURIForUI(fileName, aResult);
} else {
CopyUTF8toUTF16(fileName, aResult);
}

View File

@@ -24,18 +24,17 @@ interface nsITextToSubURI : nsISupports
* <ul>
* <li> escaping back the result (unescaped string) is not guaranteed to
* give the original escaped string
* <li> In case of a conversion error, the URI fragment (escaped) is
* assumed to be in UTF-8 and converted to AString (UTF-16)
* <li> The URI fragment (escaped) is assumed to be in UTF-8 and converted
* to AString (UTF-16)
* <li> In case of successful conversion any resulting character listed
* in netwerk/dns/IDNCharacterBlocklist.inc (except space) is escaped
* <li> Always succeeeds (callers don't need to do error checking)
* </ul>
*
* @param aCharset the charset to convert from
* @param aURIFragment the URI (or URI fragment) to unescape
* @return Unescaped aURIFragment converted to unicode
*/
AString unEscapeURIForUI(in ACString aCharset, in AUTF8String aURIFragment);
AString unEscapeURIForUI(in AUTF8String aURIFragment);
/**
* Unescapes only non ASCII characters in the given URI fragment

View File

@@ -101,8 +101,7 @@ nsresult nsTextToSubURI::convertURItoUnicode(const nsCString& aCharset,
return encoding->DecodeWithoutBOMHandlingAndWithoutReplacement(aURI, aOut);
}
NS_IMETHODIMP nsTextToSubURI::UnEscapeURIForUI(const nsACString& aCharset,
const nsACString& aURIFragment,
NS_IMETHODIMP nsTextToSubURI::UnEscapeURIForUI(const nsACString& aURIFragment,
nsAString& _retval) {
nsAutoCString unescapedSpec;
// skip control octets (0x00 - 0x1f and 0x7f) when unescaping
@@ -112,7 +111,7 @@ NS_IMETHODIMP nsTextToSubURI::UnEscapeURIForUI(const nsACString& aCharset,
// in case of failure, return escaped URI
// Test for != NS_OK rather than NS_FAILED, because incomplete multi-byte
// sequences are also considered failure in this context
if (convertURItoUnicode(PromiseFlatCString(aCharset), unescapedSpec,
if (convertURItoUnicode(NS_LITERAL_CSTRING("UTF-8"), unescapedSpec,
_retval) != NS_OK) {
// assume UTF-8 instead of ASCII because hostname (IDN) may be in UTF-8
CopyUTF8toUTF16(aURIFragment, _retval);

View File

@@ -1289,8 +1289,7 @@ void nsPrintJob::GetDisplayTitleAndURL(Document& aDoc,
return;
}
textToSubURI->UnEscapeURIForUI(NS_LITERAL_CSTRING("UTF-8"), urlCStr,
aURLStr);
textToSubURI->UnEscapeURIForUI(urlCStr, aURLStr);
}
}

View File

@@ -2684,8 +2684,7 @@ nsresult NS_GetFilenameFromDisposition(nsAString& aFilename,
do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv)) {
nsAutoString unescaped;
textToSubURI->UnEscapeURIForUI(NS_LITERAL_CSTRING("UTF-8"),
NS_ConvertUTF16toUTF8(aFilename),
textToSubURI->UnEscapeURIForUI(NS_ConvertUTF16toUTF8(aFilename),
unescaped);
aFilename.Assign(unescaped);
}

View File

@@ -689,7 +689,7 @@ this.downloads = class extends ExtensionAPI {
let uri = Services.io.newURI(options.url);
if (uri instanceof Ci.nsIURL) {
filename = DownloadPaths.sanitize(
Services.textToSubURI.unEscapeURIForUI("UTF-8", uri.fileName)
Services.textToSubURI.unEscapeURIForUI(uri.fileName)
);
}
}

View File

@@ -661,7 +661,6 @@ function Search(
this._originalSearchString = searchString;
this._trimmedOriginalSearchString = searchString.trim();
let unescapedSearchString = Services.textToSubURI.unEscapeURIForUI(
"UTF-8",
this._trimmedOriginalSearchString
);
let [prefix, suffix] = stripPrefix(unescapedSearchString);
@@ -2050,10 +2049,7 @@ Search.prototype = {
// to be displayed to the user, and in any case the front-end should not
// rely on it being canonical.
let escapedURL = uri.displaySpec;
let displayURL = Services.textToSubURI.unEscapeURIForUI(
"UTF-8",
escapedURL
);
let displayURL = Services.textToSubURI.unEscapeURIForUI(escapedURL);
let value = makeActionUrl("visiturl", {
url: escapedURL,

View File

@@ -1199,9 +1199,7 @@ function getDefaultFileName(
} catch (e) {}
}
if (fileName) {
return validateFileName(
Services.textToSubURI.unEscapeURIForUI("UTF-8", fileName)
);
return validateFileName(Services.textToSubURI.unEscapeURIForUI(fileName));
}
}
@@ -1227,7 +1225,7 @@ function getDefaultFileName(
if (url.fileName != "") {
// 3) Use the actual file name, if present
return validateFileName(
Services.textToSubURI.unEscapeURIForUI("UTF-8", url.fileName)
Services.textToSubURI.unEscapeURIForUI(url.fileName)
);
}
} catch (e) {

View File

@@ -378,7 +378,7 @@
}
_unescapeUrl(url) {
return Services.textToSubURI.unEscapeURIForUI("UTF-8", url);
return Services.textToSubURI.unEscapeURIForUI(url);
}
_reuseAcItem() {

View File

@@ -113,16 +113,7 @@ Finder.prototype = {
let foundLink = this._fastFind.foundLink;
let linkURL = null;
if (foundLink) {
let docCharset = null;
let ownerDoc = foundLink.ownerDocument;
if (ownerDoc) {
docCharset = ownerDoc.characterSet;
}
linkURL = Services.textToSubURI.unEscapeURIForUI(
docCharset,
foundLink.href
);
linkURL = Services.textToSubURI.unEscapeURIForUI(foundLink.href);
}
options.linkURL = linkURL;

View File

@@ -149,8 +149,7 @@ static nsresult UnescapeFragment(const nsACString& aFragment, nsIURI* aURI,
do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
return textToSubURI->UnEscapeURIForUI(NS_LITERAL_CSTRING("UTF-8"), aFragment,
aResult);
return textToSubURI->UnEscapeURIForUI(aFragment, aResult);
}
/**