Bug 633638 - Need a way to cancel PlacesUtils::asyncGetBookmarkIds requests.

r=mak a=blocker
This commit is contained in:
Hiroyuki Ikezoe 2011-02-15 12:39:21 +01:00
parent 3b1c54ef8a
commit 8658e69633
4 changed files with 28 additions and 12 deletions

View File

@ -941,6 +941,10 @@ var PlacesStarButton = {
if (this._hasBookmarksObserver) {
PlacesUtils.bookmarks.removeObserver(this);
}
if (this._pendingStmt) {
this._pendingStmt.cancel();
delete this._pendingStmt;
}
},
QueryInterface: XPCOMUtils.generateQI([
@ -971,15 +975,23 @@ var PlacesStarButton = {
this._uri = gBrowser.currentURI;
this._itemIds = [];
// Ignore clicks on the star while we update its state.
this._ignoreClicks = true;
if (this._pendingStmt) {
this._pendingStmt.cancel();
delete this._pendingStmt;
}
// We can load about:blank before the actual page, but there is no point in handling that page.
if (this._uri.spec == "about:blank") {
return;
}
PlacesUtils.asyncGetBookmarkIds(this._uri, function (aItemIds) {
this._pendingStmt = PlacesUtils.asyncGetBookmarkIds(this._uri, function (aItemIds, aURI) {
// Safety check that the bookmarked URI equals the tracked one.
if (!aURI.equals(this._uri)) {
Components.utils.reportError("PlacesStarButton did not receive current URI");
return;
}
this._itemIds = aItemIds;
this._updateStateInternal();
@ -993,8 +1005,7 @@ var PlacesStarButton = {
}
}
// Finally re-enable the star.
this._ignoreClicks = false;
delete this._pendingStmt;
}, this);
},
@ -1016,7 +1027,8 @@ var PlacesStarButton = {
onClick: function PSB_onClick(aEvent)
{
if (aEvent.button == 0 && !this._ignoreClicks) {
// Ignore clicks on the star while we update its state.
if (aEvent.button == 0 && !this._pendingStmt) {
PlacesCommandHook.bookmarkCurrentPage(this._itemIds.length > 0);
}
// Don't bubble to the textbox, to avoid unwanted selection of the address.

View File

@ -61,7 +61,7 @@ function initTest() {
function waitForStarChange(aValue, aCallback) {
let starButton = document.getElementById("star-button");
if (PlacesStarButton._ignoreClicks || starButton.hasAttribute("starred") != aValue) {
if (PlacesStarButton._pendingStmt || starButton.hasAttribute("starred") != aValue) {
info("Waiting for star button change.");
setTimeout(arguments.callee, 50, aValue, aCallback);
return;

View File

@ -35,7 +35,7 @@ function test() {
}
function waitForStarChange(aValue, aCallback) {
if (PlacesStarButton._ignoreClicks || starButton.hasAttribute("starred") != aValue) {
if (PlacesStarButton._pendingStmt || starButton.hasAttribute("starred") != aValue) {
info("Waiting for star button change.");
timerID = setTimeout(arguments.callee, 50, aValue, aCallback);
return;

View File

@ -2049,11 +2049,15 @@ var PlacesUtils = {
* nsIURI or spec of the page.
* @param aCallback
* Function to be called when done.
* The function will receive an array of itemIds associated to aURI.
* The function will receive an array of itemIds associated to aURI and
* aURI itself.
* @param aScope
* Scope for the callback.
*
* @note Children of live bookmarks folders are excluded.
* @return A mozIStoragePendingStatement that can be used to cancel the request.
*
* @note Children of live bookmarks folders are excluded. The callback function is
* not invoked if the request is cancelled or hits an error.
*/
asyncGetBookmarkIds: function PU_asyncGetBookmarkIds(aURI, aCallback, aScope)
{
@ -2078,7 +2082,7 @@ var PlacesUtils = {
let url = aURI instanceof Ci.nsIURI ? aURI.spec : aURI;
this._asyncGetBookmarksStmt.params.url = url;
this._asyncGetBookmarksStmt.params.name = this.LMANNO_FEEDURI;
this._asyncGetBookmarksStmt.executeAsync({
return this._asyncGetBookmarksStmt.executeAsync({
_itemIds: [],
handleResult: function(aResultSet) {
let row, haveMatches = false;
@ -2093,7 +2097,7 @@ var PlacesUtils = {
handleCompletion: function(aReason)
{
if (aReason == Ci.mozIStorageStatementCallback.REASON_FINISHED) {
aCallback.apply(aScope, [this._itemIds]);
aCallback.apply(aScope, [this._itemIds, aURI]);
}
}
});