Bug 791669 - [New Tab Page] don't immediately preload the new tab page on startup; r=jaws
authorTim Taubert <ttaubert@mozilla.com>
Fri, 21 Sep 2012 11:28:06 +0200
changeset 107761 9c75ed3092e83275bbe82de5fc7bce7b07bc35d3
parent 107760 0d1172e85858c72a7e6369fd8f3f7bcd405f13c0
child 107762 6cd4d5d108d6c077b6b2163ddbf53db446bcd8f5
push idunknown
push userunknown
push dateunknown
reviewersjaws
bugs791669
milestone18.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 791669 - [New Tab Page] don't immediately preload the new tab page on startup; r=jaws
browser/modules/BrowserNewTabPreloader.jsm
--- a/browser/modules/BrowserNewTabPreloader.jsm
+++ b/browser/modules/BrowserNewTabPreloader.jsm
@@ -10,39 +10,83 @@ const Cu = Components.utils;
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
 const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
 const PREF_BRANCH = "browser.newtab.";
+const TOPIC_DELAYED_STARTUP = "browser-delayed-startup-finished";
+const PRELOADER_INIT_DELAY_MS = 5000;
 
-let BrowserNewTabPreloader =  {
+let BrowserNewTabPreloader = {
   init: function Preloader_init() {
-    Preferences.init();
-
-    if (Preferences.enabled) {
-      HiddenBrowser.create();
-    }
+    Initializer.start();
   },
 
   uninit: function Preloader_uninit() {
+    Initializer.stop();
     HostFrame.destroy();
     Preferences.uninit();
     HiddenBrowser.destroy();
   },
 
   newTab: function Preloader_newTab(aTab) {
     HiddenBrowser.swapWithNewTab(aTab);
   }
 };
 
 Object.freeze(BrowserNewTabPreloader);
 
+let Initializer = {
+  _timer: null,
+  _observing: false,
+
+  start: function Initializer_start() {
+    Services.obs.addObserver(this, TOPIC_DELAYED_STARTUP, false);
+    this._observing = true;
+  },
+
+  stop: function Initializer_stop() {
+    if (this._timer) {
+      this._timer.cancel();
+      this._timer = null;
+    }
+
+    if (this._observing) {
+      Services.obs.removeObserver(this, TOPIC_DELAYED_STARTUP);
+      this._observing = false;
+    }
+  },
+
+  observe: function Initializer_observe(aSubject, aTopic, aData) {
+    if (aTopic == TOPIC_DELAYED_STARTUP) {
+      Services.obs.removeObserver(this, TOPIC_DELAYED_STARTUP);
+      this._observing = false;
+      this._startTimer();
+    } else if (aTopic == "timer-callback") {
+      this._timer = null;
+      this._startPreloader();
+    }
+  },
+
+  _startTimer: function Initializer_startTimer() {
+    this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
+    this._timer.init(this, PRELOADER_INIT_DELAY_MS, Ci.nsITimer.TYPE_ONE_SHOT);
+  },
+
+  _startPreloader: function Initializer_startPreloader() {
+    Preferences.init();
+    if (Preferences.enabled) {
+      HiddenBrowser.create();
+    }
+  }
+};
+
 let Preferences = {
   _enabled: null,
   _branch: null,
   _url: null,
 
   get enabled() {
     if (this._enabled === null) {
       this._enabled = this._branch.getBoolPref("preload") &&