Bug 1472491: Part 5d - Add ContentSearchChild actor. r?felipe draft
authorKris Maglione <maglione.k@gmail.com>
Sun, 29 Jul 2018 20:00:11 -0700
changeset 825675 ad9552612de7de8106292cae20c1082bb380b00e
parent 825674 090b53ed299d4aef031407327319967ac7de2b62
child 825676 d960f91939f2ee86734b059f78816d03db8fda1b
push id118150
push usermaglione.k@gmail.com
push dateThu, 02 Aug 2018 04:47:08 +0000
reviewersfelipe
bugs1472491
milestone63.0a1
Bug 1472491: Part 5d - Add ContentSearchChild actor. r?felipe MozReview-Commit-ID: 5u7VpedF3xH
browser/base/content/tab-content.js
browser/base/content/test/general/browser_contentSearchUI.js
browser/components/nsBrowserGlue.js
browser/modules/ContentSearchChild.jsm
browser/modules/moz.build
browser/modules/test/browser/browser_ContentSearch.js
--- a/browser/base/content/tab-content.js
+++ b/browser/base/content/tab-content.js
@@ -53,74 +53,16 @@ let themeablePagesWhitelist = new Set([
 
 addEventListener("pageshow", function({ originalTarget }) {
   if (originalTarget.defaultView == content && themeablePagesWhitelist.has(content.document.documentURI)) {
     LightweightThemeChildHelper.listen(themeablePagesWhitelist);
     LightweightThemeChildHelper.update(chromeOuterWindowID, content);
   }
 }, false, true);
 
-
-var ContentSearchMediator = {
-
-  whitelist: new Set([
-    "about:home",
-    "about:newtab",
-    "about:welcome",
-  ]),
-
-  init(chromeGlobal) {
-    chromeGlobal.addEventListener("ContentSearchClient", this, true, true);
-    addMessageListener("ContentSearch", this);
-    this.init = null;
-  },
-
-  handleEvent(event) {
-    if (this._contentWhitelisted) {
-      this._sendMsg(event.detail.type, event.detail.data);
-    }
-  },
-
-  receiveMessage(msg) {
-    if (msg.data.type == "AddToWhitelist") {
-      for (let uri of msg.data.data) {
-        this.whitelist.add(uri);
-      }
-      this._sendMsg("AddToWhitelistAck");
-      return;
-    }
-    if (this._contentWhitelisted) {
-      this._fireEvent(msg.data.type, msg.data.data);
-    }
-  },
-
-  get _contentWhitelisted() {
-    return this.whitelist.has(content.document.documentURI);
-  },
-
-  _sendMsg(type, data = null) {
-    sendAsyncMessage("ContentSearch", {
-      type,
-      data,
-    });
-  },
-
-  _fireEvent(type, data = null) {
-    let event = Cu.cloneInto({
-      detail: {
-        type,
-        data,
-      },
-    }, content);
-    content.dispatchEvent(new content.CustomEvent("ContentSearchService",
-                                                  event));
-  },
-};
-ContentSearchMediator.init(this);
-
 // Keep a reference to the translation content handler to avoid it it being GC'ed.
 var trHandler = null;
 if (Services.prefs.getBoolPref("browser.translation.detectLanguage")) {
   ChromeUtils.import("resource:///modules/translation/TranslationContentHandler.jsm");
   trHandler = new TranslationContentHandler(global, docShell);
 }
 
 function gKeywordURIFixup(fixupInfo) {
--- a/browser/base/content/test/general/browser_contentSearchUI.js
+++ b/browser/base/content/test/general/browser_contentSearchUI.js
@@ -707,25 +707,20 @@ var gMsgMan;
 async function promiseTab() {
   let deferred = PromiseUtils.defer();
   let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
   registerCleanupFunction(() => BrowserTestUtils.removeTab(tab));
   let pageURL = getRootDirectory(gTestPath) + TEST_PAGE_BASENAME;
   tab.linkedBrowser.addEventListener("load", function onLoad(event) {
     tab.linkedBrowser.removeEventListener("load", onLoad, true);
     gMsgMan = tab.linkedBrowser.messageManager;
-    gMsgMan.sendAsyncMessage("ContentSearch", {
-      type: "AddToWhitelist",
-      data: [pageURL],
-    });
-    promiseMsg("ContentSearch", "AddToWhitelistAck", gMsgMan).then(() => {
-      let jsURL = getRootDirectory(gTestPath) + TEST_CONTENT_SCRIPT_BASENAME;
-      gMsgMan.loadFrameScript(jsURL, false);
-      deferred.resolve(msg("init"));
-    });
+
+    let jsURL = getRootDirectory(gTestPath) + TEST_CONTENT_SCRIPT_BASENAME;
+    gMsgMan.loadFrameScript(jsURL, false);
+    deferred.resolve(msg("init"));
   }, true, true);
   openTrustedLinkIn(pageURL, "current");
   return deferred.promise;
 }
 
 function promiseMsg(name, type, msgMan) {
   return new Promise(resolve => {
     info("Waiting for " + name + " message " + type + "...");
--- a/browser/components/nsBrowserGlue.js
+++ b/browser/components/nsBrowserGlue.js
@@ -48,16 +48,31 @@ let ACTORS = {
         "Browser:Reload",
         "MixedContent:ReenableProtection",
         "SwitchDocumentDirection",
         "UpdateCharacterSet",
       ],
     },
   },
 
+  ContentSearch: {
+    module: "resource:///modules/ContentSearch",
+    child: {
+      group: "browsers",
+      matches: ["about:home", "about:newtab", "about:welcome",
+                "chrome://mochitests/content/*"],
+      events: {
+        "ContentSearchClient": {capture: true, wantUntrusted: true},
+      },
+      messages: [
+        "ContentSearch",
+      ]
+    },
+  },
+
   PageStyle: {
     module: "resource:///modules/PageStyle",
     child: {
       group: "browsers",
       events: {
         "pageshow": {},
       },
       messages: [
new file mode 100644
--- /dev/null
+++ b/browser/modules/ContentSearchChild.jsm
@@ -0,0 +1,37 @@
+/* vim: set ts=2 sw=2 sts=2 et tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+var EXPORTED_SYMBOLS = ["ContentSearchChild"];
+
+ChromeUtils.import("resource://gre/modules/ActorChild.jsm");
+
+class ContentSearchChild extends ActorChild {
+  handleEvent(event) {
+    this._sendMsg(event.detail.type, event.detail.data);
+  }
+
+  receiveMessage(msg) {
+    this._fireEvent(msg.data.type, msg.data.data);
+  }
+
+  _sendMsg(type, data = null) {
+    this.mm.sendAsyncMessage("ContentSearch", {
+      type,
+      data,
+    });
+  }
+
+  _fireEvent(type, data = null) {
+    let event = Cu.cloneInto({
+      detail: {
+        type,
+        data,
+      },
+    }, this.content);
+    this.content.dispatchEvent(new this.content.CustomEvent("ContentSearchService",
+                                                            event));
+  }
+}
--- a/browser/modules/moz.build
+++ b/browser/modules/moz.build
@@ -142,16 +142,17 @@ EXTRA_JS_MODULES += [
     'BrowserWindowTracker.jsm',
     'ClickEventHandler.jsm',
     'ContentClick.jsm',
     'ContentCrashHandlers.jsm',
     'ContentLinkHandler.jsm',
     'ContentMetaHandler.jsm',
     'ContentObservers.js',
     'ContentSearch.jsm',
+    'ContentSearchChild.jsm',
     'ContentWebRTC.jsm',
     'ContextMenu.jsm',
     'ExtensionsUI.jsm',
     'Feeds.jsm',
     'FormSubmitObserver.jsm',
     'FormValidationHandler.jsm',
     'HomePage.jsm',
     'LaterRun.jsm',
--- a/browser/modules/test/browser/browser_ContentSearch.js
+++ b/browser/modules/test/browser/browser_ContentSearch.js
@@ -10,16 +10,20 @@ var gMsgMan;
 /* import-globals-from ../../../components/search/test/head.js */
 Services.scriptloader.loadSubScript(
   "chrome://mochitests/content/browser/browser/components/search/test/head.js",
   this);
 
 let originalEngine = Services.search.currentEngine;
 
 add_task(async function setup() {
+  await SpecialPowers.pushPrefEnv({
+    set: [["browser.newtab.preload", false]],
+  });
+
   await promiseNewEngine("testEngine.xml", {
     setAsCurrent: true,
     testPath: "chrome://mochitests/content/browser/browser/components/search/test/",
   });
 
   registerCleanupFunction(() => {
     Services.search.currentEngine = originalEngine;
   });
@@ -341,28 +345,21 @@ function waitForNewEngine(basename, numI
       addDeferred.reject();
     },
   });
 
   return Promise.all([addDeferred.promise].concat(eventPromises));
 }
 
 async function addTab() {
-  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
+  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:newtab");
   registerCleanupFunction(() => gBrowser.removeTab(tab));
 
   let url = getRootDirectory(gTestPath) + TEST_CONTENT_SCRIPT_BASENAME;
   gMsgMan = tab.linkedBrowser.messageManager;
-  gMsgMan.sendAsyncMessage(CONTENT_SEARCH_MSG, {
-    type: "AddToWhitelist",
-    data: ["about:blank"],
-  });
-
-  await waitForMsg(CONTENT_SEARCH_MSG, "AddToWhitelistAck");
-
   gMsgMan.loadFrameScript(url, false);
 }
 
 var currentStateObj = async function() {
   let state = {
     engines: [],
     currentEngine: await currentEngineObj(),
   };