2010-06-17 00:19:11 +00:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is storage.js.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
2010-08-11 22:28:45 +00:00
|
|
|
* the Mozilla Foundation.
|
2010-06-17 00:19:11 +00:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2010-08-11 22:28:45 +00:00
|
|
|
* Ehsan Akhgari <ehsan@mozilla.com>
|
2010-06-17 00:19:11 +00:00
|
|
|
* Ian Gilman <ian@iangilman.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
// **********
|
2010-07-18 15:58:10 +00:00
|
|
|
// Title: storage.js
|
2010-05-12 00:22:06 +00:00
|
|
|
|
2010-04-22 21:26:57 +00:00
|
|
|
// ##########
|
2010-07-16 00:23:39 +00:00
|
|
|
// Class: Storage
|
2010-07-29 19:37:25 +00:00
|
|
|
// Singleton for permanent storage of TabView data.
|
2010-08-12 02:01:29 +00:00
|
|
|
let Storage = {
|
2010-08-11 22:28:45 +00:00
|
|
|
GROUP_DATA_IDENTIFIER: "tabview-group",
|
2010-07-29 19:37:25 +00:00
|
|
|
GROUPS_DATA_IDENTIFIER: "tabview-groups",
|
2010-08-11 22:28:45 +00:00
|
|
|
TAB_DATA_IDENTIFIER: "tabview-tab",
|
|
|
|
UI_DATA_IDENTIFIER: "tabview-ui",
|
2010-05-21 22:44:15 +00:00
|
|
|
|
2010-04-23 00:19:42 +00:00
|
|
|
// ----------
|
2010-07-16 00:23:39 +00:00
|
|
|
// Function: init
|
2010-07-18 15:58:10 +00:00
|
|
|
// Sets up the object.
|
2010-04-22 21:26:57 +00:00
|
|
|
init: function() {
|
2010-07-03 04:33:33 +00:00
|
|
|
this._sessionStore =
|
2010-08-11 22:28:45 +00:00
|
|
|
Cc["@mozilla.org/browser/sessionstore;1"].
|
|
|
|
getService(Ci.nsISessionStore);
|
2010-05-21 22:44:15 +00:00
|
|
|
},
|
|
|
|
|
2010-08-12 04:36:58 +00:00
|
|
|
// ----------
|
|
|
|
// Function: uninit
|
|
|
|
uninit : function() {
|
|
|
|
this._sessionStore = null;
|
|
|
|
},
|
|
|
|
|
2010-05-24 21:49:03 +00:00
|
|
|
// ----------
|
2010-07-16 00:23:39 +00:00
|
|
|
// Function: wipe
|
2010-07-18 15:58:10 +00:00
|
|
|
// Cleans out all the stored data, leaving empty objects.
|
2010-05-24 21:49:03 +00:00
|
|
|
wipe: function() {
|
|
|
|
try {
|
|
|
|
var self = this;
|
2010-07-18 15:58:10 +00:00
|
|
|
|
2010-05-24 21:49:03 +00:00
|
|
|
// ___ Tabs
|
2010-08-06 22:19:57 +00:00
|
|
|
AllTabs.tabs.forEach(function(tab) {
|
2010-07-28 21:20:41 +00:00
|
|
|
if (tab.ownerDocument.defaultView != gWindow)
|
|
|
|
return;
|
|
|
|
|
2010-07-22 22:09:36 +00:00
|
|
|
self.saveTab(tab, null);
|
2010-05-24 21:49:03 +00:00
|
|
|
});
|
2010-07-18 15:58:10 +00:00
|
|
|
|
2010-05-24 21:49:03 +00:00
|
|
|
// ___ Other
|
2010-08-06 22:46:55 +00:00
|
|
|
this.saveGroupItemsData(gWindow, {});
|
2010-07-22 19:34:13 +00:00
|
|
|
this.saveUIData(gWindow, {});
|
2010-07-18 15:58:10 +00:00
|
|
|
|
2010-07-22 19:34:13 +00:00
|
|
|
this._sessionStore.setWindowValue(gWindow, this.GROUP_DATA_IDENTIFIER,
|
2010-05-24 21:49:03 +00:00
|
|
|
JSON.stringify({}));
|
|
|
|
} catch (e) {
|
|
|
|
Utils.log("Error in wipe: "+e);
|
|
|
|
}
|
|
|
|
},
|
2010-07-18 15:58:10 +00:00
|
|
|
|
2010-05-21 22:44:15 +00:00
|
|
|
// ----------
|
2010-07-16 00:23:39 +00:00
|
|
|
// Function: saveTab
|
2010-07-18 15:58:10 +00:00
|
|
|
// Saves the data for a single tab.
|
2010-05-21 22:44:15 +00:00
|
|
|
saveTab: function(tab, data) {
|
2010-08-10 18:13:10 +00:00
|
|
|
Utils.assert(tab, "tab");
|
2010-06-14 23:56:27 +00:00
|
|
|
|
2010-05-21 22:44:15 +00:00
|
|
|
this._sessionStore.setTabValue(tab, this.TAB_DATA_IDENTIFIER,
|
|
|
|
JSON.stringify(data));
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
2010-07-16 00:23:39 +00:00
|
|
|
// Function: getTabData
|
|
|
|
// Returns the data object associated with a single tab.
|
2010-05-21 22:44:15 +00:00
|
|
|
getTabData: function(tab) {
|
2010-08-10 18:13:10 +00:00
|
|
|
Utils.assert(tab, "tab");
|
2010-06-14 23:56:27 +00:00
|
|
|
|
2010-05-21 22:44:15 +00:00
|
|
|
var existingData = null;
|
|
|
|
try {
|
|
|
|
var tabData = this._sessionStore.getTabValue(tab, this.TAB_DATA_IDENTIFIER);
|
|
|
|
if (tabData != "") {
|
|
|
|
existingData = JSON.parse(tabData);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// getWindowValue will fail if the property doesn't exist
|
2010-06-14 23:56:27 +00:00
|
|
|
Utils.log(e);
|
2010-05-21 22:44:15 +00:00
|
|
|
}
|
2010-07-18 15:58:10 +00:00
|
|
|
|
2010-05-21 22:44:15 +00:00
|
|
|
return existingData;
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
2010-08-06 22:46:55 +00:00
|
|
|
// Function: saveGroupItem
|
|
|
|
// Saves the data for a single groupItem, associated with a specific window.
|
|
|
|
saveGroupItem: function(win, data) {
|
2010-05-21 22:44:15 +00:00
|
|
|
var id = data.id;
|
2010-08-06 22:46:55 +00:00
|
|
|
var existingData = this.readGroupItemData(win);
|
2010-05-21 22:44:15 +00:00
|
|
|
existingData[id] = data;
|
|
|
|
this._sessionStore.setWindowValue(win, this.GROUP_DATA_IDENTIFIER,
|
|
|
|
JSON.stringify(existingData));
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
2010-08-06 22:46:55 +00:00
|
|
|
// Function: deleteGroupItem
|
|
|
|
// Deletes the data for a single groupItem from the given window.
|
|
|
|
deleteGroupItem: function(win, id) {
|
|
|
|
var existingData = this.readGroupItemData(win);
|
2010-05-21 22:44:15 +00:00
|
|
|
delete existingData[id];
|
|
|
|
this._sessionStore.setWindowValue(win, this.GROUP_DATA_IDENTIFIER,
|
|
|
|
JSON.stringify(existingData));
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
2010-08-06 22:46:55 +00:00
|
|
|
// Function: readGroupItemData
|
|
|
|
// Returns the data for all groupItems associated with the given window.
|
|
|
|
readGroupItemData: function(win) {
|
2010-05-21 22:44:15 +00:00
|
|
|
var existingData = {};
|
|
|
|
try {
|
|
|
|
existingData = JSON.parse(
|
|
|
|
this._sessionStore.getWindowValue(win, this.GROUP_DATA_IDENTIFIER)
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
// getWindowValue will fail if the property doesn't exist
|
2010-08-06 22:46:55 +00:00
|
|
|
Utils.log("Error in readGroupItemData: "+e);
|
2010-05-21 22:44:15 +00:00
|
|
|
}
|
|
|
|
return existingData;
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
2010-08-06 22:46:55 +00:00
|
|
|
// Function: saveGroupItemsData
|
|
|
|
// Saves the global data for the <GroupItems> singleton for the given window.
|
|
|
|
saveGroupItemsData: function(win, data) {
|
2010-05-24 21:49:03 +00:00
|
|
|
this.saveData(win, this.GROUPS_DATA_IDENTIFIER, data);
|
2010-05-21 22:44:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
2010-08-06 22:46:55 +00:00
|
|
|
// Function: readGroupItemsData
|
|
|
|
// Reads the global data for the <GroupItems> singleton for the given window.
|
|
|
|
readGroupItemsData: function(win) {
|
2010-05-24 21:49:03 +00:00
|
|
|
return this.readData(win, this.GROUPS_DATA_IDENTIFIER);
|
2010-04-23 00:19:42 +00:00
|
|
|
},
|
2010-07-18 15:58:10 +00:00
|
|
|
|
2010-04-23 00:19:42 +00:00
|
|
|
// ----------
|
2010-07-16 00:23:39 +00:00
|
|
|
// Function: saveUIData
|
2010-07-23 07:03:40 +00:00
|
|
|
// Saves the global data for the <UIManager> singleton for the given window.
|
2010-05-24 21:49:03 +00:00
|
|
|
saveUIData: function(win, data) {
|
|
|
|
this.saveData(win, this.UI_DATA_IDENTIFIER, data);
|
2010-04-23 00:19:42 +00:00
|
|
|
},
|
2010-05-24 21:49:03 +00:00
|
|
|
|
2010-04-23 00:19:42 +00:00
|
|
|
// ----------
|
2010-07-16 00:23:39 +00:00
|
|
|
// Function: readUIData
|
2010-07-23 07:03:40 +00:00
|
|
|
// Reads the global data for the <UIManager> singleton for the given window.
|
2010-05-24 21:49:03 +00:00
|
|
|
readUIData: function(win) {
|
|
|
|
return this.readData(win, this.UI_DATA_IDENTIFIER);
|
2010-04-23 00:19:42 +00:00
|
|
|
},
|
2010-07-13 04:04:04 +00:00
|
|
|
|
2010-05-24 21:49:03 +00:00
|
|
|
// ----------
|
2010-07-16 00:23:39 +00:00
|
|
|
// Function: saveData
|
2010-07-18 15:58:10 +00:00
|
|
|
// Generic routine for saving data to a window.
|
2010-05-24 21:49:03 +00:00
|
|
|
saveData: function(win, id, data) {
|
|
|
|
try {
|
|
|
|
this._sessionStore.setWindowValue(win, id, JSON.stringify(data));
|
|
|
|
} catch (e) {
|
|
|
|
Utils.log("Error in saveData: "+e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
2010-07-16 00:23:39 +00:00
|
|
|
// Function: readData
|
2010-07-18 15:58:10 +00:00
|
|
|
// Generic routine for reading data from a window.
|
2010-05-24 21:49:03 +00:00
|
|
|
readData: function(win, id) {
|
|
|
|
var existingData = {};
|
|
|
|
try {
|
|
|
|
var data = this._sessionStore.getWindowValue(win, id);
|
2010-07-12 00:54:42 +00:00
|
|
|
if (data)
|
2010-05-24 21:49:03 +00:00
|
|
|
existingData = JSON.parse(data);
|
|
|
|
} catch (e) {
|
|
|
|
Utils.log("Error in readData: "+e);
|
|
|
|
}
|
2010-07-18 15:58:10 +00:00
|
|
|
|
2010-05-24 21:49:03 +00:00
|
|
|
return existingData;
|
2010-04-22 21:26:57 +00:00
|
|
|
}
|
2010-07-22 19:35:11 +00:00
|
|
|
};
|