Bug 1371335. Part 1 - Lazily load TabListener module. r=gijs draft
authorMike Taylor <miket@mozilla.com>
Wed, 28 Jun 2017 09:57:52 -0700
changeset 607099 bb62478387df977c3430cb57b3cdcbadc03fe591
parent 606990 c4b3e9bd89bfb2f0afdf6cf53e100bff50a45bec
child 607100 502503751817302a931bb9ce27a6942f49637aba
push id67891
push userbmo:miket@mozilla.com
push dateTue, 11 Jul 2017 21:04:35 +0000
reviewersgijs
bugs1371335
milestone56.0a1
Bug 1371335. Part 1 - Lazily load TabListener module. r=gijs MozReview-Commit-ID: JDEOMwkHVlj
browser/extensions/webcompat-reporter/content/WebCompatReporter.jsm
--- a/browser/extensions/webcompat-reporter/content/WebCompatReporter.jsm
+++ b/browser/extensions/webcompat-reporter/content/WebCompatReporter.jsm
@@ -4,47 +4,46 @@
 
 this.EXPORTED_SYMBOLS = ["WebCompatReporter"];
 
 let { classes: Cc, interfaces: Ci, utils: Cu } = Components;
 
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
+const TABLISTENER_JSM = "chrome://webcompat-reporter/content/TabListener.jsm";
+const WIDGET_ID = "webcompat-reporter-button";
+
 XPCOMUtils.defineLazyModuleGetter(this, "CustomizableUI",
   "resource:///modules/CustomizableUI.jsm");
 
 XPCOMUtils.defineLazyGetter(this, "wcStrings", function() {
   return Services.strings.createBundle(
     "chrome://webcompat-reporter/locale/webcompat.properties");
 });
 
 XPCOMUtils.defineLazyGetter(this, "wcStyleURI", function() {
   return Services.io.newURI("chrome://webcompat-reporter/skin/lightbulb.css");
 });
 
-const WIDGET_ID = "webcompat-reporter-button";
-const TABLISTENER_JSM = "chrome://webcompat-reporter/content/TabListener.jsm";
-
 let WebCompatReporter = {
   get endpoint() {
     return Services.urlFormatter.formatURLPref(
       "extensions.webcompat-reporter.newIssueEndpoint");
   },
 
   init() {
-    /* global TabListener */
-    Cu.import(TABLISTENER_JSM);
-
     let styleSheetService = Cc["@mozilla.org/content/style-sheet-service;1"]
       .getService(Ci.nsIStyleSheetService);
     this._sheetType = styleSheetService.AUTHOR_SHEET;
     this._cachedSheet = styleSheetService.preloadSheet(wcStyleURI,
                                                        this._sheetType);
 
+    XPCOMUtils.defineLazyModuleGetter(this, "TabListener", TABLISTENER_JSM);
+
     CustomizableUI.createWidget({
       id: WIDGET_ID,
       label: wcStrings.GetStringFromName("wc-reporter.label"),
       tooltiptext: wcStrings.GetStringFromName("wc-reporter.tooltip"),
       defaultArea: CustomizableUI.AREA_PANEL,
       disabled: true,
       onCommand: (e) => this.reportIssue(e.target.ownerDocument),
     });
@@ -57,17 +56,17 @@ let WebCompatReporter = {
   },
 
   onWindowOpened(win) {
     // Attach stylesheet for the button icon.
     win.QueryInterface(Ci.nsIInterfaceRequestor)
       .getInterface(Ci.nsIDOMWindowUtils)
       .addSheet(this._cachedSheet, this._sheetType);
     // Attach listeners to new window.
-    win._webcompatReporterTabListener = new TabListener(win);
+    win._webcompatReporterTabListener = new this.TabListener(win);
   },
 
   onWindowClosed(win) {
     if (win._webcompatReporterTabListener) {
       win._webcompatReporterTabListener.removeListeners();
       delete win._webcompatReporterTabListener;
     }
   },
@@ -79,17 +78,20 @@ let WebCompatReporter = {
       this.onWindowClosed(win);
 
       win.QueryInterface(Ci.nsIInterfaceRequestor)
         .getInterface(Ci.nsIDOMWindowUtils)
         .removeSheet(wcStyleURI, this._sheetType);
     }
 
     CustomizableUI.removeListener(this);
-    Cu.unload(TABLISTENER_JSM);
+
+    if (Cu.isModuleLoaded(TABLISTENER_JSM)) {
+      Cu.unload(TABLISTENER_JSM);
+    }
   },
 
   // This method injects a framescript that should send back a screenshot blob
   // of the top-level window of the currently selected tab, resolved as a
   // Promise.
   getScreenshot(gBrowser) {
     const FRAMESCRIPT = "chrome://webcompat-reporter/content/tab-frame.js";
     const TABDATA_MESSAGE = "WebCompat:SendTabData";