Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 // **********
6 // Title: storage.js
8 // ##########
9 // Class: Storage
10 // Singleton for permanent storage of TabView data.
11 let Storage = {
12 GROUP_DATA_IDENTIFIER: "tabview-group",
13 GROUPS_DATA_IDENTIFIER: "tabview-groups",
14 TAB_DATA_IDENTIFIER: "tabview-tab",
15 UI_DATA_IDENTIFIER: "tabview-ui",
17 // ----------
18 // Function: toString
19 // Prints [Storage] for debug use
20 toString: function Storage_toString() {
21 return "[Storage]";
22 },
24 // ----------
25 // Function: init
26 // Sets up the object.
27 init: function Storage_init() {
28 this._sessionStore =
29 Cc["@mozilla.org/browser/sessionstore;1"].
30 getService(Ci.nsISessionStore);
31 },
33 // ----------
34 // Function: uninit
35 uninit: function Storage_uninit () {
36 this._sessionStore = null;
37 },
39 // ----------
40 // Function: saveTab
41 // Saves the data for a single tab.
42 saveTab: function Storage_saveTab(tab, data) {
43 Utils.assert(tab, "tab");
45 this._sessionStore.setTabValue(tab, this.TAB_DATA_IDENTIFIER,
46 JSON.stringify(data));
47 },
49 // ----------
50 // Function: getTabData
51 // Load tab data from session store and return it.
52 getTabData: function Storage_getTabData(tab) {
53 Utils.assert(tab, "tab");
55 let existingData = null;
57 try {
58 let tabData = this._sessionStore.getTabValue(tab, this.TAB_DATA_IDENTIFIER);
59 if (tabData != "")
60 existingData = JSON.parse(tabData);
61 } catch (e) {
62 // getTabValue will fail if the property doesn't exist.
63 Utils.log(e);
64 }
66 return existingData;
67 },
69 // ----------
70 // Function: getTabState
71 // Returns the current state of the given tab.
72 getTabState: function Storage_getTabState(tab) {
73 Utils.assert(tab, "tab");
74 let tabState;
76 try {
77 tabState = JSON.parse(this._sessionStore.getTabState(tab));
78 } catch (e) {}
80 return tabState;
81 },
83 // ----------
84 // Function: saveGroupItem
85 // Saves the data for a single groupItem, associated with a specific window.
86 saveGroupItem: function Storage_saveGroupItem(win, data) {
87 var id = data.id;
88 var existingData = this.readGroupItemData(win);
89 existingData[id] = data;
90 this._sessionStore.setWindowValue(win, this.GROUP_DATA_IDENTIFIER,
91 JSON.stringify(existingData));
92 },
94 // ----------
95 // Function: deleteGroupItem
96 // Deletes the data for a single groupItem from the given window.
97 deleteGroupItem: function Storage_deleteGroupItem(win, id) {
98 var existingData = this.readGroupItemData(win);
99 delete existingData[id];
100 this._sessionStore.setWindowValue(win, this.GROUP_DATA_IDENTIFIER,
101 JSON.stringify(existingData));
102 },
104 // ----------
105 // Function: readGroupItemData
106 // Returns the data for all groupItems associated with the given window.
107 readGroupItemData: function Storage_readGroupItemData(win) {
108 var existingData = {};
109 let data;
110 try {
111 data = this._sessionStore.getWindowValue(win, this.GROUP_DATA_IDENTIFIER);
112 if (data)
113 existingData = JSON.parse(data);
114 } catch (e) {
115 // getWindowValue will fail if the property doesn't exist
116 Utils.log("Error in readGroupItemData: "+e, data);
117 }
118 return existingData;
119 },
121 // ----------
122 // Function: readWindowBusyState
123 // Returns the current busyState for the given window.
124 readWindowBusyState: function Storage_readWindowBusyState(win) {
125 let state;
127 try {
128 let data = this._sessionStore.getWindowState(win);
129 if (data)
130 state = JSON.parse(data);
131 } catch (e) {
132 Utils.log("Error while parsing window state");
133 }
135 return (state && state.windows[0].busy);
136 },
138 // ----------
139 // Function: saveGroupItemsData
140 // Saves the global data for the <GroupItems> singleton for the given window.
141 saveGroupItemsData: function Storage_saveGroupItemsData(win, data) {
142 this.saveData(win, this.GROUPS_DATA_IDENTIFIER, data);
143 },
145 // ----------
146 // Function: readGroupItemsData
147 // Reads the global data for the <GroupItems> singleton for the given window.
148 readGroupItemsData: function Storage_readGroupItemsData(win) {
149 return this.readData(win, this.GROUPS_DATA_IDENTIFIER);
150 },
152 // ----------
153 // Function: saveUIData
154 // Saves the global data for the <UIManager> singleton for the given window.
155 saveUIData: function Storage_saveUIData(win, data) {
156 this.saveData(win, this.UI_DATA_IDENTIFIER, data);
157 },
159 // ----------
160 // Function: readUIData
161 // Reads the global data for the <UIManager> singleton for the given window.
162 readUIData: function Storage_readUIData(win) {
163 return this.readData(win, this.UI_DATA_IDENTIFIER);
164 },
166 // ----------
167 // Function: saveVisibilityData
168 // Saves visibility for the given window.
169 saveVisibilityData: function Storage_saveVisibilityData(win, data) {
170 this._sessionStore.setWindowValue(
171 win, win.TabView.VISIBILITY_IDENTIFIER, data);
172 },
174 // ----------
175 // Function: saveData
176 // Generic routine for saving data to a window.
177 saveData: function Storage_saveData(win, id, data) {
178 try {
179 this._sessionStore.setWindowValue(win, id, JSON.stringify(data));
180 } catch (e) {
181 Utils.log("Error in saveData: "+e);
182 }
183 },
185 // ----------
186 // Function: readData
187 // Generic routine for reading data from a window.
188 readData: function Storage_readData(win, id) {
189 var existingData = {};
190 try {
191 var data = this._sessionStore.getWindowValue(win, id);
192 if (data)
193 existingData = JSON.parse(data);
194 } catch (e) {
195 Utils.log("Error in readData: "+e);
196 }
198 return existingData;
199 }
200 };