Move the disableCreateObjectURL option from the global PDFJS object and into getDocument instead

This commit is contained in:
Jonas Jenwald 2018-02-17 22:51:03 +01:00
parent 05c05bdef5
commit 1d03ad0060
11 changed files with 54 additions and 36 deletions

View File

@ -178,6 +178,9 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) {
* The default value is `false`. * The default value is `false`.
* NOTE: It is also necessary to disable streaming, see above, * NOTE: It is also necessary to disable streaming, see above,
* in order for disabling of pre-fetching to work correctly. * in order for disabling of pre-fetching to work correctly.
* @property {boolean} disableCreateObjectURL - (optional) Disable the use of
* `URL.createObjectURL`, for compatibility with older browsers.
* The default value is `false`.
*/ */
/** /**
@ -289,6 +292,10 @@ function getDocument(src) {
if (typeof params.disableAutoFetch !== 'boolean') { if (typeof params.disableAutoFetch !== 'boolean') {
params.disableAutoFetch = false; params.disableAutoFetch = false;
} }
if (typeof params.disableCreateObjectURL !== 'boolean') {
params.disableCreateObjectURL =
apiCompatibilityParams.disableCreateObjectURL || false;
}
// Set the main-thread verbosity level. // Set the main-thread verbosity level.
setVerbosityLevel(params.verbosity); setVerbosityLevel(params.verbosity);
@ -373,7 +380,7 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
}, },
maxImageSize: source.maxImageSize, maxImageSize: source.maxImageSize,
disableFontFace: source.disableFontFace, disableFontFace: source.disableFontFace,
disableCreateObjectURL: getDefaultSetting('disableCreateObjectURL'), disableCreateObjectURL: source.disableCreateObjectURL,
postMessageTransfers: worker.postMessageTransfers, postMessageTransfers: worker.postMessageTransfers,
docBaseUrl: source.docBaseUrl, docBaseUrl: source.docBaseUrl,
nativeImageDecoderSupport: source.nativeImageDecoderSupport, nativeImageDecoderSupport: source.nativeImageDecoderSupport,
@ -2148,6 +2155,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
disableRange: params.disableRange, disableRange: params.disableRange,
disableStream: params.disableStream, disableStream: params.disableStream,
disableAutoFetch: params.disableAutoFetch, disableAutoFetch: params.disableAutoFetch,
disableCreateObjectURL: params.disableCreateObjectURL,
}); });
}, },
}; };

View File

@ -336,8 +336,6 @@ function getDefaultSetting(id) {
switch (id) { switch (id) {
case 'pdfBug': case 'pdfBug':
return globalSettings ? globalSettings.pdfBug : false; return globalSettings ? globalSettings.pdfBug : false;
case 'disableCreateObjectURL':
return globalSettings ? globalSettings.disableCreateObjectURL : false;
default: default:
throw new Error('Unknown default setting: ' + id); throw new Error('Unknown default setting: ' + id);
} }

View File

@ -14,9 +14,9 @@
*/ */
import { import {
createBlob, createObjectURL, createPromiseCapability, InvalidPDFException, createBlob, createPromiseCapability, InvalidPDFException, isLittleEndian,
isLittleEndian, MissingPDFException, OPS, PageViewport, PasswordException, MissingPDFException, OPS, PageViewport, PasswordException, PasswordResponses,
PasswordResponses, removeNullCharacters, shadow, UnexpectedResponseException, removeNullCharacters, shadow, UnexpectedResponseException,
UnknownErrorException, UNSUPPORTED_FEATURES, Util UnknownErrorException, UNSUPPORTED_FEATURES, Util
} from '../shared/util'; } from '../shared/util';
import { import {
@ -45,9 +45,6 @@ PDFJS.OPS = OPS;
PDFJS.UNSUPPORTED_FEATURES = UNSUPPORTED_FEATURES; PDFJS.UNSUPPORTED_FEATURES = UNSUPPORTED_FEATURES;
PDFJS.shadow = shadow; PDFJS.shadow = shadow;
PDFJS.createBlob = createBlob; PDFJS.createBlob = createBlob;
PDFJS.createObjectURL = function PDFJS_createObjectURL(data, contentType) {
return createObjectURL(data, contentType, PDFJS.disableCreateObjectURL);
};
Object.defineProperty(PDFJS, 'isLittleEndian', { Object.defineProperty(PDFJS, 'isLittleEndian', {
configurable: true, configurable: true,
get: function PDFJS_isLittleEndian() { get: function PDFJS_isLittleEndian() {
@ -71,13 +68,6 @@ PDFJS.createPromiseCapability = createPromiseCapability;
*/ */
PDFJS.pdfBug = (PDFJS.pdfBug === undefined ? false : PDFJS.pdfBug); PDFJS.pdfBug = (PDFJS.pdfBug === undefined ? false : PDFJS.pdfBug);
/**
* Disables URL.createObjectURL usage.
* @var {boolean}
*/
PDFJS.disableCreateObjectURL = (PDFJS.disableCreateObjectURL === undefined ?
false : PDFJS.disableCreateObjectURL);
PDFJS.getDocument = getDocument; PDFJS.getDocument = getDocument;
PDFJS.LoopbackPort = LoopbackPort; PDFJS.LoopbackPort = LoopbackPort;
PDFJS.PDFDataRangeTransport = PDFDataRangeTransport; PDFJS.PDFDataRangeTransport = PDFDataRangeTransport;

View File

@ -55,7 +55,7 @@ const DefaultExternalServices = {
initPassiveLoading(callbacks) {}, initPassiveLoading(callbacks) {},
fallback(data, callback) {}, fallback(data, callback) {},
reportTelemetry(data) {}, reportTelemetry(data) {},
createDownloadManager() { createDownloadManager(options) {
throw new Error('Not implemented: createDownloadManager'); throw new Error('Not implemented: createDownloadManager');
}, },
createPreferences() { createPreferences() {
@ -357,7 +357,9 @@ let PDFViewerApplication = {
}); });
this.pdfLinkService = pdfLinkService; this.pdfLinkService = pdfLinkService;
let downloadManager = this.externalServices.createDownloadManager(); let downloadManager = this.externalServices.createDownloadManager({
disableCreateObjectURL: AppOptions.get('disableCreateObjectURL'),
});
this.downloadManager = downloadManager; this.downloadManager = downloadManager;
let container = appConfig.mainContainer; let container = appConfig.mainContainer;
@ -1873,7 +1875,7 @@ if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
webViewerFileInputChange = function webViewerFileInputChange(evt) { webViewerFileInputChange = function webViewerFileInputChange(evt) {
let file = evt.fileInput.files[0]; let file = evt.fileInput.files[0];
if (!PDFJS.disableCreateObjectURL && URL.createObjectURL) { if (URL.createObjectURL && !AppOptions.get('disableCreateObjectURL')) {
PDFViewerApplication.open(URL.createObjectURL(file)); PDFViewerApplication.open(URL.createObjectURL(file));
} else { } else {
// Read the local file into a Uint8Array. // Read the local file into a Uint8Array.

View File

@ -145,6 +145,11 @@ const defaultOptions = {
value: false, value: false,
kind: OptionKind.API, kind: OptionKind.API,
}, },
disableCreateObjectURL: {
/** @type {boolean} */
value: apiCompatibilityParams.disableCreateObjectURL || false,
kind: OptionKind.API,
},
disableFontFace: { disableFontFace: {
/** @type {boolean} */ /** @type {boolean} */
value: false, value: false,

View File

@ -363,8 +363,8 @@ ChromeExternalServices.initPassiveLoading = function(callbacks) {
callbacks.onOpenWithURL(url, length, originalURL); callbacks.onOpenWithURL(url, length, originalURL);
}); });
}; };
ChromeExternalServices.createDownloadManager = function() { ChromeExternalServices.createDownloadManager = function(options) {
return new DownloadManager(); return new DownloadManager(options);
}; };
ChromeExternalServices.createPreferences = function() { ChromeExternalServices.createPreferences = function() {
return new ChromePreferences(); return new ChromePreferences();

View File

@ -13,13 +13,18 @@
* limitations under the License. * limitations under the License.
*/ */
import { createObjectURL, createValidAbsoluteUrl, PDFJS } from 'pdfjs-lib'; import {
apiCompatibilityParams, createObjectURL, createValidAbsoluteUrl
} from 'pdfjs-lib';
if (typeof PDFJSDev !== 'undefined' && !PDFJSDev.test('CHROME || GENERIC')) { if (typeof PDFJSDev !== 'undefined' && !PDFJSDev.test('CHROME || GENERIC')) {
throw new Error('Module "pdfjs-web/download_manager" shall not be used ' + throw new Error('Module "pdfjs-web/download_manager" shall not be used ' +
'outside CHROME and GENERIC builds.'); 'outside CHROME and GENERIC builds.');
} }
const DISABLE_CREATE_OBJECT_URL =
apiCompatibilityParams.disableCreateObjectURL || false;
function download(blobUrl, filename) { function download(blobUrl, filename) {
let a = document.createElement('a'); let a = document.createElement('a');
if (!a.click) { if (!a.click) {
@ -40,6 +45,10 @@ function download(blobUrl, filename) {
} }
class DownloadManager { class DownloadManager {
constructor({ disableCreateObjectURL = DISABLE_CREATE_OBJECT_URL, }) {
this.disableCreateObjectURL = disableCreateObjectURL;
}
downloadUrl(url, filename) { downloadUrl(url, filename) {
if (!createValidAbsoluteUrl(url, 'http://example.com')) { if (!createValidAbsoluteUrl(url, 'http://example.com')) {
return; // restricted/invalid URL return; // restricted/invalid URL
@ -53,7 +62,7 @@ class DownloadManager {
filename); filename);
} }
let blobUrl = createObjectURL(data, contentType, let blobUrl = createObjectURL(data, contentType,
PDFJS.disableCreateObjectURL); this.disableCreateObjectURL);
download(blobUrl, filename); download(blobUrl, filename);
} }
@ -66,7 +75,7 @@ class DownloadManager {
return; return;
} }
if (PDFJS.disableCreateObjectURL) { if (this.disableCreateObjectURL) {
// URL.createObjectURL is not supported // URL.createObjectURL is not supported
this.downloadUrl(url, filename); this.downloadUrl(url, filename);
return; return;

View File

@ -84,6 +84,10 @@ let FirefoxCom = (function FirefoxComClosure() {
})(); })();
class DownloadManager { class DownloadManager {
constructor(options) {
this.disableCreateObjectURL = false;
}
downloadUrl(url, filename) { downloadUrl(url, filename) {
FirefoxCom.request('download', { FirefoxCom.request('download', {
originalUrl: url, originalUrl: url,
@ -92,7 +96,7 @@ class DownloadManager {
} }
downloadData(data, filename, contentType) { downloadData(data, filename, contentType) {
let blobUrl = createObjectURL(data, contentType, false); let blobUrl = createObjectURL(data, contentType);
FirefoxCom.request('download', { FirefoxCom.request('download', {
blobUrl, blobUrl,
@ -256,8 +260,8 @@ PDFViewerApplication.externalServices = {
FirefoxCom.request('reportTelemetry', JSON.stringify(data)); FirefoxCom.request('reportTelemetry', JSON.stringify(data));
}, },
createDownloadManager() { createDownloadManager(options) {
return new DownloadManager(); return new DownloadManager(options);
}, },
createPreferences() { createPreferences() {

View File

@ -42,8 +42,8 @@ class GenericPreferences extends BasePreferences {
} }
let GenericExternalServices = Object.create(DefaultExternalServices); let GenericExternalServices = Object.create(DefaultExternalServices);
GenericExternalServices.createDownloadManager = function() { GenericExternalServices.createDownloadManager = function(options) {
return new DownloadManager(); return new DownloadManager(options);
}; };
GenericExternalServices.createPreferences = function() { GenericExternalServices.createPreferences = function() {
return new GenericPreferences(); return new GenericPreferences();

View File

@ -14,7 +14,7 @@
*/ */
import { import {
createObjectURL, createPromiseCapability, getFilenameFromUrl, PDFJS, createObjectURL, createPromiseCapability, getFilenameFromUrl,
removeNullCharacters removeNullCharacters
} from 'pdfjs-lib'; } from 'pdfjs-lib';
@ -74,9 +74,9 @@ class PDFAttachmentViewer {
* @private * @private
*/ */
_bindPdfLink(button, content, filename) { _bindPdfLink(button, content, filename) {
if (PDFJS.disableCreateObjectURL) { if (this.downloadManager.disableCreateObjectURL) {
throw new Error('bindPdfLink: ' + throw new Error(
'Unsupported "PDFJS.disableCreateObjectURL" value.'); 'bindPdfLink: Unsupported "disableCreateObjectURL" value.');
} }
let blobUrl; let blobUrl;
button.onclick = function() { button.onclick = function() {
@ -141,7 +141,8 @@ class PDFAttachmentViewer {
div.className = 'attachmentsItem'; div.className = 'attachmentsItem';
let button = document.createElement('button'); let button = document.createElement('button');
button.textContent = filename; button.textContent = filename;
if (/\.pdf$/i.test(filename) && !PDFJS.disableCreateObjectURL) { if (/\.pdf$/i.test(filename) &&
!this.downloadManager.disableCreateObjectURL) {
this._bindPdfLink(button, item.content, filename); this._bindPdfLink(button, item.content, filename);
} else { } else {
this._bindLink(button, item.content, filename); this._bindLink(button, item.content, filename);

View File

@ -15,7 +15,6 @@
import { CSS_UNITS, NullL10n } from './ui_utils'; import { CSS_UNITS, NullL10n } from './ui_utils';
import { PDFPrintServiceFactory, PDFViewerApplication } from './app'; import { PDFPrintServiceFactory, PDFViewerApplication } from './app';
import { PDFJS } from 'pdfjs-lib';
let activeService = null; let activeService = null;
let overlayManager = null; let overlayManager = null;
@ -62,6 +61,8 @@ function PDFPrintService(pdfDocument, pagesOverview, printContainer, l10n) {
this.pagesOverview = pagesOverview; this.pagesOverview = pagesOverview;
this.printContainer = printContainer; this.printContainer = printContainer;
this.l10n = l10n || NullL10n; this.l10n = l10n || NullL10n;
this.disableCreateObjectURL =
pdfDocument.loadingParams['disableCreateObjectURL'];
this.currentPage = -1; this.currentPage = -1;
// The temporary canvas where renderPage paints one page at a time. // The temporary canvas where renderPage paints one page at a time.
this.scratchCanvas = document.createElement('canvas'); this.scratchCanvas = document.createElement('canvas');
@ -153,7 +154,7 @@ PDFPrintService.prototype = {
img.style.height = printItem.height; img.style.height = printItem.height;
let scratchCanvas = this.scratchCanvas; let scratchCanvas = this.scratchCanvas;
if (('toBlob' in scratchCanvas) && !PDFJS.disableCreateObjectURL) { if (('toBlob' in scratchCanvas) && !this.disableCreateObjectURL) {
scratchCanvas.toBlob(function(blob) { scratchCanvas.toBlob(function(blob) {
img.src = URL.createObjectURL(blob); img.src = URL.createObjectURL(blob);
}); });