Skip to content

Instantly share code, notes, and snippets.

@zaach
Created October 10, 2012 23:22
Show Gist options
  • Save zaach/3869189 to your computer and use it in GitHub Desktop.
Save zaach/3869189 to your computer and use it in GitHub Desktop.
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* 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/. */
// This JS shim contains the callbacks to fire DOMRequest events for
// navigator.pay API within the payment processor's scope.
"use strict";
let { 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");
XPCOMUtils.defineLazyServiceGetter(this, "uuidgen",
"@mozilla.org/uuid-generator;1",
"nsIUUIDGenerator");
XPCOMUtils.defineLazyModuleGetter(this, "Logger",
"resource://gre/modules/identity/LogUtils.jsm");
function log(...aMessageArgs) {
Logger.log.apply(Logger, ["injected identity.js"].concat(aMessageArgs));
}
log("======================= identity.js ======================= \n");
const kCloseIdentityDialog = "close-id-dialog";
function identityCallback(aAssertion) {
log("got the callback to identityCallback!");
closeIdentityDialog(function notifySuccess() {
sendAsyncMessage("identity-delegate-return-assertion", {assertion: aAssertion});
});
}
function closeIdentityDialog(aCallback) {
// After receiving the payment provider confirmation about the
// successful or failed payment flow, we notify the UI to close the
// payment flow dialog and return to the caller application.
let randomId = uuidgen.generateUUID().toString();
let id = kCloseIdentityDialog + "-" + randomId;
let browser = Services.wm.getMostRecentWindow("navigator:browser");
let content = browser.getContentWindow();
if (!content) {
return;
}
let detail = {
type: kCloseIdentityDialog,
id: id
};
// In order to avoid race conditions, we wait for the UI to notify that
// it has successfully closed the identity flow and has recovered the
// caller app, before notifying the parent process.
content.addEventListener("mozContentEvent",
function closeIdentityDialogReturn(evt) {
log("details", evt.detail, id);
if (evt.detail.id == id && aCallback) {
log("invoking callback to closeIdentityDialog");
aCallback();
}
content.removeEventListener("mozContentEvent",
closeIdentityDialogReturn);
});
browser.shell.sendChromeEvent(detail);
}
var options = null;
var isLoaded = false;
function doInternalCall() {
if (options && isLoaded) {
log("calling BrowserID with", options);
content.wrappedJSObject.BrowserID.internal.get(options.origin, identityCallback, options);
}
}
addEventListener("DOMContentLoaded", function(e) {
content.addEventListener("load", function(e) {
isLoaded = true;
doInternalCall();
}, true);
});
// listen for request
addMessageListener("identity-delegate-request", function(aMessage) {
log("**** received identity-delegate-request", aMessage.json);
options = aMessage.json;
doInternalCall();
});
// listen for watch
addMessageListener("identity-delegate-watch", function(aMessage) {
log("**** received identity-delegate-watch", aMessage.json);
options = aMessage.json;
options.silent = true;
doInternalCall();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment