Bug 1364477 - Only append formautofill.css the first time autocomplete is opening. r=lchang,ralin draft
authorMatthew Noorenberghe <mozilla@noorenberghe.ca>
Tue, 16 May 2017 01:08:56 -0700
changeset 578610 0d0da653a6055e665ddc49fdb34ef0c6975984d2
parent 576090 a9e4724a00d2fe6c2f472f59b09f407d7d2ceda1
child 578611 6158cc1120c31fb0d090cfffbd19b580a439d727
child 579346 55423ba2d8c23ce47dd48cd3ea72945ae9fb08d2
push id58984
push usermozilla@noorenberghe.ca
push dateTue, 16 May 2017 08:12:53 +0000
reviewerslchang, ralin
bugs1364477
milestone55.0a1
Bug 1364477 - Only append formautofill.css the first time autocomplete is opening. r=lchang,ralin MozReview-Commit-ID: NF5wEuqp4r
browser/extensions/formautofill/bootstrap.js
--- a/browser/extensions/formautofill/bootstrap.js
+++ b/browser/extensions/formautofill/bootstrap.js
@@ -25,53 +25,40 @@ function insertStyleSheet(domWindow, url
 
   if (CACHED_STYLESHEETS.has(domWindow)) {
     CACHED_STYLESHEETS.get(domWindow).push(styleSheet);
   } else {
     CACHED_STYLESHEETS.set(domWindow, [styleSheet]);
   }
 }
 
-let windowListener = {
-  onOpenWindow(window) {
-    let domWindow = window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
-
-    domWindow.addEventListener("load", function onWindowLoaded() {
-      insertStyleSheet(domWindow, STYLESHEET_URI);
-    }, {once: true});
-  },
-};
-
 function startup() {
   if (!Services.prefs.getBoolPref("browser.formautofill.experimental")) {
     return;
   }
 
-  let parent = new FormAutofillParent();
-  let enumerator = Services.wm.getEnumerator("navigator:browser");
-  // Load stylesheet to already opened windows
-  while (enumerator.hasMoreElements()) {
-    let win = enumerator.getNext();
-    let domWindow = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
+  Services.mm.addMessageListener("FormAutoComplete:MaybeOpenPopup", function onMaybeOpenPopup(evt) {
+    let domWindow = evt.target.ownerGlobal;
+    if (CACHED_STYLESHEETS.has(domWindow)) {
+      // This window already has autofill stylesheets.
+      return;
+    }
 
     insertStyleSheet(domWindow, STYLESHEET_URI);
-  }
+  });
 
-  Services.wm.addListener(windowListener);
-
+  let parent = new FormAutofillParent();
   parent.init();
   Services.ppmm.loadProcessScript("data:,new " + function() {
     Components.utils.import("resource://formautofill/FormAutofillContent.jsm");
   }, true);
   Services.mm.loadFrameScript("chrome://formautofill/content/FormAutofillFrameScript.js", true);
 }
 
 function shutdown() {
-  Services.wm.removeListener(windowListener);
-
   let enumerator = Services.wm.getEnumerator("navigator:browser");
 
   while (enumerator.hasMoreElements()) {
     let win = enumerator.getNext();
     let domWindow = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
     let cachedStyleSheets = CACHED_STYLESHEETS.get(domWindow);
 
     if (!cachedStyleSheets) {