Backed out changeset 496886cb30a5 (bug 1867152) for bc failures on browser_user_input...
[gecko.git] / browser / components / sessionstore / GlobalState.sys.mjs
bloba49fe4650d89c9f9221c191982faa3bf1da67556
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 const EXPORTED_METHODS = [
6   "getState",
7   "clear",
8   "get",
9   "set",
10   "delete",
11   "setFromState",
14 /**
15  * Module that contains global session data.
16  */
17 export function GlobalState() {
18   let internal = new GlobalStateInternal();
19   let external = {};
20   for (let method of EXPORTED_METHODS) {
21     external[method] = internal[method].bind(internal);
22   }
23   return Object.freeze(external);
26 function GlobalStateInternal() {
27   // Storage for global state.
28   this.state = {};
31 GlobalStateInternal.prototype = {
32   /**
33    * Get all value from the global state.
34    */
35   getState() {
36     return this.state;
37   },
39   /**
40    * Clear all currently stored global state.
41    */
42   clear() {
43     this.state = {};
44   },
46   /**
47    * Retrieve a value from the global state.
48    *
49    * @param aKey
50    *        A key the value is stored under.
51    * @return The value stored at aKey, or an empty string if no value is set.
52    */
53   get(aKey) {
54     return this.state[aKey] || "";
55   },
57   /**
58    * Set a global value.
59    *
60    * @param aKey
61    *        A key to store the value under.
62    */
63   set(aKey, aStringValue) {
64     this.state[aKey] = aStringValue;
65   },
67   /**
68    * Delete a global value.
69    *
70    * @param aKey
71    *        A key to delete the value for.
72    */
73   delete(aKey) {
74     delete this.state[aKey];
75   },
77   /**
78    * Set the current global state from a state object. Any previous global
79    * state will be removed, even if the new state does not contain a matching
80    * key.
81    *
82    * @param aState
83    *        A state object to extract global state from to be set.
84    */
85   setFromState(aState) {
86     this.state = (aState && aState.global) || {};
87   },