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

One additional complication with removing this option from the global `PDFJS` object, is that the viewer currently needs to check `disableAutoFetch` in a couple of places. To address this I'm thus proposing adding a getter in `PDFDocumentProxy`, to allow checking the *actually* used values for a particular `getDocument` invocation.
This commit is contained in:
Jonas Jenwald 2018-02-17 22:08:45 +01:00
parent c7c583583b
commit 69d7191034
7 changed files with 38 additions and 22 deletions

View File

@ -18,7 +18,7 @@ import {
assert, createPromiseCapability, getVerbosityLevel, info, InvalidPDFException,
isArrayBuffer, isNum, isSameOrigin, MessageHandler, MissingPDFException,
NativeImageDecoding, PageViewport, PasswordException, setVerbosityLevel,
stringToBytes, UnexpectedResponseException, UnknownErrorException,
shadow, stringToBytes, UnexpectedResponseException, UnknownErrorException,
unreachable, Util, warn
} from '../shared/util';
import {
@ -164,6 +164,12 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) {
* converted to OpenType fonts and loaded via font face rules. If disabled,
* fonts will be rendered using a built-in font renderer that constructs the
* glyphs with primitive path commands. The default value is `false`.
* @property {boolean} disableAutoFetch - (optional) Disable pre-fetching of PDF
* file data. When range requests are enabled PDF.js will automatically keep
* fetching more data even if it isn't needed to display the current page.
* 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.
*/
/**
@ -266,6 +272,10 @@ function getDocument(src) {
params.disableFontFace = false;
}
if (typeof params.disableAutoFetch !== 'boolean') {
params.disableAutoFetch = false;
}
// Set the main-thread verbosity level.
setVerbosityLevel(params.verbosity);
@ -333,7 +343,6 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
typeof PDFJSDev !== 'undefined' ? PDFJSDev.eval('BUNDLE_VERSION') : null;
source.disableRange = getDefaultSetting('disableRange');
source.disableAutoFetch = getDefaultSetting('disableAutoFetch');
source.disableStream = getDefaultSetting('disableStream');
if (pdfDataRangeTransport) {
source.length = pdfDataRangeTransport.length;
@ -683,6 +692,10 @@ var PDFDocumentProxy = (function PDFDocumentProxyClosure() {
destroy: function PDFDocumentProxy_destroy() {
return this.loadingTask.destroy();
},
get loadingParams() {
return this.transport.loadingParams;
},
};
return PDFDocumentProxy;
})();
@ -2116,6 +2129,13 @@ var WorkerTransport = (function WorkerTransportClosure() {
this.fontLoader.clear();
});
},
get loadingParams() {
let params = this._params;
return shadow(this, 'loadingParams', {
disableAutoFetch: params.disableAutoFetch,
});
},
};
return WorkerTransport;

View File

@ -336,8 +336,6 @@ function getDefaultSetting(id) {
switch (id) {
case 'pdfBug':
return globalSettings ? globalSettings.pdfBug : false;
case 'disableAutoFetch':
return globalSettings ? globalSettings.disableAutoFetch : false;
case 'disableStream':
return globalSettings ? globalSettings.disableStream : false;
case 'disableRange':

View File

@ -82,18 +82,6 @@ PDFJS.disableRange = (PDFJS.disableRange === undefined ?
PDFJS.disableStream = (PDFJS.disableStream === undefined ?
false : PDFJS.disableStream);
/**
* Disable pre-fetching of PDF file data. When range requests are enabled
* PDF.js will automatically keep fetching more data even if it isn't needed
* to display the current page. This default behavior can be disabled.
*
* NOTE: It is also necessary to disable streaming, see above,
* in order for disabling of pre-fetching to work correctly.
* @var {boolean}
*/
PDFJS.disableAutoFetch = (PDFJS.disableAutoFetch === undefined ?
false : PDFJS.disableAutoFetch);
/**
* Enables special hooks for debugging PDF.js.
* @var {boolean}

View File

@ -360,7 +360,6 @@ var Driver = (function DriverClosure() { // eslint-disable-line no-unused-vars
let absoluteUrl = new URL(task.file, window.location).href;
PDFJS.disableRange = task.disableRange;
PDFJS.disableAutoFetch = !task.enableAutoFetch;
try {
PDFJS.getDocument({
url: absoluteUrl,
@ -368,6 +367,7 @@ var Driver = (function DriverClosure() { // eslint-disable-line no-unused-vars
nativeImageDecoderSupport: task.nativeImageDecoderSupport,
cMapUrl: CMAP_URL,
cMapPacked: CMAP_PACKED,
disableAutoFetch: !task.enableAutoFetch,
}).then((doc) => {
task.pdfDoc = doc;
this._nextPage(task, failure);

View File

@ -207,7 +207,7 @@ let PDFViewerApplication = {
PDFJS.disableStream = value;
}),
preferences.get('disableAutoFetch').then(function resolved(value) {
PDFJS.disableAutoFetch = value;
AppOptions.set('disableAutoFetch', value);
}),
preferences.get('disableFontFace').then(function resolved(value) {
if (AppOptions.get('disableFontFace') === true) {
@ -266,7 +266,8 @@ let PDFViewerApplication = {
PDFJS.disableStream = (hashParams['disablestream'] === 'true');
}
if ('disableautofetch' in hashParams) {
PDFJS.disableAutoFetch = (hashParams['disableautofetch'] === 'true');
AppOptions.set('disableAutoFetch',
hashParams['disableautofetch'] === 'true');
}
if ('disablefontface' in hashParams) {
AppOptions.set('disableFontFace',
@ -933,7 +934,11 @@ let PDFViewerApplication = {
// the loading bar will not be completely filled, nor will it be hidden.
// To prevent displaying a partially filled loading bar permanently, we
// hide it when no data has been loaded during a certain amount of time.
if (PDFJS.disableAutoFetch && percent) {
const disableAutoFetch = this.pdfDocument ?
this.pdfDocument.loadingParams['disableAutoFetch'] :
AppOptions.get('disableAutoFetch');
if (disableAutoFetch && percent) {
if (this.disableAutoFetchLoadingBarTimeout) {
clearTimeout(this.disableAutoFetchLoadingBarTimeout);
this.disableAutoFetchLoadingBarTimeout = null;

View File

@ -139,6 +139,11 @@ const defaultOptions = {
'../external/bcmaps/' : '../web/cmaps/'),
kind: OptionKind.API,
},
disableAutoFetch: {
/** @type {boolean} */
value: false,
kind: OptionKind.API,
},
disableFontFace: {
/** @type {boolean} */
value: false,

View File

@ -13,7 +13,6 @@
* limitations under the License.
*/
import { createPromiseCapability, PDFJS } from 'pdfjs-lib';
import {
CSS_UNITS, DEFAULT_SCALE, DEFAULT_SCALE_VALUE, isValidRotation,
MAX_AUTO_SCALE, NullL10n, PresentationModeState, RendererType,
@ -21,6 +20,7 @@ import {
} from './ui_utils';
import { PDFRenderingQueue, RenderingStates } from './pdf_rendering_queue';
import { AnnotationLayerBuilder } from './annotation_layer_builder';
import { createPromiseCapability } from 'pdfjs-lib';
import { getGlobalEventBus } from './dom_events';
import { PDFPageView } from './pdf_page_view';
import { SimpleLinkService } from './pdf_link_service';
@ -408,7 +408,7 @@ class BaseViewer {
// starts to create the correct size canvas. Wait until one page is
// rendered so we don't tie up too many resources early on.
onePageRenderedCapability.promise.then(() => {
if (PDFJS.disableAutoFetch) {
if (pdfDocument.loadingParams['disableAutoFetch']) {
// XXX: Printing is semi-broken with auto fetch disabled.
pagesCapability.resolve();
return;