Bug 786674 - Forward browser chrome zoom events to the PDF.js viewer. r=bdahl,Gijs

Differential Revision: https://phabricator.services.mozilla.com/D26338

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Jonas Jenwald 2019-07-15 13:06:37 +00:00
Родитель 2eabf6d0f6
Коммит 048ab60385
3 изменённых файлов: 133 добавлений и 0 удалений

Просмотреть файл

@ -167,6 +167,10 @@ var FullZoom = {
return;
}
if (this._isPDFViewer(browser)) {
return;
}
let ctxt = this._loadContextFromBrowser(browser);
let domain = this._cps2.extractDomain(browser.currentURI.spec);
if (aGroup) {
@ -268,6 +272,17 @@ var FullZoom = {
return;
}
// The PDF viewer zooming isn't handled by `ZoomManager`, ensure that the
// browser zoom level always gets reset on load.
if (this._isPDFViewer(browser)) {
this._applyPrefToZoom(
undefined,
browser,
this._notifyOnLocationChange.bind(this, browser)
);
return;
}
// See if the zoom pref is cached.
let ctxt = this._loadContextFromBrowser(browser);
let pref = this._cps2.getCachedByDomainAndName(aURI.spec, this.name, ctxt);
@ -319,6 +334,8 @@ var FullZoom = {
let browser = gBrowser.selectedBrowser;
if (browser.currentURI.spec.startsWith("about:reader")) {
browser.messageManager.sendAsyncMessage("Reader:ZoomOut");
} else if (this._isPDFViewer(browser)) {
browser.messageManager.sendAsyncMessage("PDFJS:ZoomOut");
} else {
ZoomManager.reduce();
this._ignorePendingZoomAccesses(browser);
@ -334,6 +351,8 @@ var FullZoom = {
let browser = gBrowser.selectedBrowser;
if (browser.currentURI.spec.startsWith("about:reader")) {
browser.messageManager.sendAsyncMessage("Reader:ZoomIn");
} else if (this._isPDFViewer(browser)) {
browser.messageManager.sendAsyncMessage("PDFJS:ZoomIn");
} else {
ZoomManager.enlarge();
this._ignorePendingZoomAccesses(browser);
@ -346,6 +365,9 @@ var FullZoom = {
* point value, where 1 is the default zoom level.
*/
setZoom(value, browser = gBrowser.selectedBrowser) {
if (this._isPDFViewer(browser)) {
return;
}
ZoomManager.setZoomForBrowser(browser, value);
this._ignorePendingZoomAccesses(browser);
this._applyZoomToPref(browser);
@ -360,6 +382,8 @@ var FullZoom = {
reset: function FullZoom_reset(browser = gBrowser.selectedBrowser) {
if (browser.currentURI.spec.startsWith("about:reader")) {
browser.messageManager.sendAsyncMessage("Reader:ResetZoom");
} else if (this._isPDFViewer(browser)) {
browser.messageManager.sendAsyncMessage("PDFJS:ZoomReset");
} else {
let token = this._getBrowserToken(browser);
let result = this._getGlobalValue(browser).then(value => {
@ -635,4 +659,12 @@ var FullZoom = {
}
Services.tm.dispatchToMainThread(callback);
},
_isPDFViewer(browser) {
return !!(
browser.contentPrincipal &&
browser.contentPrincipal.URI &&
browser.contentPrincipal.URI.spec == "resource://pdf.js/web/viewer.html"
);
},
};

Просмотреть файл

@ -930,6 +930,45 @@ class FindEventManager {
}
}
/**
* Forwards zoom events from the browser chrome to the currently active viewer.
*/
class ZoomEventManager {
constructor(contentWindow) {
this.contentWindow = contentWindow;
this.winmm = contentWindow.docShell.messageManager;
}
bind() {
this.contentWindow.addEventListener(
"unload",
evt => {
this.unbind();
},
{ once: true }
);
this.winmm.addMessageListener("PDFJS:ZoomIn", this);
this.winmm.addMessageListener("PDFJS:ZoomOut", this);
this.winmm.addMessageListener("PDFJS:ZoomReset", this);
}
receiveMessage(msg) {
const type = msg.name.split("PDFJS:")[1].toLowerCase();
const contentWindow = this.contentWindow;
const forward = contentWindow.document.createEvent("CustomEvent");
forward.initCustomEvent(type, true, true, null);
contentWindow.dispatchEvent(forward);
}
unbind() {
this.winmm.removeMessageListener("PDFJS:ZoomIn", this);
this.winmm.removeMessageListener("PDFJS:ZoomOut", this);
this.winmm.removeMessageListener("PDFJS:ZoomReset", this);
}
}
function PdfStreamConverter() {}
PdfStreamConverter.prototype = {
@ -1109,6 +1148,9 @@ PdfStreamConverter.prototype = {
var findEventManager = new FindEventManager(domWindow);
findEventManager.bind();
}
const zoomEventManager = new ZoomEventManager(domWindow);
zoomEventManager.bind();
listener.onStopRequest(aRequest, statusCode);
if (domWindow.frameElement) {

Просмотреть файл

@ -189,3 +189,62 @@ add_task(async function test() {
}
);
});
async function waitForRenderAndGetWidth(newTabBrowser) {
return ContentTask.spawn(newTabBrowser, null, async function() {
function waitForRender(document) {
return new Promise(resolve => {
document.addEventListener(
"pagerendered",
function onPageRendered(e) {
if (e.detail.pageNumber !== 1) {
return;
}
document.removeEventListener("pagerendered", onPageRendered, true);
resolve();
},
true
);
});
}
// check that PDF is opened with internal viewer
Assert.ok(
content.document.querySelector("div#viewer"),
"document content has viewer UI"
);
await waitForRender(content.document);
return parseInt(
content.document.querySelector("div.page[data-page-number='1']").style
.width
);
});
}
add_task(async function test_browser_zoom() {
await BrowserTestUtils.withNewTab(
{ gBrowser, url: "about:blank" },
async function(newTabBrowser) {
await waitForPdfJS(newTabBrowser, TESTROOT + "file_pdfjs_test.pdf");
const initialWidth = await waitForRenderAndGetWidth(newTabBrowser);
// Zoom in
FullZoom.enlarge();
let newWidth = await waitForRenderAndGetWidth(newTabBrowser);
ok(newWidth > initialWidth, "Zoom in makes the page bigger.");
// Reset
FullZoom.reset();
newWidth = await waitForRenderAndGetWidth(newTabBrowser);
is(newWidth, initialWidth, "Zoom reset restores page.");
// Zoom out
FullZoom.reduce();
newWidth = await waitForRenderAndGetWidth(newTabBrowser);
ok(newWidth < initialWidth, "Zoom out makes the page smaller.");
}
);
});