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 michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: const Cr = Components.results; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: #ifdef MOZ_CRASHREPORTER michael@0: XPCOMUtils.defineLazyServiceGetter(this, "CrashReporter", michael@0: "@mozilla.org/xre/app-info;1", "nsICrashReporter"); michael@0: #endif michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "Task", "resource://gre/modules/Task.jsm"); michael@0: XPCOMUtils.defineLazyModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm"); michael@0: XPCOMUtils.defineLazyModuleGetter(this, "sendMessageToJava", "resource://gre/modules/Messaging.jsm"); michael@0: michael@0: function dump(a) { michael@0: Services.console.logStringMessage(a); michael@0: } michael@0: michael@0: // ----------------------------------------------------------------------- michael@0: // Session Store michael@0: // ----------------------------------------------------------------------- michael@0: michael@0: const STATE_STOPPED = 0; michael@0: const STATE_RUNNING = 1; michael@0: michael@0: function SessionStore() { } michael@0: michael@0: SessionStore.prototype = { michael@0: classID: Components.ID("{8c1f07d6-cba3-4226-a315-8bd43d67d032}"), michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsISessionStore, michael@0: Ci.nsIDOMEventListener, michael@0: Ci.nsIObserver, michael@0: Ci.nsISupportsWeakReference]), michael@0: michael@0: _windows: {}, michael@0: _lastSaveTime: 0, michael@0: _interval: 10000, michael@0: _maxTabsUndo: 1, michael@0: _pendingWrite: 0, michael@0: michael@0: init: function ss_init() { michael@0: // Get file references michael@0: this._sessionFile = Services.dirsvc.get("ProfD", Ci.nsILocalFile); michael@0: this._sessionFileBackup = this._sessionFile.clone(); michael@0: this._sessionFile.append("sessionstore.js"); michael@0: this._sessionFileBackup.append("sessionstore.bak"); michael@0: michael@0: this._loadState = STATE_STOPPED; michael@0: michael@0: this._interval = Services.prefs.getIntPref("browser.sessionstore.interval"); michael@0: this._maxTabsUndo = Services.prefs.getIntPref("browser.sessionstore.max_tabs_undo"); michael@0: }, michael@0: michael@0: _clearDisk: function ss_clearDisk() { michael@0: OS.File.remove(this._sessionFile.path); michael@0: OS.File.remove(this._sessionFileBackup.path); michael@0: }, michael@0: michael@0: observe: function ss_observe(aSubject, aTopic, aData) { michael@0: let self = this; michael@0: let observerService = Services.obs; michael@0: switch (aTopic) { michael@0: case "app-startup": michael@0: observerService.addObserver(this, "final-ui-startup", true); michael@0: observerService.addObserver(this, "domwindowopened", true); michael@0: observerService.addObserver(this, "domwindowclosed", true); michael@0: observerService.addObserver(this, "browser:purge-session-history", true); michael@0: observerService.addObserver(this, "Session:Restore", true); michael@0: observerService.addObserver(this, "application-background", true); michael@0: break; michael@0: case "final-ui-startup": michael@0: observerService.removeObserver(this, "final-ui-startup"); michael@0: this.init(); michael@0: break; michael@0: case "domwindowopened": { michael@0: let window = aSubject; michael@0: window.addEventListener("load", function() { michael@0: self.onWindowOpen(window); michael@0: window.removeEventListener("load", arguments.callee, false); michael@0: }, false); michael@0: break; michael@0: } michael@0: case "domwindowclosed": // catch closed windows michael@0: this.onWindowClose(aSubject); michael@0: break; michael@0: case "browser:purge-session-history": // catch sanitization michael@0: this._clearDisk(); michael@0: michael@0: // Clear all data about closed tabs michael@0: for (let [ssid, win] in Iterator(this._windows)) michael@0: win.closedTabs = []; michael@0: michael@0: if (this._loadState == STATE_RUNNING) { michael@0: // Save the purged state immediately michael@0: this.saveState(); michael@0: } michael@0: michael@0: Services.obs.notifyObservers(null, "sessionstore-state-purge-complete", ""); michael@0: break; michael@0: case "timer-callback": michael@0: // Timer call back for delayed saving michael@0: this._saveTimer = null; michael@0: if (this._pendingWrite) { michael@0: this.saveState(); michael@0: } michael@0: break; michael@0: case "Session:Restore": { michael@0: Services.obs.removeObserver(this, "Session:Restore"); michael@0: if (aData) { michael@0: // Be ready to handle any restore failures by making sure we have a valid tab opened michael@0: let window = Services.wm.getMostRecentWindow("navigator:browser"); michael@0: let restoreCleanup = { michael@0: observe: function (aSubject, aTopic, aData) { michael@0: Services.obs.removeObserver(restoreCleanup, "sessionstore-windows-restored"); michael@0: michael@0: if (window.BrowserApp.tabs.length == 0) { michael@0: window.BrowserApp.addTab("about:home", { michael@0: selected: true michael@0: }); michael@0: } michael@0: michael@0: // Let Java know we're done restoring tabs so tabs added after this can be animated michael@0: sendMessageToJava({ michael@0: type: "Session:RestoreEnd" michael@0: }); michael@0: }.bind(this) michael@0: }; michael@0: Services.obs.addObserver(restoreCleanup, "sessionstore-windows-restored", false); michael@0: michael@0: // Do a restore, triggered by Java michael@0: let data = JSON.parse(aData); michael@0: this.restoreLastSession(data.sessionString); michael@0: } else { michael@0: // Not doing a restore; just send restore message michael@0: Services.obs.notifyObservers(null, "sessionstore-windows-restored", ""); michael@0: } michael@0: break; michael@0: } michael@0: case "application-background": michael@0: // We receive this notification when Android's onPause callback is michael@0: // executed. After onPause, the application may be terminated at any michael@0: // point without notice; therefore, we must synchronously write out any michael@0: // pending save state to ensure that this data does not get lost. michael@0: this.flushPendingState(); michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: handleEvent: function ss_handleEvent(aEvent) { michael@0: let window = aEvent.currentTarget.ownerDocument.defaultView; michael@0: switch (aEvent.type) { michael@0: case "TabOpen": { michael@0: let browser = aEvent.target; michael@0: this.onTabAdd(window, browser); michael@0: break; michael@0: } michael@0: case "TabClose": { michael@0: let browser = aEvent.target; michael@0: this.onTabClose(window, browser); michael@0: this.onTabRemove(window, browser); michael@0: break; michael@0: } michael@0: case "TabSelect": { michael@0: let browser = aEvent.target; michael@0: this.onTabSelect(window, browser); michael@0: break; michael@0: } michael@0: case "DOMTitleChanged": { michael@0: let browser = aEvent.currentTarget; michael@0: michael@0: // Handle only top-level DOMTitleChanged event michael@0: if (browser.contentDocument !== aEvent.originalTarget) michael@0: return; michael@0: michael@0: // Use DOMTitleChanged to detect page loads over alternatives. michael@0: // onLocationChange happens too early, so we don't have the page title michael@0: // yet; pageshow happens too late, so we could lose session data if the michael@0: // browser were killed. michael@0: this.onTabLoad(window, browser); michael@0: break; michael@0: } michael@0: } michael@0: }, michael@0: michael@0: onWindowOpen: function ss_onWindowOpen(aWindow) { michael@0: // Return if window has already been initialized michael@0: if (aWindow && aWindow.__SSID && this._windows[aWindow.__SSID]) michael@0: return; michael@0: michael@0: // Ignore non-browser windows and windows opened while shutting down michael@0: if (aWindow.document.documentElement.getAttribute("windowtype") != "navigator:browser") michael@0: return; michael@0: michael@0: // Assign it a unique identifier (timestamp) and create its data object michael@0: aWindow.__SSID = "window" + Date.now(); michael@0: this._windows[aWindow.__SSID] = { tabs: [], selected: 0, closedTabs: [] }; michael@0: michael@0: // Perform additional initialization when the first window is loading michael@0: if (this._loadState == STATE_STOPPED) { michael@0: this._loadState = STATE_RUNNING; michael@0: this._lastSaveTime = Date.now(); michael@0: } michael@0: michael@0: // Add tab change listeners to all already existing tabs michael@0: let tabs = aWindow.BrowserApp.tabs; michael@0: for (let i = 0; i < tabs.length; i++) michael@0: this.onTabAdd(aWindow, tabs[i].browser, true); michael@0: michael@0: // Notification of tab add/remove/selection michael@0: let browsers = aWindow.document.getElementById("browsers"); michael@0: browsers.addEventListener("TabOpen", this, true); michael@0: browsers.addEventListener("TabClose", this, true); michael@0: browsers.addEventListener("TabSelect", this, true); michael@0: }, michael@0: michael@0: onWindowClose: function ss_onWindowClose(aWindow) { michael@0: // Ignore windows not tracked by SessionStore michael@0: if (!aWindow.__SSID || !this._windows[aWindow.__SSID]) michael@0: return; michael@0: michael@0: let browsers = aWindow.document.getElementById("browsers"); michael@0: browsers.removeEventListener("TabOpen", this, true); michael@0: browsers.removeEventListener("TabClose", this, true); michael@0: browsers.removeEventListener("TabSelect", this, true); michael@0: michael@0: if (this._loadState == STATE_RUNNING) { michael@0: // Update all window data for a last time michael@0: this._collectWindowData(aWindow); michael@0: michael@0: // Clear this window from the list michael@0: delete this._windows[aWindow.__SSID]; michael@0: michael@0: // Save the state without this window to disk michael@0: this.saveStateDelayed(); michael@0: } michael@0: michael@0: let tabs = aWindow.BrowserApp.tabs; michael@0: for (let i = 0; i < tabs.length; i++) michael@0: this.onTabRemove(aWindow, tabs[i].browser, true); michael@0: michael@0: delete aWindow.__SSID; michael@0: }, michael@0: michael@0: onTabAdd: function ss_onTabAdd(aWindow, aBrowser, aNoNotification) { michael@0: aBrowser.addEventListener("DOMTitleChanged", this, true); michael@0: if (!aNoNotification) michael@0: this.saveStateDelayed(); michael@0: this._updateCrashReportURL(aWindow); michael@0: }, michael@0: michael@0: onTabRemove: function ss_onTabRemove(aWindow, aBrowser, aNoNotification) { michael@0: aBrowser.removeEventListener("DOMTitleChanged", this, true); michael@0: michael@0: // If this browser is being restored, skip any session save activity michael@0: if (aBrowser.__SS_restore) michael@0: return; michael@0: michael@0: delete aBrowser.__SS_data; michael@0: michael@0: if (!aNoNotification) michael@0: this.saveStateDelayed(); michael@0: }, michael@0: michael@0: onTabClose: function ss_onTabClose(aWindow, aBrowser) { michael@0: if (this._maxTabsUndo == 0) michael@0: return; michael@0: michael@0: if (aWindow.BrowserApp.tabs.length > 0) { michael@0: // Bundle this browser's data and extra data and save in the closedTabs michael@0: // window property michael@0: let data = aBrowser.__SS_data; michael@0: data.extData = aBrowser.__SS_extdata; michael@0: michael@0: this._windows[aWindow.__SSID].closedTabs.unshift(data); michael@0: let length = this._windows[aWindow.__SSID].closedTabs.length; michael@0: if (length > this._maxTabsUndo) michael@0: this._windows[aWindow.__SSID].closedTabs.splice(this._maxTabsUndo, length - this._maxTabsUndo); michael@0: } michael@0: }, michael@0: michael@0: onTabLoad: function ss_onTabLoad(aWindow, aBrowser) { michael@0: // If this browser is being restored, skip any session save activity michael@0: if (aBrowser.__SS_restore) michael@0: return; michael@0: michael@0: // Ignore a transient "about:blank" michael@0: if (!aBrowser.canGoBack && aBrowser.currentURI.spec == "about:blank") michael@0: return; michael@0: michael@0: let history = aBrowser.sessionHistory; michael@0: michael@0: // Serialize the tab data michael@0: let entries = []; michael@0: let index = history.index + 1; michael@0: for (let i = 0; i < history.count; i++) { michael@0: let historyEntry = history.getEntryAtIndex(i, false); michael@0: // Don't try to restore wyciwyg URLs michael@0: if (historyEntry.URI.schemeIs("wyciwyg")) { michael@0: // Adjust the index to account for skipped history entries michael@0: if (i <= history.index) michael@0: index--; michael@0: continue; michael@0: } michael@0: let entry = this._serializeHistoryEntry(historyEntry); michael@0: entries.push(entry); michael@0: } michael@0: let data = { entries: entries, index: index }; michael@0: michael@0: delete aBrowser.__SS_data; michael@0: this._collectTabData(aWindow, aBrowser, data); michael@0: this.saveStateDelayed(); michael@0: michael@0: this._updateCrashReportURL(aWindow); michael@0: }, michael@0: michael@0: onTabSelect: function ss_onTabSelect(aWindow, aBrowser) { michael@0: if (this._loadState != STATE_RUNNING) michael@0: return; michael@0: michael@0: let browsers = aWindow.document.getElementById("browsers"); michael@0: let index = browsers.selectedIndex; michael@0: this._windows[aWindow.__SSID].selected = parseInt(index) + 1; // 1-based michael@0: michael@0: // Restore the resurrected browser michael@0: if (aBrowser.__SS_restore) { michael@0: let data = aBrowser.__SS_data; michael@0: if (data.entries.length > 0) michael@0: this._restoreHistory(data, aBrowser.sessionHistory); michael@0: michael@0: delete aBrowser.__SS_restore; michael@0: aBrowser.removeAttribute("pending"); michael@0: } michael@0: michael@0: this.saveStateDelayed(); michael@0: this._updateCrashReportURL(aWindow); michael@0: }, michael@0: michael@0: saveStateDelayed: function ss_saveStateDelayed() { michael@0: if (!this._saveTimer) { michael@0: // Interval until the next disk operation is allowed michael@0: let minimalDelay = this._lastSaveTime + this._interval - Date.now(); michael@0: michael@0: // If we have to wait, set a timer, otherwise saveState directly michael@0: let delay = Math.max(minimalDelay, 2000); michael@0: if (delay > 0) { michael@0: this._pendingWrite++; michael@0: this._saveTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); michael@0: this._saveTimer.init(this, delay, Ci.nsITimer.TYPE_ONE_SHOT); michael@0: } else { michael@0: this.saveState(); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: saveState: function ss_saveState() { michael@0: this._pendingWrite++; michael@0: this._saveState(true); michael@0: }, michael@0: michael@0: // Immediately and synchronously writes any pending state to disk. michael@0: flushPendingState: function ss_flushPendingState() { michael@0: if (this._pendingWrite) { michael@0: this._saveState(false); michael@0: } michael@0: }, michael@0: michael@0: _saveState: function ss_saveState(aAsync) { michael@0: // Kill any queued timer and save immediately michael@0: if (this._saveTimer) { michael@0: this._saveTimer.cancel(); michael@0: this._saveTimer = null; michael@0: } michael@0: michael@0: let data = this._getCurrentState(); michael@0: let normalData = { windows: [] }; michael@0: let privateData = { windows: [] }; michael@0: michael@0: for (let winIndex = 0; winIndex < data.windows.length; ++winIndex) { michael@0: let win = data.windows[winIndex]; michael@0: let normalWin = {}; michael@0: for (let prop in win) { michael@0: normalWin[prop] = data[prop]; michael@0: } michael@0: normalWin.tabs = []; michael@0: normalData.windows.push(normalWin); michael@0: privateData.windows.push({ tabs: [] }); michael@0: michael@0: // Split the session data into private and non-private data objects. michael@0: // Non-private session data will be saved to disk, and private session michael@0: // data will be sent to Java for Android to hold it in memory. michael@0: for (let i = 0; i < win.tabs.length; ++i) { michael@0: let tab = win.tabs[i]; michael@0: let savedWin = tab.isPrivate ? privateData.windows[winIndex] : normalData.windows[winIndex]; michael@0: savedWin.tabs.push(tab); michael@0: if (win.selected == i + 1) { michael@0: savedWin.selected = savedWin.tabs.length; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Write only non-private data to disk michael@0: this._writeFile(this._sessionFile, JSON.stringify(normalData), aAsync); michael@0: michael@0: // If we have private data, send it to Java; otherwise, send null to michael@0: // indicate that there is no private data michael@0: sendMessageToJava({ michael@0: type: "PrivateBrowsing:Data", michael@0: session: (privateData.windows[0].tabs.length > 0) ? JSON.stringify(privateData) : null michael@0: }); michael@0: michael@0: this._lastSaveTime = Date.now(); michael@0: }, michael@0: michael@0: _getCurrentState: function ss_getCurrentState() { michael@0: let self = this; michael@0: this._forEachBrowserWindow(function(aWindow) { michael@0: self._collectWindowData(aWindow); michael@0: }); michael@0: michael@0: let data = { windows: [] }; michael@0: for (let index in this._windows) { michael@0: data.windows.push(this._windows[index]); michael@0: } michael@0: michael@0: return data; michael@0: }, michael@0: michael@0: _collectTabData: function ss__collectTabData(aWindow, aBrowser, aHistory) { michael@0: // If this browser is being restored, skip any session save activity michael@0: if (aBrowser.__SS_restore) michael@0: return; michael@0: michael@0: aHistory = aHistory || { entries: [{ url: aBrowser.currentURI.spec, title: aBrowser.contentTitle }], index: 1 }; michael@0: michael@0: let tabData = {}; michael@0: tabData.entries = aHistory.entries; michael@0: tabData.index = aHistory.index; michael@0: tabData.attributes = { image: aBrowser.mIconURL }; michael@0: tabData.desktopMode = aWindow.BrowserApp.getTabForBrowser(aBrowser).desktopMode; michael@0: tabData.isPrivate = aBrowser.docShell.QueryInterface(Ci.nsILoadContext).usePrivateBrowsing; michael@0: michael@0: aBrowser.__SS_data = tabData; michael@0: }, michael@0: michael@0: _collectWindowData: function ss__collectWindowData(aWindow) { michael@0: // Ignore windows not tracked by SessionStore michael@0: if (!aWindow.__SSID || !this._windows[aWindow.__SSID]) michael@0: return; michael@0: michael@0: let winData = this._windows[aWindow.__SSID]; michael@0: winData.tabs = []; michael@0: michael@0: let browsers = aWindow.document.getElementById("browsers"); michael@0: let index = browsers.selectedIndex; michael@0: winData.selected = parseInt(index) + 1; // 1-based michael@0: michael@0: let tabs = aWindow.BrowserApp.tabs; michael@0: for (let i = 0; i < tabs.length; i++) { michael@0: let browser = tabs[i].browser; michael@0: if (browser.__SS_data) { michael@0: let tabData = browser.__SS_data; michael@0: if (browser.__SS_extdata) michael@0: tabData.extData = browser.__SS_extdata; michael@0: winData.tabs.push(tabData); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: _forEachBrowserWindow: function ss_forEachBrowserWindow(aFunc) { michael@0: let windowsEnum = Services.wm.getEnumerator("navigator:browser"); michael@0: while (windowsEnum.hasMoreElements()) { michael@0: let window = windowsEnum.getNext(); michael@0: if (window.__SSID && !window.closed) michael@0: aFunc.call(this, window); michael@0: } michael@0: }, michael@0: michael@0: _writeFile: function ss_writeFile(aFile, aData, aAsync) { michael@0: let stateString = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString); michael@0: stateString.data = aData; michael@0: Services.obs.notifyObservers(stateString, "sessionstore-state-write", ""); michael@0: michael@0: // Don't touch the file if an observer has deleted all state data michael@0: if (!stateString.data) michael@0: return; michael@0: michael@0: if (aAsync) { michael@0: let array = new TextEncoder().encode(aData); michael@0: let pendingWrite = this._pendingWrite; michael@0: OS.File.writeAtomic(aFile.path, array, { tmpPath: aFile.path + ".tmp" }).then(function onSuccess() { michael@0: // Make sure this._pendingWrite is the same value it was before we michael@0: // fired off the async write. If the count is different, another write michael@0: // is pending, so we shouldn't reset this._pendingWrite yet. michael@0: if (pendingWrite === this._pendingWrite) michael@0: this._pendingWrite = 0; michael@0: Services.obs.notifyObservers(null, "sessionstore-state-write-complete", ""); michael@0: }.bind(this)); michael@0: } else { michael@0: this._pendingWrite = 0; michael@0: let foStream = Cc["@mozilla.org/network/file-output-stream;1"]. michael@0: createInstance(Ci.nsIFileOutputStream); michael@0: foStream.init(aFile, 0x02 | 0x08 | 0x20, 0666, 0); michael@0: let converter = Cc["@mozilla.org/intl/converter-output-stream;1"]. michael@0: createInstance(Ci.nsIConverterOutputStream); michael@0: converter.init(foStream, "UTF-8", 0, 0); michael@0: converter.writeString(aData); michael@0: converter.close(); michael@0: } michael@0: }, michael@0: michael@0: _updateCrashReportURL: function ss_updateCrashReportURL(aWindow) { michael@0: #ifdef MOZ_CRASHREPORTER michael@0: if (!aWindow.BrowserApp.selectedBrowser) michael@0: return; michael@0: michael@0: try { michael@0: let currentURI = aWindow.BrowserApp.selectedBrowser.currentURI.clone(); michael@0: // if the current URI contains a username/password, remove it michael@0: try { michael@0: currentURI.userPass = ""; michael@0: } michael@0: catch (ex) { } // ignore failures on about: URIs michael@0: michael@0: CrashReporter.annotateCrashReport("URL", currentURI.spec); michael@0: } michael@0: catch (ex) { michael@0: // don't make noise when crashreporter is built but not enabled michael@0: if (ex.result != Components.results.NS_ERROR_NOT_INITIALIZED) michael@0: Components.utils.reportError("SessionStore:" + ex); michael@0: } michael@0: #endif michael@0: }, michael@0: michael@0: _serializeHistoryEntry: function _serializeHistoryEntry(aEntry) { michael@0: let entry = { url: aEntry.URI.spec }; michael@0: michael@0: if (aEntry.title && aEntry.title != entry.url) michael@0: entry.title = aEntry.title; michael@0: michael@0: if (!(aEntry instanceof Ci.nsISHEntry)) michael@0: return entry; michael@0: michael@0: let cacheKey = aEntry.cacheKey; michael@0: if (cacheKey && cacheKey instanceof Ci.nsISupportsPRUint32 && cacheKey.data != 0) michael@0: entry.cacheKey = cacheKey.data; michael@0: michael@0: entry.ID = aEntry.ID; michael@0: entry.docshellID = aEntry.docshellID; michael@0: michael@0: if (aEntry.referrerURI) michael@0: entry.referrer = aEntry.referrerURI.spec; michael@0: michael@0: if (aEntry.contentType) michael@0: entry.contentType = aEntry.contentType; michael@0: michael@0: let x = {}, y = {}; michael@0: aEntry.getScrollPosition(x, y); michael@0: if (x.value != 0 || y.value != 0) michael@0: entry.scroll = x.value + "," + y.value; michael@0: michael@0: if (aEntry.owner) { michael@0: try { michael@0: let binaryStream = Cc["@mozilla.org/binaryoutputstream;1"].createInstance(Ci.nsIObjectOutputStream); michael@0: let pipe = Cc["@mozilla.org/pipe;1"].createInstance(Ci.nsIPipe); michael@0: pipe.init(false, false, 0, 0xffffffff, null); michael@0: binaryStream.setOutputStream(pipe.outputStream); michael@0: binaryStream.writeCompoundObject(aEntry.owner, Ci.nsISupports, true); michael@0: binaryStream.close(); michael@0: michael@0: // Now we want to read the data from the pipe's input end and encode it. michael@0: let scriptableStream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream); michael@0: scriptableStream.setInputStream(pipe.inputStream); michael@0: let ownerBytes = scriptableStream.readByteArray(scriptableStream.available()); michael@0: // We can stop doing base64 encoding once our serialization into JSON michael@0: // is guaranteed to handle all chars in strings, including embedded michael@0: // nulls. michael@0: entry.owner_b64 = btoa(String.fromCharCode.apply(null, ownerBytes)); michael@0: } catch (e) { dump(e); } michael@0: } michael@0: michael@0: entry.docIdentifier = aEntry.BFCacheEntry.ID; michael@0: michael@0: if (aEntry.stateData != null) { michael@0: entry.structuredCloneState = aEntry.stateData.getDataAsBase64(); michael@0: entry.structuredCloneVersion = aEntry.stateData.formatVersion; michael@0: } michael@0: michael@0: if (!(aEntry instanceof Ci.nsISHContainer)) michael@0: return entry; michael@0: michael@0: if (aEntry.childCount > 0) { michael@0: let children = []; michael@0: for (let i = 0; i < aEntry.childCount; i++) { michael@0: let child = aEntry.GetChildAt(i); michael@0: michael@0: if (child) { michael@0: // don't try to restore framesets containing wyciwyg URLs (cf. bug 424689 and bug 450595) michael@0: if (child.URI.schemeIs("wyciwyg")) { michael@0: children = []; michael@0: break; michael@0: } michael@0: children.push(this._serializeHistoryEntry(child)); michael@0: } michael@0: michael@0: if (children.length) michael@0: entry.children = children; michael@0: } michael@0: } michael@0: michael@0: return entry; michael@0: }, michael@0: michael@0: _deserializeHistoryEntry: function _deserializeHistoryEntry(aEntry, aIdMap, aDocIdentMap) { michael@0: let shEntry = Cc["@mozilla.org/browser/session-history-entry;1"].createInstance(Ci.nsISHEntry); michael@0: michael@0: shEntry.setURI(Services.io.newURI(aEntry.url, null, null)); michael@0: shEntry.setTitle(aEntry.title || aEntry.url); michael@0: if (aEntry.subframe) michael@0: shEntry.setIsSubFrame(aEntry.subframe || false); michael@0: shEntry.loadType = Ci.nsIDocShellLoadInfo.loadHistory; michael@0: if (aEntry.contentType) michael@0: shEntry.contentType = aEntry.contentType; michael@0: if (aEntry.referrer) michael@0: shEntry.referrerURI = Services.io.newURI(aEntry.referrer, null, null); michael@0: michael@0: if (aEntry.cacheKey) { michael@0: let cacheKey = Cc["@mozilla.org/supports-PRUint32;1"].createInstance(Ci.nsISupportsPRUint32); michael@0: cacheKey.data = aEntry.cacheKey; michael@0: shEntry.cacheKey = cacheKey; michael@0: } michael@0: michael@0: if (aEntry.ID) { michael@0: // get a new unique ID for this frame (since the one from the last michael@0: // start might already be in use) michael@0: let id = aIdMap[aEntry.ID] || 0; michael@0: if (!id) { michael@0: for (id = Date.now(); id in aIdMap.used; id++); michael@0: aIdMap[aEntry.ID] = id; michael@0: aIdMap.used[id] = true; michael@0: } michael@0: shEntry.ID = id; michael@0: } michael@0: michael@0: if (aEntry.docshellID) michael@0: shEntry.docshellID = aEntry.docshellID; michael@0: michael@0: if (aEntry.structuredCloneState && aEntry.structuredCloneVersion) { michael@0: shEntry.stateData = michael@0: Cc["@mozilla.org/docshell/structured-clone-container;1"]. michael@0: createInstance(Ci.nsIStructuredCloneContainer); michael@0: michael@0: shEntry.stateData.initFromBase64(aEntry.structuredCloneState, aEntry.structuredCloneVersion); michael@0: } michael@0: michael@0: if (aEntry.scroll) { michael@0: let scrollPos = aEntry.scroll.split(","); michael@0: scrollPos = [parseInt(scrollPos[0]) || 0, parseInt(scrollPos[1]) || 0]; michael@0: shEntry.setScrollPosition(scrollPos[0], scrollPos[1]); michael@0: } michael@0: michael@0: let childDocIdents = {}; michael@0: if (aEntry.docIdentifier) { michael@0: // If we have a serialized document identifier, try to find an SHEntry michael@0: // which matches that doc identifier and adopt that SHEntry's michael@0: // BFCacheEntry. If we don't find a match, insert shEntry as the match michael@0: // for the document identifier. michael@0: let matchingEntry = aDocIdentMap[aEntry.docIdentifier]; michael@0: if (!matchingEntry) { michael@0: matchingEntry = {shEntry: shEntry, childDocIdents: childDocIdents}; michael@0: aDocIdentMap[aEntry.docIdentifier] = matchingEntry; michael@0: } else { michael@0: shEntry.adoptBFCacheEntry(matchingEntry.shEntry); michael@0: childDocIdents = matchingEntry.childDocIdents; michael@0: } michael@0: } michael@0: michael@0: if (aEntry.owner_b64) { michael@0: let ownerInput = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream); michael@0: let binaryData = atob(aEntry.owner_b64); michael@0: ownerInput.setData(binaryData, binaryData.length); michael@0: let binaryStream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIObjectInputStream); michael@0: binaryStream.setInputStream(ownerInput); michael@0: try { // Catch possible deserialization exceptions michael@0: shEntry.owner = binaryStream.readObject(true); michael@0: } catch (ex) { dump(ex); } michael@0: } michael@0: michael@0: if (aEntry.children && shEntry instanceof Ci.nsISHContainer) { michael@0: for (let i = 0; i < aEntry.children.length; i++) { michael@0: if (!aEntry.children[i].url) michael@0: continue; michael@0: michael@0: // We're getting sessionrestore.js files with a cycle in the michael@0: // doc-identifier graph, likely due to bug 698656. (That is, we have michael@0: // an entry where doc identifier A is an ancestor of doc identifier B, michael@0: // and another entry where doc identifier B is an ancestor of A.) michael@0: // michael@0: // If we were to respect these doc identifiers, we'd create a cycle in michael@0: // the SHEntries themselves, which causes the docshell to loop forever michael@0: // when it looks for the root SHEntry. michael@0: // michael@0: // So as a hack to fix this, we restrict the scope of a doc identifier michael@0: // to be a node's siblings and cousins, and pass childDocIdents, not michael@0: // aDocIdents, to _deserializeHistoryEntry. That is, we say that two michael@0: // SHEntries with the same doc identifier have the same document iff michael@0: // they have the same parent or their parents have the same document. michael@0: michael@0: shEntry.AddChild(this._deserializeHistoryEntry(aEntry.children[i], aIdMap, childDocIdents), i); michael@0: } michael@0: } michael@0: michael@0: return shEntry; michael@0: }, michael@0: michael@0: _restoreHistory: function _restoreHistory(aTabData, aHistory) { michael@0: if (aHistory.count > 0) michael@0: aHistory.PurgeHistory(aHistory.count); michael@0: aHistory.QueryInterface(Ci.nsISHistoryInternal); michael@0: michael@0: // helper hashes for ensuring unique frame IDs and unique document michael@0: // identifiers. michael@0: let idMap = { used: {} }; michael@0: let docIdentMap = {}; michael@0: michael@0: for (let i = 0; i < aTabData.entries.length; i++) { michael@0: if (!aTabData.entries[i].url) michael@0: continue; michael@0: aHistory.addEntry(this._deserializeHistoryEntry(aTabData.entries[i], idMap, docIdentMap), true); michael@0: } michael@0: michael@0: // We need to force set the active history item and cause it to reload since michael@0: // we stop the load above michael@0: let activeIndex = (aTabData.index || aTabData.entries.length) - 1; michael@0: aHistory.getEntryAtIndex(activeIndex, true); michael@0: aHistory.QueryInterface(Ci.nsISHistory).reloadCurrentEntry(); michael@0: }, michael@0: michael@0: getBrowserState: function ss_getBrowserState() { michael@0: return this._getCurrentState(); michael@0: }, michael@0: michael@0: _restoreWindow: function ss_restoreWindow(aData) { michael@0: let state; michael@0: try { michael@0: state = JSON.parse(aData); michael@0: } catch (e) { michael@0: Cu.reportError("SessionStore: invalid session JSON"); michael@0: return false; michael@0: } michael@0: michael@0: // To do a restore, we must have at least one window with one tab michael@0: if (!state || state.windows.length == 0 || !state.windows[0].tabs || state.windows[0].tabs.length == 0) { michael@0: Cu.reportError("SessionStore: no tabs to restore"); michael@0: return false; michael@0: } michael@0: michael@0: let window = Services.wm.getMostRecentWindow("navigator:browser"); michael@0: michael@0: let tabs = state.windows[0].tabs; michael@0: let selected = state.windows[0].selected; michael@0: if (selected == null || selected > tabs.length) // Clamp the selected index if it's bogus michael@0: selected = 1; michael@0: michael@0: for (let i = 0; i < tabs.length; i++) { michael@0: let tabData = tabs[i]; michael@0: let entry = tabData.entries[tabData.index - 1]; michael@0: michael@0: // Use stubbed tab if we've already created it; otherwise, make a new tab michael@0: let tab; michael@0: if (tabData.tabId == null) { michael@0: let params = { michael@0: selected: (selected == i+1), michael@0: delayLoad: true, michael@0: title: entry.title, michael@0: desktopMode: (tabData.desktopMode == true), michael@0: isPrivate: (tabData.isPrivate == true) michael@0: }; michael@0: tab = window.BrowserApp.addTab(entry.url, params); michael@0: } else { michael@0: tab = window.BrowserApp.getTabForId(tabData.tabId); michael@0: delete tabData.tabId; michael@0: michael@0: // Don't restore tab if user has closed it michael@0: if (tab == null) { michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: if (window.BrowserApp.selectedTab == tab) { michael@0: this._restoreHistory(tabData, tab.browser.sessionHistory); michael@0: delete tab.browser.__SS_restore; michael@0: tab.browser.removeAttribute("pending"); michael@0: } else { michael@0: // Make sure the browser has its session data for the delay reload michael@0: tab.browser.__SS_data = tabData; michael@0: tab.browser.__SS_restore = true; michael@0: tab.browser.setAttribute("pending", "true"); michael@0: } michael@0: michael@0: tab.browser.__SS_extdata = tabData.extData; michael@0: } michael@0: michael@0: return true; michael@0: }, michael@0: michael@0: getClosedTabCount: function ss_getClosedTabCount(aWindow) { michael@0: if (!aWindow || !aWindow.__SSID || !this._windows[aWindow.__SSID]) michael@0: return 0; // not a browser window, or not otherwise tracked by SS. michael@0: michael@0: return this._windows[aWindow.__SSID].closedTabs.length; michael@0: }, michael@0: michael@0: getClosedTabData: function ss_getClosedTabData(aWindow) { michael@0: if (!aWindow.__SSID) michael@0: throw (Components.returnCode = Cr.NS_ERROR_INVALID_ARG); michael@0: michael@0: return JSON.stringify(this._windows[aWindow.__SSID].closedTabs); michael@0: }, michael@0: michael@0: undoCloseTab: function ss_undoCloseTab(aWindow, aIndex) { michael@0: if (!aWindow.__SSID) michael@0: throw (Components.returnCode = Cr.NS_ERROR_INVALID_ARG); michael@0: michael@0: let closedTabs = this._windows[aWindow.__SSID].closedTabs; michael@0: if (!closedTabs) michael@0: return null; michael@0: michael@0: // default to the most-recently closed tab michael@0: aIndex = aIndex || 0; michael@0: if (!(aIndex in closedTabs)) michael@0: throw (Components.returnCode = Cr.NS_ERROR_INVALID_ARG); michael@0: michael@0: // fetch the data of closed tab, while removing it from the array michael@0: let closedTab = closedTabs.splice(aIndex, 1).shift(); michael@0: michael@0: // create a new tab and bring to front michael@0: let params = { selected: true }; michael@0: let tab = aWindow.BrowserApp.addTab(closedTab.entries[closedTab.index - 1].url, params); michael@0: this._restoreHistory(closedTab, tab.browser.sessionHistory); michael@0: michael@0: // Put back the extra data michael@0: tab.browser.__SS_extdata = closedTab.extData; michael@0: michael@0: return tab.browser; michael@0: }, michael@0: michael@0: forgetClosedTab: function ss_forgetClosedTab(aWindow, aIndex) { michael@0: if (!aWindow.__SSID) michael@0: throw (Components.returnCode = Cr.NS_ERROR_INVALID_ARG); michael@0: michael@0: let closedTabs = this._windows[aWindow.__SSID].closedTabs; michael@0: michael@0: // default to the most-recently closed tab michael@0: aIndex = aIndex || 0; michael@0: if (!(aIndex in closedTabs)) michael@0: throw (Components.returnCode = Cr.NS_ERROR_INVALID_ARG); michael@0: michael@0: // remove closed tab from the array michael@0: closedTabs.splice(aIndex, 1); michael@0: }, michael@0: michael@0: getTabValue: function ss_getTabValue(aTab, aKey) { michael@0: let browser = aTab.browser; michael@0: let data = browser.__SS_extdata || {}; michael@0: return data[aKey] || ""; michael@0: }, michael@0: michael@0: setTabValue: function ss_setTabValue(aTab, aKey, aStringValue) { michael@0: let browser = aTab.browser; michael@0: michael@0: if (!browser.__SS_extdata) michael@0: browser.__SS_extdata = {}; michael@0: browser.__SS_extdata[aKey] = aStringValue; michael@0: this.saveStateDelayed(); michael@0: }, michael@0: michael@0: deleteTabValue: function ss_deleteTabValue(aTab, aKey) { michael@0: let browser = aTab.browser; michael@0: if (browser.__SS_extdata && browser.__SS_extdata[aKey]) michael@0: delete browser.__SS_extdata[aKey]; michael@0: else michael@0: throw (Components.returnCode = Cr.NS_ERROR_INVALID_ARG); michael@0: }, michael@0: michael@0: restoreLastSession: function ss_restoreLastSession(aSessionString) { michael@0: let self = this; michael@0: michael@0: function restoreWindow(data) { michael@0: if (!self._restoreWindow(data)) { michael@0: throw "Could not restore window"; michael@0: } michael@0: michael@0: notifyObservers(); michael@0: } michael@0: michael@0: function notifyObservers(aMessage) { michael@0: Services.obs.notifyObservers(null, "sessionstore-windows-restored", aMessage || ""); michael@0: } michael@0: michael@0: try { michael@0: // Normally, we'll receive the session string from Java, but there are michael@0: // cases where we may want to restore that Java cannot detect (e.g., if michael@0: // browser.sessionstore.resume_session_once is true). In these cases, the michael@0: // session will be read from sessionstore.bak (which is also used for michael@0: // "tabs from last time"). michael@0: if (aSessionString == null) { michael@0: Task.spawn(function() { michael@0: let bytes = yield OS.File.read(this._sessionFileBackup.path); michael@0: let data = JSON.parse(new TextDecoder().decode(bytes) || ""); michael@0: restoreWindow(data); michael@0: }.bind(this)).then(null, function onError(reason) { michael@0: if (reason instanceof OS.File.Error && reason.becauseNoSuchFile) { michael@0: Cu.reportError("Session file doesn't exist"); michael@0: } else { michael@0: Cu.reportError("SessionStore: " + reason.message); michael@0: } michael@0: notifyObservers("fail"); michael@0: }); michael@0: } else { michael@0: restoreWindow(aSessionString); michael@0: } michael@0: } catch (e) { michael@0: Cu.reportError("SessionStore: " + e); michael@0: notifyObservers("fail"); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SessionStore]);