Protection against login loop while sending. Simplified file sending for Firefox 8.

This commit is contained in:
amla70
2011-07-23 14:22:56 +00:00
parent e0a66f24a1
commit 11ae8e6ba9

View File

@@ -26,7 +26,6 @@ var sendtophoneCore = {
apiVersion : 6,
apkUrl : "http://code.google.com/p/chrometophone/wiki/AndroidApp",
returnOAuthUrl : "https://code.google.com/p/chrometophone/logo",
retryCount : 0,
init: function()
{
@@ -231,7 +230,18 @@ var sendtophoneCore = {
var data = 'title=' + encodeURIComponent(title) +
'&url=' + encodeURIComponent(url) + '&sel=' + encodeURIComponent(selection);
this.pendingMessage = {title:title, url: url, selection: selection};
if (!this.pendingMessage)
this.pendingMessage = {title:title, url: url, selection: selection, retries: 3};
else
{
this.pendingMessage.retries -= 1;
if (this.pendingMessage.retries == 0)
{
delete this.pendingMessage;
this.alert("Too many retries");
return;
}
}
if (!this.oauth.hasToken())
{
@@ -287,7 +297,6 @@ var sendtophoneCore = {
if (body.substring(0, 2) == 'OK')
{
delete self.pendingMessage;
self.retryCount=0;
self.popupNotification(self.getString("InfoSent"));
return;
}
@@ -440,6 +449,68 @@ var sendtophoneCore = {
}
let uploadName = nsFile.leafName;
var req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance(Ci.nsIXMLHttpRequest);
// Show the progress of uploads
sendtophoneUploadsManager.addUpload(nsFile, req);
req.open('POST', uri, true);
req.addEventListener("load", function(event)
{
// If there's a callback (to delete temporary files) we call it now
if (callback)
callback();
onSuccess(event.target, uploadName);
}, false);
// Handle errors or aborted uploads
req.addEventListener("error", function(evt)
{
sendtophoneCore.alert("Error while sending the file to the server:\r\n" + uri);
// If there's a callback (to delete temporary files) we call it now
if (callback)
callback();
}, false);
req.addEventListener("abort", function(evt)
{
// Silent.
// If there's a callback (to delete temporary files) we call it now
if (callback)
callback();
}, false);
// Enable cookies
try {
req.channel.QueryInterface(Ci.nsIHttpChannelInternal).
forceAllowThirdPartyCookie = true;
}
catch(ex) {}
var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
.getService(Components.interfaces.nsIXULAppInfo);
var majorVersion = /(\d+)\./.exec( appInfo.platformVersion )[1];
if (majorVersion>=8)
{
this.toConsole("using formData")
var formData = Components.classes["@mozilla.org/files/formdata;1"]
.createInstance(Components.interfaces.nsIDOMFormData);
var file = new File( nsFile );
formData.append( formElementName, file );
req.send( formData );
}
else
{
// Try to determine the MIME type of the file
var mimeType = "text/plain";
try {
@@ -482,53 +553,13 @@ var sendtophoneCore = {
multiStream.appendStream(mimeStream);
multiStream.appendStream(endBoundryStream);
var req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance(Ci.nsIXMLHttpRequest);
// Show the progress of uploads
sendtophoneUploadsManager.addUpload(nsFile, req);
req.open('POST', uri, true);
req.setRequestHeader("Content-length",multiStream.available());
req.setRequestHeader("Content-type","multipart/form-data; boundary="+boundary);
req.addEventListener("load", function(event)
{
// If there's a callback (to delete temporary files) we call it now
if (callback)
callback();
onSuccess(event.target, uploadName);
}, false);
// Handle errors or aborted uploads
req.addEventListener("error", function(evt)
{
sendtophoneCore.alert("Error while sending the file to the server:\r\n" + uri);
// If there's a callback (to delete temporary files) we call it now
if (callback)
callback();
}, false);
req.addEventListener("abort", function(evt)
{
// Silent.
// If there's a callback (to delete temporary files) we call it now
if (callback)
callback();
}, false);
// Enable cookies
try {
req.channel.QueryInterface(Ci.nsIHttpChannelInternal).
forceAllowThirdPartyCookie = true;
}
catch(ex) {}
req.send(multiStream);
}
}
};
// http://min.us/