Skip to content

Commit 665ff0d

Browse files
committed
Merge pull request mozilla#1849 from kingsquare/master
Fix for web worker test in Safari
2 parents 54db748 + 8292951 commit 665ff0d

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

src/api.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
* @return {Promise} A promise that is resolved with {PDFDocumentProxy} object.
1919
*/
2020
PDFJS.getDocument = function getDocument(source) {
21-
var url, data, headers, password, parameters = {};
21+
var url, data, headers, password, parameters = {}, workerInitializedPromise,
22+
workerReadyPromise, transport;
23+
2224
if (typeof source === 'string') {
2325
url = source;
2426
} else if (isArrayBuffer(source)) {
@@ -37,8 +39,9 @@ PDFJS.getDocument = function getDocument(source) {
3739
'string or a parameter object');
3840
}
3941

40-
var promise = new PDFJS.Promise();
41-
var transport = new WorkerTransport(promise);
42+
workerInitializedPromise = new PDFJS.Promise();
43+
workerReadyPromise = new PDFJS.Promise();
44+
transport = new WorkerTransport(workerInitializedPromise, workerReadyPromise);
4245
if (data) {
4346
// assuming the data is array, instantiating directly from it
4447
transport.sendData(data, parameters);
@@ -48,24 +51,31 @@ PDFJS.getDocument = function getDocument(source) {
4851
{
4952
url: url,
5053
progress: function getPDFProgress(evt) {
51-
if (evt.lengthComputable)
52-
promise.progress({
54+
if (evt.lengthComputable) {
55+
workerReadyPromise.progress({
5356
loaded: evt.loaded,
5457
total: evt.total
5558
});
59+
}
5660
},
5761
error: function getPDFError(e) {
58-
promise.reject('Unexpected server response of ' +
62+
workerReadyPromise.reject('Unexpected server response of ' +
5963
e.target.status + '.');
6064
},
6165
headers: headers
6266
},
6367
function getPDFLoad(data) {
64-
transport.sendData(data, parameters);
68+
// sometimes the pdf has finished downloading before the web worker-test
69+
// has finished. In that case the rendering of the final pdf would cause
70+
// errors. We have to wait for the WorkerTransport to finalize worker-
71+
// support detection
72+
workerInitializedPromise.then(function workerInitialized() {
73+
transport.sendData(data, parameters);
74+
});
6575
});
6676
}
6777

68-
return promise;
78+
return workerReadyPromise;
6979
};
7080

7181
/**
@@ -426,8 +436,8 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
426436
* For internal use only.
427437
*/
428438
var WorkerTransport = (function WorkerTransportClosure() {
429-
function WorkerTransport(promise) {
430-
this.workerReadyPromise = promise;
439+
function WorkerTransport(workerInitializedPromise, workerReadyPromise) {
440+
this.workerReadyPromise = workerReadyPromise;
431441
this.objs = new PDFObjects();
432442

433443
this.pageCache = [];
@@ -471,6 +481,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
471481
globalScope.PDFJS.disableWorker = true;
472482
this.setupFakeWorker();
473483
}
484+
workerInitializedPromise.resolve();
474485
}.bind(this));
475486

476487
var testObj = new Uint8Array(1);
@@ -486,6 +497,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
486497
// Thus, we fallback to a faked worker.
487498
globalScope.PDFJS.disableWorker = true;
488499
this.setupFakeWorker();
500+
workerInitializedPromise.resolve();
489501
}
490502
WorkerTransport.prototype = {
491503
destroy: function WorkerTransport_destroy() {

web/compatibility.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
// Checking if the typed arrays are supported
77
(function checkTypedArrayCompatibility() {
88
if (typeof Uint8Array !== 'undefined') {
9+
// some mobile versions do not support subarray (e.g. safari 5 / iOS)
10+
if (typeof Uint8Array.prototype.subarray === 'undefined') {
11+
Uint8Array.prototype.subarray = function subarray(start, end) {
12+
return new Uint8Array(this.slice(start, end));
13+
};
14+
Float32Array.prototype.subarray = function subarray(start, end) {
15+
return new Float32Array(this.slice(start, end));
16+
};
17+
}
18+
919
// some mobile version might not support Float64Array
1020
if (typeof Float64Array === 'undefined')
1121
window.Float64Array = Float32Array;
@@ -69,8 +79,17 @@
6979

7080
// Object.defineProperty() ?
7181
(function checkObjectDefinePropertyCompatibility() {
72-
if (typeof Object.defineProperty !== 'undefined')
73-
return;
82+
if (typeof Object.defineProperty !== 'undefined') {
83+
// some browsers (e.g. safari) cannot use defineProperty() on DOM objects
84+
// and thus the native version is not sufficient
85+
var definePropertyPossible = true;
86+
try {
87+
Object.defineProperty(new Image(), 'id', { value: 'test' });
88+
} catch (e) {
89+
definePropertyPossible = false;
90+
}
91+
if (definePropertyPossible) return;
92+
}
7493

7594
Object.defineProperty = function objectDefineProperty(obj, name, def) {
7695
delete obj[name];

0 commit comments

Comments
 (0)