michael@0: /* -*- Mode: js2; js2-basic-offset: 2; indent-tabs-mode: nil; -*- */ michael@0: /* vim: set ft=javascript ts=2 et sw=2 tw=80: */ 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: "use strict"; michael@0: michael@0: const Cu = Components.utils; michael@0: const Ci = Components.interfaces; michael@0: const Cc = Components.classes; michael@0: const Cr = Components.results; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: this.EXPORTED_SYMBOLS = ["IdentityStore"]; michael@0: michael@0: // the data store for IDService michael@0: // written as a separate thing so it can easily be mocked michael@0: function IDServiceStore() { michael@0: this.reset(); michael@0: } michael@0: michael@0: // Note: eventually these methods may be async, but we haven no need for this michael@0: // for now, since we're not storing to disk. michael@0: IDServiceStore.prototype = { michael@0: addIdentity: function addIdentity(aEmail, aKeyPair, aCert) { michael@0: this._identities[aEmail] = {keyPair: aKeyPair, cert: aCert}; michael@0: }, michael@0: fetchIdentity: function fetchIdentity(aEmail) { michael@0: return aEmail in this._identities ? this._identities[aEmail] : null; michael@0: }, michael@0: removeIdentity: function removeIdentity(aEmail) { michael@0: let data = this._identities[aEmail]; michael@0: delete this._identities[aEmail]; michael@0: return data; michael@0: }, michael@0: getIdentities: function getIdentities() { michael@0: // XXX - should clone? michael@0: return this._identities; michael@0: }, michael@0: clearCert: function clearCert(aEmail) { michael@0: // XXX - should remove key from store? michael@0: this._identities[aEmail].cert = null; michael@0: this._identities[aEmail].keyPair = null; michael@0: }, michael@0: michael@0: /** michael@0: * set the login state for a given origin michael@0: * michael@0: * @param aOrigin michael@0: * (string) a web origin michael@0: * michael@0: * @param aState michael@0: * (boolean) whether or not the user is logged in michael@0: * michael@0: * @param aEmail michael@0: * (email) the email address the user is logged in with, michael@0: * or, if not logged in, the default email for that origin. michael@0: */ michael@0: setLoginState: function setLoginState(aOrigin, aState, aEmail) { michael@0: if (aState && !aEmail) { michael@0: throw "isLoggedIn cannot be set to true without an email"; michael@0: } michael@0: return this._loginStates[aOrigin] = {isLoggedIn: aState, email: aEmail}; michael@0: }, michael@0: getLoginState: function getLoginState(aOrigin) { michael@0: return aOrigin in this._loginStates ? this._loginStates[aOrigin] : null; michael@0: }, michael@0: clearLoginState: function clearLoginState(aOrigin) { michael@0: delete this._loginStates[aOrigin]; michael@0: }, michael@0: michael@0: reset: function Store_reset() { michael@0: // _identities associates emails with keypairs and certificates michael@0: this._identities = {}; michael@0: michael@0: // _loginStates associates. remote origins with a login status and michael@0: // the email the user has chosen as his or her identity when logging michael@0: // into that origin. michael@0: this._loginStates = {}; michael@0: }, michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, Ci.nsIObserver]), michael@0: michael@0: observe: function observe(aSubject, aTopic, aData) { michael@0: switch (aTopic) { michael@0: case "quit-application-granted": michael@0: Services.obs.removeObserver(this, "quit-application-granted"); michael@0: this.reset(); michael@0: break; michael@0: } michael@0: }, michael@0: }; michael@0: michael@0: this.IdentityStore = new IDServiceStore();