Bug 1367462 - Preemptively get screenshots when page is open for Activity Stream Top Sites r=Ehsan
authorUrsula Sarracini
Thu, 25 May 2017 15:56:37 -0400
changeset 361098 dec37391ecf8c26962fede1c15db9b5f8c769b28
parent 361097 c88e2ecc9472a1c9983c924362e71f598635e36b
child 361099 c605d84491f8cf7529cbb084341feafe3f3f906a
push id31917
push usercbook@mozilla.com
push dateTue, 30 May 2017 09:14:52 +0000
treeherdermozilla-central@0c712d76d598 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersEhsan
bugs1367462
milestone55.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 1367462 - Preemptively get screenshots when page is open for Activity Stream Top Sites r=Ehsan MozReview-Commit-ID: cORp0JmG17
browser/base/content/browser-thumbnails.js
--- a/browser/base/content/browser-thumbnails.js
+++ b/browser/base/content/browser-thumbnails.js
@@ -6,16 +6,23 @@
  * Keeps thumbnails of open web pages up-to-date.
  */
 var gBrowserThumbnails = {
   /**
    * Pref that controls whether we can store SSL content on disk
    */
   PREF_DISK_CACHE_SSL: "browser.cache.disk_cache_ssl",
 
+  /**
+   * Pref that controls whether activity stream is enabled
+   */
+  PREF_ACTIVITY_STREAM_ENABLED: "browser.newtabpage.activity-stream.enabled",
+
+  _activityStreamEnabled: null,
+
   _captureDelayMS: 1000,
 
   /**
    * Used to keep track of disk_cache_ssl preference
    */
   _sslDiskCacheEnabled: null,
 
   /**
@@ -32,31 +39,36 @@ var gBrowserThumbnails = {
    * List of tab events we want to listen for.
    */
   _tabEvents: ["TabClose", "TabSelect"],
 
   init: function Thumbnails_init() {
     PageThumbs.addExpirationFilter(this);
     gBrowser.addTabsProgressListener(this);
     Services.prefs.addObserver(this.PREF_DISK_CACHE_SSL, this);
+    Services.prefs.addObserver(this.PREF_ACTIVITY_STREAM_ENABLED, this);
 
     this._sslDiskCacheEnabled =
       Services.prefs.getBoolPref(this.PREF_DISK_CACHE_SSL);
+    this._activityStreamEnabled =
+      Services.prefs.getBoolPref(this.PREF_ACTIVITY_STREAM_ENABLED);
 
     this._tabEvents.forEach(function(aEvent) {
       gBrowser.tabContainer.addEventListener(aEvent, this);
     }, this);
 
     this._timeouts = new WeakMap();
   },
 
   uninit: function Thumbnails_uninit() {
     PageThumbs.removeExpirationFilter(this);
     gBrowser.removeTabsProgressListener(this);
     Services.prefs.removeObserver(this.PREF_DISK_CACHE_SSL, this);
+    Services.prefs.removeObserver(this.PREF_ACTIVITY_STREAM_ENABLED, this);
+
     if (this._topSiteURLsRefreshTimer) {
       this._topSiteURLsRefreshTimer.cancel();
       this._topSiteURLsRefreshTimer = null;
     }
 
     this._tabEvents.forEach(function(aEvent) {
       gBrowser.tabContainer.removeEventListener(aEvent, this);
     }, this);
@@ -74,56 +86,71 @@ var gBrowserThumbnails = {
         break;
       case "TabClose": {
         this._clearTimeout(aEvent.target.linkedBrowser);
         break;
       }
     }
   },
 
-  observe: function Thumbnails_observe() {
-    this._sslDiskCacheEnabled =
-      Services.prefs.getBoolPref(this.PREF_DISK_CACHE_SSL);
+  observe: function Thumbnails_observe(subject, topic, data) {
+    switch (data) {
+      case this.PREF_DISK_CACHE_SSL:
+        this._sslDiskCacheEnabled =
+          Services.prefs.getBoolPref(this.PREF_DISK_CACHE_SSL);
+        break;
+      case this.PREF_ACTIVITY_STREAM_ENABLED:
+        this._activityStreamEnabled =
+          Services.prefs.getBoolPref(this.PREF_ACTIVITY_STREAM_ENABLED);
+        // Get the new top sites
+        XPCOMUtils.defineLazyGetter(this, "_topSiteURLs", getTopSiteURLs);
+        break;
+    }
   },
 
+  /**
+   * clearTopSiteURLCache is only ever called if we've created an nsITimer,
+   * which only happens if we've loaded the tiles top sites. Therefore we only
+   * need to clear the tiles top sites (and not activity stream's top sites)
+   */
   clearTopSiteURLCache: function Thumbnails_clearTopSiteURLCache() {
     if (this._topSiteURLsRefreshTimer) {
       this._topSiteURLsRefreshTimer.cancel();
       this._topSiteURLsRefreshTimer = null;
     }
     // Delete the defined property
     delete this._topSiteURLs;
-    XPCOMUtils.defineLazyGetter(this, "_topSiteURLs",
-                                Thumbnails_getTopSiteURLs);
+    XPCOMUtils.defineLazyGetter(this, "_topSiteURLs", getTopSiteURLs);
   },
 
   notify: function Thumbnails_notify(timer) {
     gBrowserThumbnails._topSiteURLsRefreshTimer = null;
     gBrowserThumbnails.clearTopSiteURLCache();
   },
 
-  filterForThumbnailExpiration:
-  function Thumbnails_filterForThumbnailExpiration(aCallback) {
-    aCallback(this._topSiteURLs);
+  async filterForThumbnailExpiration(aCallback) {
+    const topSites = await this._topSiteURLs;
+    aCallback(topSites);
   },
 
   /**
    * State change progress listener for all tabs.
    */
   onStateChange: function Thumbnails_onStateChange(aBrowser, aWebProgress,
                                                    aRequest, aStateFlags, aStatus) {
     if (aStateFlags & Ci.nsIWebProgressListener.STATE_STOP &&
         aStateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK)
       this._delayedCapture(aBrowser);
   },
 
-  _capture: function Thumbnails_capture(aBrowser) {
+  async _capture(aBrowser) {
     // Only capture about:newtab top sites.
+    const topSites = await this._topSiteURLs;
     if (!aBrowser.currentURI ||
-        this._topSiteURLs.indexOf(aBrowser.currentURI.spec) == -1)
+        topSites.indexOf(aBrowser.currentURI.spec) == -1)
       return;
     this._shouldCapture(aBrowser, function(aResult) {
       if (aResult) {
         PageThumbs.captureAndStoreIfStale(aBrowser);
       }
     });
   },
 
@@ -154,26 +181,30 @@ var gBrowserThumbnails = {
     if (this._timeouts.has(aBrowser)) {
       aBrowser.removeEventListener("scroll", this);
       clearTimeout(this._timeouts.get(aBrowser));
       this._timeouts.delete(aBrowser);
     }
   }
 };
 
-function Thumbnails_getTopSiteURLs() {
-  // The _topSiteURLs getter can be expensive to run, but its return value can
-  // change frequently on new profiles, so as a compromise we cache its return
-  // value as a lazy getter for 1 minute every time it's called.
-  gBrowserThumbnails._topSiteURLsRefreshTimer =
-    Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
-  gBrowserThumbnails._topSiteURLsRefreshTimer.initWithCallback(gBrowserThumbnails,
-                                                               60 * 1000,
-                                                               Ci.nsITimer.TYPE_ONE_SHOT);
-  return NewTabUtils.links.getLinks().reduce((urls, link) => {
-    if (link)
-      urls.push(link.url);
+async function getTopSiteURLs() {
+  let sites = [];
+  if (gBrowserThumbnails._activityStreamEnabled) {
+    sites = await NewTabUtils.activityStreamLinks.getTopSites();
+  } else {
+    // The _topSiteURLs getter can be expensive to run, but its return value can
+    // change frequently on new profiles, so as a compromise we cache its return
+    // value as a lazy getter for 1 minute every time it's called.
+    gBrowserThumbnails._topSiteURLsRefreshTimer =
+      Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
+    gBrowserThumbnails._topSiteURLsRefreshTimer.initWithCallback(gBrowserThumbnails,
+                                                                 60 * 1000,
+                                                                 Ci.nsITimer.TYPE_ONE_SHOT);
+    sites = NewTabUtils.links.getLinks();
+  }
+  return sites.reduce((urls, link) => {
+    if (link) urls.push(link.url);
     return urls;
   }, []);
 }
 
-XPCOMUtils.defineLazyGetter(gBrowserThumbnails, "_topSiteURLs",
-                            Thumbnails_getTopSiteURLs);
+XPCOMUtils.defineLazyGetter(gBrowserThumbnails, "_topSiteURLs", getTopSiteURLs);