Bug 870100 (part 2) - about:newtab observes notifications for new page thumbnails. r=ttaubert
authorMark Hammond <mhammond@skippinet.com.au>
Tue, 23 Jul 2013 17:41:12 +1000
changeset 151833 1accaa61b2579799210048a468b7253f947e9189
parent 151832 d493a47e087ad3e1a26af8e043f9bc8942201191
child 151834 96d374c8f83369c6c9e1bc5b1c7c8c58d47b3ebd
push id2859
push userakeybl@mozilla.com
push dateMon, 16 Sep 2013 19:14:59 +0000
treeherdermozilla-beta@87d3c51cd2bf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersttaubert
bugs870100
milestone25.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 870100 (part 2) - about:newtab observes notifications for new page thumbnails. r=ttaubert
browser/base/content/newtab/page.js
browser/base/content/newtab/sites.js
toolkit/modules/NewTabUtils.jsm
--- a/browser/base/content/newtab/page.js
+++ b/browser/base/content/newtab/page.js
@@ -29,25 +29,33 @@ let gPage = {
       this._init();
 
     this._updateAttributes(enabled);
   },
 
   /**
    * Listens for notifications specific to this page.
    */
-  observe: function Page_observe() {
-    let enabled = gAllPages.enabled;
-    this._updateAttributes(enabled);
+  observe: function Page_observe(aSubject, aTopic, aData) {
+    if (aTopic == "nsPref:changed") {
+      let enabled = gAllPages.enabled;
+      this._updateAttributes(enabled);
 
-    // Initialize the whole page if we haven't done that, yet.
-    if (enabled) {
-      this._init();
-    } else {
-      gUndoDialog.hide();
+      // Initialize the whole page if we haven't done that, yet.
+      if (enabled) {
+        this._init();
+      } else {
+        gUndoDialog.hide();
+      }
+    } else if (aTopic == "page-thumbnail:create" && gGrid.ready) {
+      for (let site of gGrid.sites) {
+        if (site && site.url === aData) {
+          site.refreshThumbnail();
+        }
+      }
     }
   },
 
   /**
    * Updates the whole page and the grid when the storage has changed.
    */
   update: function Page_update() {
     // The grid might not be ready yet as we initialize it asynchronously.
--- a/browser/base/content/newtab/sites.js
+++ b/browser/base/content/newtab/sites.js
@@ -126,19 +126,33 @@ Site.prototype = {
 
     let link = this._querySelector(".newtab-link");
     link.setAttribute("title", tooltip);
     link.setAttribute("href", url);
     this._querySelector(".newtab-title").textContent = title;
 
     if (this.isPinned())
       this._updateAttributes(true);
+    // request a staleness check for the thumbnail, which will cause page.js
+    // to be notified and call our refreshThumbnail() method.
+    PageThumbs.captureIfStale(this.url);
+    // but still display whatever thumbnail might be available now.
+    this.refreshThumbnail();
+  },
 
+  /**
+   * Refreshes the thumbnail for the site.
+   */
+  refreshThumbnail: function Site_refreshThumbnail() {
     let thumbnailURL = PageThumbs.getThumbnailURL(this.url);
     let thumbnail = this._querySelector(".newtab-thumbnail");
+    // if this is being called due to the thumbnail being updated we will
+    // be setting it to the same value it had before.  To be confident the
+    // change wont be optimized away we remove the property first.
+    thumbnail.style.removeProperty("backgroundImage");
     thumbnail.style.backgroundImage = "url(" + thumbnailURL + ")";
   },
 
   /**
    * Adds event handlers for the site and its buttons.
    */
   _addEventHandlers: function Site_addEventHandlers() {
     // Register drag-and-drop event handlers.
--- a/toolkit/modules/NewTabUtils.jsm
+++ b/toolkit/modules/NewTabUtils.jsm
@@ -249,35 +249,36 @@ let AllPages = {
     this._pages.forEach(function (aPage) {
       if (aExceptPage != aPage)
         aPage.update();
     });
   },
 
   /**
    * Implements the nsIObserver interface to get notified when the preference
-   * value changes.
+   * value changes or when a new copy of a page thumbnail is available.
    */
-  observe: function AllPages_observe() {
-    // Clear the cached value.
-    this._enabled = null;
-
-    let args = Array.slice(arguments);
-
+  observe: function AllPages_observe(aSubject, aTopic, aData) {
+    if (aTopic == "nsPref:changed") {
+      // Clear the cached value.
+      this._enabled = null;
+    }
+    // and all notifications get forwarded to each page.
     this._pages.forEach(function (aPage) {
-      aPage.observe.apply(aPage, args);
+      aPage.observe(aSubject, aTopic, aData);
     }, this);
   },
 
   /**
-   * Adds a preference observer and turns itself into a no-op after the first
-   * invokation.
+   * Adds a preference and new thumbnail observer and turns itself into a
+   * no-op after the first invokation.
    */
   _addObserver: function AllPages_addObserver() {
     Services.prefs.addObserver(PREF_NEWTAB_ENABLED, this, true);
+    Services.obs.addObserver(this, "page-thumbnail:create", true);
     this._addObserver = function () {};
   },
 
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
                                          Ci.nsISupportsWeakReference])
 };
 
 /**