summaryrefslogtreecommitdiffstats
path: root/application/basilisk/components/sessionstore/SessionMigration.jsm
diff options
context:
space:
mode:
authorwolfbeast <mcwerewolf@gmail.com>2018-06-04 13:17:38 +0200
committerwolfbeast <mcwerewolf@gmail.com>2018-06-04 13:17:38 +0200
commita1be17c1cea81ebb1e8b131a662c698d78f3f7f2 (patch)
treea92f7de513be600cc07bac458183e9af40e00c06 /application/basilisk/components/sessionstore/SessionMigration.jsm
parentbf11fdd304898ac675e39b01b280d39550e419d0 (diff)
downloadUXP-a1be17c1cea81ebb1e8b131a662c698d78f3f7f2.tar
UXP-a1be17c1cea81ebb1e8b131a662c698d78f3f7f2.tar.gz
UXP-a1be17c1cea81ebb1e8b131a662c698d78f3f7f2.tar.lz
UXP-a1be17c1cea81ebb1e8b131a662c698d78f3f7f2.tar.xz
UXP-a1be17c1cea81ebb1e8b131a662c698d78f3f7f2.zip
Issue #303 Part 1: Move basilisk files from /browser to /application/basilisk
Diffstat (limited to 'application/basilisk/components/sessionstore/SessionMigration.jsm')
-rw-r--r--application/basilisk/components/sessionstore/SessionMigration.jsm106
1 files changed, 106 insertions, 0 deletions
diff --git a/application/basilisk/components/sessionstore/SessionMigration.jsm b/application/basilisk/components/sessionstore/SessionMigration.jsm
new file mode 100644
index 000000000..1aa22f1a9
--- /dev/null
+++ b/application/basilisk/components/sessionstore/SessionMigration.jsm
@@ -0,0 +1,106 @@
+/* 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/. */
+
+"use strict";
+
+this.EXPORTED_SYMBOLS = ["SessionMigration"];
+
+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);
+
+XPCOMUtils.defineLazyModuleGetter(this, "Utils",
+ "resource://gre/modules/sessionstore/Utils.jsm");
+
+// An encoder to UTF-8.
+XPCOMUtils.defineLazyGetter(this, "gEncoder", function () {
+ return new TextEncoder();
+});
+
+// A decoder.
+XPCOMUtils.defineLazyGetter(this, "gDecoder", function () {
+ return new TextDecoder();
+});
+
+var 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, triggeringPrincipal
+ * - with pinned state
+ * - with tab group info (hidden + group id)
+ * - with selected tab info
+ * - with selected window 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, urls and triggeringPrincipals for history entries
+ tab.entries = oldTab.entries.map(function(entry) {
+ return { url: entry.url,
+ triggeringPrincipal_base64: entry.triggeringPrincipal_base64,
+ title: entry.title };
+ });
+ tab.index = oldTab.index;
+ tab.hidden = oldTab.hidden;
+ tab.pinned = oldTab.pinned;
+ return tab;
+ });
+ win.selected = oldWin.selected;
+ win._closedTabs = [];
+ return win;
+ });
+ let url = "about:welcomeback";
+ let formdata = {id: {sessionData: state}, url};
+ let entry = { url, triggeringPrincipal_base64: Utils.SERIALIZED_SYSTEMPRINCIPAL };
+ return { windows: [{ tabs: [{ entries: [ entry ], 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"});
+ }
+}
+
+var 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);
+ });
+ }
+};