Bug 1423053 - Close the PaymentRequest UI when complete is called. r=jaws draft
authorMatthew Noorenberghe <mozilla@noorenberghe.ca>
Tue, 19 Dec 2017 05:55:29 -0600
changeset 712994 c0d0a7b36f2857de1a9af1ddfe6520c620e5d4b4
parent 710528 52fbf40d3e7d0ee443fe8ee3c0d360c2ce87d2ce
child 712995 6fb081f74da70b53b8329c789c90b36b4a069082
child 713671 da0149737028b9ef93d2254ca317888e7fccdc96
child 713803 46535f2ae0b2c7da136113d5215f29980ff44fe8
push id93520
push usermozilla@noorenberghe.ca
push dateTue, 19 Dec 2017 11:56:00 +0000
reviewersjaws
bugs1423053
milestone59.0a1
Bug 1423053 - Close the PaymentRequest UI when complete is called. r=jaws https://w3c.github.io/payment-request/#complete-method MozReview-Commit-ID: 3CJkunoby30
toolkit/components/payments/paymentUIService.js
--- a/toolkit/components/payments/paymentUIService.js
+++ b/toolkit/components/payments/paymentUIService.js
@@ -52,51 +52,64 @@ PaymentUIService.prototype = {
                             `${this.REQUEST_ID_PREFIX}${requestId}`,
                             "modal,dialog,centerscreen,resizable=no");
   },
 
   abortPayment(requestId) {
     this.log.debug("abortPayment:", requestId);
     let abortResponse = Cc["@mozilla.org/dom/payments/payment-abort-action-response;1"]
                           .createInstance(Ci.nsIPaymentAbortActionResponse);
-
-    let enu = Services.wm.getEnumerator(null);
-    let win;
-    while ((win = enu.getNext())) {
-      if (win.name == `${this.REQUEST_ID_PREFIX}${requestId}`) {
-        this.log.debug(`closing: ${win.name}`);
-        win.close();
-        break;
-      }
-    }
+    let found = this.closeDialog(requestId);
 
     // if `win` is falsy, then we haven't found the dialog, so the abort fails
     // otherwise, the abort is successful
-    let response = win ?
+    let response = found ?
       Ci.nsIPaymentActionResponse.ABORT_SUCCEEDED :
       Ci.nsIPaymentActionResponse.ABORT_FAILED;
 
     abortResponse.init(requestId, response);
     paymentSrv.respondPayment(abortResponse);
   },
 
   completePayment(requestId) {
     this.log.debug("completePayment:", requestId);
+    let closed = this.closeDialog(requestId);
+    let responseCode = closed ?
+        Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED :
+        Ci.nsIPaymentActionResponse.COMPLETE_FAILED;
     let completeResponse = Cc["@mozilla.org/dom/payments/payment-complete-action-response;1"]
                              .createInstance(Ci.nsIPaymentCompleteActionResponse);
-    completeResponse.init(requestId, Ci.nsIPaymentActionResponse.COMPLTETE_SUCCEEDED);
+    completeResponse.init(requestId, responseCode);
     paymentSrv.respondPayment(completeResponse.QueryInterface(Ci.nsIPaymentActionResponse));
   },
 
   updatePayment(requestId) {
     this.log.debug("updatePayment:", requestId);
   },
 
   // other helper methods
 
+  /**
+   * @param {string} requestId - Payment Request ID of the dialog to close.
+   * @returns {boolean} whether the specified dialog was closed.
+   */
+  closeDialog(requestId) {
+    let enu = Services.wm.getEnumerator(null);
+    let win;
+    while ((win = enu.getNext())) {
+      if (win.name == `${this.REQUEST_ID_PREFIX}${requestId}`) {
+        this.log.debug(`closing: ${win.name}`);
+        win.close();
+        return true;
+      }
+    }
+
+    return false;
+  },
+
   requestIdForWindow(window) {
     let windowName = window.name;
 
     return windowName.startsWith(this.REQUEST_ID_PREFIX) ?
       windowName.replace(this.REQUEST_ID_PREFIX, "") : // returns suffix, which is the requestId
       null;
   },
 };