Bug 634244 - Port |Bug 633638 - Need a way to cancel PlacesUtils::asyncGetBookmarkIds request| to SeaMonkey; (Av1a) Just copy it.
authorSerge Gautherie <sgautherie.bz@free.fr>
Sun, 13 Mar 2011 19:45:54 +0100
changeset 7339 7368388813b08b3a5e90db177399da8159fbbc21
parent 7338 06d9e044fd7a807d0cdca8aa8df6f73e36dc566a
child 7340 7fdea9ed43684510e956459881abff11356d5c97
push idunknown
push userunknown
push dateunknown
bugs634244, 633638
Bug 634244 - Port |Bug 633638 - Need a way to cancel PlacesUtils::asyncGetBookmarkIds request| to SeaMonkey; (Av1a) Just copy it. r=iann.
suite/common/bookmarks/browser-places.js
--- a/suite/common/bookmarks/browser-places.js
+++ b/suite/common/bookmarks/browser-places.js
@@ -710,16 +710,21 @@ var PlacesMenuDNDHandler = {
 var PlacesStarButton = {
   _hasBookmarksObserver: false,
 
   uninit: function PSB_uninit()
   {
     if (this._hasBookmarksObserver) {
       PlacesUtils.bookmarks.removeObserver(this);
     }
+
+    if (this._pendingStmt) {
+      this._pendingStmt.cancel();
+      delete this._pendingStmt;
+    }
   },
 
   QueryInterface: XPCOMUtils.generateQI([
     Components.interfaces.nsINavBookmarkObserver
   ]),
 
   get _starredTooltip()
   {
@@ -739,20 +744,28 @@ var PlacesStarButton = {
     if (this._uri && gBrowser.currentURI.equals(this._uri)) {
       return;
     }
 
     // Reset tracked values.
     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;
+    }
 
-    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;
+      }
+
       // It's possible that onItemAdded gets called before the async statement
       // calls back.  For such an edge case, retain all unique entries from both
       // arrays.
       this._itemIds = this._itemIds.filter(
         function (id) aItemIds.indexOf(id) == -1
       ).concat(aItemIds);
       this._updateStateInternal();
 
@@ -761,18 +774,17 @@ var PlacesStarButton = {
         try {
           PlacesUtils.bookmarks.addObserver(this, false);
           this._hasBookmarksObserver = true;
         } catch(ex) {
           Components.utils.reportError("PlacesStarButton failed adding a bookmarks observer: " + ex);
         }
       }
 
-      // Finally re-enable the star.
-      this._ignoreClicks = false;
+      delete this._pendingStmt;
     }, this);
   },
 
   _updateStateInternal: function PSB__updateStateInternal()
   {
     let starIcon = document.getElementById("star-button");
     if (this._itemIds.length > 0) {
       starIcon.setAttribute("starred", "true");
@@ -781,17 +793,18 @@ var PlacesStarButton = {
     else {
       starIcon.removeAttribute("starred");
       starIcon.setAttribute("tooltiptext", this._unstarredTooltip);
     }
   },
 
   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);
   },
 
   // nsINavBookmarkObserver
   onItemAdded:
   function PSB_onItemAdded(aItemId, aFolder, aIndex, aItemType, aURI)
   {
     if (aURI && aURI.equals(this._uri)) {