Commit 5e6bf7f0 authored by Patrick Brunschwig's avatar Patrick Brunschwig

moved dispatchMessages() from enigmail.js to enigmailConvert.jsm

parent 41956720
......@@ -2784,60 +2784,6 @@ function getEnigmailString(aStr) {
Filter actions for decrypting messages permanently
********************************************************************************/
/***
* dispatchMessages
*
* Because thunderbird throws all messages at once at us thus we have to rate limit the dispatching
* of the message processing. Because there is only a negligible performance gain when dispatching
* several message at once we serialize to not overwhelm low power devices.
*
* The function is implemented such that the 1st call (requireSync == true) is a synchronous function,
* while any other call is asynchronous. This is required to make the filters work correctly in case
* there are other filters that work on the message. (see bug 374).
*
* Parameters
* aMsgHdrs: Array of nsIMsgDBHdr
* targetFolder: String; target folder URI
* move: Boolean: type of action; true = "move" / false = "copy"
* requireSync: Boolean: true = require function to behave synchronously
* false = async function (no useful return value)
*
**/
function dispatchMessages(aMsgHdrs, targetFolder, move, requireSync) {
var inspector = Cc["@mozilla.org/jsinspector;1"].getService(Ci.nsIJSInspector);
var promise = EnigmailDecryptPermanently(aMsgHdrs[0], targetFolder, move);
var done = false;
var processNext = function (data) {
aMsgHdrs.splice(0,1);
if (aMsgHdrs.length > 0) {
dispatchMessages(aMsgHdrs, targetFolder, move, false);
}
else {
// last message was finished processing
done = true;
EC.DEBUG_LOG("enigmail.js: dispatchMessage: exit nested loop\n");
inspector.exitNestedEventLoop();
}
};
promise.then(processNext);
promise.catch(function(err) {
EC.ERROR_LOG("enigmail.js: dispatchMessage: caught error: "+err+"\n");
processNext(null);
});
if (requireSync && ! done) {
// wait here until all messages processed, such that the function returns
// synchronously
EC.DEBUG_LOG("enigmail.js: dispatchMessage: enter nested loop\n");
inspector.enterNestedEventLoop({value : 0});
}
}
/**
* filter action for creating a decrypted version of the mail and
* deleting the original mail at the same time
......@@ -2857,7 +2803,7 @@ var filterActionMoveDecrypt = {
msgHdrs.push(aMsgHdrs.queryElementAt(i, Ci.nsIMsgDBHdr));
}
dispatchMessages(msgHdrs, aActionValue, true, true);
EnigmailDecryptPermanently.dispatchMessages(msgHdrs, aActionValue, true, true);
return;
},
......@@ -2909,7 +2855,7 @@ var filterActionCopyDecrypt = {
msgHdrs.push(aMsgHdrs.queryElementAt(i, Ci.nsIMsgDBHdr));
}
dispatchMessages(msgHdrs, aActionValue, false, true);
EnigmailDecryptPermanently.dispatchMessages(msgHdrs, aActionValue, false, true);
return;
},
......
......@@ -65,7 +65,9 @@ Components.utils.import("resource:///modules/MailUtils.js");
Components.utils.import("resource://enigmail/enigmailCore.jsm");
Components.utils.import("resource://enigmail/enigmailCommon.jsm");
Components.utils.import("resource://enigmail/commonFuncs.jsm");
var Ec = EnigmailCommon;
var EC = EnigmailCore;
var EXPORTED_SYMBOLS = ["EnigmailDecryptPermanently"];
......@@ -87,31 +89,87 @@ const STATUS_NOT_REQUIRED = 2;
*
* @return a Promise that we do that
*/
function EnigmailDecryptPermanently(hdr, destFolder, move) {
return new Promise(
function(resolve, reject) {
let msgUriSpec = hdr.folder.getUriForMsg(hdr);
Ec.DEBUG_LOG("enigmailConvert.jsm: EnigmailDecryptPermanently: MessageUri: "+msgUriSpec+"\n");
var EnigmailDecryptPermanently = {
/***
* dispatchMessages
*
* Because Thunderbird throws all messages at once at us thus we have to rate limit the dispatching
* of the message processing. Because there is only a negligible performance gain when dispatching
* several message at once we serialize to not overwhelm low power devices.
*
* The function is implemented such that the 1st call (requireSync == true) is a synchronous function,
* while any other call is asynchronous. This is required to make the filters work correctly in case
* there are other filters that work on the message. (see bug 374).
*
* Parameters
* aMsgHdrs: Array of nsIMsgDBHdr
* targetFolder: String; target folder URI
* move: Boolean: type of action; true = "move" / false = "copy"
* requireSync: Boolean: true = require function to behave synchronously
* false = async function (no useful return value)
*
**/
dispatchMessages: function(aMsgHdrs, targetFolder, move, requireSync) {
var inspector = Cc["@mozilla.org/jsinspector;1"].getService(Ci.nsIJSInspector);
var promise = EnigmailDecryptPermanently.decryptMessage(aMsgHdrs[0], targetFolder, move);
var done = false;
var processNext = function (data) {
aMsgHdrs.splice(0,1);
if (aMsgHdrs.length > 0) {
EnigmailDecryptPermanently.dispatchMessages(aMsgHdrs, targetFolder, move, false);
}
else {
// last message was finished processing
done = true;
EC.DEBUG_LOG("enigmailConvert.jsm: dispatchMessage: exit nested loop\n");
inspector.exitNestedEventLoop();
}
};
var messenger = Cc["@mozilla.org/messenger;1"].createInstance(Ci.nsIMessenger);
var msgSvc = messenger.messageServiceFromURI(msgUriSpec);
promise.then(processNext);
var decrypt = new decryptMessageIntoFolder(destFolder, move, resolve);
promise.catch(function(err) {
EC.ERROR_LOG("enigmailConvert.jsm: dispatchMessage: caught error: "+err+"\n");
processNext(null);
});
Ec.DEBUG_LOG("enigmailConvert.jsm: EnigmailDecryptPermanently: Calling MsgHdrToMimeMessage\n");
try {
MsgHdrToMimeMessage(hdr, decrypt, decrypt.messageParseCallback, true, {examineEncryptedParts: false, partsOnDemand: false});
}
catch (ex) {
Ec.ERROR_LOG("enigmailConvert.jsm: MsgHdrToMimeMessage failed: "+ex.toString()+"\n");
reject("MsgHdrToMimeMessage failed");
}
return;
if (requireSync && ! done) {
// wait here until all messages processed, such that the function returns
// synchronously
EC.DEBUG_LOG("enigmailConvert.jsm: dispatchMessage: enter nested loop\n");
inspector.enterNestedEventLoop({value : 0});
}
);
};
},
decryptMessage: function (hdr, destFolder, move) {
return new Promise(
function(resolve, reject) {
let msgUriSpec = hdr.folder.getUriForMsg(hdr);
Ec.DEBUG_LOG("enigmailConvert.jsm: decryptMessage: MessageUri: "+msgUriSpec+"\n");
var messenger = Cc["@mozilla.org/messenger;1"].createInstance(Ci.nsIMessenger);
var msgSvc = messenger.messageServiceFromURI(msgUriSpec);
var decrypt = new decryptMessageIntoFolder(destFolder, move, resolve);
Ec.DEBUG_LOG("enigmailConvert.jsm: EnigmailDecryptPermanently: Calling MsgHdrToMimeMessage\n");
try {
MsgHdrToMimeMessage(hdr, decrypt, decrypt.messageParseCallback, true, {examineEncryptedParts: false, partsOnDemand: false});
}
catch (ex) {
Ec.ERROR_LOG("enigmailConvert.jsm: MsgHdrToMimeMessage failed: "+ex.toString()+"\n");
reject("MsgHdrToMimeMessage failed");
}
return;
}
);
}
}
function decryptMessageIntoFolder(destFolder, move, resolve) {
this.destFolder = destFolder;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment