Bug 846635 - Use asynchronous getCharsetForURI in getShortcutOrURI

Issue #121
This commit is contained in:
janekptacijarabaci 2018-04-15 09:31:07 +02:00 • committed by Roy Tam
commit d8637395c5
4 changed files with 305 additions and 260 deletions

View file

@ -10,6 +10,9 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm"); Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource:///modules/RecentWindow.jsm"); Cu.import("resource:///modules/RecentWindow.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Task",
"resource://gre/modules/Task.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "CharsetMenu", XPCOMUtils.defineLazyModuleGetter(this, "CharsetMenu",
"resource:///modules/CharsetMenu.jsm"); "resource:///modules/CharsetMenu.jsm");
@ -1895,88 +1898,89 @@ function loadURI(uri, referrer, postData, allowThirdPartyFixup) {
} catch (e) {} } catch (e) {}
} }
function getShortcutOrURI(aURL, aPostDataRef, aMayInheritPrincipal) { function getShortcutOrURIAndPostData(aURL) {
// Initialize outparam to false return Task.spawn(function() {
if (aMayInheritPrincipal) let mayInheritPrincipal = false;
aMayInheritPrincipal.value = false; let postData = null;
let shortcutURL = null;
let keyword = aURL;
let param = "";
var shortcutURL = null; let offset = aURL.indexOf(" ");
var keyword = aURL; if (offset > 0) {
var param = ""; keyword = aURL.substr(0, offset);
param = aURL.substr(offset + 1);
var offset = aURL.indexOf(" ");
if (offset > 0) {
keyword = aURL.substr(0, offset);
param = aURL.substr(offset + 1);
}
if (!aPostDataRef)
aPostDataRef = {};
var engine = Services.search.getEngineByAlias(keyword);
if (engine) {
var submission = engine.getSubmission(param);
aPostDataRef.value = submission.postData;
return submission.uri.spec;
}
[shortcutURL, aPostDataRef.value] =
PlacesUtils.getURLAndPostDataForKeyword(keyword);
if (!shortcutURL)
return aURL;
var postData = "";
if (aPostDataRef.value)
postData = unescape(aPostDataRef.value);
if (/%s/i.test(shortcutURL) || /%s/i.test(postData)) {
var charset = "";
const re = /^(.*)\&mozcharset=([a-zA-Z][_\-a-zA-Z0-9]+)\s*$/;
var matches = shortcutURL.match(re);
if (matches)
[, shortcutURL, charset] = matches;
else {
// Try to get the saved character-set.
try {
// makeURI throws if URI is invalid.
// Will return an empty string if character-set is not found.
charset = PlacesUtils.history.getCharsetForURI(makeURI(shortcutURL));
} catch (e) {}
} }
// encodeURIComponent produces UTF-8, and cannot be used for other charsets. let engine = Services.search.getEngineByAlias(keyword);
// escape() works in those cases, but it doesn't uri-encode +, @, and /. if (engine) {
// Therefore we need to manually replace these ASCII characters by their let submission = engine.getSubmission(param);
// encodeURIComponent result, to match the behavior of nsEscape() with postData = submission.postData;
// url_XPAlphas throw new Task.Result({ postData: submission.postData,
var encodedParam = ""; url: submission.uri.spec,
if (charset && charset != "UTF-8") mayInheritPrincipal: mayInheritPrincipal });
encodedParam = escape(convertFromUnicode(charset, param)). }
replace(/[+@\/]+/g, encodeURIComponent);
else // Default charset is UTF-8
encodedParam = encodeURIComponent(param);
shortcutURL = shortcutURL.replace(/%s/g, encodedParam).replace(/%S/g, param); [shortcutURL, postData] =
PlacesUtils.getURLAndPostDataForKeyword(keyword);
if (/%s/i.test(postData)) // POST keyword if (!shortcutURL)
aPostDataRef.value = getPostDataStream(postData, param, encodedParam, throw new Task.Result({ postData: postData, url: aURL,
"application/x-www-form-urlencoded"); mayInheritPrincipal: mayInheritPrincipal });
}
else if (param) {
// This keyword doesn't take a parameter, but one was provided. Just return
// the original URL.
aPostDataRef.value = null;
return aURL; let escapedPostData = "";
} if (postData)
escapedPostData = unescape(postData);
// This URL came from a bookmark, so it's safe to let it inherit the current if (/%s/i.test(shortcutURL) || /%s/i.test(escapedPostData)) {
// document's principal. let charset = "";
if (aMayInheritPrincipal) const re = /^(.*)\&mozcharset=([a-zA-Z][_\-a-zA-Z0-9]+)\s*$/;
aMayInheritPrincipal.value = true; let matches = shortcutURL.match(re);
if (matches)
[, shortcutURL, charset] = matches;
else {
// Try to get the saved character-set.
try {
// makeURI throws if URI is invalid.
// Will return an empty string if character-set is not found.
charset = yield PlacesUtils.getCharsetForURI(makeURI(shortcutURL));
} catch (e) {}
}
return shortcutURL; // encodeURIComponent produces UTF-8, and cannot be used for other charsets.
// escape() works in those cases, but it doesn't uri-encode +, @, and /.
// Therefore we need to manually replace these ASCII characters by their
// encodeURIComponent result, to match the behavior of nsEscape() with
// url_XPAlphas
let encodedParam = "";
if (charset && charset != "UTF-8")
encodedParam = escape(convertFromUnicode(charset, param)).
replace(/[+@\/]+/g, encodeURIComponent);
else // Default charset is UTF-8
encodedParam = encodeURIComponent(param);
shortcutURL = shortcutURL.replace(/%s/g, encodedParam).replace(/%S/g, param);
if (/%s/i.test(escapedPostData)) // POST keyword
postData = getPostDataStream(escapedPostData, param, encodedParam,
"application/x-www-form-urlencoded");
}
else if (param) {
// This keyword doesn't take a parameter, but one was provided. Just return
// the original URL.
postData = null;
throw new Task.Result({ postData: postData, url: aURL,
mayInheritPrincipal: mayInheritPrincipal });
}
// This URL came from a bookmark, so it's safe to let it inherit the current
// document's principal.
mayInheritPrincipal = true;
throw new Task.Result({ postData: postData, url: shortcutURL,
mayInheritPrincipal: mayInheritPrincipal });
});
} }
function getPostDataStream(aStringData, aKeyword, aEncKeyword, aType) { function getPostDataStream(aStringData, aKeyword, aEncKeyword, aType) {
@ -2721,12 +2725,13 @@ var newTabButtonObserver = {
onDrop: function (aEvent) onDrop: function (aEvent)
{ {
let url = browserDragAndDrop.drop(aEvent, { }); let url = browserDragAndDrop.drop(aEvent, { });
var postData = {}; Task.spawn(function() {
url = getShortcutOrURI(url, postData); let data = yield getShortcutOrURIAndPostData(url);
if (url) { if (data.url) {
// allow third-party services to fixup this URL // allow third-party services to fixup this URL
openNewTabWith(url, null, postData.value, aEvent, true); openNewTabWith(data.url, null, data.postData, aEvent, true);
} }
});
} }
} }
@ -2741,12 +2746,13 @@ var newWindowButtonObserver = {
onDrop: function (aEvent) onDrop: function (aEvent)
{ {
let url = browserDragAndDrop.drop(aEvent, { }); let url = browserDragAndDrop.drop(aEvent, { });
var postData = {}; Task.spawn(function() {
url = getShortcutOrURI(url, postData); let data = yield getShortcutOrURIAndPostData(url);
if (url) { if (data.url) {
// allow third-party services to fixup this URL // allow third-party services to fixup this URL
openNewWindowWith(url, null, postData.value, true); openNewWindowWith(data.url, null, data.postData, true);
} }
});
} }
} }
@ -5034,36 +5040,52 @@ function middleMousePaste(event) {
// bar's behavior (stripsurroundingwhitespace) // bar's behavior (stripsurroundingwhitespace)
clipboard = clipboard.replace(/\s*\n\s*/g, ""); clipboard = clipboard.replace(/\s*\n\s*/g, "");
let mayInheritPrincipal = { value: false }; // if it's not the current tab, we don't need to do anything because the
let url = getShortcutOrURI(clipboard, mayInheritPrincipal); // browser doesn't exist.
try { let where = whereToOpenLink(event, true, false);
makeURI(url); let lastLocationChange;
} catch (ex) { if (where == "current") {
// Not a valid URI. lastLocationChange = gBrowser.selectedBrowser.lastLocationChange;
return;
} }
try { Task.spawn(function() {
addToUrlbarHistory(url); let data = yield getShortcutOrURIAndPostData(clipboard);
} catch (ex) { try {
// Things may go wrong when adding url to session history, makeURI(data.url);
// but don't let that interfere with the loading of the url. } catch (ex) {
Cu.reportError(ex); // Not a valid URI.
} return;
}
openUILink(url, event, try {
{ ignoreButton: true, addToUrlbarHistory(data.url);
disallowInheritPrincipal: !mayInheritPrincipal.value }); } catch (ex) {
// Things may go wrong when adding url to session history,
// but don't let that interfere with the loading of the url.
Cu.reportError(ex);
}
if (where != "current" ||
lastLocationChange == gBrowser.selectedBrowser.lastLocationChange) {
openUILink(data.url, event,
{ ignoreButton: true,
disallowInheritPrincipal: !data.mayInheritPrincipal });
}
});
event.stopPropagation(); event.stopPropagation();
} }
function handleDroppedLink(event, url, name) function handleDroppedLink(event, url, name)
{ {
let postData = { }; let lastLocationChange = gBrowser.selectedBrowser.lastLocationChange;
let uri = getShortcutOrURI(url, postData);
if (uri) Task.spawn(function() {
loadURI(uri, null, postData.value, false); let data = yield getShortcutOrURIAndPostData(url);
if (data.url &&
lastLocationChange == gBrowser.selectedBrowser.lastLocationChange)
loadURI(data.url, null, data.postData, false);
});
// Keep the event from being handled by the dragDrop listeners // Keep the event from being handled by the dragDrop listeners
// built-in to goanna if they happen to be above us. // built-in to goanna if they happen to be above us.
@ -5178,7 +5200,6 @@ function charsetLoadListener() {
} }
} }
var gPageStyleMenu = { var gPageStyleMenu = {
_getAllStyleSheets: function (frameset) { _getAllStyleSheets: function (frameset) {

View file

@ -16,7 +16,8 @@ try {
} }
Components.utils.import("resource:///modules/openLocationLastURL.jsm", openLocationModule); Components.utils.import("resource:///modules/openLocationLastURL.jsm", openLocationModule);
var gOpenLocationLastURL = new openLocationModule.OpenLocationLastURL(window.opener); Components.utils.import("resource://gre/modules/Task.jsm");
let gOpenLocationLastURL = new openLocationModule.OpenLocationLastURL(window.opener);
function onLoad() function onLoad()
{ {
@ -61,45 +62,52 @@ function doEnabling()
function open() function open()
{ {
var url; Task.spawn(function() {
var postData = {}; let url;
var mayInheritPrincipal = {value: false}; let postData = null;
if (browser) let mayInheritPrincipal = false;
url = browser.getShortcutOrURI(dialog.input.value, postData, mayInheritPrincipal);
else
url = dialog.input.value;
try { if (browser) {
// Whichever target we use for the load, we allow third-party services to let data = yield browser.getShortcutOrURIAndPostData(dialog.input.value);
// fixup the URI url = data.url;
switch (dialog.openWhereList.value) { postData = data.postData;
case "0": mayInheritPrincipal = data.mayInheritPrincipal;
var webNav = Components.interfaces.nsIWebNavigation; } else {
var flags = webNav.LOAD_FLAGS_ALLOW_THIRD_PARTY_FIXUP | url = dialog.input.value;
webNav.LOAD_FLAGS_FIXUP_SCHEME_TYPOS;
if (!mayInheritPrincipal.value)
flags |= webNav.LOAD_FLAGS_DISALLOW_INHERIT_OWNER;
browser.gBrowser.loadURIWithFlags(url, flags, null, null, postData.value);
break;
case "1":
window.opener.delayedOpenWindow(getBrowserURL(), "all,dialog=no",
url, postData.value, null, null, true);
break;
case "3":
browser.delayedOpenTab(url, null, null, postData.value, true);
break;
} }
}
catch(exception) {
}
if (pref) { try {
gOpenLocationLastURL.value = dialog.input.value; // Whichever target we use for the load, we allow third-party services to
pref.setIntPref("general.open_location.last_window_choice", dialog.openWhereList.value); // fixup the URI
} switch (dialog.openWhereList.value) {
case "0":
var webNav = Components.interfaces.nsIWebNavigation;
var flags = webNav.LOAD_FLAGS_ALLOW_THIRD_PARTY_FIXUP;
if (!mayInheritPrincipal)
flags |= webNav.LOAD_FLAGS_DISALLOW_INHERIT_OWNER;
browser.gBrowser.loadURIWithFlags(url, flags, null, null, postData);
break;
case "1":
window.opener.delayedOpenWindow(getBrowserURL(), "all,dialog=no",
url, postData, null, null, true);
break;
case "3":
browser.delayedOpenTab(url, null, null, postData, true);
break;
}
}
catch(exception) {
}
if (pref) {
gOpenLocationLastURL.value = dialog.input.value;
pref.setIntPref("general.open_location.last_window_choice", dialog.openWhereList.value);
}
// Delay closing slightly to avoid timing bug on Linux.
window.close();
});
// Delay closing slightly to avoid timing bug on Linux.
window.close();
return false; return false;
} }

View file

@ -738,8 +738,10 @@
aFlags]); aFlags]);
} }
if (topLevel) if (topLevel) {
this.mBrowser.lastURI = aLocation; this.mBrowser.lastURI = aLocation;
this.mBrowser.lastLocationChange = Date.now();
}
}, },
onStatusChange: function (aWebProgress, aRequest, aStatus, aMessage) { onStatusChange: function (aWebProgress, aRequest, aStatus, aMessage) {

View file

@ -263,151 +263,165 @@
var postData = null; var postData = null;
var action = this._parseActionUrl(url); var action = this._parseActionUrl(url);
if (action) { let lastLocationChange = gBrowser.selectedBrowser.lastLocationChange;
url = action.param; Task.spawn(function() {
if (this.hasAttribute("actiontype")) { let matchLastLocationChange = true;
if (action.type == "switchtab") { if (action) {
this.handleRevert(); url = action.param;
let prevTab = gBrowser.selectedTab; if (this.hasAttribute("actiontype")) {
if (switchToTabHavingURI(url) && if (action.type == "switchtab") {
isTabEmpty(prevTab)) this.handleRevert();
gBrowser.removeTab(prevTab); let prevTab = gBrowser.selectedTab;
if (switchToTabHavingURI(url) &&
isTabEmpty(prevTab))
gBrowser.removeTab(prevTab);
}
return;
} }
return;
} }
} else {
else { [url, postData, mayInheritPrincipal] = yield this._canonizeURL(aTriggeringEvent);
[url, postData, mayInheritPrincipal] = this._canonizeURL(aTriggeringEvent); matchLastLocationChange = (lastLocationChange ==
if (!url) gBrowser.selectedBrowser.lastLocationChange);
return; if (!url)
} return;
}
this.value = url; this.value = url;
gBrowser.userTypedValue = url; gBrowser.userTypedValue = url;
try { try {
addToUrlbarHistory(url); addToUrlbarHistory(url);
} catch (ex) { } catch (ex) {
// Things may go wrong when adding url to session history, // Things may go wrong when adding url to session history,
// but don't let that interfere with the loading of the url. // but don't let that interfere with the loading of the url.
Cu.reportError(ex); Cu.reportError(ex);
} }
function loadCurrent() { function loadCurrent() {
let webnav = Ci.nsIWebNavigation; let flags = Ci.nsIWebNavigation.LOAD_FLAGS_ALLOW_THIRD_PARTY_FIXUP;
let flags = webnav.LOAD_FLAGS_ALLOW_THIRD_PARTY_FIXUP | // Pass LOAD_FLAGS_DISALLOW_INHERIT_OWNER to prevent any loads from
webnav.LOAD_FLAGS_FIXUP_SCHEME_TYPOS; // inheriting the currently loaded document's principal, unless this
// Pass LOAD_FLAGS_DISALLOW_INHERIT_OWNER to prevent any loads from // URL is marked as safe to inherit (e.g. came from a bookmark
// inheriting the currently loaded document's principal, unless this // keyword).
// URL is marked as safe to inherit (e.g. came from a bookmark if (!mayInheritPrincipal)
// keyword). flags |= Ci.nsIWebNavigation.LOAD_FLAGS_DISALLOW_INHERIT_OWNER;
if (!mayInheritPrincipal) // If the value wasn't typed, we know that we decoded the value as
flags |= Ci.nsIWebNavigation.LOAD_FLAGS_DISALLOW_INHERIT_OWNER; // UTF-8 (see losslessDecodeURI)
gBrowser.loadURIWithFlags(url, flags, null, null, postData); if (!this.valueIsTyped)
} flags |= Ci.nsIWebNavigation.LOAD_FLAGS_URI_IS_UTF8;
gBrowser.loadURIWithFlags(url, flags, null, null, postData);
}
// Focus the content area before triggering loads, since if the load // Focus the content area before triggering loads, since if the load
// occurs in a new tab, we want focus to be restored to the content // occurs in a new tab, we want focus to be restored to the content
// area when the current tab is re-selected. // area when the current tab is re-selected.
gBrowser.selectedBrowser.focus(); gBrowser.selectedBrowser.focus();
let isMouseEvent = aTriggeringEvent instanceof MouseEvent; let isMouseEvent = aTriggeringEvent instanceof MouseEvent;
let altEnter = !isMouseEvent && aTriggeringEvent && aTriggeringEvent.altKey; let altEnter = !isMouseEvent && aTriggeringEvent && aTriggeringEvent.altKey;
if (altEnter) { if (altEnter) {
// XXX This was added a long time ago, and I'm not sure why it is // XXX This was added a long time ago, and I'm not sure why it is
// necessary. Alt+Enter's default action might cause a system beep, // necessary. Alt+Enter's default action might cause a system beep,
// or something like that? // or something like that?
aTriggeringEvent.preventDefault(); aTriggeringEvent.preventDefault();
aTriggeringEvent.stopPropagation(); aTriggeringEvent.stopPropagation();
} }
// If the current tab is empty, ignore Alt+Enter (just reuse this tab) // If the current tab is empty, ignore Alt+Enter (just reuse this tab)
altEnter = altEnter && !isTabEmpty(gBrowser.selectedTab); altEnter = altEnter && !isTabEmpty(gBrowser.selectedTab);
if (isMouseEvent || altEnter) { if (isMouseEvent || altEnter) {
// Use the standard UI link behaviors for clicks or Alt+Enter // Use the standard UI link behaviors for clicks or Alt+Enter
let where = "tab"; let where = "tab";
if (isMouseEvent) if (isMouseEvent)
where = whereToOpenLink(aTriggeringEvent, false, false); where = whereToOpenLink(aTriggeringEvent, false, false);
if (where == "current") { if (where == "current") {
loadCurrent(); if (matchLastLocationChange) {
loadCurrent();
}
} else {
this.handleRevert();
let params = { allowThirdPartyFixup: true,
postData: postData,
initiatingDoc: document };
if (!this.valueIsTyped)
params.isUTF8 = true;
openUILinkIn(url, where, params);
}
} else { } else {
this.handleRevert(); if (matchLastLocationChange) {
let params = { allowThirdPartyFixup: true, loadCurrent();
postData: postData, }
initiatingDoc: document };
openUILinkIn(url, where, params);
} }
} else { }.bind(this));
loadCurrent();
}
]]></body> ]]></body>
</method> </method>
<method name="_canonizeURL"> <method name="_canonizeURL">
<parameter name="aTriggeringEvent"/> <parameter name="aTriggeringEvent"/>
<body><![CDATA[ <body><![CDATA[
var url = this.value; return Task.spawn(function() {
if (!url) var url = this.value;
return ["", null, false]; if (!url)
throw new Task.Result(["", null, false]);
// Only add the suffix when the URL bar value isn't already "URL-like", // Only add the suffix when the URL bar value isn't already "URL-like",
// and only if we get a keyboard event, to match user expectations. // and only if we get a keyboard event, to match user expectations.
if (/^\s*[^.:\/\s]+(?:\/.*|\s*)$/i.test(url) && if (/^\s*[^.:\/\s]+(?:\/.*|\s*)$/i.test(url) &&
(aTriggeringEvent instanceof KeyEvent)) { (aTriggeringEvent instanceof KeyEvent)) {
#ifdef XP_MACOSX #ifdef XP_MACOSX
let accel = aTriggeringEvent.metaKey; let accel = aTriggeringEvent.metaKey;
#else #else
let accel = aTriggeringEvent.ctrlKey; let accel = aTriggeringEvent.ctrlKey;
#endif #endif
let shift = aTriggeringEvent.shiftKey; let shift = aTriggeringEvent.shiftKey;
let suffix = ""; let suffix = "";
switch (true) { switch (true) {
case (accel && shift): case (accel && shift):
suffix = ".org/"; suffix = ".org/";
break; break;
case (shift): case (shift):
suffix = ".net/"; suffix = ".net/";
break; break;
case (accel): case (accel):
try { try {
suffix = gPrefService.getCharPref("browser.fixup.alternate.suffix"); suffix = gPrefService.getCharPref("browser.fixup.alternate.suffix");
if (suffix.charAt(suffix.length - 1) != "/") if (suffix.charAt(suffix.length - 1) != "/")
suffix += "/"; suffix += "/";
} catch(e) { } catch(e) {
suffix = ".com/"; suffix = ".com/";
} }
break; break;
}
if (suffix) {
// trim leading/trailing spaces (bug 233205)
url = url.trim();
// Tack www. and suffix on. If user has appended directories, insert
// suffix before them (bug 279035). Be careful not to get two slashes.
let firstSlash = url.indexOf("/");
if (firstSlash >= 0) {
url = url.substring(0, firstSlash) + suffix +
url.substring(firstSlash + 1);
} else {
url = url + suffix;
} }
url = "http://www." + url; if (suffix) {
// trim leading/trailing spaces (bug 233205)
url = url.trim();
// Tack www. and suffix on. If user has appended directories, insert
// suffix before them (bug 279035). Be careful not to get two slashes.
let firstSlash = url.indexOf("/");
if (firstSlash >= 0) {
url = url.substring(0, firstSlash) + suffix +
url.substring(firstSlash + 1);
} else {
url = url + suffix;
}
url = "http://www." + url;
}
} }
}
var postData = {}; let data = yield getShortcutOrURIAndPostData(url);
var mayInheritPrincipal = { value: false };
url = getShortcutOrURI(url, postData, mayInheritPrincipal);
return [url, postData.value, mayInheritPrincipal.value]; throw new Task.Result([data.url, data.postData, data.mayInheritPrincipal]);
}.bind(this));
]]></body> ]]></body>
</method> </method>