Bug 1044289 - Update PromptService.js to handle stuff from nsPrompter.js r=margaret
authorMark Finkle <mfinkle@mozilla.com>
Wed, 30 Dec 2015 14:59:45 -0500
changeset 277883 30a4af7454413b81262627213147a1dfa8657d30
parent 277882 5404603acfffd6591a1d9e410002127d4de90bd9
child 277884 cabf012e0c3b1d08d42396ee9dd74e4f35b2636e
child 277938 ec3eeea80a5e844869e9897dd496ef312ef17dfe
push id16810
push usermfinkle@mozilla.com
push dateWed, 30 Dec 2015 19:59:58 +0000
treeherderfx-team@30a4af745441 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret
bugs1044289
milestone46.0a1
Bug 1044289 - Update PromptService.js to handle stuff from nsPrompter.js r=margaret
mobile/android/components/MobileComponents.manifest
mobile/android/components/PromptService.js
--- a/mobile/android/components/MobileComponents.manifest
+++ b/mobile/android/components/MobileComponents.manifest
@@ -45,17 +45,18 @@ contract @mozilla.org/content-permission
 component {8f7002cb-e959-4f0a-a2e8-563232564385} WebappsUpdateTimer.js
 contract @mozilla.org/webapps-update-timer;1 {8f7002cb-e959-4f0a-a2e8-563232564385}
 category update-timer WebappsUpdateTimer @mozilla.org/webapps-update-timer;1,getService,webapp-background-update-timer,browser.webapps.updateInterval,86400
 
 # PromptService.js
 component {9a61149b-2276-4a0a-b79c-be994ad106cf} PromptService.js
 contract @mozilla.org/prompter;1 {9a61149b-2276-4a0a-b79c-be994ad106cf}
 contract @mozilla.org/embedcomp/prompt-service;1 {9a61149b-2276-4a0a-b79c-be994ad106cf}
-category wakeup-request PromptService @mozilla.org/embedcomp/prompt-service;1,nsIPromptService,getService,Prompt:Call
+component {80dae1e9-e0d2-4974-915f-f97050fa8068} PromptService.js
+contract @mozilla.org/network/authprompt-adapter-factory;1 {80dae1e9-e0d2-4974-915f-f97050fa8068}
 #endif
 
 # ImageBlockingPolicy.js
 component {f55f77f9-d33d-4759-82fc-60db3ee0bb91} ImageBlockingPolicy.js
 contract @mozilla.org/browser/blockimages-policy;1 {f55f77f9-d33d-4759-82fc-60db3ee0bb91}
 category content-policy ImageBlockingPolicy @mozilla.org/browser/blockimages-policy;1
 
 # XPIDialogService.js
--- a/mobile/android/components/PromptService.js
+++ b/mobile/android/components/PromptService.js
@@ -21,50 +21,39 @@ function PromptService() {
 PromptService.prototype = {
   classID: Components.ID("{9a61149b-2276-4a0a-b79c-be994ad106cf}"),
 
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIPromptFactory, Ci.nsIPromptService, Ci.nsIPromptService2]),
 
   /* ----------  nsIPromptFactory  ---------- */
   // XXX Copied from nsPrompter.js.
   getPrompt: function getPrompt(domWin, iid) {
-    let doc = this.getDocument();
-    if (!doc) {
-      let fallback = this._getFallbackService();
-      return fallback.QueryInterface(Ci.nsIPromptFactory).getPrompt(domWin, iid);
+    // This is still kind of dumb; the C++ code delegated to login manager
+    // here, which in turn calls back into us via nsIPromptService2.
+    if (iid.equals(Ci.nsIAuthPrompt2) || iid.equals(Ci.nsIAuthPrompt)) {
+      try {
+        let pwmgr = Cc["@mozilla.org/passwordmanager/authpromptfactory;1"].getService(Ci.nsIPromptFactory);
+        return pwmgr.getPrompt(domWin, iid);
+      } catch (e) {
+        Cu.reportError("nsPrompter: Delegation to password manager failed: " + e);
+      }
     }
 
-    let p = new InternalPrompt(domWin, doc);
+    let p = new InternalPrompt(domWin);
     p.QueryInterface(iid);
     return p;
   },
 
   /* ----------  private memebers  ---------- */
 
-  _getFallbackService: function _getFallbackService() {
-    return Components.classesByID["{7ad1b327-6dfa-46ec-9234-f2a620ea7e00}"]
-                     .getService(Ci.nsIPromptService);
-  },
-
-  getDocument: function getDocument() {
-    let win = Services.wm.getMostRecentWindow("navigator:browser");
-    return win ? win.document : null;
-  },
-
   // nsIPromptService and nsIPromptService2 methods proxy to our Prompt class
-  // if we can show in-document popups, or to the fallback service otherwise.
   callProxy: function(aMethod, aArguments) {
     let prompt;
-    let doc = this.getDocument();
-    if (!doc) {
-      let fallback = this._getFallbackService();
-      return fallback[aMethod].apply(fallback, aArguments);
-    }
     let domWin = aArguments[0];
-    prompt = new InternalPrompt(domWin, doc);
+    prompt = new InternalPrompt(domWin);
     return prompt[aMethod].apply(prompt, Array.prototype.slice.call(aArguments, 1));
   },
 
   /* ----------  nsIPromptService  ---------- */
 
   alert: function() {
     return this.callProxy("alert", arguments);
   },
@@ -97,24 +86,22 @@ PromptService.prototype = {
   promptAuth: function() {
     return this.callProxy("promptAuth", arguments);
   },
   asyncPromptAuth: function() {
     return this.callProxy("asyncPromptAuth", arguments);
   }
 };
 
-function InternalPrompt(aDomWin, aDocument) {
+function InternalPrompt(aDomWin) {
   this._domWin = aDomWin;
-  this._doc = aDocument;
 }
 
 InternalPrompt.prototype = {
   _domWin: null,
-  _doc: null,
 
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIPrompt, Ci.nsIAuthPrompt, Ci.nsIAuthPrompt2]),
 
   /* ---------- internal methods ---------- */
   _getPrompt: function _getPrompt(aTitle, aText, aButtons, aCheckMsg, aCheckState) {
     let p = new Prompt({
       window: this._domWin,
       title: aTitle,
@@ -368,21 +355,21 @@ InternalPrompt.prototype = {
 
   nsIAuthPrompt_prompt : function (title, text, passwordRealm, savePassword, defaultText, result) {
     // TODO: Port functions from nsLoginManagerPrompter.js to here
     if (defaultText)
       result.value = defaultText;
     return this.nsIPrompt_prompt(title, text, result, null, {});
   },
 
-  nsIAuthPrompt_promptUsernameAndPassword : function (aTitle, aText, aPasswordRealm, aSavePassword, aUser, aPass) {
+  nsIAuthPrompt_promptUsernameAndPassword : function(aTitle, aText, aPasswordRealm, aSavePassword, aUser, aPass) {
     return this.nsIAuthPrompt_loginPrompt(aTitle, aText, aPasswordRealm, aSavePassword, aUser, aPass);
   },
 
-  nsIAuthPrompt_promptPassword : function (aTitle, aText, aPasswordRealm, aSavePassword, aPass) {
+  nsIAuthPrompt_promptPassword : function(aTitle, aText, aPasswordRealm, aSavePassword, aPass) {
     return this.nsIAuthPrompt_loginPrompt(aTitle, aText, aPasswordRealm, aSavePassword, null, aPass);
   },
 
   nsIAuthPrompt_loginPrompt: function(aTitle, aPasswordRealm, aSavePassword, aUser, aPass) {
     let checkMsg = null;
     let check = { value: false };
     let hostname, realm;
     [hostname, realm, aUser] = PromptUtils.getHostnameAndRealm(aPasswordRealm);
@@ -823,9 +810,64 @@ var PromptUtils = {
 XPCOMUtils.defineLazyGetter(PromptUtils, "passwdBundle", function () {
   return Services.strings.createBundle("chrome://passwordmgr/locale/passwordmgr.properties");
 });
 
 XPCOMUtils.defineLazyGetter(PromptUtils, "bundle", function () {
   return Services.strings.createBundle("chrome://global/locale/commonDialogs.properties");
 });
 
-this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PromptService]);
+
+// Factory for wrapping nsIAuthPrompt interfaces to make them usable via an nsIAuthPrompt2 interface.
+// XXX Copied from nsPrompter.js.
+function AuthPromptAdapterFactory() {
+}
+
+AuthPromptAdapterFactory.prototype = {
+  classID: Components.ID("{80dae1e9-e0d2-4974-915f-f97050fa8068}"),
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsIAuthPromptAdapterFactory]),
+
+  /* ----------  nsIAuthPromptAdapterFactory ---------- */
+
+  createAdapter: function(aPrompt) {
+    return new AuthPromptAdapter(aPrompt);
+  }
+};
+
+
+// Takes an nsIAuthPrompt implementation, wraps it with a nsIAuthPrompt2 shell.
+// XXX Copied from nsPrompter.js.
+function AuthPromptAdapter(aPrompt) {
+  this.prompt = aPrompt;
+}
+
+AuthPromptAdapter.prototype = {
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsIAuthPrompt2]),
+  prompt: null,
+
+  /* ----------  nsIAuthPrompt2 ---------- */
+
+  promptAuth: function(aChannel, aLevel, aAuthInfo, aCheckLabel, aCheckValue) {
+    let message = PromptUtils.makeDialogText(aChannel, aAuthInfo);
+
+    let [username, password] = PromptUtils.getAuthInfo(aAuthInfo);
+    let [host, realm]  = PromptUtils.getAuthTarget(aChannel, aAuthInfo);
+    let authTarget = host + " (" + realm + ")";
+
+    let ok;
+    if (aAuthInfo.flags & Ci.nsIAuthInformation.ONLY_PASSWORD) {
+      ok = this.prompt.promptPassword(null, message, authTarget, Ci.nsIAuthPrompt.SAVE_PASSWORD_PERMANENTLY, password);
+    } else {
+      ok = this.prompt.promptUsernameAndPassword(null, message, authTarget, Ci.nsIAuthPrompt.SAVE_PASSWORD_PERMANENTLY, username, password);
+    }
+
+    if (ok) {
+      PromptUtils.setAuthInfo(aAuthInfo, username.value, password.value);
+    }
+    return ok;
+  },
+
+  asyncPromptAuth: function(aChannel, aCallback, aContext, aLevel, aAuthInfo, aCheckLabel, aCheckValue) {
+    throw Cr.NS_ERROR_NOT_IMPLEMENTED;
+  }
+};
+
+this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PromptService, AuthPromptAdapterFactory]);