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 file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | /** |
michael@0 | 6 | * A worker dedicated to handle I/O for Session Store. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | "use strict"; |
michael@0 | 10 | |
michael@0 | 11 | importScripts("resource://gre/modules/osfile.jsm"); |
michael@0 | 12 | |
michael@0 | 13 | let File = OS.File; |
michael@0 | 14 | let Encoder = new TextEncoder(); |
michael@0 | 15 | let Decoder = new TextDecoder(); |
michael@0 | 16 | |
michael@0 | 17 | /** |
michael@0 | 18 | * Communications with the controller. |
michael@0 | 19 | * |
michael@0 | 20 | * Accepts messages: |
michael@0 | 21 | * {fun:function_name, args:array_of_arguments_or_null, id: custom_id} |
michael@0 | 22 | * |
michael@0 | 23 | * Sends messages: |
michael@0 | 24 | * {ok: result, id: custom_id, telemetry: {}} / |
michael@0 | 25 | * {fail: serialized_form_of_OS.File.Error, id: custom_id} |
michael@0 | 26 | */ |
michael@0 | 27 | self.onmessage = function (msg) { |
michael@0 | 28 | let data = msg.data; |
michael@0 | 29 | if (!(data.fun in Agent)) { |
michael@0 | 30 | throw new Error("Cannot find method " + data.fun); |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | let result; |
michael@0 | 34 | let id = data.id; |
michael@0 | 35 | |
michael@0 | 36 | try { |
michael@0 | 37 | result = Agent[data.fun].apply(Agent, data.args) || {}; |
michael@0 | 38 | } catch (ex if ex instanceof OS.File.Error) { |
michael@0 | 39 | // Instances of OS.File.Error know how to serialize themselves |
michael@0 | 40 | // (deserialization ensures that we end up with OS-specific |
michael@0 | 41 | // instances of |OS.File.Error|) |
michael@0 | 42 | self.postMessage({fail: OS.File.Error.toMsg(ex), id: id}); |
michael@0 | 43 | return; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | // Other exceptions do not, and should be propagated through DOM's |
michael@0 | 47 | // built-in mechanism for uncaught errors, although this mechanism |
michael@0 | 48 | // may lose interesting information. |
michael@0 | 49 | self.postMessage({ |
michael@0 | 50 | ok: result.result, |
michael@0 | 51 | id: id, |
michael@0 | 52 | telemetry: result.telemetry || {} |
michael@0 | 53 | }); |
michael@0 | 54 | }; |
michael@0 | 55 | |
michael@0 | 56 | let Agent = { |
michael@0 | 57 | // Boolean that tells whether we already made a |
michael@0 | 58 | // call to write(). We will only attempt to move |
michael@0 | 59 | // sessionstore.js to sessionstore.bak on the |
michael@0 | 60 | // first write. |
michael@0 | 61 | hasWrittenState: false, |
michael@0 | 62 | |
michael@0 | 63 | // The path to sessionstore.js |
michael@0 | 64 | path: OS.Path.join(OS.Constants.Path.profileDir, "sessionstore.js"), |
michael@0 | 65 | |
michael@0 | 66 | // The path to sessionstore.bak |
michael@0 | 67 | backupPath: OS.Path.join(OS.Constants.Path.profileDir, "sessionstore.bak"), |
michael@0 | 68 | |
michael@0 | 69 | /** |
michael@0 | 70 | * NO-OP to start the worker. |
michael@0 | 71 | */ |
michael@0 | 72 | init: function () { |
michael@0 | 73 | return {result: true}; |
michael@0 | 74 | }, |
michael@0 | 75 | |
michael@0 | 76 | /** |
michael@0 | 77 | * Write the session to disk. |
michael@0 | 78 | */ |
michael@0 | 79 | write: function (stateString) { |
michael@0 | 80 | let exn; |
michael@0 | 81 | let telemetry = {}; |
michael@0 | 82 | |
michael@0 | 83 | if (!this.hasWrittenState) { |
michael@0 | 84 | try { |
michael@0 | 85 | let startMs = Date.now(); |
michael@0 | 86 | File.move(this.path, this.backupPath); |
michael@0 | 87 | telemetry.FX_SESSION_RESTORE_BACKUP_FILE_MS = Date.now() - startMs; |
michael@0 | 88 | } catch (ex if isNoSuchFileEx(ex)) { |
michael@0 | 89 | // Ignore exceptions about non-existent files. |
michael@0 | 90 | } catch (ex) { |
michael@0 | 91 | // Throw the exception after we wrote the state to disk |
michael@0 | 92 | // so that the backup can't interfere with the actual write. |
michael@0 | 93 | exn = ex; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | this.hasWrittenState = true; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | let ret = this._write(stateString, telemetry); |
michael@0 | 100 | |
michael@0 | 101 | if (exn) { |
michael@0 | 102 | throw exn; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | return ret; |
michael@0 | 106 | }, |
michael@0 | 107 | |
michael@0 | 108 | /** |
michael@0 | 109 | * Extract all sorts of useful statistics from a state string, |
michael@0 | 110 | * for use with Telemetry. |
michael@0 | 111 | * |
michael@0 | 112 | * @return {object} |
michael@0 | 113 | */ |
michael@0 | 114 | gatherTelemetry: function (stateString) { |
michael@0 | 115 | return Statistics.collect(stateString); |
michael@0 | 116 | }, |
michael@0 | 117 | |
michael@0 | 118 | /** |
michael@0 | 119 | * Write a stateString to disk |
michael@0 | 120 | */ |
michael@0 | 121 | _write: function (stateString, telemetry = {}) { |
michael@0 | 122 | let bytes = Encoder.encode(stateString); |
michael@0 | 123 | let startMs = Date.now(); |
michael@0 | 124 | let result = File.writeAtomic(this.path, bytes, {tmpPath: this.path + ".tmp"}); |
michael@0 | 125 | telemetry.FX_SESSION_RESTORE_WRITE_FILE_MS = Date.now() - startMs; |
michael@0 | 126 | telemetry.FX_SESSION_RESTORE_FILE_SIZE_BYTES = bytes.byteLength; |
michael@0 | 127 | return {result: result, telemetry: telemetry}; |
michael@0 | 128 | }, |
michael@0 | 129 | |
michael@0 | 130 | /** |
michael@0 | 131 | * Creates a copy of sessionstore.js. |
michael@0 | 132 | */ |
michael@0 | 133 | createBackupCopy: function (ext) { |
michael@0 | 134 | try { |
michael@0 | 135 | return {result: File.copy(this.path, this.backupPath + ext)}; |
michael@0 | 136 | } catch (ex if isNoSuchFileEx(ex)) { |
michael@0 | 137 | // Ignore exceptions about non-existent files. |
michael@0 | 138 | return {result: true}; |
michael@0 | 139 | } |
michael@0 | 140 | }, |
michael@0 | 141 | |
michael@0 | 142 | /** |
michael@0 | 143 | * Removes a backup copy. |
michael@0 | 144 | */ |
michael@0 | 145 | removeBackupCopy: function (ext) { |
michael@0 | 146 | try { |
michael@0 | 147 | return {result: File.remove(this.backupPath + ext)}; |
michael@0 | 148 | } catch (ex if isNoSuchFileEx(ex)) { |
michael@0 | 149 | // Ignore exceptions about non-existent files. |
michael@0 | 150 | return {result: true}; |
michael@0 | 151 | } |
michael@0 | 152 | }, |
michael@0 | 153 | |
michael@0 | 154 | /** |
michael@0 | 155 | * Wipes all files holding session data from disk. |
michael@0 | 156 | */ |
michael@0 | 157 | wipe: function () { |
michael@0 | 158 | let exn; |
michael@0 | 159 | |
michael@0 | 160 | // Erase session state file |
michael@0 | 161 | try { |
michael@0 | 162 | File.remove(this.path); |
michael@0 | 163 | } catch (ex if isNoSuchFileEx(ex)) { |
michael@0 | 164 | // Ignore exceptions about non-existent files. |
michael@0 | 165 | } catch (ex) { |
michael@0 | 166 | // Don't stop immediately. |
michael@0 | 167 | exn = ex; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | // Erase any backup, any file named "sessionstore.bak[-buildID]". |
michael@0 | 171 | let iter = new File.DirectoryIterator(OS.Constants.Path.profileDir); |
michael@0 | 172 | for (let entry in iter) { |
michael@0 | 173 | if (!entry.isDir && entry.path.startsWith(this.backupPath)) { |
michael@0 | 174 | try { |
michael@0 | 175 | File.remove(entry.path); |
michael@0 | 176 | } catch (ex) { |
michael@0 | 177 | // Don't stop immediately. |
michael@0 | 178 | exn = exn || ex; |
michael@0 | 179 | } |
michael@0 | 180 | } |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | if (exn) { |
michael@0 | 184 | throw exn; |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | return {result: true}; |
michael@0 | 188 | } |
michael@0 | 189 | }; |
michael@0 | 190 | |
michael@0 | 191 | function isNoSuchFileEx(aReason) { |
michael@0 | 192 | return aReason instanceof OS.File.Error && aReason.becauseNoSuchFile; |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | /** |
michael@0 | 196 | * Estimate the number of bytes that a data structure will use on disk |
michael@0 | 197 | * once serialized. |
michael@0 | 198 | */ |
michael@0 | 199 | function getByteLength(str) { |
michael@0 | 200 | return Encoder.encode(JSON.stringify(str)).byteLength; |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | /** |
michael@0 | 204 | * Tools for gathering statistics on a state string. |
michael@0 | 205 | */ |
michael@0 | 206 | let Statistics = { |
michael@0 | 207 | collect: function(stateString) { |
michael@0 | 208 | let start = Date.now(); |
michael@0 | 209 | let TOTAL_PREFIX = "FX_SESSION_RESTORE_TOTAL_"; |
michael@0 | 210 | let INDIVIDUAL_PREFIX = "FX_SESSION_RESTORE_INDIVIDUAL_"; |
michael@0 | 211 | let SIZE_SUFFIX = "_SIZE_BYTES"; |
michael@0 | 212 | |
michael@0 | 213 | let state = JSON.parse(stateString); |
michael@0 | 214 | |
michael@0 | 215 | // Gather all data |
michael@0 | 216 | let subsets = {}; |
michael@0 | 217 | this.gatherSimpleData(state, subsets); |
michael@0 | 218 | this.gatherComplexData(state, subsets); |
michael@0 | 219 | |
michael@0 | 220 | // Extract telemetry |
michael@0 | 221 | let telemetry = {}; |
michael@0 | 222 | for (let k of Object.keys(subsets)) { |
michael@0 | 223 | let obj = subsets[k]; |
michael@0 | 224 | telemetry[TOTAL_PREFIX + k + SIZE_SUFFIX] = getByteLength(obj); |
michael@0 | 225 | |
michael@0 | 226 | if (Array.isArray(obj)) { |
michael@0 | 227 | let size = obj.map(getByteLength); |
michael@0 | 228 | telemetry[INDIVIDUAL_PREFIX + k + SIZE_SUFFIX] = size; |
michael@0 | 229 | } |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | let stop = Date.now(); |
michael@0 | 233 | telemetry["FX_SESSION_RESTORE_EXTRACTING_STATISTICS_DURATION_MS"] = stop - start; |
michael@0 | 234 | return { |
michael@0 | 235 | telemetry: telemetry |
michael@0 | 236 | }; |
michael@0 | 237 | }, |
michael@0 | 238 | |
michael@0 | 239 | /** |
michael@0 | 240 | * Collect data that doesn't require a recursive walk through the |
michael@0 | 241 | * data structure. |
michael@0 | 242 | */ |
michael@0 | 243 | gatherSimpleData: function(state, subsets) { |
michael@0 | 244 | // The subset of sessionstore.js dealing with open windows |
michael@0 | 245 | subsets.OPEN_WINDOWS = state.windows; |
michael@0 | 246 | |
michael@0 | 247 | // The subset of sessionstore.js dealing with closed windows |
michael@0 | 248 | subsets.CLOSED_WINDOWS = state._closedWindows; |
michael@0 | 249 | |
michael@0 | 250 | // The subset of sessionstore.js dealing with closed tabs |
michael@0 | 251 | // in open windows |
michael@0 | 252 | subsets.CLOSED_TABS_IN_OPEN_WINDOWS = []; |
michael@0 | 253 | |
michael@0 | 254 | // The subset of sessionstore.js dealing with cookies |
michael@0 | 255 | // in both open and closed windows |
michael@0 | 256 | subsets.COOKIES = []; |
michael@0 | 257 | |
michael@0 | 258 | for (let winData of state.windows) { |
michael@0 | 259 | let closedTabs = winData._closedTabs || []; |
michael@0 | 260 | subsets.CLOSED_TABS_IN_OPEN_WINDOWS.push(...closedTabs); |
michael@0 | 261 | |
michael@0 | 262 | let cookies = winData.cookies || []; |
michael@0 | 263 | subsets.COOKIES.push(...cookies); |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | for (let winData of state._closedWindows) { |
michael@0 | 267 | let cookies = winData.cookies || []; |
michael@0 | 268 | subsets.COOKIES.push(...cookies); |
michael@0 | 269 | } |
michael@0 | 270 | }, |
michael@0 | 271 | |
michael@0 | 272 | /** |
michael@0 | 273 | * Walk through a data structure, recursively. |
michael@0 | 274 | * |
michael@0 | 275 | * @param {object} root The object from which to start walking. |
michael@0 | 276 | * @param {function(key, value)} cb Callback, called for each |
michael@0 | 277 | * item except the root. Returns |true| to walk the subtree rooted |
michael@0 | 278 | * at |value|, |false| otherwise */ |
michael@0 | 279 | walk: function(root, cb) { |
michael@0 | 280 | if (!root || typeof root !== "object") { |
michael@0 | 281 | return; |
michael@0 | 282 | } |
michael@0 | 283 | for (let k of Object.keys(root)) { |
michael@0 | 284 | let obj = root[k]; |
michael@0 | 285 | let stepIn = cb(k, obj); |
michael@0 | 286 | if (stepIn) { |
michael@0 | 287 | this.walk(obj, cb); |
michael@0 | 288 | } |
michael@0 | 289 | } |
michael@0 | 290 | }, |
michael@0 | 291 | |
michael@0 | 292 | /** |
michael@0 | 293 | * Collect data that requires walking through the data structure |
michael@0 | 294 | */ |
michael@0 | 295 | gatherComplexData: function(state, subsets) { |
michael@0 | 296 | // The subset of sessionstore.js dealing with DOM storage |
michael@0 | 297 | subsets.DOM_STORAGE = []; |
michael@0 | 298 | // The subset of sessionstore.js storing form data |
michael@0 | 299 | subsets.FORMDATA = []; |
michael@0 | 300 | // The subset of sessionstore.js storing history |
michael@0 | 301 | subsets.HISTORY = []; |
michael@0 | 302 | |
michael@0 | 303 | |
michael@0 | 304 | this.walk(state, function(k, value) { |
michael@0 | 305 | let dest; |
michael@0 | 306 | switch (k) { |
michael@0 | 307 | case "entries": |
michael@0 | 308 | subsets.HISTORY.push(value); |
michael@0 | 309 | return true; |
michael@0 | 310 | case "storage": |
michael@0 | 311 | subsets.DOM_STORAGE.push(value); |
michael@0 | 312 | // Never visit storage, it's full of weird stuff |
michael@0 | 313 | return false; |
michael@0 | 314 | case "formdata": |
michael@0 | 315 | subsets.FORMDATA.push(value); |
michael@0 | 316 | // Never visit formdata, it's full of weird stuff |
michael@0 | 317 | return false; |
michael@0 | 318 | case "cookies": // Don't visit these places, they are full of weird stuff |
michael@0 | 319 | case "extData": |
michael@0 | 320 | return false; |
michael@0 | 321 | default: |
michael@0 | 322 | return true; |
michael@0 | 323 | } |
michael@0 | 324 | }); |
michael@0 | 325 | |
michael@0 | 326 | return subsets; |
michael@0 | 327 | }, |
michael@0 | 328 | |
michael@0 | 329 | }; |