mobile/android/components/SessionStore.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 const Cc = Components.classes;
michael@0 6 const Ci = Components.interfaces;
michael@0 7 const Cu = Components.utils;
michael@0 8 const Cr = Components.results;
michael@0 9
michael@0 10 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 11 Cu.import("resource://gre/modules/Services.jsm");
michael@0 12
michael@0 13 #ifdef MOZ_CRASHREPORTER
michael@0 14 XPCOMUtils.defineLazyServiceGetter(this, "CrashReporter",
michael@0 15 "@mozilla.org/xre/app-info;1", "nsICrashReporter");
michael@0 16 #endif
michael@0 17
michael@0 18 XPCOMUtils.defineLazyModuleGetter(this, "Task", "resource://gre/modules/Task.jsm");
michael@0 19 XPCOMUtils.defineLazyModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm");
michael@0 20 XPCOMUtils.defineLazyModuleGetter(this, "sendMessageToJava", "resource://gre/modules/Messaging.jsm");
michael@0 21
michael@0 22 function dump(a) {
michael@0 23 Services.console.logStringMessage(a);
michael@0 24 }
michael@0 25
michael@0 26 // -----------------------------------------------------------------------
michael@0 27 // Session Store
michael@0 28 // -----------------------------------------------------------------------
michael@0 29
michael@0 30 const STATE_STOPPED = 0;
michael@0 31 const STATE_RUNNING = 1;
michael@0 32
michael@0 33 function SessionStore() { }
michael@0 34
michael@0 35 SessionStore.prototype = {
michael@0 36 classID: Components.ID("{8c1f07d6-cba3-4226-a315-8bd43d67d032}"),
michael@0 37
michael@0 38 QueryInterface: XPCOMUtils.generateQI([Ci.nsISessionStore,
michael@0 39 Ci.nsIDOMEventListener,
michael@0 40 Ci.nsIObserver,
michael@0 41 Ci.nsISupportsWeakReference]),
michael@0 42
michael@0 43 _windows: {},
michael@0 44 _lastSaveTime: 0,
michael@0 45 _interval: 10000,
michael@0 46 _maxTabsUndo: 1,
michael@0 47 _pendingWrite: 0,
michael@0 48
michael@0 49 init: function ss_init() {
michael@0 50 // Get file references
michael@0 51 this._sessionFile = Services.dirsvc.get("ProfD", Ci.nsILocalFile);
michael@0 52 this._sessionFileBackup = this._sessionFile.clone();
michael@0 53 this._sessionFile.append("sessionstore.js");
michael@0 54 this._sessionFileBackup.append("sessionstore.bak");
michael@0 55
michael@0 56 this._loadState = STATE_STOPPED;
michael@0 57
michael@0 58 this._interval = Services.prefs.getIntPref("browser.sessionstore.interval");
michael@0 59 this._maxTabsUndo = Services.prefs.getIntPref("browser.sessionstore.max_tabs_undo");
michael@0 60 },
michael@0 61
michael@0 62 _clearDisk: function ss_clearDisk() {
michael@0 63 OS.File.remove(this._sessionFile.path);
michael@0 64 OS.File.remove(this._sessionFileBackup.path);
michael@0 65 },
michael@0 66
michael@0 67 observe: function ss_observe(aSubject, aTopic, aData) {
michael@0 68 let self = this;
michael@0 69 let observerService = Services.obs;
michael@0 70 switch (aTopic) {
michael@0 71 case "app-startup":
michael@0 72 observerService.addObserver(this, "final-ui-startup", true);
michael@0 73 observerService.addObserver(this, "domwindowopened", true);
michael@0 74 observerService.addObserver(this, "domwindowclosed", true);
michael@0 75 observerService.addObserver(this, "browser:purge-session-history", true);
michael@0 76 observerService.addObserver(this, "Session:Restore", true);
michael@0 77 observerService.addObserver(this, "application-background", true);
michael@0 78 break;
michael@0 79 case "final-ui-startup":
michael@0 80 observerService.removeObserver(this, "final-ui-startup");
michael@0 81 this.init();
michael@0 82 break;
michael@0 83 case "domwindowopened": {
michael@0 84 let window = aSubject;
michael@0 85 window.addEventListener("load", function() {
michael@0 86 self.onWindowOpen(window);
michael@0 87 window.removeEventListener("load", arguments.callee, false);
michael@0 88 }, false);
michael@0 89 break;
michael@0 90 }
michael@0 91 case "domwindowclosed": // catch closed windows
michael@0 92 this.onWindowClose(aSubject);
michael@0 93 break;
michael@0 94 case "browser:purge-session-history": // catch sanitization
michael@0 95 this._clearDisk();
michael@0 96
michael@0 97 // Clear all data about closed tabs
michael@0 98 for (let [ssid, win] in Iterator(this._windows))
michael@0 99 win.closedTabs = [];
michael@0 100
michael@0 101 if (this._loadState == STATE_RUNNING) {
michael@0 102 // Save the purged state immediately
michael@0 103 this.saveState();
michael@0 104 }
michael@0 105
michael@0 106 Services.obs.notifyObservers(null, "sessionstore-state-purge-complete", "");
michael@0 107 break;
michael@0 108 case "timer-callback":
michael@0 109 // Timer call back for delayed saving
michael@0 110 this._saveTimer = null;
michael@0 111 if (this._pendingWrite) {
michael@0 112 this.saveState();
michael@0 113 }
michael@0 114 break;
michael@0 115 case "Session:Restore": {
michael@0 116 Services.obs.removeObserver(this, "Session:Restore");
michael@0 117 if (aData) {
michael@0 118 // Be ready to handle any restore failures by making sure we have a valid tab opened
michael@0 119 let window = Services.wm.getMostRecentWindow("navigator:browser");
michael@0 120 let restoreCleanup = {
michael@0 121 observe: function (aSubject, aTopic, aData) {
michael@0 122 Services.obs.removeObserver(restoreCleanup, "sessionstore-windows-restored");
michael@0 123
michael@0 124 if (window.BrowserApp.tabs.length == 0) {
michael@0 125 window.BrowserApp.addTab("about:home", {
michael@0 126 selected: true
michael@0 127 });
michael@0 128 }
michael@0 129
michael@0 130 // Let Java know we're done restoring tabs so tabs added after this can be animated
michael@0 131 sendMessageToJava({
michael@0 132 type: "Session:RestoreEnd"
michael@0 133 });
michael@0 134 }.bind(this)
michael@0 135 };
michael@0 136 Services.obs.addObserver(restoreCleanup, "sessionstore-windows-restored", false);
michael@0 137
michael@0 138 // Do a restore, triggered by Java
michael@0 139 let data = JSON.parse(aData);
michael@0 140 this.restoreLastSession(data.sessionString);
michael@0 141 } else {
michael@0 142 // Not doing a restore; just send restore message
michael@0 143 Services.obs.notifyObservers(null, "sessionstore-windows-restored", "");
michael@0 144 }
michael@0 145 break;
michael@0 146 }
michael@0 147 case "application-background":
michael@0 148 // We receive this notification when Android's onPause callback is
michael@0 149 // executed. After onPause, the application may be terminated at any
michael@0 150 // point without notice; therefore, we must synchronously write out any
michael@0 151 // pending save state to ensure that this data does not get lost.
michael@0 152 this.flushPendingState();
michael@0 153 break;
michael@0 154 }
michael@0 155 },
michael@0 156
michael@0 157 handleEvent: function ss_handleEvent(aEvent) {
michael@0 158 let window = aEvent.currentTarget.ownerDocument.defaultView;
michael@0 159 switch (aEvent.type) {
michael@0 160 case "TabOpen": {
michael@0 161 let browser = aEvent.target;
michael@0 162 this.onTabAdd(window, browser);
michael@0 163 break;
michael@0 164 }
michael@0 165 case "TabClose": {
michael@0 166 let browser = aEvent.target;
michael@0 167 this.onTabClose(window, browser);
michael@0 168 this.onTabRemove(window, browser);
michael@0 169 break;
michael@0 170 }
michael@0 171 case "TabSelect": {
michael@0 172 let browser = aEvent.target;
michael@0 173 this.onTabSelect(window, browser);
michael@0 174 break;
michael@0 175 }
michael@0 176 case "DOMTitleChanged": {
michael@0 177 let browser = aEvent.currentTarget;
michael@0 178
michael@0 179 // Handle only top-level DOMTitleChanged event
michael@0 180 if (browser.contentDocument !== aEvent.originalTarget)
michael@0 181 return;
michael@0 182
michael@0 183 // Use DOMTitleChanged to detect page loads over alternatives.
michael@0 184 // onLocationChange happens too early, so we don't have the page title
michael@0 185 // yet; pageshow happens too late, so we could lose session data if the
michael@0 186 // browser were killed.
michael@0 187 this.onTabLoad(window, browser);
michael@0 188 break;
michael@0 189 }
michael@0 190 }
michael@0 191 },
michael@0 192
michael@0 193 onWindowOpen: function ss_onWindowOpen(aWindow) {
michael@0 194 // Return if window has already been initialized
michael@0 195 if (aWindow && aWindow.__SSID && this._windows[aWindow.__SSID])
michael@0 196 return;
michael@0 197
michael@0 198 // Ignore non-browser windows and windows opened while shutting down
michael@0 199 if (aWindow.document.documentElement.getAttribute("windowtype") != "navigator:browser")
michael@0 200 return;
michael@0 201
michael@0 202 // Assign it a unique identifier (timestamp) and create its data object
michael@0 203 aWindow.__SSID = "window" + Date.now();
michael@0 204 this._windows[aWindow.__SSID] = { tabs: [], selected: 0, closedTabs: [] };
michael@0 205
michael@0 206 // Perform additional initialization when the first window is loading
michael@0 207 if (this._loadState == STATE_STOPPED) {
michael@0 208 this._loadState = STATE_RUNNING;
michael@0 209 this._lastSaveTime = Date.now();
michael@0 210 }
michael@0 211
michael@0 212 // Add tab change listeners to all already existing tabs
michael@0 213 let tabs = aWindow.BrowserApp.tabs;
michael@0 214 for (let i = 0; i < tabs.length; i++)
michael@0 215 this.onTabAdd(aWindow, tabs[i].browser, true);
michael@0 216
michael@0 217 // Notification of tab add/remove/selection
michael@0 218 let browsers = aWindow.document.getElementById("browsers");
michael@0 219 browsers.addEventListener("TabOpen", this, true);
michael@0 220 browsers.addEventListener("TabClose", this, true);
michael@0 221 browsers.addEventListener("TabSelect", this, true);
michael@0 222 },
michael@0 223
michael@0 224 onWindowClose: function ss_onWindowClose(aWindow) {
michael@0 225 // Ignore windows not tracked by SessionStore
michael@0 226 if (!aWindow.__SSID || !this._windows[aWindow.__SSID])
michael@0 227 return;
michael@0 228
michael@0 229 let browsers = aWindow.document.getElementById("browsers");
michael@0 230 browsers.removeEventListener("TabOpen", this, true);
michael@0 231 browsers.removeEventListener("TabClose", this, true);
michael@0 232 browsers.removeEventListener("TabSelect", this, true);
michael@0 233
michael@0 234 if (this._loadState == STATE_RUNNING) {
michael@0 235 // Update all window data for a last time
michael@0 236 this._collectWindowData(aWindow);
michael@0 237
michael@0 238 // Clear this window from the list
michael@0 239 delete this._windows[aWindow.__SSID];
michael@0 240
michael@0 241 // Save the state without this window to disk
michael@0 242 this.saveStateDelayed();
michael@0 243 }
michael@0 244
michael@0 245 let tabs = aWindow.BrowserApp.tabs;
michael@0 246 for (let i = 0; i < tabs.length; i++)
michael@0 247 this.onTabRemove(aWindow, tabs[i].browser, true);
michael@0 248
michael@0 249 delete aWindow.__SSID;
michael@0 250 },
michael@0 251
michael@0 252 onTabAdd: function ss_onTabAdd(aWindow, aBrowser, aNoNotification) {
michael@0 253 aBrowser.addEventListener("DOMTitleChanged", this, true);
michael@0 254 if (!aNoNotification)
michael@0 255 this.saveStateDelayed();
michael@0 256 this._updateCrashReportURL(aWindow);
michael@0 257 },
michael@0 258
michael@0 259 onTabRemove: function ss_onTabRemove(aWindow, aBrowser, aNoNotification) {
michael@0 260 aBrowser.removeEventListener("DOMTitleChanged", this, true);
michael@0 261
michael@0 262 // If this browser is being restored, skip any session save activity
michael@0 263 if (aBrowser.__SS_restore)
michael@0 264 return;
michael@0 265
michael@0 266 delete aBrowser.__SS_data;
michael@0 267
michael@0 268 if (!aNoNotification)
michael@0 269 this.saveStateDelayed();
michael@0 270 },
michael@0 271
michael@0 272 onTabClose: function ss_onTabClose(aWindow, aBrowser) {
michael@0 273 if (this._maxTabsUndo == 0)
michael@0 274 return;
michael@0 275
michael@0 276 if (aWindow.BrowserApp.tabs.length > 0) {
michael@0 277 // Bundle this browser's data and extra data and save in the closedTabs
michael@0 278 // window property
michael@0 279 let data = aBrowser.__SS_data;
michael@0 280 data.extData = aBrowser.__SS_extdata;
michael@0 281
michael@0 282 this._windows[aWindow.__SSID].closedTabs.unshift(data);
michael@0 283 let length = this._windows[aWindow.__SSID].closedTabs.length;
michael@0 284 if (length > this._maxTabsUndo)
michael@0 285 this._windows[aWindow.__SSID].closedTabs.splice(this._maxTabsUndo, length - this._maxTabsUndo);
michael@0 286 }
michael@0 287 },
michael@0 288
michael@0 289 onTabLoad: function ss_onTabLoad(aWindow, aBrowser) {
michael@0 290 // If this browser is being restored, skip any session save activity
michael@0 291 if (aBrowser.__SS_restore)
michael@0 292 return;
michael@0 293
michael@0 294 // Ignore a transient "about:blank"
michael@0 295 if (!aBrowser.canGoBack && aBrowser.currentURI.spec == "about:blank")
michael@0 296 return;
michael@0 297
michael@0 298 let history = aBrowser.sessionHistory;
michael@0 299
michael@0 300 // Serialize the tab data
michael@0 301 let entries = [];
michael@0 302 let index = history.index + 1;
michael@0 303 for (let i = 0; i < history.count; i++) {
michael@0 304 let historyEntry = history.getEntryAtIndex(i, false);
michael@0 305 // Don't try to restore wyciwyg URLs
michael@0 306 if (historyEntry.URI.schemeIs("wyciwyg")) {
michael@0 307 // Adjust the index to account for skipped history entries
michael@0 308 if (i <= history.index)
michael@0 309 index--;
michael@0 310 continue;
michael@0 311 }
michael@0 312 let entry = this._serializeHistoryEntry(historyEntry);
michael@0 313 entries.push(entry);
michael@0 314 }
michael@0 315 let data = { entries: entries, index: index };
michael@0 316
michael@0 317 delete aBrowser.__SS_data;
michael@0 318 this._collectTabData(aWindow, aBrowser, data);
michael@0 319 this.saveStateDelayed();
michael@0 320
michael@0 321 this._updateCrashReportURL(aWindow);
michael@0 322 },
michael@0 323
michael@0 324 onTabSelect: function ss_onTabSelect(aWindow, aBrowser) {
michael@0 325 if (this._loadState != STATE_RUNNING)
michael@0 326 return;
michael@0 327
michael@0 328 let browsers = aWindow.document.getElementById("browsers");
michael@0 329 let index = browsers.selectedIndex;
michael@0 330 this._windows[aWindow.__SSID].selected = parseInt(index) + 1; // 1-based
michael@0 331
michael@0 332 // Restore the resurrected browser
michael@0 333 if (aBrowser.__SS_restore) {
michael@0 334 let data = aBrowser.__SS_data;
michael@0 335 if (data.entries.length > 0)
michael@0 336 this._restoreHistory(data, aBrowser.sessionHistory);
michael@0 337
michael@0 338 delete aBrowser.__SS_restore;
michael@0 339 aBrowser.removeAttribute("pending");
michael@0 340 }
michael@0 341
michael@0 342 this.saveStateDelayed();
michael@0 343 this._updateCrashReportURL(aWindow);
michael@0 344 },
michael@0 345
michael@0 346 saveStateDelayed: function ss_saveStateDelayed() {
michael@0 347 if (!this._saveTimer) {
michael@0 348 // Interval until the next disk operation is allowed
michael@0 349 let minimalDelay = this._lastSaveTime + this._interval - Date.now();
michael@0 350
michael@0 351 // If we have to wait, set a timer, otherwise saveState directly
michael@0 352 let delay = Math.max(minimalDelay, 2000);
michael@0 353 if (delay > 0) {
michael@0 354 this._pendingWrite++;
michael@0 355 this._saveTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
michael@0 356 this._saveTimer.init(this, delay, Ci.nsITimer.TYPE_ONE_SHOT);
michael@0 357 } else {
michael@0 358 this.saveState();
michael@0 359 }
michael@0 360 }
michael@0 361 },
michael@0 362
michael@0 363 saveState: function ss_saveState() {
michael@0 364 this._pendingWrite++;
michael@0 365 this._saveState(true);
michael@0 366 },
michael@0 367
michael@0 368 // Immediately and synchronously writes any pending state to disk.
michael@0 369 flushPendingState: function ss_flushPendingState() {
michael@0 370 if (this._pendingWrite) {
michael@0 371 this._saveState(false);
michael@0 372 }
michael@0 373 },
michael@0 374
michael@0 375 _saveState: function ss_saveState(aAsync) {
michael@0 376 // Kill any queued timer and save immediately
michael@0 377 if (this._saveTimer) {
michael@0 378 this._saveTimer.cancel();
michael@0 379 this._saveTimer = null;
michael@0 380 }
michael@0 381
michael@0 382 let data = this._getCurrentState();
michael@0 383 let normalData = { windows: [] };
michael@0 384 let privateData = { windows: [] };
michael@0 385
michael@0 386 for (let winIndex = 0; winIndex < data.windows.length; ++winIndex) {
michael@0 387 let win = data.windows[winIndex];
michael@0 388 let normalWin = {};
michael@0 389 for (let prop in win) {
michael@0 390 normalWin[prop] = data[prop];
michael@0 391 }
michael@0 392 normalWin.tabs = [];
michael@0 393 normalData.windows.push(normalWin);
michael@0 394 privateData.windows.push({ tabs: [] });
michael@0 395
michael@0 396 // Split the session data into private and non-private data objects.
michael@0 397 // Non-private session data will be saved to disk, and private session
michael@0 398 // data will be sent to Java for Android to hold it in memory.
michael@0 399 for (let i = 0; i < win.tabs.length; ++i) {
michael@0 400 let tab = win.tabs[i];
michael@0 401 let savedWin = tab.isPrivate ? privateData.windows[winIndex] : normalData.windows[winIndex];
michael@0 402 savedWin.tabs.push(tab);
michael@0 403 if (win.selected == i + 1) {
michael@0 404 savedWin.selected = savedWin.tabs.length;
michael@0 405 }
michael@0 406 }
michael@0 407 }
michael@0 408
michael@0 409 // Write only non-private data to disk
michael@0 410 this._writeFile(this._sessionFile, JSON.stringify(normalData), aAsync);
michael@0 411
michael@0 412 // If we have private data, send it to Java; otherwise, send null to
michael@0 413 // indicate that there is no private data
michael@0 414 sendMessageToJava({
michael@0 415 type: "PrivateBrowsing:Data",
michael@0 416 session: (privateData.windows[0].tabs.length > 0) ? JSON.stringify(privateData) : null
michael@0 417 });
michael@0 418
michael@0 419 this._lastSaveTime = Date.now();
michael@0 420 },
michael@0 421
michael@0 422 _getCurrentState: function ss_getCurrentState() {
michael@0 423 let self = this;
michael@0 424 this._forEachBrowserWindow(function(aWindow) {
michael@0 425 self._collectWindowData(aWindow);
michael@0 426 });
michael@0 427
michael@0 428 let data = { windows: [] };
michael@0 429 for (let index in this._windows) {
michael@0 430 data.windows.push(this._windows[index]);
michael@0 431 }
michael@0 432
michael@0 433 return data;
michael@0 434 },
michael@0 435
michael@0 436 _collectTabData: function ss__collectTabData(aWindow, aBrowser, aHistory) {
michael@0 437 // If this browser is being restored, skip any session save activity
michael@0 438 if (aBrowser.__SS_restore)
michael@0 439 return;
michael@0 440
michael@0 441 aHistory = aHistory || { entries: [{ url: aBrowser.currentURI.spec, title: aBrowser.contentTitle }], index: 1 };
michael@0 442
michael@0 443 let tabData = {};
michael@0 444 tabData.entries = aHistory.entries;
michael@0 445 tabData.index = aHistory.index;
michael@0 446 tabData.attributes = { image: aBrowser.mIconURL };
michael@0 447 tabData.desktopMode = aWindow.BrowserApp.getTabForBrowser(aBrowser).desktopMode;
michael@0 448 tabData.isPrivate = aBrowser.docShell.QueryInterface(Ci.nsILoadContext).usePrivateBrowsing;
michael@0 449
michael@0 450 aBrowser.__SS_data = tabData;
michael@0 451 },
michael@0 452
michael@0 453 _collectWindowData: function ss__collectWindowData(aWindow) {
michael@0 454 // Ignore windows not tracked by SessionStore
michael@0 455 if (!aWindow.__SSID || !this._windows[aWindow.__SSID])
michael@0 456 return;
michael@0 457
michael@0 458 let winData = this._windows[aWindow.__SSID];
michael@0 459 winData.tabs = [];
michael@0 460
michael@0 461 let browsers = aWindow.document.getElementById("browsers");
michael@0 462 let index = browsers.selectedIndex;
michael@0 463 winData.selected = parseInt(index) + 1; // 1-based
michael@0 464
michael@0 465 let tabs = aWindow.BrowserApp.tabs;
michael@0 466 for (let i = 0; i < tabs.length; i++) {
michael@0 467 let browser = tabs[i].browser;
michael@0 468 if (browser.__SS_data) {
michael@0 469 let tabData = browser.__SS_data;
michael@0 470 if (browser.__SS_extdata)
michael@0 471 tabData.extData = browser.__SS_extdata;
michael@0 472 winData.tabs.push(tabData);
michael@0 473 }
michael@0 474 }
michael@0 475 },
michael@0 476
michael@0 477 _forEachBrowserWindow: function ss_forEachBrowserWindow(aFunc) {
michael@0 478 let windowsEnum = Services.wm.getEnumerator("navigator:browser");
michael@0 479 while (windowsEnum.hasMoreElements()) {
michael@0 480 let window = windowsEnum.getNext();
michael@0 481 if (window.__SSID && !window.closed)
michael@0 482 aFunc.call(this, window);
michael@0 483 }
michael@0 484 },
michael@0 485
michael@0 486 _writeFile: function ss_writeFile(aFile, aData, aAsync) {
michael@0 487 let stateString = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
michael@0 488 stateString.data = aData;
michael@0 489 Services.obs.notifyObservers(stateString, "sessionstore-state-write", "");
michael@0 490
michael@0 491 // Don't touch the file if an observer has deleted all state data
michael@0 492 if (!stateString.data)
michael@0 493 return;
michael@0 494
michael@0 495 if (aAsync) {
michael@0 496 let array = new TextEncoder().encode(aData);
michael@0 497 let pendingWrite = this._pendingWrite;
michael@0 498 OS.File.writeAtomic(aFile.path, array, { tmpPath: aFile.path + ".tmp" }).then(function onSuccess() {
michael@0 499 // Make sure this._pendingWrite is the same value it was before we
michael@0 500 // fired off the async write. If the count is different, another write
michael@0 501 // is pending, so we shouldn't reset this._pendingWrite yet.
michael@0 502 if (pendingWrite === this._pendingWrite)
michael@0 503 this._pendingWrite = 0;
michael@0 504 Services.obs.notifyObservers(null, "sessionstore-state-write-complete", "");
michael@0 505 }.bind(this));
michael@0 506 } else {
michael@0 507 this._pendingWrite = 0;
michael@0 508 let foStream = Cc["@mozilla.org/network/file-output-stream;1"].
michael@0 509 createInstance(Ci.nsIFileOutputStream);
michael@0 510 foStream.init(aFile, 0x02 | 0x08 | 0x20, 0666, 0);
michael@0 511 let converter = Cc["@mozilla.org/intl/converter-output-stream;1"].
michael@0 512 createInstance(Ci.nsIConverterOutputStream);
michael@0 513 converter.init(foStream, "UTF-8", 0, 0);
michael@0 514 converter.writeString(aData);
michael@0 515 converter.close();
michael@0 516 }
michael@0 517 },
michael@0 518
michael@0 519 _updateCrashReportURL: function ss_updateCrashReportURL(aWindow) {
michael@0 520 #ifdef MOZ_CRASHREPORTER
michael@0 521 if (!aWindow.BrowserApp.selectedBrowser)
michael@0 522 return;
michael@0 523
michael@0 524 try {
michael@0 525 let currentURI = aWindow.BrowserApp.selectedBrowser.currentURI.clone();
michael@0 526 // if the current URI contains a username/password, remove it
michael@0 527 try {
michael@0 528 currentURI.userPass = "";
michael@0 529 }
michael@0 530 catch (ex) { } // ignore failures on about: URIs
michael@0 531
michael@0 532 CrashReporter.annotateCrashReport("URL", currentURI.spec);
michael@0 533 }
michael@0 534 catch (ex) {
michael@0 535 // don't make noise when crashreporter is built but not enabled
michael@0 536 if (ex.result != Components.results.NS_ERROR_NOT_INITIALIZED)
michael@0 537 Components.utils.reportError("SessionStore:" + ex);
michael@0 538 }
michael@0 539 #endif
michael@0 540 },
michael@0 541
michael@0 542 _serializeHistoryEntry: function _serializeHistoryEntry(aEntry) {
michael@0 543 let entry = { url: aEntry.URI.spec };
michael@0 544
michael@0 545 if (aEntry.title && aEntry.title != entry.url)
michael@0 546 entry.title = aEntry.title;
michael@0 547
michael@0 548 if (!(aEntry instanceof Ci.nsISHEntry))
michael@0 549 return entry;
michael@0 550
michael@0 551 let cacheKey = aEntry.cacheKey;
michael@0 552 if (cacheKey && cacheKey instanceof Ci.nsISupportsPRUint32 && cacheKey.data != 0)
michael@0 553 entry.cacheKey = cacheKey.data;
michael@0 554
michael@0 555 entry.ID = aEntry.ID;
michael@0 556 entry.docshellID = aEntry.docshellID;
michael@0 557
michael@0 558 if (aEntry.referrerURI)
michael@0 559 entry.referrer = aEntry.referrerURI.spec;
michael@0 560
michael@0 561 if (aEntry.contentType)
michael@0 562 entry.contentType = aEntry.contentType;
michael@0 563
michael@0 564 let x = {}, y = {};
michael@0 565 aEntry.getScrollPosition(x, y);
michael@0 566 if (x.value != 0 || y.value != 0)
michael@0 567 entry.scroll = x.value + "," + y.value;
michael@0 568
michael@0 569 if (aEntry.owner) {
michael@0 570 try {
michael@0 571 let binaryStream = Cc["@mozilla.org/binaryoutputstream;1"].createInstance(Ci.nsIObjectOutputStream);
michael@0 572 let pipe = Cc["@mozilla.org/pipe;1"].createInstance(Ci.nsIPipe);
michael@0 573 pipe.init(false, false, 0, 0xffffffff, null);
michael@0 574 binaryStream.setOutputStream(pipe.outputStream);
michael@0 575 binaryStream.writeCompoundObject(aEntry.owner, Ci.nsISupports, true);
michael@0 576 binaryStream.close();
michael@0 577
michael@0 578 // Now we want to read the data from the pipe's input end and encode it.
michael@0 579 let scriptableStream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream);
michael@0 580 scriptableStream.setInputStream(pipe.inputStream);
michael@0 581 let ownerBytes = scriptableStream.readByteArray(scriptableStream.available());
michael@0 582 // We can stop doing base64 encoding once our serialization into JSON
michael@0 583 // is guaranteed to handle all chars in strings, including embedded
michael@0 584 // nulls.
michael@0 585 entry.owner_b64 = btoa(String.fromCharCode.apply(null, ownerBytes));
michael@0 586 } catch (e) { dump(e); }
michael@0 587 }
michael@0 588
michael@0 589 entry.docIdentifier = aEntry.BFCacheEntry.ID;
michael@0 590
michael@0 591 if (aEntry.stateData != null) {
michael@0 592 entry.structuredCloneState = aEntry.stateData.getDataAsBase64();
michael@0 593 entry.structuredCloneVersion = aEntry.stateData.formatVersion;
michael@0 594 }
michael@0 595
michael@0 596 if (!(aEntry instanceof Ci.nsISHContainer))
michael@0 597 return entry;
michael@0 598
michael@0 599 if (aEntry.childCount > 0) {
michael@0 600 let children = [];
michael@0 601 for (let i = 0; i < aEntry.childCount; i++) {
michael@0 602 let child = aEntry.GetChildAt(i);
michael@0 603
michael@0 604 if (child) {
michael@0 605 // don't try to restore framesets containing wyciwyg URLs (cf. bug 424689 and bug 450595)
michael@0 606 if (child.URI.schemeIs("wyciwyg")) {
michael@0 607 children = [];
michael@0 608 break;
michael@0 609 }
michael@0 610 children.push(this._serializeHistoryEntry(child));
michael@0 611 }
michael@0 612
michael@0 613 if (children.length)
michael@0 614 entry.children = children;
michael@0 615 }
michael@0 616 }
michael@0 617
michael@0 618 return entry;
michael@0 619 },
michael@0 620
michael@0 621 _deserializeHistoryEntry: function _deserializeHistoryEntry(aEntry, aIdMap, aDocIdentMap) {
michael@0 622 let shEntry = Cc["@mozilla.org/browser/session-history-entry;1"].createInstance(Ci.nsISHEntry);
michael@0 623
michael@0 624 shEntry.setURI(Services.io.newURI(aEntry.url, null, null));
michael@0 625 shEntry.setTitle(aEntry.title || aEntry.url);
michael@0 626 if (aEntry.subframe)
michael@0 627 shEntry.setIsSubFrame(aEntry.subframe || false);
michael@0 628 shEntry.loadType = Ci.nsIDocShellLoadInfo.loadHistory;
michael@0 629 if (aEntry.contentType)
michael@0 630 shEntry.contentType = aEntry.contentType;
michael@0 631 if (aEntry.referrer)
michael@0 632 shEntry.referrerURI = Services.io.newURI(aEntry.referrer, null, null);
michael@0 633
michael@0 634 if (aEntry.cacheKey) {
michael@0 635 let cacheKey = Cc["@mozilla.org/supports-PRUint32;1"].createInstance(Ci.nsISupportsPRUint32);
michael@0 636 cacheKey.data = aEntry.cacheKey;
michael@0 637 shEntry.cacheKey = cacheKey;
michael@0 638 }
michael@0 639
michael@0 640 if (aEntry.ID) {
michael@0 641 // get a new unique ID for this frame (since the one from the last
michael@0 642 // start might already be in use)
michael@0 643 let id = aIdMap[aEntry.ID] || 0;
michael@0 644 if (!id) {
michael@0 645 for (id = Date.now(); id in aIdMap.used; id++);
michael@0 646 aIdMap[aEntry.ID] = id;
michael@0 647 aIdMap.used[id] = true;
michael@0 648 }
michael@0 649 shEntry.ID = id;
michael@0 650 }
michael@0 651
michael@0 652 if (aEntry.docshellID)
michael@0 653 shEntry.docshellID = aEntry.docshellID;
michael@0 654
michael@0 655 if (aEntry.structuredCloneState && aEntry.structuredCloneVersion) {
michael@0 656 shEntry.stateData =
michael@0 657 Cc["@mozilla.org/docshell/structured-clone-container;1"].
michael@0 658 createInstance(Ci.nsIStructuredCloneContainer);
michael@0 659
michael@0 660 shEntry.stateData.initFromBase64(aEntry.structuredCloneState, aEntry.structuredCloneVersion);
michael@0 661 }
michael@0 662
michael@0 663 if (aEntry.scroll) {
michael@0 664 let scrollPos = aEntry.scroll.split(",");
michael@0 665 scrollPos = [parseInt(scrollPos[0]) || 0, parseInt(scrollPos[1]) || 0];
michael@0 666 shEntry.setScrollPosition(scrollPos[0], scrollPos[1]);
michael@0 667 }
michael@0 668
michael@0 669 let childDocIdents = {};
michael@0 670 if (aEntry.docIdentifier) {
michael@0 671 // If we have a serialized document identifier, try to find an SHEntry
michael@0 672 // which matches that doc identifier and adopt that SHEntry's
michael@0 673 // BFCacheEntry. If we don't find a match, insert shEntry as the match
michael@0 674 // for the document identifier.
michael@0 675 let matchingEntry = aDocIdentMap[aEntry.docIdentifier];
michael@0 676 if (!matchingEntry) {
michael@0 677 matchingEntry = {shEntry: shEntry, childDocIdents: childDocIdents};
michael@0 678 aDocIdentMap[aEntry.docIdentifier] = matchingEntry;
michael@0 679 } else {
michael@0 680 shEntry.adoptBFCacheEntry(matchingEntry.shEntry);
michael@0 681 childDocIdents = matchingEntry.childDocIdents;
michael@0 682 }
michael@0 683 }
michael@0 684
michael@0 685 if (aEntry.owner_b64) {
michael@0 686 let ownerInput = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
michael@0 687 let binaryData = atob(aEntry.owner_b64);
michael@0 688 ownerInput.setData(binaryData, binaryData.length);
michael@0 689 let binaryStream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIObjectInputStream);
michael@0 690 binaryStream.setInputStream(ownerInput);
michael@0 691 try { // Catch possible deserialization exceptions
michael@0 692 shEntry.owner = binaryStream.readObject(true);
michael@0 693 } catch (ex) { dump(ex); }
michael@0 694 }
michael@0 695
michael@0 696 if (aEntry.children && shEntry instanceof Ci.nsISHContainer) {
michael@0 697 for (let i = 0; i < aEntry.children.length; i++) {
michael@0 698 if (!aEntry.children[i].url)
michael@0 699 continue;
michael@0 700
michael@0 701 // We're getting sessionrestore.js files with a cycle in the
michael@0 702 // doc-identifier graph, likely due to bug 698656. (That is, we have
michael@0 703 // an entry where doc identifier A is an ancestor of doc identifier B,
michael@0 704 // and another entry where doc identifier B is an ancestor of A.)
michael@0 705 //
michael@0 706 // If we were to respect these doc identifiers, we'd create a cycle in
michael@0 707 // the SHEntries themselves, which causes the docshell to loop forever
michael@0 708 // when it looks for the root SHEntry.
michael@0 709 //
michael@0 710 // So as a hack to fix this, we restrict the scope of a doc identifier
michael@0 711 // to be a node's siblings and cousins, and pass childDocIdents, not
michael@0 712 // aDocIdents, to _deserializeHistoryEntry. That is, we say that two
michael@0 713 // SHEntries with the same doc identifier have the same document iff
michael@0 714 // they have the same parent or their parents have the same document.
michael@0 715
michael@0 716 shEntry.AddChild(this._deserializeHistoryEntry(aEntry.children[i], aIdMap, childDocIdents), i);
michael@0 717 }
michael@0 718 }
michael@0 719
michael@0 720 return shEntry;
michael@0 721 },
michael@0 722
michael@0 723 _restoreHistory: function _restoreHistory(aTabData, aHistory) {
michael@0 724 if (aHistory.count > 0)
michael@0 725 aHistory.PurgeHistory(aHistory.count);
michael@0 726 aHistory.QueryInterface(Ci.nsISHistoryInternal);
michael@0 727
michael@0 728 // helper hashes for ensuring unique frame IDs and unique document
michael@0 729 // identifiers.
michael@0 730 let idMap = { used: {} };
michael@0 731 let docIdentMap = {};
michael@0 732
michael@0 733 for (let i = 0; i < aTabData.entries.length; i++) {
michael@0 734 if (!aTabData.entries[i].url)
michael@0 735 continue;
michael@0 736 aHistory.addEntry(this._deserializeHistoryEntry(aTabData.entries[i], idMap, docIdentMap), true);
michael@0 737 }
michael@0 738
michael@0 739 // We need to force set the active history item and cause it to reload since
michael@0 740 // we stop the load above
michael@0 741 let activeIndex = (aTabData.index || aTabData.entries.length) - 1;
michael@0 742 aHistory.getEntryAtIndex(activeIndex, true);
michael@0 743 aHistory.QueryInterface(Ci.nsISHistory).reloadCurrentEntry();
michael@0 744 },
michael@0 745
michael@0 746 getBrowserState: function ss_getBrowserState() {
michael@0 747 return this._getCurrentState();
michael@0 748 },
michael@0 749
michael@0 750 _restoreWindow: function ss_restoreWindow(aData) {
michael@0 751 let state;
michael@0 752 try {
michael@0 753 state = JSON.parse(aData);
michael@0 754 } catch (e) {
michael@0 755 Cu.reportError("SessionStore: invalid session JSON");
michael@0 756 return false;
michael@0 757 }
michael@0 758
michael@0 759 // To do a restore, we must have at least one window with one tab
michael@0 760 if (!state || state.windows.length == 0 || !state.windows[0].tabs || state.windows[0].tabs.length == 0) {
michael@0 761 Cu.reportError("SessionStore: no tabs to restore");
michael@0 762 return false;
michael@0 763 }
michael@0 764
michael@0 765 let window = Services.wm.getMostRecentWindow("navigator:browser");
michael@0 766
michael@0 767 let tabs = state.windows[0].tabs;
michael@0 768 let selected = state.windows[0].selected;
michael@0 769 if (selected == null || selected > tabs.length) // Clamp the selected index if it's bogus
michael@0 770 selected = 1;
michael@0 771
michael@0 772 for (let i = 0; i < tabs.length; i++) {
michael@0 773 let tabData = tabs[i];
michael@0 774 let entry = tabData.entries[tabData.index - 1];
michael@0 775
michael@0 776 // Use stubbed tab if we've already created it; otherwise, make a new tab
michael@0 777 let tab;
michael@0 778 if (tabData.tabId == null) {
michael@0 779 let params = {
michael@0 780 selected: (selected == i+1),
michael@0 781 delayLoad: true,
michael@0 782 title: entry.title,
michael@0 783 desktopMode: (tabData.desktopMode == true),
michael@0 784 isPrivate: (tabData.isPrivate == true)
michael@0 785 };
michael@0 786 tab = window.BrowserApp.addTab(entry.url, params);
michael@0 787 } else {
michael@0 788 tab = window.BrowserApp.getTabForId(tabData.tabId);
michael@0 789 delete tabData.tabId;
michael@0 790
michael@0 791 // Don't restore tab if user has closed it
michael@0 792 if (tab == null) {
michael@0 793 continue;
michael@0 794 }
michael@0 795 }
michael@0 796
michael@0 797 if (window.BrowserApp.selectedTab == tab) {
michael@0 798 this._restoreHistory(tabData, tab.browser.sessionHistory);
michael@0 799 delete tab.browser.__SS_restore;
michael@0 800 tab.browser.removeAttribute("pending");
michael@0 801 } else {
michael@0 802 // Make sure the browser has its session data for the delay reload
michael@0 803 tab.browser.__SS_data = tabData;
michael@0 804 tab.browser.__SS_restore = true;
michael@0 805 tab.browser.setAttribute("pending", "true");
michael@0 806 }
michael@0 807
michael@0 808 tab.browser.__SS_extdata = tabData.extData;
michael@0 809 }
michael@0 810
michael@0 811 return true;
michael@0 812 },
michael@0 813
michael@0 814 getClosedTabCount: function ss_getClosedTabCount(aWindow) {
michael@0 815 if (!aWindow || !aWindow.__SSID || !this._windows[aWindow.__SSID])
michael@0 816 return 0; // not a browser window, or not otherwise tracked by SS.
michael@0 817
michael@0 818 return this._windows[aWindow.__SSID].closedTabs.length;
michael@0 819 },
michael@0 820
michael@0 821 getClosedTabData: function ss_getClosedTabData(aWindow) {
michael@0 822 if (!aWindow.__SSID)
michael@0 823 throw (Components.returnCode = Cr.NS_ERROR_INVALID_ARG);
michael@0 824
michael@0 825 return JSON.stringify(this._windows[aWindow.__SSID].closedTabs);
michael@0 826 },
michael@0 827
michael@0 828 undoCloseTab: function ss_undoCloseTab(aWindow, aIndex) {
michael@0 829 if (!aWindow.__SSID)
michael@0 830 throw (Components.returnCode = Cr.NS_ERROR_INVALID_ARG);
michael@0 831
michael@0 832 let closedTabs = this._windows[aWindow.__SSID].closedTabs;
michael@0 833 if (!closedTabs)
michael@0 834 return null;
michael@0 835
michael@0 836 // default to the most-recently closed tab
michael@0 837 aIndex = aIndex || 0;
michael@0 838 if (!(aIndex in closedTabs))
michael@0 839 throw (Components.returnCode = Cr.NS_ERROR_INVALID_ARG);
michael@0 840
michael@0 841 // fetch the data of closed tab, while removing it from the array
michael@0 842 let closedTab = closedTabs.splice(aIndex, 1).shift();
michael@0 843
michael@0 844 // create a new tab and bring to front
michael@0 845 let params = { selected: true };
michael@0 846 let tab = aWindow.BrowserApp.addTab(closedTab.entries[closedTab.index - 1].url, params);
michael@0 847 this._restoreHistory(closedTab, tab.browser.sessionHistory);
michael@0 848
michael@0 849 // Put back the extra data
michael@0 850 tab.browser.__SS_extdata = closedTab.extData;
michael@0 851
michael@0 852 return tab.browser;
michael@0 853 },
michael@0 854
michael@0 855 forgetClosedTab: function ss_forgetClosedTab(aWindow, aIndex) {
michael@0 856 if (!aWindow.__SSID)
michael@0 857 throw (Components.returnCode = Cr.NS_ERROR_INVALID_ARG);
michael@0 858
michael@0 859 let closedTabs = this._windows[aWindow.__SSID].closedTabs;
michael@0 860
michael@0 861 // default to the most-recently closed tab
michael@0 862 aIndex = aIndex || 0;
michael@0 863 if (!(aIndex in closedTabs))
michael@0 864 throw (Components.returnCode = Cr.NS_ERROR_INVALID_ARG);
michael@0 865
michael@0 866 // remove closed tab from the array
michael@0 867 closedTabs.splice(aIndex, 1);
michael@0 868 },
michael@0 869
michael@0 870 getTabValue: function ss_getTabValue(aTab, aKey) {
michael@0 871 let browser = aTab.browser;
michael@0 872 let data = browser.__SS_extdata || {};
michael@0 873 return data[aKey] || "";
michael@0 874 },
michael@0 875
michael@0 876 setTabValue: function ss_setTabValue(aTab, aKey, aStringValue) {
michael@0 877 let browser = aTab.browser;
michael@0 878
michael@0 879 if (!browser.__SS_extdata)
michael@0 880 browser.__SS_extdata = {};
michael@0 881 browser.__SS_extdata[aKey] = aStringValue;
michael@0 882 this.saveStateDelayed();
michael@0 883 },
michael@0 884
michael@0 885 deleteTabValue: function ss_deleteTabValue(aTab, aKey) {
michael@0 886 let browser = aTab.browser;
michael@0 887 if (browser.__SS_extdata && browser.__SS_extdata[aKey])
michael@0 888 delete browser.__SS_extdata[aKey];
michael@0 889 else
michael@0 890 throw (Components.returnCode = Cr.NS_ERROR_INVALID_ARG);
michael@0 891 },
michael@0 892
michael@0 893 restoreLastSession: function ss_restoreLastSession(aSessionString) {
michael@0 894 let self = this;
michael@0 895
michael@0 896 function restoreWindow(data) {
michael@0 897 if (!self._restoreWindow(data)) {
michael@0 898 throw "Could not restore window";
michael@0 899 }
michael@0 900
michael@0 901 notifyObservers();
michael@0 902 }
michael@0 903
michael@0 904 function notifyObservers(aMessage) {
michael@0 905 Services.obs.notifyObservers(null, "sessionstore-windows-restored", aMessage || "");
michael@0 906 }
michael@0 907
michael@0 908 try {
michael@0 909 // Normally, we'll receive the session string from Java, but there are
michael@0 910 // cases where we may want to restore that Java cannot detect (e.g., if
michael@0 911 // browser.sessionstore.resume_session_once is true). In these cases, the
michael@0 912 // session will be read from sessionstore.bak (which is also used for
michael@0 913 // "tabs from last time").
michael@0 914 if (aSessionString == null) {
michael@0 915 Task.spawn(function() {
michael@0 916 let bytes = yield OS.File.read(this._sessionFileBackup.path);
michael@0 917 let data = JSON.parse(new TextDecoder().decode(bytes) || "");
michael@0 918 restoreWindow(data);
michael@0 919 }.bind(this)).then(null, function onError(reason) {
michael@0 920 if (reason instanceof OS.File.Error && reason.becauseNoSuchFile) {
michael@0 921 Cu.reportError("Session file doesn't exist");
michael@0 922 } else {
michael@0 923 Cu.reportError("SessionStore: " + reason.message);
michael@0 924 }
michael@0 925 notifyObservers("fail");
michael@0 926 });
michael@0 927 } else {
michael@0 928 restoreWindow(aSessionString);
michael@0 929 }
michael@0 930 } catch (e) {
michael@0 931 Cu.reportError("SessionStore: " + e);
michael@0 932 notifyObservers("fail");
michael@0 933 }
michael@0 934 }
michael@0 935 };
michael@0 936
michael@0 937 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SessionStore]);

mercurial