Bug 989984 - getShortcutOrURIAndPostData should have a synchronous callback behavior

Issue #112
This commit is contained in:
janekptacijarabaci 2018-04-15 09:46:17 +02:00 committed by Roy Tam
commit 6b4c0ab0f1
2 changed files with 152 additions and 135 deletions

View file

@ -1898,55 +1898,47 @@ function loadURI(uri, referrer, postData, allowThirdPartyFixup) {
} catch (e) {}
}
function getShortcutOrURIAndPostData(aURL) {
return Task.spawn(function() {
let mayInheritPrincipal = false;
let postData = null;
let shortcutURL = null;
let keyword = aURL;
let param = "";
function getShortcutOrURIAndPostData(aURL, aCallback) {
let mayInheritPrincipal = false;
let postData = null;
let shortcutURL = null;
let keyword = aURL;
let param = "";
let offset = aURL.indexOf(" ");
if (offset > 0) {
keyword = aURL.substr(0, offset);
param = aURL.substr(offset + 1);
}
let offset = aURL.indexOf(" ");
if (offset > 0) {
keyword = aURL.substr(0, offset);
param = aURL.substr(offset + 1);
}
let engine = Services.search.getEngineByAlias(keyword);
if (engine) {
let submission = engine.getSubmission(param);
postData = submission.postData;
throw new Task.Result({ postData: submission.postData,
url: submission.uri.spec,
mayInheritPrincipal: mayInheritPrincipal });
}
let engine = Services.search.getEngineByAlias(keyword);
if (engine) {
let submission = engine.getSubmission(param);
postData = submission.postData;
aCallback({ postData: submission.postData, url: submission.uri.spec,
mayInheritPrincipal: mayInheritPrincipal });
return;
}
[shortcutURL, postData] =
PlacesUtils.getURLAndPostDataForKeyword(keyword);
[shortcutURL, postData] =
PlacesUtils.getURLAndPostDataForKeyword(keyword);
if (!shortcutURL)
throw new Task.Result({ postData: postData, url: aURL,
mayInheritPrincipal: mayInheritPrincipal });
if (!shortcutURL) {
aCallback({ postData: postData, url: aURL,
mayInheritPrincipal: mayInheritPrincipal });
return;
}
let escapedPostData = "";
if (postData)
escapedPostData = unescape(postData);
let escapedPostData = "";
if (postData)
escapedPostData = unescape(postData);
if (/%s/i.test(shortcutURL) || /%s/i.test(escapedPostData)) {
let charset = "";
const re = /^(.*)\&mozcharset=([a-zA-Z][_\-a-zA-Z0-9]+)\s*$/;
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) {}
}
if (/%s/i.test(shortcutURL) || /%s/i.test(escapedPostData)) {
let charset = "";
const re = /^(.*)\&mozcharset=([a-zA-Z][_\-a-zA-Z0-9]+)\s*$/;
let matches = shortcutURL.match(re);
let continueOperation = function () {
// 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
@ -1964,23 +1956,45 @@ function getShortcutOrURIAndPostData(aURL) {
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;
aCallback({ postData: postData, url: shortcutURL,
mayInheritPrincipal: mayInheritPrincipal });
}
if (matches) {
[, shortcutURL, charset] = matches;
continueOperation();
} else {
// Try to get the saved character-set.
// makeURI throws if URI is invalid.
// Will return an empty string if character-set is not found.
try {
PlacesUtils.getCharsetForURI(makeURI(shortcutURL))
.then(c => { charset = c; continueOperation(); });
} catch (ex) {
continueOperation();
}
}
}
else if (param) {
// This keyword doesn't take a parameter, but one was provided. Just return
// the original URL.
postData = null;
aCallback({ postData: postData, url: aURL,
mayInheritPrincipal: mayInheritPrincipal });
} else {
// 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 });
});
aCallback({ postData: postData, url: shortcutURL,
mayInheritPrincipal: mayInheritPrincipal });
}
}
function getPostDataStream(aStringData, aKeyword, aEncKeyword, aType) {
@ -2725,8 +2739,7 @@ var newTabButtonObserver = {
onDrop: function (aEvent)
{
let url = browserDragAndDrop.drop(aEvent, { });
Task.spawn(function() {
let data = yield getShortcutOrURIAndPostData(url);
getShortcutOrURIAndPostData(url, data => {
if (data.url) {
// allow third-party services to fixup this URL
openNewTabWith(data.url, null, data.postData, aEvent, true);
@ -2746,8 +2759,7 @@ var newWindowButtonObserver = {
onDrop: function (aEvent)
{
let url = browserDragAndDrop.drop(aEvent, { });
Task.spawn(function() {
let data = yield getShortcutOrURIAndPostData(url);
getShortcutOrURIAndPostData(url, data => {
if (data.url) {
// allow third-party services to fixup this URL
openNewWindowWith(data.url, null, data.postData, true);
@ -5048,8 +5060,7 @@ function middleMousePaste(event) {
lastLocationChange = gBrowser.selectedBrowser.lastLocationChange;
}
Task.spawn(function() {
let data = yield getShortcutOrURIAndPostData(clipboard);
getShortcutOrURIAndPostData(clipboard, data => {
try {
makeURI(data.url);
} catch (ex) {
@ -5080,8 +5091,7 @@ function handleDroppedLink(event, url, name)
{
let lastLocationChange = gBrowser.selectedBrowser.lastLocationChange;
Task.spawn(function() {
let data = yield getShortcutOrURIAndPostData(url);
getShortcutOrURIAndPostData(url, data => {
if (data.url &&
lastLocationChange == gBrowser.selectedBrowser.lastLocationChange)
loadURI(data.url, null, data.postData, false);

View file

@ -264,29 +264,35 @@
var action = this._parseActionUrl(url);
let lastLocationChange = gBrowser.selectedBrowser.lastLocationChange;
Task.spawn(function() {
let matchLastLocationChange = true;
if (action) {
url = action.param;
if (this.hasAttribute("actiontype")) {
if (action.type == "switchtab") {
this.handleRevert();
let prevTab = gBrowser.selectedTab;
if (switchToTabHavingURI(url) &&
isTabEmpty(prevTab))
gBrowser.removeTab(prevTab);
}
return;
}
}
else {
[url, postData, mayInheritPrincipal] = yield this._canonizeURL(aTriggeringEvent);
matchLastLocationChange = (lastLocationChange ==
gBrowser.selectedBrowser.lastLocationChange);
if (!url)
return;
}
let matchLastLocationChange = true;
if (action) {
url = action.param;
if (this.hasAttribute("actiontype")) {
if (action.type == "switchtab") {
this.handleRevert();
let prevTab = gBrowser.selectedTab;
if (switchToTabHavingURI(url) &&
isTabEmpty(prevTab))
gBrowser.removeTab(prevTab);
}
return;
}
continueOperation.call(this);
}
else {
this._canonizeURL(aTriggeringEvent, response => {
[url, postData, mayInheritPrincipal] = response;
if (url) {
matchLastLocationChange = (lastLocationChange ==
gBrowser.selectedBrowser.lastLocationChange);
continueOperation.call(this);
}
});
}
function continueOperation()
{
this.value = url;
gBrowser.userTypedValue = url;
try {
@ -355,73 +361,74 @@
loadCurrent();
}
}
}.bind(this));
}
]]></body>
</method>
<method name="_canonizeURL">
<parameter name="aTriggeringEvent"/>
<parameter name="aCallback"/>
<body><![CDATA[
return Task.spawn(function() {
var url = this.value;
if (!url)
throw new Task.Result(["", null, false]);
var url = this.value;
if (!url) {
aCallback(["", null, false]);
return;
}
// 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.
if (/^\s*[^.:\/\s]+(?:\/.*|\s*)$/i.test(url) &&
(aTriggeringEvent instanceof KeyEvent)) {
// 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.
if (/^\s*[^.:\/\s]+(?:\/.*|\s*)$/i.test(url) &&
(aTriggeringEvent instanceof KeyEvent)) {
#ifdef XP_MACOSX
let accel = aTriggeringEvent.metaKey;
let accel = aTriggeringEvent.metaKey;
#else
let accel = aTriggeringEvent.ctrlKey;
let accel = aTriggeringEvent.ctrlKey;
#endif
let shift = aTriggeringEvent.shiftKey;
let shift = aTriggeringEvent.shiftKey;
let suffix = "";
let suffix = "";
switch (true) {
case (accel && shift):
suffix = ".org/";
break;
case (shift):
suffix = ".net/";
break;
case (accel):
try {
suffix = gPrefService.getCharPref("browser.fixup.alternate.suffix");
if (suffix.charAt(suffix.length - 1) != "/")
suffix += "/";
} catch(e) {
suffix = ".com/";
}
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;
switch (true) {
case (accel && shift):
suffix = ".org/";
break;
case (shift):
suffix = ".net/";
break;
case (accel):
try {
suffix = gPrefService.getCharPref("browser.fixup.alternate.suffix");
if (suffix.charAt(suffix.length - 1) != "/")
suffix += "/";
} catch(e) {
suffix = ".com/";
}
url = "http://www." + url;
}
break;
}
let data = yield getShortcutOrURIAndPostData(url);
if (suffix) {
// trim leading/trailing spaces (bug 233205)
url = url.trim();
throw new Task.Result([data.url, data.postData, data.mayInheritPrincipal]);
}.bind(this));
// 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;
}
}
getShortcutOrURIAndPostData(url, data => {
aCallback([data.url, data.postData, data.mayInheritPrincipal]);
});
]]></body>
</method>