Bug 696520 - Enable add-on installation : part 1/2 Adds JS helper for doorhanger notifications [r=mfinkle]
authorFabrice Desré <fabrice@mozilla.com>
Wed, 26 Oct 2011 13:21:42 -0700
changeset 81639 5bd3a03bc11b1da3fa102eaefb7b7824bb140adb
parent 81638 5a87519d61739c25e3f1b46cf61698541859945d
child 81640 04e3f9b035221444ada558d3e91b140495965396
push idunknown
push userunknown
push dateunknown
reviewersmfinkle
bugs696520
milestone10.0a1
Bug 696520 - Enable add-on installation : part 1/2 Adds JS helper for doorhanger notifications [r=mfinkle]
mobile/chrome/content/browser.js
--- a/mobile/chrome/content/browser.js
+++ b/mobile/chrome/content/browser.js
@@ -443,20 +443,22 @@ var BrowserApp = {
       this.setPreferences(aData);
     }
   }
 }
 
 var NativeWindow = {
   init: function() {
     Services.obs.addObserver(this, "Menu:Clicked", false);
+    Services.obs.addObserver(this, "Doorhanger:Reply", false);
   },
 
   uninit: function() {
     Services.obs.removeObserver(this, "Menu:Clicked");
+    Services.obs.removeObserver(this, "Doorhanger:Reply");
   },
 
   toast: {
     show: function(aMessage, aDuration) {
       sendMessageToJava({
         gecko: {
           type: "Toast:Show",
           message: aMessage,
@@ -483,19 +485,58 @@ var NativeWindow = {
       return this._menuId - 1;
     },
 
     remove: function(aId) {
       sendMessageToJava({ gecko: {type: "Menu:Remove", id: aId }});
     }
   },
 
+  doorhanger: {
+    _callbacks: [],
+    _callbacksId: 0,
+    _promptId: 0,
+    show: function(aMessage, aButtons, aTab) {
+      // use the current tab if none is provided
+      let tabID = aTab ? aTab : BrowserApp.selectedTab.id;
+      aButtons.forEach((function(aButton) {
+        this._callbacks[this._callbacksId] = { cb: aButton.callback, prompt: this._promptId };
+        aButton.callback = this._callbacksId;
+        this._callbacksId++;
+      }).bind(this));
+
+      this._promptId++;
+      let json = {
+        gecko: {
+          type: "Doorhanger:Add",
+          message: aMessage,
+          severity: "PRIORITY_WARNING_MEDIUM",
+          buttons: aButtons,
+          tabID: tabID
+        }
+      };
+      sendMessageToJava(json);
+    }
+  },
+  
   observe: function(aSubject, aTopic, aData) {
-    if (this.menu._callbacks[aData])
-      this.menu._callbacks[aData]();
+    if (aTopic == "Menu:Clicked") {
+      if (this.menu._callbacks[aData])
+        this.menu._callbacks[aData]();
+    } else if (aTopic == "Doorhanger:Reply") {
+      let id = parseInt(aData);
+      if (this.doorhanger._callbacks[id]) {
+        let prompt = this.doorhanger._callbacks[id].prompt;
+        this.doorhanger._callbacks[id].cb();
+        for (let i = 0; i < this._callbacksId; i++) {
+          if (this._callbacks[i] && this._callbacks[i].prompt == prompt)
+            delete this._callbacks[i];
+        }
+      }
+    }
   }
 };
 
 
 function nsBrowserAccess() {
 }
 
 nsBrowserAccess.prototype = {