michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: const {classes: Cc, interfaces: Ci, utils: Cu} = Components; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/DOMRequestHelper.jsm"); michael@0: michael@0: const PAYMENTCONTENTHELPER_CID = michael@0: Components.ID("{a920adc0-c36e-4fd0-8de0-aac1ac6ebbd0}"); michael@0: michael@0: const PAYMENT_IPC_MSG_NAMES = ["Payment:Success", michael@0: "Payment:Failed"]; michael@0: michael@0: const PREF_DEBUG = "dom.payment.debug"; michael@0: michael@0: XPCOMUtils.defineLazyServiceGetter(this, "cpmm", michael@0: "@mozilla.org/childprocessmessagemanager;1", michael@0: "nsIMessageSender"); michael@0: michael@0: function PaymentContentHelper() { michael@0: }; michael@0: michael@0: PaymentContentHelper.prototype = { michael@0: __proto__: DOMRequestIpcHelper.prototype, michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsINavigatorPayment, michael@0: Ci.nsIDOMGlobalPropertyInitializer, michael@0: Ci.nsISupportsWeakReference, michael@0: Ci.nsIObserver]), michael@0: classID: PAYMENTCONTENTHELPER_CID, michael@0: classInfo: XPCOMUtils.generateCI({ michael@0: classID: PAYMENTCONTENTHELPER_CID, michael@0: contractID: "@mozilla.org/payment/content-helper;1", michael@0: classDescription: "Payment Content Helper", michael@0: flags: Ci.nsIClassInfo.DOM_OBJECT, michael@0: interfaces: [Ci.nsINavigatorPayment] michael@0: }), michael@0: michael@0: // nsINavigatorPayment michael@0: michael@0: pay: function pay(aJwts) { michael@0: let request = this.createRequest(); michael@0: let requestId = this.getRequestId(request); michael@0: michael@0: let docShell = this._window.QueryInterface(Ci.nsIInterfaceRequestor) michael@0: .getInterface(Ci.nsIWebNavigation) michael@0: .QueryInterface(Ci.nsIDocShell); michael@0: if (!docShell.isActive) { michael@0: if (this._debug) { michael@0: this.LOG("The caller application is a background app. No request " + michael@0: "will be sent"); michael@0: } michael@0: let runnable = { michael@0: run: function run() { michael@0: Services.DOMRequest.fireError(request, "BACKGROUND_APP"); michael@0: } michael@0: } michael@0: Services.tm.currentThread.dispatch(runnable, michael@0: Ci.nsIThread.DISPATCH_NORMAL); michael@0: return request; michael@0: } michael@0: michael@0: if (!Array.isArray(aJwts)) { michael@0: aJwts = [aJwts]; michael@0: } michael@0: michael@0: cpmm.sendAsyncMessage("Payment:Pay", { michael@0: jwts: aJwts, michael@0: requestId: requestId michael@0: }); michael@0: return request; michael@0: }, michael@0: michael@0: // nsIDOMGlobalPropertyInitializer michael@0: michael@0: init: function(aWindow) { michael@0: try { michael@0: if (!Services.prefs.getBoolPref("dom.mozPay.enabled")) { michael@0: return null; michael@0: } michael@0: } catch (e) { michael@0: return null; michael@0: } michael@0: michael@0: this._window = aWindow; michael@0: this.initDOMRequestHelper(aWindow, PAYMENT_IPC_MSG_NAMES); michael@0: michael@0: try { michael@0: this._debug = michael@0: Services.prefs.getPrefType(PREF_DEBUG) == Ci.nsIPrefBranch.PREF_BOOL michael@0: && Services.prefs.getBoolPref(PREF_DEBUG); michael@0: } catch(e) { michael@0: this._debug = false; michael@0: } michael@0: michael@0: return this.pay.bind(this); michael@0: }, michael@0: michael@0: // nsIFrameMessageListener michael@0: michael@0: receiveMessage: function receiveMessage(aMessage) { michael@0: let name = aMessage.name; michael@0: let msg = aMessage.json; michael@0: if (this._debug) { michael@0: this.LOG("Received message '" + name + "': " + JSON.stringify(msg)); michael@0: } michael@0: let requestId = msg.requestId; michael@0: let request = this.takeRequest(requestId); michael@0: if (!request) { michael@0: return; michael@0: } michael@0: switch (name) { michael@0: case "Payment:Success": michael@0: Services.DOMRequest.fireSuccess(request, msg.result); michael@0: break; michael@0: case "Payment:Failed": michael@0: Services.DOMRequest.fireError(request, msg.errorMsg); michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: LOG: function LOG(s) { michael@0: if (!this._debug) { michael@0: return; michael@0: } michael@0: dump("-*- PaymentContentHelper: " + s + "\n"); michael@0: } michael@0: }; michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PaymentContentHelper]);