29 changed files with 3342 additions and 1310 deletions
@ -0,0 +1,441 @@
@@ -0,0 +1,441 @@
|
||||
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
||||
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ |
||||
/* 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. |
||||
*/ |
||||
/* globals assert, MissingDataException, isInt, NetworkManager, Promise, |
||||
isEmptyObj */ |
||||
|
||||
'use strict'; |
||||
|
||||
var ChunkedStream = (function ChunkedStreamClosure() { |
||||
function ChunkedStream(length, chunkSize) { |
||||
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); |
||||
} |
||||
|
||||
// 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) { |
||||
if (!(chunk in this.loadedChunks)) { |
||||
chunks.push(chunk); |
||||
} |
||||
} |
||||
return chunks; |
||||
}, |
||||
|
||||
allChunksLoaded: function ChunkedStream_allChunksLoaded() { |
||||
return this.numChunksLoaded === this.numChunks; |
||||
}, |
||||
|
||||
onReceiveData: function(begin, chunk) { |
||||
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, |
||||
'Bad end offset: ' + end); |
||||
|
||||
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; |
||||
|
||||
for (var chunk = beginChunk; chunk < endChunk; ++chunk) { |
||||
if (!(chunk in this.loadedChunks)) { |
||||
this.loadedChunks[chunk] = true; |
||||
++this.numChunksLoaded; |
||||
} |
||||
} |
||||
}, |
||||
|
||||
ensureRange: function ChunkedStream_ensureRange(begin, end) { |
||||
if (begin >= end) { |
||||
return; |
||||
} |
||||
|
||||
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) { |
||||
if (!(chunk in this.loadedChunks)) { |
||||
throw new MissingDataException(begin, end); |
||||
} |
||||
} |
||||
}, |
||||
|
||||
nextEmptyChunk: function ChunkedStream_nextEmptyChunk(beginChunk) { |
||||
for (var chunk = beginChunk, n = this.numChunks; chunk < n; ++chunk) { |
||||
if (!(chunk in this.loadedChunks)) { |
||||
return chunk; |
||||
} |
||||
} |
||||
// Wrap around to beginning
|
||||
for (var chunk = 0; chunk < beginChunk; ++chunk) { |
||||
if (!(chunk in this.loadedChunks)) { |
||||
return chunk; |
||||
} |
||||
} |
||||
return null; |
||||
}, |
||||
|
||||
hasChunk: function ChunkedStream_hasChunk(chunk) { |
||||
return chunk in this.loadedChunks; |
||||
}, |
||||
|
||||
get length() { |
||||
return this.end - this.start; |
||||
}, |
||||
|
||||
getByte: function ChunkedStream_getByte() { |
||||
var pos = this.pos; |
||||
if (pos >= this.end) { |
||||
return null; |
||||
} |
||||
this.ensureRange(pos, pos + 1); |
||||
return this.bytes[this.pos++]; |
||||
}, |
||||
|
||||
// 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; |
||||
if (end > strEnd) |
||||
end = strEnd; |
||||
this.ensureRange(pos, end); |
||||
|
||||
this.pos = end; |
||||
return bytes.subarray(pos, end); |
||||
}, |
||||
|
||||
getByteRange: function ChunkedStream_getBytes(begin, end) { |
||||
this.ensureRange(begin, end); |
||||
return this.bytes.subarray(begin, end); |
||||
}, |
||||
|
||||
lookChar: function ChunkedStream_lookChar() { |
||||
var pos = this.pos; |
||||
if (pos >= this.end) |
||||
return null; |
||||
this.ensureRange(pos, pos + 1); |
||||
return String.fromCharCode(this.bytes[pos]); |
||||
}, |
||||
|
||||
getChar: function ChunkedStream_getChar() { |
||||
var pos = this.pos; |
||||
if (pos >= this.end) |
||||
return null; |
||||
this.ensureRange(pos, pos + 1); |
||||
return String.fromCharCode(this.bytes[this.pos++]); |
||||
}, |
||||
|
||||
skip: function ChunkedStream_skip(n) { |
||||
if (!n) |
||||
n = 1; |
||||
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) { |
||||
function ChunkedStreamSubstream() {} |
||||
ChunkedStreamSubstream.prototype = Object.create(this); |
||||
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) { |
||||
this.stream = new ChunkedStream(length, chunkSize); |
||||
this.length = length; |
||||
this.chunkSize = chunkSize; |
||||
this.url = url; |
||||
this.disableAutoFetch = args.disableAutoFetch; |
||||
var msgHandler = this.msgHandler = args.msgHandler; |
||||
|
||||
if (args.chunkedViewerLoading) { |
||||
msgHandler.on('OnDataRange', this.onReceiveData.bind(this)); |
||||
this.sendRequest = function ChunkedStreamManager_sendRequest(begin, end) { |
||||
msgHandler.send('RequestDataRange', { begin: begin, end: end }); |
||||
}; |
||||
} else { |
||||
|
||||
var getXhr = function getXhr() { |
||||
//#if B2G
|
||||
// return new XMLHttpRequest({ mozSystem: true });
|
||||
//#else
|
||||
return new XMLHttpRequest(); |
||||
//#endif
|
||||
}; |
||||
this.networkManager = new NetworkManager(this.url, { |
||||
getXhr: getXhr, |
||||
httpHeaders: args.httpHeaders |
||||
}); |
||||
var self = this; |
||||
this.sendRequest = function ChunkedStreamManager_sendRequest(begin, end) { |
||||
this.networkManager.requestRange(begin, end, { |
||||
onDone: this.onReceiveData.bind(this), |
||||
}); |
||||
}; |
||||
} |
||||
|
||||
this.currRequestId = 0; |
||||
|
||||
this.chunksNeededByRequest = {}; |
||||
this.requestsByChunk = {}; |
||||
this.callbacksByRequest = {}; |
||||
|
||||
this.loadedStream = new Promise(); |
||||
} |
||||
|
||||
ChunkedStreamManager.prototype = { |
||||
|
||||
onLoadedStream: function ChunkedStreamManager_getLoadedStream() { |
||||
return this.loadedStream; |
||||
}, |
||||
|
||||
// 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(); |
||||
var chunksToRequest = []; |
||||
for (var i = 0, n = missingChunks.length; i < n; ++i) { |
||||
var chunk = missingChunks[i]; |
||||
if (!(chunk in this.requestsByChunk)) { |
||||
this.requestsByChunk[chunk] = []; |
||||
chunksToRequest.push(chunk); |
||||
} |
||||
} |
||||
var groupedChunks = this.groupChunks(chunksToRequest); |
||||
for (var i = 0, n = groupedChunks.length; i < n; ++i) { |
||||
var groupedChunk = groupedChunks[i]; |
||||
var begin = groupedChunk.beginChunk * this.chunkSize; |
||||
var end = groupedChunk.endChunk * this.chunkSize; |
||||
this.sendRequest(begin, end); |
||||
} |
||||
|
||||
return this.loadedStream; |
||||
}, |
||||
|
||||
getStream: function ChunkedStreamManager_getStream() { |
||||
return this.stream; |
||||
}, |
||||
|
||||
// Loads any chunks in the requested range that are not yet loaded
|
||||
requestRange: function ChunkedStreamManager_requestRange( |
||||
begin, end, callback) { |
||||
|
||||
end = Math.min(end, this.length); |
||||
|
||||
var beginChunk = this.getBeginChunk(begin); |
||||
var endChunk = this.getEndChunk(end); |
||||
|
||||
var requestId = this.currRequestId++; |
||||
|
||||
var chunksNeeded; |
||||
this.chunksNeededByRequest[requestId] = chunksNeeded = {}; |
||||
for (var chunk = beginChunk; chunk < endChunk; ++chunk) { |
||||
if (!this.stream.hasChunk(chunk)) { |
||||
chunksNeeded[chunk] = true; |
||||
} |
||||
} |
||||
|
||||
if (isEmptyObj(chunksNeeded)) { |
||||
callback(); |
||||
return; |
||||
} |
||||
|
||||
this.callbacksByRequest[requestId] = callback; |
||||
|
||||
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) { |
||||
return; |
||||
} |
||||
|
||||
var groupedChunksToRequest = this.groupChunks(chunksToRequest); |
||||
|
||||
for (var i = 0; i < groupedChunksToRequest.length; ++i) { |
||||
var groupedChunk = groupedChunksToRequest[i]; |
||||
var begin = groupedChunk.beginChunk * this.chunkSize; |
||||
var end = groupedChunk.endChunk * this.chunkSize; |
||||
this.sendRequest(begin, end); |
||||
} |
||||
}, |
||||
|
||||
// Groups a sorted array of chunks into as few continguous larger
|
||||
// chunks as possible
|
||||
groupChunks: function ChunkedStreamManager_groupChunks(chunks) { |
||||
var groupedChunks = []; |
||||
var beginChunk; |
||||
var prevChunk; |
||||
for (var i = 0; i < chunks.length; ++i) { |
||||
var chunk = chunks[i]; |
||||
|
||||
if (!beginChunk) { |
||||
beginChunk = chunk; |
||||
} |
||||
|
||||
if (prevChunk && prevChunk + 1 !== chunk) { |
||||
groupedChunks.push({ |
||||
beginChunk: beginChunk, endChunk: prevChunk + 1}); |
||||
beginChunk = chunk; |
||||
} |
||||
if (i + 1 === chunks.length) { |
||||
groupedChunks.push({ |
||||
beginChunk: beginChunk, endChunk: chunk + 1}); |
||||
} |
||||
|
||||
prevChunk = chunk; |
||||
} |
||||
return groupedChunks; |
||||
}, |
||||
|
||||
onReceiveData: function ChunkedStreamManager_onReceiveData(args) { |
||||
var chunk = args.chunk; |
||||
var begin = args.begin; |
||||
var end = begin + chunk.byteLength; |
||||
|
||||
var beginChunk = this.getBeginChunk(begin); |
||||
var endChunk = this.getEndChunk(end); |
||||
|
||||
this.stream.onReceiveData(begin, chunk); |
||||
if (this.stream.allChunksLoaded()) { |
||||
this.loadedStream.resolve(this.stream); |
||||
} |
||||
|
||||
var loadedRequests = []; |
||||
for (var chunk = beginChunk; chunk < endChunk; ++chunk) { |
||||
|
||||
var requestIds = this.requestsByChunk[chunk]; |
||||
delete this.requestsByChunk[chunk]; |
||||
|
||||
for (var i = 0; i < requestIds.length; ++i) { |
||||
var requestId = requestIds[i]; |
||||
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
|
||||
if (!this.disableAutoFetch && isEmptyObj(this.requestsByChunk)) { |
||||
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)) { |
||||
var nextEmptyByte = nextEmptyChunk * this.chunkSize; |
||||
this.requestRange(nextEmptyByte, nextEmptyByte + this.chunkSize, |
||||
function() {}); |
||||
} |
||||
} |
||||
|
||||
for (var i = 0; i < loadedRequests.length; ++i) { |
||||
var requestId = loadedRequests[i]; |
||||
var callback = this.callbacksByRequest[requestId]; |
||||
delete this.callbacksByRequest[requestId]; |
||||
callback(); |
||||
} |
||||
|
||||
this.msgHandler.send('DocProgress', { |
||||
loaded: this.stream.numChunksLoaded * this.chunkSize, |
||||
total: this.length |
||||
}); |
||||
}, |
||||
|
||||
getBeginChunk: function ChunkedStreamManager_getBeginChunk(begin) { |
||||
var chunk = Math.floor(begin / this.chunkSize); |
||||
return chunk; |
||||
}, |
||||
|
||||
getEndChunk: function ChunkedStreamManager_getEndChunk(end) { |
||||
if (end % this.chunkSize === 0) { |
||||
return end / this.chunkSize; |
||||
} |
||||
|
||||
// 0 -> 0
|
||||
// 1 -> 1
|
||||
// 99 -> 1
|
||||
// 100 -> 1
|
||||
// 101 -> 2
|
||||
var chunk = Math.floor((end - 1) / this.chunkSize) + 1; |
||||
return chunk; |
||||
} |
||||
}; |
||||
|
||||
return ChunkedStreamManager; |
||||
})(); |
||||
|
@ -0,0 +1,224 @@
@@ -0,0 +1,224 @@
|
||||
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
||||
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ |
||||
/* 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. |
||||
*/ |
||||
|
||||
// NOTE: Be careful what goes in this file, as it is also used from the context
|
||||
// of the addon. So using warn/error in here will break the addon.
|
||||
|
||||
'use strict'; |
||||
|
||||
|
||||
//#if (FIREFOX || MOZCENTRAL)
|
||||
//
|
||||
//Components.utils.import('resource://gre/modules/Services.jsm');
|
||||
//
|
||||
//var EXPORTED_SYMBOLS = ['NetworkManager'];
|
||||
//
|
||||
//function log(aMsg) {
|
||||
// var msg = 'network.js: ' + (aMsg.join ? aMsg.join('') : aMsg);
|
||||
// Services.console.logStringMessage(msg);
|
||||
// // TODO(mack): dump() doesn't seem to work here...
|
||||
// dump(msg + '\n');
|
||||
//}
|
||||
//#else
|
||||
function log(aMsg) { |
||||
console.log(aMsg); |
||||
} |
||||
//#endif
|
||||
|
||||
var NetworkManager = (function NetworkManagerClosure() { |
||||
function NetworkManager(url, args) { |
||||
this.url = url; |
||||
args = args || {}; |
||||
this.httpHeaders = args.httpHeaders || {}; |
||||
this.getXhr = args.getXhr || |
||||
function NetworkManager_getXhr() { |
||||
return new XMLHttpRequest(); |
||||
}; |
||||
|
||||
this.currXhrId = 0; |
||||
this.pendingRequests = {}; |
||||
this.loadedRequests = {}; |
||||
} |
||||
|
||||
function getArrayBuffer(xhr) { |
||||
var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse || |
||||
xhr.responseArrayBuffer || xhr.response); |
||||
if (typeof data !== 'string') { |
||||
return data; |
||||
} |
||||
var length = data.length; |
||||
var buffer = new Uint8Array(length); |
||||
for (var i = 0; i < length; i++) { |
||||
buffer[i] = data.charCodeAt(i) & 0xFF; |
||||
} |
||||
return buffer; |
||||
} |
||||
|
||||
NetworkManager.prototype = { |
||||
requestRange: function NetworkManager_requestRange(begin, end, listeners) { |
||||
var args = { |
||||
begin: begin, |
||||
end: end |
||||
}; |
||||
for (var prop in listeners) { |
||||
args[prop] = listeners[prop]; |
||||
} |
||||
return this.request(args); |
||||
}, |
||||
|
||||
requestFull: function NetworkManager_requestRange(listeners) { |
||||
return this.request(listeners); |
||||
}, |
||||
|
||||
request: function NetworkManager_requestRange(args) { |
||||
var xhr = this.getXhr(); |
||||
var xhrId = this.currXhrId++; |
||||
var pendingRequest = this.pendingRequests[xhrId] = { |
||||
xhr: xhr |
||||
}; |
||||
|
||||
xhr.open('GET', this.url); |
||||
for (var property in this.httpHeaders) { |
||||
var value = this.httpHeaders[property]; |
||||
if (typeof value === 'undefined') { |
||||
continue; |
||||
} |
||||
xhr.setRequestHeader(property, value); |
||||
} |
||||
if ('begin' in args && 'end' in args) { |
||||
var rangeStr = args.begin + '-' + (args.end - 1); |
||||
xhr.setRequestHeader('Range', 'bytes=' + rangeStr); |
||||
pendingRequest.expectedStatus = 206; |
||||
} else { |
||||
pendingRequest.expectedStatus = 200; |
||||
} |
||||
|
||||
xhr.mozResponseType = xhr.responseType = 'arraybuffer'; |
||||
|
||||
if (args.onProgress) { |
||||
xhr.onprogress = args.onProgress; |
||||
} |
||||
if (args.onError) { |
||||
xhr.onerror = function(evt) { |
||||
args.onError(xhr.status); |
||||
}; |
||||
} |
||||
xhr.onreadystatechange = this.onStateChange.bind(this, xhrId); |
||||
|
||||
pendingRequest.onHeadersReceived = args.onHeadersReceived; |
||||
pendingRequest.onDone = args.onDone; |
||||
pendingRequest.onError = args.onError; |
||||
|
||||
xhr.send(null); |
||||
|
||||
return xhrId; |
||||
}, |
||||
|
||||
onStateChange: function NetworkManager_onStateChange(xhrId, evt) { |
||||
var pendingRequest = this.pendingRequests[xhrId]; |
||||
if (!pendingRequest) { |
||||
// Maybe abortRequest was called...
|
||||
return; |
||||
} |
||||
|
||||
var xhr = pendingRequest.xhr; |
||||
if (xhr.readyState >= 2 && pendingRequest.onHeadersReceived) { |
||||
pendingRequest.onHeadersReceived(); |
||||
delete pendingRequest.onHeadersReceived; |
||||
} |
||||
|
||||
if (xhr.readyState !== 4) { |
||||
return; |
||||
} |
||||
|
||||
if (!(xhrId in this.pendingRequests)) { |
||||
// The XHR request might have been aborted in onHeadersReceived()
|
||||
// callback, in which case we should abort request
|
||||
return; |
||||
} |
||||
|
||||
delete this.pendingRequests[xhrId]; |
||||
|
||||
if (xhr.status === 0) { |
||||
if (pendingRequest.onError) { |
||||
pendingRequest.onError(xhr.status); |
||||
} |
||||
return; |
||||
} |
||||
|
||||
if (xhr.status !== pendingRequest.expectedStatus) { |
||||
if (pendingRequest.onError) { |
||||
pendingRequest.onError(xhr.status); |
||||
} |
||||
return; |
||||
} |
||||
|
||||
this.loadedRequests[xhrId] = true; |
||||
|
||||
var chunk = getArrayBuffer(xhr); |
||||
if (pendingRequest.expectedStatus === 206) { |
||||
var rangeHeader = xhr.getResponseHeader('Content-Range'); |
||||
var matches = /bytes (\d+)-(\d+)\/(\d+)/.exec(rangeHeader); |
||||
var begin = parseInt(matches[1], 10); |
||||
var end = parseInt(matches[2], 10) + 1; |
||||
pendingRequest.onDone({ |
||||
begin: begin, |
||||
end: end, |
||||
chunk: chunk |
||||
}); |
||||
} else { |
||||
pendingRequest.onDone({ |
||||
chunk: chunk |
||||
}); |
||||
} |
||||
}, |
||||
|
||||
hasPendingRequests: function NetworkManager_hasPendingRequests() { |
||||
for (var xhrId in this.pendingRequests) { |
||||
return true; |
||||
} |
||||
return false; |
||||
}, |
||||
|
||||
getRequestXhr: function NetworkManager_getXhr(xhrId) { |
||||
return this.pendingRequests[xhrId].xhr; |
||||
}, |
||||
|
||||
isPendingRequest: function NetworkManager_isPendingRequest(xhrId) { |
||||
return xhrId in this.pendingRequests; |
||||
}, |
||||
|
||||
isLoadedRequest: function NetworkManager_isLoadedRequest(xhrId) { |
||||
return xhrId in this.loadedRequests; |
||||
}, |
||||
|
||||
abortAllRequests: function NetworkManager_abortAllRequests() { |
||||
for (var xhrId in this.pendingRequests) { |
||||
this.abortRequest(xhrId | 0); |
||||
} |
||||
}, |
||||
|
||||
abortRequest: function NetworkManager_abortRequest(xhrId) { |
||||
var xhr = this.pendingRequests[xhrId].xhr; |
||||
delete this.pendingRequests[xhrId]; |
||||
xhr.abort(); |
||||
} |
||||
}; |
||||
|
||||
return NetworkManager; |
||||
})(); |
||||
|
@ -0,0 +1,190 @@
@@ -0,0 +1,190 @@
|
||||
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
||||
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ |
||||
/* 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. |
||||
*/ |
||||
/* globals NotImplementedException, MissingDataException, Promise, Stream, |
||||
PDFDocument, ChunkedStream, ChunkedStreamManager */ |
||||
|
||||
'use strict'; |
||||
|
||||
// TODO(mack): Make use of PDFJS.Util.inherit() when it becomes available
|
||||
var BasePdfManager = (function BasePdfManagerClosure() { |
||||
function BasePdfManager() { |
||||
throw new Error('Cannot initialize BaseManagerManager'); |
||||
} |
||||
|
||||
BasePdfManager.prototype = { |
||||
onLoadedStream: function BasePdfManager_onLoadedStream() { |
||||
throw new NotImplementedException(); |
||||
}, |
||||
|
||||
ensureModel: function BasePdfManager_ensureModel(prop, args) { |
||||
return this.ensure(this.pdfModel, prop, args); |
||||
}, |
||||
|
||||
ensureXRef: function BasePdfManager_ensureXRef(prop, args) { |
||||
return this.ensure(this.pdfModel.xref, prop, args); |
||||
}, |
||||
|
||||
ensureCatalog: function BasePdfManager_ensureCatalog(prop, args) { |
||||
return this.ensure(this.pdfModel.catalog, prop, args); |
||||
}, |
||||
|
||||
getPage: function BasePdfManager_pagePage(pageIndex) { |
||||
return this.pdfModel.getPage(pageIndex); |
||||
}, |
||||
|
||||
ensure: function BasePdfManager_ensure(obj, prop, args) { |
||||
return new NotImplementedException(); |
||||
}, |
||||
|
||||
requestRange: function BasePdfManager_ensure(begin, end) { |
||||
return new NotImplementedException(); |
||||
}, |
||||
|
||||
requestLoadedStream: function BasePdfManager_requestLoadedStream() { |
||||
return new NotImplementedException(); |
||||
} |
||||
}; |
||||
|
||||
return BasePdfManager; |
||||
})(); |
||||
|
||||
var LocalPdfManager = (function LocalPdfManagerClosure() { |
||||
function LocalPdfManager(data, password) { |
||||
var stream = new Stream(data); |
||||
this.pdfModel = new PDFDocument(this, stream, password); |
||||
this.loadedStream = new Promise(); |
||||
this.loadedStream.resolve(stream); |
||||
} |
||||
|
||||
LocalPdfManager.prototype = Object.create(BasePdfManager.prototype); |
||||
LocalPdfManager.prototype.constructor = LocalPdfManager; |
||||
|
||||
LocalPdfManager.prototype.ensure = |
||||
function LocalPdfManager_ensure(obj, prop, args) { |
||||
var promise = new Promise(); |
||||
try { |
||||
var value = obj[prop]; |
||||
var result; |
||||
if (typeof(value) === 'function') { |
||||
result = value.apply(obj, args); |
||||
} else { |
||||
result = value; |
||||
} |
||||
promise.resolve(result); |
||||
} catch (e) { |
||||
console.log(e.stack); |
||||
promise.reject(e); |
||||
} |
||||
return promise; |
||||
}; |
||||
|
||||
LocalPdfManager.prototype.requestRange = |
||||
function LocalPdfManager_requestRange(begin, end) { |
||||
var promise = new Promise(); |
||||
promise.resolve(); |
||||
return promise; |
||||
}; |
||||
|
||||
LocalPdfManager.prototype.requestLoadedStream = |
||||
function LocalPdfManager_requestLoadedStream() { |
||||
}; |
||||
|
||||
LocalPdfManager.prototype.onLoadedStream = |
||||
function LocalPdfManager_getLoadedStream() { |
||||
return this.loadedStream; |
||||
}; |
||||
|
||||
return LocalPdfManager; |
||||
})(); |
||||
|
||||
var NetworkPdfManager = (function NetworkPdfManagerClosure() { |
||||
|
||||
var CHUNK_SIZE = 65536; |
||||
|
||||
function NetworkPdfManager(args, msgHandler) { |
||||
|
||||
this.msgHandler = msgHandler; |
||||
|
||||
var params = { |
||||
msgHandler: msgHandler, |
||||
httpHeaders: args.httpHeaders, |
||||
chunkedViewerLoading: args.chunkedViewerLoading, |
||||
disableAutoFetch: args.disableAutoFetch |
||||
}; |
||||
this.streamManager = new ChunkedStreamManager(args.length, CHUNK_SIZE, |
||||
args.url, params); |
||||
|
||||
this.pdfModel = new PDFDocument(this, this.streamManager.getStream(), |
||||
args.password); |
||||
} |
||||
|
||||
NetworkPdfManager.prototype = Object.create(BasePdfManager.prototype); |
||||
NetworkPdfManager.prototype.constructor = NetworkPdfManager; |
||||
|
||||
NetworkPdfManager.prototype.ensure = |
||||
function NetworkPdfManager_ensure(obj, prop, args) { |
||||
var promise = new Promise(); |
||||
this.ensureHelper(promise, obj, prop, args); |
||||
return promise; |
||||
}; |
||||
|
||||
NetworkPdfManager.prototype.ensureHelper = |
||||
function NetworkPdfManager_ensureHelper(promise, obj, prop, args) { |
||||
try { |
||||
var result; |
||||
var value = obj[prop]; |
||||
if (typeof(value) === 'function') { |
||||
result = value.apply(obj, args); |
||||
} else { |
||||
result = value; |
||||
} |
||||
promise.resolve(result); |
||||
} catch(e) { |
||||
if (!(e instanceof MissingDataException)) { |
||||
console.log(e.stack); |
||||
promise.reject(e); |
||||
return; |
||||
} |
||||
|
||||
this.streamManager.requestRange(e.begin, e.end, function() { |
||||
this.ensureHelper(promise, obj, prop, args); |
||||
}.bind(this)); |
||||
} |
||||
}; |
||||
|
||||
NetworkPdfManager.prototype.requestRange = |
||||
function NetworkPdfManager_requestRange(begin, end) { |
||||
var promise = new Promise(); |
||||
this.streamManager.requestRange(begin, end, function() { |
||||
promise.resolve(); |
||||
}); |
||||
return promise; |
||||
}; |
||||
|
||||
NetworkPdfManager.prototype.requestLoadedStream = |
||||
function NetworkPdfManager_requestLoadedStream() { |
||||
this.streamManager.requestAllChunks(); |
||||
}; |
||||
|
||||
NetworkPdfManager.prototype.onLoadedStream = |
||||
function NetworkPdfManager_getLoadedStream() { |
||||
return this.streamManager.onLoadedStream(); |
||||
}; |
||||
|
||||
return NetworkPdfManager; |
||||
})(); |
||||
|
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue