Bug 1364477 - Only append formautofill.css the first time autocomplete is opening. r=lchang,ralin draft
authorMatthew Noorenberghe <mozilla@noorenberghe.ca>
Wed, 17 May 2017 23:28:00 -0700
changeset 581015 c973f678908e6118a02ebbfab921b89e0f87c600
parent 581014 64062a9557da7a9cdfdc3f0ecf5ac201414fd299
child 581016 ada90dfc9fa149947c322a4539a75edafb42a34f
child 582807 098db1d42c1811ef27302725f7ab7f528dcf7bc3
push id59746
push usermozilla@noorenberghe.ca
push dateFri, 19 May 2017 07:00:34 +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,55 +25,46 @@ 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);
+function onMaybeOpenPopup(evt) {
+  let domWindow = evt.target.ownerGlobal;
+  if (CACHED_STYLESHEETS.has(domWindow)) {
+    // This window already has autofill stylesheets.
+    return;
+  }
 
-    domWindow.addEventListener("load", function onWindowLoaded() {
-      insertStyleSheet(domWindow, STYLESHEET_URI);
-    }, {once: true});
-  },
-};
+  insertStyleSheet(domWindow, STYLESHEET_URI);
+}
 
 function startup() {
   if (!Services.prefs.getBoolPref("extensions.formautofill.experimental")) {
     return;
   }
 
+  // Listen for the autocomplete popup message to lazily append our stylesheet related to the popup.
+  Services.mm.addMessageListener("FormAutoComplete:MaybeOpenPopup", onMaybeOpenPopup);
+
   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);
-
-    insertStyleSheet(domWindow, STYLESHEET_URI);
-  }
-
-  Services.wm.addListener(windowListener);
-
   parent.init().catch(Cu.reportError);
   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);
+  Services.mm.removeMessageListener("FormAutoComplete:MaybeOpenPopup", onMaybeOpenPopup);
 
   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) {
       continue;
     }