diff options
author | wolfbeast <mcwerewolf@gmail.com> | 2014-05-21 11:38:25 +0200 |
---|---|---|
committer | wolfbeast <mcwerewolf@gmail.com> | 2014-05-21 11:38:25 +0200 |
commit | d25ba7d760b017b038e5aa6c0a605b4a330eb68d (patch) | |
tree | 16ec27edc7d5f83986f16236d3a36a2682a0f37e /dom/payment/Payment.js | |
parent | a942906574671868daf122284a9c4689e6924f74 (diff) | |
download | palemoon-gre-d25ba7d760b017b038e5aa6c0a605b4a330eb68d.tar.gz |
Recommit working copy to repo with proper line endings.
Diffstat (limited to 'dom/payment/Payment.js')
-rw-r--r-- | dom/payment/Payment.js | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/dom/payment/Payment.js b/dom/payment/Payment.js new file mode 100644 index 000000000..6102cc1bf --- /dev/null +++ b/dom/payment/Payment.js @@ -0,0 +1,107 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +const {classes: Cc, interfaces: Ci, utils: Cu} = Components; + +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); +Cu.import("resource://gre/modules/Services.jsm"); +Cu.import("resource://gre/modules/DOMRequestHelper.jsm"); + +const PAYMENTCONTENTHELPER_CID = + Components.ID("{a920adc0-c36e-4fd0-8de0-aac1ac6ebbd0}"); + +const PAYMENT_IPC_MSG_NAMES = ["Payment:Success", + "Payment:Failed"]; + +XPCOMUtils.defineLazyServiceGetter(this, "cpmm", + "@mozilla.org/childprocessmessagemanager;1", + "nsIMessageSender"); + +function debug (s) { + //dump("-*- PaymentContentHelper: " + s + "\n"); +}; + +function PaymentContentHelper() { +}; + +PaymentContentHelper.prototype = { + __proto__: DOMRequestIpcHelper.prototype, + + QueryInterface: XPCOMUtils.generateQI([Ci.nsINavigatorPayment, + Ci.nsIDOMGlobalPropertyInitializer]), + classID: PAYMENTCONTENTHELPER_CID, + classInfo: XPCOMUtils.generateCI({ + classID: PAYMENTCONTENTHELPER_CID, + contractID: "@mozilla.org/payment/content-helper;1", + classDescription: "Payment Content Helper", + flags: Ci.nsIClassInfo.DOM_OBJECT, + interfaces: [Ci.nsINavigatorPayment] + }), + + // nsINavigatorPayment + + pay: function pay(aJwts) { + let request = this.createRequest(); + let requestId = this.getRequestId(request); + + let docShell = this._window.QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIWebNavigation) + .QueryInterface(Ci.nsIDocShell); + if (!docShell.isActive) { + debug("The caller application is a background app. No request " + + "will be sent"); + let runnable = { + run: function run() { + Services.DOMRequest.fireError(request, "BACKGROUND_APP"); + } + } + Services.tm.currentThread.dispatch(runnable, + Ci.nsIThread.DISPATCH_NORMAL); + return request; + } + + if (!Array.isArray(aJwts)) { + aJwts = [aJwts]; + } + + cpmm.sendAsyncMessage("Payment:Pay", { + jwts: aJwts, + requestId: requestId + }); + return request; + }, + + // nsIDOMGlobalPropertyInitializer + + init: function(aWindow) { + this._window = aWindow; + this.initHelper(aWindow, PAYMENT_IPC_MSG_NAMES); + return this.pay.bind(this); + }, + + // nsIFrameMessageListener + + receiveMessage: function receiveMessage(aMessage) { + let name = aMessage.name; + let msg = aMessage.json; + debug("Received message '" + name + "': " + JSON.stringify(msg)); + let requestId = msg.requestId; + let request = this.takeRequest(requestId); + if (!request) { + return; + } + switch (name) { + case "Payment:Success": + Services.DOMRequest.fireSuccess(request, msg.result); + break; + case "Payment:Failed": + Services.DOMRequest.fireError(request, msg.errorMsg); + break; + } + } +}; + +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PaymentContentHelper]); |