fix: Run automated key requests in root zone

This commit is contained in:
Sorunome 2020-09-21 18:10:46 +02:00
parent 70939a7c9c
commit 86a4f90a5a
No known key found for this signature in database
GPG key ID: B19471D07FC9BE9C
3 changed files with 43 additions and 20 deletions

View file

@ -23,24 +23,13 @@ import 'package:pedantic/pedantic.dart';
import '../famedlysdk.dart'; import '../famedlysdk.dart';
import '../matrix_api.dart'; import '../matrix_api.dart';
import '../src/utils/logs.dart'; import '../src/utils/run_in_root.dart';
import 'cross_signing.dart'; import 'cross_signing.dart';
import 'key_manager.dart'; import 'key_manager.dart';
import 'key_verification_manager.dart'; import 'key_verification_manager.dart';
import 'olm_manager.dart'; import 'olm_manager.dart';
import 'ssss.dart'; import 'ssss.dart';
Future<T> _runInRoot<T>(FutureOr<T> Function() fn) async {
return await Zone.root.run(() async {
try {
return await fn();
} catch (e, s) {
Logs.error('Error thrown in root zone: ' + e.toString(), s);
}
return null;
});
}
class Encryption { class Encryption {
final Client client; final Client client;
final bool debug; final bool debug;
@ -80,7 +69,7 @@ class Encryption {
} }
void handleDeviceOneTimeKeysCount(Map<String, int> countJson) { void handleDeviceOneTimeKeysCount(Map<String, int> countJson) {
_runInRoot(() => olmManager.handleDeviceOneTimeKeysCount(countJson)); runInRoot(() => olmManager.handleDeviceOneTimeKeysCount(countJson));
} }
void onSync() { void onSync() {
@ -96,21 +85,21 @@ class Encryption {
if (['m.room_key_request', 'm.forwarded_room_key'].contains(event.type)) { if (['m.room_key_request', 'm.forwarded_room_key'].contains(event.type)) {
// "just" room key request things. We don't need these asap, so we handle // "just" room key request things. We don't need these asap, so we handle
// them in the background // them in the background
unawaited(_runInRoot(() => keyManager.handleToDeviceEvent(event))); unawaited(runInRoot(() => keyManager.handleToDeviceEvent(event)));
} }
if (event.type.startsWith('m.key.verification.')) { if (event.type.startsWith('m.key.verification.')) {
// some key verification event. No need to handle it now, we can easily // some key verification event. No need to handle it now, we can easily
// do this in the background // do this in the background
unawaited( unawaited(
_runInRoot(() => keyVerificationManager.handleToDeviceEvent(event))); runInRoot(() => keyVerificationManager.handleToDeviceEvent(event)));
} }
if (event.type.startsWith('m.secret.')) { if (event.type.startsWith('m.secret.')) {
// some ssss thing. We can do this in the background // some ssss thing. We can do this in the background
unawaited(_runInRoot(() => ssss.handleToDeviceEvent(event))); unawaited(runInRoot(() => ssss.handleToDeviceEvent(event)));
} }
if (event.sender == client.userID) { if (event.sender == client.userID) {
// maybe we need to re-try SSSS secrets // maybe we need to re-try SSSS secrets
unawaited(_runInRoot(() => ssss.periodicallyRequestMissingCache())); unawaited(runInRoot(() => ssss.periodicallyRequestMissingCache()));
} }
} }
@ -125,12 +114,12 @@ class Encryption {
.startsWith('m.key.verification.'))) { .startsWith('m.key.verification.'))) {
// "just" key verification, no need to do this in sync // "just" key verification, no need to do this in sync
unawaited( unawaited(
_runInRoot(() => keyVerificationManager.handleEventUpdate(update))); runInRoot(() => keyVerificationManager.handleEventUpdate(update)));
} }
if (update.content['sender'] == client.userID && if (update.content['sender'] == client.userID &&
!update.content['unsigned'].containsKey('transaction_id')) { !update.content['unsigned'].containsKey('transaction_id')) {
// maybe we need to re-try SSSS secrets // maybe we need to re-try SSSS secrets
unawaited(_runInRoot(() => ssss.periodicallyRequestMissingCache())); unawaited(runInRoot(() => ssss.periodicallyRequestMissingCache()));
} }
} }

View file

@ -29,6 +29,7 @@ import '../matrix_api.dart';
import '../src/database/database.dart'; import '../src/database/database.dart';
import '../src/utils/logs.dart'; import '../src/utils/logs.dart';
import '../src/utils/run_in_background.dart'; import '../src/utils/run_in_background.dart';
import '../src/utils/run_in_root.dart';
const MEGOLM_KEY = 'm.megolm_backup.v1'; const MEGOLM_KEY = 'm.megolm_backup.v1';
@ -208,7 +209,8 @@ class KeyManager {
!client.isUnknownSession) { !client.isUnknownSession) {
// do e2ee recovery // do e2ee recovery
_requestedSessionIds.add(requestIdent); _requestedSessionIds.add(requestIdent);
unawaited(request(room, sessionId, senderKey, askOnlyOwnDevices: true)); unawaited(runInRoot(() =>
request(room, sessionId, senderKey, askOnlyOwnDevices: true)));
} }
return null; return null;
} }

View file

@ -0,0 +1,32 @@
/*
* Famedly Matrix SDK
* Copyright (C) 2020 Famedly GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import 'dart:async';
import 'logs.dart';
Future<T> runInRoot<T>(FutureOr<T> Function() fn) async {
return await Zone.root.run(() async {
try {
return await fn();
} catch (e, s) {
Logs.error('Error thrown in root zone: ' + e.toString(), s);
}
return null;
});
}