Bug 594816 - Add a built-in system for badge handlers [r=vingtetun]
authorMark Finkle <mfinkle@mozilla.com>
Thu, 09 Sep 2010 13:45:40 -0400
changeset 66552 4cf211c738a84684f13ef3a6bab1ebbb5d2f1946
parent 66551 f16aa642eb1450ed670d0ca98678f06fcbd5ae76
child 66553 219b6d8ee65d97f8e2e97a3eed7cce3453373ac3
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)
reviewersvingtetun
bugs594816
Bug 594816 - Add a built-in system for badge handlers [r=vingtetun]
mobile/chrome/content/browser-ui.js
--- a/mobile/chrome/content/browser-ui.js
+++ b/mobile/chrome/content/browser-ui.js
@@ -390,16 +390,18 @@ var BrowserUI = {
     this._edit = document.getElementById("urlbar-edit");
     this._throbber = document.getElementById("urlbar-throbber");
     this._favicon = document.getElementById("urlbar-favicon");
     this._favicon.addEventListener("error", this, false);
 
     this._edit.addEventListener("click", this, false);
     this._edit.addEventListener("mousedown", this, false);
 
+    BadgeHandlers.register(this._edit.popup);
+
     let awesomePopup = document.getElementById("popup_autocomplete");
     awesomePopup.addEventListener("popupshown", this, false);
     awesomePopup.addEventListener("popuphidden", this, false);
 
     document.getElementById("toolbar-main").ignoreDrag = true;
 
     let tabs = document.getElementById("tabs");
     tabs.addEventListener("TabSelect", this, true);
@@ -2494,8 +2496,62 @@ var SharingUI = {
       callback: function callback(aURL, aTitle) {
         let url = "http://www.facebook.com/share.php?u=" + encodeURIComponent(aURL);
         Browser.addTab(url, true, Browser.selectedTab);
       }
     }
   ]
 };
 
+
+var BadgeHandlers = {
+  _handlers: [
+    {
+      _lastUpdate: 0,
+      _lastCount: null,
+      url: "http://mail.google.com",
+      updateBadge: function(aItem) {
+        // Use the cache if possible
+        let now = Date.now();
+        if (this._lastCount && this._lastUpdate > now - 1000) {
+          aItem.setAttribute("badge", this._lastCount);
+          return;
+        }
+
+        this._lastUpdate = now;
+
+        // Use any saved username and password. If we don't have any login and we are not
+        // currently logged into Gmail, we won't get any count.
+        let lm = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
+        let logins = lm.findLogins({}, "https://www.google.com", "https://www.google.com", null);
+        let username = logins.length > 0 ? logins[0].username : "";
+        let password = logins.length > 0 ? logins[0].password : "";
+
+        // Get the feed and read the count, passing any saved username and password
+        // but do not show any security dialogs if we fail
+        let req = new XMLHttpRequest();
+        req.mozBackgroundRequest = true;
+        req.open("GET", "https://mail.google.com/mail/feed/atom", true, username, password);
+        req.onreadystatechange = function(aEvent) {
+          if (req.readyState == 4) {
+            if (req.status == 200) {
+              let count = req.responseXML.getElementsByTagName("fullcount")[0].childNodes[0].nodeValue;
+              if (count > 100)
+                count = "99+";
+
+              this._lastCount = count;
+              aItem.setAttribute("badge", count);
+            } else {
+              aItem.removeAttribute("badge");
+            }
+          }
+        };
+        req.send(null);
+      }
+    }
+  ],
+
+  register: function(aPopup) {
+    let handlers = this._handlers;
+    for (let i = 0; i < handlers.length; i++)
+      aPopup.registerBadgeHandler(handlers[i].url, handlers[i]);
+  }
+};