Bug 1398819 - Turn on prerendered version of activity stream in aboutNewTabService draft
authork88hudson <khudson@mozilla.com>
Mon, 11 Sep 2017 13:22:44 -0400
changeset 662428 13f0607094502c1dfbbccc7cdc3beb6084fe5041
parent 662290 f9a5e9ed62103c84e4cde915f4d08f1ce71be83e
child 730861 0888418c759dbd272c27cae819fe36ba5b4f37e7
push id79076
push userkhudson@mozilla.com
push dateMon, 11 Sep 2017 17:23:05 +0000
bugs1398819
milestone57.0a1
Bug 1398819 - Turn on prerendered version of activity stream in aboutNewTabService MozReview-Commit-ID: 3v9UR0A9K1z
browser/app/profile/firefox.js
browser/components/newtab/aboutNewTabService.js
browser/components/newtab/nsIAboutNewTabService.idl
browser/components/newtab/tests/xpcshell/test_AboutNewTabService.js
--- a/browser/app/profile/firefox.js
+++ b/browser/app/profile/firefox.js
@@ -1261,16 +1261,17 @@ pref("browser.newtabpage.rows", 3);
 // number of columns of newtab grid
 pref("browser.newtabpage.columns", 5);
 
 // directory tiles download URL
 pref("browser.newtabpage.directory.source", "https://tiles.services.mozilla.com/v3/links/fetch/%LOCALE%/%CHANNEL%");
 
 // activates Activity Stream
 pref("browser.newtabpage.activity-stream.enabled", true);
+pref("browser.newtabpage.activity-stream.prerender", true);
 pref("browser.newtabpage.activity-stream.aboutHome.enabled", false);
 
 // Enable the DOM fullscreen API.
 pref("full-screen-api.enabled", true);
 
 // Startup Crash Tracking
 // number of startup crashes that can occur before starting into safe mode automatically
 // (this pref has no effect if more than 6 hours have passed since the last crash)
--- a/browser/components/newtab/aboutNewTabService.js
+++ b/browser/components/newtab/aboutNewTabService.js
@@ -11,27 +11,29 @@ const {utils: Cu, interfaces: Ci} = Comp
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "AboutNewTab",
                                   "resource:///modules/AboutNewTab.jsm");
 
 const LOCAL_NEWTAB_URL = "chrome://browser/content/newtab/newTab.xhtml";
 
 const ACTIVITY_STREAM_URL = "resource://activity-stream/data/content/activity-stream.html";
-
+const ACTIVITY_STREAM_PRERENDERED_URL = "resource://activity-stream/data/content/activity-stream-prerendered.html";
 const ABOUT_URL = "about:newtab";
 
 const IS_MAIN_PROCESS = Services.appinfo.processType === Services.appinfo.PROCESS_TYPE_DEFAULT;
 
 // Pref that tells if activity stream is enabled
 const PREF_ACTIVITY_STREAM_ENABLED = "browser.newtabpage.activity-stream.enabled";
+const PREF_ACTIVITY_STREAM_PRERENDERED_ENABLED = "browser.newtabpage.activity-stream.prerender";
 
 function AboutNewTabService() {
   Services.obs.addObserver(this, "quit-application-granted");
   Services.prefs.addObserver(PREF_ACTIVITY_STREAM_ENABLED, this);
+  Services.prefs.addObserver(PREF_ACTIVITY_STREAM_PRERENDERED_ENABLED, this);
   this.toggleActivityStream();
   if (IS_MAIN_PROCESS) {
     AboutNewTab.init();
   }
 }
 
 /*
  * A service that allows for the overriding, at runtime, of the newtab page's url.
@@ -66,37 +68,46 @@ function AboutNewTabService() {
  * Loading a chrome resource, or an about: URL in the redirector with either the
  * LOAD_NORMAL or LOAD_REPLACE flags yield unexpected behaviors, so a roundtrip
  * to the redirector from browser chrome is avoided.
  */
 AboutNewTabService.prototype = {
 
   _newTabURL: ABOUT_URL,
   _activityStreamEnabled: false,
+  _activityStreamPrerendered: true,
   _overridden: false,
 
   classID: Components.ID("{dfcd2adc-7867-4d3a-ba70-17501f208142}"),
   QueryInterface: XPCOMUtils.generateQI([
     Ci.nsIAboutNewTabService,
     Ci.nsIObserver
   ]),
   _xpcom_categories: [{
     service: true
   }],
 
   observe(subject, topic, data) {
     switch (topic) {
       case "nsPref:changed":
-        if (this.toggleActivityStream()) {
-          Services.obs.notifyObservers(null, "newtab-url-changed", ABOUT_URL);
+        this.toggleActivityStream();
+        Services.obs.notifyObservers(null, "newtab-url-changed", ABOUT_URL);
+        if (data === PREF_ACTIVITY_STREAM_ENABLED) {
+          if (this.toggleActivityStream()) {
+            Services.obs.notifyObservers(null, "newtab-url-changed", ABOUT_URL);
+          }
+        } else if (data === PREF_ACTIVITY_STREAM_PRERENDERED_ENABLED) {
+          this._activityStreamPrerendered = Services.prefs.getBoolPref(PREF_ACTIVITY_STREAM_PRERENDERED_ENABLED);
+          Services.obs.notifyObservers(null, "newtab-default-url-changed");
         }
         break;
       case "quit-application-granted":
         Services.obs.removeObserver(this, "quit-application-granted");
         Services.prefs.removeObserver(PREF_ACTIVITY_STREAM_ENABLED, this);
+        Services.prefs.removeObserver(PREF_ACTIVITY_STREAM_PRERENDERED_ENABLED, this);
         if (IS_MAIN_PROCESS) {
           AboutNewTab.uninit();
         }
         break;
     }
   },
 
   /**
@@ -134,16 +145,19 @@ AboutNewTabService.prototype = {
    *
    * This URL only depends on the browser.newtabpage.activity-stream.enabled pref. Overriding
    * the newtab page has no effect on the result of this function.
    *
    * @returns {String} the default newtab URL, activity-stream or regular depending on browser.newtabpage.activity-stream.enabled
    */
   get defaultURL() {
     if (this.activityStreamEnabled) {
+      if (this.activityStreamPrerendered) {
+        return this.activityStreamPrerenderedURL;
+      }
       return this.activityStreamURL;
     }
     return LOCAL_NEWTAB_URL;
   },
 
   get newTabURL() {
     return this._newTabURL;
   },
@@ -167,20 +181,28 @@ AboutNewTabService.prototype = {
   get overridden() {
     return this._overridden;
   },
 
   get activityStreamEnabled() {
     return this._activityStreamEnabled;
   },
 
+  get activityStreamPrerendered() {
+    return this._activityStreamPrerendered;
+  },
+
   get activityStreamURL() {
     return ACTIVITY_STREAM_URL;
   },
 
+  get activityStreamPrerenderedURL() {
+    return ACTIVITY_STREAM_PRERENDERED_URL;
+  },
+
   resetNewTabURL() {
     this._overridden = false;
     this._newTabURL = ABOUT_URL;
     this.toggleActivityStream(undefined, true);
     Services.obs.notifyObservers(null, "newtab-url-changed", this._newTabURL);
   }
 };
 
--- a/browser/components/newtab/nsIAboutNewTabService.idl
+++ b/browser/components/newtab/nsIAboutNewTabService.idl
@@ -30,18 +30,28 @@ interface nsIAboutNewTabService : nsISup
 
   /**
    * Returns true if the default resource is activity stream and isn't
    * overridden
    */
   readonly attribute bool activityStreamEnabled;
 
   /**
+   * Returns true if the the prerendering pref for activity stream is true
+   */
+  readonly attribute bool activityStreamPrerendered;
+
+  /**
    * Returns the activity stream resource URL for the newtab page
    */
   readonly attribute ACString activityStreamURL;
 
   /**
+   * Returns the prerendered activity stream resource URL for the newtab page
+   */
+  readonly attribute ACString activityStreamPrerenderedURL;
+
+  /**
    * Resets to the default resource and also resets the
    * overridden attribute to false.
    */
   void resetNewTabURL();
 };
--- a/browser/components/newtab/tests/xpcshell/test_AboutNewTabService.js
+++ b/browser/components/newtab/tests/xpcshell/test_AboutNewTabService.js
@@ -3,17 +3,17 @@
  */
 
 "use strict";
 
 const {utils: Cu} = Components;
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Preferences.jsm");
-
+Cu.import("resource://gre/modules/Console.jsm");
 XPCOMUtils.defineLazyServiceGetter(this, "aboutNewTabService",
                                    "@mozilla.org/browser/aboutnewtab-service;1",
                                    "nsIAboutNewTabService");
 
 const DEFAULT_HREF = aboutNewTabService.activityStreamURL;
 const DEFAULT_CHROME_URL = "chrome://browser/content/newtab/newTab.xhtml";
 const DOWNLOADS_URL = "chrome://browser/content/downloads/contentAreaDownloadsView.xul";
 
@@ -57,40 +57,55 @@ add_task(async function test_override_ac
   await notificationPromise;
   Assert.ok(aboutNewTabService.overridden, "Newtab URL should be overridden");
   Assert.equal(aboutNewTabService.newTabURL, DOWNLOADS_URL, "Newtab URL should be the custom URL");
 
   cleanup();
 });
 
 add_task(async function test_override_activity_stream_enabled() {
-  let notificationPromise;
-  // change newtab page to activity stream
-  notificationPromise = nextChangeNotificationPromise("about:newtab");
-  Services.prefs.setBoolPref("browser.newtabpage.activity-stream.enabled", true);
-  await notificationPromise;
-  let activityStreamURL = aboutNewTabService.activityStreamURL;
-  Assert.equal(aboutNewTabService.defaultURL, activityStreamURL, "Newtab URL should be the default activity stream URL");
+  let notificationPromise = await setupASPrerendered();
+
+  Assert.equal(aboutNewTabService.defaultURL, aboutNewTabService.activityStreamPrerenderedURL, "Newtab URL should be the default activity stream prerendered URL");
   Assert.ok(!aboutNewTabService.overridden, "Newtab URL should not be overridden");
   Assert.ok(aboutNewTabService.activityStreamEnabled, "Activity Stream should be enabled");
+  Assert.ok(aboutNewTabService.activityStreamPrerendered, "Activity Stream should be prerendered");
 
   // change to local newtab page while activity stream is enabled
   notificationPromise = nextChangeNotificationPromise(DEFAULT_CHROME_URL);
   aboutNewTabService.newTabURL = DEFAULT_CHROME_URL;
   await notificationPromise;
   Assert.equal(aboutNewTabService.newTabURL, DEFAULT_CHROME_URL,
                "Newtab URL set to chrome url");
   Assert.equal(aboutNewTabService.defaultURL, DEFAULT_CHROME_URL,
                "Newtab URL defaultURL set to the default chrome URL");
   Assert.ok(aboutNewTabService.overridden, "Newtab URL should be overridden");
   Assert.ok(!aboutNewTabService.activityStreamEnabled, "Activity Stream should not be enabled");
 
   cleanup();
 });
 
+add_task(async function test_default_url() {
+  await setupASPrerendered();
+  Assert.equal(aboutNewTabService.defaultURL, aboutNewTabService.activityStreamPrerenderedURL,
+               "Newtab defaultURL initially set to prerendered AS url");
+
+  // Change activity-stream.prerendered to false and wait for the required event to fire
+  await new Promise(resolve => {
+    Services.obs.addObserver(function observer(aSubject, aTopic, aData) {
+      Services.obs.removeObserver(observer, aTopic);
+      resolve();
+    }, "newtab-default-url-changed");
+    Services.prefs.setBoolPref("browser.newtabpage.activity-stream.prerender", false);
+  });
+
+  Assert.equal(aboutNewTabService.defaultURL, aboutNewTabService.activityStreamURL,
+               "Newtab defaultURL set to un-prerendered AS url after the pref has been changed");
+});
+
 /**
  * Tests reponse to updates to prefs
  */
 add_task(async function test_updates() {
   /*
    * Simulates a "cold-boot" situation, with some pref already set before testing a series
    * of changes.
    */
@@ -126,8 +141,17 @@ function nextChangeNotificationPromise(a
   return new Promise(resolve => {
     Services.obs.addObserver(function observer(aSubject, aTopic, aData) {  // jshint unused:false
       Services.obs.removeObserver(observer, aTopic);
       Assert.equal(aData, aNewURL, testMessage);
       resolve();
     }, "newtab-url-changed");
   });
 }
+
+function setupASPrerendered() {
+  let notificationPromise;
+  // change newtab page to activity stream
+  notificationPromise = nextChangeNotificationPromise("about:newtab");
+  Services.prefs.setBoolPref("browser.newtabpage.activity-stream.enabled", true);
+  Services.prefs.setBoolPref("browser.newtabpage.activity-stream.prerender", true);
+  return notificationPromise;
+}