dom/payment/Payment.js

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
michael@0 8
michael@0 9 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 10 Cu.import("resource://gre/modules/Services.jsm");
michael@0 11 Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
michael@0 12
michael@0 13 const PAYMENTCONTENTHELPER_CID =
michael@0 14 Components.ID("{a920adc0-c36e-4fd0-8de0-aac1ac6ebbd0}");
michael@0 15
michael@0 16 const PAYMENT_IPC_MSG_NAMES = ["Payment:Success",
michael@0 17 "Payment:Failed"];
michael@0 18
michael@0 19 const PREF_DEBUG = "dom.payment.debug";
michael@0 20
michael@0 21 XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
michael@0 22 "@mozilla.org/childprocessmessagemanager;1",
michael@0 23 "nsIMessageSender");
michael@0 24
michael@0 25 function PaymentContentHelper() {
michael@0 26 };
michael@0 27
michael@0 28 PaymentContentHelper.prototype = {
michael@0 29 __proto__: DOMRequestIpcHelper.prototype,
michael@0 30
michael@0 31 QueryInterface: XPCOMUtils.generateQI([Ci.nsINavigatorPayment,
michael@0 32 Ci.nsIDOMGlobalPropertyInitializer,
michael@0 33 Ci.nsISupportsWeakReference,
michael@0 34 Ci.nsIObserver]),
michael@0 35 classID: PAYMENTCONTENTHELPER_CID,
michael@0 36 classInfo: XPCOMUtils.generateCI({
michael@0 37 classID: PAYMENTCONTENTHELPER_CID,
michael@0 38 contractID: "@mozilla.org/payment/content-helper;1",
michael@0 39 classDescription: "Payment Content Helper",
michael@0 40 flags: Ci.nsIClassInfo.DOM_OBJECT,
michael@0 41 interfaces: [Ci.nsINavigatorPayment]
michael@0 42 }),
michael@0 43
michael@0 44 // nsINavigatorPayment
michael@0 45
michael@0 46 pay: function pay(aJwts) {
michael@0 47 let request = this.createRequest();
michael@0 48 let requestId = this.getRequestId(request);
michael@0 49
michael@0 50 let docShell = this._window.QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 51 .getInterface(Ci.nsIWebNavigation)
michael@0 52 .QueryInterface(Ci.nsIDocShell);
michael@0 53 if (!docShell.isActive) {
michael@0 54 if (this._debug) {
michael@0 55 this.LOG("The caller application is a background app. No request " +
michael@0 56 "will be sent");
michael@0 57 }
michael@0 58 let runnable = {
michael@0 59 run: function run() {
michael@0 60 Services.DOMRequest.fireError(request, "BACKGROUND_APP");
michael@0 61 }
michael@0 62 }
michael@0 63 Services.tm.currentThread.dispatch(runnable,
michael@0 64 Ci.nsIThread.DISPATCH_NORMAL);
michael@0 65 return request;
michael@0 66 }
michael@0 67
michael@0 68 if (!Array.isArray(aJwts)) {
michael@0 69 aJwts = [aJwts];
michael@0 70 }
michael@0 71
michael@0 72 cpmm.sendAsyncMessage("Payment:Pay", {
michael@0 73 jwts: aJwts,
michael@0 74 requestId: requestId
michael@0 75 });
michael@0 76 return request;
michael@0 77 },
michael@0 78
michael@0 79 // nsIDOMGlobalPropertyInitializer
michael@0 80
michael@0 81 init: function(aWindow) {
michael@0 82 try {
michael@0 83 if (!Services.prefs.getBoolPref("dom.mozPay.enabled")) {
michael@0 84 return null;
michael@0 85 }
michael@0 86 } catch (e) {
michael@0 87 return null;
michael@0 88 }
michael@0 89
michael@0 90 this._window = aWindow;
michael@0 91 this.initDOMRequestHelper(aWindow, PAYMENT_IPC_MSG_NAMES);
michael@0 92
michael@0 93 try {
michael@0 94 this._debug =
michael@0 95 Services.prefs.getPrefType(PREF_DEBUG) == Ci.nsIPrefBranch.PREF_BOOL
michael@0 96 && Services.prefs.getBoolPref(PREF_DEBUG);
michael@0 97 } catch(e) {
michael@0 98 this._debug = false;
michael@0 99 }
michael@0 100
michael@0 101 return this.pay.bind(this);
michael@0 102 },
michael@0 103
michael@0 104 // nsIFrameMessageListener
michael@0 105
michael@0 106 receiveMessage: function receiveMessage(aMessage) {
michael@0 107 let name = aMessage.name;
michael@0 108 let msg = aMessage.json;
michael@0 109 if (this._debug) {
michael@0 110 this.LOG("Received message '" + name + "': " + JSON.stringify(msg));
michael@0 111 }
michael@0 112 let requestId = msg.requestId;
michael@0 113 let request = this.takeRequest(requestId);
michael@0 114 if (!request) {
michael@0 115 return;
michael@0 116 }
michael@0 117 switch (name) {
michael@0 118 case "Payment:Success":
michael@0 119 Services.DOMRequest.fireSuccess(request, msg.result);
michael@0 120 break;
michael@0 121 case "Payment:Failed":
michael@0 122 Services.DOMRequest.fireError(request, msg.errorMsg);
michael@0 123 break;
michael@0 124 }
michael@0 125 },
michael@0 126
michael@0 127 LOG: function LOG(s) {
michael@0 128 if (!this._debug) {
michael@0 129 return;
michael@0 130 }
michael@0 131 dump("-*- PaymentContentHelper: " + s + "\n");
michael@0 132 }
michael@0 133 };
michael@0 134
michael@0 135 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PaymentContentHelper]);

mercurial