1 /* -*- js-indent-level: 2; indent-tabs-mode: nil -*- */
2 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
9 const Cu = Components.utils;
10 const Ci = Components.interfaces;
11 const Cc = Components.classes;
12 const Cr = Components.results;
14 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
15 Cu.import("resource://gre/modules/Services.jsm");
17 this.EXPORTED_SYMBOLS = ["IdentityStore"];
19 // the data store for IDService
20 // written as a separate thing so it can easily be mocked
21 function IDServiceStore() {
25 // Note: eventually these methods may be async, but we haven no need for this
26 // for now, since we're not storing to disk.
27 IDServiceStore.prototype = {
28 addIdentity: function addIdentity(aEmail, aKeyPair, aCert) {
29 this._identities[aEmail] = {keyPair: aKeyPair, cert: aCert};
31 fetchIdentity: function fetchIdentity(aEmail) {
32 return aEmail in this._identities ? this._identities[aEmail] : null;
34 removeIdentity: function removeIdentity(aEmail) {
35 let data = this._identities[aEmail];
36 delete this._identities[aEmail];
39 getIdentities: function getIdentities() {
40 // XXX - should clone?
41 return this._identities;
43 clearCert: function clearCert(aEmail) {
44 // XXX - should remove key from store?
45 this._identities[aEmail].cert = null;
46 this._identities[aEmail].keyPair = null;
50 * set the login state for a given origin
53 * (string) a web origin
56 * (boolean) whether or not the user is logged in
59 * (email) the email address the user is logged in with,
60 * or, if not logged in, the default email for that origin.
62 setLoginState: function setLoginState(aOrigin, aState, aEmail) {
63 if (aState && !aEmail) {
64 throw "isLoggedIn cannot be set to true without an email";
66 return this._loginStates[aOrigin] = {isLoggedIn: aState, email: aEmail};
68 getLoginState: function getLoginState(aOrigin) {
69 return aOrigin in this._loginStates ? this._loginStates[aOrigin] : null;
71 clearLoginState: function clearLoginState(aOrigin) {
72 delete this._loginStates[aOrigin];
75 reset: function Store_reset() {
76 // _identities associates emails with keypairs and certificates
77 this._identities = {};
79 // _loginStates associates. remote origins with a login status and
80 // the email the user has chosen as his or her identity when logging
82 this._loginStates = {};
85 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, Ci.nsIObserver]),
87 observe: function observe(aSubject, aTopic, aData) {
89 case "quit-application-granted":
90 Services.obs.removeObserver(this, "quit-application-granted");
97 this.IdentityStore = new IDServiceStore();