2017-07-01 02:59:52 +09:00
|
|
|
/* Copyright 2012 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { assert, createPromiseCapability } from '../shared/util';
|
|
|
|
|
|
|
|
/** @implements {IPDFStream} */
|
|
|
|
var PDFDataTransportStream = (function PDFDataTransportStreamClosure() {
|
|
|
|
function PDFDataTransportStream(params, pdfDataRangeTransport) {
|
|
|
|
assert(pdfDataRangeTransport);
|
|
|
|
|
|
|
|
this._queuedChunks = [];
|
[Firefox regression] Fix `disableRange=true` bug in `PDFDataTransportStream`
Currently if trying to set `disableRange=true` in the built-in PDF Viewer in Firefox, either through `about:config` or via the URL hash, the PDF document will never load. It appears that this has been broken for a couple of years, without anyone noticing.
Obviously it's not a good idea to set `disableRange=true`, however it seems that this bug affects the PDF Viewer in Firefox even with default settings:
- In the case where `initialData` already contains the *entire* file, we're forced to dispatch a range request to re-fetch already available data just so that file loading may complete.
- (In the case where the data arrives, via streaming, before being specifically requested through `requestDataRange`, we're also forced to re-fetch data unnecessarily.) *This part was removed, to reduce the scope/risk of the patch somewhat.*
In the cases outlined above, we're having to re-fetch already available data thus potentially delaying loading/rendering of PDF files in Firefox (and wasting resources in the process).
2019-03-27 00:05:30 +09:00
|
|
|
this._progressiveDone = params.progressiveDone || false;
|
|
|
|
|
|
|
|
const initialData = params.initialData;
|
2017-07-01 02:59:52 +09:00
|
|
|
if (initialData && initialData.length > 0) {
|
|
|
|
let buffer = new Uint8Array(initialData).buffer;
|
|
|
|
this._queuedChunks.push(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._pdfDataRangeTransport = pdfDataRangeTransport;
|
2018-02-09 21:39:38 +09:00
|
|
|
this._isStreamingSupported = !params.disableStream;
|
|
|
|
this._isRangeSupported = !params.disableRange;
|
2017-07-01 02:59:52 +09:00
|
|
|
this._contentLength = params.length;
|
|
|
|
|
|
|
|
this._fullRequestReader = null;
|
|
|
|
this._rangeReaders = [];
|
|
|
|
|
|
|
|
this._pdfDataRangeTransport.addRangeListener((begin, chunk) => {
|
|
|
|
this._onReceiveData({ begin, chunk, });
|
|
|
|
});
|
|
|
|
|
2019-04-06 18:04:44 +09:00
|
|
|
this._pdfDataRangeTransport.addProgressListener((loaded, total) => {
|
|
|
|
this._onProgress({ loaded, total, });
|
2017-07-01 02:59:52 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
this._pdfDataRangeTransport.addProgressiveReadListener((chunk) => {
|
|
|
|
this._onReceiveData({ chunk, });
|
|
|
|
});
|
|
|
|
|
[Firefox regression] Fix `disableRange=true` bug in `PDFDataTransportStream`
Currently if trying to set `disableRange=true` in the built-in PDF Viewer in Firefox, either through `about:config` or via the URL hash, the PDF document will never load. It appears that this has been broken for a couple of years, without anyone noticing.
Obviously it's not a good idea to set `disableRange=true`, however it seems that this bug affects the PDF Viewer in Firefox even with default settings:
- In the case where `initialData` already contains the *entire* file, we're forced to dispatch a range request to re-fetch already available data just so that file loading may complete.
- (In the case where the data arrives, via streaming, before being specifically requested through `requestDataRange`, we're also forced to re-fetch data unnecessarily.) *This part was removed, to reduce the scope/risk of the patch somewhat.*
In the cases outlined above, we're having to re-fetch already available data thus potentially delaying loading/rendering of PDF files in Firefox (and wasting resources in the process).
2019-03-27 00:05:30 +09:00
|
|
|
this._pdfDataRangeTransport.addProgressiveDoneListener(() => {
|
|
|
|
this._onProgressiveDone();
|
|
|
|
});
|
|
|
|
|
2017-07-01 02:59:52 +09:00
|
|
|
this._pdfDataRangeTransport.transportReady();
|
|
|
|
}
|
|
|
|
PDFDataTransportStream.prototype = {
|
|
|
|
_onReceiveData: function PDFDataTransportStream_onReceiveData(args) {
|
|
|
|
let buffer = new Uint8Array(args.chunk).buffer;
|
|
|
|
if (args.begin === undefined) {
|
|
|
|
if (this._fullRequestReader) {
|
|
|
|
this._fullRequestReader._enqueue(buffer);
|
|
|
|
} else {
|
|
|
|
this._queuedChunks.push(buffer);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var found = this._rangeReaders.some(function (rangeReader) {
|
|
|
|
if (rangeReader._begin !== args.begin) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
rangeReader._enqueue(buffer);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
assert(found);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-03-28 01:54:05 +09:00
|
|
|
get _progressiveDataLength() {
|
|
|
|
return (this._fullRequestReader ? this._fullRequestReader._loaded : 0);
|
|
|
|
},
|
|
|
|
|
2017-07-01 02:59:52 +09:00
|
|
|
_onProgress: function PDFDataTransportStream_onDataProgress(evt) {
|
2019-04-16 22:19:19 +09:00
|
|
|
if (evt.total === undefined) {
|
|
|
|
// Reporting to first range reader, if it exists.
|
|
|
|
let firstReader = this._rangeReaders[0];
|
|
|
|
if (firstReader && firstReader.onProgress) {
|
|
|
|
firstReader.onProgress({ loaded: evt.loaded, });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let fullReader = this._fullRequestReader;
|
|
|
|
if (fullReader && fullReader.onProgress) {
|
|
|
|
fullReader.onProgress({ loaded: evt.loaded, total: evt.total, });
|
|
|
|
}
|
|
|
|
}
|
2017-07-01 02:59:52 +09:00
|
|
|
},
|
|
|
|
|
[Firefox regression] Fix `disableRange=true` bug in `PDFDataTransportStream`
Currently if trying to set `disableRange=true` in the built-in PDF Viewer in Firefox, either through `about:config` or via the URL hash, the PDF document will never load. It appears that this has been broken for a couple of years, without anyone noticing.
Obviously it's not a good idea to set `disableRange=true`, however it seems that this bug affects the PDF Viewer in Firefox even with default settings:
- In the case where `initialData` already contains the *entire* file, we're forced to dispatch a range request to re-fetch already available data just so that file loading may complete.
- (In the case where the data arrives, via streaming, before being specifically requested through `requestDataRange`, we're also forced to re-fetch data unnecessarily.) *This part was removed, to reduce the scope/risk of the patch somewhat.*
In the cases outlined above, we're having to re-fetch already available data thus potentially delaying loading/rendering of PDF files in Firefox (and wasting resources in the process).
2019-03-27 00:05:30 +09:00
|
|
|
_onProgressiveDone() {
|
|
|
|
if (this._fullRequestReader) {
|
|
|
|
this._fullRequestReader.progressiveDone();
|
|
|
|
}
|
|
|
|
this._progressiveDone = true;
|
|
|
|
},
|
|
|
|
|
2017-07-01 02:59:52 +09:00
|
|
|
_removeRangeReader:
|
|
|
|
function PDFDataTransportStream_removeRangeReader(reader) {
|
|
|
|
var i = this._rangeReaders.indexOf(reader);
|
|
|
|
if (i >= 0) {
|
|
|
|
this._rangeReaders.splice(i, 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getFullReader: function PDFDataTransportStream_getFullReader() {
|
|
|
|
assert(!this._fullRequestReader);
|
|
|
|
var queuedChunks = this._queuedChunks;
|
|
|
|
this._queuedChunks = null;
|
[Firefox regression] Fix `disableRange=true` bug in `PDFDataTransportStream`
Currently if trying to set `disableRange=true` in the built-in PDF Viewer in Firefox, either through `about:config` or via the URL hash, the PDF document will never load. It appears that this has been broken for a couple of years, without anyone noticing.
Obviously it's not a good idea to set `disableRange=true`, however it seems that this bug affects the PDF Viewer in Firefox even with default settings:
- In the case where `initialData` already contains the *entire* file, we're forced to dispatch a range request to re-fetch already available data just so that file loading may complete.
- (In the case where the data arrives, via streaming, before being specifically requested through `requestDataRange`, we're also forced to re-fetch data unnecessarily.) *This part was removed, to reduce the scope/risk of the patch somewhat.*
In the cases outlined above, we're having to re-fetch already available data thus potentially delaying loading/rendering of PDF files in Firefox (and wasting resources in the process).
2019-03-27 00:05:30 +09:00
|
|
|
return new PDFDataTransportStreamReader(this, queuedChunks,
|
|
|
|
this._progressiveDone);
|
2017-07-01 02:59:52 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
getRangeReader: function PDFDataTransportStream_getRangeReader(begin, end) {
|
2019-03-28 01:54:05 +09:00
|
|
|
if (end <= this._progressiveDataLength) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-07-01 02:59:52 +09:00
|
|
|
var reader = new PDFDataTransportStreamRangeReader(this, begin, end);
|
|
|
|
this._pdfDataRangeTransport.requestDataRange(begin, end);
|
|
|
|
this._rangeReaders.push(reader);
|
|
|
|
return reader;
|
|
|
|
},
|
|
|
|
|
|
|
|
cancelAllRequests:
|
|
|
|
function PDFDataTransportStream_cancelAllRequests(reason) {
|
|
|
|
if (this._fullRequestReader) {
|
|
|
|
this._fullRequestReader.cancel(reason);
|
|
|
|
}
|
|
|
|
var readers = this._rangeReaders.slice(0);
|
|
|
|
readers.forEach(function (rangeReader) {
|
|
|
|
rangeReader.cancel(reason);
|
|
|
|
});
|
|
|
|
this._pdfDataRangeTransport.abort();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @implements {IPDFStreamReader} */
|
[Firefox regression] Fix `disableRange=true` bug in `PDFDataTransportStream`
Currently if trying to set `disableRange=true` in the built-in PDF Viewer in Firefox, either through `about:config` or via the URL hash, the PDF document will never load. It appears that this has been broken for a couple of years, without anyone noticing.
Obviously it's not a good idea to set `disableRange=true`, however it seems that this bug affects the PDF Viewer in Firefox even with default settings:
- In the case where `initialData` already contains the *entire* file, we're forced to dispatch a range request to re-fetch already available data just so that file loading may complete.
- (In the case where the data arrives, via streaming, before being specifically requested through `requestDataRange`, we're also forced to re-fetch data unnecessarily.) *This part was removed, to reduce the scope/risk of the patch somewhat.*
In the cases outlined above, we're having to re-fetch already available data thus potentially delaying loading/rendering of PDF files in Firefox (and wasting resources in the process).
2019-03-27 00:05:30 +09:00
|
|
|
function PDFDataTransportStreamReader(stream, queuedChunks,
|
|
|
|
progressiveDone = false) {
|
2017-07-01 02:59:52 +09:00
|
|
|
this._stream = stream;
|
[Firefox regression] Fix `disableRange=true` bug in `PDFDataTransportStream`
Currently if trying to set `disableRange=true` in the built-in PDF Viewer in Firefox, either through `about:config` or via the URL hash, the PDF document will never load. It appears that this has been broken for a couple of years, without anyone noticing.
Obviously it's not a good idea to set `disableRange=true`, however it seems that this bug affects the PDF Viewer in Firefox even with default settings:
- In the case where `initialData` already contains the *entire* file, we're forced to dispatch a range request to re-fetch already available data just so that file loading may complete.
- (In the case where the data arrives, via streaming, before being specifically requested through `requestDataRange`, we're also forced to re-fetch data unnecessarily.) *This part was removed, to reduce the scope/risk of the patch somewhat.*
In the cases outlined above, we're having to re-fetch already available data thus potentially delaying loading/rendering of PDF files in Firefox (and wasting resources in the process).
2019-03-27 00:05:30 +09:00
|
|
|
this._done = progressiveDone || false;
|
2018-01-17 00:24:36 +09:00
|
|
|
this._filename = null;
|
2017-07-01 02:59:52 +09:00
|
|
|
this._queuedChunks = queuedChunks || [];
|
2019-03-28 01:54:05 +09:00
|
|
|
this._loaded = 0;
|
|
|
|
for (const chunk of this._queuedChunks) {
|
|
|
|
this._loaded += chunk.byteLength;
|
|
|
|
}
|
2017-07-01 02:59:52 +09:00
|
|
|
this._requests = [];
|
|
|
|
this._headersReady = Promise.resolve();
|
|
|
|
stream._fullRequestReader = this;
|
|
|
|
|
|
|
|
this.onProgress = null; // not used
|
|
|
|
}
|
|
|
|
PDFDataTransportStreamReader.prototype = {
|
|
|
|
_enqueue: function PDFDataTransportStreamReader_enqueue(chunk) {
|
|
|
|
if (this._done) {
|
|
|
|
return; // ignore new data
|
|
|
|
}
|
|
|
|
if (this._requests.length > 0) {
|
|
|
|
var requestCapability = this._requests.shift();
|
|
|
|
requestCapability.resolve({ value: chunk, done: false, });
|
2019-03-28 01:54:05 +09:00
|
|
|
} else {
|
|
|
|
this._queuedChunks.push(chunk);
|
2017-07-01 02:59:52 +09:00
|
|
|
}
|
2019-03-28 01:54:05 +09:00
|
|
|
this._loaded += chunk.byteLength;
|
2017-07-01 02:59:52 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
get headersReady() {
|
|
|
|
return this._headersReady;
|
|
|
|
},
|
|
|
|
|
2018-01-17 00:24:36 +09:00
|
|
|
get filename() {
|
|
|
|
return this._filename;
|
|
|
|
},
|
|
|
|
|
2017-07-01 02:59:52 +09:00
|
|
|
get isRangeSupported() {
|
|
|
|
return this._stream._isRangeSupported;
|
|
|
|
},
|
|
|
|
|
|
|
|
get isStreamingSupported() {
|
|
|
|
return this._stream._isStreamingSupported;
|
|
|
|
},
|
|
|
|
|
|
|
|
get contentLength() {
|
|
|
|
return this._stream._contentLength;
|
|
|
|
},
|
|
|
|
|
2018-07-30 20:58:09 +09:00
|
|
|
async read() {
|
2017-07-01 02:59:52 +09:00
|
|
|
if (this._queuedChunks.length > 0) {
|
|
|
|
var chunk = this._queuedChunks.shift();
|
2018-07-30 20:58:09 +09:00
|
|
|
return { value: chunk, done: false, };
|
2017-07-01 02:59:52 +09:00
|
|
|
}
|
|
|
|
if (this._done) {
|
2018-07-30 20:58:09 +09:00
|
|
|
return { value: undefined, done: true, };
|
2017-07-01 02:59:52 +09:00
|
|
|
}
|
|
|
|
var requestCapability = createPromiseCapability();
|
|
|
|
this._requests.push(requestCapability);
|
|
|
|
return requestCapability.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
cancel: function PDFDataTransportStreamReader_cancel(reason) {
|
|
|
|
this._done = true;
|
|
|
|
this._requests.forEach(function (requestCapability) {
|
|
|
|
requestCapability.resolve({ value: undefined, done: true, });
|
|
|
|
});
|
|
|
|
this._requests = [];
|
|
|
|
},
|
[Firefox regression] Fix `disableRange=true` bug in `PDFDataTransportStream`
Currently if trying to set `disableRange=true` in the built-in PDF Viewer in Firefox, either through `about:config` or via the URL hash, the PDF document will never load. It appears that this has been broken for a couple of years, without anyone noticing.
Obviously it's not a good idea to set `disableRange=true`, however it seems that this bug affects the PDF Viewer in Firefox even with default settings:
- In the case where `initialData` already contains the *entire* file, we're forced to dispatch a range request to re-fetch already available data just so that file loading may complete.
- (In the case where the data arrives, via streaming, before being specifically requested through `requestDataRange`, we're also forced to re-fetch data unnecessarily.) *This part was removed, to reduce the scope/risk of the patch somewhat.*
In the cases outlined above, we're having to re-fetch already available data thus potentially delaying loading/rendering of PDF files in Firefox (and wasting resources in the process).
2019-03-27 00:05:30 +09:00
|
|
|
|
|
|
|
progressiveDone() {
|
|
|
|
if (this._done) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._done = true;
|
|
|
|
},
|
2017-07-01 02:59:52 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
/** @implements {IPDFStreamRangeReader} */
|
|
|
|
function PDFDataTransportStreamRangeReader(stream, begin, end) {
|
|
|
|
this._stream = stream;
|
|
|
|
this._begin = begin;
|
|
|
|
this._end = end;
|
|
|
|
this._queuedChunk = null;
|
|
|
|
this._requests = [];
|
|
|
|
this._done = false;
|
|
|
|
|
|
|
|
this.onProgress = null;
|
|
|
|
}
|
|
|
|
PDFDataTransportStreamRangeReader.prototype = {
|
|
|
|
_enqueue: function PDFDataTransportStreamRangeReader_enqueue(chunk) {
|
|
|
|
if (this._done) {
|
|
|
|
return; // ignore new data
|
|
|
|
}
|
|
|
|
if (this._requests.length === 0) {
|
|
|
|
this._queuedChunk = chunk;
|
|
|
|
} else {
|
|
|
|
var requestsCapability = this._requests.shift();
|
|
|
|
requestsCapability.resolve({ value: chunk, done: false, });
|
|
|
|
this._requests.forEach(function (requestCapability) {
|
|
|
|
requestCapability.resolve({ value: undefined, done: true, });
|
|
|
|
});
|
|
|
|
this._requests = [];
|
|
|
|
}
|
|
|
|
this._done = true;
|
|
|
|
this._stream._removeRangeReader(this);
|
|
|
|
},
|
|
|
|
|
|
|
|
get isStreamingSupported() {
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2018-07-30 20:58:09 +09:00
|
|
|
async read() {
|
2017-07-01 02:59:52 +09:00
|
|
|
if (this._queuedChunk) {
|
|
|
|
let chunk = this._queuedChunk;
|
|
|
|
this._queuedChunk = null;
|
2018-07-30 20:58:09 +09:00
|
|
|
return { value: chunk, done: false, };
|
2017-07-01 02:59:52 +09:00
|
|
|
}
|
|
|
|
if (this._done) {
|
2018-07-30 20:58:09 +09:00
|
|
|
return { value: undefined, done: true, };
|
2017-07-01 02:59:52 +09:00
|
|
|
}
|
|
|
|
var requestCapability = createPromiseCapability();
|
|
|
|
this._requests.push(requestCapability);
|
|
|
|
return requestCapability.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
cancel: function PDFDataTransportStreamRangeReader_cancel(reason) {
|
|
|
|
this._done = true;
|
|
|
|
this._requests.forEach(function (requestCapability) {
|
|
|
|
requestCapability.resolve({ value: undefined, done: true, });
|
|
|
|
});
|
|
|
|
this._requests = [];
|
|
|
|
this._stream._removeRangeReader(this);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return PDFDataTransportStream;
|
|
|
|
})();
|
|
|
|
|
|
|
|
export {
|
|
|
|
PDFDataTransportStream,
|
|
|
|
};
|