Bug 1100294 - Deprecate PlacesUtils.getURLAndPostDataForKeyword() and turn getShortcutOrURIAndPostData() into a task that uses the new keywords API r=mak

This commit is contained in:
Tim Taubert
2015-03-25 18:25:43 +01:00
parent 5a2685fc1a
commit c431f7e024
4 changed files with 96 additions and 74 deletions

View File

@@ -2100,53 +2100,81 @@ function loadURI(uri, referrer, postData, allowThirdPartyFixup, referrerPolicy)
} catch (e) {} } catch (e) {}
} }
function getShortcutOrURIAndPostData(aURL, aCallback) { /**
let mayInheritPrincipal = false; * Given a urlbar value, discerns between URIs, keywords and aliases.
let postData = null; *
let shortcutURL = null; * @param url
let keyword = aURL; * The urlbar value.
let param = ""; * @param callback (optional, deprecated)
* The callback function invoked when done. This parameter is
// XXX Bug 1100294 will remove this little hack by using an async version of * deprecated, please use the Promise that is returned.
// PlacesUtils.getURLAndPostDataForKeyword(). For now we simulate an async *
// execution with at least a setTimeout(fn, 0). * @return Promise<{ postData, url, mayInheritPrincipal }>
let originalCallback = aCallback; */
aCallback = data => setTimeout(() => originalCallback(data)); function getShortcutOrURIAndPostData(url, callback = null) {
if (callback) {
let offset = aURL.indexOf(" "); Deprecated.warning("Please use the Promise returned by " +
if (offset > 0) { "getShortcutOrURIAndPostData() instead of passing a " +
keyword = aURL.substr(0, offset); "callback",
param = aURL.substr(offset + 1); "https://bugzilla.mozilla.org/show_bug.cgi?id=1100294");
} }
let engine = Services.search.getEngineByAlias(keyword); return Task.spawn(function* () {
if (engine) { let mayInheritPrincipal = false;
let submission = engine.getSubmission(param, null, "keyword"); let postData = null;
postData = submission.postData; let shortcutURL = null;
aCallback({ postData: submission.postData, url: submission.uri.spec, let keyword = url;
mayInheritPrincipal: mayInheritPrincipal }); let param = "";
return;
}
[shortcutURL, postData] = let offset = url.indexOf(" ");
PlacesUtils.getURLAndPostDataForKeyword(keyword); if (offset > 0) {
keyword = url.substr(0, offset);
param = url.substr(offset + 1);
}
if (!shortcutURL) { let engine = Services.search.getEngineByAlias(keyword);
aCallback({ postData: postData, url: aURL, if (engine) {
mayInheritPrincipal: mayInheritPrincipal }); let submission = engine.getSubmission(param, null, "keyword");
return; postData = submission.postData;
} return { postData: submission.postData, url: submission.uri.spec,
mayInheritPrincipal };
}
let escapedPostData = ""; let entry = yield PlacesUtils.keywords.fetch(keyword);
if (postData) if (entry) {
escapedPostData = unescape(postData); shortcutURL = entry.url.href;
postData = entry.postData;
}
if (/%s/i.test(shortcutURL) || /%s/i.test(escapedPostData)) { if (!shortcutURL) {
let charset = ""; return { postData, url, mayInheritPrincipal };
const re = /^(.*)\&mozcharset=([a-zA-Z][_\-a-zA-Z0-9]+)\s*$/; }
let matches = shortcutURL.match(re);
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 {
let uri;
try {
// makeURI() throws if URI is invalid.
uri = makeURI(shortcutURL);
} catch (ex) {}
if (uri) {
// Try to get the saved character-set.
// Will return an empty string if character-set is not found.
charset = yield PlacesUtils.getCharsetForURI(uri);
}
}
let continueOperation = function () {
// encodeURIComponent produces UTF-8, and cannot be used for other charsets. // encodeURIComponent produces UTF-8, and cannot be used for other charsets.
// escape() works in those cases, but it doesn't uri-encode +, @, and /. // escape() works in those cases, but it doesn't uri-encode +, @, and /.
// Therefore we need to manually replace these ASCII characters by their // Therefore we need to manually replace these ASCII characters by their
@@ -2169,40 +2197,29 @@ function getShortcutOrURIAndPostData(aURL, aCallback) {
// document's principal. // document's principal.
mayInheritPrincipal = true; mayInheritPrincipal = true;
aCallback({ postData: postData, url: shortcutURL, return { postData, url: shortcutURL, mayInheritPrincipal };
mayInheritPrincipal: mayInheritPrincipal });
} }
if (matches) { if (param) {
[, shortcutURL, charset] = matches; // This keyword doesn't take a parameter, but one was provided. Just return
continueOperation(); // the original URL.
} else { postData = null;
// Try to get the saved character-set.
// makeURI throws if URI is invalid. return { postData, url, mayInheritPrincipal };
// 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 // This URL came from a bookmark, so it's safe to let it inherit the current
// document's principal. // document's principal.
mayInheritPrincipal = true; mayInheritPrincipal = true;
aCallback({ postData: postData, url: shortcutURL, return { postData, url: shortcutURL, mayInheritPrincipal };
mayInheritPrincipal: mayInheritPrincipal }); }).then(data => {
} if (callback) {
callback(data);
}
return data;
});
} }
function getPostDataStream(aStringData, aKeyword, aEncKeyword, aType) { function getPostDataStream(aStringData, aKeyword, aEncKeyword, aType) {
@@ -3252,7 +3269,7 @@ var newTabButtonObserver = {
onDrop: function (aEvent) onDrop: function (aEvent)
{ {
let url = browserDragAndDrop.drop(aEvent, { }); let url = browserDragAndDrop.drop(aEvent, { });
getShortcutOrURIAndPostData(url, data => { getShortcutOrURIAndPostData(url).then(data => {
if (data.url) { if (data.url) {
// allow third-party services to fixup this URL // allow third-party services to fixup this URL
openNewTabWith(data.url, null, data.postData, aEvent, true); openNewTabWith(data.url, null, data.postData, aEvent, true);
@@ -3272,7 +3289,7 @@ var newWindowButtonObserver = {
onDrop: function (aEvent) onDrop: function (aEvent)
{ {
let url = browserDragAndDrop.drop(aEvent, { }); let url = browserDragAndDrop.drop(aEvent, { });
getShortcutOrURIAndPostData(url, data => { getShortcutOrURIAndPostData(url).then(data => {
if (data.url) { if (data.url) {
// allow third-party services to fixup this URL // allow third-party services to fixup this URL
openNewWindowWith(data.url, null, data.postData, true); openNewWindowWith(data.url, null, data.postData, true);
@@ -5608,7 +5625,7 @@ function middleMousePaste(event) {
lastLocationChange = gBrowser.selectedBrowser.lastLocationChange; lastLocationChange = gBrowser.selectedBrowser.lastLocationChange;
} }
getShortcutOrURIAndPostData(clipboard, data => { getShortcutOrURIAndPostData(clipboard).then(data => {
try { try {
makeURI(data.url); makeURI(data.url);
} catch (ex) { } catch (ex) {
@@ -5645,7 +5662,7 @@ function handleDroppedLink(event, url, name)
{ {
let lastLocationChange = gBrowser.selectedBrowser.lastLocationChange; let lastLocationChange = gBrowser.selectedBrowser.lastLocationChange;
getShortcutOrURIAndPostData(url, data => { getShortcutOrURIAndPostData(url).then(data => {
if (data.url && if (data.url &&
lastLocationChange == gBrowser.selectedBrowser.lastLocationChange) lastLocationChange == gBrowser.selectedBrowser.lastLocationChange)
loadURI(data.url, null, data.postData, false); loadURI(data.url, null, data.postData, false);

View File

@@ -100,8 +100,7 @@ add_task(function* test_getshortcutoruri() {
let query = data.keyword; let query = data.keyword;
if (data.searchWord) if (data.searchWord)
query += " " + data.searchWord; query += " " + data.searchWord;
let returnedData = yield new Promise( let returnedData = yield getShortcutOrURIAndPostData(query);
resolve => getShortcutOrURIAndPostData(query, resolve));
// null result.url means we should expect the same query we sent in // null result.url means we should expect the same query we sent in
let expected = result.url || query; let expected = result.url || query;
is(returnedData.url, expected, "got correct URL for " + data.keyword); is(returnedData.url, expected, "got correct URL for " + data.keyword);

View File

@@ -450,7 +450,7 @@
} }
} }
getShortcutOrURIAndPostData(url, data => { getShortcutOrURIAndPostData(url).then(data => {
aCallback([data.url, data.postData, data.mayInheritPrincipal]); aCallback([data.url, data.postData, data.mayInheritPrincipal]);
}); });
]]></body> ]]></body>

View File

@@ -932,8 +932,14 @@ this.PlacesUtils = {
* Get the URI (and any associated POST data) for a given keyword. * Get the URI (and any associated POST data) for a given keyword.
* @param aKeyword string keyword * @param aKeyword string keyword
* @returns an array containing a string URL and a string of POST data * @returns an array containing a string URL and a string of POST data
*
* @deprecated
*/ */
getURLAndPostDataForKeyword(aKeyword) { getURLAndPostDataForKeyword(aKeyword) {
Deprecated.warning("getURLAndPostDataForKeyword() is deprecated, please " +
"use PlacesUtils.keywords.fetch() instead",
"https://bugzilla.mozilla.org/show_bug.cgi?id=1100294");
let stmt = PlacesUtils.history.DBConnection.createStatement( let stmt = PlacesUtils.history.DBConnection.createStatement(
`SELECT h.url, k.post_data `SELECT h.url, k.post_data
FROM moz_keywords k FROM moz_keywords k