Bug 1472491: Part 5o - Add FindBarChild actor. r=gijs draft
authorKris Maglione <maglione.k@gmail.com>
Sun, 29 Jul 2018 21:38:21 -0700
changeset 828450 4db52dd9d2b219a12b45eb974ae5bb25e1d7b6f1
parent 828449 c8e8dfb04bac2360f2832dc6aa7957acfe731f84
child 828451 c002d60bdd3d3665197ea38e1311f69740e3123b
push id118680
push usermaglione.k@gmail.com
push dateFri, 10 Aug 2018 23:04:22 +0000
reviewersgijs
bugs1472491
milestone63.0a1
Bug 1472491: Part 5o - Add FindBarChild actor. r=gijs MozReview-Commit-ID: IGNjeh8o91l
toolkit/actors/FindBarChild.jsm
toolkit/actors/moz.build
toolkit/content/browser-content.js
toolkit/modules/ActorManagerParent.jsm
toolkit/modules/FindBarChild.jsm
toolkit/modules/FindBarContent.jsm
toolkit/modules/moz.build
rename from toolkit/modules/FindBarChild.jsm
rename to toolkit/actors/FindBarChild.jsm
--- a/toolkit/modules/FindBarChild.jsm
+++ b/toolkit/actors/FindBarChild.jsm
@@ -1,99 +1,85 @@
-// vim: set ts=2 sw=2 sts=2 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/.
+/* 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 = ["FindBarChild"];
 
+ChromeUtils.import("resource://gre/modules/ActorChild.jsm");
+ChromeUtils.import("resource://gre/modules/Services.jsm");
+ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
+
+ChromeUtils.defineModuleGetter(this, "BrowserUtils",
+                               "resource://gre/modules/BrowserUtils.jsm");
 ChromeUtils.defineModuleGetter(this, "RemoteFinder",
                                "resource://gre/modules/RemoteFinder.jsm");
-ChromeUtils.defineModuleGetter(this, "Services",
-                               "resource://gre/modules/Services.jsm");
 
-/* Please keep in sync with toolkit/this.mm.content/widgets/findbar.xml */
-const FIND_NORMAL = 0;
-const FIND_TYPEAHEAD = 1;
-const FIND_LINKS = 2;
+class FindBarChild extends ActorChild {
+  constructor(mm) {
+    super(mm);
+    this._findKey = null;
 
-class FindBarChild {
-  constructor(mm) {
-    this.mm = mm;
-
-    this.findMode = 0;
-    this.inQuickFind = false;
-
-    this.mm.addMessageListener("Findbar:UpdateState", this);
-
-    Services.els.addSystemEventListener(this.mm, "mouseup", this, false);
+    XPCOMUtils.defineLazyProxy(this, "FindBarContent", () => {
+      let tmp = {};
+      ChromeUtils.import("resource://gre/modules/FindBarContent.jsm", tmp);
+      return new tmp.FindBarContent(this.mm);
+    }, {inQuickFind: false, inPassThrough: false});
   }
 
-  start(event) {
-    this.inPassThrough = true;
-  }
-
-  startQuickFind(event, autostart = false) {
-    let mode = FIND_TYPEAHEAD;
-    if (event.charCode == "'".charAt(0) ||
-        autostart && RemoteFinder._typeAheadLinksOnly) {
-      mode = FIND_LINKS;
+  /**
+   * Check whether this key event will start the findbar in the parent,
+   * in which case we should pass any further key events to the parent to avoid
+   * them being lost.
+   * @param aEvent the key event to check.
+   */
+  eventMatchesFindShortcut(aEvent) {
+    if (!this._findKey) {
+      this._findKey = Services.cpmm.sharedData.get("Findbar:Shortcut");
+      if (!this._findKey) {
+        return false;
+      }
     }
-
-    // Set findMode immediately (without waiting for child->parent->child roundtrip)
-    // to ensure we pass any further keypresses, too.
-    this.findMode = mode;
-    this.passKeyToParent(event);
-  }
-
-  receiveMessage(msg) {
-    switch (msg.name) {
-      case "Findbar:UpdateState":
-        this.findMode = msg.data.findMode;
-        this.inQuickFind = msg.data.hasQuickFindTimeout;
-        if (msg.data.isOpenAndFocused) {
-          this.inPassThrough = false;
-        }
-        break;
+    for (let k in this._findKey) {
+      if (this._findKey[k] != aEvent[k]) {
+        return false;
+      }
     }
+    return true;
   }
 
   handleEvent(event) {
-    switch (event.type) {
-      case "keypress":
-        this.onKeypress(event);
-        break;
-      case "mouseup":
-        this.onMouseup(event);
-        break;
+    if (event.type == "keypress") {
+      this.onKeypress(event);
     }
   }
 
   onKeypress(event) {
-    if (this.inPassThrough) {
-      this.passKeyToParent(event);
-    } else if (this.findMode != FIND_NORMAL && this.inQuickFind && event.charCode) {
-      this.passKeyToParent(event);
+    let {FindBarContent} = this;
+
+    if (!FindBarContent.inPassThrough &&
+        this.eventMatchesFindShortcut(event)) {
+      return FindBarContent.start(event);
     }
-  }
+
+    if (event.ctrlKey || event.altKey || event.metaKey || event.defaultPrevented ||
+        !BrowserUtils.canFastFind(this.content)) {
+      return null;
+    }
 
-  passKeyToParent(event) {
-    event.preventDefault();
-    // These are the properties required to dispatch another 'real' event
-    // to the findbar in the parent in _dispatchKeypressEvent in findbar.xml .
-    // If you make changes here, verify that that method can still do its job.
-    const kRequiredProps = [
-      "type", "bubbles", "cancelable", "ctrlKey", "altKey", "shiftKey",
-      "metaKey", "keyCode", "charCode",
-    ];
-    let fakeEvent = {};
-    for (let prop of kRequiredProps) {
-      fakeEvent[prop] = event[prop];
+    if (FindBarContent.inPassThrough || FindBarContent.inQuickFind) {
+      return FindBarContent.onKeypress(event);
     }
-    this.mm.sendAsyncMessage("Findbar:Keypress", fakeEvent);
-  }
 
-  onMouseup(event) {
-    if (this.findMode != FIND_NORMAL)
-      this.mm.sendAsyncMessage("Findbar:Mouseup");
+    if (event.charCode && BrowserUtils.shouldFastFind(event.target)) {
+      let key = String.fromCharCode(event.charCode);
+      if ((key == "/" || key == "'") && RemoteFinder._manualFAYT) {
+        return FindBarContent.startQuickFind(event);
+      }
+      if (key != " " && RemoteFinder._findAsYouType) {
+        return FindBarContent.startQuickFind(event, true);
+      }
+    }
+    return null;
   }
 }
--- a/toolkit/actors/moz.build
+++ b/toolkit/actors/moz.build
@@ -1,9 +1,10 @@
 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # 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/.
 
 FINAL_TARGET_FILES.actors += [
     'AudioPlaybackChild.jsm',
+    'FindBarChild.jsm',
 ]
--- a/toolkit/content/browser-content.js
+++ b/toolkit/content/browser-content.js
@@ -12,26 +12,22 @@ ChromeUtils.import("resource://gre/modul
 ChromeUtils.import("resource://gre/modules/ActorManagerChild.jsm");
 
 ActorManagerChild.attach(this);
 
 ChromeUtils.defineModuleGetter(this, "AutoCompletePopup",
   "resource://gre/modules/AutoCompletePopupContent.jsm");
 ChromeUtils.defineModuleGetter(this, "AutoScrollController",
   "resource://gre/modules/AutoScrollController.jsm");
-ChromeUtils.defineModuleGetter(this, "BrowserUtils",
-  "resource://gre/modules/BrowserUtils.jsm");
 ChromeUtils.defineModuleGetter(this, "SelectContentHelper",
   "resource://gre/modules/SelectContentHelper.jsm");
 ChromeUtils.defineModuleGetter(this, "FindContent",
   "resource://gre/modules/FindContent.jsm");
 ChromeUtils.defineModuleGetter(this, "PrintingContent",
   "resource://gre/modules/PrintingContent.jsm");
-ChromeUtils.defineModuleGetter(this, "RemoteFinder",
-  "resource://gre/modules/RemoteFinder.jsm");
 
 XPCOMUtils.defineLazyServiceGetter(this, "formFill",
                                    "@mozilla.org/satchel/form-fill-controller;1",
                                    "nsIFormFillController");
 
 var global = this;
 
 XPCOMUtils.defineLazyProxy(this, "PopupBlocking", () => {
@@ -59,23 +55,16 @@ XPCOMUtils.defineLazyProxy(this, "WebCha
   "resource://gre/modules/WebChannelContent.jsm");
 
 XPCOMUtils.defineLazyProxy(this, "DateTimePickerContent", () => {
   let tmp = {};
   ChromeUtils.import("resource://gre/modules/DateTimePickerContent.jsm", tmp);
   return new tmp.DateTimePickerContent(this);
 });
 
-XPCOMUtils.defineLazyProxy(this, "FindBarChild", () => {
-  let tmp = {};
-  ChromeUtils.import("resource://gre/modules/FindBarChild.jsm", tmp);
-  return new tmp.FindBarChild(this);
-}, {inQuickFind: false, inPassThrough: false});
-
-
 // Lazily load the finder code
 addMessageListener("Finder:Initialize", function() {
   let {RemoteFinderListener} = ChromeUtils.import("resource://gre/modules/RemoteFinder.jsm", {});
   new RemoteFinderListener(global);
 });
 
 var AutoScrollListener = {
   handleEvent(event) {
@@ -116,81 +105,16 @@ var Printing = {
   },
 
   receiveMessage(message) {
     return PrintingContent.receiveMessage(global, message);
   },
 };
 Printing.init();
 
-var FindBar = {
-  /**
-   * _findKey and _findModifiers are used to determine whether a keypress
-   * is a user attempting to use the find shortcut, after which we'll
-   * route keypresses to the parent until we know the findbar has focus
-   * there. To do this, we need shortcut data from the parent.
-   */
-  _findKey: null,
-
-  init() {
-    Services.els.addSystemEventListener(global, "keypress",
-                                        this.onKeypress.bind(this), false);
-    this.init = null;
-  },
-
-  /**
-   * Check whether this key event will start the findbar in the parent,
-   * in which case we should pass any further key events to the parent to avoid
-   * them being lost.
-   * @param aEvent the key event to check.
-   */
-  eventMatchesFindShortcut(aEvent) {
-    if (!this._findKey) {
-      this._findKey = Services.cpmm.sharedData.get("Findbar:Shortcut");
-      if (!this._findKey) {
-          return false;
-      }
-    }
-    for (let k in this._findKey) {
-      if (this._findKey[k] != aEvent[k]) {
-        return false;
-      }
-    }
-    return true;
-  },
-
-  onKeypress(event) {
-    if (!FindBarChild.inPassThrough &&
-        this.eventMatchesFindShortcut(event)) {
-      return FindBarChild.start(event);
-    }
-
-    if (event.ctrlKey || event.altKey || event.metaKey || event.defaultPrevented ||
-        !BrowserUtils.canFastFind(content)) {
-      return null;
-    }
-
-    if (FindBarChild.inPassThrough || FindBarChild.inQuickFind) {
-      return FindBarChild.onKeypress(event);
-    }
-
-    if (event.charCode && BrowserUtils.shouldFastFind(event.target)) {
-      let key = String.fromCharCode(event.charCode);
-      if ((key == "/" || key == "'") && RemoteFinder._manualFAYT) {
-        return FindBarChild.startQuickFind(event);
-      }
-      if (key != " " && RemoteFinder._findAsYouType) {
-        return FindBarChild.startQuickFind(event, true);
-      }
-    }
-    return null;
-  },
-};
-FindBar.init();
-
 addEventListener("WebChannelMessageToChrome", WebChannelContent,
                  true, true);
 addMessageListener("WebChannelMessageToContent", WebChannelContent);
 
 var UnselectedTabHoverObserver = {
   init() {
     addMessageListener("Browser:UnselectedTabHover", this);
     addEventListener("UnselectedTabHover:Enable", this);
--- a/toolkit/modules/ActorManagerParent.jsm
+++ b/toolkit/modules/ActorManagerParent.jsm
@@ -105,16 +105,25 @@ let ACTORS = {
       messages: [
         "AudioPlayback",
       ],
       observers: [
         "audio-playback",
       ],
     },
   },
+
+  FindBar: {
+    child: {
+      module: "resource://gre/actors/FindBarChild.jsm",
+      events: {
+        "keypress": {mozSystemGroup: true},
+      },
+    },
+  },
 };
 
 class ActorSet {
   constructor(group, actorSide) {
     this.group = group;
     this.actorSide = actorSide;
 
     this.actors = new Map();
copy from toolkit/modules/FindBarChild.jsm
copy to toolkit/modules/FindBarContent.jsm
--- a/toolkit/modules/FindBarChild.jsm
+++ b/toolkit/modules/FindBarContent.jsm
@@ -1,27 +1,27 @@
 // vim: set ts=2 sw=2 sts=2 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 = ["FindBarChild"];
+var EXPORTED_SYMBOLS = ["FindBarContent"];
 
 ChromeUtils.defineModuleGetter(this, "RemoteFinder",
                                "resource://gre/modules/RemoteFinder.jsm");
 ChromeUtils.defineModuleGetter(this, "Services",
                                "resource://gre/modules/Services.jsm");
 
 /* Please keep in sync with toolkit/this.mm.content/widgets/findbar.xml */
 const FIND_NORMAL = 0;
 const FIND_TYPEAHEAD = 1;
 const FIND_LINKS = 2;
 
-class FindBarChild {
+class FindBarContent {
   constructor(mm) {
     this.mm = mm;
 
     this.findMode = 0;
     this.inQuickFind = false;
 
     this.mm.addMessageListener("Findbar:UpdateState", this);
 
--- a/toolkit/modules/moz.build
+++ b/toolkit/modules/moz.build
@@ -198,17 +198,17 @@ EXTRA_JS_MODULES += [
     'css-selector.js',
     'DateTimePickerContent.jsm',
     'DateTimePickerParent.jsm',
     'DeferredTask.jsm',
     'Deprecated.jsm',
     'E10SUtils.jsm',
     'EventEmitter.jsm',
     'FileUtils.jsm',
-    'FindBarChild.jsm',
+    'FindBarContent.jsm',
     'Finder.jsm',
     'FinderHighlighter.jsm',
     'FinderIterator.jsm',
     'FormLikeFactory.jsm',
     'Geometry.jsm',
     'GMPExtractorWorker.js',
     'GMPInstallManager.jsm',
     'GMPUtils.jsm',