Bug 1078085 - Simplify preference checking code in the newtab preloader r=gijs
authorTim Taubert <ttaubert@mozilla.com>
Tue, 07 Oct 2014 17:54:31 +0200
changeset 232538 2c435535573d73ab43116c88200c3eab28a89600
parent 232537 44e09b1fd2ed2c3e60ed3bdd6b76eb42039db347
child 232539 ec19d8282465016ddd24295b448003dc63f29c03
push id4187
push userbhearsum@mozilla.com
push dateFri, 28 Nov 2014 15:29:12 +0000
treeherdermozilla-beta@f23cc6a30c11 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgijs
bugs1078085
milestone35.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 1078085 - Simplify preference checking code in the newtab preloader r=gijs
browser/base/content/newtab/intro.js
browser/components/nsBrowserGlue.js
browser/modules/BrowserNewTabPreloader.jsm
--- a/browser/base/content/newtab/intro.js
+++ b/browser/base/content/newtab/intro.js
@@ -2,18 +2,16 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 #endif
 
 const PREF_INTRO_SHOWN = "browser.newtabpage.introShown";
 
 let gIntro = {
-  _introShown: Services.prefs.getBoolPref(PREF_INTRO_SHOWN),
-
   _nodeIDSuffixes: [
     "panel",
     "what",
   ],
 
   _nodes: {},
 
   init: function() {
@@ -21,17 +19,17 @@ let gIntro = {
       this._nodes[idSuffix] = document.getElementById("newtab-intro-" + idSuffix);
     }
 
     this._nodes.panel.addEventListener("popupshowing", e => this._setUpPanel());
     this._nodes.what.addEventListener("click", e => this.showPanel());
   },
 
   showIfNecessary: function() {
-    if (!this._introShown) {
+    if (!Services.prefs.getBoolPref(PREF_INTRO_SHOWN)) {
       Services.prefs.setBoolPref(PREF_INTRO_SHOWN, true);
       this.showPanel();
     }
   },
 
   showPanel: function() {
     // Point the panel at the 'what' link
     this._nodes.panel.openPopup(this._nodes.what);
--- a/browser/components/nsBrowserGlue.js
+++ b/browser/components/nsBrowserGlue.js
@@ -502,17 +502,16 @@ BrowserGlue.prototype = {
 
     this._syncSearchEngines();
 
     WebappManager.init();
     PageThumbs.init();
     NewTabUtils.init();
     DirectoryLinksProvider.init();
     NewTabUtils.links.addProvider(DirectoryLinksProvider);
-    BrowserNewTabPreloader.init();
 #ifdef NIGHTLY_BUILD
     if (Services.prefs.getBoolPref("dom.identity.enabled")) {
       SignInToWebsiteUX.init();
     }
 #endif
 #ifdef NIGHTLY_BUILD
     ShumwayUtils.init();
 #endif
--- a/browser/modules/BrowserNewTabPreloader.jsm
+++ b/browser/modules/BrowserNewTabPreloader.jsm
@@ -13,58 +13,60 @@ const Ci = Components.interfaces;
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Promise.jsm");
 
 const HTML_NS = "http://www.w3.org/1999/xhtml";
 const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
 const XUL_PAGE = "data:application/vnd.mozilla.xul+xml;charset=utf-8,<window%20id='win'/>";
 const NEWTAB_URL = "about:newtab";
-const PREF_BRANCH = "browser.newtab.";
+
+const PREF_NEWTAB_URL = "browser.newtab.url";
+const PREF_NEWTAB_PRELOAD = "browser.newtab.preload";
 
 // The interval between swapping in a preload docShell and kicking off the
 // next preload in the background.
 const PRELOADER_INTERVAL_MS = 600;
 // The number of miliseconds we'll wait after we received a notification that
 // causes us to update our list of browsers and tabbrowser sizes. This acts as
 // kind of a damper when too many events are occuring in quick succession.
 const PRELOADER_UPDATE_DELAY_MS = 3000;
 
 const TOPIC_TIMER_CALLBACK = "timer-callback";
 const TOPIC_DELAYED_STARTUP = "browser-delayed-startup-finished";
 const TOPIC_XUL_WINDOW_CLOSED = "xul-window-destroyed";
 
 const BROWSER_CONTENT_SCRIPT = "chrome://browser/content/content.js";
 
+function isPreloadingEnabled() {
+  return Services.prefs.getBoolPref(PREF_NEWTAB_PRELOAD) &&
+         !Services.prefs.prefHasUserValue(PREF_NEWTAB_URL);
+}
+
 function createTimer(obj, delay) {
   let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
   timer.init(obj, delay, Ci.nsITimer.TYPE_ONE_SHOT);
   return timer;
 }
 
 function clearTimer(timer) {
   if (timer) {
     timer.cancel();
   }
   return null;
 }
 
 this.BrowserNewTabPreloader = {
-  init: function Preloader_init() {
-    Preferences.init();
-  },
-
   uninit: function Preloader_uninit() {
     HostFrame.destroy();
-    Preferences.uninit();
     HiddenBrowsers.uninit();
   },
 
   newTab: function Preloader_newTab(aTab) {
-    if (!Preferences.enabled) {
+    if (!isPreloadingEnabled()) {
       return false;
     }
 
     let win = aTab.ownerDocument.defaultView;
     if (win.gBrowser) {
       let utils = win.QueryInterface(Ci.nsIInterfaceRequestor)
                      .getInterface(Ci.nsIDOMWindowUtils);
 
@@ -76,51 +78,16 @@ this.BrowserNewTabPreloader = {
     }
 
     return false;
   }
 };
 
 Object.freeze(BrowserNewTabPreloader);
 
-let Preferences = {
-  _enabled: null,
-  _branch: null,
-
-  get enabled() {
-    if (this._enabled === null) {
-      this._enabled = this._branch.getBoolPref("preload") &&
-                      !this._branch.prefHasUserValue("url");
-    }
-
-    return this._enabled;
-  },
-
-  init: function Preferences_init() {
-    this._branch = Services.prefs.getBranch(PREF_BRANCH);
-    this._branch.addObserver("", this, false);
-  },
-
-  uninit: function Preferences_uninit() {
-    if (this._branch) {
-      this._branch.removeObserver("", this);
-      this._branch = null;
-    }
-  },
-
-  observe: function Preferences_observe() {
-    let prevEnabled = this._enabled;
-    this._enabled = null;
-
-    if (prevEnabled && !this.enabled) {
-      HiddenBrowsers.uninit();
-    }
-  },
-};
-
 let HiddenBrowsers = {
   _browsers: null,
   _updateTimer: null,
 
   _topics: [
     TOPIC_DELAYED_STARTUP,
     TOPIC_XUL_WINDOW_CLOSED
   ],