browser/components/tabview/storage.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/tabview/storage.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,201 @@
     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
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +// **********
     1.9 +// Title: storage.js
    1.10 +
    1.11 +// ##########
    1.12 +// Class: Storage
    1.13 +// Singleton for permanent storage of TabView data.
    1.14 +let Storage = {
    1.15 +  GROUP_DATA_IDENTIFIER: "tabview-group",
    1.16 +  GROUPS_DATA_IDENTIFIER: "tabview-groups",
    1.17 +  TAB_DATA_IDENTIFIER: "tabview-tab",
    1.18 +  UI_DATA_IDENTIFIER: "tabview-ui",
    1.19 +
    1.20 +  // ----------
    1.21 +  // Function: toString
    1.22 +  // Prints [Storage] for debug use
    1.23 +  toString: function Storage_toString() {
    1.24 +    return "[Storage]";
    1.25 +  },
    1.26 +
    1.27 +  // ----------
    1.28 +  // Function: init
    1.29 +  // Sets up the object.
    1.30 +  init: function Storage_init() {
    1.31 +    this._sessionStore =
    1.32 +      Cc["@mozilla.org/browser/sessionstore;1"].
    1.33 +        getService(Ci.nsISessionStore);
    1.34 +  },
    1.35 +
    1.36 +  // ----------
    1.37 +  // Function: uninit
    1.38 +  uninit: function Storage_uninit () {
    1.39 +    this._sessionStore = null;
    1.40 +  },
    1.41 +
    1.42 +  // ----------
    1.43 +  // Function: saveTab
    1.44 +  // Saves the data for a single tab.
    1.45 +  saveTab: function Storage_saveTab(tab, data) {
    1.46 +    Utils.assert(tab, "tab");
    1.47 +
    1.48 +    this._sessionStore.setTabValue(tab, this.TAB_DATA_IDENTIFIER,
    1.49 +      JSON.stringify(data));
    1.50 +  },
    1.51 +
    1.52 +  // ----------
    1.53 +  // Function: getTabData
    1.54 +  // Load tab data from session store and return it.
    1.55 +  getTabData: function Storage_getTabData(tab) {
    1.56 +    Utils.assert(tab, "tab");
    1.57 +
    1.58 +    let existingData = null;
    1.59 +
    1.60 +    try {
    1.61 +      let tabData = this._sessionStore.getTabValue(tab, this.TAB_DATA_IDENTIFIER);
    1.62 +      if (tabData != "")
    1.63 +        existingData = JSON.parse(tabData);
    1.64 +    } catch (e) {
    1.65 +      // getTabValue will fail if the property doesn't exist.
    1.66 +      Utils.log(e);
    1.67 +    }
    1.68 +
    1.69 +    return existingData;
    1.70 +  },
    1.71 +
    1.72 +  // ----------
    1.73 +  // Function: getTabState
    1.74 +  // Returns the current state of the given tab.
    1.75 +  getTabState: function Storage_getTabState(tab) {
    1.76 +    Utils.assert(tab, "tab");
    1.77 +    let tabState;
    1.78 +
    1.79 +    try {
    1.80 +      tabState = JSON.parse(this._sessionStore.getTabState(tab));
    1.81 +    } catch (e) {}
    1.82 +
    1.83 +    return tabState;
    1.84 +  },
    1.85 +
    1.86 +  // ----------
    1.87 +  // Function: saveGroupItem
    1.88 +  // Saves the data for a single groupItem, associated with a specific window.
    1.89 +  saveGroupItem: function Storage_saveGroupItem(win, data) {
    1.90 +    var id = data.id;
    1.91 +    var existingData = this.readGroupItemData(win);
    1.92 +    existingData[id] = data;
    1.93 +    this._sessionStore.setWindowValue(win, this.GROUP_DATA_IDENTIFIER,
    1.94 +      JSON.stringify(existingData));
    1.95 +  },
    1.96 +
    1.97 +  // ----------
    1.98 +  // Function: deleteGroupItem
    1.99 +  // Deletes the data for a single groupItem from the given window.
   1.100 +  deleteGroupItem: function Storage_deleteGroupItem(win, id) {
   1.101 +    var existingData = this.readGroupItemData(win);
   1.102 +    delete existingData[id];
   1.103 +    this._sessionStore.setWindowValue(win, this.GROUP_DATA_IDENTIFIER,
   1.104 +      JSON.stringify(existingData));
   1.105 +  },
   1.106 +
   1.107 +  // ----------
   1.108 +  // Function: readGroupItemData
   1.109 +  // Returns the data for all groupItems associated with the given window.
   1.110 +  readGroupItemData: function Storage_readGroupItemData(win) {
   1.111 +    var existingData = {};
   1.112 +    let data;
   1.113 +    try {
   1.114 +      data = this._sessionStore.getWindowValue(win, this.GROUP_DATA_IDENTIFIER);
   1.115 +      if (data)
   1.116 +        existingData = JSON.parse(data);
   1.117 +    } catch (e) {
   1.118 +      // getWindowValue will fail if the property doesn't exist
   1.119 +      Utils.log("Error in readGroupItemData: "+e, data);
   1.120 +    }
   1.121 +    return existingData;
   1.122 +  },
   1.123 +
   1.124 +  // ----------
   1.125 +  // Function: readWindowBusyState
   1.126 +  // Returns the current busyState for the given window.
   1.127 +  readWindowBusyState: function Storage_readWindowBusyState(win) {
   1.128 +    let state;
   1.129 +
   1.130 +    try {
   1.131 +      let data = this._sessionStore.getWindowState(win);
   1.132 +      if (data)
   1.133 +        state = JSON.parse(data);
   1.134 +    } catch (e) {
   1.135 +      Utils.log("Error while parsing window state");
   1.136 +    }
   1.137 +
   1.138 +    return (state && state.windows[0].busy);
   1.139 +  },
   1.140 +
   1.141 +  // ----------
   1.142 +  // Function: saveGroupItemsData
   1.143 +  // Saves the global data for the <GroupItems> singleton for the given window.
   1.144 +  saveGroupItemsData: function Storage_saveGroupItemsData(win, data) {
   1.145 +    this.saveData(win, this.GROUPS_DATA_IDENTIFIER, data);
   1.146 +  },
   1.147 +
   1.148 +  // ----------
   1.149 +  // Function: readGroupItemsData
   1.150 +  // Reads the global data for the <GroupItems> singleton for the given window.
   1.151 +  readGroupItemsData: function Storage_readGroupItemsData(win) {
   1.152 +    return this.readData(win, this.GROUPS_DATA_IDENTIFIER);
   1.153 +  },
   1.154 +
   1.155 +  // ----------
   1.156 +  // Function: saveUIData
   1.157 +  // Saves the global data for the <UIManager> singleton for the given window.
   1.158 +  saveUIData: function Storage_saveUIData(win, data) {
   1.159 +    this.saveData(win, this.UI_DATA_IDENTIFIER, data);
   1.160 +  },
   1.161 +
   1.162 +  // ----------
   1.163 +  // Function: readUIData
   1.164 +  // Reads the global data for the <UIManager> singleton for the given window.
   1.165 +  readUIData: function Storage_readUIData(win) {
   1.166 +    return this.readData(win, this.UI_DATA_IDENTIFIER);
   1.167 +  },
   1.168 +
   1.169 +  // ----------
   1.170 +  // Function: saveVisibilityData
   1.171 +  // Saves visibility for the given window.
   1.172 +  saveVisibilityData: function Storage_saveVisibilityData(win, data) {
   1.173 +    this._sessionStore.setWindowValue(
   1.174 +      win, win.TabView.VISIBILITY_IDENTIFIER, data);
   1.175 +  },
   1.176 +
   1.177 +  // ----------
   1.178 +  // Function: saveData
   1.179 +  // Generic routine for saving data to a window.
   1.180 +  saveData: function Storage_saveData(win, id, data) {
   1.181 +    try {
   1.182 +      this._sessionStore.setWindowValue(win, id, JSON.stringify(data));
   1.183 +    } catch (e) {
   1.184 +      Utils.log("Error in saveData: "+e);
   1.185 +    }
   1.186 +  },
   1.187 +
   1.188 +  // ----------
   1.189 +  // Function: readData
   1.190 +  // Generic routine for reading data from a window.
   1.191 +  readData: function Storage_readData(win, id) {
   1.192 +    var existingData = {};
   1.193 +    try {
   1.194 +      var data = this._sessionStore.getWindowValue(win, id);
   1.195 +      if (data)
   1.196 +        existingData = JSON.parse(data);
   1.197 +    } catch (e) {
   1.198 +      Utils.log("Error in readData: "+e);
   1.199 +    }
   1.200 +
   1.201 +    return existingData;
   1.202 +  }
   1.203 +};
   1.204 +

mercurial