browser/components/sessionstore/src/GlobalState.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/sessionstore/src/GlobalState.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,84 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +this.EXPORTED_SYMBOLS = ["GlobalState"];
    1.11 +
    1.12 +const EXPORTED_METHODS = ["getState", "clear", "get", "set", "delete", "setFromState"];
    1.13 +/**
    1.14 + * Module that contains global session data.
    1.15 + */
    1.16 +function GlobalState() {
    1.17 +  let internal = new GlobalStateInternal();
    1.18 +  let external = {};
    1.19 +  for (let method of EXPORTED_METHODS) {
    1.20 +    external[method] = internal[method].bind(internal);
    1.21 +  }
    1.22 +  return Object.freeze(external);
    1.23 +}
    1.24 +
    1.25 +function GlobalStateInternal() {
    1.26 +  // Storage for global state.
    1.27 +  this.state = {};
    1.28 +}
    1.29 +
    1.30 +GlobalStateInternal.prototype = {
    1.31 +  /**
    1.32 +   * Get all value from the global state.
    1.33 +   */
    1.34 +  getState: function() {
    1.35 +    return this.state;
    1.36 +  },
    1.37 +
    1.38 +  /**
    1.39 +   * Clear all currently stored global state.
    1.40 +   */
    1.41 +  clear: function() {
    1.42 +    this.state = {};
    1.43 +  },
    1.44 +
    1.45 +  /**
    1.46 +   * Retrieve a value from the global state.
    1.47 +   *
    1.48 +   * @param aKey
    1.49 +   *        A key the value is stored under.
    1.50 +   * @return The value stored at aKey, or an empty string if no value is set.
    1.51 +   */
    1.52 +  get: function(aKey) {
    1.53 +    return this.state[aKey] || "";
    1.54 +  },
    1.55 +
    1.56 +  /**
    1.57 +   * Set a global value.
    1.58 +   *
    1.59 +   * @param aKey
    1.60 +   *        A key to store the value under.
    1.61 +   */
    1.62 +  set: function(aKey, aStringValue) {
    1.63 +    this.state[aKey] = aStringValue;
    1.64 +  },
    1.65 +
    1.66 +  /**
    1.67 +   * Delete a global value.
    1.68 +   *
    1.69 +   * @param aKey
    1.70 +   *        A key to delete the value for.
    1.71 +   */
    1.72 +  delete: function(aKey) {
    1.73 +    delete this.state[aKey];
    1.74 +  },
    1.75 +
    1.76 +  /**
    1.77 +   * Set the current global state from a state object. Any previous global
    1.78 +   * state will be removed, even if the new state does not contain a matching
    1.79 +   * key.
    1.80 +   *
    1.81 +   * @param aState
    1.82 +   *        A state object to extract global state from to be set.
    1.83 +   */
    1.84 +  setFromState: function (aState) {
    1.85 +    this.state = (aState && aState.global) || {};
    1.86 +  }
    1.87 +};

mercurial