michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // ********** michael@0: // Title: storage.js michael@0: michael@0: // ########## michael@0: // Class: Storage michael@0: // Singleton for permanent storage of TabView data. michael@0: let Storage = { michael@0: GROUP_DATA_IDENTIFIER: "tabview-group", michael@0: GROUPS_DATA_IDENTIFIER: "tabview-groups", michael@0: TAB_DATA_IDENTIFIER: "tabview-tab", michael@0: UI_DATA_IDENTIFIER: "tabview-ui", michael@0: michael@0: // ---------- michael@0: // Function: toString michael@0: // Prints [Storage] for debug use michael@0: toString: function Storage_toString() { michael@0: return "[Storage]"; michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: init michael@0: // Sets up the object. michael@0: init: function Storage_init() { michael@0: this._sessionStore = michael@0: Cc["@mozilla.org/browser/sessionstore;1"]. michael@0: getService(Ci.nsISessionStore); michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: uninit michael@0: uninit: function Storage_uninit () { michael@0: this._sessionStore = null; michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: saveTab michael@0: // Saves the data for a single tab. michael@0: saveTab: function Storage_saveTab(tab, data) { michael@0: Utils.assert(tab, "tab"); michael@0: michael@0: this._sessionStore.setTabValue(tab, this.TAB_DATA_IDENTIFIER, michael@0: JSON.stringify(data)); michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: getTabData michael@0: // Load tab data from session store and return it. michael@0: getTabData: function Storage_getTabData(tab) { michael@0: Utils.assert(tab, "tab"); michael@0: michael@0: let existingData = null; michael@0: michael@0: try { michael@0: let tabData = this._sessionStore.getTabValue(tab, this.TAB_DATA_IDENTIFIER); michael@0: if (tabData != "") michael@0: existingData = JSON.parse(tabData); michael@0: } catch (e) { michael@0: // getTabValue will fail if the property doesn't exist. michael@0: Utils.log(e); michael@0: } michael@0: michael@0: return existingData; michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: getTabState michael@0: // Returns the current state of the given tab. michael@0: getTabState: function Storage_getTabState(tab) { michael@0: Utils.assert(tab, "tab"); michael@0: let tabState; michael@0: michael@0: try { michael@0: tabState = JSON.parse(this._sessionStore.getTabState(tab)); michael@0: } catch (e) {} michael@0: michael@0: return tabState; michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: saveGroupItem michael@0: // Saves the data for a single groupItem, associated with a specific window. michael@0: saveGroupItem: function Storage_saveGroupItem(win, data) { michael@0: var id = data.id; michael@0: var existingData = this.readGroupItemData(win); michael@0: existingData[id] = data; michael@0: this._sessionStore.setWindowValue(win, this.GROUP_DATA_IDENTIFIER, michael@0: JSON.stringify(existingData)); michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: deleteGroupItem michael@0: // Deletes the data for a single groupItem from the given window. michael@0: deleteGroupItem: function Storage_deleteGroupItem(win, id) { michael@0: var existingData = this.readGroupItemData(win); michael@0: delete existingData[id]; michael@0: this._sessionStore.setWindowValue(win, this.GROUP_DATA_IDENTIFIER, michael@0: JSON.stringify(existingData)); michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: readGroupItemData michael@0: // Returns the data for all groupItems associated with the given window. michael@0: readGroupItemData: function Storage_readGroupItemData(win) { michael@0: var existingData = {}; michael@0: let data; michael@0: try { michael@0: data = this._sessionStore.getWindowValue(win, this.GROUP_DATA_IDENTIFIER); michael@0: if (data) michael@0: existingData = JSON.parse(data); michael@0: } catch (e) { michael@0: // getWindowValue will fail if the property doesn't exist michael@0: Utils.log("Error in readGroupItemData: "+e, data); michael@0: } michael@0: return existingData; michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: readWindowBusyState michael@0: // Returns the current busyState for the given window. michael@0: readWindowBusyState: function Storage_readWindowBusyState(win) { michael@0: let state; michael@0: michael@0: try { michael@0: let data = this._sessionStore.getWindowState(win); michael@0: if (data) michael@0: state = JSON.parse(data); michael@0: } catch (e) { michael@0: Utils.log("Error while parsing window state"); michael@0: } michael@0: michael@0: return (state && state.windows[0].busy); michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: saveGroupItemsData michael@0: // Saves the global data for the singleton for the given window. michael@0: saveGroupItemsData: function Storage_saveGroupItemsData(win, data) { michael@0: this.saveData(win, this.GROUPS_DATA_IDENTIFIER, data); michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: readGroupItemsData michael@0: // Reads the global data for the singleton for the given window. michael@0: readGroupItemsData: function Storage_readGroupItemsData(win) { michael@0: return this.readData(win, this.GROUPS_DATA_IDENTIFIER); michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: saveUIData michael@0: // Saves the global data for the singleton for the given window. michael@0: saveUIData: function Storage_saveUIData(win, data) { michael@0: this.saveData(win, this.UI_DATA_IDENTIFIER, data); michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: readUIData michael@0: // Reads the global data for the singleton for the given window. michael@0: readUIData: function Storage_readUIData(win) { michael@0: return this.readData(win, this.UI_DATA_IDENTIFIER); michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: saveVisibilityData michael@0: // Saves visibility for the given window. michael@0: saveVisibilityData: function Storage_saveVisibilityData(win, data) { michael@0: this._sessionStore.setWindowValue( michael@0: win, win.TabView.VISIBILITY_IDENTIFIER, data); michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: saveData michael@0: // Generic routine for saving data to a window. michael@0: saveData: function Storage_saveData(win, id, data) { michael@0: try { michael@0: this._sessionStore.setWindowValue(win, id, JSON.stringify(data)); michael@0: } catch (e) { michael@0: Utils.log("Error in saveData: "+e); michael@0: } michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: readData michael@0: // Generic routine for reading data from a window. michael@0: readData: function Storage_readData(win, id) { michael@0: var existingData = {}; michael@0: try { michael@0: var data = this._sessionStore.getWindowValue(win, id); michael@0: if (data) michael@0: existingData = JSON.parse(data); michael@0: } catch (e) { michael@0: Utils.log("Error in readData: "+e); michael@0: } michael@0: michael@0: return existingData; michael@0: } michael@0: }; michael@0: