Use foreground requests for manual sync and error reporting

Adds a 'foreground' flag to Zotero.HTTP.promise() options

Also, can now pass successCodes: false to always resolve the promise and never
throw UnexpectedStatusException
This commit is contained in:
Dan Stillman 2013-05-01 15:30:39 -04:00
parent e09295ee76
commit 2a7a604f28
4 changed files with 62 additions and 38 deletions

View File

@ -53,8 +53,13 @@
body += key + '=' + encodeURIComponent(parts[key]) + '&'; body += key + '=' + encodeURIComponent(parts[key]) + '&';
} }
body = body.substr(0, body.length - 1); body = body.substr(0, body.length - 1);
Zotero.HTTP.doPost("https://repo.zotero.org/repo/report", body, var url = 'https://repo.zotero.org/repo/report';
_sendErrorReportCallback); Zotero.HTTP.promise('POST', url,
{ body: body, successCodes: false, foreground: true })
.then(function (xmlhttp) {
_sendErrorReportCallback(xmlhttp);
})
.done();
}); });
} }

View File

@ -333,7 +333,11 @@ Zotero_Preferences.Sync = {
// TODO: better way of checking for an active session? // TODO: better way of checking for an active session?
if (Zotero.Sync.Server.sessionIDComponent == 'sessionid=') { if (Zotero.Sync.Server.sessionIDComponent == 'sessionid=') {
Zotero.Sync.Server.login(callback); Zotero.Sync.Server.login()
.then(function () {
callback();
})
.done();
} }
else { else {
callback(); callback();

View File

@ -53,11 +53,12 @@ Zotero.HTTP = new function() {
* <li>cookieSandbox - The sandbox from which cookies should be taken</li> * <li>cookieSandbox - The sandbox from which cookies should be taken</li>
* <li>debug - Log response text and status code</li> * <li>debug - Log response text and status code</li>
* <li>dontCache - If set, specifies that the request should not be fulfilled from the cache</li> * <li>dontCache - If set, specifies that the request should not be fulfilled from the cache</li>
* <li>foreground - Make a foreground request, showing certificate/authentication dialogs if necessary</li>
* <li>headers - HTTP headers to include in the request</li> * <li>headers - HTTP headers to include in the request</li>
* <li>requestObserver - Callback to receive XMLHttpRequest after open()</li> * <li>requestObserver - Callback to receive XMLHttpRequest after open()</li>
* <li>responseType - The type of the response. See XHR 2 documentation for legal values</li> * <li>responseType - The type of the response. See XHR 2 documentation for legal values</li>
* <li>responseCharset - The charset the response should be interpreted as</li> * <li>responseCharset - The charset the response should be interpreted as</li>
* <li>successCodes - HTTP status codes that are considered successful</li> * <li>successCodes - HTTP status codes that are considered successful, or FALSE to allow all</li>
* </ul> * </ul>
* @param {Zotero.CookieSandbox} [cookieSandbox] Cookie sandbox object * @param {Zotero.CookieSandbox} [cookieSandbox] Cookie sandbox object
* @return {Promise} A promise resolved with the XMLHttpRequest object if the request * @return {Promise} A promise resolved with the XMLHttpRequest object if the request
@ -105,7 +106,9 @@ Zotero.HTTP = new function() {
var xmlhttp = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"] var xmlhttp = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance(); .createInstance();
// Prevent certificate/authentication dialogs from popping up // Prevent certificate/authentication dialogs from popping up
if (!options || !options.foreground) {
xmlhttp.mozBackgroundRequest = true; xmlhttp.mozBackgroundRequest = true;
}
xmlhttp.open(method, url, true); xmlhttp.open(method, url, true);
// Pass the request to a callback // Pass the request to a callback
@ -157,6 +160,10 @@ Zotero.HTTP = new function() {
if (options && options.successCodes) { if (options && options.successCodes) {
var success = options.successCodes.indexOf(status) != -1; var success = options.successCodes.indexOf(status) != -1;
} }
// Explicit FALSE means allow any status code
else if (options && options.successCodes === false) {
var success = true;
}
else if(isFile) { else if(isFile) {
var success = status == 200 || status == 0; var success = status == 200 || status == 0;
} }

View File

@ -1345,7 +1345,7 @@ Zotero.Sync.Server = new function () {
} }
}; };
function login(callback) { function login() {
var url = _serverURL + "login"; var url = _serverURL + "login";
var username = Zotero.Sync.Server.username; var username = Zotero.Sync.Server.username;
@ -1368,7 +1368,9 @@ Zotero.Sync.Server = new function () {
Zotero.Sync.Runner.setSyncStatus(Zotero.getString('sync.status.loggingIn')); Zotero.Sync.Runner.setSyncStatus(Zotero.getString('sync.status.loggingIn'));
Zotero.HTTP.doPost(url, body, function (xmlhttp) { return Zotero.HTTP.promise("POST", url,
{ body: body, successCodes: false, foreground: !Zotero.Sync.Runner.background })
.then(function (xmlhttp) {
_checkResponse(xmlhttp, true); _checkResponse(xmlhttp, true);
var response = xmlhttp.responseXML.childNodes[0]; var response = xmlhttp.responseXML.childNodes[0];
@ -1396,10 +1398,6 @@ Zotero.Sync.Server = new function () {
//Zotero.debug('Got session ID ' + _sessionID + ' from server'); //Zotero.debug('Got session ID ' + _sessionID + ' from server');
if (callback) {
callback();
}
}); });
} }
@ -1416,9 +1414,11 @@ Zotero.Sync.Server = new function () {
if (!_sessionID) { if (!_sessionID) {
Zotero.debug("Session ID not available -- logging in"); Zotero.debug("Session ID not available -- logging in");
Zotero.Sync.Server.login(function () { Zotero.Sync.Server.login()
.then(function () {
Zotero.Sync.Server.sync(_callbacks); Zotero.Sync.Server.sync(_callbacks);
}); })
.done();
return; return;
} }
@ -1472,9 +1472,11 @@ Zotero.Sync.Server = new function () {
Zotero.debug("Invalid session ID -- logging in"); Zotero.debug("Invalid session ID -- logging in");
_sessionID = false; _sessionID = false;
_syncInProgress = false; _syncInProgress = false;
Zotero.Sync.Server.login(function () { Zotero.Sync.Server.login()
.then(function () {
Zotero.Sync.Server.sync(_callbacks); Zotero.Sync.Server.sync(_callbacks);
}); })
.done();
return; return;
} }
@ -1800,9 +1802,11 @@ Zotero.Sync.Server = new function () {
function clear(callback) { function clear(callback) {
if (!_sessionID) { if (!_sessionID) {
Zotero.debug("Session ID not available -- logging in"); Zotero.debug("Session ID not available -- logging in");
Zotero.Sync.Server.login(function () { Zotero.Sync.Server.login()
.then(function () {
Zotero.Sync.Server.clear(callback); Zotero.Sync.Server.clear(callback);
}); })
.done();
return; return;
} }
@ -1814,9 +1818,11 @@ Zotero.Sync.Server = new function () {
if (_invalidSession(xmlhttp)) { if (_invalidSession(xmlhttp)) {
Zotero.debug("Invalid session ID -- logging in"); Zotero.debug("Invalid session ID -- logging in");
_sessionID = false; _sessionID = false;
Zotero.Sync.Server.login(function () { Zotero.Sync.Server.login()
.then(function () {
Zotero.Sync.Server.clear(callback); Zotero.Sync.Server.clear(callback);
}); })
.done();
return; return;
} }
@ -1896,6 +1902,7 @@ Zotero.Sync.Server = new function () {
if (!xmlhttp.responseText) { if (!xmlhttp.responseText) {
var channel = xmlhttp.channel; var channel = xmlhttp.channel;
// Check SSL cert // Check SSL cert
if (channel) {
var secInfo = channel.securityInfo; var secInfo = channel.securityInfo;
if (secInfo instanceof Ci.nsITransportSecurityInfo) { if (secInfo instanceof Ci.nsITransportSecurityInfo) {
secInfo.QueryInterface(Ci.nsITransportSecurityInfo); secInfo.QueryInterface(Ci.nsITransportSecurityInfo);
@ -1919,6 +1926,7 @@ Zotero.Sync.Server = new function () {
_error(Zotero.getString('sync.error.sslConnectionError'), false, noReloadOnFailure); _error(Zotero.getString('sync.error.sslConnectionError'), false, noReloadOnFailure);
} }
} }
}
if (xmlhttp.status === 0) { if (xmlhttp.status === 0) {
_error(Zotero.getString('sync.error.checkConnection'), false, noReloadOnFailure); _error(Zotero.getString('sync.error.checkConnection'), false, noReloadOnFailure);
} }