From 62afa9f6951c1f1267d3c98082d01f3ced82856e Mon Sep 17 00:00:00 2001 From: Yury Delendik Date: Fri, 23 Oct 2015 08:49:02 -0500 Subject: [PATCH] Fixes PDFViewerApplication.open/close methods signature. --- web/chromecom.js | 12 +++--- web/viewer.js | 97 +++++++++++++++++++++++++++++++++++------------- 2 files changed, 77 insertions(+), 32 deletions(-) diff --git a/web/chromecom.js b/web/chromecom.js index 6823dd3a1..134dd6f00 100644 --- a/web/chromecom.js +++ b/web/chromecom.js @@ -64,7 +64,7 @@ var ChromeCom = (function ChromeComClosure() { var streamUrl = response.streamUrl; if (streamUrl) { console.log('Found data stream for ' + file); - PDFViewerApplication.open(streamUrl, 0, undefined, undefined, { + PDFViewerApplication.open(streamUrl, { length: response.contentLength }); PDFViewerApplication.setTitleUsingUrl(file); @@ -91,7 +91,7 @@ var ChromeCom = (function ChromeComClosure() { resolveLocalFileSystemURL(file, function onResolvedFSURL(fileEntry) { fileEntry.file(function(fileObject) { var blobUrl = URL.createObjectURL(fileObject); - PDFViewerApplication.open(blobUrl, 0, undefined, undefined, { + PDFViewerApplication.open(blobUrl, { length: fileObject.size }); }); @@ -100,7 +100,7 @@ var ChromeCom = (function ChromeComClosure() { // usual way of getting the File's data (via the Web worker). console.warn('Cannot resolve file ' + file + ', ' + error.name + ' ' + error.message); - PDFViewerApplication.open(file, 0); + PDFViewerApplication.open(file); }); return; } @@ -109,7 +109,7 @@ var ChromeCom = (function ChromeComClosure() { // There is no UI to input a different URL, so this assumption will hold // for now. setReferer(file, function() { - PDFViewerApplication.open(file, 0); + PDFViewerApplication.open(file); }); return; } @@ -122,14 +122,14 @@ var ChromeCom = (function ChromeComClosure() { } isAllowedFileSchemeAccess(function(isAllowedAccess) { if (isAllowedAccess) { - PDFViewerApplication.open(file, 0); + PDFViewerApplication.open(file); } else { requestAccessToLocalFile(file); } }); return; } - PDFViewerApplication.open(file, 0); + PDFViewerApplication.open(file); }); }; diff --git a/web/viewer.js b/web/viewer.js index 632afe077..947b42241 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -92,6 +92,7 @@ var PDFViewerApplication = { initialized: false, fellback: false, pdfDocument: null, + pdfLoadingTask: null, sidebarOpen: false, printing: false, /** @type {PDFViewer} */ @@ -434,8 +435,8 @@ var PDFViewerApplication = { pdfDataRangeTransport = new FirefoxComDataRangeTransport(args.length, args.data); - PDFViewerApplication.open(args.pdfUrl, 0, undefined, - pdfDataRangeTransport); + PDFViewerApplication.open(args.pdfUrl, + {range: pdfDataRangeTransport}); if (args.length) { PDFViewerApplication.pdfDocumentProperties @@ -460,7 +461,7 @@ var PDFViewerApplication = { 'An error occurred while loading the PDF.'), e); break; } - PDFViewerApplication.open(args.data, 0); + PDFViewerApplication.open(args.data); break; } }); @@ -487,36 +488,76 @@ var PDFViewerApplication = { document.title = title; }, + /** + * Closes opened PDF document. + * @returns {Promise} - Returns the promise, which is resolved when all + * destruction is completed. + */ close: function pdfViewClose() { var errorWrapper = document.getElementById('errorWrapper'); errorWrapper.setAttribute('hidden', 'true'); - if (!this.pdfDocument) { - return; + if (!this.pdfLoadingTask) { + return Promise.resolve(); } - this.pdfDocument.destroy(); - this.pdfDocument = null; + var promise = this.pdfLoadingTask.destroy(); + this.pdfLoadingTask = null; - this.pdfThumbnailViewer.setDocument(null); - this.pdfViewer.setDocument(null); - this.pdfLinkService.setDocument(null, null); + if (this.pdfDocument) { + this.pdfDocument = null; + + this.pdfThumbnailViewer.setDocument(null); + this.pdfViewer.setDocument(null); + this.pdfLinkService.setDocument(null, null); + } if (typeof PDFBug !== 'undefined') { PDFBug.cleanup(); } + return promise; }, - // TODO(mack): This function signature should really be pdfViewOpen(url, args) - open: function pdfViewOpen(file, scale, password, - pdfDataRangeTransport, args) { - if (this.pdfDocument) { - // Reload the preferences if a document was previously opened. - Preferences.reload(); + /** + * Opens PDF document specified by URL or array with additional arguments. + * @param {string|TypedArray|ArrayBuffer} file - PDF location or binary data. + * @param {Object} args - (optional) Additional arguments for the getDocument + * call, e.g. HTTP headers ('httpHeaders') or + * alternative data transport ('range'). + * @returns {Promise} - Returns the promise, which is resolved when document + * is opened. + */ + open: function pdfViewOpen(file, args) { + var scale = 0; + if (arguments.length > 2 || typeof args === 'number') { + console.warn('Call of open() with obsolete signature.'); + if (typeof args === 'number') { + scale = args; // scale argument was found + } + args = arguments[4] || null; + if (arguments[3] && typeof arguments[3] === 'object') { + // The pdfDataRangeTransport argument is present. + args = Object.create(args); + args.range = arguments[3]; + } + if (typeof arguments[2] === 'string') { + // The password argument is present. + args = Object.create(args); + args.password = arguments[2]; + } } - this.close(); - var parameters = {password: password}; + if (this.pdfLoadingTask) { + // We need to destroy already opened document. + return this.close().then(function () { + // Reload the preferences if a document was previously opened. + Preferences.reload(); + // ... and repeat the open() call. + return this.open(file, args); + }.bind(this)); + } + + var parameters = Object.create(null); if (typeof file === 'string') { // URL this.setTitleUsingUrl(file); parameters.url = file; @@ -526,9 +567,6 @@ var PDFViewerApplication = { this.setTitleUsingUrl(file.originalUrl); parameters.url = file.url; } - if (pdfDataRangeTransport) { - parameters.range = pdfDataRangeTransport; - } if (args) { for (var prop in args) { parameters[prop] = args[prop]; @@ -539,6 +577,7 @@ var PDFViewerApplication = { self.downloadComplete = false; var loadingTask = PDFJS.getDocument(parameters); + this.pdfLoadingTask = loadingTask; loadingTask.onPassword = function passwordNeeded(updatePassword, reason) { PasswordPrompt.updatePassword = updatePassword; @@ -550,7 +589,7 @@ var PDFViewerApplication = { self.progress(progressData.loaded / progressData.total); }; - loadingTask.promise.then( + var result = loadingTask.promise.then( function getDocumentCallback(pdfDocument) { self.load(pdfDocument, scale); }, @@ -576,12 +615,15 @@ var PDFViewerApplication = { message: message }; self.error(loadingErrorMessage, moreInfo); + + throw new Error(loadingErrorMessage); } ); if (args && args.length) { PDFViewerApplication.pdfDocumentProperties.setFileSize(args.length); } + return result; }, download: function pdfViewDownload() { @@ -1008,6 +1050,9 @@ var PDFViewerApplication = { }, cleanup: function pdfViewCleanup() { + if (!this.pdfDocument) { + return; // run cleanup when document is loaded + } this.pdfViewer.cleanup(); this.pdfThumbnailViewer.cleanup(); this.pdfDocument.cleanup(); @@ -1502,7 +1547,7 @@ function webViewerInitialized() { PDFViewerApplication.setTitleUsingUrl(file); var xhr = new XMLHttpRequest(); xhr.onload = function() { - PDFViewerApplication.open(new Uint8Array(xhr.response), 0); + PDFViewerApplication.open(new Uint8Array(xhr.response)); }; try { xhr.open('GET', file); @@ -1516,7 +1561,7 @@ function webViewerInitialized() { } if (file) { - PDFViewerApplication.open(file, 0); + PDFViewerApplication.open(file); } //#endif //#if CHROME @@ -1731,14 +1776,14 @@ window.addEventListener('change', function webViewerChange(evt) { if (!PDFJS.disableCreateObjectURL && typeof URL !== 'undefined' && URL.createObjectURL) { - PDFViewerApplication.open(URL.createObjectURL(file), 0); + PDFViewerApplication.open(URL.createObjectURL(file)); } else { // Read the local file into a Uint8Array. var fileReader = new FileReader(); fileReader.onload = function webViewerChangeFileReaderOnload(evt) { var buffer = evt.target.result; var uint8Array = new Uint8Array(buffer); - PDFViewerApplication.open(uint8Array, 0); + PDFViewerApplication.open(uint8Array); }; fileReader.readAsArrayBuffer(file); }