1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/b2g/components/PaymentGlue.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,208 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +const { classes: Cc, interfaces: Ci, utils: Cu } = Components; 1.11 + 1.12 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.13 +Cu.import("resource://gre/modules/Services.jsm"); 1.14 + 1.15 +// JS shim that contains the callback functions to be triggered from the 1.16 +// payment provider's code in order to fire DOMRequest events. 1.17 +const kPaymentShimFile = "chrome://b2g/content/payment.js"; 1.18 + 1.19 +// Type of MozChromEvents to handle payment dialogs. 1.20 +const kOpenPaymentConfirmationEvent = "open-payment-confirmation-dialog"; 1.21 +const kOpenPaymentFlowEvent = "open-payment-flow-dialog"; 1.22 + 1.23 +const PREF_DEBUG = "dom.payment.debug"; 1.24 + 1.25 +XPCOMUtils.defineLazyServiceGetter(this, "uuidgen", 1.26 + "@mozilla.org/uuid-generator;1", 1.27 + "nsIUUIDGenerator"); 1.28 + 1.29 +XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy", 1.30 + "resource://gre/modules/SystemAppProxy.jsm"); 1.31 + 1.32 +function PaymentUI() { 1.33 + try { 1.34 + this._debug = 1.35 + Services.prefs.getPrefType(PREF_DEBUG) == Ci.nsIPrefBranch.PREF_BOOL 1.36 + && Services.prefs.getBoolPref(PREF_DEBUG); 1.37 + } catch(e) { 1.38 + this._debug = false; 1.39 + } 1.40 +} 1.41 + 1.42 +PaymentUI.prototype = { 1.43 + 1.44 + confirmPaymentRequest: function confirmPaymentRequest(aRequestId, 1.45 + aRequests, 1.46 + aSuccessCb, 1.47 + aErrorCb) { 1.48 + let _error = function _error(errorMsg) { 1.49 + if (aErrorCb) { 1.50 + aErrorCb.onresult(aRequestId, errorMsg); 1.51 + } 1.52 + }; 1.53 + 1.54 + // The UI should listen for mozChromeEvent 'open-payment-confirmation-dialog' 1.55 + // type in order to create and show the payment request confirmation frame 1.56 + // embeded within a trusted dialog. 1.57 + let id = kOpenPaymentConfirmationEvent + "-" + this.getRandomId(); 1.58 + let detail = { 1.59 + type: kOpenPaymentConfirmationEvent, 1.60 + id: id, 1.61 + requestId: aRequestId, 1.62 + paymentRequests: aRequests 1.63 + }; 1.64 + 1.65 + // Once the user confirm the payment request and makes his choice, we get 1.66 + // back to the DOM part to get the appropriate payment flow information 1.67 + // based on the selected payment provider. 1.68 + this._handleSelection = (function _handleSelection(evt) { 1.69 + let msg = evt.detail; 1.70 + if (msg.id != id) { 1.71 + return; 1.72 + } 1.73 + 1.74 + if (msg.userSelection && aSuccessCb) { 1.75 + aSuccessCb.onresult(aRequestId, msg.userSelection); 1.76 + } else if (msg.errorMsg) { 1.77 + _error(msg.errorMsg); 1.78 + } 1.79 + 1.80 + SystemAppProxy.removeEventListener("mozContentEvent", this._handleSelection); 1.81 + this._handleSelection = null; 1.82 + }).bind(this); 1.83 + SystemAppProxy.addEventListener("mozContentEvent", this._handleSelection); 1.84 + 1.85 + SystemAppProxy.dispatchEvent(detail); 1.86 + }, 1.87 + 1.88 + showPaymentFlow: function showPaymentFlow(aRequestId, 1.89 + aPaymentFlowInfo, 1.90 + aErrorCb) { 1.91 + let _error = function _error(errorMsg) { 1.92 + if (aErrorCb) { 1.93 + aErrorCb.onresult(aRequestId, errorMsg); 1.94 + } 1.95 + }; 1.96 + 1.97 + // We ask the UI to browse to the selected payment flow. 1.98 + let id = kOpenPaymentFlowEvent + "-" + this.getRandomId(); 1.99 + let detail = { 1.100 + type: kOpenPaymentFlowEvent, 1.101 + id: id, 1.102 + requestId: aRequestId, 1.103 + uri: aPaymentFlowInfo.uri, 1.104 + method: aPaymentFlowInfo.requestMethod, 1.105 + jwt: aPaymentFlowInfo.jwt 1.106 + }; 1.107 + 1.108 + // At some point the UI would send the created iframe back so the 1.109 + // callbacks for firing DOMRequest events can be loaded on its 1.110 + // content. 1.111 + this._loadPaymentShim = (function _loadPaymentShim(evt) { 1.112 + let msg = evt.detail; 1.113 + if (msg.id != id) { 1.114 + return; 1.115 + } 1.116 + 1.117 + if (msg.errorMsg) { 1.118 + SystemAppProxy.removeEventListener("mozContentEvent", this._loadPaymentShim); 1.119 + this._loadPaymentShim = null; 1.120 + _error("ERROR_LOADING_PAYMENT_SHIM: " + msg.errorMsg); 1.121 + return; 1.122 + } 1.123 + 1.124 + if (!msg.frame) { 1.125 + SystemAppProxy.removeEventListener("mozContentEvent", this._loadPaymentShim); 1.126 + this._loadPaymentShim = null; 1.127 + _error("ERROR_LOADING_PAYMENT_SHIM"); 1.128 + return; 1.129 + } 1.130 + 1.131 + // Try to load the payment shim file containing the payment callbacks 1.132 + // in the content script. 1.133 + let frame = msg.frame; 1.134 + let frameLoader = frame.QueryInterface(Ci.nsIFrameLoaderOwner) 1.135 + .frameLoader; 1.136 + let mm = frameLoader.messageManager; 1.137 + try { 1.138 + mm.loadFrameScript(kPaymentShimFile, true, true); 1.139 + mm.sendAsyncMessage("Payment:LoadShim", { requestId: aRequestId }); 1.140 + } catch (e) { 1.141 + if (this._debug) { 1.142 + this.LOG("Error loading " + kPaymentShimFile + " as a frame script: " 1.143 + + e); 1.144 + } 1.145 + _error("ERROR_LOADING_PAYMENT_SHIM"); 1.146 + } finally { 1.147 + SystemAppProxy.removeEventListener("mozContentEvent", this._loadPaymentShim); 1.148 + this._loadPaymentShim = null; 1.149 + } 1.150 + }).bind(this); 1.151 + SystemAppProxy.addEventListener("mozContentEvent", this._loadPaymentShim); 1.152 + 1.153 + // We also listen for UI notifications about a closed payment flow. The UI 1.154 + // should provide the reason of the closure within the 'errorMsg' parameter 1.155 + this._notifyPayFlowClosed = (function _notifyPayFlowClosed(evt) { 1.156 + let msg = evt.detail; 1.157 + if (msg.id != id) { 1.158 + return; 1.159 + } 1.160 + 1.161 + if (msg.type != 'cancel') { 1.162 + return; 1.163 + } 1.164 + 1.165 + if (msg.errorMsg) { 1.166 + _error(msg.errorMsg); 1.167 + } 1.168 + SystemAppProxy.removeEventListener("mozContentEvent", 1.169 + this._notifyPayFlowClosed); 1.170 + this._notifyPayFlowClosed = null; 1.171 + }).bind(this); 1.172 + SystemAppProxy.addEventListener("mozContentEvent", 1.173 + this._notifyPayFlowClosed); 1.174 + 1.175 + SystemAppProxy.dispatchEvent(detail); 1.176 + }, 1.177 + 1.178 + cleanup: function cleanup() { 1.179 + if (this._handleSelection) { 1.180 + SystemAppProxy.removeEventListener("mozContentEvent", this._handleSelection); 1.181 + this._handleSelection = null; 1.182 + } 1.183 + 1.184 + if (this._notifyPayFlowClosed) { 1.185 + SystemAppProxy.removeEventListener("mozContentEvent", this._notifyPayFlowClosed); 1.186 + this._notifyPayFlowClosed = null; 1.187 + } 1.188 + 1.189 + if (this._loadPaymentShim) { 1.190 + SystemAppProxy.removeEventListener("mozContentEvent", this._loadPaymentShim); 1.191 + this._loadPaymentShim = null; 1.192 + } 1.193 + }, 1.194 + 1.195 + getRandomId: function getRandomId() { 1.196 + return uuidgen.generateUUID().toString(); 1.197 + }, 1.198 + 1.199 + LOG: function LOG(s) { 1.200 + if (!this._debug) { 1.201 + return; 1.202 + } 1.203 + dump("-*- PaymentGlue: " + s + "\n"); 1.204 + }, 1.205 + 1.206 + classID: Components.ID("{8b83eabc-7929-47f4-8b48-4dea8d887e4b}"), 1.207 + 1.208 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIPaymentUIGlue]) 1.209 +} 1.210 + 1.211 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PaymentUI]);