Bug 1383299 - Ensure we start to set up network connection before content process requests. r=mconley draft
authorEvelyn Hung <jj.evelyn@gmail.com>
Tue, 22 Aug 2017 12:00:31 +0800
changeset 650263 1b911c736d549f1ebc64d966ebd52f88f79e5ea6
parent 650228 128a79130ecd6f277190d031a623f991c73c5272
child 650264 0a7870ce989b679f8aa6a4f6eb5e2b25d6289044
push id75319
push userbmo:ehung@mozilla.com
push dateTue, 22 Aug 2017 04:08:53 +0000
reviewersmconley
bugs1383299
milestone57.0a1
Bug 1383299 - Ensure we start to set up network connection before content process requests. r=mconley In chrome process, we often know which url is going to be loaded. As a performance optimization, we can start initiating network connection before sending out the 'LoadURI' message to the content process. MozReview-Commit-ID: L79ylHOaxX8
toolkit/components/remotebrowserutils/RemoteWebNavigation.js
--- a/toolkit/components/remotebrowserutils/RemoteWebNavigation.js
+++ b/toolkit/components/remotebrowserutils/RemoteWebNavigation.js
@@ -8,16 +8,18 @@ const { interfaces: Ci, classes: Cc, uti
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "Services",
   "resource://gre/modules/Services.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
   "resource://gre/modules/NetUtil.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "Utils",
   "resource://gre/modules/sessionstore/Utils.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
+  "resource://gre/modules/PrivateBrowsingUtils.jsm");
 
 function makeURI(url) {
   return Services.io.newURI(url);
 }
 
 function RemoteWebNavigation() {
   this.wrappedJSObject = this;
 }
@@ -67,16 +69,39 @@ RemoteWebNavigation.prototype = {
   },
   loadURI(aURI, aLoadFlags, aReferrer, aPostData, aHeaders) {
     this.loadURIWithOptions(aURI, aLoadFlags, aReferrer,
                             Ci.nsIHttpChannel.REFERRER_POLICY_UNSET,
                             aPostData, aHeaders, null);
   },
   loadURIWithOptions(aURI, aLoadFlags, aReferrer, aReferrerPolicy,
                      aPostData, aHeaders, aBaseURI, aTriggeringPrincipal) {
+    // We know the url is going to be loaded, let's start requesting network
+    // connection before the content process asks.
+    // Note that we might have already setup the speculative connection in some
+    // cases, especially when the url is from location bar or its popup menu.
+    if (aURI.startsWith("http")) {
+      let uri = makeURI(aURI);
+      let principal = aTriggeringPrincipal;
+      // We usually have a aTriggeringPrincipal assigned, but in case we don't
+      // have one, create it with OA inferred from the current context.
+      if (!principal) {
+        let attrs = {
+          userContextId: this._browser.getAttribute("usercontextid") || 0,
+          privateBrowsingId: PrivateBrowsingUtils.isBrowserPrivate(this._browser) ? 1 : 0
+        };
+        principal = Services.scriptSecurityManager.createCodebasePrincipal(uri, attrs);
+      }
+      try {
+        Services.io.speculativeConnect2(uri, principal, null);
+      } catch (ex) {
+        // Can't setup speculative connection for this uri string for some
+        // reason, just ignore it.
+      }
+    }
     this._sendMessage("WebNavigation:LoadURI", {
       uri: aURI,
       flags: aLoadFlags,
       referrer: aReferrer ? aReferrer.spec : null,
       referrerPolicy: aReferrerPolicy,
       postData: aPostData ? Utils.serializeInputStream(aPostData) : null,
       headers: aHeaders ? Utils.serializeInputStream(aHeaders) : null,
       baseURI: aBaseURI ? aBaseURI.spec : null,