1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/identity/IdentityStore.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,97 @@ 1.4 +/* -*- Mode: js2; js2-basic-offset: 2; indent-tabs-mode: nil; -*- */ 1.5 +/* vim: set ft=javascript ts=2 et sw=2 tw=80: */ 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 +"use strict"; 1.11 + 1.12 +const Cu = Components.utils; 1.13 +const Ci = Components.interfaces; 1.14 +const Cc = Components.classes; 1.15 +const Cr = Components.results; 1.16 + 1.17 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.18 +Cu.import("resource://gre/modules/Services.jsm"); 1.19 + 1.20 +this.EXPORTED_SYMBOLS = ["IdentityStore"]; 1.21 + 1.22 +// the data store for IDService 1.23 +// written as a separate thing so it can easily be mocked 1.24 +function IDServiceStore() { 1.25 + this.reset(); 1.26 +} 1.27 + 1.28 +// Note: eventually these methods may be async, but we haven no need for this 1.29 +// for now, since we're not storing to disk. 1.30 +IDServiceStore.prototype = { 1.31 + addIdentity: function addIdentity(aEmail, aKeyPair, aCert) { 1.32 + this._identities[aEmail] = {keyPair: aKeyPair, cert: aCert}; 1.33 + }, 1.34 + fetchIdentity: function fetchIdentity(aEmail) { 1.35 + return aEmail in this._identities ? this._identities[aEmail] : null; 1.36 + }, 1.37 + removeIdentity: function removeIdentity(aEmail) { 1.38 + let data = this._identities[aEmail]; 1.39 + delete this._identities[aEmail]; 1.40 + return data; 1.41 + }, 1.42 + getIdentities: function getIdentities() { 1.43 + // XXX - should clone? 1.44 + return this._identities; 1.45 + }, 1.46 + clearCert: function clearCert(aEmail) { 1.47 + // XXX - should remove key from store? 1.48 + this._identities[aEmail].cert = null; 1.49 + this._identities[aEmail].keyPair = null; 1.50 + }, 1.51 + 1.52 + /** 1.53 + * set the login state for a given origin 1.54 + * 1.55 + * @param aOrigin 1.56 + * (string) a web origin 1.57 + * 1.58 + * @param aState 1.59 + * (boolean) whether or not the user is logged in 1.60 + * 1.61 + * @param aEmail 1.62 + * (email) the email address the user is logged in with, 1.63 + * or, if not logged in, the default email for that origin. 1.64 + */ 1.65 + setLoginState: function setLoginState(aOrigin, aState, aEmail) { 1.66 + if (aState && !aEmail) { 1.67 + throw "isLoggedIn cannot be set to true without an email"; 1.68 + } 1.69 + return this._loginStates[aOrigin] = {isLoggedIn: aState, email: aEmail}; 1.70 + }, 1.71 + getLoginState: function getLoginState(aOrigin) { 1.72 + return aOrigin in this._loginStates ? this._loginStates[aOrigin] : null; 1.73 + }, 1.74 + clearLoginState: function clearLoginState(aOrigin) { 1.75 + delete this._loginStates[aOrigin]; 1.76 + }, 1.77 + 1.78 + reset: function Store_reset() { 1.79 + // _identities associates emails with keypairs and certificates 1.80 + this._identities = {}; 1.81 + 1.82 + // _loginStates associates. remote origins with a login status and 1.83 + // the email the user has chosen as his or her identity when logging 1.84 + // into that origin. 1.85 + this._loginStates = {}; 1.86 + }, 1.87 + 1.88 + QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, Ci.nsIObserver]), 1.89 + 1.90 + observe: function observe(aSubject, aTopic, aData) { 1.91 + switch (aTopic) { 1.92 + case "quit-application-granted": 1.93 + Services.obs.removeObserver(this, "quit-application-granted"); 1.94 + this.reset(); 1.95 + break; 1.96 + } 1.97 + }, 1.98 +}; 1.99 + 1.100 +this.IdentityStore = new IDServiceStore();