Bug 815148 - r=fabrice

This commit is contained in:
Fernando Jiménez 2012-12-03 21:44:58 +01:00
parent d355423782
commit da970e35bc
4 changed files with 84 additions and 49 deletions

View File

@ -24,15 +24,25 @@ XPCOMUtils.defineLazyServiceGetter(this, "uuidgen",
const kClosePaymentFlowEvent = "close-payment-flow-dialog";
let requestId;
function paymentSuccess(aResult) {
closePaymentFlowDialog(function notifySuccess() {
cpmm.sendAsyncMessage("Payment:Success", { result: aResult });
if (!requestId) {
return;
}
cpmm.sendAsyncMessage("Payment:Success", { result: aResult,
requestId: requestId });
});
}
function paymentFailed(aErrorMsg) {
closePaymentFlowDialog(function notifyError() {
cpmm.sendAsyncMessage("Payment:Failed", { errorMsg: aErrorMsg });
if (!requestId) {
return;
}
cpmm.sendAsyncMessage("Payment:Failed", { errorMsg: aErrorMsg,
requestId: requestId });
});
}
@ -75,6 +85,12 @@ function closePaymentFlowDialog(aCallback) {
browser.shell.sendChromeEvent(detail);
}
// We save the identifier of the DOM request, so we can dispatch the results
// of the payment flow to the appropriate content process.
addMessageListener("Payment:LoadShim", function receiveMessage(aMessage) {
requestId = aMessage.json.requestId;
});
addEventListener("DOMContentLoaded", function(e) {
content.wrappedJSObject.paymentSuccess = paymentSuccess;
content.wrappedJSObject.paymentFailed = paymentFailed;

View File

@ -30,12 +30,13 @@ function PaymentUI() {
PaymentUI.prototype = {
confirmPaymentRequest: function confirmPaymentRequest(aRequests,
confirmPaymentRequest: function confirmPaymentRequest(aRequestId,
aRequests,
aSuccessCb,
aErrorCb) {
let _error = function _error(errorMsg) {
if (aErrorCb) {
aErrorCb.onresult(errorMsg);
aErrorCb.onresult(aRequestId, errorMsg);
}
};
@ -66,7 +67,7 @@ PaymentUI.prototype = {
}
if (msg.userSelection && aSuccessCb) {
aSuccessCb.onresult(msg.userSelection);
aSuccessCb.onresult(aRequestId, msg.userSelection);
} else if (msg.errorMsg) {
_error(msg.errorMsg);
}
@ -77,12 +78,12 @@ PaymentUI.prototype = {
browser.shell.sendChromeEvent(detail);
},
showPaymentFlow: function showPaymentFlow(aPaymentFlowInfo, aErrorCb) {
debug("showPaymentFlow. uri " + aPaymentFlowInfo.uri);
showPaymentFlow: function showPaymentFlow(aRequestId,
aPaymentFlowInfo,
aErrorCb) {
let _error = function _error(errorMsg) {
if (aErrorCb) {
aErrorCb.onresult(errorMsg);
aErrorCb.onresult(aRequestId, errorMsg);
}
};
@ -124,6 +125,7 @@ PaymentUI.prototype = {
let mm = frameLoader.messageManager;
try {
mm.loadFrameScript(kPaymentShimFile, true);
mm.sendAsyncMessage("Payment:LoadShim", { requestId: aRequestId });
} catch (e) {
debug("Error loading " + kPaymentShimFile + " as a frame script: " + e);
_error("ERROR_LOADING_PAYMENT_SHIM");

View File

@ -31,8 +31,6 @@ function debug (s) {
let PaymentManager = {
init: function init() {
this.requestId = null;
// Payment providers data are stored as a preference.
this.registeredProviders = null;
@ -53,10 +51,6 @@ let PaymentManager = {
let msg = aMessage.json;
debug("Received '" + name + "' message from content process");
if (msg.requestId) {
this.requestId = msg.requestId;
}
switch (name) {
case "Payment:Pay": {
// First of all, we register the payment providers.
@ -67,7 +61,8 @@ let PaymentManager = {
// We save the message target message manager so we can later dispatch
// back messages without broadcasting to all child processes.
this.messageManagers[this.requestId] = aMessage.target;
let requestId = msg.requestId;
this.messageManagers[requestId] = aMessage.target;
// We check the jwt type and look for a match within the
// registered payment providers to get the correct payment request
@ -75,7 +70,7 @@ let PaymentManager = {
let paymentRequests = [];
let jwtTypes = [];
for (let i in msg.jwts) {
let pr = this.getPaymentRequestInfo(msg.jwts[i]);
let pr = this.getPaymentRequestInfo(requestId, msg.jwts[i]);
if (!pr) {
continue;
}
@ -84,7 +79,8 @@ let PaymentManager = {
}
// We consider jwt type repetition an error.
if (jwtTypes[pr.type]) {
this.paymentFailed("PAY_REQUEST_ERROR_DUPLICATED_JWT_TYPE");
this.paymentFailed(requestId,
"PAY_REQUEST_ERROR_DUPLICATED_JWT_TYPE");
return;
}
jwtTypes[pr.type] = true;
@ -92,7 +88,8 @@ let PaymentManager = {
}
if (!paymentRequests.length) {
this.paymentFailed("PAY_REQUEST_ERROR_NO_VALID_REQUEST_FOUND");
this.paymentFailed(requestId,
"PAY_REQUEST_ERROR_NO_VALID_REQUEST_FOUND");
return;
}
@ -104,17 +101,20 @@ let PaymentManager = {
.createInstance(Ci.nsIPaymentUIGlue);
if (!glue) {
debug("Could not create nsIPaymentUIGlue instance");
this.paymentFailed("INTERNAL_ERROR_CREATE_PAYMENT_GLUE_FAILED");
this.paymentFailed(requestId,
"INTERNAL_ERROR_CREATE_PAYMENT_GLUE_FAILED");
return;
}
let confirmPaymentSuccessCb = function successCb(aResult) {
let confirmPaymentSuccessCb = function successCb(aRequestId,
aResult) {
// Get the appropriate payment provider data based on user's choice.
let selectedProvider = this.registeredProviders[aResult];
if (!selectedProvider || !selectedProvider.uri) {
debug("Could not retrieve a valid provider based on user's " +
"selection");
this.paymentFailed("INTERNAL_ERROR_NO_VALID_SELECTED_PROVIDER");
this.paymentFailed(aRequestId,
"INTERNAL_ERROR_NO_VALID_SELECTED_PROVIDER");
return;
}
@ -127,25 +127,27 @@ let PaymentManager = {
}
if (!jwt) {
debug("The selected request has no JWT information associated");
this.paymentFailed("INTERNAL_ERROR_NO_JWT_ASSOCIATED_TO_REQUEST");
this.paymentFailed(aRequestId,
"INTERNAL_ERROR_NO_JWT_ASSOCIATED_TO_REQUEST");
return;
}
this.showPaymentFlow(selectedProvider, jwt);
this.showPaymentFlow(aRequestId, selectedProvider, jwt);
};
let confirmPaymentErrorCb = this.paymentFailed;
glue.confirmPaymentRequest(paymentRequests,
glue.confirmPaymentRequest(requestId,
paymentRequests,
confirmPaymentSuccessCb.bind(this),
confirmPaymentErrorCb.bind(this));
break;
}
case "Payment:Success":
case "Payment:Failed": {
let mm = this.messageManagers[this.requestId];
let mm = this.messageManagers[msg.requestId];
mm.sendAsyncMessage(name, {
requestId: this.requestId,
requestId: msg.requestId,
result: msg.result,
errorMsg: msg.errorMsg
});
@ -203,10 +205,10 @@ let PaymentManager = {
/**
* Helper for sending a Payment:Failed message to the parent process.
*/
paymentFailed: function paymentFailed(aErrorMsg) {
let mm = this.messageManagers[this.requestId];
paymentFailed: function paymentFailed(aRequestId, aErrorMsg) {
let mm = this.messageManagers[aRequestId];
mm.sendAsyncMessage("Payment:Failed", {
requestId: this.requestId,
requestId: aRequestId,
errorMsg: aErrorMsg
});
},
@ -215,9 +217,9 @@ let PaymentManager = {
* Helper function to get the payment request info according to the jwt
* type. Payment provider's data is stored as a preference.
*/
getPaymentRequestInfo: function getPaymentRequestInfo(aJwt) {
getPaymentRequestInfo: function getPaymentRequestInfo(aRequestId, aJwt) {
if (!aJwt) {
this.paymentFailed("INTERNAL_ERROR_CALL_WITH_MISSING_JWT");
this.paymentFailed(aRequestId, "INTERNAL_ERROR_CALL_WITH_MISSING_JWT");
return true;
}
@ -230,7 +232,8 @@ let PaymentManager = {
if (segments.length !== 3) {
debug("Error getting payment provider's uri. " +
"Not enough or too many segments");
this.paymentFailed("PAY_REQUEST_ERROR_WRONG_SEGMENTS_COUNT");
this.paymentFailed(aRequestId,
"PAY_REQUEST_ERROR_WRONG_SEGMENTS_COUNT");
return true;
}
@ -242,7 +245,7 @@ let PaymentManager = {
let payload = atob(segments[1]);
debug("Payload " + payload);
if (!payload.length) {
this.paymentFailed("PAY_REQUEST_ERROR_EMPTY_PAYLOAD");
this.paymentFailed(aRequestId, "PAY_REQUEST_ERROR_EMPTY_PAYLOAD");
return true;
}
@ -258,21 +261,25 @@ let PaymentManager = {
payloadObject = JSON.parse(payload);
if (!payloadObject) {
this.paymentFailed("PAY_REQUEST_ERROR_ERROR_PARSING_JWT_PAYLOAD");
this.paymentFailed(aRequestId,
"PAY_REQUEST_ERROR_ERROR_PARSING_JWT_PAYLOAD");
return true;
}
} catch (e) {
this.paymentFailed("PAY_REQUEST_ERROR_ERROR_DECODING_JWT");
this.paymentFailed(aRequestId,
"PAY_REQUEST_ERROR_ERROR_DECODING_JWT");
return true;
}
if (!payloadObject.typ) {
this.paymentFailed("PAY_REQUEST_ERROR_NO_TYP_PARAMETER");
this.paymentFailed(aRequestId,
"PAY_REQUEST_ERROR_NO_TYP_PARAMETER");
return true;
}
if (!payloadObject.request) {
this.paymentFailed("PAY_REQUEST_ERROR_NO_REQUEST_PARAMETER");
this.paymentFailed(aRequestId,
"PAY_REQUEST_ERROR_NO_REQUEST_PARAMETER");
return true;
}
@ -290,7 +297,8 @@ let PaymentManager = {
}
if (!provider.uri || !provider.name) {
this.paymentFailed("INTERNAL_ERROR_WRONG_REGISTERED_PAY_PROVIDER");
this.paymentFailed(aRequestId,
"INTERNAL_ERROR_WRONG_REGISTERED_PAY_PROVIDER");
return true;
}
@ -298,7 +306,8 @@ let PaymentManager = {
if (!/^https/.exec(provider.uri.toLowerCase())) {
// We should never get this far.
debug("Payment provider uris must be https: " + provider.uri);
this.paymentFailed("INTERNAL_ERROR_NON_HTTPS_PROVIDER_URI");
this.paymentFailed(aRequestId,
"INTERNAL_ERROR_NON_HTTPS_PROVIDER_URI");
return true;
}
@ -306,7 +315,8 @@ let PaymentManager = {
let request = Cc["@mozilla.org/payment/request-info;1"]
.createInstance(Ci.nsIDOMPaymentRequestInfo);
if (!request) {
this.paymentFailed("INTERNAL_ERROR_ERROR_CREATING_PAY_REQUEST");
this.paymentFailed(aRequestId,
"INTERNAL_ERROR_ERROR_CREATING_PAY_REQUEST");
return true;
}
request.wrappedJSObject.init(aJwt,
@ -315,7 +325,9 @@ let PaymentManager = {
return request;
},
showPaymentFlow: function showPaymentFlow(aPaymentProvider, aJwt) {
showPaymentFlow: function showPaymentFlow(aRequestId,
aPaymentProvider,
aJwt) {
let paymentFlowInfo = Cc["@mozilla.org/payment/flow-info;1"]
.createInstance(Ci.nsIPaymentFlowInfo);
paymentFlowInfo.uri = aPaymentProvider.uri;
@ -326,10 +338,13 @@ let PaymentManager = {
.createInstance(Ci.nsIPaymentUIGlue);
if (!glue) {
debug("Could not create nsIPaymentUIGlue instance");
this.paymentFailed("INTERNAL_ERROR_CREATE_PAYMENT_GLUE_FAILED");
this.paymentFailed(aRequestId,
"INTERNAL_ERROR_CREATE_PAYMENT_GLUE_FAILED");
return false;
}
glue.showPaymentFlow(paymentFlowInfo, this.paymentFailed.bind(this));
glue.showPaymentFlow(aRequestId,
paymentFlowInfo,
this.paymentFailed.bind(this));
},
// nsIObserver

View File

@ -6,22 +6,24 @@
interface nsIPaymentFlowInfo;
[scriptable, function, uuid(ca475754-6852-49a2-97e8-8a94cc7a453f)]
[scriptable, function, uuid(b9afa678-71a5-4975-bcdb-0c4098730eff)]
interface nsIPaymentUIGlueCallback : nsISupports
{
void onresult(in DOMString result);
void onresult(in DOMString requestId, in DOMString result);
};
[scriptable, uuid(344e5442-7cc2-4636-bc42-e2249cb67813)]
[scriptable, uuid(4dda9aa0-df88-4dcd-a583-199e516fa438)]
interface nsIPaymentUIGlue : nsISupports
{
// The 'paymentRequestsInfo' contains the payment request information
// for each JWT provided via navigator.mozPay call.
void confirmPaymentRequest(in jsval paymentRequestsInfo,
void confirmPaymentRequest(in DOMString requestId,
in jsval paymentRequestsInfo,
in nsIPaymentUIGlueCallback successCb,
in nsIPaymentUIGlueCallback errorCb);
void showPaymentFlow(in nsIPaymentFlowInfo paymentFlowInfo,
void showPaymentFlow(in DOMString requestId,
in nsIPaymentFlowInfo paymentFlowInfo,
in nsIPaymentUIGlueCallback errorCb);
void cleanup();