Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/JNI.jsm"); |
michael@0 | 12 | |
michael@0 | 13 | XPCOMUtils.defineLazyServiceGetter(this, "cpmm", |
michael@0 | 14 | "@mozilla.org/childprocessmessagemanager;1", |
michael@0 | 15 | "nsIMessageSender"); |
michael@0 | 16 | |
michael@0 | 17 | function paymentSuccess(aRequestId) { |
michael@0 | 18 | return function(aResult) { |
michael@0 | 19 | closePaymentTab(aRequestId, function() { |
michael@0 | 20 | cpmm.sendAsyncMessage("Payment:Success", { result: aResult, |
michael@0 | 21 | requestId: aRequestId }); |
michael@0 | 22 | }); |
michael@0 | 23 | } |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | function paymentFailed(aRequestId) { |
michael@0 | 27 | return function(aErrorMsg) { |
michael@0 | 28 | closePaymentTab(aRequestId, function() { |
michael@0 | 29 | cpmm.sendAsyncMessage("Payment:Failed", { errorMsg: aErrorMsg, |
michael@0 | 30 | requestId: aRequestId }); |
michael@0 | 31 | }); |
michael@0 | 32 | } |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | let paymentTabs = {}; |
michael@0 | 36 | let cancelTabCallbacks = {}; |
michael@0 | 37 | function paymentCanceled(aRequestId) { |
michael@0 | 38 | return function() { |
michael@0 | 39 | paymentFailed(aRequestId)(); |
michael@0 | 40 | } |
michael@0 | 41 | } |
michael@0 | 42 | function closePaymentTab(aId, aCallback) { |
michael@0 | 43 | if (paymentTabs[aId]) { |
michael@0 | 44 | paymentTabs[aId].browser.removeEventListener("TabClose", cancelTabCallbacks[aId]); |
michael@0 | 45 | delete cancelTabCallbacks[aId]; |
michael@0 | 46 | |
michael@0 | 47 | // We ask the UI to close the selected payment flow. |
michael@0 | 48 | let content = Services.wm.getMostRecentWindow("navigator:browser"); |
michael@0 | 49 | if (content) { |
michael@0 | 50 | content.BrowserApp.closeTab(paymentTabs[aId]); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | paymentTabs[aId] = null; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | aCallback(); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | function PaymentUI() { |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | PaymentUI.prototype = { |
michael@0 | 63 | get bundle() { |
michael@0 | 64 | delete this.bundle; |
michael@0 | 65 | return this.bundle = Services.strings.createBundle("chrome://browser/locale/payments.properties"); |
michael@0 | 66 | }, |
michael@0 | 67 | |
michael@0 | 68 | confirmPaymentRequest: function confirmPaymentRequest(aRequestId, |
michael@0 | 69 | aRequests, |
michael@0 | 70 | aSuccessCb, |
michael@0 | 71 | aErrorCb) { |
michael@0 | 72 | let _error = this._error(aErrorCb); |
michael@0 | 73 | |
michael@0 | 74 | let listItems = []; |
michael@0 | 75 | |
michael@0 | 76 | // If there's only one payment provider that will work, just move on without prompting the user. |
michael@0 | 77 | if (aRequests.length == 1) { |
michael@0 | 78 | aSuccessCb.onresult(aRequestId, aRequests[0].type); |
michael@0 | 79 | return; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | // Otherwise, let the user select a payment provider from a list. |
michael@0 | 83 | for (let i = 0; i < aRequests.length; i++) { |
michael@0 | 84 | let request = aRequests[i]; |
michael@0 | 85 | let requestText = request.providerName; |
michael@0 | 86 | if (request.productPrice) { |
michael@0 | 87 | requestText += " (" + request.productPrice[0].amount + " " + |
michael@0 | 88 | request.productPrice[0].currency + ")"; |
michael@0 | 89 | } |
michael@0 | 90 | listItems.push({ label: requestText }); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | let p = new Prompt({ |
michael@0 | 94 | window: null, |
michael@0 | 95 | title: this.bundle.GetStringFromName("payments.providerdialog.title"), |
michael@0 | 96 | }).setSingleChoiceItems(listItems).show(function(data) { |
michael@0 | 97 | if (data.button > -1 && aSuccessCb) { |
michael@0 | 98 | aSuccessCb.onresult(aRequestId, aRequests[data.button].type); |
michael@0 | 99 | } else { |
michael@0 | 100 | _error(aRequestId, "USER_CANCELED"); |
michael@0 | 101 | } |
michael@0 | 102 | }); |
michael@0 | 103 | }, |
michael@0 | 104 | |
michael@0 | 105 | _error: function(aCallback) { |
michael@0 | 106 | return function _error(id, msg) { |
michael@0 | 107 | if (aCallback) { |
michael@0 | 108 | aCallback.onresult(id, msg); |
michael@0 | 109 | } |
michael@0 | 110 | }; |
michael@0 | 111 | }, |
michael@0 | 112 | |
michael@0 | 113 | showPaymentFlow: function showPaymentFlow(aRequestId, |
michael@0 | 114 | aPaymentFlowInfo, |
michael@0 | 115 | aErrorCb) { |
michael@0 | 116 | let _error = this._error(aErrorCb); |
michael@0 | 117 | |
michael@0 | 118 | // We ask the UI to browse to the selected payment flow. |
michael@0 | 119 | let content = Services.wm.getMostRecentWindow("navigator:browser"); |
michael@0 | 120 | if (!content) { |
michael@0 | 121 | _error(aRequestId, "NO_CONTENT_WINDOW"); |
michael@0 | 122 | return; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | // TODO: For now, known payment providers (BlueVia and Mozilla Market) |
michael@0 | 126 | // only accepts the JWT by GET, so we just add it to the URI. |
michael@0 | 127 | // https://github.com/mozilla-b2g/gaia/blob/master/apps/system/js/payment.js |
michael@0 | 128 | let tab = content.BrowserApp.addTab(aPaymentFlowInfo.uri + aPaymentFlowInfo.jwt); |
michael@0 | 129 | |
michael@0 | 130 | // Inject paymentSuccess and paymentFailed methods into the document after its loaded. |
michael@0 | 131 | tab.browser.addEventListener("DOMWindowCreated", function loadPaymentShim() { |
michael@0 | 132 | let frame = tab.browser.contentDocument.defaultView; |
michael@0 | 133 | try { |
michael@0 | 134 | frame.wrappedJSObject.mozPaymentProvider = { |
michael@0 | 135 | __exposedProps__: { |
michael@0 | 136 | paymentSuccess: 'r', |
michael@0 | 137 | paymentFailed: 'r', |
michael@0 | 138 | mnc: 'r', |
michael@0 | 139 | mcc: 'r', |
michael@0 | 140 | }, |
michael@0 | 141 | |
michael@0 | 142 | _getNetworkInfo: function(type) { |
michael@0 | 143 | let jni = new JNI(); |
michael@0 | 144 | let cls = jni.findClass("org/mozilla/gecko/GeckoNetworkManager"); |
michael@0 | 145 | let method = jni.getStaticMethodID(cls, "get" + type.toUpperCase(), "()I"); |
michael@0 | 146 | let val = jni.callStaticIntMethod(cls, method); |
michael@0 | 147 | jni.close(); |
michael@0 | 148 | |
michael@0 | 149 | if (val < 0) |
michael@0 | 150 | return null; |
michael@0 | 151 | return val; |
michael@0 | 152 | }, |
michael@0 | 153 | |
michael@0 | 154 | get mnc() { |
michael@0 | 155 | delete this.mnc; |
michael@0 | 156 | return this.mnc = this._getNetworkInfo("mnc"); |
michael@0 | 157 | }, |
michael@0 | 158 | |
michael@0 | 159 | get mcc() { |
michael@0 | 160 | delete this.mcc; |
michael@0 | 161 | return this.mcc = this._getNetworkInfo("mcc"); |
michael@0 | 162 | }, |
michael@0 | 163 | |
michael@0 | 164 | paymentSuccess: paymentSuccess(aRequestId), |
michael@0 | 165 | paymentFailed: paymentFailed(aRequestId) |
michael@0 | 166 | }; |
michael@0 | 167 | } catch (e) { |
michael@0 | 168 | _error(aRequestId, "ERROR_ADDING_METHODS"); |
michael@0 | 169 | } finally { |
michael@0 | 170 | tab.browser.removeEventListener("DOMWindowCreated", loadPaymentShim); |
michael@0 | 171 | } |
michael@0 | 172 | }, true); |
michael@0 | 173 | |
michael@0 | 174 | // Store a reference to the tab so that we can close it when the payment succeeds or fails. |
michael@0 | 175 | paymentTabs[aRequestId] = tab; |
michael@0 | 176 | cancelTabCallbacks[aRequestId] = paymentCanceled(aRequestId); |
michael@0 | 177 | |
michael@0 | 178 | // Fail the payment if the tab is closed on its own |
michael@0 | 179 | tab.browser.addEventListener("TabClose", cancelTabCallbacks[aRequestId]); |
michael@0 | 180 | }, |
michael@0 | 181 | |
michael@0 | 182 | cleanup: function cleanup() { |
michael@0 | 183 | // Nothing to do here. |
michael@0 | 184 | }, |
michael@0 | 185 | |
michael@0 | 186 | classID: Components.ID("{3c6c9575-f57e-427b-a8aa-57bc3cbff48f}"), |
michael@0 | 187 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIPaymentUIGlue]) |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PaymentUI]); |