Bug 1312954 - Part 9: Updating speculativeConnect to speculativeConnect2 for the search service. r?Gijs draft
authorTim Huang <tihuang@mozilla.com>
Thu, 19 Jan 2017 16:03:15 +0800
changeset 466003 f1229abab447d11347b99f5ace8deca66eb6ebf1
parent 466002 30516e772f3cf5d9fc0258cf7f9de204443dbb92
child 466004 56dffc33d182c507e989dad9398ae813d4153e2e
push id42767
push userbmo:tihuang@mozilla.com
push dateWed, 25 Jan 2017 04:18:41 +0000
reviewersGijs
bugs1312954
milestone54.0a1
Bug 1312954 - Part 9: Updating speculativeConnect to speculativeConnect2 for the search service. r?Gijs
browser/components/search/content/search.xml
browser/modules/ContentSearch.jsm
mobile/android/chrome/content/browser.js
netwerk/base/nsIBrowserSearchService.idl
toolkit/components/search/nsSearchService.js
--- a/browser/components/search/content/search.xml
+++ b/browser/components/search/content/search.xml
@@ -490,17 +490,19 @@
         // received focus, ignore the next focus event.
         this._ignoreFocus = (document.activeElement == this._textbox.inputField);
       ]]></handler>
 
       <handler event="focus">
       <![CDATA[
         // Speculatively connect to the current engine's search URI (and
         // suggest URI, if different) to reduce request latency
-        this.currentEngine.speculativeConnect({window});
+        this.currentEngine.speculativeConnect({window,
+                                               originAttributes: gBrowser.contentPrincipal
+                                                                         .originAttributes});
 
         if (this._ignoreFocus) {
           // This window has been re-focused, don't show the suggestions
           this._ignoreFocus = false;
           return;
         }
 
         // Don't open the suggestions if there is no text in the textbox.
--- a/browser/modules/ContentSearch.jsm
+++ b/browser/modules/ContentSearch.jsm
@@ -450,16 +450,17 @@ this.ContentSearch = {
   _onMessageSpeculativeConnect(msg, engineName) {
     let engine = Services.search.getEngineByName(engineName);
     if (!engine) {
       throw new Error("Unknown engine name: " + engineName);
     }
     if (msg.target.contentWindow) {
       engine.speculativeConnect({
         window: msg.target.contentWindow,
+        originAttributes: msg.target.contentPrincipal.originAttributes
       });
     }
   },
 
   _onObserve: Task.async(function* (data) {
     if (data === "engine-current") {
       let engine = yield this._currentEngineObj();
       this._broadcast("CurrentEngine", engine);
--- a/mobile/android/chrome/content/browser.js
+++ b/mobile/android/chrome/content/browser.js
@@ -6099,17 +6099,18 @@ var SearchEngines = {
         engine: suggestEngine,
         template: suggestTemplate,
         enabled: Services.prefs.getBoolPref(this.PREF_SUGGEST_ENABLED),
         prompted: Services.prefs.getBoolPref(this.PREF_SUGGEST_PROMPTED)
       }
     });
 
     // Send a speculative connection to the default engine.
-    Services.search.defaultEngine.speculativeConnect({window: window});
+    Services.search.defaultEngine.speculativeConnect({ window: window,
+                                                       originAttributes: {} });
   },
 
   // Helper method to extract the engine name from a JSON. Simplifies the observe function.
   _extractEngineFromJSON: function _extractEngineFromJSON(aData) {
     let data = JSON.parse(aData);
     return Services.search.getEngineByName(data.engine);
   },
 
--- a/netwerk/base/nsIBrowserSearchService.idl
+++ b/netwerk/base/nsIBrowserSearchService.idl
@@ -104,16 +104,17 @@ interface nsISearchEngine : nsISupports
 
   /**
    * Opens a speculative connection to the engine's search URI
    * (and suggest URI, if different) to reduce request latency
    *
    * @param  options
    *         An object that must contain the following fields:
    *         {window} the content window for the window performing the search
+   *         {originAttributes} the originAttributes for performing the search
    *
    * @throws NS_ERROR_INVALID_ARG if options is omitted or lacks required
    *         elemeents
    */
   void speculativeConnect(in jsval options);
 
   /**
    * An optional shortcut alias for the engine.
--- a/toolkit/components/search/nsSearchService.js
+++ b/toolkit/components/search/nsSearchService.js
@@ -2553,16 +2553,17 @@ Engine.prototype = {
 
   /**
    * Opens a speculative connection to the engine's search URI
    * (and suggest URI, if different) to reduce request latency
    *
    * @param  options
    *         An object that must contain the following fields:
    *         {window} the content window for the window performing the search
+   *         {originAttributes} the originAttributes for performing the search
    *
    * @throws NS_ERROR_INVALID_ARG if options is omitted or lacks required
    *         elemeents
    */
   speculativeConnect: function SRCH_ENG_speculativeConnect(options) {
     if (!options || !options.window) {
       Cu.reportError("invalid options arg passed to nsISearchEngine.speculativeConnect");
       throw Cr.NS_ERROR_INVALID_ARG;
@@ -2571,22 +2572,36 @@ Engine.prototype = {
         Services.io.QueryInterface(Components.interfaces.nsISpeculativeConnect);
 
     let searchURI = this.getSubmission("dummy").uri;
 
     let callbacks = options.window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                            .getInterface(Components.interfaces.nsIWebNavigation)
                            .QueryInterface(Components.interfaces.nsILoadContext);
 
-    connector.speculativeConnect(searchURI, callbacks);
+    // Using the codebase principal which is constructed by the search URI
+    // and given originAttributes. If originAttributes are not given, we
+    // fallback to use the docShell's originAttributes.
+    let attrs = options.originAttributes;
+
+    if (!attrs) {
+      attrs = options.window.document
+                            .docShell
+                            .getOriginAttributes();
+    }
+
+    let principal = Services.scriptSecurityManager
+                            .createCodebasePrincipal(searchURI, attrs);
+
+    connector.speculativeConnect2(searchURI, principal, callbacks);
 
     if (this.supportsResponseType(URLTYPE_SUGGEST_JSON)) {
       let suggestURI = this.getSubmission("dummy", URLTYPE_SUGGEST_JSON).uri;
       if (suggestURI.prePath != searchURI.prePath)
-        connector.speculativeConnect(suggestURI, callbacks);
+        connector.speculativeConnect2(suggestURI, principal, callbacks);
     }
   },
 };
 
 // nsISearchSubmission
 function Submission(aURI, aPostData = null) {
   this._uri = aURI;
   this._postData = aPostData;