1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-26 10:52: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');
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
@ -118,11 +120,16 @@ define(function(require) {
* Do PBKDF2 key derivation in a WebWorker thread
*/
self.deriveKey = function(password, keySize, callback) {
startWorker('/crypto/pbkdf2-worker.js', {
startWorker({
script: PBKDF2_WORKER,
args: {
password: password,
keySize: keySize
}, callback, function() {
},
callback: callback,
noWorker: function() {
return pbkdf2.getKey(password, keySize);
}
});
};
@ -147,27 +154,37 @@ define(function(require) {
envelopes.push(envelope);
});
startWorker('/crypto/crypto-batch-worker.js', {
startWorker({
script: BATCH_WORKER,
args: {
type: 'symEncrypt',
list: envelopes
}, function(err, encryptedList) {
},
callback: function(err, encryptedList) {
// return generated secret key
callback(err, {
key: key,
list: encryptedList
});
}, function() {
},
noWorker: function() {
return cryptoBatch.authEncryptList(envelopes);
}
});
};
self.symDecryptList = function(list, keys, callback) {
startWorker('/crypto/crypto-batch-worker.js', {
startWorker({
script: BATCH_WORKER,
args: {
type: 'symDecrypt',
list: list,
keys: keys
}, callback, function() {
},
callback: callback,
noWorker: function() {
return cryptoBatch.authDecryptList(list, keys);
}
});
};
@ -203,13 +220,18 @@ define(function(require) {
envelopes.push(envelope);
});
startWorker('/crypto/crypto-batch-worker.js', {
startWorker({
script: BATCH_WORKER,
args: {
type: 'asymEncrypt',
list: envelopes,
senderPrivkey: senderPrivkey,
receiverPubkeys: receiverPubkeys
}, callback, function() {
},
callback: callback,
noWorker: function() {
return cryptoBatch.encryptListForUser(envelopes, receiverPubkeys, senderPrivkey);
}
});
};
@ -227,13 +249,18 @@ define(function(require) {
privateKey: keypair.privkeyPem
};
startWorker('/crypto/crypto-batch-worker.js', {
startWorker({
script: BATCH_WORKER,
args: {
type: 'asymDecrypt',
list: list,
receiverPrivkey: receiverPrivkey,
senderPubkeys: senderPubkeys
}, callback, function() {
},
callback: callback,
noWorker: function() {
return cryptoBatch.decryptListForUser(list, senderPubkeys, receiverPrivkey);
}
});
};
@ -248,24 +275,34 @@ define(function(require) {
privateKey: keypair.privkeyPem
};
startWorker('/crypto/crypto-batch-worker.js', {
startWorker({
script: BATCH_WORKER,
args: {
type: 'reencrypt',
list: list,
receiverPrivkey: receiverPrivkey,
senderPubkeys: senderPubkeys,
symKey: passBasedKey
}, callback, function() {
},
callback: callback,
noWorker: function() {
return cryptoBatch.reencryptListKeysForUser(list, senderPubkeys, receiverPrivkey, passBasedKey);
}
});
};
self.decryptKeysAndList = function(list, callback) {
startWorker('/crypto/crypto-batch-worker.js', {
startWorker({
script: BATCH_WORKER,
args: {
type: 'decryptItems',
list: list,
symKey: passBasedKey
}, callback, function() {
},
callback: callback,
noWorker: function() {
return cryptoBatch.decryptKeysAndList(list, passBasedKey);
}
});
};
@ -273,39 +310,36 @@ define(function(require) {
// helper functions
//
function startWorker(script, args, callback, noWorker) {
function startWorker(options) {
// check for WebWorker support
if (window.Worker) {
// init webworker thread
var worker = new Worker(app.config.workerPath + script);
var worker = new Worker(app.config.workerPath + options.script);
worker.onmessage = function(e) {
if (e.data.err) {
callback(e.data.err);
options.callback(e.data.err);
return;
}
// return result from the worker
callback(null, e.data);
options.callback(null, e.data);
};
// 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();
result = options.noWorker();
} catch (e) {
callback({
// return error
options.callback({
errMsg: (e.message) ? e.message : e
});
return;
}
callback(null, result);
}
options.callback(null, result);
}
return self;