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 | /* 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 = ["GlobalState"]; |
michael@0 | 8 | |
michael@0 | 9 | const EXPORTED_METHODS = ["getState", "clear", "get", "set", "delete", "setFromState"]; |
michael@0 | 10 | /** |
michael@0 | 11 | * Module that contains global session data. |
michael@0 | 12 | */ |
michael@0 | 13 | function GlobalState() { |
michael@0 | 14 | let internal = new GlobalStateInternal(); |
michael@0 | 15 | let external = {}; |
michael@0 | 16 | for (let method of EXPORTED_METHODS) { |
michael@0 | 17 | external[method] = internal[method].bind(internal); |
michael@0 | 18 | } |
michael@0 | 19 | return Object.freeze(external); |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | function GlobalStateInternal() { |
michael@0 | 23 | // Storage for global state. |
michael@0 | 24 | this.state = {}; |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | GlobalStateInternal.prototype = { |
michael@0 | 28 | /** |
michael@0 | 29 | * Get all value from the global state. |
michael@0 | 30 | */ |
michael@0 | 31 | getState: function() { |
michael@0 | 32 | return this.state; |
michael@0 | 33 | }, |
michael@0 | 34 | |
michael@0 | 35 | /** |
michael@0 | 36 | * Clear all currently stored global state. |
michael@0 | 37 | */ |
michael@0 | 38 | clear: function() { |
michael@0 | 39 | this.state = {}; |
michael@0 | 40 | }, |
michael@0 | 41 | |
michael@0 | 42 | /** |
michael@0 | 43 | * Retrieve a value from the global state. |
michael@0 | 44 | * |
michael@0 | 45 | * @param aKey |
michael@0 | 46 | * A key the value is stored under. |
michael@0 | 47 | * @return The value stored at aKey, or an empty string if no value is set. |
michael@0 | 48 | */ |
michael@0 | 49 | get: function(aKey) { |
michael@0 | 50 | return this.state[aKey] || ""; |
michael@0 | 51 | }, |
michael@0 | 52 | |
michael@0 | 53 | /** |
michael@0 | 54 | * Set a global value. |
michael@0 | 55 | * |
michael@0 | 56 | * @param aKey |
michael@0 | 57 | * A key to store the value under. |
michael@0 | 58 | */ |
michael@0 | 59 | set: function(aKey, aStringValue) { |
michael@0 | 60 | this.state[aKey] = aStringValue; |
michael@0 | 61 | }, |
michael@0 | 62 | |
michael@0 | 63 | /** |
michael@0 | 64 | * Delete a global value. |
michael@0 | 65 | * |
michael@0 | 66 | * @param aKey |
michael@0 | 67 | * A key to delete the value for. |
michael@0 | 68 | */ |
michael@0 | 69 | delete: function(aKey) { |
michael@0 | 70 | delete this.state[aKey]; |
michael@0 | 71 | }, |
michael@0 | 72 | |
michael@0 | 73 | /** |
michael@0 | 74 | * Set the current global state from a state object. Any previous global |
michael@0 | 75 | * state will be removed, even if the new state does not contain a matching |
michael@0 | 76 | * key. |
michael@0 | 77 | * |
michael@0 | 78 | * @param aState |
michael@0 | 79 | * A state object to extract global state from to be set. |
michael@0 | 80 | */ |
michael@0 | 81 | setFromState: function (aState) { |
michael@0 | 82 | this.state = (aState && aState.global) || {}; |
michael@0 | 83 | } |
michael@0 | 84 | }; |