From 1914bb683143373132e9a5b62c47b7197803c292 Mon Sep 17 00:00:00 2001 From: Beekill95 Date: Fri, 23 Jun 2017 10:27:32 +0700 Subject: [PATCH] Bug 874533 - Part 1: Speculative connect when users hover mouse on a tab or when restoring tabs automatically. r=mikedeboer MozReview-Commit-ID: aJUyM4z3Nh --HG-- extra : rebase_source : 028e8c05ff1dc6285d9391525e6bee60919cbfe2 --- .../components/sessionstore/SessionStore.jsm | 60 ++++++++++++++++++- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/browser/components/sessionstore/SessionStore.jsm b/browser/components/sessionstore/SessionStore.jsm index 2d0314783b7e..6ce15f877e60 100644 --- a/browser/components/sessionstore/SessionStore.jsm +++ b/browser/components/sessionstore/SessionStore.jsm @@ -3279,8 +3279,8 @@ var SessionStoreInternal = { } } - let restoreTabsLazily = this._prefBranch.getBoolPref("sessionstore.restore_tabs_lazily") && - this._prefBranch.getBoolPref("sessionstore.restore_on_demand"); + let restoreOnDemand = this._prefBranch.getBoolPref("sessionstore.restore_on_demand"); + let restoreTabsLazily = this._prefBranch.getBoolPref("sessionstore.restore_tabs_lazily") && restoreOnDemand; for (var t = 0; t < newTabCount; t++) { let tabData = winData.tabs[t]; @@ -3333,6 +3333,13 @@ var SessionStoreInternal = { tabbrowser.selectedTab = tab; tabbrowser.removeTab(leftoverTab); } + + // Prepare connection to the host when users hover mouse over this + // tab. If we're not restoring on demand, we'll prepare connection + // when we're restoring next tab. + if (!tabData.pinned && restoreOnDemand) { + this.speculativeConnectOnTabHover(tab, url); + } } tabs.push(tab); @@ -3417,6 +3424,42 @@ var SessionStoreInternal = { this._sendRestoreCompletedNotifications(); }, + /** + * Prepare connection to host beforehand. + * + * @param url + * URL of a host. + * @returns a flag indicates whether a connection has been made + */ + prepareConnectionToHost(url) { + if (!url.startsWith("about:")) { + let sc = Services.io.QueryInterface(Ci.nsISpeculativeConnect); + let uri = Services.io.newURI(url); + sc.speculativeConnect(uri, null, null); + return true; + } + return false; + }, + + /** + * Make a connection to a host when users hover mouse on a tab. + * + * @param tab + * A tab to set up a hover listener. + * @param url + * URL of a host. + */ + speculativeConnectOnTabHover(tab, url) { + tab.addEventListener("mouseover", () => { + let prepared = this.prepareConnectionToHost(url); + // This is used to test if a connection has been made beforehand. + if (gDebuggingEnabled) { + tab.__test_connection_prepared = prepared; + tab.__test_connection_url = url; + } + }, {once: true}); + }, + /** * Restore multiple windows using the provided state. * @param aWindow @@ -3683,6 +3726,19 @@ var SessionStoreInternal = { this.restoreTabContent(tab, options); } else if (!forceOnDemand) { TabRestoreQueue.add(tab); + // Check if a tab is in queue and will be restored + // after the currently loading tabs. If so, prepare + // a connection to host to speed up page loading. + if (TabRestoreQueue.willRestoreSoon(tab)) { + if (activeIndex in tabData.entries) { + let url = tabData.entries[activeIndex].url; + let prepared = this.prepareConnectionToHost(url); + if (gDebuggingEnabled) { + tab.__test_connection_prepared = prepared; + tab.__test_connection_url = url; + } + } + } this.restoreNextTab(); } } else {