Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: js2; js2-basic-offset: 2; indent-tabs-mode: nil; -*- */ |
michael@0 | 2 | /* vim: set ft=javascript ts=2 et sw=2 tw=80: */ |
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 | "use strict"; |
michael@0 | 8 | |
michael@0 | 9 | const Cu = Components.utils; |
michael@0 | 10 | const Ci = Components.interfaces; |
michael@0 | 11 | const Cc = Components.classes; |
michael@0 | 12 | const Cr = Components.results; |
michael@0 | 13 | |
michael@0 | 14 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 15 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 16 | |
michael@0 | 17 | this.EXPORTED_SYMBOLS = ["IdentityStore"]; |
michael@0 | 18 | |
michael@0 | 19 | // the data store for IDService |
michael@0 | 20 | // written as a separate thing so it can easily be mocked |
michael@0 | 21 | function IDServiceStore() { |
michael@0 | 22 | this.reset(); |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | // Note: eventually these methods may be async, but we haven no need for this |
michael@0 | 26 | // for now, since we're not storing to disk. |
michael@0 | 27 | IDServiceStore.prototype = { |
michael@0 | 28 | addIdentity: function addIdentity(aEmail, aKeyPair, aCert) { |
michael@0 | 29 | this._identities[aEmail] = {keyPair: aKeyPair, cert: aCert}; |
michael@0 | 30 | }, |
michael@0 | 31 | fetchIdentity: function fetchIdentity(aEmail) { |
michael@0 | 32 | return aEmail in this._identities ? this._identities[aEmail] : null; |
michael@0 | 33 | }, |
michael@0 | 34 | removeIdentity: function removeIdentity(aEmail) { |
michael@0 | 35 | let data = this._identities[aEmail]; |
michael@0 | 36 | delete this._identities[aEmail]; |
michael@0 | 37 | return data; |
michael@0 | 38 | }, |
michael@0 | 39 | getIdentities: function getIdentities() { |
michael@0 | 40 | // XXX - should clone? |
michael@0 | 41 | return this._identities; |
michael@0 | 42 | }, |
michael@0 | 43 | clearCert: function clearCert(aEmail) { |
michael@0 | 44 | // XXX - should remove key from store? |
michael@0 | 45 | this._identities[aEmail].cert = null; |
michael@0 | 46 | this._identities[aEmail].keyPair = null; |
michael@0 | 47 | }, |
michael@0 | 48 | |
michael@0 | 49 | /** |
michael@0 | 50 | * set the login state for a given origin |
michael@0 | 51 | * |
michael@0 | 52 | * @param aOrigin |
michael@0 | 53 | * (string) a web origin |
michael@0 | 54 | * |
michael@0 | 55 | * @param aState |
michael@0 | 56 | * (boolean) whether or not the user is logged in |
michael@0 | 57 | * |
michael@0 | 58 | * @param aEmail |
michael@0 | 59 | * (email) the email address the user is logged in with, |
michael@0 | 60 | * or, if not logged in, the default email for that origin. |
michael@0 | 61 | */ |
michael@0 | 62 | setLoginState: function setLoginState(aOrigin, aState, aEmail) { |
michael@0 | 63 | if (aState && !aEmail) { |
michael@0 | 64 | throw "isLoggedIn cannot be set to true without an email"; |
michael@0 | 65 | } |
michael@0 | 66 | return this._loginStates[aOrigin] = {isLoggedIn: aState, email: aEmail}; |
michael@0 | 67 | }, |
michael@0 | 68 | getLoginState: function getLoginState(aOrigin) { |
michael@0 | 69 | return aOrigin in this._loginStates ? this._loginStates[aOrigin] : null; |
michael@0 | 70 | }, |
michael@0 | 71 | clearLoginState: function clearLoginState(aOrigin) { |
michael@0 | 72 | delete this._loginStates[aOrigin]; |
michael@0 | 73 | }, |
michael@0 | 74 | |
michael@0 | 75 | reset: function Store_reset() { |
michael@0 | 76 | // _identities associates emails with keypairs and certificates |
michael@0 | 77 | this._identities = {}; |
michael@0 | 78 | |
michael@0 | 79 | // _loginStates associates. remote origins with a login status and |
michael@0 | 80 | // the email the user has chosen as his or her identity when logging |
michael@0 | 81 | // into that origin. |
michael@0 | 82 | this._loginStates = {}; |
michael@0 | 83 | }, |
michael@0 | 84 | |
michael@0 | 85 | QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, Ci.nsIObserver]), |
michael@0 | 86 | |
michael@0 | 87 | observe: function observe(aSubject, aTopic, aData) { |
michael@0 | 88 | switch (aTopic) { |
michael@0 | 89 | case "quit-application-granted": |
michael@0 | 90 | Services.obs.removeObserver(this, "quit-application-granted"); |
michael@0 | 91 | this.reset(); |
michael@0 | 92 | break; |
michael@0 | 93 | } |
michael@0 | 94 | }, |
michael@0 | 95 | }; |
michael@0 | 96 | |
michael@0 | 97 | this.IdentityStore = new IDServiceStore(); |