1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-22 17:02:17 -05:00

cleanup web worker callback code to use options argument

This commit is contained in:
Tankred Hase 2013-08-31 16:29:18 +02:00
parent 873671f011
commit 04e672552b

View File

@ -13,7 +13,9 @@ define(function(require) {
app = require('js/app-config'); app = require('js/app-config');
var self = {}, var self = {},
passBasedKey; passBasedKey,
BATCH_WORKER = '/crypto/crypto-batch-worker.js',
PBKDF2_WORKER = '/crypto/pbkdf2-worker.js';
/** /**
* Initializes the crypto modules by fetching the user's * Initializes the crypto modules by fetching the user's
@ -118,11 +120,16 @@ define(function(require) {
* Do PBKDF2 key derivation in a WebWorker thread * Do PBKDF2 key derivation in a WebWorker thread
*/ */
self.deriveKey = function(password, keySize, callback) { self.deriveKey = function(password, keySize, callback) {
startWorker('/crypto/pbkdf2-worker.js', { startWorker({
password: password, script: PBKDF2_WORKER,
keySize: keySize args: {
}, callback, function() { password: password,
return pbkdf2.getKey(password, keySize); keySize: keySize
},
callback: callback,
noWorker: function() {
return pbkdf2.getKey(password, keySize);
}
}); });
}; };
@ -147,27 +154,37 @@ define(function(require) {
envelopes.push(envelope); envelopes.push(envelope);
}); });
startWorker('/crypto/crypto-batch-worker.js', { startWorker({
type: 'symEncrypt', script: BATCH_WORKER,
list: envelopes args: {
}, function(err, encryptedList) { type: 'symEncrypt',
// return generated secret key list: envelopes
callback(err, { },
key: key, callback: function(err, encryptedList) {
list: encryptedList // return generated secret key
}); callback(err, {
}, function() { key: key,
return cryptoBatch.authEncryptList(envelopes); list: encryptedList
});
},
noWorker: function() {
return cryptoBatch.authEncryptList(envelopes);
}
}); });
}; };
self.symDecryptList = function(list, keys, callback) { self.symDecryptList = function(list, keys, callback) {
startWorker('/crypto/crypto-batch-worker.js', { startWorker({
type: 'symDecrypt', script: BATCH_WORKER,
list: list, args: {
keys: keys type: 'symDecrypt',
}, callback, function() { list: list,
return cryptoBatch.authDecryptList(list, keys); keys: keys
},
callback: callback,
noWorker: function() {
return cryptoBatch.authDecryptList(list, keys);
}
}); });
}; };
@ -203,13 +220,18 @@ define(function(require) {
envelopes.push(envelope); envelopes.push(envelope);
}); });
startWorker('/crypto/crypto-batch-worker.js', { startWorker({
type: 'asymEncrypt', script: BATCH_WORKER,
list: envelopes, args: {
senderPrivkey: senderPrivkey, type: 'asymEncrypt',
receiverPubkeys: receiverPubkeys list: envelopes,
}, callback, function() { senderPrivkey: senderPrivkey,
return cryptoBatch.encryptListForUser(envelopes, receiverPubkeys, senderPrivkey); receiverPubkeys: receiverPubkeys
},
callback: callback,
noWorker: function() {
return cryptoBatch.encryptListForUser(envelopes, receiverPubkeys, senderPrivkey);
}
}); });
}; };
@ -227,13 +249,18 @@ define(function(require) {
privateKey: keypair.privkeyPem privateKey: keypair.privkeyPem
}; };
startWorker('/crypto/crypto-batch-worker.js', { startWorker({
type: 'asymDecrypt', script: BATCH_WORKER,
list: list, args: {
receiverPrivkey: receiverPrivkey, type: 'asymDecrypt',
senderPubkeys: senderPubkeys list: list,
}, callback, function() { receiverPrivkey: receiverPrivkey,
return cryptoBatch.decryptListForUser(list, senderPubkeys, receiverPrivkey); senderPubkeys: senderPubkeys
},
callback: callback,
noWorker: function() {
return cryptoBatch.decryptListForUser(list, senderPubkeys, receiverPrivkey);
}
}); });
}; };
@ -248,24 +275,34 @@ define(function(require) {
privateKey: keypair.privkeyPem privateKey: keypair.privkeyPem
}; };
startWorker('/crypto/crypto-batch-worker.js', { startWorker({
type: 'reencrypt', script: BATCH_WORKER,
list: list, args: {
receiverPrivkey: receiverPrivkey, type: 'reencrypt',
senderPubkeys: senderPubkeys, list: list,
symKey: passBasedKey receiverPrivkey: receiverPrivkey,
}, callback, function() { senderPubkeys: senderPubkeys,
return cryptoBatch.reencryptListKeysForUser(list, senderPubkeys, receiverPrivkey, passBasedKey); symKey: passBasedKey
},
callback: callback,
noWorker: function() {
return cryptoBatch.reencryptListKeysForUser(list, senderPubkeys, receiverPrivkey, passBasedKey);
}
}); });
}; };
self.decryptKeysAndList = function(list, callback) { self.decryptKeysAndList = function(list, callback) {
startWorker('/crypto/crypto-batch-worker.js', { startWorker({
type: 'decryptItems', script: BATCH_WORKER,
list: list, args: {
symKey: passBasedKey type: 'decryptItems',
}, callback, function() { list: list,
return cryptoBatch.decryptKeysAndList(list, passBasedKey); symKey: passBasedKey
},
callback: callback,
noWorker: function() {
return cryptoBatch.decryptKeysAndList(list, passBasedKey);
}
}); });
}; };
@ -273,39 +310,36 @@ define(function(require) {
// helper functions // helper functions
// //
function startWorker(script, args, callback, noWorker) { function startWorker(options) {
// check for WebWorker support // check for WebWorker support
if (window.Worker) { if (window.Worker) {
// init webworker thread // init webworker thread
var worker = new Worker(app.config.workerPath + script); var worker = new Worker(app.config.workerPath + options.script);
worker.onmessage = function(e) { worker.onmessage = function(e) {
if (e.data.err) { if (e.data.err) {
callback(e.data.err); options.callback(e.data.err);
return; return;
} }
// return result from the worker // return result from the worker
callback(null, e.data); options.callback(null, e.data);
}; };
// send data to the worker // send data to the worker
worker.postMessage(args); worker.postMessage(options.args);
return;
} else {
// no WebWorker support... do synchronous call
var result;
try {
result = noWorker();
} catch (e) {
callback({
errMsg: (e.message) ? e.message : e
});
return;
}
callback(null, result);
} }
// no WebWorker support... do synchronous call
var result;
try {
result = options.noWorker();
} catch (e) {
// return error
options.callback({
errMsg: (e.message) ? e.message : e
});
return;
}
options.callback(null, result);
} }
return self; return self;