toolkit/identity/FirefoxAccounts.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 this.EXPORTED_SYMBOLS = ["FirefoxAccounts"];
michael@0 8
michael@0 9 const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
michael@0 10
michael@0 11 Cu.import("resource://gre/modules/Log.jsm");
michael@0 12 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 13 Cu.import("resource://gre/modules/Services.jsm");
michael@0 14 Cu.import("resource://gre/modules/identity/LogUtils.jsm");
michael@0 15
michael@0 16 XPCOMUtils.defineLazyModuleGetter(this, "objectCopy",
michael@0 17 "resource://gre/modules/identity/IdentityUtils.jsm");
michael@0 18
michael@0 19 XPCOMUtils.defineLazyModuleGetter(this, "makeMessageObject",
michael@0 20 "resource://gre/modules/identity/IdentityUtils.jsm");
michael@0 21
michael@0 22 // loglevel preference should be one of: "FATAL", "ERROR", "WARN", "INFO",
michael@0 23 // "CONFIG", "DEBUG", "TRACE" or "ALL". We will be logging error messages by
michael@0 24 // default.
michael@0 25 const PREF_LOG_LEVEL = "identity.fxaccounts.loglevel";
michael@0 26 try {
michael@0 27 this.LOG_LEVEL =
michael@0 28 Services.prefs.getPrefType(PREF_LOG_LEVEL) == Ci.nsIPrefBranch.PREF_STRING
michael@0 29 && Services.prefs.getCharPref(PREF_LOG_LEVEL);
michael@0 30 } catch (e) {
michael@0 31 this.LOG_LEVEL = Log.Level.Error;
michael@0 32 }
michael@0 33
michael@0 34 let log = Log.repository.getLogger("Identity.FxAccounts");
michael@0 35 log.level = LOG_LEVEL;
michael@0 36 log.addAppender(new Log.ConsoleAppender(new Log.BasicFormatter()));
michael@0 37
michael@0 38 #ifdef MOZ_B2G
michael@0 39 XPCOMUtils.defineLazyModuleGetter(this, "FxAccountsManager",
michael@0 40 "resource://gre/modules/FxAccountsManager.jsm",
michael@0 41 "FxAccountsManager");
michael@0 42 #else
michael@0 43 log.warn("The FxAccountsManager is only functional in B2G at this time.");
michael@0 44 var FxAccountsManager = null;
michael@0 45 #endif
michael@0 46
michael@0 47 function FxAccountsService() {
michael@0 48 Services.obs.addObserver(this, "quit-application-granted", false);
michael@0 49
michael@0 50 // Maintain interface parity with Identity.jsm and MinimalIdentity.jsm
michael@0 51 this.RP = this;
michael@0 52
michael@0 53 this._rpFlows = new Map();
michael@0 54
michael@0 55 // Enable us to mock FxAccountsManager service in testing
michael@0 56 this.fxAccountsManager = FxAccountsManager;
michael@0 57 }
michael@0 58
michael@0 59 FxAccountsService.prototype = {
michael@0 60 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, Ci.nsIObserver]),
michael@0 61
michael@0 62 observe: function observe(aSubject, aTopic, aData) {
michael@0 63 switch (aTopic) {
michael@0 64 case "quit-application-granted":
michael@0 65 Services.obs.removeObserver(this, "quit-application-granted");
michael@0 66 break;
michael@0 67 }
michael@0 68 },
michael@0 69
michael@0 70 /**
michael@0 71 * Register a listener for a given windowID as a result of a call to
michael@0 72 * navigator.id.watch().
michael@0 73 *
michael@0 74 * @param aCaller
michael@0 75 * (Object) an object that represents the caller document, and
michael@0 76 * is expected to have properties:
michael@0 77 * - id (unique, e.g. uuid)
michael@0 78 * - origin (string)
michael@0 79 *
michael@0 80 * and a bunch of callbacks
michael@0 81 * - doReady()
michael@0 82 * - doLogin()
michael@0 83 * - doLogout()
michael@0 84 * - doError()
michael@0 85 * - doCancel()
michael@0 86 *
michael@0 87 */
michael@0 88 watch: function watch(aRpCaller) {
michael@0 89 this._rpFlows.set(aRpCaller.id, aRpCaller);
michael@0 90 log.debug("watch: " + aRpCaller.id);
michael@0 91 log.debug("Current rp flows: " + this._rpFlows.size);
michael@0 92
michael@0 93 // Log the user in, if possible, and then call ready().
michael@0 94 let runnable = {
michael@0 95 run: () => {
michael@0 96 this.fxAccountsManager.getAssertion(aRpCaller.audience, {silent:true}).then(
michael@0 97 data => {
michael@0 98 if (data) {
michael@0 99 this.doLogin(aRpCaller.id, data);
michael@0 100 } else {
michael@0 101 this.doLogout(aRpCaller.id);
michael@0 102 }
michael@0 103 this.doReady(aRpCaller.id);
michael@0 104 },
michael@0 105 error => {
michael@0 106 log.error("get silent assertion failed: " + JSON.stringify(error));
michael@0 107 this.doError(aRpCaller.id, error);
michael@0 108 }
michael@0 109 );
michael@0 110 }
michael@0 111 };
michael@0 112 Services.tm.currentThread.dispatch(runnable,
michael@0 113 Ci.nsIThread.DISPATCH_NORMAL);
michael@0 114 },
michael@0 115
michael@0 116 /**
michael@0 117 * Delete the flow when the screen is unloaded
michael@0 118 */
michael@0 119 unwatch: function(aRpCallerId, aTargetMM) {
michael@0 120 log.debug("unwatching: " + aRpCallerId);
michael@0 121 this._rpFlows.delete(aRpCallerId);
michael@0 122 },
michael@0 123
michael@0 124 /**
michael@0 125 * Initiate a login with user interaction as a result of a call to
michael@0 126 * navigator.id.request().
michael@0 127 *
michael@0 128 * @param aRPId
michael@0 129 * (integer) the id of the doc object obtained in .watch()
michael@0 130 *
michael@0 131 * @param aOptions
michael@0 132 * (Object) options including privacyPolicy, termsOfService
michael@0 133 */
michael@0 134 request: function request(aRPId, aOptions) {
michael@0 135 aOptions = aOptions || {};
michael@0 136 let rp = this._rpFlows.get(aRPId);
michael@0 137 if (!rp) {
michael@0 138 log.error("request() called before watch()");
michael@0 139 return;
michael@0 140 }
michael@0 141
michael@0 142 let options = makeMessageObject(rp);
michael@0 143 objectCopy(aOptions, options);
michael@0 144
michael@0 145 log.debug("get assertion for " + rp.audience);
michael@0 146
michael@0 147 this.fxAccountsManager.getAssertion(rp.audience, options).then(
michael@0 148 data => {
michael@0 149 log.debug("got assertion for " + rp.audience + ": " + data);
michael@0 150 this.doLogin(aRPId, data);
michael@0 151 },
michael@0 152 error => {
michael@0 153 log.error("get assertion failed: " + JSON.stringify(error));
michael@0 154 this.doError(aRPId, error);
michael@0 155 }
michael@0 156 );
michael@0 157 },
michael@0 158
michael@0 159 /**
michael@0 160 * Invoked when a user wishes to logout of a site (for instance, when clicking
michael@0 161 * on an in-content logout button).
michael@0 162 *
michael@0 163 * @param aRpCallerId
michael@0 164 * (integer) the id of the doc object obtained in .watch()
michael@0 165 *
michael@0 166 */
michael@0 167 logout: function logout(aRpCallerId) {
michael@0 168 // XXX Bug 945363 - Resolve the SSO story for FXA and implement
michael@0 169 // logout accordingly.
michael@0 170 //
michael@0 171 // For now, it makes no sense to logout from a specific RP in
michael@0 172 // Firefox Accounts, so just directly call the logout callback.
michael@0 173 if (!this._rpFlows.has(aRpCallerId)) {
michael@0 174 log.error("logout() called before watch()");
michael@0 175 return;
michael@0 176 }
michael@0 177
michael@0 178 // Call logout() on the next tick
michael@0 179 let runnable = {
michael@0 180 run: () => {
michael@0 181 this.fxAccountsManager.signOut().then(() => {
michael@0 182 this.doLogout(aRpCallerId);
michael@0 183 });
michael@0 184 }
michael@0 185 };
michael@0 186 Services.tm.currentThread.dispatch(runnable,
michael@0 187 Ci.nsIThread.DISPATCH_NORMAL);
michael@0 188 },
michael@0 189
michael@0 190 childProcessShutdown: function childProcessShutdown(messageManager) {
michael@0 191 for (let [key,] of this._rpFlows) {
michael@0 192 if (this._rpFlows.get(key)._mm === messageManager) {
michael@0 193 this._rpFlows.delete(key);
michael@0 194 }
michael@0 195 }
michael@0 196 },
michael@0 197
michael@0 198 doLogin: function doLogin(aRpCallerId, aAssertion) {
michael@0 199 let rp = this._rpFlows.get(aRpCallerId);
michael@0 200 if (!rp) {
michael@0 201 log.warn("doLogin found no rp to go with callerId " + aRpCallerId + "\n");
michael@0 202 return;
michael@0 203 }
michael@0 204
michael@0 205 rp.doLogin(aAssertion);
michael@0 206 },
michael@0 207
michael@0 208 doLogout: function doLogout(aRpCallerId) {
michael@0 209 let rp = this._rpFlows.get(aRpCallerId);
michael@0 210 if (!rp) {
michael@0 211 log.warn("doLogout found no rp to go with callerId " + aRpCallerId + "\n");
michael@0 212 return;
michael@0 213 }
michael@0 214
michael@0 215 rp.doLogout();
michael@0 216 },
michael@0 217
michael@0 218 doReady: function doReady(aRpCallerId) {
michael@0 219 let rp = this._rpFlows.get(aRpCallerId);
michael@0 220 if (!rp) {
michael@0 221 log.warn("doReady found no rp to go with callerId " + aRpCallerId + "\n");
michael@0 222 return;
michael@0 223 }
michael@0 224
michael@0 225 rp.doReady();
michael@0 226 },
michael@0 227
michael@0 228 doCancel: function doCancel(aRpCallerId) {
michael@0 229 let rp = this._rpFlows.get(aRpCallerId);
michael@0 230 if (!rp) {
michael@0 231 log.warn("doCancel found no rp to go with callerId " + aRpCallerId + "\n");
michael@0 232 return;
michael@0 233 }
michael@0 234
michael@0 235 rp.doCancel();
michael@0 236 },
michael@0 237
michael@0 238 doError: function doError(aRpCallerId, aError) {
michael@0 239 let rp = this._rpFlows.get(aRpCallerId);
michael@0 240 if (!rp) {
michael@0 241 log.warn("doCancel found no rp to go with callerId " + aRpCallerId + "\n");
michael@0 242 return;
michael@0 243 }
michael@0 244
michael@0 245 rp.doError(aError);
michael@0 246 }
michael@0 247 };
michael@0 248
michael@0 249 this.FirefoxAccounts = new FxAccountsService();
michael@0 250

mercurial