2012-08-30 01:41:34 +04:00
|
|
|
/* 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"];
|
|
|
|
|
2013-09-06 20:43:54 +04:00
|
|
|
const PREF_DEBUG = "dom.payment.debug";
|
|
|
|
|
2012-08-30 01:41:34 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
|
|
|
"nsIMessageSender");
|
|
|
|
|
|
|
|
function PaymentContentHelper() {
|
|
|
|
};
|
|
|
|
|
|
|
|
PaymentContentHelper.prototype = {
|
|
|
|
__proto__: DOMRequestIpcHelper.prototype,
|
|
|
|
|
2013-05-07 00:37:50 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsINavigatorPayment,
|
2013-07-09 01:55:42 +04:00
|
|
|
Ci.nsIDOMGlobalPropertyInitializer,
|
|
|
|
Ci.nsISupportsWeakReference]),
|
2012-08-30 01:41:34 +04:00
|
|
|
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,
|
2013-05-07 00:37:50 +04:00
|
|
|
interfaces: [Ci.nsINavigatorPayment]
|
2012-08-30 01:41:34 +04:00
|
|
|
}),
|
|
|
|
|
2013-05-07 00:37:50 +04:00
|
|
|
// nsINavigatorPayment
|
2012-08-30 01:41:34 +04:00
|
|
|
|
|
|
|
pay: function pay(aJwts) {
|
|
|
|
let request = this.createRequest();
|
|
|
|
let requestId = this.getRequestId(request);
|
2012-09-21 11:20:05 +04:00
|
|
|
|
|
|
|
let docShell = this._window.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsIDocShell);
|
|
|
|
if (!docShell.isActive) {
|
2013-09-06 20:43:54 +04:00
|
|
|
if (this._debug) {
|
|
|
|
this.LOG("The caller application is a background app. No request " +
|
|
|
|
"will be sent");
|
|
|
|
}
|
2012-09-21 11:20:05 +04:00
|
|
|
let runnable = {
|
|
|
|
run: function run() {
|
|
|
|
Services.DOMRequest.fireError(request, "BACKGROUND_APP");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Services.tm.currentThread.dispatch(runnable,
|
|
|
|
Ci.nsIThread.DISPATCH_NORMAL);
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
2012-08-30 01:41:34 +04:00
|
|
|
if (!Array.isArray(aJwts)) {
|
|
|
|
aJwts = [aJwts];
|
|
|
|
}
|
2012-09-21 11:20:05 +04:00
|
|
|
|
2012-08-30 01:41:34 +04:00
|
|
|
cpmm.sendAsyncMessage("Payment:Pay", {
|
|
|
|
jwts: aJwts,
|
|
|
|
requestId: requestId
|
|
|
|
});
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsIDOMGlobalPropertyInitializer
|
|
|
|
|
|
|
|
init: function(aWindow) {
|
2013-09-27 22:38:14 +04:00
|
|
|
try {
|
|
|
|
if (!Services.prefs.getBoolPref("dom.mozPay.enabled")) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-09-21 11:20:05 +04:00
|
|
|
this._window = aWindow;
|
2013-07-09 01:55:42 +04:00
|
|
|
this.initDOMRequestHelper(aWindow, PAYMENT_IPC_MSG_NAMES);
|
2013-09-06 20:43:54 +04:00
|
|
|
|
|
|
|
try {
|
|
|
|
this._debug =
|
|
|
|
Services.prefs.getPrefType(PREF_DEBUG) == Ci.nsIPrefBranch.PREF_BOOL
|
|
|
|
&& Services.prefs.getBoolPref(PREF_DEBUG);
|
|
|
|
} catch(e) {
|
|
|
|
this._debug = false;
|
|
|
|
}
|
|
|
|
|
2012-08-30 01:41:34 +04:00
|
|
|
return this.pay.bind(this);
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsIFrameMessageListener
|
|
|
|
|
|
|
|
receiveMessage: function receiveMessage(aMessage) {
|
|
|
|
let name = aMessage.name;
|
|
|
|
let msg = aMessage.json;
|
2013-09-06 20:43:54 +04:00
|
|
|
if (this._debug) {
|
|
|
|
this.LOG("Received message '" + name + "': " + JSON.stringify(msg));
|
|
|
|
}
|
2012-08-30 01:41:34 +04:00
|
|
|
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;
|
|
|
|
}
|
2013-09-06 20:43:54 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
LOG: function LOG(s) {
|
|
|
|
if (!this._debug) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dump("-*- PaymentContentHelper: " + s + "\n");
|
2012-08-30 01:41:34 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PaymentContentHelper]);
|