Bug 1239119 - NewTab webchannel messages for thumbnails r=ursula

MozReview-Commit-ID: JyW9kmxY9OE

--HG--
extra : rebase_source : 934bcce36e87aca6dbe6d4cbf211f955c15fb21c
This commit is contained in:
Olivier Yiptong 2016-03-08 14:21:07 -05:00
parent e626493cd5
commit 10471cf5de
4 changed files with 97 additions and 7 deletions

View File

@ -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);
}

View File

@ -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]

View File

@ -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);
});

View File

@ -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>