2013-02-07 08:19:29 +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.
|
|
|
|
*/
|
2013-04-19 02:41:33 +09:00
|
|
|
/* globals assert, MissingDataException, isInt, NetworkManager, Promise,
|
2014-04-23 23:33:42 +09:00
|
|
|
isEmptyObj, createPromiseCapability */
|
2013-02-07 08:19:29 +09:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var ChunkedStream = (function ChunkedStreamClosure() {
|
2013-06-05 09:57:52 +09:00
|
|
|
function ChunkedStream(length, chunkSize, manager) {
|
2013-02-07 08:19:29 +09:00
|
|
|
this.bytes = new Uint8Array(length);
|
|
|
|
this.start = 0;
|
|
|
|
this.pos = 0;
|
|
|
|
this.end = length;
|
|
|
|
this.chunkSize = chunkSize;
|
|
|
|
this.loadedChunks = [];
|
|
|
|
this.numChunksLoaded = 0;
|
|
|
|
this.numChunks = Math.ceil(length / chunkSize);
|
2013-06-05 09:57:52 +09:00
|
|
|
this.manager = manager;
|
2014-09-06 10:02:54 +09:00
|
|
|
this.progressiveDataLength = 0;
|
2014-06-18 13:20:58 +09:00
|
|
|
this.lastSuccessfulEnsureByteChunk = -1; // a single-entry cache
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// required methods for a stream. if a particular stream does not
|
|
|
|
// implement these, an error should be thrown
|
|
|
|
ChunkedStream.prototype = {
|
|
|
|
|
|
|
|
getMissingChunks: function ChunkedStream_getMissingChunks() {
|
|
|
|
var chunks = [];
|
|
|
|
for (var chunk = 0, n = this.numChunks; chunk < n; ++chunk) {
|
2014-09-06 10:05:12 +09:00
|
|
|
if (!this.loadedChunks[chunk]) {
|
2013-02-07 08:19:29 +09:00
|
|
|
chunks.push(chunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return chunks;
|
|
|
|
},
|
|
|
|
|
2013-07-04 06:29:38 +09:00
|
|
|
getBaseStreams: function ChunkedStream_getBaseStreams() {
|
|
|
|
return [this];
|
|
|
|
},
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
allChunksLoaded: function ChunkedStream_allChunksLoaded() {
|
|
|
|
return this.numChunksLoaded === this.numChunks;
|
|
|
|
},
|
|
|
|
|
2014-03-23 04:21:22 +09:00
|
|
|
onReceiveData: function ChunkedStream_onReceiveData(begin, chunk) {
|
2013-02-07 08:19:29 +09:00
|
|
|
var end = begin + chunk.byteLength;
|
|
|
|
|
|
|
|
assert(begin % this.chunkSize === 0, 'Bad begin offset: ' + begin);
|
|
|
|
// Using this.length is inaccurate here since this.start can be moved
|
|
|
|
// See ChunkedStream.moveStart()
|
|
|
|
var length = this.bytes.length;
|
|
|
|
assert(end % this.chunkSize === 0 || end === length,
|
2014-03-23 04:21:22 +09:00
|
|
|
'Bad end offset: ' + end);
|
2013-02-07 08:19:29 +09:00
|
|
|
|
|
|
|
this.bytes.set(new Uint8Array(chunk), begin);
|
|
|
|
var chunkSize = this.chunkSize;
|
|
|
|
var beginChunk = Math.floor(begin / chunkSize);
|
|
|
|
var endChunk = Math.floor((end - 1) / chunkSize) + 1;
|
2014-04-08 06:42:54 +09:00
|
|
|
var curChunk;
|
2013-02-07 08:19:29 +09:00
|
|
|
|
2014-04-08 06:42:54 +09:00
|
|
|
for (curChunk = beginChunk; curChunk < endChunk; ++curChunk) {
|
2014-09-06 10:05:12 +09:00
|
|
|
if (!this.loadedChunks[curChunk]) {
|
2014-04-08 06:42:54 +09:00
|
|
|
this.loadedChunks[curChunk] = true;
|
2013-02-07 08:19:29 +09:00
|
|
|
++this.numChunksLoaded;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-09-06 10:02:54 +09:00
|
|
|
onReceiveProgressiveData:
|
|
|
|
function ChunkedStream_onReceiveProgressiveData(data) {
|
|
|
|
var position = this.progressiveDataLength;
|
|
|
|
var beginChunk = Math.floor(position / this.chunkSize);
|
|
|
|
|
|
|
|
this.bytes.set(new Uint8Array(data), position);
|
|
|
|
position += data.byteLength;
|
|
|
|
this.progressiveDataLength = position;
|
|
|
|
var endChunk = position >= this.end ? this.numChunks :
|
|
|
|
Math.floor(position / this.chunkSize);
|
|
|
|
var curChunk;
|
|
|
|
for (curChunk = beginChunk; curChunk < endChunk; ++curChunk) {
|
2014-09-06 10:05:12 +09:00
|
|
|
if (!this.loadedChunks[curChunk]) {
|
2014-09-06 10:02:54 +09:00
|
|
|
this.loadedChunks[curChunk] = true;
|
|
|
|
++this.numChunksLoaded;
|
|
|
|
}
|
2013-11-19 04:17:26 +09:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-08-19 16:33:57 +09:00
|
|
|
ensureByte: function ChunkedStream_ensureByte(pos) {
|
2014-06-18 13:20:58 +09:00
|
|
|
var chunk = Math.floor(pos / this.chunkSize);
|
|
|
|
if (chunk === this.lastSuccessfulEnsureByteChunk) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-06 10:05:12 +09:00
|
|
|
if (!this.loadedChunks[chunk]) {
|
2014-06-18 13:20:58 +09:00
|
|
|
throw new MissingDataException(pos, pos + 1);
|
|
|
|
}
|
|
|
|
this.lastSuccessfulEnsureByteChunk = chunk;
|
|
|
|
},
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
ensureRange: function ChunkedStream_ensureRange(begin, end) {
|
|
|
|
if (begin >= end) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-06 10:02:54 +09:00
|
|
|
if (end <= this.progressiveDataLength) {
|
2013-11-19 04:17:26 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
var chunkSize = this.chunkSize;
|
|
|
|
var beginChunk = Math.floor(begin / chunkSize);
|
|
|
|
var endChunk = Math.floor((end - 1) / chunkSize) + 1;
|
|
|
|
for (var chunk = beginChunk; chunk < endChunk; ++chunk) {
|
2014-09-06 10:05:12 +09:00
|
|
|
if (!this.loadedChunks[chunk]) {
|
2013-02-07 08:19:29 +09:00
|
|
|
throw new MissingDataException(begin, end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
nextEmptyChunk: function ChunkedStream_nextEmptyChunk(beginChunk) {
|
2014-10-01 02:42:50 +09:00
|
|
|
var chunk, numChunks = this.numChunks;
|
|
|
|
for (var i = 0; i < numChunks; ++i) {
|
|
|
|
chunk = (beginChunk + i) % numChunks; // Wrap around to beginning
|
2014-09-06 10:05:12 +09:00
|
|
|
if (!this.loadedChunks[chunk]) {
|
2013-02-07 08:19:29 +09:00
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
hasChunk: function ChunkedStream_hasChunk(chunk) {
|
2014-09-06 10:05:12 +09:00
|
|
|
return !!this.loadedChunks[chunk];
|
2013-02-07 08:19:29 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
get length() {
|
|
|
|
return this.end - this.start;
|
|
|
|
},
|
|
|
|
|
2014-05-18 07:31:47 +09:00
|
|
|
get isEmpty() {
|
|
|
|
return this.length === 0;
|
|
|
|
},
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
getByte: function ChunkedStream_getByte() {
|
|
|
|
var pos = this.pos;
|
|
|
|
if (pos >= this.end) {
|
2013-07-01 05:45:15 +09:00
|
|
|
return -1;
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
2014-08-07 19:07:45 +09:00
|
|
|
this.ensureByte(pos);
|
|
|
|
return this.bytes[this.pos++];
|
2013-02-07 08:19:29 +09:00
|
|
|
},
|
|
|
|
|
2014-03-12 13:09:49 +09:00
|
|
|
getUint16: function ChunkedStream_getUint16() {
|
|
|
|
var b0 = this.getByte();
|
|
|
|
var b1 = this.getByte();
|
2015-01-10 02:15:17 +09:00
|
|
|
if (b0 === -1 || b1 === -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-03-12 13:09:49 +09:00
|
|
|
return (b0 << 8) + b1;
|
|
|
|
},
|
|
|
|
|
2014-04-17 04:31:16 +09:00
|
|
|
getInt32: function ChunkedStream_getInt32() {
|
2014-03-12 13:09:49 +09:00
|
|
|
var b0 = this.getByte();
|
|
|
|
var b1 = this.getByte();
|
|
|
|
var b2 = this.getByte();
|
|
|
|
var b3 = this.getByte();
|
|
|
|
return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
|
|
|
|
},
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
// returns subarray of original buffer
|
|
|
|
// should only be read
|
|
|
|
getBytes: function ChunkedStream_getBytes(length) {
|
|
|
|
var bytes = this.bytes;
|
|
|
|
var pos = this.pos;
|
|
|
|
var strEnd = this.end;
|
|
|
|
|
|
|
|
if (!length) {
|
|
|
|
this.ensureRange(pos, strEnd);
|
|
|
|
return bytes.subarray(pos, strEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
var end = pos + length;
|
2014-03-23 04:21:22 +09:00
|
|
|
if (end > strEnd) {
|
2013-02-07 08:19:29 +09:00
|
|
|
end = strEnd;
|
2014-03-23 04:21:22 +09:00
|
|
|
}
|
2013-02-07 08:19:29 +09:00
|
|
|
this.ensureRange(pos, end);
|
|
|
|
|
|
|
|
this.pos = end;
|
|
|
|
return bytes.subarray(pos, end);
|
|
|
|
},
|
|
|
|
|
2014-09-11 23:33:49 +09:00
|
|
|
peekByte: function ChunkedStream_peekByte() {
|
|
|
|
var peekedByte = this.getByte();
|
|
|
|
this.pos--;
|
|
|
|
return peekedByte;
|
|
|
|
},
|
|
|
|
|
2013-06-23 03:21:19 +09:00
|
|
|
peekBytes: function ChunkedStream_peekBytes(length) {
|
|
|
|
var bytes = this.getBytes(length);
|
|
|
|
this.pos -= bytes.length;
|
|
|
|
return bytes;
|
|
|
|
},
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
getByteRange: function ChunkedStream_getBytes(begin, end) {
|
|
|
|
this.ensureRange(begin, end);
|
|
|
|
return this.bytes.subarray(begin, end);
|
|
|
|
},
|
|
|
|
|
|
|
|
skip: function ChunkedStream_skip(n) {
|
2014-03-23 04:21:22 +09:00
|
|
|
if (!n) {
|
2013-02-07 08:19:29 +09:00
|
|
|
n = 1;
|
2014-03-23 04:21:22 +09:00
|
|
|
}
|
2013-02-07 08:19:29 +09:00
|
|
|
this.pos += n;
|
|
|
|
},
|
|
|
|
|
|
|
|
reset: function ChunkedStream_reset() {
|
|
|
|
this.pos = this.start;
|
|
|
|
},
|
|
|
|
|
|
|
|
moveStart: function ChunkedStream_moveStart() {
|
|
|
|
this.start = this.pos;
|
|
|
|
},
|
|
|
|
|
|
|
|
makeSubStream: function ChunkedStream_makeSubStream(start, length, dict) {
|
2014-03-07 09:14:21 +09:00
|
|
|
this.ensureRange(start, start + length);
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
function ChunkedStreamSubstream() {}
|
|
|
|
ChunkedStreamSubstream.prototype = Object.create(this);
|
2013-06-05 09:57:52 +09:00
|
|
|
ChunkedStreamSubstream.prototype.getMissingChunks = function() {
|
|
|
|
var chunkSize = this.chunkSize;
|
|
|
|
var beginChunk = Math.floor(this.start / chunkSize);
|
|
|
|
var endChunk = Math.floor((this.end - 1) / chunkSize) + 1;
|
|
|
|
var missingChunks = [];
|
|
|
|
for (var chunk = beginChunk; chunk < endChunk; ++chunk) {
|
2014-09-06 10:05:12 +09:00
|
|
|
if (!this.loadedChunks[chunk]) {
|
2013-06-05 09:57:52 +09:00
|
|
|
missingChunks.push(chunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return missingChunks;
|
|
|
|
};
|
2013-02-07 08:19:29 +09:00
|
|
|
var subStream = new ChunkedStreamSubstream();
|
|
|
|
subStream.pos = subStream.start = start;
|
|
|
|
subStream.end = start + length || this.end;
|
|
|
|
subStream.dict = dict;
|
|
|
|
return subStream;
|
|
|
|
},
|
|
|
|
|
|
|
|
isStream: true
|
|
|
|
};
|
|
|
|
|
|
|
|
return ChunkedStream;
|
|
|
|
})();
|
|
|
|
|
|
|
|
var ChunkedStreamManager = (function ChunkedStreamManagerClosure() {
|
|
|
|
|
|
|
|
function ChunkedStreamManager(length, chunkSize, url, args) {
|
2013-06-05 09:57:52 +09:00
|
|
|
this.stream = new ChunkedStream(length, chunkSize, this);
|
2013-02-07 08:19:29 +09:00
|
|
|
this.length = length;
|
|
|
|
this.chunkSize = chunkSize;
|
|
|
|
this.url = url;
|
2013-04-13 03:37:49 +09:00
|
|
|
this.disableAutoFetch = args.disableAutoFetch;
|
2013-02-07 08:19:29 +09:00
|
|
|
var msgHandler = this.msgHandler = args.msgHandler;
|
|
|
|
|
|
|
|
if (args.chunkedViewerLoading) {
|
|
|
|
msgHandler.on('OnDataRange', this.onReceiveData.bind(this));
|
2013-04-20 05:53:22 +09:00
|
|
|
msgHandler.on('OnDataProgress', this.onProgress.bind(this));
|
2013-02-07 08:19:29 +09:00
|
|
|
this.sendRequest = function ChunkedStreamManager_sendRequest(begin, end) {
|
|
|
|
msgHandler.send('RequestDataRange', { begin: begin, end: end });
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
|
|
|
|
var getXhr = function getXhr() {
|
|
|
|
return new XMLHttpRequest();
|
|
|
|
};
|
|
|
|
this.networkManager = new NetworkManager(this.url, {
|
|
|
|
getXhr: getXhr,
|
2014-01-15 17:57:17 +09:00
|
|
|
httpHeaders: args.httpHeaders,
|
|
|
|
withCredentials: args.withCredentials
|
2013-02-07 08:19:29 +09:00
|
|
|
});
|
|
|
|
this.sendRequest = function ChunkedStreamManager_sendRequest(begin, end) {
|
|
|
|
this.networkManager.requestRange(begin, end, {
|
|
|
|
onDone: this.onReceiveData.bind(this),
|
2013-04-20 05:53:22 +09:00
|
|
|
onProgress: this.onProgress.bind(this)
|
2013-02-07 08:19:29 +09:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.currRequestId = 0;
|
|
|
|
|
|
|
|
this.chunksNeededByRequest = {};
|
|
|
|
this.requestsByChunk = {};
|
2015-10-21 07:45:55 +09:00
|
|
|
this.promisesByRequest = {};
|
2014-09-06 10:02:54 +09:00
|
|
|
this.progressiveDataLength = 0;
|
2013-02-07 08:19:29 +09:00
|
|
|
|
2014-04-23 23:33:42 +09:00
|
|
|
this._loadedStreamCapability = createPromiseCapability();
|
|
|
|
|
2013-11-19 04:17:26 +09:00
|
|
|
if (args.initialData) {
|
2014-09-06 10:02:54 +09:00
|
|
|
this.onReceiveData({chunk: args.initialData});
|
2013-11-19 04:17:26 +09:00
|
|
|
}
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
ChunkedStreamManager.prototype = {
|
|
|
|
onLoadedStream: function ChunkedStreamManager_getLoadedStream() {
|
2014-04-23 23:33:42 +09:00
|
|
|
return this._loadedStreamCapability.promise;
|
2013-02-07 08:19:29 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
// Get all the chunks that are not yet loaded and groups them into
|
|
|
|
// contiguous ranges to load in as few requests as possible
|
|
|
|
requestAllChunks: function ChunkedStreamManager_requestAllChunks() {
|
|
|
|
var missingChunks = this.stream.getMissingChunks();
|
2015-10-21 07:45:55 +09:00
|
|
|
this._requestChunks(missingChunks);
|
2014-04-23 23:33:42 +09:00
|
|
|
return this._loadedStreamCapability.promise;
|
2013-02-07 08:19:29 +09:00
|
|
|
},
|
|
|
|
|
2015-10-21 07:45:55 +09:00
|
|
|
_requestChunks: function ChunkedStreamManager_requestChunks(chunks) {
|
2013-02-07 08:19:29 +09:00
|
|
|
var requestId = this.currRequestId++;
|
|
|
|
|
|
|
|
var chunksNeeded;
|
2014-04-08 06:42:54 +09:00
|
|
|
var i, ii;
|
2013-02-07 08:19:29 +09:00
|
|
|
this.chunksNeededByRequest[requestId] = chunksNeeded = {};
|
2014-04-08 06:42:54 +09:00
|
|
|
for (i = 0, ii = chunks.length; i < ii; i++) {
|
2013-06-05 09:57:52 +09:00
|
|
|
if (!this.stream.hasChunk(chunks[i])) {
|
|
|
|
chunksNeeded[chunks[i]] = true;
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isEmptyObj(chunksNeeded)) {
|
2015-10-21 07:45:55 +09:00
|
|
|
return Promise.resolve();
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
|
|
|
|
2015-10-21 07:45:55 +09:00
|
|
|
var capability = createPromiseCapability();
|
|
|
|
this.promisesByRequest[requestId] = capability;
|
2013-02-07 08:19:29 +09:00
|
|
|
|
|
|
|
var chunksToRequest = [];
|
|
|
|
for (var chunk in chunksNeeded) {
|
|
|
|
chunk = chunk | 0;
|
|
|
|
if (!(chunk in this.requestsByChunk)) {
|
|
|
|
this.requestsByChunk[chunk] = [];
|
|
|
|
chunksToRequest.push(chunk);
|
|
|
|
}
|
|
|
|
this.requestsByChunk[chunk].push(requestId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!chunksToRequest.length) {
|
2015-10-21 07:45:55 +09:00
|
|
|
return capability.promise;
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
var groupedChunksToRequest = this.groupChunks(chunksToRequest);
|
|
|
|
|
2014-04-08 06:42:54 +09:00
|
|
|
for (i = 0; i < groupedChunksToRequest.length; ++i) {
|
2013-02-07 08:19:29 +09:00
|
|
|
var groupedChunk = groupedChunksToRequest[i];
|
|
|
|
var begin = groupedChunk.beginChunk * this.chunkSize;
|
2013-04-20 08:27:39 +09:00
|
|
|
var end = Math.min(groupedChunk.endChunk * this.chunkSize, this.length);
|
2013-02-07 08:19:29 +09:00
|
|
|
this.sendRequest(begin, end);
|
|
|
|
}
|
2015-10-21 07:45:55 +09:00
|
|
|
|
|
|
|
return capability.promise;
|
2013-02-07 08:19:29 +09:00
|
|
|
},
|
|
|
|
|
2013-06-05 09:57:52 +09:00
|
|
|
getStream: function ChunkedStreamManager_getStream() {
|
|
|
|
return this.stream;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Loads any chunks in the requested range that are not yet loaded
|
2015-10-21 07:45:55 +09:00
|
|
|
requestRange: function ChunkedStreamManager_requestRange(begin, end) {
|
2013-06-05 09:57:52 +09:00
|
|
|
|
|
|
|
end = Math.min(end, this.length);
|
|
|
|
|
|
|
|
var beginChunk = this.getBeginChunk(begin);
|
|
|
|
var endChunk = this.getEndChunk(end);
|
|
|
|
|
|
|
|
var chunks = [];
|
|
|
|
for (var chunk = beginChunk; chunk < endChunk; ++chunk) {
|
|
|
|
chunks.push(chunk);
|
|
|
|
}
|
|
|
|
|
2015-10-21 07:45:55 +09:00
|
|
|
return this._requestChunks(chunks);
|
2013-06-05 09:57:52 +09:00
|
|
|
},
|
|
|
|
|
2015-10-21 07:45:55 +09:00
|
|
|
requestRanges: function ChunkedStreamManager_requestRanges(ranges) {
|
2013-06-05 09:57:52 +09:00
|
|
|
ranges = ranges || [];
|
|
|
|
var chunksToRequest = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < ranges.length; i++) {
|
|
|
|
var beginChunk = this.getBeginChunk(ranges[i].begin);
|
|
|
|
var endChunk = this.getEndChunk(ranges[i].end);
|
|
|
|
for (var chunk = beginChunk; chunk < endChunk; ++chunk) {
|
|
|
|
if (chunksToRequest.indexOf(chunk) < 0) {
|
|
|
|
chunksToRequest.push(chunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chunksToRequest.sort(function(a, b) { return a - b; });
|
2015-10-21 07:45:55 +09:00
|
|
|
return this._requestChunks(chunksToRequest);
|
2013-06-05 09:57:52 +09:00
|
|
|
},
|
|
|
|
|
2014-10-01 02:42:50 +09:00
|
|
|
// Groups a sorted array of chunks into as few contiguous larger
|
2013-02-07 08:19:29 +09:00
|
|
|
// chunks as possible
|
|
|
|
groupChunks: function ChunkedStreamManager_groupChunks(chunks) {
|
|
|
|
var groupedChunks = [];
|
2013-11-24 03:03:47 +09:00
|
|
|
var beginChunk = -1;
|
|
|
|
var prevChunk = -1;
|
2013-02-07 08:19:29 +09:00
|
|
|
for (var i = 0; i < chunks.length; ++i) {
|
|
|
|
var chunk = chunks[i];
|
|
|
|
|
2013-11-24 03:03:47 +09:00
|
|
|
if (beginChunk < 0) {
|
2013-02-07 08:19:29 +09:00
|
|
|
beginChunk = chunk;
|
|
|
|
}
|
|
|
|
|
2013-11-24 03:03:47 +09:00
|
|
|
if (prevChunk >= 0 && prevChunk + 1 !== chunk) {
|
2014-03-23 04:21:22 +09:00
|
|
|
groupedChunks.push({ beginChunk: beginChunk,
|
|
|
|
endChunk: prevChunk + 1 });
|
2013-02-07 08:19:29 +09:00
|
|
|
beginChunk = chunk;
|
|
|
|
}
|
|
|
|
if (i + 1 === chunks.length) {
|
2014-03-23 04:21:22 +09:00
|
|
|
groupedChunks.push({ beginChunk: beginChunk,
|
|
|
|
endChunk: chunk + 1 });
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
prevChunk = chunk;
|
|
|
|
}
|
|
|
|
return groupedChunks;
|
|
|
|
},
|
|
|
|
|
2013-04-20 05:53:22 +09:00
|
|
|
onProgress: function ChunkedStreamManager_onProgress(args) {
|
2014-03-23 04:21:22 +09:00
|
|
|
var bytesLoaded = (this.stream.numChunksLoaded * this.chunkSize +
|
|
|
|
args.loaded);
|
2013-04-20 05:53:22 +09:00
|
|
|
this.msgHandler.send('DocProgress', {
|
|
|
|
loaded: bytesLoaded,
|
|
|
|
total: this.length
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
onReceiveData: function ChunkedStreamManager_onReceiveData(args) {
|
|
|
|
var chunk = args.chunk;
|
2014-09-06 10:02:54 +09:00
|
|
|
var isProgressive = args.begin === undefined;
|
|
|
|
var begin = isProgressive ? this.progressiveDataLength : args.begin;
|
2013-02-07 08:19:29 +09:00
|
|
|
var end = begin + chunk.byteLength;
|
|
|
|
|
2014-09-06 10:02:54 +09:00
|
|
|
var beginChunk = Math.floor(begin / this.chunkSize);
|
|
|
|
var endChunk = end < this.length ? Math.floor(end / this.chunkSize) :
|
|
|
|
Math.ceil(end / this.chunkSize);
|
|
|
|
|
|
|
|
if (isProgressive) {
|
|
|
|
this.stream.onReceiveProgressiveData(chunk);
|
|
|
|
this.progressiveDataLength = end;
|
|
|
|
} else {
|
|
|
|
this.stream.onReceiveData(begin, chunk);
|
|
|
|
}
|
2013-02-07 08:19:29 +09:00
|
|
|
|
|
|
|
if (this.stream.allChunksLoaded()) {
|
2014-04-23 23:33:42 +09:00
|
|
|
this._loadedStreamCapability.resolve(this.stream);
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
var loadedRequests = [];
|
2014-04-08 06:42:54 +09:00
|
|
|
var i, requestId;
|
|
|
|
for (chunk = beginChunk; chunk < endChunk; ++chunk) {
|
2013-04-26 02:57:40 +09:00
|
|
|
// The server might return more chunks than requested
|
|
|
|
var requestIds = this.requestsByChunk[chunk] || [];
|
2013-02-07 08:19:29 +09:00
|
|
|
delete this.requestsByChunk[chunk];
|
|
|
|
|
2014-04-08 06:42:54 +09:00
|
|
|
for (i = 0; i < requestIds.length; ++i) {
|
|
|
|
requestId = requestIds[i];
|
2013-02-07 08:19:29 +09:00
|
|
|
var chunksNeeded = this.chunksNeededByRequest[requestId];
|
|
|
|
if (chunk in chunksNeeded) {
|
|
|
|
delete chunksNeeded[chunk];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isEmptyObj(chunksNeeded)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadedRequests.push(requestId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no pending requests, automatically fetch the next
|
|
|
|
// unfetched chunk of the PDF
|
2013-04-13 03:37:49 +09:00
|
|
|
if (!this.disableAutoFetch && isEmptyObj(this.requestsByChunk)) {
|
2013-02-07 08:19:29 +09:00
|
|
|
var nextEmptyChunk;
|
|
|
|
if (this.stream.numChunksLoaded === 1) {
|
|
|
|
// This is a special optimization so that after fetching the first
|
|
|
|
// chunk, rather than fetching the second chunk, we fetch the last
|
|
|
|
// chunk.
|
|
|
|
var lastChunk = this.stream.numChunks - 1;
|
|
|
|
if (!this.stream.hasChunk(lastChunk)) {
|
|
|
|
nextEmptyChunk = lastChunk;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nextEmptyChunk = this.stream.nextEmptyChunk(endChunk);
|
|
|
|
}
|
|
|
|
if (isInt(nextEmptyChunk)) {
|
2015-10-21 07:45:55 +09:00
|
|
|
this._requestChunks([nextEmptyChunk]);
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-08 06:42:54 +09:00
|
|
|
for (i = 0; i < loadedRequests.length; ++i) {
|
|
|
|
requestId = loadedRequests[i];
|
2015-10-21 07:45:55 +09:00
|
|
|
var capability = this.promisesByRequest[requestId];
|
|
|
|
delete this.promisesByRequest[requestId];
|
|
|
|
capability.resolve();
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
this.msgHandler.send('DocProgress', {
|
|
|
|
loaded: this.stream.numChunksLoaded * this.chunkSize,
|
|
|
|
total: this.length
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-05-02 01:20:55 +09:00
|
|
|
onError: function ChunkedStreamManager_onError(err) {
|
|
|
|
this._loadedStreamCapability.reject(err);
|
|
|
|
},
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
getBeginChunk: function ChunkedStreamManager_getBeginChunk(begin) {
|
|
|
|
var chunk = Math.floor(begin / this.chunkSize);
|
|
|
|
return chunk;
|
|
|
|
},
|
|
|
|
|
|
|
|
getEndChunk: function ChunkedStreamManager_getEndChunk(end) {
|
|
|
|
var chunk = Math.floor((end - 1) / this.chunkSize) + 1;
|
|
|
|
return chunk;
|
2015-10-21 07:45:55 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
abort: function ChunkedStreamManager_abort() {
|
|
|
|
if (this.networkManager) {
|
|
|
|
this.networkManager.abortAllRequests();
|
|
|
|
}
|
|
|
|
for(var requestId in this.promisesByRequest) {
|
|
|
|
var capability = this.promisesByRequest[requestId];
|
|
|
|
capability.reject(new Error('Request was aborted'));
|
|
|
|
}
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return ChunkedStreamManager;
|
|
|
|
})();
|