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 NotImplementedException, MissingDataException, Promise, Stream,
|
2014-05-02 06:36:06 +09:00
|
|
|
PDFDocument, ChunkedStreamManager, createPromiseCapability */
|
2013-02-07 08:19:29 +09:00
|
|
|
|
|
|
|
'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 = {
|
2015-10-28 07:48:10 +09:00
|
|
|
get docId() {
|
|
|
|
return this._docId;
|
|
|
|
},
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
onLoadedStream: function BasePdfManager_onLoadedStream() {
|
|
|
|
throw new NotImplementedException();
|
|
|
|
},
|
|
|
|
|
2014-03-12 17:51:40 +09:00
|
|
|
ensureDoc: function BasePdfManager_ensureDoc(prop, args) {
|
|
|
|
return this.ensure(this.pdfDocument, prop, args);
|
2013-02-07 08:19:29 +09:00
|
|
|
},
|
|
|
|
|
2013-04-18 16:12:40 +09:00
|
|
|
ensureXRef: function BasePdfManager_ensureXRef(prop, args) {
|
2014-03-12 17:51:40 +09:00
|
|
|
return this.ensure(this.pdfDocument.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) {
|
2014-03-12 17:51:40 +09:00
|
|
|
return this.ensure(this.pdfDocument.catalog, prop, args);
|
2013-02-07 08:19:29 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
getPage: function BasePdfManager_pagePage(pageIndex) {
|
2014-03-12 17:51:40 +09:00
|
|
|
return this.pdfDocument.getPage(pageIndex);
|
2013-02-07 08:19:29 +09:00
|
|
|
},
|
|
|
|
|
2013-11-15 06:43:38 +09:00
|
|
|
cleanup: function BasePdfManager_cleanup() {
|
2014-03-12 17:51:40 +09:00
|
|
|
return this.pdfDocument.cleanup();
|
2013-11-15 06:43:38 +09:00
|
|
|
},
|
|
|
|
|
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();
|
2013-05-10 07:35:23 +09:00
|
|
|
},
|
|
|
|
|
2014-09-06 10:02:54 +09:00
|
|
|
sendProgressiveData: function BasePdfManager_sendProgressiveData(chunk) {
|
|
|
|
return new NotImplementedException();
|
|
|
|
},
|
|
|
|
|
2013-05-10 07:35:23 +09:00
|
|
|
updatePassword: function BasePdfManager_updatePassword(password) {
|
2014-03-12 17:51:40 +09:00
|
|
|
this.pdfDocument.xref.password = this.password = password;
|
2014-05-02 08:38:49 +09:00
|
|
|
if (this._passwordChangedCapability) {
|
|
|
|
this._passwordChangedCapability.resolve();
|
2013-05-10 07:35:23 +09:00
|
|
|
}
|
2013-09-26 06:25:41 +09:00
|
|
|
},
|
|
|
|
|
2014-05-02 08:38:49 +09:00
|
|
|
passwordChanged: function BasePdfManager_passwordChanged() {
|
|
|
|
this._passwordChangedCapability = createPromiseCapability();
|
|
|
|
return this._passwordChangedCapability.promise;
|
|
|
|
},
|
|
|
|
|
2013-09-26 06:25:41 +09:00
|
|
|
terminate: function BasePdfManager_terminate() {
|
|
|
|
return new NotImplementedException();
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return BasePdfManager;
|
|
|
|
})();
|
|
|
|
|
|
|
|
var LocalPdfManager = (function LocalPdfManagerClosure() {
|
2015-10-28 07:48:10 +09:00
|
|
|
function LocalPdfManager(docId, data, password) {
|
|
|
|
this._docId = docId;
|
2013-02-07 08:19:29 +09:00
|
|
|
var stream = new Stream(data);
|
2014-03-12 17:51:40 +09:00
|
|
|
this.pdfDocument = new PDFDocument(this, stream, password);
|
2014-05-02 06:36:06 +09:00
|
|
|
this._loadedStreamCapability = createPromiseCapability();
|
|
|
|
this._loadedStreamCapability.resolve(stream);
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2014-05-02 06:36:06 +09:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
try {
|
|
|
|
var value = obj[prop];
|
|
|
|
var result;
|
|
|
|
if (typeof value === 'function') {
|
|
|
|
result = value.apply(obj, args);
|
|
|
|
} else {
|
|
|
|
result = value;
|
|
|
|
}
|
|
|
|
resolve(result);
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
2014-05-02 06:36:06 +09:00
|
|
|
});
|
2013-02-07 08:19:29 +09:00
|
|
|
};
|
|
|
|
|
2013-04-19 02:41:33 +09:00
|
|
|
LocalPdfManager.prototype.requestRange =
|
|
|
|
function LocalPdfManager_requestRange(begin, end) {
|
2014-05-02 06:36:06 +09:00
|
|
|
return Promise.resolve();
|
2013-04-19 02:41:33 +09:00
|
|
|
};
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
LocalPdfManager.prototype.requestLoadedStream =
|
|
|
|
function LocalPdfManager_requestLoadedStream() {
|
|
|
|
};
|
|
|
|
|
|
|
|
LocalPdfManager.prototype.onLoadedStream =
|
|
|
|
function LocalPdfManager_getLoadedStream() {
|
2014-05-02 06:36:06 +09:00
|
|
|
return this._loadedStreamCapability.promise;
|
2013-02-07 08:19:29 +09:00
|
|
|
};
|
|
|
|
|
2013-09-26 06:25:41 +09:00
|
|
|
LocalPdfManager.prototype.terminate =
|
|
|
|
function LocalPdfManager_terminate() {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
return LocalPdfManager;
|
|
|
|
})();
|
|
|
|
|
|
|
|
var NetworkPdfManager = (function NetworkPdfManagerClosure() {
|
2015-10-28 07:48:10 +09:00
|
|
|
function NetworkPdfManager(docId, args, msgHandler) {
|
|
|
|
this._docId = docId;
|
2013-02-07 08:19:29 +09:00
|
|
|
this.msgHandler = msgHandler;
|
|
|
|
|
|
|
|
var params = {
|
|
|
|
msgHandler: msgHandler,
|
|
|
|
httpHeaders: args.httpHeaders,
|
2014-01-15 17:57:17 +09:00
|
|
|
withCredentials: args.withCredentials,
|
2013-04-13 03:37:49 +09:00
|
|
|
chunkedViewerLoading: args.chunkedViewerLoading,
|
2013-11-19 04:17:26 +09:00
|
|
|
disableAutoFetch: args.disableAutoFetch,
|
|
|
|
initialData: args.initialData
|
2013-02-07 08:19:29 +09:00
|
|
|
};
|
2015-10-22 08:56:27 +09:00
|
|
|
this.streamManager = new ChunkedStreamManager(args.length,
|
|
|
|
args.rangeChunkSize,
|
2013-02-07 08:19:29 +09:00
|
|
|
args.url, params);
|
|
|
|
|
2014-03-12 17:51:40 +09:00
|
|
|
this.pdfDocument = 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) {
|
2014-05-02 06:36:06 +09:00
|
|
|
var pdfManager = this;
|
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
function ensureHelper() {
|
|
|
|
try {
|
|
|
|
var result;
|
|
|
|
var value = obj[prop];
|
|
|
|
if (typeof value === 'function') {
|
|
|
|
result = value.apply(obj, args);
|
|
|
|
} else {
|
|
|
|
result = value;
|
|
|
|
}
|
|
|
|
resolve(result);
|
|
|
|
} catch(e) {
|
|
|
|
if (!(e instanceof MissingDataException)) {
|
|
|
|
reject(e);
|
|
|
|
return;
|
|
|
|
}
|
2015-10-21 07:45:55 +09:00
|
|
|
pdfManager.streamManager.requestRange(e.begin, e.end).
|
|
|
|
then(ensureHelper, reject);
|
2014-05-02 06:36:06 +09:00
|
|
|
}
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
|
|
|
|
2014-05-02 06:36:06 +09:00
|
|
|
ensureHelper();
|
|
|
|
});
|
2013-02-07 08:19:29 +09:00
|
|
|
};
|
|
|
|
|
2013-04-19 02:41:33 +09:00
|
|
|
NetworkPdfManager.prototype.requestRange =
|
|
|
|
function NetworkPdfManager_requestRange(begin, end) {
|
2015-10-21 07:45:55 +09:00
|
|
|
return this.streamManager.requestRange(begin, end);
|
2013-04-19 02:41:33 +09:00
|
|
|
};
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
NetworkPdfManager.prototype.requestLoadedStream =
|
|
|
|
function NetworkPdfManager_requestLoadedStream() {
|
|
|
|
this.streamManager.requestAllChunks();
|
|
|
|
};
|
|
|
|
|
2014-09-06 10:02:54 +09:00
|
|
|
NetworkPdfManager.prototype.sendProgressiveData =
|
|
|
|
function NetworkPdfManager_sendProgressiveData(chunk) {
|
|
|
|
this.streamManager.onReceiveData({ chunk: chunk });
|
|
|
|
};
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
NetworkPdfManager.prototype.onLoadedStream =
|
|
|
|
function NetworkPdfManager_getLoadedStream() {
|
|
|
|
return this.streamManager.onLoadedStream();
|
|
|
|
};
|
|
|
|
|
2013-09-26 06:25:41 +09:00
|
|
|
NetworkPdfManager.prototype.terminate =
|
|
|
|
function NetworkPdfManager_terminate() {
|
2015-10-21 07:45:55 +09:00
|
|
|
this.streamManager.abort();
|
2013-09-26 06:25:41 +09:00
|
|
|
};
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
return NetworkPdfManager;
|
|
|
|
})();
|