1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/b2g/chrome/content/identity.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,166 @@ 1.4 +/* -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- / 1.5 +/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +// This JS shim contains the callbacks to fire DOMRequest events for 1.11 +// navigator.pay API within the payment processor's scope. 1.12 + 1.13 +"use strict"; 1.14 + 1.15 +let { classes: Cc, interfaces: Ci, utils: Cu } = Components; 1.16 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.17 +Cu.import("resource://gre/modules/Services.jsm"); 1.18 + 1.19 +XPCOMUtils.defineLazyServiceGetter(this, "cpmm", 1.20 + "@mozilla.org/childprocessmessagemanager;1", 1.21 + "nsIMessageSender"); 1.22 + 1.23 +XPCOMUtils.defineLazyServiceGetter(this, "uuidgen", 1.24 + "@mozilla.org/uuid-generator;1", 1.25 + "nsIUUIDGenerator"); 1.26 + 1.27 +XPCOMUtils.defineLazyModuleGetter(this, "Logger", 1.28 + "resource://gre/modules/identity/LogUtils.jsm"); 1.29 + 1.30 +function log(...aMessageArgs) { 1.31 + Logger.log.apply(Logger, ["injected identity.js"].concat(aMessageArgs)); 1.32 +} 1.33 + 1.34 +log("\n\n======================= identity.js =======================\n\n"); 1.35 + 1.36 +// This script may be injected more than once into an iframe. 1.37 +// Ensure we don't redefine contstants 1.38 +if (typeof kIdentityJSLoaded === 'undefined') { 1.39 + const kIdentityDelegateWatch = "identity-delegate-watch"; 1.40 + const kIdentityDelegateRequest = "identity-delegate-request"; 1.41 + const kIdentityDelegateLogout = "identity-delegate-logout"; 1.42 + const kIdentityDelegateReady = "identity-delegate-ready"; 1.43 + const kIdentityDelegateFinished = "identity-delegate-finished"; 1.44 + const kIdentityControllerDoMethod = "identity-controller-doMethod"; 1.45 + const kIdentktyJSLoaded = true; 1.46 +} 1.47 + 1.48 +var showUI = false; 1.49 +var options = {}; 1.50 +var isLoaded = false; 1.51 +var func = null; 1.52 + 1.53 +/* 1.54 + * Message back to the SignInToWebsite pipe. Message should be an 1.55 + * object with the following keys: 1.56 + * 1.57 + * method: one of 'login', 'logout', 'ready' 1.58 + * assertion: optional assertion 1.59 + */ 1.60 +function identityCall(message) { 1.61 + if (options._internal) { 1.62 + message._internal = options._internal; 1.63 + } 1.64 + sendAsyncMessage(kIdentityControllerDoMethod, message); 1.65 +} 1.66 + 1.67 +/* 1.68 + * To close the dialog, we first tell the gecko SignInToWebsite manager that it 1.69 + * can clean up. Then we tell the gaia component that we are finished. It is 1.70 + * necessary to notify gecko first, so that the message can be sent before gaia 1.71 + * destroys our context. 1.72 + */ 1.73 +function closeIdentityDialog() { 1.74 + // tell gecko we're done. 1.75 + func = null; options = null; 1.76 + sendAsyncMessage(kIdentityDelegateFinished); 1.77 +} 1.78 + 1.79 +/* 1.80 + * doInternalWatch - call the internal.watch api and relay the results 1.81 + * up to the controller. 1.82 + */ 1.83 +function doInternalWatch() { 1.84 + log("doInternalWatch:", options, isLoaded); 1.85 + if (options && isLoaded) { 1.86 + let BrowserID = content.wrappedJSObject.BrowserID; 1.87 + BrowserID.internal.watch(function(aParams, aInternalParams) { 1.88 + identityCall(aParams); 1.89 + if (aParams.method === "ready") { 1.90 + closeIdentityDialog(); 1.91 + } 1.92 + }, 1.93 + JSON.stringify(options), 1.94 + function(...things) { 1.95 + // internal watch log callback 1.96 + log("(watch) internal: ", things); 1.97 + } 1.98 + ); 1.99 + } 1.100 +} 1.101 + 1.102 +function doInternalRequest() { 1.103 + log("doInternalRequest:", options && isLoaded); 1.104 + if (options && isLoaded) { 1.105 + var stringifiedOptions = JSON.stringify(options); 1.106 + content.wrappedJSObject.BrowserID.internal.get( 1.107 + options.origin, 1.108 + function(assertion, internalParams) { 1.109 + internalParams = internalParams || {}; 1.110 + if (assertion) { 1.111 + identityCall({ 1.112 + method: 'login', 1.113 + assertion: assertion, 1.114 + _internalParams: internalParams}); 1.115 + } else { 1.116 + identityCall({ 1.117 + method: 'cancel' 1.118 + }); 1.119 + } 1.120 + closeIdentityDialog(); 1.121 + }, 1.122 + stringifiedOptions); 1.123 + } 1.124 +} 1.125 +function doInternalLogout(aOptions) { 1.126 + log("doInternalLogout:", (options && isLoaded)); 1.127 + if (options && isLoaded) { 1.128 + let BrowserID = content.wrappedJSObject.BrowserID; 1.129 + BrowserID.internal.logout(options.origin, function() { 1.130 + identityCall({method:'logout'}); 1.131 + closeIdentityDialog(); 1.132 + }); 1.133 + } 1.134 +} 1.135 + 1.136 +addEventListener("DOMContentLoaded", function(e) { 1.137 + content.addEventListener("load", function(e) { 1.138 + isLoaded = true; 1.139 + // bring da func 1.140 + if (func) func(); 1.141 + }); 1.142 +}); 1.143 + 1.144 +// listen for request 1.145 +addMessageListener(kIdentityDelegateRequest, function(aMessage) { 1.146 + log("injected identity.js received", kIdentityDelegateRequest); 1.147 + options = aMessage.json; 1.148 + showUI = true; 1.149 + func = doInternalRequest; 1.150 + func(); 1.151 +}); 1.152 + 1.153 +// listen for watch 1.154 +addMessageListener(kIdentityDelegateWatch, function(aMessage) { 1.155 + log("injected identity.js received", kIdentityDelegateWatch); 1.156 + options = aMessage.json; 1.157 + showUI = false; 1.158 + func = doInternalWatch; 1.159 + func(); 1.160 +}); 1.161 + 1.162 +// listen for logout 1.163 +addMessageListener(kIdentityDelegateLogout, function(aMessage) { 1.164 + log("injected identity.js received", kIdentityDelegateLogout); 1.165 + options = aMessage.json; 1.166 + showUI = false; 1.167 + func = doInternalLogout; 1.168 + func(); 1.169 +});