b2g/chrome/content/identity.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
michael@0 2 /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 // This JS shim contains the callbacks to fire DOMRequest events for
michael@0 8 // navigator.pay API within the payment processor's scope.
michael@0 9
michael@0 10 "use strict";
michael@0 11
michael@0 12 let { classes: Cc, interfaces: Ci, utils: Cu } = Components;
michael@0 13 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 14 Cu.import("resource://gre/modules/Services.jsm");
michael@0 15
michael@0 16 XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
michael@0 17 "@mozilla.org/childprocessmessagemanager;1",
michael@0 18 "nsIMessageSender");
michael@0 19
michael@0 20 XPCOMUtils.defineLazyServiceGetter(this, "uuidgen",
michael@0 21 "@mozilla.org/uuid-generator;1",
michael@0 22 "nsIUUIDGenerator");
michael@0 23
michael@0 24 XPCOMUtils.defineLazyModuleGetter(this, "Logger",
michael@0 25 "resource://gre/modules/identity/LogUtils.jsm");
michael@0 26
michael@0 27 function log(...aMessageArgs) {
michael@0 28 Logger.log.apply(Logger, ["injected identity.js"].concat(aMessageArgs));
michael@0 29 }
michael@0 30
michael@0 31 log("\n\n======================= identity.js =======================\n\n");
michael@0 32
michael@0 33 // This script may be injected more than once into an iframe.
michael@0 34 // Ensure we don't redefine contstants
michael@0 35 if (typeof kIdentityJSLoaded === 'undefined') {
michael@0 36 const kIdentityDelegateWatch = "identity-delegate-watch";
michael@0 37 const kIdentityDelegateRequest = "identity-delegate-request";
michael@0 38 const kIdentityDelegateLogout = "identity-delegate-logout";
michael@0 39 const kIdentityDelegateReady = "identity-delegate-ready";
michael@0 40 const kIdentityDelegateFinished = "identity-delegate-finished";
michael@0 41 const kIdentityControllerDoMethod = "identity-controller-doMethod";
michael@0 42 const kIdentktyJSLoaded = true;
michael@0 43 }
michael@0 44
michael@0 45 var showUI = false;
michael@0 46 var options = {};
michael@0 47 var isLoaded = false;
michael@0 48 var func = null;
michael@0 49
michael@0 50 /*
michael@0 51 * Message back to the SignInToWebsite pipe. Message should be an
michael@0 52 * object with the following keys:
michael@0 53 *
michael@0 54 * method: one of 'login', 'logout', 'ready'
michael@0 55 * assertion: optional assertion
michael@0 56 */
michael@0 57 function identityCall(message) {
michael@0 58 if (options._internal) {
michael@0 59 message._internal = options._internal;
michael@0 60 }
michael@0 61 sendAsyncMessage(kIdentityControllerDoMethod, message);
michael@0 62 }
michael@0 63
michael@0 64 /*
michael@0 65 * To close the dialog, we first tell the gecko SignInToWebsite manager that it
michael@0 66 * can clean up. Then we tell the gaia component that we are finished. It is
michael@0 67 * necessary to notify gecko first, so that the message can be sent before gaia
michael@0 68 * destroys our context.
michael@0 69 */
michael@0 70 function closeIdentityDialog() {
michael@0 71 // tell gecko we're done.
michael@0 72 func = null; options = null;
michael@0 73 sendAsyncMessage(kIdentityDelegateFinished);
michael@0 74 }
michael@0 75
michael@0 76 /*
michael@0 77 * doInternalWatch - call the internal.watch api and relay the results
michael@0 78 * up to the controller.
michael@0 79 */
michael@0 80 function doInternalWatch() {
michael@0 81 log("doInternalWatch:", options, isLoaded);
michael@0 82 if (options && isLoaded) {
michael@0 83 let BrowserID = content.wrappedJSObject.BrowserID;
michael@0 84 BrowserID.internal.watch(function(aParams, aInternalParams) {
michael@0 85 identityCall(aParams);
michael@0 86 if (aParams.method === "ready") {
michael@0 87 closeIdentityDialog();
michael@0 88 }
michael@0 89 },
michael@0 90 JSON.stringify(options),
michael@0 91 function(...things) {
michael@0 92 // internal watch log callback
michael@0 93 log("(watch) internal: ", things);
michael@0 94 }
michael@0 95 );
michael@0 96 }
michael@0 97 }
michael@0 98
michael@0 99 function doInternalRequest() {
michael@0 100 log("doInternalRequest:", options && isLoaded);
michael@0 101 if (options && isLoaded) {
michael@0 102 var stringifiedOptions = JSON.stringify(options);
michael@0 103 content.wrappedJSObject.BrowserID.internal.get(
michael@0 104 options.origin,
michael@0 105 function(assertion, internalParams) {
michael@0 106 internalParams = internalParams || {};
michael@0 107 if (assertion) {
michael@0 108 identityCall({
michael@0 109 method: 'login',
michael@0 110 assertion: assertion,
michael@0 111 _internalParams: internalParams});
michael@0 112 } else {
michael@0 113 identityCall({
michael@0 114 method: 'cancel'
michael@0 115 });
michael@0 116 }
michael@0 117 closeIdentityDialog();
michael@0 118 },
michael@0 119 stringifiedOptions);
michael@0 120 }
michael@0 121 }
michael@0 122 function doInternalLogout(aOptions) {
michael@0 123 log("doInternalLogout:", (options && isLoaded));
michael@0 124 if (options && isLoaded) {
michael@0 125 let BrowserID = content.wrappedJSObject.BrowserID;
michael@0 126 BrowserID.internal.logout(options.origin, function() {
michael@0 127 identityCall({method:'logout'});
michael@0 128 closeIdentityDialog();
michael@0 129 });
michael@0 130 }
michael@0 131 }
michael@0 132
michael@0 133 addEventListener("DOMContentLoaded", function(e) {
michael@0 134 content.addEventListener("load", function(e) {
michael@0 135 isLoaded = true;
michael@0 136 // bring da func
michael@0 137 if (func) func();
michael@0 138 });
michael@0 139 });
michael@0 140
michael@0 141 // listen for request
michael@0 142 addMessageListener(kIdentityDelegateRequest, function(aMessage) {
michael@0 143 log("injected identity.js received", kIdentityDelegateRequest);
michael@0 144 options = aMessage.json;
michael@0 145 showUI = true;
michael@0 146 func = doInternalRequest;
michael@0 147 func();
michael@0 148 });
michael@0 149
michael@0 150 // listen for watch
michael@0 151 addMessageListener(kIdentityDelegateWatch, function(aMessage) {
michael@0 152 log("injected identity.js received", kIdentityDelegateWatch);
michael@0 153 options = aMessage.json;
michael@0 154 showUI = false;
michael@0 155 func = doInternalWatch;
michael@0 156 func();
michael@0 157 });
michael@0 158
michael@0 159 // listen for logout
michael@0 160 addMessageListener(kIdentityDelegateLogout, function(aMessage) {
michael@0 161 log("injected identity.js received", kIdentityDelegateLogout);
michael@0 162 options = aMessage.json;
michael@0 163 showUI = false;
michael@0 164 func = doInternalLogout;
michael@0 165 func();
michael@0 166 });

mercurial