Bug 1358921: Lazily load SelfSupportBackend.jsm when first needed. r=florian draft
authorKris Maglione <maglione.k@gmail.com>
Tue, 02 May 2017 19:51:17 -0700
changeset 571722 dfc43aa4a77b5dbcbeb998e1b481daf5019aa80a
parent 571721 08d1a24ca158cd91bd1691f1c230e4bfbacf149b
child 626855 879a62486fd61eea1f9790e8757a8686effbae5d
push id56891
push usermaglione.k@gmail.com
push dateWed, 03 May 2017 05:58:28 +0000
reviewersflorian
bugs1358921
milestone55.0a1
Bug 1358921: Lazily load SelfSupportBackend.jsm when first needed. r=florian MozReview-Commit-ID: 8tG8VcWXRYm
browser/components/nsBrowserGlue.js
browser/modules/SelfSupportBackend.jsm
--- a/browser/components/nsBrowserGlue.js
+++ b/browser/components/nsBrowserGlue.js
@@ -75,17 +75,17 @@ let initializedModules = {};
   ["PlacesBackups", "resource://gre/modules/PlacesBackups.jsm"],
   ["PlacesUtils", "resource://gre/modules/PlacesUtils.jsm"],
   ["PluralForm", "resource://gre/modules/PluralForm.jsm"],
   ["PrivateBrowsingUtils", "resource://gre/modules/PrivateBrowsingUtils.jsm"],
   ["ProcessHangMonitor", "resource:///modules/ProcessHangMonitor.jsm"],
   ["ReaderParent", "resource:///modules/ReaderParent.jsm"],
   ["RecentWindow", "resource:///modules/RecentWindow.jsm"],
   ["RemotePrompt", "resource:///modules/RemotePrompt.jsm"],
-  ["SelfSupportBackend", "resource:///modules/SelfSupportBackend.jsm"],
+  ["SelfSupportBackend", "resource:///modules/SelfSupportBackend.jsm", "init"],
   ["SessionStore", "resource:///modules/sessionstore/SessionStore.jsm"],
   ["ShellService", "resource:///modules/ShellService.jsm"],
   ["SimpleServiceDiscovery", "resource://gre/modules/SimpleServiceDiscovery.jsm"],
   ["TabCrashHandler", "resource:///modules/ContentCrashHandlers.jsm"],
   ["Task", "resource://gre/modules/Task.jsm"],
   ["UITour", "resource:///modules/UITour.jsm"],
   ["WebChannel", "resource://gre/modules/WebChannel.jsm"],
   ["WindowsRegistry", "resource://gre/modules/WindowsRegistry.jsm"],
@@ -119,16 +119,17 @@ XPCOMUtils.defineLazyGetter(this, "gBran
 XPCOMUtils.defineLazyGetter(this, "gBrowserBundle", function() {
   return Services.strings.createBundle("chrome://browser/locale/browser.properties");
 });
 
 const global = this;
 
 const listeners = {
   observers: {
+    "sessionstore-windows-restored": ["SelfSupportBackend"],
   },
 
   ppmm: {
     // PLEASE KEEP THIS LIST IN SYNC WITH THE LISTENERS ADDED IN ContentPrefServiceParent.init
     "ContentPrefs:FunctionCall": ["ContentPrefServiceParent"],
     "ContentPrefs:AddObserverForName": ["ContentPrefServiceParent"],
     "ContentPrefs:RemoveObserverForName": ["ContentPrefServiceParent"],
     // PLEASE KEEP THIS LIST IN SYNC WITH THE LISTENERS ADDED IN ContentPrefServiceParent.init
@@ -605,18 +606,16 @@ BrowserGlue.prototype = {
     NewTabUtils.init();
     NewTabUtils.links.addProvider(DirectoryLinksProvider);
     AboutNewTab.init();
 
     SessionStore.init();
     BrowserUsageTelemetry.init();
     BrowserUITelemetry.init();
 
-    SelfSupportBackend.init();
-
     if (AppConstants.INSTALL_COMPACT_THEMES) {
       let vendorShortName = gBrandBundle.GetStringFromName("vendorShortName");
 
       LightweightThemeManager.addBuiltInTheme({
         id: "firefox-compact-light@mozilla.org",
         name: gBrowserBundle.GetStringFromName("compactLightTheme.name"),
         description: gBrowserBundle.GetStringFromName("compactLightTheme.description"),
         headerURL: "resource:///chrome/browser/content/browser/defaultthemes/compact.header.png",
@@ -980,17 +979,17 @@ BrowserGlue.prototype = {
 
     for (let mod of Object.values(initializedModules)) {
       if (mod.uninit) {
         mod.uninit();
       }
     }
 
     BrowserUsageTelemetry.uninit();
-    SelfSupportBackend.uninit();
+
     PageThumbs.uninit();
     AboutNewTab.uninit();
     NewTabUtils.uninit();
     AutoCompletePopup.uninit();
     DateTimePickerHelper.uninit();
   },
 
   _initServiceDiscovery() {
--- a/browser/modules/SelfSupportBackend.jsm
+++ b/browser/modules/SelfSupportBackend.jsm
@@ -6,17 +6,16 @@
 
 this.EXPORTED_SYMBOLS = ["SelfSupportBackend"];
 
 const Cu = Components.utils;
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 
 Cu.import("resource://gre/modules/Log.jsm");
-Cu.import("resource://gre/modules/Preferences.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/Timer.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "HiddenFrame",
   "resource://gre/modules/HiddenFrame.jsm");
 
 // Enables or disables the Self Support.
@@ -43,17 +42,17 @@ const PREF_LOG_DUMP = PREF_BRANCH_LOG + 
 
 const HTML_NS = "http://www.w3.org/1999/xhtml";
 const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
 
 const UITOUR_FRAME_SCRIPT = "chrome://browser/content/content-UITour.js";
 
 // Whether the FHR/Telemetry unification features are enabled.
 // Changing this pref requires a restart.
-const IS_UNIFIED_TELEMETRY = Preferences.get(PREF_TELEMETRY_UNIFIED, false);
+const IS_UNIFIED_TELEMETRY = Services.prefs.getBoolPref(PREF_TELEMETRY_UNIFIED, false);
 
 var gLogAppenderDump = null;
 
 this.SelfSupportBackend = Object.freeze({
   init() {
     SelfSupportBackendInternal.init();
   },
 
@@ -70,16 +69,20 @@ var SelfSupportBackendInternal = {
   // The HiddenFrame holding the _browser element.
   _frame: null,
   _log: null,
   _progressListener: null,
 
   // Whether we're invited to let test code talk to our frame.
   _testing: false,
 
+  // Whether self-support is enabled, and we want to continue lazy UI
+  // startup after the session has been restored.
+  _lazyStartupEnabled: false,
+
   /**
    * Initializes the self support backend.
    */
   init() {
     this._configureLogging();
 
     this._log.trace("init");
 
@@ -88,35 +91,40 @@ var SelfSupportBackendInternal = {
     // Only allow to use SelfSupport if Unified Telemetry is enabled.
     let reportingEnabled = IS_UNIFIED_TELEMETRY;
     if (!reportingEnabled) {
       this._log.config("init - Disabling SelfSupport because FHR and Unified Telemetry are disabled.");
       return;
     }
 
     // Make sure UITour is enabled.
-    let uiTourEnabled = Preferences.get(PREF_UITOUR_ENABLED, false);
+    let uiTourEnabled = Services.prefs.getBoolPref(PREF_UITOUR_ENABLED, false);
     if (!uiTourEnabled) {
       this._log.config("init - Disabling SelfSupport because UITour is disabled.");
       return;
     }
 
     // Check the preferences to see if we want this to be active.
-    if (!Preferences.get(PREF_ENABLED, true)) {
+    if (!Services.prefs.getBoolPref(PREF_ENABLED, true)) {
       this._log.config("init - SelfSupport is disabled.");
       return;
     }
 
-    Services.obs.addObserver(this, "sessionstore-windows-restored");
+    this._lazyStartupEnabled = true;
   },
 
   /**
    * Shut down the self support backend, if active.
    */
   uninit() {
+    if (!this._log) {
+      // We haven't been initialized yet, so just return.
+      return;
+    }
+
     this._log.trace("uninit");
 
     Services.prefs.removeObserver(PREF_BRANCH_LOG, this);
 
     // Cancel delayed loading, if still active, when shutting down.
     clearTimeout(this._delayedLoadTimerId);
 
     // Dispose of the hidden browser.
@@ -143,22 +151,25 @@ var SelfSupportBackendInternal = {
       Services.obs.notifyObservers(this._browser, "self-support-browser-destroyed");
     }
   },
 
   /**
    * Handle notifications. Once all windows are created, we wait a little bit more
    * since tabs might still be loading. Then, we open the self support.
    */
+  // Observers are added in nsBrowserGlue.js
   observe(aSubject, aTopic, aData) {
     this._log.trace("observe - Topic " + aTopic);
 
     if (aTopic === "sessionstore-windows-restored") {
-      Services.obs.removeObserver(this, "sessionstore-windows-restored");
-      this._delayedLoadTimerId = setTimeout(this._loadSelfSupport.bind(this), STARTUP_DELAY_MS);
+      if (this._lazyStartupEnabled) {
+        this._delayedLoadTimerId = setTimeout(this._loadSelfSupport.bind(this), STARTUP_DELAY_MS);
+        this._lazyStartupEnabled = false;
+      }
     } else if (aTopic === "nsPref:changed") {
       this._configureLogging();
     }
   },
 
   /**
    * Configure the logger based on the preferences.
    */
@@ -167,20 +178,20 @@ var SelfSupportBackendInternal = {
       this._log = Log.repository.getLogger(LOGGER_NAME);
 
       // Log messages need to go to the browser console.
       let consoleAppender = new Log.ConsoleAppender(new Log.BasicFormatter());
       this._log.addAppender(consoleAppender);
     }
 
     // Make sure the logger keeps up with the logging level preference.
-    this._log.level = Log.Level[Preferences.get(PREF_LOG_LEVEL, "Warn")];
+    this._log.level = Log.Level[Services.prefs.getStringPref(PREF_LOG_LEVEL, "Warn")];
 
     // If enabled in the preferences, add a dump appender.
-    let logDumping = Preferences.get(PREF_LOG_DUMP, false);
+    let logDumping = Services.prefs.getBoolPref(PREF_LOG_DUMP, false);
     if (logDumping != !!gLogAppenderDump) {
       if (logDumping) {
         gLogAppenderDump = new Log.DumpAppender(new Log.BasicFormatter());
         this._log.addAppender(gLogAppenderDump);
       } else {
         this._log.removeAppender(gLogAppenderDump);
         gLogAppenderDump = null;
       }
@@ -248,17 +259,17 @@ var SelfSupportBackendInternal = {
 
   /**
    * Create a browser and attach it to an hidden window. The browser will contain the
    * self support page and attempt to load the page content. If loading fails, try again
    * after an interval.
    */
   _loadSelfSupport() {
     // Fetch the Self Support URL from the preferences.
-    let unformattedURL = Preferences.get(PREF_URL, null);
+    let unformattedURL = Services.prefs.getStringPref(PREF_URL, "");
     let url = Services.urlFormatter.formatURL(unformattedURL);
     if (!url.startsWith("https:")) {
       this._log.error("_loadSelfSupport - Non HTTPS URL provided: " + url);
       return;
     }
 
     this._log.config("_loadSelfSupport - URL " + url);