Bug 1239119 - NewTab webchannel messages for thumbnails r=ursula
authorOlivier Yiptong <olivier@olivieryiptong.com>
Tue, 08 Mar 2016 14:21:07 -0500
changeset 291406 14ebae70ac3783e47cc16dc94817f9ea0daaf1be
parent 291405 e17ded471c3ff88d278ac451f42fe62b00fc6509
child 291407 ef48e50010010bd30ea94ce07116d82b12afebd7
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersursula
bugs1239119
milestone48.0a1
Bug 1239119 - NewTab webchannel messages for thumbnails r=ursula MozReview-Commit-ID: JyW9kmxY9OE
browser/components/newtab/NewTabMessages.jsm
browser/components/newtab/tests/browser/browser.ini
browser/components/newtab/tests/browser/browser_newtabmessages.js
browser/components/newtab/tests/browser/newtabmessages_preview.html
--- a/browser/components/newtab/NewTabMessages.jsm
+++ b/browser/components/newtab/NewTabMessages.jsm
@@ -8,51 +8,66 @@
 /* exported NewTabMessages */
 
 "use strict";
 
 const {utils: Cu} = Components;
 Cu.import("resource://gre/modules/Preferences.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
+XPCOMUtils.defineLazyModuleGetter(this, "PreviewProvider",
+                                  "resource:///modules/PreviewProvider.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "NewTabPrefsProvider",
                                   "resource:///modules/NewTabPrefsProvider.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "NewTabWebChannel",
                                   "resource:///modules/NewTabWebChannel.jsm");
 
 this.EXPORTED_SYMBOLS = ["NewTabMessages"];
 
 const PREF_ENABLED = "browser.newtabpage.remote";
 
 // Action names are from the content's perspective. in from chrome == out from content
 // Maybe replace the ACTION objects by a bi-directional Map a bit later?
 const ACTIONS = {
   prefs: {
     inPrefs: "REQUEST_PREFS",
     outPrefs: "RECEIVE_PREFS",
-    action_types: new Set(["REQUEST_PREFS", "RECEIVE_PREFS"]),
-  }
+    action_types: new Set(["REQUEST_PREFS"]),
+  },
+  preview: {
+    inThumb: "REQUEST_THUMB",
+    outThumb: "RECEIVE_THUMB",
+    action_types: new Set(["REQUEST_THUMB"]),
+  },
 };
 
 let NewTabMessages = {
 
   _prefs: {},
 
   /** NEWTAB EVENT HANDLERS **/
 
   /*
    * Return to the originator all newtabpage prefs. A point-to-point request.
    */
   handlePrefRequest(actionName, {target}) {
-    if (ACTIONS.prefs.action_types.has(actionName)) {
+    if (ACTIONS.prefs.inPrefs === actionName) {
       let results = NewTabPrefsProvider.prefs.newtabPagePrefs;
       NewTabWebChannel.send(ACTIONS.prefs.outPrefs, results, target);
     }
   },
 
+  handlePreviewRequest(actionName, {data, target}) {
+    if (ACTIONS.preview.inThumb === actionName) {
+      PreviewProvider.getThumbnail(data).then(imgData => {
+        NewTabWebChannel.send(ACTIONS.preview.outThumb, {url: data, imgData}, target);
+      });
+    }
+  },
+
   /*
    * Broadcast preference changes to all open newtab pages
    */
   handlePrefChange(actionName, value) {
     let prefChange = {};
     prefChange[actionName] = value;
     NewTabWebChannel.broadcast(ACTIONS.prefs.outPrefs, prefChange);
   },
@@ -63,38 +78,46 @@ let NewTabMessages = {
         this.uninit();
       } else if (!this._prefs.enabled && value) {
         this.init();
       }
     }
   },
 
   init() {
+    this.handlePrefRequest = this.handlePrefRequest.bind(this);
+    this.handlePreviewRequest = this.handlePreviewRequest.bind(this);
+    this.handlePrefChange = this.handlePrefChange.bind(this);
+    this._handleEnabledChange = this._handleEnabledChange.bind(this);
+
     NewTabPrefsProvider.prefs.init();
     NewTabWebChannel.init();
 
     this._prefs.enabled = Preferences.get(PREF_ENABLED, false);
 
     if (this._prefs.enabled) {
-      NewTabWebChannel.on(ACTIONS.prefs.inPrefs, this.handlePrefRequest.bind(this));
-      NewTabPrefsProvider.prefs.on(PREF_ENABLED, this._handleEnabledChange.bind(this));
+      NewTabWebChannel.on(ACTIONS.prefs.inPrefs, this.handlePrefRequest);
+      NewTabWebChannel.on(ACTIONS.preview.inThumb, this.handlePreviewRequest);
+
+      NewTabPrefsProvider.prefs.on(PREF_ENABLED, this._handleEnabledChange);
 
       for (let pref of NewTabPrefsProvider.newtabPagePrefSet) {
-        NewTabPrefsProvider.prefs.on(pref, this.handlePrefChange.bind(this));
+        NewTabPrefsProvider.prefs.on(pref, this.handlePrefChange);
       }
     }
   },
 
   uninit() {
     this._prefs.enabled = Preferences.get(PREF_ENABLED, false);
 
     if (this._prefs.enabled) {
       NewTabPrefsProvider.prefs.off(PREF_ENABLED, this._handleEnabledChange);
 
       NewTabWebChannel.off(ACTIONS.prefs.inPrefs, this.handlePrefRequest);
+      NewTabWebChannel.off(ACTIONS.prefs.inThumb, this.handlePreviewRequest);
       for (let pref of NewTabPrefsProvider.newtabPagePrefSet) {
         NewTabPrefsProvider.prefs.off(pref, this.handlePrefChange);
       }
     }
 
     NewTabPrefsProvider.prefs.uninit();
     NewTabWebChannel.uninit();
   }
--- a/browser/components/newtab/tests/browser/browser.ini
+++ b/browser/components/newtab/tests/browser/browser.ini
@@ -1,12 +1,13 @@
 [DEFAULT]
 support-files =
+  blue_page.html
   dummy_page.html
   newtabwebchannel_basic.html
   newtabmessages_prefs.html
-  blue_page.html
+  newtabmessages_preview.html
 
 [browser_PreviewProvider.js]
 [browser_remotenewtab_pageloads.js]
 [browser_newtab_overrides.js]
 [browser_newtabmessages.js]
 [browser_newtabwebchannel.js]
--- a/browser/components/newtab/tests/browser/browser_newtabmessages.js
+++ b/browser/components/newtab/tests/browser/browser_newtabmessages.js
@@ -49,8 +49,37 @@ add_task(function* prefMessages_request(
         resolve();
       });
     });
     Preferences.set("browser.newtabpage.enhanced", false);
     yield prefChangeAck;
   });
   cleanup();
 });
+
+/*
+ * Sanity tests for preview messages
+ */
+add_task(function* previewMessages_request() {
+  setup();
+  var oldEnabledPref = Services.prefs.getBoolPref("browser.pagethumbnails.capturing_disabled");
+  Services.prefs.setBoolPref("browser.pagethumbnails.capturing_disabled", false);
+
+  let testURL = "https://example.com/browser/browser/components/newtab/tests/browser/newtabmessages_preview.html";
+
+  let tabOptions = {
+    gBrowser,
+    url: testURL
+  };
+
+  let previewResponseAck = new Promise(resolve => {
+    NewTabWebChannel.once("responseAck", () => {
+      ok(true, "a request response has been received");
+      resolve();
+    });
+  });
+
+  yield BrowserTestUtils.withNewTab(tabOptions, function*() {
+    yield previewResponseAck;
+  });
+  cleanup();
+  Services.prefs.setBoolPref("browser.pagethumbnails.capturing_disabled", oldEnabledPref);
+});
new file mode 100644
--- /dev/null
+++ b/browser/components/newtab/tests/browser/newtabmessages_preview.html
@@ -0,0 +1,37 @@
+<html>
+  <head>
+    <meta charset="utf8">
+    <title>Newtab WebChannel test</title>
+  </head>
+  <body>
+    <script>
+      let thumbURL = "https://example.com/browser/browser/components/newtab/tests/browser/blue_page.html";
+
+      window.addEventListener("WebChannelMessageToContent", function(e) {
+        if (e.detail.message && e.detail.message.type === "RECEIVE_THUMB") {
+          if (e.detail.message.data.imgData && e.detail.message.data.url === thumbURL) {
+            let reply = new window.CustomEvent("WebChannelMessageToChrome", {
+              detail: {
+                id: "newtab",
+                message: JSON.stringify({type: "responseAck"}),
+              }
+            });
+            window.dispatchEvent(reply);
+          }
+        }
+      }, true);
+
+      document.onreadystatechange = function () {
+        if (document.readyState === "complete") {
+          let msg = new window.CustomEvent("WebChannelMessageToChrome", {
+            detail: {
+              id: "newtab",
+              message: JSON.stringify({type: "REQUEST_THUMB", data: thumbURL}),
+            }
+          });
+          window.dispatchEvent(msg);
+        };
+      }
+    </script>
+  </body>
+</html>