Merge pull request #1849 from kingsquare/master

Fix for web worker test in Safari
This commit is contained in:
Yury Delendik 2012-06-28 03:05:04 -07:00
commit 665ff0d36f
2 changed files with 43 additions and 12 deletions

View File

@ -18,7 +18,9 @@
* @return {Promise} A promise that is resolved with {PDFDocumentProxy} object. * @return {Promise} A promise that is resolved with {PDFDocumentProxy} object.
*/ */
PDFJS.getDocument = function getDocument(source) { PDFJS.getDocument = function getDocument(source) {
var url, data, headers, password, parameters = {}; var url, data, headers, password, parameters = {}, workerInitializedPromise,
workerReadyPromise, transport;
if (typeof source === 'string') { if (typeof source === 'string') {
url = source; url = source;
} else if (isArrayBuffer(source)) { } else if (isArrayBuffer(source)) {
@ -37,8 +39,9 @@ PDFJS.getDocument = function getDocument(source) {
'string or a parameter object'); 'string or a parameter object');
} }
var promise = new PDFJS.Promise(); workerInitializedPromise = new PDFJS.Promise();
var transport = new WorkerTransport(promise); workerReadyPromise = new PDFJS.Promise();
transport = new WorkerTransport(workerInitializedPromise, workerReadyPromise);
if (data) { if (data) {
// assuming the data is array, instantiating directly from it // assuming the data is array, instantiating directly from it
transport.sendData(data, parameters); transport.sendData(data, parameters);
@ -48,24 +51,31 @@ PDFJS.getDocument = function getDocument(source) {
{ {
url: url, url: url,
progress: function getPDFProgress(evt) { progress: function getPDFProgress(evt) {
if (evt.lengthComputable) if (evt.lengthComputable) {
promise.progress({ workerReadyPromise.progress({
loaded: evt.loaded, loaded: evt.loaded,
total: evt.total total: evt.total
}); });
}
}, },
error: function getPDFError(e) { error: function getPDFError(e) {
promise.reject('Unexpected server response of ' + workerReadyPromise.reject('Unexpected server response of ' +
e.target.status + '.'); e.target.status + '.');
}, },
headers: headers headers: headers
}, },
function getPDFLoad(data) { function getPDFLoad(data) {
transport.sendData(data, parameters); // sometimes the pdf has finished downloading before the web worker-test
// has finished. In that case the rendering of the final pdf would cause
// errors. We have to wait for the WorkerTransport to finalize worker-
// support detection
workerInitializedPromise.then(function workerInitialized() {
transport.sendData(data, parameters);
});
}); });
} }
return promise; return workerReadyPromise;
}; };
/** /**
@ -426,8 +436,8 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
* For internal use only. * For internal use only.
*/ */
var WorkerTransport = (function WorkerTransportClosure() { var WorkerTransport = (function WorkerTransportClosure() {
function WorkerTransport(promise) { function WorkerTransport(workerInitializedPromise, workerReadyPromise) {
this.workerReadyPromise = promise; this.workerReadyPromise = workerReadyPromise;
this.objs = new PDFObjects(); this.objs = new PDFObjects();
this.pageCache = []; this.pageCache = [];
@ -471,6 +481,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
globalScope.PDFJS.disableWorker = true; globalScope.PDFJS.disableWorker = true;
this.setupFakeWorker(); this.setupFakeWorker();
} }
workerInitializedPromise.resolve();
}.bind(this)); }.bind(this));
var testObj = new Uint8Array(1); var testObj = new Uint8Array(1);
@ -486,6 +497,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
// Thus, we fallback to a faked worker. // Thus, we fallback to a faked worker.
globalScope.PDFJS.disableWorker = true; globalScope.PDFJS.disableWorker = true;
this.setupFakeWorker(); this.setupFakeWorker();
workerInitializedPromise.resolve();
} }
WorkerTransport.prototype = { WorkerTransport.prototype = {
destroy: function WorkerTransport_destroy() { destroy: function WorkerTransport_destroy() {

View File

@ -6,6 +6,16 @@
// Checking if the typed arrays are supported // Checking if the typed arrays are supported
(function checkTypedArrayCompatibility() { (function checkTypedArrayCompatibility() {
if (typeof Uint8Array !== 'undefined') { if (typeof Uint8Array !== 'undefined') {
// some mobile versions do not support subarray (e.g. safari 5 / iOS)
if (typeof Uint8Array.prototype.subarray === 'undefined') {
Uint8Array.prototype.subarray = function subarray(start, end) {
return new Uint8Array(this.slice(start, end));
};
Float32Array.prototype.subarray = function subarray(start, end) {
return new Float32Array(this.slice(start, end));
};
}
// some mobile version might not support Float64Array // some mobile version might not support Float64Array
if (typeof Float64Array === 'undefined') if (typeof Float64Array === 'undefined')
window.Float64Array = Float32Array; window.Float64Array = Float32Array;
@ -69,8 +79,17 @@
// Object.defineProperty() ? // Object.defineProperty() ?
(function checkObjectDefinePropertyCompatibility() { (function checkObjectDefinePropertyCompatibility() {
if (typeof Object.defineProperty !== 'undefined') if (typeof Object.defineProperty !== 'undefined') {
return; // some browsers (e.g. safari) cannot use defineProperty() on DOM objects
// and thus the native version is not sufficient
var definePropertyPossible = true;
try {
Object.defineProperty(new Image(), 'id', { value: 'test' });
} catch (e) {
definePropertyPossible = false;
}
if (definePropertyPossible) return;
}
Object.defineProperty = function objectDefineProperty(obj, name, def) { Object.defineProperty = function objectDefineProperty(obj, name, def) {
delete obj[name]; delete obj[name];