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