Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api-minor] Adds controlled destruction of the worker. #6546

Merged
merged 4 commits into from
Oct 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions extensions/firefox/content/PdfStreamConverter.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,8 @@ var RangedChromeActions = (function RangedChromeActionsClosure() {
// If we are in range request mode, this means we manually issued xhr
// requests, which we need to abort when we leave the page
domWindow.addEventListener('unload', function unload(e) {
self.networkManager.abortAllRequests();
domWindow.removeEventListener(e.type, unload);
self.abortLoading();
});
}

Expand Down Expand Up @@ -691,7 +691,7 @@ var RangedChromeActions = (function RangedChromeActionsClosure() {
}, '*');
};
this.dataListener.oncomplete = function () {
delete self.dataListener;
self.dataListener = null;
};
}

Expand Down Expand Up @@ -735,6 +735,15 @@ var RangedChromeActions = (function RangedChromeActionsClosure() {
});
};

proto.abortLoading = function RangedChromeActions_abortLoading() {
this.networkManager.abortAllRequests();
if (this.originalRequest) {
this.originalRequest.cancel(Cr.NS_BINDING_ABORTED);
this.originalRequest = null;
}
this.dataListener = null;
};

return RangedChromeActions;
})();

Expand All @@ -744,9 +753,10 @@ var StandardChromeActions = (function StandardChromeActionsClosure() {
* This is for a single network stream
*/
function StandardChromeActions(domWindow, contentDispositionFilename,
dataListener) {
originalRequest, dataListener) {

ChromeActions.call(this, domWindow, contentDispositionFilename);
this.originalRequest = originalRequest;
this.dataListener = dataListener;
}

Expand All @@ -772,20 +782,29 @@ var StandardChromeActions = (function StandardChromeActionsClosure() {
}, '*');
};

this.dataListener.oncomplete = function ChromeActions_dataListenerComplete(
data, errorCode) {
this.dataListener.oncomplete =
function StandardChromeActions_dataListenerComplete(data, errorCode) {
self.domWindow.postMessage({
pdfjsLoadAction: 'complete',
data: data,
errorCode: errorCode
}, '*');

delete self.dataListener;
self.dataListener = null;
self.originalRequest = null;
};

return true;
};

proto.abortLoading = function StandardChromeActions_abortLoading() {
if (this.originalRequest) {
this.originalRequest.cancel(Cr.NS_BINDING_ABORTED);
this.originalRequest = null;
}
this.dataListener = null;
};

return StandardChromeActions;
})();

Expand Down Expand Up @@ -1029,7 +1048,7 @@ PdfStreamConverter.prototype = {
rangeRequest, streamRequest, dataListener);
} else {
actions = new StandardChromeActions(
domWindow, contentDispositionFilename, dataListener);
domWindow, contentDispositionFilename, aRequest, dataListener);
}
var requestListener = new RequestListener(actions);
domWindow.addEventListener(PDFJS_EVENT_ID, function(event) {
Expand Down
17 changes: 10 additions & 7 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ var Annotation = (function AnnotationClosure() {
}.bind(this));
},

getOperatorList: function Annotation_getOperatorList(evaluator) {
getOperatorList: function Annotation_getOperatorList(evaluator, task) {

if (!this.appearance) {
return Promise.resolve(new OperatorList());
Expand All @@ -308,7 +308,8 @@ var Annotation = (function AnnotationClosure() {
return resourcesPromise.then(function(resources) {
var opList = new OperatorList();
opList.addOp(OPS.beginAnnotation, [data.rect, transform, matrix]);
return evaluator.getOperatorList(self.appearance, resources, opList).
return evaluator.getOperatorList(self.appearance, task,
resources, opList).
then(function () {
opList.addOp(OPS.endAnnotation, []);
self.appearance.reset();
Expand All @@ -319,7 +320,7 @@ var Annotation = (function AnnotationClosure() {
};

Annotation.appendToOperatorList = function Annotation_appendToOperatorList(
annotations, opList, pdfManager, partialEvaluator, intent) {
annotations, opList, pdfManager, partialEvaluator, task, intent) {

function reject(e) {
annotationsReadyCapability.reject(e);
Expand All @@ -332,7 +333,7 @@ var Annotation = (function AnnotationClosure() {
if (intent === 'display' && annotations[i].isViewable() ||
intent === 'print' && annotations[i].isPrintable()) {
annotationPromises.push(
annotations[i].getOperatorList(partialEvaluator));
annotations[i].getOperatorList(partialEvaluator, task));
}
}
Promise.all(annotationPromises).then(function(datas) {
Expand Down Expand Up @@ -564,9 +565,10 @@ var TextWidgetAnnotation = (function TextWidgetAnnotationClosure() {
}

Util.inherit(TextWidgetAnnotation, WidgetAnnotation, {
getOperatorList: function TextWidgetAnnotation_getOperatorList(evaluator) {
getOperatorList: function TextWidgetAnnotation_getOperatorList(evaluator,
task) {
if (this.appearance) {
return Annotation.prototype.getOperatorList.call(this, evaluator);
return Annotation.prototype.getOperatorList.call(this, evaluator, task);
}

var opList = new OperatorList();
Expand All @@ -579,7 +581,8 @@ var TextWidgetAnnotation = (function TextWidgetAnnotationClosure() {
}

var stream = new Stream(stringToBytes(data.defaultAppearance));
return evaluator.getOperatorList(stream, this.fieldResources, opList).
return evaluator.getOperatorList(stream, task,
this.fieldResources, opList).
then(function () {
return opList;
});
Expand Down
49 changes: 27 additions & 22 deletions src/core/chunked_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ var ChunkedStreamManager = (function ChunkedStreamManagerClosure() {

this.chunksNeededByRequest = {};
this.requestsByChunk = {};
this.callbacksByRequest = {};
this.promisesByRequest = {};
this.progressiveDataLength = 0;

this._loadedStreamCapability = createPromiseCapability();
Expand All @@ -320,12 +320,11 @@ var ChunkedStreamManager = (function ChunkedStreamManagerClosure() {
// contiguous ranges to load in as few requests as possible
requestAllChunks: function ChunkedStreamManager_requestAllChunks() {
var missingChunks = this.stream.getMissingChunks();
this.requestChunks(missingChunks);
this._requestChunks(missingChunks);
return this._loadedStreamCapability.promise;
},

requestChunks: function ChunkedStreamManager_requestChunks(chunks,
callback) {
_requestChunks: function ChunkedStreamManager_requestChunks(chunks) {
var requestId = this.currRequestId++;

var chunksNeeded;
Expand All @@ -338,13 +337,11 @@ var ChunkedStreamManager = (function ChunkedStreamManagerClosure() {
}

if (isEmptyObj(chunksNeeded)) {
if (callback) {
callback();
}
return;
return Promise.resolve();
}

this.callbacksByRequest[requestId] = callback;
var capability = createPromiseCapability();
this.promisesByRequest[requestId] = capability;

var chunksToRequest = [];
for (var chunk in chunksNeeded) {
Expand All @@ -357,7 +354,7 @@ var ChunkedStreamManager = (function ChunkedStreamManagerClosure() {
}

if (!chunksToRequest.length) {
return;
return capability.promise;
}

var groupedChunksToRequest = this.groupChunks(chunksToRequest);
Expand All @@ -368,15 +365,16 @@ var ChunkedStreamManager = (function ChunkedStreamManagerClosure() {
var end = Math.min(groupedChunk.endChunk * this.chunkSize, this.length);
this.sendRequest(begin, end);
}

return capability.promise;
},

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) {
requestRange: function ChunkedStreamManager_requestRange(begin, end) {

end = Math.min(end, this.length);

Expand All @@ -388,11 +386,10 @@ var ChunkedStreamManager = (function ChunkedStreamManagerClosure() {
chunks.push(chunk);
}

this.requestChunks(chunks, callback);
return this._requestChunks(chunks);
},

requestRanges: function ChunkedStreamManager_requestRanges(ranges,
callback) {
requestRanges: function ChunkedStreamManager_requestRanges(ranges) {
ranges = ranges || [];
var chunksToRequest = [];

Expand All @@ -407,7 +404,7 @@ var ChunkedStreamManager = (function ChunkedStreamManagerClosure() {
}

chunksToRequest.sort(function(a, b) { return a - b; });
this.requestChunks(chunksToRequest, callback);
return this._requestChunks(chunksToRequest);
},

// Groups a sorted array of chunks into as few contiguous larger
Expand Down Expand Up @@ -506,17 +503,15 @@ var ChunkedStreamManager = (function ChunkedStreamManagerClosure() {
nextEmptyChunk = this.stream.nextEmptyChunk(endChunk);
}
if (isInt(nextEmptyChunk)) {
this.requestChunks([nextEmptyChunk]);
this._requestChunks([nextEmptyChunk]);
}
}

for (i = 0; i < loadedRequests.length; ++i) {
requestId = loadedRequests[i];
var callback = this.callbacksByRequest[requestId];
delete this.callbacksByRequest[requestId];
if (callback) {
callback();
}
var capability = this.promisesByRequest[requestId];
delete this.promisesByRequest[requestId];
capability.resolve();
}

this.msgHandler.send('DocProgress', {
Expand All @@ -537,6 +532,16 @@ var ChunkedStreamManager = (function ChunkedStreamManagerClosure() {
getEndChunk: function ChunkedStreamManager_getEndChunk(end) {
var chunk = Math.floor((end - 1) / this.chunkSize) + 1;
return chunk;
},

abort: function ChunkedStreamManager_abort() {
if (this.networkManager) {
this.networkManager.abortAllRequests();
}
for(var requestId in this.promisesByRequest) {
var capability = this.promisesByRequest[requestId];
capability.reject(new Error('Request was aborted'));
}
}
};

Expand Down
11 changes: 6 additions & 5 deletions src/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ var Page = (function PageClosure() {
}.bind(this));
},

getOperatorList: function Page_getOperatorList(handler, intent) {
getOperatorList: function Page_getOperatorList(handler, task, intent) {
var self = this;

var pdfManager = this.pdfManager;
Expand Down Expand Up @@ -194,8 +194,8 @@ var Page = (function PageClosure() {
pageIndex: self.pageIndex,
intent: intent
});
return partialEvaluator.getOperatorList(contentStream, self.resources,
opList).then(function () {
return partialEvaluator.getOperatorList(contentStream, task,
self.resources, opList).then(function () {
return opList;
});
});
Expand All @@ -212,15 +212,15 @@ var Page = (function PageClosure() {
}

var annotationsReadyPromise = Annotation.appendToOperatorList(
annotations, pageOpList, pdfManager, partialEvaluator, intent);
annotations, pageOpList, pdfManager, partialEvaluator, task, intent);
return annotationsReadyPromise.then(function () {
pageOpList.flush(true);
return pageOpList;
});
});
},

extractTextContent: function Page_extractTextContent() {
extractTextContent: function Page_extractTextContent(task) {
var handler = {
on: function nullHandlerOn() {},
send: function nullHandlerSend() {}
Expand Down Expand Up @@ -249,6 +249,7 @@ var Page = (function PageClosure() {
self.fontCache);

return partialEvaluator.getTextContent(contentStream,
task,
self.resources);
});
},
Expand Down
Loading