diff --git a/lib/encryption/encryption.dart b/lib/encryption/encryption.dart index db56cb5..aa627a9 100644 --- a/lib/encryption/encryption.dart +++ b/lib/encryption/encryption.dart @@ -23,24 +23,13 @@ import 'package:pedantic/pedantic.dart'; import '../famedlysdk.dart'; import '../matrix_api.dart'; -import '../src/utils/logs.dart'; +import '../src/utils/run_in_root.dart'; import 'cross_signing.dart'; import 'key_manager.dart'; import 'key_verification_manager.dart'; import 'olm_manager.dart'; import 'ssss.dart'; -Future _runInRoot(FutureOr 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 { final Client client; final bool debug; @@ -80,7 +69,7 @@ class Encryption { } void handleDeviceOneTimeKeysCount(Map countJson) { - _runInRoot(() => olmManager.handleDeviceOneTimeKeysCount(countJson)); + runInRoot(() => olmManager.handleDeviceOneTimeKeysCount(countJson)); } void onSync() { @@ -96,21 +85,21 @@ class Encryption { 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 // them in the background - unawaited(_runInRoot(() => keyManager.handleToDeviceEvent(event))); + unawaited(runInRoot(() => keyManager.handleToDeviceEvent(event))); } if (event.type.startsWith('m.key.verification.')) { // some key verification event. No need to handle it now, we can easily // do this in the background unawaited( - _runInRoot(() => keyVerificationManager.handleToDeviceEvent(event))); + runInRoot(() => keyVerificationManager.handleToDeviceEvent(event))); } if (event.type.startsWith('m.secret.')) { // 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) { // 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.'))) { // "just" key verification, no need to do this in sync unawaited( - _runInRoot(() => keyVerificationManager.handleEventUpdate(update))); + runInRoot(() => keyVerificationManager.handleEventUpdate(update))); } if (update.content['sender'] == client.userID && !update.content['unsigned'].containsKey('transaction_id')) { // maybe we need to re-try SSSS secrets - unawaited(_runInRoot(() => ssss.periodicallyRequestMissingCache())); + unawaited(runInRoot(() => ssss.periodicallyRequestMissingCache())); } } diff --git a/lib/encryption/key_manager.dart b/lib/encryption/key_manager.dart index 4da6c14..3c4ce5b 100644 --- a/lib/encryption/key_manager.dart +++ b/lib/encryption/key_manager.dart @@ -29,6 +29,7 @@ import '../matrix_api.dart'; import '../src/database/database.dart'; import '../src/utils/logs.dart'; import '../src/utils/run_in_background.dart'; +import '../src/utils/run_in_root.dart'; const MEGOLM_KEY = 'm.megolm_backup.v1'; @@ -208,7 +209,8 @@ class KeyManager { !client.isUnknownSession) { // do e2ee recovery _requestedSessionIds.add(requestIdent); - unawaited(request(room, sessionId, senderKey, askOnlyOwnDevices: true)); + unawaited(runInRoot(() => + request(room, sessionId, senderKey, askOnlyOwnDevices: true))); } return null; } diff --git a/lib/src/utils/run_in_root.dart b/lib/src/utils/run_in_root.dart new file mode 100644 index 0000000..b898dde --- /dev/null +++ b/lib/src/utils/run_in_root.dart @@ -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 . + */ + +import 'dart:async'; + +import 'logs.dart'; + +Future runInRoot(FutureOr 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; + }); +}