michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * A worker dedicated to handle I/O for Session Store. michael@0: */ michael@0: michael@0: "use strict"; michael@0: michael@0: importScripts("resource://gre/modules/osfile.jsm"); michael@0: michael@0: let File = OS.File; michael@0: let Encoder = new TextEncoder(); michael@0: let Decoder = new TextDecoder(); michael@0: michael@0: /** michael@0: * Communications with the controller. michael@0: * michael@0: * Accepts messages: michael@0: * {fun:function_name, args:array_of_arguments_or_null, id: custom_id} michael@0: * michael@0: * Sends messages: michael@0: * {ok: result, id: custom_id, telemetry: {}} / michael@0: * {fail: serialized_form_of_OS.File.Error, id: custom_id} michael@0: */ michael@0: self.onmessage = function (msg) { michael@0: let data = msg.data; michael@0: if (!(data.fun in Agent)) { michael@0: throw new Error("Cannot find method " + data.fun); michael@0: } michael@0: michael@0: let result; michael@0: let id = data.id; michael@0: michael@0: try { michael@0: result = Agent[data.fun].apply(Agent, data.args) || {}; michael@0: } catch (ex if ex instanceof OS.File.Error) { michael@0: // Instances of OS.File.Error know how to serialize themselves michael@0: // (deserialization ensures that we end up with OS-specific michael@0: // instances of |OS.File.Error|) michael@0: self.postMessage({fail: OS.File.Error.toMsg(ex), id: id}); michael@0: return; michael@0: } michael@0: michael@0: // Other exceptions do not, and should be propagated through DOM's michael@0: // built-in mechanism for uncaught errors, although this mechanism michael@0: // may lose interesting information. michael@0: self.postMessage({ michael@0: ok: result.result, michael@0: id: id, michael@0: telemetry: result.telemetry || {} michael@0: }); michael@0: }; michael@0: michael@0: let Agent = { michael@0: // Boolean that tells whether we already made a michael@0: // call to write(). We will only attempt to move michael@0: // sessionstore.js to sessionstore.bak on the michael@0: // first write. michael@0: hasWrittenState: false, michael@0: michael@0: // The path to sessionstore.js michael@0: path: OS.Path.join(OS.Constants.Path.profileDir, "sessionstore.js"), michael@0: michael@0: // The path to sessionstore.bak michael@0: backupPath: OS.Path.join(OS.Constants.Path.profileDir, "sessionstore.bak"), michael@0: michael@0: /** michael@0: * NO-OP to start the worker. michael@0: */ michael@0: init: function () { michael@0: return {result: true}; michael@0: }, michael@0: michael@0: /** michael@0: * Write the session to disk. michael@0: */ michael@0: write: function (stateString) { michael@0: let exn; michael@0: let telemetry = {}; michael@0: michael@0: if (!this.hasWrittenState) { michael@0: try { michael@0: let startMs = Date.now(); michael@0: File.move(this.path, this.backupPath); michael@0: telemetry.FX_SESSION_RESTORE_BACKUP_FILE_MS = Date.now() - startMs; michael@0: } catch (ex if isNoSuchFileEx(ex)) { michael@0: // Ignore exceptions about non-existent files. michael@0: } catch (ex) { michael@0: // Throw the exception after we wrote the state to disk michael@0: // so that the backup can't interfere with the actual write. michael@0: exn = ex; michael@0: } michael@0: michael@0: this.hasWrittenState = true; michael@0: } michael@0: michael@0: let ret = this._write(stateString, telemetry); michael@0: michael@0: if (exn) { michael@0: throw exn; michael@0: } michael@0: michael@0: return ret; michael@0: }, michael@0: michael@0: /** michael@0: * Extract all sorts of useful statistics from a state string, michael@0: * for use with Telemetry. michael@0: * michael@0: * @return {object} michael@0: */ michael@0: gatherTelemetry: function (stateString) { michael@0: return Statistics.collect(stateString); michael@0: }, michael@0: michael@0: /** michael@0: * Write a stateString to disk michael@0: */ michael@0: _write: function (stateString, telemetry = {}) { michael@0: let bytes = Encoder.encode(stateString); michael@0: let startMs = Date.now(); michael@0: let result = File.writeAtomic(this.path, bytes, {tmpPath: this.path + ".tmp"}); michael@0: telemetry.FX_SESSION_RESTORE_WRITE_FILE_MS = Date.now() - startMs; michael@0: telemetry.FX_SESSION_RESTORE_FILE_SIZE_BYTES = bytes.byteLength; michael@0: return {result: result, telemetry: telemetry}; michael@0: }, michael@0: michael@0: /** michael@0: * Creates a copy of sessionstore.js. michael@0: */ michael@0: createBackupCopy: function (ext) { michael@0: try { michael@0: return {result: File.copy(this.path, this.backupPath + ext)}; michael@0: } catch (ex if isNoSuchFileEx(ex)) { michael@0: // Ignore exceptions about non-existent files. michael@0: return {result: true}; michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Removes a backup copy. michael@0: */ michael@0: removeBackupCopy: function (ext) { michael@0: try { michael@0: return {result: File.remove(this.backupPath + ext)}; michael@0: } catch (ex if isNoSuchFileEx(ex)) { michael@0: // Ignore exceptions about non-existent files. michael@0: return {result: true}; michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Wipes all files holding session data from disk. michael@0: */ michael@0: wipe: function () { michael@0: let exn; michael@0: michael@0: // Erase session state file michael@0: try { michael@0: File.remove(this.path); michael@0: } catch (ex if isNoSuchFileEx(ex)) { michael@0: // Ignore exceptions about non-existent files. michael@0: } catch (ex) { michael@0: // Don't stop immediately. michael@0: exn = ex; michael@0: } michael@0: michael@0: // Erase any backup, any file named "sessionstore.bak[-buildID]". michael@0: let iter = new File.DirectoryIterator(OS.Constants.Path.profileDir); michael@0: for (let entry in iter) { michael@0: if (!entry.isDir && entry.path.startsWith(this.backupPath)) { michael@0: try { michael@0: File.remove(entry.path); michael@0: } catch (ex) { michael@0: // Don't stop immediately. michael@0: exn = exn || ex; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (exn) { michael@0: throw exn; michael@0: } michael@0: michael@0: return {result: true}; michael@0: } michael@0: }; michael@0: michael@0: function isNoSuchFileEx(aReason) { michael@0: return aReason instanceof OS.File.Error && aReason.becauseNoSuchFile; michael@0: } michael@0: michael@0: /** michael@0: * Estimate the number of bytes that a data structure will use on disk michael@0: * once serialized. michael@0: */ michael@0: function getByteLength(str) { michael@0: return Encoder.encode(JSON.stringify(str)).byteLength; michael@0: } michael@0: michael@0: /** michael@0: * Tools for gathering statistics on a state string. michael@0: */ michael@0: let Statistics = { michael@0: collect: function(stateString) { michael@0: let start = Date.now(); michael@0: let TOTAL_PREFIX = "FX_SESSION_RESTORE_TOTAL_"; michael@0: let INDIVIDUAL_PREFIX = "FX_SESSION_RESTORE_INDIVIDUAL_"; michael@0: let SIZE_SUFFIX = "_SIZE_BYTES"; michael@0: michael@0: let state = JSON.parse(stateString); michael@0: michael@0: // Gather all data michael@0: let subsets = {}; michael@0: this.gatherSimpleData(state, subsets); michael@0: this.gatherComplexData(state, subsets); michael@0: michael@0: // Extract telemetry michael@0: let telemetry = {}; michael@0: for (let k of Object.keys(subsets)) { michael@0: let obj = subsets[k]; michael@0: telemetry[TOTAL_PREFIX + k + SIZE_SUFFIX] = getByteLength(obj); michael@0: michael@0: if (Array.isArray(obj)) { michael@0: let size = obj.map(getByteLength); michael@0: telemetry[INDIVIDUAL_PREFIX + k + SIZE_SUFFIX] = size; michael@0: } michael@0: } michael@0: michael@0: let stop = Date.now(); michael@0: telemetry["FX_SESSION_RESTORE_EXTRACTING_STATISTICS_DURATION_MS"] = stop - start; michael@0: return { michael@0: telemetry: telemetry michael@0: }; michael@0: }, michael@0: michael@0: /** michael@0: * Collect data that doesn't require a recursive walk through the michael@0: * data structure. michael@0: */ michael@0: gatherSimpleData: function(state, subsets) { michael@0: // The subset of sessionstore.js dealing with open windows michael@0: subsets.OPEN_WINDOWS = state.windows; michael@0: michael@0: // The subset of sessionstore.js dealing with closed windows michael@0: subsets.CLOSED_WINDOWS = state._closedWindows; michael@0: michael@0: // The subset of sessionstore.js dealing with closed tabs michael@0: // in open windows michael@0: subsets.CLOSED_TABS_IN_OPEN_WINDOWS = []; michael@0: michael@0: // The subset of sessionstore.js dealing with cookies michael@0: // in both open and closed windows michael@0: subsets.COOKIES = []; michael@0: michael@0: for (let winData of state.windows) { michael@0: let closedTabs = winData._closedTabs || []; michael@0: subsets.CLOSED_TABS_IN_OPEN_WINDOWS.push(...closedTabs); michael@0: michael@0: let cookies = winData.cookies || []; michael@0: subsets.COOKIES.push(...cookies); michael@0: } michael@0: michael@0: for (let winData of state._closedWindows) { michael@0: let cookies = winData.cookies || []; michael@0: subsets.COOKIES.push(...cookies); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Walk through a data structure, recursively. michael@0: * michael@0: * @param {object} root The object from which to start walking. michael@0: * @param {function(key, value)} cb Callback, called for each michael@0: * item except the root. Returns |true| to walk the subtree rooted michael@0: * at |value|, |false| otherwise */ michael@0: walk: function(root, cb) { michael@0: if (!root || typeof root !== "object") { michael@0: return; michael@0: } michael@0: for (let k of Object.keys(root)) { michael@0: let obj = root[k]; michael@0: let stepIn = cb(k, obj); michael@0: if (stepIn) { michael@0: this.walk(obj, cb); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Collect data that requires walking through the data structure michael@0: */ michael@0: gatherComplexData: function(state, subsets) { michael@0: // The subset of sessionstore.js dealing with DOM storage michael@0: subsets.DOM_STORAGE = []; michael@0: // The subset of sessionstore.js storing form data michael@0: subsets.FORMDATA = []; michael@0: // The subset of sessionstore.js storing history michael@0: subsets.HISTORY = []; michael@0: michael@0: michael@0: this.walk(state, function(k, value) { michael@0: let dest; michael@0: switch (k) { michael@0: case "entries": michael@0: subsets.HISTORY.push(value); michael@0: return true; michael@0: case "storage": michael@0: subsets.DOM_STORAGE.push(value); michael@0: // Never visit storage, it's full of weird stuff michael@0: return false; michael@0: case "formdata": michael@0: subsets.FORMDATA.push(value); michael@0: // Never visit formdata, it's full of weird stuff michael@0: return false; michael@0: case "cookies": // Don't visit these places, they are full of weird stuff michael@0: case "extData": michael@0: return false; michael@0: default: michael@0: return true; michael@0: } michael@0: }); michael@0: michael@0: return subsets; michael@0: }, michael@0: michael@0: };