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`.
* NOTE: It is also necessary to disable streaming, see above,
* 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') {
params.disableAutoFetch = false;
}
if (typeof params.disableCreateObjectURL !== 'boolean') {
params.disableCreateObjectURL =
apiCompatibilityParams.disableCreateObjectURL || false;
}
// Set the main-thread verbosity level.
setVerbosityLevel(params.verbosity);
@ -373,7 +380,7 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
},
maxImageSize: source.maxImageSize,
disableFontFace: source.disableFontFace,
disableCreateObjectURL: getDefaultSetting('disableCreateObjectURL'),
disableCreateObjectURL: source.disableCreateObjectURL,
postMessageTransfers: worker.postMessageTransfers,
docBaseUrl: source.docBaseUrl,
nativeImageDecoderSupport: source.nativeImageDecoderSupport,
@ -2148,6 +2155,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
disableRange: params.disableRange,
disableStream: params.disableStream,
disableAutoFetch: params.disableAutoFetch,
disableCreateObjectURL: params.disableCreateObjectURL,
});
},
};

View File

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

View File

@ -14,9 +14,9 @@
*/
import {
createBlob, createObjectURL, createPromiseCapability, InvalidPDFException,
isLittleEndian, MissingPDFException, OPS, PageViewport, PasswordException,
PasswordResponses, removeNullCharacters, shadow, UnexpectedResponseException,
createBlob, createPromiseCapability, InvalidPDFException, isLittleEndian,
MissingPDFException, OPS, PageViewport, PasswordException, PasswordResponses,
removeNullCharacters, shadow, UnexpectedResponseException,
UnknownErrorException, UNSUPPORTED_FEATURES, Util
} from '../shared/util';
import {
@ -45,9 +45,6 @@ PDFJS.OPS = OPS;
PDFJS.UNSUPPORTED_FEATURES = UNSUPPORTED_FEATURES;
PDFJS.shadow = shadow;
PDFJS.createBlob = createBlob;
PDFJS.createObjectURL = function PDFJS_createObjectURL(data, contentType) {
return createObjectURL(data, contentType, PDFJS.disableCreateObjectURL);
};
Object.defineProperty(PDFJS, 'isLittleEndian', {
configurable: true,
get: function PDFJS_isLittleEndian() {
@ -71,13 +68,6 @@ PDFJS.createPromiseCapability = createPromiseCapability;
*/
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.LoopbackPort = LoopbackPort;
PDFJS.PDFDataRangeTransport = PDFDataRangeTransport;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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