|
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/. */ |
|
4 |
|
5 // ********** |
|
6 // Title: storage.js |
|
7 |
|
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", |
|
16 |
|
17 // ---------- |
|
18 // Function: toString |
|
19 // Prints [Storage] for debug use |
|
20 toString: function Storage_toString() { |
|
21 return "[Storage]"; |
|
22 }, |
|
23 |
|
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 }, |
|
32 |
|
33 // ---------- |
|
34 // Function: uninit |
|
35 uninit: function Storage_uninit () { |
|
36 this._sessionStore = null; |
|
37 }, |
|
38 |
|
39 // ---------- |
|
40 // Function: saveTab |
|
41 // Saves the data for a single tab. |
|
42 saveTab: function Storage_saveTab(tab, data) { |
|
43 Utils.assert(tab, "tab"); |
|
44 |
|
45 this._sessionStore.setTabValue(tab, this.TAB_DATA_IDENTIFIER, |
|
46 JSON.stringify(data)); |
|
47 }, |
|
48 |
|
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"); |
|
54 |
|
55 let existingData = null; |
|
56 |
|
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 } |
|
65 |
|
66 return existingData; |
|
67 }, |
|
68 |
|
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; |
|
75 |
|
76 try { |
|
77 tabState = JSON.parse(this._sessionStore.getTabState(tab)); |
|
78 } catch (e) {} |
|
79 |
|
80 return tabState; |
|
81 }, |
|
82 |
|
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 }, |
|
93 |
|
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 }, |
|
103 |
|
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 }, |
|
120 |
|
121 // ---------- |
|
122 // Function: readWindowBusyState |
|
123 // Returns the current busyState for the given window. |
|
124 readWindowBusyState: function Storage_readWindowBusyState(win) { |
|
125 let state; |
|
126 |
|
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 } |
|
134 |
|
135 return (state && state.windows[0].busy); |
|
136 }, |
|
137 |
|
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 }, |
|
144 |
|
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 }, |
|
151 |
|
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 }, |
|
158 |
|
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 }, |
|
165 |
|
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 }, |
|
173 |
|
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 }, |
|
184 |
|
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 } |
|
197 |
|
198 return existingData; |
|
199 } |
|
200 }; |
|
201 |