Bug 513944 - Weave should not load / do anything until it absolutely needs to
authorEdward Lee <edilee@mozilla.com>
Fri, 25 Sep 2009 08:13:12 -0700
changeset 45775 572f4450781d6cd272a9f915c014747054e52bb7
parent 45774 c8d22eb88702a74cd804dcc6197f877674f7074d
child 45776 4bba8266b684e0173b1706c50415c570426bc1e0
push idunknown
push userunknown
push dateunknown
bugs513944
Bug 513944 - Weave should not load / do anything until it absolutely needs to Weave already triggers on a late notification and puts itself on the event loop, so just additionally delay startup based on the number of open tabs (which will all be busy at startup).
services/sync/Weave.js
services/sync/modules/service.js
--- a/services/sync/Weave.js
+++ b/services/sync/Weave.js
@@ -57,17 +57,17 @@ WeaveService.prototype = {
         getService(Ci.nsIObserverService);
       os.addObserver(this, "sessionstore-windows-restored", true);
       break;
    /* The following event doesn't exist on Fennec; for Fennec loading, see
     * fennec-weave-overlay.js.
     */
     case "sessionstore-windows-restored":
       Cu.import("resource://weave/service.js");
-      Weave.Utils.delay(function() Weave.Service.onStartup());
+      Weave.Service.onStartup();
       break;
     }
   }
 };
 
 function AboutWeaveService() {}
 AboutWeaveService.prototype = {
   classDescription: "about:weave",
--- a/services/sync/modules/service.js
+++ b/services/sync/modules/service.js
@@ -298,24 +298,46 @@ WeaveSvc.prototype = {
     }
 
     return ok;
   },
 
   onWindowOpened: function WeaveSvc__onWindowOpened() {
   },
 
+  /**
+   * Prepare to initialize the rest of Weave after waiting a little bit
+   */
+  onStartup: function onStartup() {
+    this._status = new StatusRecord();
+    this.status.service = STATUS_DELAYED;
+
+    // Figure out how many seconds to delay loading Weave based on the app
+    let wait = 0;
+    switch (Svc.AppInfo.ID) {
+      case FIREFOX_ID:
+        // Add one second delay for each tab in every window
+        let enum = Svc.WinMediator.getEnumerator("navigator:browser");
+        while (enum.hasMoreElements())
+          wait += enum.getNext().gBrowser.mTabs.length;
+    }
+
+    // Make sure we wait a little but but not too long in the worst case
+    wait = Math.ceil(Math.max(5, Math.min(20, wait)));
+
+    this._initLogs();
+    this._log.info("Loading Weave " + WEAVE_VERSION + " in " + wait + " sec.");
+    Utils.delay(this._onStartup, wait * 1000, this, "_startupTimer");
+  },
+
   // one-time initialization like setting up observers and the like
   // xxx we might need to split some of this out into something we can call
   //     again when username/server/etc changes
-  onStartup: function WeaveSvc_onStartup() {
-    this._initLogs();
-    this._log.info("Weave " + WEAVE_VERSION + " initializing");
+  _onStartup: function _onStartup() {
     this._registerEngines();
-    this._status = new StatusRecord();
 
     // Reset our sync id if we're upgrading, so sync knows to reset local data
     if (WEAVE_VERSION != Svc.Prefs.get("lastversion")) {
       this._log.info("Resetting client syncID from _onStartup.");
       Clients.resetSyncID();
     }
 
     let ua = Cc["@mozilla.org/network/protocol;1?name=http"].