Bug 1633270 - view application/octet-stream PDFs in PDF.JS and add a pref to turn this off, r=jaws

Differential Revision: https://phabricator.services.mozilla.com/D75582
This commit is contained in:
Gijs Kruitbosch 2020-05-18 15:41:15 +00:00
Родитель 34a6029cd6
Коммит 998df7c063
7 изменённых файлов: 84 добавлений и 2 удалений

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

@ -1372,6 +1372,9 @@ pref("pdfjs.firstRun", true);
pref("pdfjs.previousHandler.preferredAction", 0);
pref("pdfjs.previousHandler.alwaysAskBeforeHandling", false);
// Try to convert PDFs sent as octet-stream
pref("pdfjs.handleOctetStream", true);
// Is the sidebar positioned ahead of the content browser
pref("sidebar.position_start", true);

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

@ -10,6 +10,8 @@ Classes = [
'contract_ids': [
'@mozilla.org/streamconv;1?from=application/pdf&to=*/*',
'@mozilla.org/streamconv;1?from=application/pdf&to=text/html',
'@mozilla.org/streamconv;1?from=application/octet-stream&to=*/*',
'@mozilla.org/streamconv;1?from=application/octet-stream&to=text/html',
],
'jsm': 'resource:///modules/pdfjs.js',
'constructor': 'StreamConverterFactory',

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

@ -1031,6 +1031,13 @@ PdfStreamConverter.prototype = {
// nsIStreamConverter::asyncConvertData
asyncConvertData(aFromType, aToType, aListener, aCtxt) {
if (aCtxt && aCtxt instanceof Ci.nsIChannel) {
aCtxt.QueryInterface(Ci.nsIChannel);
}
// We need to check if we're supposed to convert here, because not all
// asyncConvertData consumers will call getConvertedType first:
this.getConvertedType(aFromType, aCtxt);
// Store the listener passed to us
this.listener = aListener;
},
@ -1109,13 +1116,32 @@ PdfStreamConverter.prototype = {
getConvertedType(aFromType, aChannel) {
const HTML = "text/html";
let channelURI = aChannel?.URI;
// We can be invoked for application/octet-stream; check if we want the
// channel first:
if (aFromType != "application/pdf") {
let isPDF = channelURI?.QueryInterface(Ci.nsIURL).fileExtension == "pdf";
let typeIsOctetStream = aFromType == "application/octet-stream";
if (
!isPDF ||
!typeIsOctetStream ||
!getBoolPref(PREF_PREFIX + ".handleOctetStream", false)
) {
throw new Components.Exception(
"Ignore PDF.js for this download.",
Cr.NS_ERROR_FAILURE
);
}
// fall through, this appears to be a pdf.
}
if (this._validateAndMaybeUpdatePDFPrefs()) {
return HTML;
}
// Hm, so normally, no pdfjs. However... if this is a file: channel loaded
// with system principal, load it anyway:
if (aChannel && aChannel.URI.schemeIs("file")) {
let triggeringPrincipal = aChannel?.loadInfo?.triggeringPrincipal;
if (channelURI?.schemeIs("file")) {
let triggeringPrincipal = aChannel.loadInfo?.triggeringPrincipal;
if (triggeringPrincipal?.isSystemPrincipal) {
return HTML;
}

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

@ -6,6 +6,10 @@ support-files =
[browser_pdfjs_force_opening_files.js]
[browser_pdfjs_main.js]
[browser_pdfjs_navigation.js]
[browser_pdfjs_octet_stream.js]
support-files =
file_pdfjs_object_stream.pdf
file_pdfjs_object_stream.pdf^headers^
[browser_pdfjs_savedialog.js]
skip-if = verify
[browser_pdfjs_views.js]

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

@ -0,0 +1,45 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const TESTROOT = getRootDirectory(gTestPath).replace(
"chrome://mochitests/content/",
"http://mochi.test:8888/"
);
/**
* Check that if we open a PDF with octet-stream mimetype, it can load
* PDF.js .
*/
add_task(async function test_octet_stream_opens_pdfjs() {
await SpecialPowers.pushPrefEnv({ set: [["pdfjs.handleOctetStream", true]] });
// Get a ref to the pdf we want to open.
let pdfURL = TESTROOT + "file_pdfjs_object_stream.pdf";
let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
let handlerInfo = mimeService.getFromTypeAndExtension(
"application/pdf",
"pdf"
);
// Make sure pdf.js is the default handler.
is(
handlerInfo.alwaysAskBeforeHandling,
false,
"pdf handler defaults to always-ask is false"
);
is(
handlerInfo.preferredAction,
Ci.nsIHandlerInfo.handleInternally,
"pdf handler defaults to internal"
);
await BrowserTestUtils.withNewTab(
{ gBrowser, url: "about:blank" },
async function(newTabBrowser) {
await waitForPdfJS(newTabBrowser, pdfURL);
is(newTabBrowser.currentURI.spec, pdfURL, "Should load pdfjs");
}
);
});

Двоичные данные
browser/extensions/pdfjs/test/file_pdfjs_object_stream.pdf Normal file

Двоичный файл не отображается.

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

@ -0,0 +1,2 @@
Content-Type: application/octet-stream