Bug 1354341 - Don't load ConsoleAPI.jsm for passwordmgr logging unless we need it. r=dolske draft
authorAndrew McCreight <continuation@gmail.com>
Fri, 07 Apr 2017 15:01:25 -0700
changeset 560574 5b98f72e375c52d97069d3b71f18031aa35e64d2
parent 560573 68fa27ee0f44f71ffea3c3c5d852b6b758841fd4
child 560575 0785bc34b9cf9aec59b3e70442ba858f9c5877f3
push id53458
push userbmo:continuation@gmail.com
push dateTue, 11 Apr 2017 16:27:38 +0000
reviewersdolske
bugs1354341
milestone55.0a1
Bug 1354341 - Don't load ConsoleAPI.jsm for passwordmgr logging unless we need it. r=dolske MozReview-Commit-ID: IGXWYgawmLl
toolkit/components/passwordmgr/LoginHelper.jsm
--- a/toolkit/components/passwordmgr/LoginHelper.jsm
+++ b/toolkit/components/passwordmgr/LoginHelper.jsm
@@ -18,51 +18,108 @@ this.EXPORTED_SYMBOLS = [
 
 // Globals
 
 const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
 
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
+XPCOMUtils.defineLazyModuleGetter(this, "ConsoleAPI",
+  "resource://gre/modules/Console.jsm");
+
+// LazyLogger
+
+/**
+ * This is a thin wrapper around the ConsoleAPI logger that doesn't
+ * load that .jsm unless logging is actually needed (when either the
+ * pref signon.debug is set to true or it is a warning or error).
+ */
+function LazyLogger(aLoginHelper, aLogPrefix) {
+  this.loginHelper = aLoginHelper;
+  this.logPrefix = aLogPrefix;
+  this.logger = undefined;
+}
+
+LazyLogger.prototype = {
+  ensureLogger() {
+    if (typeof this.logger !== "undefined") {
+      return;
+    }
+    // Create a new instance of the ConsoleAPI so we can control the maxLogLevel with a pref.
+    let consoleOptions = {
+      maxLogLevel: this.getMaxLogLevel(),
+      prefix: this.loginHelper.logPrefix,
+    };
+    this.logger = new ConsoleAPI(consoleOptions);
+  },
+
+  getMaxLogLevel() {
+    return this.loginHelper.debug ? "debug" : "warn";
+  },
+
+  updateMaxLogLevel() {
+    if (typeof this.logger === "undefined") {
+      return;
+    }
+    this.logger.maxLogLevel = this.getMaxLogLevel();
+  },
+
+  debug() {
+    if (!this.loginHelper.debug) {
+      return;
+    }
+    this.ensureLogger();
+    this.logger.debug.apply(this.logger, arguments);
+  },
+
+  log() {
+    if (!this.loginHelper.debug) {
+      return;
+    }
+    this.ensureLogger();
+    this.logger.log.apply(this.logger, arguments);
+  },
+
+  warn() {
+    this.ensureLogger();
+    this.logger.warn.apply(this.logger, arguments);
+  },
+
+  error() {
+    this.ensureLogger();
+    this.logger.error.apply(this.logger, arguments);
+  },
+};
+
 // LoginHelper
 
 /**
  * Contains functions shared by different Login Manager components.
  */
 this.LoginHelper = {
   /**
    * Warning: these only update if a logger was created.
    */
   debug: Services.prefs.getBoolPref("signon.debug"),
   formlessCaptureEnabled: Services.prefs.getBoolPref("signon.formlessCapture.enabled"),
   schemeUpgrades: Services.prefs.getBoolPref("signon.schemeUpgrades"),
   insecureAutofill: Services.prefs.getBoolPref("signon.autofillForms.http"),
   showInsecureFieldWarning: Services.prefs.getBoolPref("security.insecure_field_warning.contextual.enabled"),
 
   createLogger(aLogPrefix) {
-    let getMaxLogLevel = () => {
-      return this.debug ? "debug" : "warn";
-    };
-
-    // Create a new instance of the ConsoleAPI so we can control the maxLogLevel with a pref.
-    let ConsoleAPI = Cu.import("resource://gre/modules/Console.jsm", {}).ConsoleAPI;
-    let consoleOptions = {
-      maxLogLevel: getMaxLogLevel(),
-      prefix: aLogPrefix,
-    };
-    let logger = new ConsoleAPI(consoleOptions);
+    let logger = new LazyLogger(this, aLogPrefix);
 
     // Watch for pref changes and update this.debug and the maxLogLevel for created loggers
     Services.prefs.addObserver("signon.", () => {
       this.debug = Services.prefs.getBoolPref("signon.debug");
       this.formlessCaptureEnabled = Services.prefs.getBoolPref("signon.formlessCapture.enabled");
       this.schemeUpgrades = Services.prefs.getBoolPref("signon.schemeUpgrades");
       this.insecureAutofill = Services.prefs.getBoolPref("signon.autofillForms.http");
-      logger.maxLogLevel = getMaxLogLevel();
+      logger.updateMaxLogLevel();
     }, false);
 
     Services.prefs.addObserver("security.insecure_field_warning.", () => {
       this.showInsecureFieldWarning = Services.prefs.getBoolPref("security.insecure_field_warning.contextual.enabled");
     }, false);
 
     return logger;
   },