Files
palemoon27/browser/components/sessionstore/SessionMigration.jsm
T
roytam1 3c4a44c16e import changes from `devel' branch of rmottola/Arctic-Fox:
- Bug 1132874 - Improve protections against sending a parent plugin protocol shutdown message to the child after the child has torn down. r=aklotz (b80b45fa7)
- Bug 1103036 - Follow-up to initial patch; r=jchen (51337c2dc)
- Bug 1132874 - Simplify PPluginWidget protocol handling, and avoid sending async messages from the parent. Addresses a problem with sub protocols that are torn down randomly from either side of the connection. r=aklotz (3ad936e84)
- Bug 1128214 - Avoid a crash when attempting to render windows titlebar specific theme elements with e10s. r=roc (b6f17da09)
- Bug 1139368 - Set FilterTypeSet dependency in improveThisTypesForCall. r=h4writer (422de7271)
- Bug 864041 - Remove Firefox 2+3 compat code from about:sessionrestore. r=mak (4cfc6fe9a)
- Bug 1009599 - Restoring from about:sessionrestore fails when there is more than one tab in the window r=smacleod (88ca1cfbc)
- Bug 1146052 - Fix empty about:sessionrestore after crash as well as empty about:welcomeback after resetting the profile r=smacleod (211b50396)
- Bug 1043797: extended popup notifications to create a generic doorhanger for all security notifications incl. mixed content r=adw (f7c2d5ded)
- Bug 900845 - We aren't using the NetUtil module in SessionStore.jsm. (3f5ddd133)
- Bug 898755 - Remove _resume_session_once_on_shutdown code from SessionStore; r=yoric (eb159fec9)
- Bug 902727 - [Session Restore] Remove legacy _writeFileEncoder; r=smacleod (8e375c529)
- space cleanup (cbd71ce91)
- Bug 968923 - part 1 - add infrastructure for defining use counters from UseCounters.conf; original-author=heycam; r=heycam,gfritzsche,mshal (d0dea9997)
- Bug 968923 - part 2 - change MappedAttrParser to store a nsSVGElement directly, instead of its nsIPrincipal; r=smaug (4eff86d7f)
- Merge branch 'devel' of https://github.com/rmottola/Arctic-Fox into devel (feb4378e6)
2020-01-17 09:03:54 +08:00

115 lines
3.8 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
this.EXPORTED_SYMBOLS = ["SessionMigration"];
"use strict";
const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);
Cu.import("resource://gre/modules/Task.jsm", this);
Cu.import("resource://gre/modules/osfile.jsm", this);
// An encoder to UTF-8.
XPCOMUtils.defineLazyGetter(this, "gEncoder", function () {
return new TextEncoder();
});
// A decoder.
XPCOMUtils.defineLazyGetter(this, "gDecoder", function () {
return new TextDecoder();
});
let SessionMigrationInternal = {
/**
* Convert the original session restore state into a minimal state. It will
* only contain:
* - open windows
* - with tabs
* - with history entries with only title, url
* - with pinned state
* - with tab group info (hidden + group id)
* - with selected tab info
* - with selected window info
* - with tabgroups info
*
* The complete state is then wrapped into the "about:welcomeback" page as
* form field info to be restored when restoring the state.
*/
convertState: function(aStateObj) {
let state = {
selectedWindow: aStateObj.selectedWindow,
_closedWindows: []
};
state.windows = aStateObj.windows.map(function(oldWin) {
var win = {extData: {}};
win.tabs = oldWin.tabs.map(function(oldTab) {
var tab = {};
// Keep only titles and urls for history entries
tab.entries = oldTab.entries.map(function(entry) {
return {url: entry.url, title: entry.title};
});
tab.index = oldTab.index;
tab.hidden = oldTab.hidden;
tab.pinned = oldTab.pinned;
// The tabgroup info is in the extData, so we need to get it out.
if (oldTab.extData && "tabview-tab" in oldTab.extData) {
tab.extData = {"tabview-tab": oldTab.extData["tabview-tab"]};
}
return tab;
});
// There are various tabgroup-related attributes that we need to get out
// of the session restore data for the window, too.
if (oldWin.extData) {
for (let k of Object.keys(oldWin.extData)) {
if (k.startsWith("tabview-")) {
win.extData[k] = oldWin.extData[k];
}
}
}
win.selected = oldWin.selected;
win._closedTabs = [];
return win;
});
let url = "about:welcomeback";
let formdata = {id: {sessionData: state}, url};
return {windows: [{tabs: [{entries: [{url}], formdata}]}]};
},
/**
* Asynchronously read session restore state (JSON) from a path
*/
readState: function(aPath) {
return Task.spawn(function() {
let bytes = yield OS.File.read(aPath);
let text = gDecoder.decode(bytes);
let state = JSON.parse(text);
throw new Task.Result(state);
});
},
/**
* Asynchronously write session restore state as JSON to a path
*/
writeState: function(aPath, aState) {
let bytes = gEncoder.encode(JSON.stringify(aState));
return OS.File.writeAtomic(aPath, bytes, {tmpPath: aPath + ".tmp"});
}
}
let SessionMigration = {
/**
* Migrate a limited set of session data from one path to another.
*/
migrate: function(aFromPath, aToPath) {
return Task.spawn(function() {
let inState = yield SessionMigrationInternal.readState(aFromPath);
let outState = SessionMigrationInternal.convertState(inState);
// Unfortunately, we can't use SessionStore's own SessionFile to
// write out the data because it has a dependency on the profile dir
// being known. When the migration runs, there is no guarantee that
// that's true.
yield SessionMigrationInternal.writeState(aToPath, outState);
});
}
};