offload creating an olm session to the olm worker

so IE11 doesn't lock up when you start typing
This commit is contained in:
Bruno Windels 2020-11-10 11:04:09 +01:00
parent bd5771e449
commit 5f6ad91ff2
4 changed files with 27 additions and 3 deletions

View File

@ -149,10 +149,14 @@ export class Account {
}
}
createOutboundOlmSession(theirIdentityKey, theirOneTimeKey) {
async createOutboundOlmSession(theirIdentityKey, theirOneTimeKey) {
const newSession = new this._olm.Session();
try {
newSession.create_outbound(this._account, theirIdentityKey, theirOneTimeKey);
if (this._olmWorker) {
await this._olmWorker.createOutboundOlmSession(this._account, newSession, theirIdentityKey, theirOneTimeKey);
} else {
newSession.create_outbound(this._account, theirIdentityKey, theirOneTimeKey);
}
return newSession;
} catch (err) {
newSession.free();

View File

@ -37,6 +37,12 @@ export class OlmWorker {
account.unpickle("", pickle);
}
async createOutboundSession(account, newSession, theirIdentityKey, theirOneTimeKey) {
const accountPickle = account.pickle("");
const sessionPickle = await this._workerPool.send({type: "olm_create_outbound", accountPickle, theirIdentityKey, theirOneTimeKey}).response();
newSession.unpickle("", sessionPickle);
}
dispose() {
this._workerPool.dispose();
}

View File

@ -154,7 +154,7 @@ export class Encryption {
try {
for (const target of newEncryptionTargets) {
const {device, oneTimeKey} = target;
target.session = this._account.createOutboundOlmSession(device.curve25519Key, oneTimeKey);
target.session = await this._account.createOutboundOlmSession(device.curve25519Key, oneTimeKey);
}
this._storeSessions(newEncryptionTargets, timestamp);
} catch (err) {

View File

@ -136,6 +136,18 @@ class MessageHandler {
account.generate_one_time_keys(otkAmount);
this._checkRandomValuesUsed();
return account.pickle("");
_olmCreateOutbound(accountPickle, theirIdentityKey, theirOneTimeKey) {
return this._toMessage(() => {
const account = new this._olm.Account();
const newSession = new this._olm.Session();
try {
account.unpickle("", accountPickle);
newSession.create_outbound(account, newSession, theirIdentityKey, theirOneTimeKey);
return newSession.pickle("");
} finally {
account.free();
newSession.free();
}
});
}
@ -149,6 +161,8 @@ class MessageHandler {
this._sendReply(message, this._megolmDecrypt(message.sessionKey, message.ciphertext));
} else if (type === "olm_create_account_otks") {
this._sendReply(message, this._olmCreateAccountAndOTKs(message.randomValues, message.otkAmount));
} else if (type === "olm_create_outbound") {
this._sendReply(message, this._olmCreateOutbound(message.accountPickle, message.theirIdentityKey, message.theirOneTimeKey));
}
}
}