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