pdf.js/src/pdf_manager.js

198 lines
5.8 KiB
JavaScript
Raw Normal View History

2013-02-07 08:19:29 +09:00
/* -*- 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.
*/
2013-04-19 02:41:33 +09:00
/* globals NotImplementedException, MissingDataException, Promise, Stream,
2013-02-07 08:19:29 +09:00
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();
},
2013-04-18 16:12:40 +09:00
ensureModel: function BasePdfManager_ensureModel(prop, args) {
return this.ensure(this.pdfModel, prop, args);
2013-02-07 08:19:29 +09:00
},
2013-04-18 16:12:40 +09:00
ensureXRef: function BasePdfManager_ensureXRef(prop, args) {
return this.ensure(this.pdfModel.xref, prop, args);
2013-02-07 08:19:29 +09:00
},
2013-04-18 16:12:40 +09:00
ensureCatalog: function BasePdfManager_ensureCatalog(prop, args) {
return this.ensure(this.pdfModel.catalog, prop, args);
2013-02-07 08:19:29 +09:00
},
getPage: function BasePdfManager_pagePage(pageIndex) {
return this.pdfModel.getPage(pageIndex);
},
2013-04-18 16:12:40 +09:00
ensure: function BasePdfManager_ensure(obj, prop, args) {
2013-02-07 08:19:29 +09:00
return new NotImplementedException();
},
2013-04-19 02:41:33 +09:00
requestRange: function BasePdfManager_ensure(begin, end) {
return new NotImplementedException();
},
2013-02-07 08:19:29 +09:00
requestLoadedStream: function BasePdfManager_requestLoadedStream() {
return new NotImplementedException();
},
updatePassword: function BasePdfManager_updatePassword(password) {
this.pdfModel.xref.password = this.password = password;
if (this.passwordChangedPromise) {
this.passwordChangedPromise.resolve();
}
2013-02-07 08:19:29 +09:00
}
};
return BasePdfManager;
})();
var LocalPdfManager = (function LocalPdfManagerClosure() {
function LocalPdfManager(data, password) {
var stream = new Stream(data);
this.pdfModel = new PDFDocument(this, stream, password);
2013-04-19 02:41:33 +09:00
this.loadedStream = new Promise();
2013-02-07 08:19:29 +09:00
this.loadedStream.resolve(stream);
}
LocalPdfManager.prototype = Object.create(BasePdfManager.prototype);
LocalPdfManager.prototype.constructor = LocalPdfManager;
LocalPdfManager.prototype.ensure =
2013-04-18 16:12:40 +09:00
function LocalPdfManager_ensure(obj, prop, args) {
2013-04-19 02:41:33 +09:00
var promise = new Promise();
2013-02-07 08:19:29 +09:00
try {
2013-04-18 16:12:40 +09:00
var value = obj[prop];
var result;
2013-02-07 08:19:29 +09:00
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;
};
2013-04-19 02:41:33 +09:00
LocalPdfManager.prototype.requestRange =
function LocalPdfManager_requestRange(begin, end) {
var promise = new Promise();
promise.resolve();
return promise;
};
2013-02-07 08:19:29 +09:00
LocalPdfManager.prototype.requestLoadedStream =
function LocalPdfManager_requestLoadedStream() {
};
LocalPdfManager.prototype.onLoadedStream =
function LocalPdfManager_getLoadedStream() {
return this.loadedStream;
};
return LocalPdfManager;
})();
var NetworkPdfManager = (function NetworkPdfManagerClosure() {
2013-04-18 16:12:40 +09:00
var CHUNK_SIZE = 65536;
2013-02-07 08:19:29 +09:00
function NetworkPdfManager(args, msgHandler) {
this.msgHandler = msgHandler;
var params = {
msgHandler: msgHandler,
httpHeaders: args.httpHeaders,
chunkedViewerLoading: args.chunkedViewerLoading,
disableAutoFetch: args.disableAutoFetch
2013-02-07 08:19:29 +09:00
};
this.streamManager = new ChunkedStreamManager(args.length, CHUNK_SIZE,
args.url, params);
this.pdfModel = new PDFDocument(this, this.streamManager.getStream(),
2013-02-07 08:19:29 +09:00
args.password);
}
NetworkPdfManager.prototype = Object.create(BasePdfManager.prototype);
NetworkPdfManager.prototype.constructor = NetworkPdfManager;
NetworkPdfManager.prototype.ensure =
2013-04-18 16:12:40 +09:00
function NetworkPdfManager_ensure(obj, prop, args) {
2013-04-19 02:41:33 +09:00
var promise = new Promise();
2013-04-18 16:12:40 +09:00
this.ensureHelper(promise, obj, prop, args);
2013-02-07 08:19:29 +09:00
return promise;
};
NetworkPdfManager.prototype.ensureHelper =
2013-04-18 16:12:40 +09:00
function NetworkPdfManager_ensureHelper(promise, obj, prop, args) {
2013-02-07 08:19:29 +09:00
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() {
2013-04-18 16:12:40 +09:00
this.ensureHelper(promise, obj, prop, args);
2013-02-07 08:19:29 +09:00
}.bind(this));
}
};
2013-04-19 02:41:33 +09:00
NetworkPdfManager.prototype.requestRange =
function NetworkPdfManager_requestRange(begin, end) {
var promise = new Promise();
this.streamManager.requestRange(begin, end, function() {
promise.resolve();
});
return promise;
};
2013-02-07 08:19:29 +09:00
NetworkPdfManager.prototype.requestLoadedStream =
function NetworkPdfManager_requestLoadedStream() {
this.streamManager.requestAllChunks();
};
NetworkPdfManager.prototype.onLoadedStream =
function NetworkPdfManager_getLoadedStream() {
return this.streamManager.onLoadedStream();
};
return NetworkPdfManager;
})();