From 10471cf5de834c29ea53057ebb4943d8d8007343 Mon Sep 17 00:00:00 2001 From: Olivier Yiptong Date: Tue, 8 Mar 2016 14:21:07 -0500 Subject: [PATCH] Bug 1239119 - NewTab webchannel messages for thumbnails r=ursula MozReview-Commit-ID: JyW9kmxY9OE --HG-- extra : rebase_source : 934bcce36e87aca6dbe6d4cbf211f955c15fb21c --- browser/components/newtab/NewTabMessages.jsm | 35 +++++++++++++++--- .../newtab/tests/browser/browser.ini | 3 +- .../tests/browser/browser_newtabmessages.js | 29 +++++++++++++++ .../tests/browser/newtabmessages_preview.html | 37 +++++++++++++++++++ 4 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 browser/components/newtab/tests/browser/newtabmessages_preview.html diff --git a/browser/components/newtab/NewTabMessages.jsm b/browser/components/newtab/NewTabMessages.jsm index eb4cfa40e0be..d52e57eca98c 100644 --- a/browser/components/newtab/NewTabMessages.jsm +++ b/browser/components/newtab/NewTabMessages.jsm @@ -13,6 +13,8 @@ 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", @@ -28,8 +30,13 @@ 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 = { @@ -42,12 +49,20 @@ let NewTabMessages = { * 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 */ @@ -68,17 +83,24 @@ let NewTabMessages = { }, 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); } } }, @@ -90,6 +112,7 @@ let NewTabMessages = { 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); } diff --git a/browser/components/newtab/tests/browser/browser.ini b/browser/components/newtab/tests/browser/browser.ini index a04ec639b03f..17a442399c4d 100644 --- a/browser/components/newtab/tests/browser/browser.ini +++ b/browser/components/newtab/tests/browser/browser.ini @@ -1,9 +1,10 @@ [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] diff --git a/browser/components/newtab/tests/browser/browser_newtabmessages.js b/browser/components/newtab/tests/browser/browser_newtabmessages.js index 6b39e15df921..88dac468e2bf 100644 --- a/browser/components/newtab/tests/browser/browser_newtabmessages.js +++ b/browser/components/newtab/tests/browser/browser_newtabmessages.js @@ -54,3 +54,32 @@ add_task(function* prefMessages_request() { }); 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); +}); diff --git a/browser/components/newtab/tests/browser/newtabmessages_preview.html b/browser/components/newtab/tests/browser/newtabmessages_preview.html new file mode 100644 index 000000000000..99be6f0cf8fe --- /dev/null +++ b/browser/components/newtab/tests/browser/newtabmessages_preview.html @@ -0,0 +1,37 @@ + + + + Newtab WebChannel test + + + + +