Bug 813756 - Implement payments frontend for Android. r=mfinkle

This commit is contained in:
Wes Johnston 2013-04-25 15:29:17 -07:00
parent 92fb0e58d2
commit 8ee64504fe
9 changed files with 190 additions and 1 deletions

View File

@ -721,6 +721,12 @@ pref("browser.contentHandlers.types.3.type", "application/vnd.mozilla.maybe.feed
pref("media.webaudio.enabled", true);
#endif
pref("dom.payment.provider.0.name", "Firefox Marketplace");
pref("dom.payment.provider.0.description", "marketplace.firefox.com");
pref("dom.payment.provider.0.uri", "https://marketplace.firefox.com/mozpay/?req=");
pref("dom.payment.provider.0.type", "mozilla/payments/pay/v1");
pref("dom.payment.provider.0.requestMethod", "GET");
// This needs more tests and stability fixes first, as well as UI.
pref("media.navigator.enabled", false);
pref("media.peerconnection.enabled", false);

View File

@ -15,6 +15,7 @@ Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/AddonManager.jsm");
Cu.import("resource://gre/modules/FileUtils.jsm");
Cu.import("resource://gre/modules/JNI.jsm");
Cu.import('resource://gre/modules/Payment.jsm');
#ifdef ACCESSIBILITY
Cu.import("resource://gre/modules/accessibility/AccessFu.jsm");

View File

@ -31,6 +31,7 @@ EXTRA_COMPONENTS = \
BlocklistPrompt.js \
NSSDialogService.js \
SiteSpecificUserAgent.js \
PaymentsUI.js \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@ -94,3 +94,8 @@ contract @mozilla.org/nsClientAuthDialogs;1 {cbc08081-49b6-4561-9c18-a7707a50bda
# SiteSpecificUserAgent.js
component {d5234c9d-0ee2-4b3c-9da3-18be9e5cf7e6} SiteSpecificUserAgent.js
contract @mozilla.org/dom/site-specific-user-agent;1 {d5234c9d-0ee2-4b3c-9da3-18be9e5cf7e6}
# PaymentsUI.js
component {3c6c9575-f57e-427b-a8aa-57bc3cbff48f} PaymentsUI.js
contract @mozilla.org/payment/ui-glue;1 {3c6c9575-f57e-427b-a8aa-57bc3cbff48f}

View File

@ -0,0 +1,164 @@
/* 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");
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
"@mozilla.org/childprocessmessagemanager;1",
"nsIMessageSender");
function paymentSuccess(aRequestId) {
return paymentCallback(aRequestId, "Payment:Success");
}
function paymentFailed(aRequestId) {
return paymentCallback(aRequestId, "Payment:Failed");
}
function paymentCallback(aRequestId, aMsg) {
return function(aResult) {
closePaymentTab(aRequestId, function() {
cpmm.sendAsyncMessage(aMsg, { result: aResult,
requestId: aRequestId });
});
}
}
let paymentTabs = {};
function closePaymentTab(aId, aCallback) {
if (paymentTabs[aId]) {
// We ask the UI to close the selected payment flow.
let content = Services.wm.getMostRecentWindow("navigator:browser");
if (content) {
content.BrowserApp.closeTab(paymentTabs[aId]);
}
paymentTabs[aId] = null;
}
aCallback();
}
function PaymentUI() {
}
PaymentUI.prototype = {
get bundle() {
delete this.bundle;
return this.bundle = Services.strings.createBundle("chrome://browser/locale/payments.properties");
},
sendMessageToJava: function(aMsg) {
let data = Cc["@mozilla.org/android/bridge;1"].getService(Ci.nsIAndroidBridge).handleGeckoMessage(JSON.stringify(aMsg));
return JSON.parse(data);
},
confirmPaymentRequest: function confirmPaymentRequest(aRequestId,
aRequests,
aSuccessCb,
aErrorCb) {
let _error = this._error(aErrorCb);
let listItems = [];
// If there's only one payment provider that will work, just move on without prompting the user.
if (aRequests.length == 1) {
aSuccessCb.onresult(aRequestId, aRequests[0].wrappedJSObject.type);
return;
}
// Otherwise, let the user select a payment provider from a list.
for (let i = 0; i < aRequests.length; i++) {
let request = aRequests[i].wrappedJSObject;
let requestText = request.providerName;
if (request.productPrice) {
requestText += " (" + request.productPrice[0].amount + " " +
request.productPrice[0].currency + ")";
}
listItems.push({
label: requestText,
isGroup: false,
inGroup: false,
disabled: false,
id: i
});
}
let result = this.sendMessageToJava({
type: "Prompt:Show",
title: this.bundle.GetStringFromName("payments.providerdialog.title"),
multiple: false,
selected: [],
listItems: listItems,
});
if (result.button > -1 && aSuccessCb) {
aSuccessCb.onresult(aRequestId, aRequests[result.button].wrappedJSObject.type);
} else {
_error(aRequestId, "USER_CANCELED");
}
},
_error: function(aCallback) {
return function _error(id, msg) {
if (aCallback) {
aCallback.onresult(id, msg);
}
};
},
showPaymentFlow: function showPaymentFlow(aRequestId,
aPaymentFlowInfo,
aErrorCb) {
let _error = this._error(aErrorCb);
// We ask the UI to browse to the selected payment flow.
let content = Services.wm.getMostRecentWindow("navigator:browser");
if (!content) {
_error(aRequestId, "NO_CONTENT_WINDOW");
return;
}
// TODO: For now, known payment providers (BlueVia and Mozilla Market)
// only accepts the JWT by GET, so we just add it to the URI.
// https://github.com/mozilla-b2g/gaia/blob/master/apps/system/js/payment.js
let tab = content.BrowserApp.addTab(aPaymentFlowInfo.uri + aPaymentFlowInfo.jwt);
// Inject paymentSuccess and paymentFailed methods into the document after its loaded.
tab.browser.addEventListener("DOMContentLoaded", function loadPaymentShim() {
let frame = tab.browser.contentDocument.defaultView;
try {
frame.wrappedJSObject.paymentSuccess = paymentSuccess(aRequestId);
frame.wrappedJSObject.paymentFailed = paymentFailed(aRequestId);
} catch (e) {
_error(aRequestId, "ERROR_ADDING_METHODS");
} finally {
tab.browser.removeEventListener("DOMContentLoaded", loadPaymentShim);
}
}, false);
// fail the payment if the tab is closed on its own
tab.browser.addEventListener("TabClose", function paymentCanceled() {
paymentFailed(aRequestId)();
});
// Store a reference to the tab so that we can close it when the payment succeeds or fails.
paymentTabs[aRequestId] = tab;
},
cleanup: function cleanup() {
// Nothing to do here.
},
classID: Components.ID("{3c6c9575-f57e-427b-a8aa-57bc3cbff48f}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIPaymentUIGlue])
}
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PaymentUI]);

View File

@ -55,3 +55,6 @@ MOZ_APP_STATIC_INI=1
MOZ_FENNEC=1
MOZ_FOLD_LIBS=1
# Enable navigator.mozPay
MOZ_PAY=1

View File

@ -129,6 +129,7 @@
@BINPATH@/components/dom_offline.xpt
@BINPATH@/components/dom_json.xpt
@BINPATH@/components/dom_browserelement.xpt
@BINPATH@/components/dom_payment.xpt
@BINPATH@/components/dom_power.xpt
@BINPATH@/components/dom_quota.xpt
@BINPATH@/components/dom_range.xpt
@ -559,7 +560,13 @@ bin/components/@DLL_PREFIX@nkgnomevfs@DLL_SUFFIX@
@BINPATH@/components/PromptService.js
@BINPATH@/components/SessionStore.js
@BINPATH@/components/Sidebar.js
@BINPATH@/components/SiteSpecificUserAgent.js
@BINPATH@/components/Payment.js
@BINPATH@/components/PaymentFlowInfo.js
@BINPATH@/components/PaymentRequestInfo.js
@BINPATH@/components/Payment.manifest
@BINPATH@/components/PaymentsUI.js
#ifdef MOZ_SAFE_BROWSING
@BINPATH@/components/SafeBrowsing.jsm
#endif

View File

@ -0,0 +1 @@
payments.providerdialog.title="Pay using"

View File

@ -28,6 +28,7 @@
locale/@AB_CD@/browser/prompt.dtd (%chrome/prompt.dtd)
locale/@AB_CD@/browser/feedback.dtd (%chrome/feedback.dtd)
locale/@AB_CD@/browser/phishing.dtd (%chrome/phishing.dtd)
locale/@AB_CD@/browser/payments.properties (%chrome/payments.properties)
# overrides for toolkit l10n, also for en-US
relativesrcdir toolkit/locales: