Skip to content

Commit

Permalink
Cancel snapshot saves after 15 seconds
Browse files Browse the repository at this point in the history
Otherwise a save could go on forever and the connector will never show
an error, and if you quit Zotero the connector will show the
save-to-server dialog (though the connector should have its own
timeout).
  • Loading branch information
dstillman committed Jul 10, 2017
1 parent e551777 commit 32dedc6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
3 changes: 1 addition & 2 deletions chrome/content/zotero/xpcom/server_connector.js
Expand Up @@ -538,8 +538,7 @@ Zotero.Server.Connector.SaveSnapshot.prototype = {

deferred.resolve(201);
} catch(e) {
Zotero.debug("ERROR");
Zotero.debug(e);
Zotero.debug(e, 1);
deferred.resolve(500);
throw e;
}
Expand Down
18 changes: 17 additions & 1 deletion chrome/content/zotero/xpcom/utilities_internal.js
Expand Up @@ -30,6 +30,8 @@
* @class Utility functions not made available to translators
*/
Zotero.Utilities.Internal = {
SNAPSHOT_SAVE_TIMEOUT: 15000,

/**
* Run a function on chunks of a given size of an array's elements.
*
Expand Down Expand Up @@ -427,6 +429,7 @@ Zotero.Utilities.Internal = {
| nsIWBP.PERSIST_FLAGS_FORCE_ALLOW_COOKIES
| nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION
| nsIWBP.PERSIST_FLAGS_FROM_CACHE
| nsIWBP.PERSIST_FLAGS_CLEANUP_ON_FAILURE
// Mostly ads
| nsIWBP.PERSIST_FLAGS_IGNORE_IFRAMES
| nsIWBP.PERSIST_FLAGS_IGNORE_REDIRECTED_DATA;
Expand All @@ -447,9 +450,10 @@ Zotero.Utilities.Internal = {
const wrapColumn = 80;

var deferred = Zotero.Promise.defer();
wbp.progressListener = new Zotero.WebProgressFinishListener(function () {
var listener = new Zotero.WebProgressFinishListener(function () {
deferred.resolve();
});
wbp.progressListener = listener;

wbp.saveDocument(
document,
Expand All @@ -460,6 +464,18 @@ Zotero.Utilities.Internal = {
wrapColumn
);

// Cancel save after timeout has passed, so we return an error to the connector and don't stay
// saving forever
var timeoutID = setTimeout(function () {
if (deferred.promise.isPending()) {
Zotero.debug("Stopping save for " + document.location.href, 2);
//Zotero.debug(listener.getRequest());
deferred.reject("Snapshot save timeout");
wbp.cancelSave();
}
}, this.SNAPSHOT_SAVE_TIMEOUT);
deferred.promise.then(() => clearTimeout(timeoutID));

return deferred.promise;
},

Expand Down
10 changes: 10 additions & 0 deletions chrome/content/zotero/xpcom/zotero.js
Expand Up @@ -2488,12 +2488,22 @@ Zotero.Browser = new function() {
* Implements nsIWebProgressListener
*/
Zotero.WebProgressFinishListener = function(onFinish) {
var _request;

this.getRequest = function () {
return _request;
};

this.onStateChange = function(wp, req, stateFlags, status) {
//Zotero.debug('onStageChange: ' + stateFlags);
if (stateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP
&& stateFlags & Components.interfaces.nsIWebProgressListener.STATE_IS_NETWORK) {
_request = null;
onFinish();
}
else {
_request = req;
}
}

this.onProgressChange = function(wp, req, curSelfProgress, maxSelfProgress, curTotalProgress, maxTotalProgress) {
Expand Down

0 comments on commit 32dedc6

Please sign in to comment.