michael@0: /* -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- / michael@0: /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // This JS shim contains the callbacks to fire DOMRequest events for michael@0: // navigator.pay API within the payment processor's scope. michael@0: michael@0: "use strict"; michael@0: michael@0: let { classes: Cc, interfaces: Ci, utils: Cu } = Components; michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyServiceGetter(this, "cpmm", michael@0: "@mozilla.org/childprocessmessagemanager;1", michael@0: "nsIMessageSender"); michael@0: michael@0: XPCOMUtils.defineLazyServiceGetter(this, "uuidgen", michael@0: "@mozilla.org/uuid-generator;1", michael@0: "nsIUUIDGenerator"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "Logger", michael@0: "resource://gre/modules/identity/LogUtils.jsm"); michael@0: michael@0: function log(...aMessageArgs) { michael@0: Logger.log.apply(Logger, ["injected identity.js"].concat(aMessageArgs)); michael@0: } michael@0: michael@0: log("\n\n======================= identity.js =======================\n\n"); michael@0: michael@0: // This script may be injected more than once into an iframe. michael@0: // Ensure we don't redefine contstants michael@0: if (typeof kIdentityJSLoaded === 'undefined') { michael@0: const kIdentityDelegateWatch = "identity-delegate-watch"; michael@0: const kIdentityDelegateRequest = "identity-delegate-request"; michael@0: const kIdentityDelegateLogout = "identity-delegate-logout"; michael@0: const kIdentityDelegateReady = "identity-delegate-ready"; michael@0: const kIdentityDelegateFinished = "identity-delegate-finished"; michael@0: const kIdentityControllerDoMethod = "identity-controller-doMethod"; michael@0: const kIdentktyJSLoaded = true; michael@0: } michael@0: michael@0: var showUI = false; michael@0: var options = {}; michael@0: var isLoaded = false; michael@0: var func = null; michael@0: michael@0: /* michael@0: * Message back to the SignInToWebsite pipe. Message should be an michael@0: * object with the following keys: michael@0: * michael@0: * method: one of 'login', 'logout', 'ready' michael@0: * assertion: optional assertion michael@0: */ michael@0: function identityCall(message) { michael@0: if (options._internal) { michael@0: message._internal = options._internal; michael@0: } michael@0: sendAsyncMessage(kIdentityControllerDoMethod, message); michael@0: } michael@0: michael@0: /* michael@0: * To close the dialog, we first tell the gecko SignInToWebsite manager that it michael@0: * can clean up. Then we tell the gaia component that we are finished. It is michael@0: * necessary to notify gecko first, so that the message can be sent before gaia michael@0: * destroys our context. michael@0: */ michael@0: function closeIdentityDialog() { michael@0: // tell gecko we're done. michael@0: func = null; options = null; michael@0: sendAsyncMessage(kIdentityDelegateFinished); michael@0: } michael@0: michael@0: /* michael@0: * doInternalWatch - call the internal.watch api and relay the results michael@0: * up to the controller. michael@0: */ michael@0: function doInternalWatch() { michael@0: log("doInternalWatch:", options, isLoaded); michael@0: if (options && isLoaded) { michael@0: let BrowserID = content.wrappedJSObject.BrowserID; michael@0: BrowserID.internal.watch(function(aParams, aInternalParams) { michael@0: identityCall(aParams); michael@0: if (aParams.method === "ready") { michael@0: closeIdentityDialog(); michael@0: } michael@0: }, michael@0: JSON.stringify(options), michael@0: function(...things) { michael@0: // internal watch log callback michael@0: log("(watch) internal: ", things); michael@0: } michael@0: ); michael@0: } michael@0: } michael@0: michael@0: function doInternalRequest() { michael@0: log("doInternalRequest:", options && isLoaded); michael@0: if (options && isLoaded) { michael@0: var stringifiedOptions = JSON.stringify(options); michael@0: content.wrappedJSObject.BrowserID.internal.get( michael@0: options.origin, michael@0: function(assertion, internalParams) { michael@0: internalParams = internalParams || {}; michael@0: if (assertion) { michael@0: identityCall({ michael@0: method: 'login', michael@0: assertion: assertion, michael@0: _internalParams: internalParams}); michael@0: } else { michael@0: identityCall({ michael@0: method: 'cancel' michael@0: }); michael@0: } michael@0: closeIdentityDialog(); michael@0: }, michael@0: stringifiedOptions); michael@0: } michael@0: } michael@0: function doInternalLogout(aOptions) { michael@0: log("doInternalLogout:", (options && isLoaded)); michael@0: if (options && isLoaded) { michael@0: let BrowserID = content.wrappedJSObject.BrowserID; michael@0: BrowserID.internal.logout(options.origin, function() { michael@0: identityCall({method:'logout'}); michael@0: closeIdentityDialog(); michael@0: }); michael@0: } michael@0: } michael@0: michael@0: addEventListener("DOMContentLoaded", function(e) { michael@0: content.addEventListener("load", function(e) { michael@0: isLoaded = true; michael@0: // bring da func michael@0: if (func) func(); michael@0: }); michael@0: }); michael@0: michael@0: // listen for request michael@0: addMessageListener(kIdentityDelegateRequest, function(aMessage) { michael@0: log("injected identity.js received", kIdentityDelegateRequest); michael@0: options = aMessage.json; michael@0: showUI = true; michael@0: func = doInternalRequest; michael@0: func(); michael@0: }); michael@0: michael@0: // listen for watch michael@0: addMessageListener(kIdentityDelegateWatch, function(aMessage) { michael@0: log("injected identity.js received", kIdentityDelegateWatch); michael@0: options = aMessage.json; michael@0: showUI = false; michael@0: func = doInternalWatch; michael@0: func(); michael@0: }); michael@0: michael@0: // listen for logout michael@0: addMessageListener(kIdentityDelegateLogout, function(aMessage) { michael@0: log("injected identity.js received", kIdentityDelegateLogout); michael@0: options = aMessage.json; michael@0: showUI = false; michael@0: func = doInternalLogout; michael@0: func(); michael@0: });