Merge inbound to m-c on a CLOSED TREE.
[gecko.git] / webapprt / PaymentUIGlue.js
blob3e091dcaf6a4c2d5dae10d9ceaedb214a0c7d20b
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 const { interfaces: Ci, utils: Cu } = Components;
9 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
10 Cu.import("resource://gre/modules/Services.jsm");
12 XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
13                                   "@mozilla.org/childprocessmessagemanager;1",
14                                   "nsIMessageSender");
16 function paymentSuccess(aRequestId) {
17   return paymentCallback(aRequestId, "Payment:Success");
20 function paymentFailed(aRequestId) {
21   return paymentCallback(aRequestId, "Payment:Failed");
24 function paymentCallback(aRequestId, aMsg) {
25   return function(aResult) {
26           closePaymentWindow(aRequestId, function() {
27             cpmm.sendAsyncMessage(aMsg, { result: aResult,
28                                           requestId: aRequestId });
29           });
30         };
33 let payments = {};
35 function closePaymentWindow(aId, aCallback) {
36   if (payments[aId]) {
37     payments[aId].handled = true;
38     payments[aId].win.close();
39     payments[aId] = null;
40   }
42   aCallback();
45 function PaymentUI() {}
47 PaymentUI.prototype = {
48   classID: Components.ID("{ede1124f-72e8-4a31-9567-3270d46f21fb}"),
49   QueryInterface: XPCOMUtils.generateQI([Ci.nsIPaymentUIGlue]),
51   confirmPaymentRequest: function(aRequestId, aRequests, aSuccessCb, aErrorCb) {
52     // If there's only one payment provider that will work, just move on
53     // without prompting the user.
54     if (aRequests.length == 1) {
55       aSuccessCb.onresult(aRequestId, aRequests[0].wrappedJSObject.type);
56       return;
57     }
59     let items = [];
61     // Otherwise, let the user select a payment provider from a list.
62     for (let i = 0; i < aRequests.length; i++) {
63       let request = aRequests[i].wrappedJSObject;
64       let requestText = request.providerName;
65       if (request.productPrice && Array.isArray(request.productPrice)) {
66         // We should guess the user currency and use that instead.
67         requestText += " (" + request.productPrice[0].amount + " " +
68                               request.productPrice[0].currency + ")";
69       }
70       items.push(requestText);
71     }
73     let selected = {};
75     let bundle = Services.strings.
76                    createBundle("chrome://webapprt/locale/webapp.properties");
77     let result = Services.prompt.
78                    select(null, bundle.GetStringFromName("paymentDialog.title"),
79                           bundle.GetStringFromName("paymentDialog.message"),
80                           items.length, items, selected);
81     if (result) {
82       aSuccessCb.onresult(aRequestId,
83                           aRequests[selected.value].wrappedJSObject.type);
84     } else {
85       aErrorCb.onresult(aRequestId, "USER_CANCELLED");
86     }
87   },
89   showPaymentFlow: function(aRequestId, aPaymentFlowInfo, aErrorCb) {
90     let win = Services.ww.
91                 openWindow(null,
92                            "chrome://webapprt/content/webapp.xul",
93                            "_blank",
94                            "chrome,dialog=no,resizable,scrollbars,centerscreen",
95                            null);
97     // Store a reference to the window so that we can close it when the payment
98     // succeeds or fails.
99     payments[aRequestId] = { win: win, handled: false };
101     // Inject paymentSuccess and paymentFailed methods into the document after
102     // its loaded.
103     win.addEventListener("DOMContentLoaded", function onDOMContentLoaded() {
104       win.removeEventListener("DOMContentLoaded", onDOMContentLoaded);
106       let browserElement = win.document.getElementById("content");
107       browserElement.
108         setAttribute("src", aPaymentFlowInfo.uri + aPaymentFlowInfo.jwt);
110       browserElement.addEventListener("DOMWindowCreated",
111         function onDOMWindowCreated() {
112           browserElement.removeEventListener("DOMWindowCreated",
113                                              onDOMWindowCreated);
116           win.document.getElementById("content").contentDocument.defaultView
117              .wrappedJSObject.mozPaymentProvider = {
118                __exposedProps__: {
119                  paymentSuccess: 'r',
120                  paymentFailed: 'r'
121                },
122                paymentSuccess: paymentSuccess(aRequestId),
123                paymentFailed: paymentFailed(aRequestId)
124              };
125         }, true);
126     });
128     let winObserver = function(aClosedWin, aTopic) {
129       if (aTopic == "domwindowclosed") {
130         // Fail the payment if the window is closed.
131         if (aClosedWin == win) {
132           Services.ww.unregisterNotification(winObserver);
133           if (payments[aRequestId] && !payments[aRequestId].handled) {
134             aErrorCb.onresult(aRequestId, "USER_CANCELLED");
135           }
136         }
137       }
138     }
140     Services.ww.registerNotification(winObserver);
141   },
143   cleanup: function() {
144   },
147 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PaymentUI]);