Bug 618715 - Make bookmark star toggle async [r=mfinkle]
authorMatt Brubeck <mbrubeck@mozilla.com>
Thu, 30 Dec 2010 09:58:31 -0800
changeset 67188 21528afd3ac4b884760b611c128be99dbee31f4b
parent 67187 7f3c90368a4722495f6e945d0e7e32091e142b21
child 67189 46c24ff8f7fbd025abb7e085ef7a5b721e7b80d7
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs618715
Bug 618715 - Make bookmark star toggle async [r=mfinkle]
mobile/chrome/content/browser-ui.js
mobile/chrome/tests/browser_bookmarks.js
mobile/chrome/tests/browser_bookmarks_tags.js
--- a/mobile/chrome/content/browser-ui.js
+++ b/mobile/chrome/content/browser-ui.js
@@ -1043,33 +1043,42 @@ var BrowserUI = {
       case "cmd_go":
         this.goToURI();
         break;
       case "cmd_openLocation":
         this.showAutoComplete();
         break;
       case "cmd_star":
       {
+        BookmarkPopup.toggle();
+        if (!this.starButton.hasAttribute("starred")) {
+          this.starButton.setAttribute("starred", "true");
+          BookmarkPopup.autoHide();
+        }
+
         let bookmarkURI = browser.currentURI;
-        let autoClose = false;
-
-        if (PlacesUtils.getMostRecentBookmarkForURI(bookmarkURI) == -1) {
-          let bookmarkTitle = browser.contentTitle || bookmarkURI.spec;
-          let bookmarkService = PlacesUtils.bookmarks;
-          let bookmarkId = bookmarkService.insertBookmark(BookmarkList.panel.mobileRoot, bookmarkURI,
-                                                          bookmarkService.DEFAULT_INDEX,
-                                                          bookmarkTitle);
-          this.updateStar();
-
-          // autoclose the bookmark popup
-          autoClose = true;
-        }
-
-        // Show/hide bookmark popup
-        BookmarkPopup.toggle(autoClose);
+        PlacesUtils.asyncGetBookmarkIds(bookmarkURI, function (aItemIds) {
+          if (!aItemIds.length) {
+            let bookmarkTitle = browser.contentTitle || bookmarkURI.spec;
+            try {
+              let bookmarkService = PlacesUtils.bookmarks;
+              let bookmarkId = bookmarkService.insertBookmark(BookmarkList.panel.mobileRoot, bookmarkURI,
+                                                              bookmarkService.DEFAULT_INDEX,
+                                                              bookmarkTitle);
+            } catch (e) {
+              // Insert failed; reset the star state.
+              this.updateStar();
+            }
+
+            // XXX Used for browser-chrome tests
+            let event = document.createEvent("Events");
+            event.initEvent("BookmarkCreated", true, false);
+            window.dispatchEvent(event);
+          }
+        }, this);
         break;
       }
       case "cmd_opensearch":
         this.blurFocusedElement();
         BrowserSearch.toggle();
         break;
       case "cmd_bookmarks":
         this.activePanel = BookmarkList;
@@ -1614,34 +1623,37 @@ var BookmarkPopup = {
     if (this._bookmarkPopupTimeout != -1) {
       clearTimeout(this._bookmarkPopupTimeout);
       this._bookmarkPopupTimeout = -1;
     }
     this.box.hidden = true;
     BrowserUI.popPopup(this);
   },
 
-  show : function show(aAutoClose) {
+  show : function show() {
     this.box.hidden = false;
     this.box.anchorTo(BrowserUI.starButton);
 
-    if (aAutoClose) {
-      this._bookmarkPopupTimeout = setTimeout(function (self) {
-        self._bookmarkPopupTimeout = -1;
-        self.hide();
-      }, 2000, this);
-    }
-
     // include starButton here, so that click-to-dismiss works as expected
     BrowserUI.pushPopup(this, [this.box, BrowserUI.starButton]);
   },
 
-  toggle : function toggle(aAutoClose) {
+  autoHide: function autoHide() {
+    if (this._bookmarkPopupTimeout != -1 || this.box.hidden)
+      return;
+
+    this._bookmarkPopupTimeout = setTimeout(function (self) {
+      self._bookmarkPopupTimeout = -1;
+      self.hide();
+    }, 2000, this);
+  },
+
+  toggle : function toggle() {
     if (this.box.hidden)
-      this.show(aAutoClose);
+      this.show();
     else
       this.hide();
   }
 };
 
 var BookmarkHelper = {
   _panel: null,
   _editor: null,
--- a/mobile/chrome/tests/browser_bookmarks.js
+++ b/mobile/chrome/tests/browser_bookmarks.js
@@ -73,23 +73,25 @@ gTests.push({
 
     // Need to wait until the page is loaded
     waitForPageShow(gCurrentTest.onPageReady);
   },
 
   onPageReady: function() {
     let starbutton = document.getElementById("tool-star");
     starbutton.click();
-
-    let bookmark = PlacesUtils.getMostRecentBookmarkForURI(makeURI(testURL_01));
-    ok(bookmark != -1, testURL_01 + " should be added.");
+    window.addEventListener("BookmarkCreated", function(aEvent) {
+      window.removeEventListener(aEvent.type, arguments.callee, false);
+      let bookmark = PlacesUtils.getMostRecentBookmarkForURI(makeURI(testURL_01));
+      ok(bookmark != -1, testURL_01 + " should be added.");
 
-    Browser.closeTab(gCurrentTest._currentTab);
+      Browser.closeTab(gCurrentTest._currentTab);
 
-    runNextTest();
+      runNextTest();
+    }, false);
   }
 });
 
 //------------------------------------------------------------------------------
 // Case: Test clicking on a bookmark loads the web page
 gTests.push({
   desc: "Test clicking on a bookmark loads the web page",
   _currentTab: null,
--- a/mobile/chrome/tests/browser_bookmarks_tags.js
+++ b/mobile/chrome/tests/browser_bookmarks_tags.js
@@ -57,22 +57,25 @@ gTests.push({
     };
     this._currentTab.browser.addEventListener("load", handleEvent , true);
   },
 
   onPageLoad: function() {
     let starbutton = document.getElementById("tool-star");
     starbutton.click();
 
-    let bookmarkItem = PlacesUtils.getMostRecentBookmarkForURI(makeURI(testURL_02));
-    ok(bookmarkItem != -1, testURL_02 + " should be added.");
+    window.addEventListener("BookmarkCreated", function(aEvent) {
+      window.removeEventListener(aEvent.type, arguments.callee, false);
+      let bookmarkItem = PlacesUtils.getMostRecentBookmarkForURI(makeURI(testURL_02));
+      ok(bookmarkItem != -1, testURL_02 + " should be added.");
 
-    // Wait for the bookmarks to load, then do the test
-    window.addEventListener("NavigationPanelShown", gCurrentTest.onBookmarksReady, false);
-    BrowserUI.doCommand("cmd_bookmarks");
+      // Wait for the bookmarks to load, then do the test
+      window.addEventListener("NavigationPanelShown", gCurrentTest.onBookmarksReady, false);
+      BrowserUI.doCommand("cmd_bookmarks");
+    }, false);
   },
 
   onBookmarksReady: function() {
     window.removeEventListener("NavigationPanelShown", gCurrentTest.onBookmarksReady, false);
 
     // Go into edit mode
     let bookmark = document.getAnonymousElementByAttribute(BookmarkList.panel, "uri", testURL_02);
     bookmark.startEditing();