crypto: Fix a crash when we access the devices before the olmMachine is set up

The crypto service is fully initialized only after the first sync but EA
seems to access live devices before that. This results in a crash since
we now use the olm machine to access devices.
This commit is contained in:
Damir Jelić 2021-04-08 15:55:38 +02:00
parent edfd1b2fe0
commit 5253f9708c
2 changed files with 8 additions and 17 deletions

View file

@ -150,6 +150,7 @@ internal class DefaultCryptoService @Inject constructor(
private val isStarting = AtomicBoolean(false)
private val isStarted = AtomicBoolean(false)
private var olmMachine: OlmMachine? = null
private val deviceObserver: DeviceUpdateObserver = DeviceUpdateObserver()
suspend fun onStateEvent(roomId: String, event: Event) {
when (event.getClearType()) {
@ -320,7 +321,7 @@ internal class DefaultCryptoService @Inject constructor(
try {
setRustLogger()
olmMachine = OlmMachine(userId, deviceId!!, dataDir)
olmMachine = OlmMachine(userId, deviceId!!, dataDir, deviceObserver)
Timber.v("HELLLO WORLD STARTING $dataDir CRYPTO ${olmMachine?.identityKeys()}")
} catch (throwable: Throwable) {
@ -393,14 +394,8 @@ internal class DefaultCryptoService @Inject constructor(
*/
override fun getDeviceInfo(userId: String, deviceId: String?): CryptoDeviceInfo? {
return if (userId.isNotEmpty() && !deviceId.isNullOrEmpty()) {
val device = runBlocking {
olmMachine!!.getDevice(userId, deviceId)
}
if (device != null) {
device.toCryptoDeviceInfo()
} else {
null
runBlocking {
olmMachine?.getDevice(userId, deviceId)?.toCryptoDeviceInfo()
}
} else {
null
@ -409,7 +404,7 @@ internal class DefaultCryptoService @Inject constructor(
override fun getCryptoDeviceInfo(userId: String): List<CryptoDeviceInfo> {
return runBlocking {
olmMachine!!.getUserDevices(userId)
olmMachine?.getUserDevices(userId) ?: listOf()
}
}
@ -419,7 +414,7 @@ internal class DefaultCryptoService @Inject constructor(
override fun getLiveCryptoDeviceInfo(userIds: List<String>): LiveData<List<CryptoDeviceInfo>> {
return runBlocking {
olmMachine!!.getLiveDevices(userIds)
olmMachine?.getLiveDevices(userIds) ?: LiveDevice(userIds, deviceObserver)
}
}

View file

@ -70,10 +70,6 @@ internal class LiveDevice(
var userIds: List<String> = userIds
private var observer: DeviceUpdateObserver = observer
private val listener = { devices: List<CryptoDeviceInfo> ->
value = devices
}
override fun onActive() {
observer.addDeviceUpdateListener(this)
}
@ -140,9 +136,9 @@ internal class DeviceUpdateObserver() {
}
}
internal class OlmMachine(user_id: String, device_id: String, path: File) {
internal class OlmMachine(user_id: String, device_id: String, path: File, deviceObserver: DeviceUpdateObserver) {
private val inner: InnerMachine = InnerMachine(user_id, device_id, path.toString())
private val deviceUpdateObserver = DeviceUpdateObserver()
private val deviceUpdateObserver = deviceObserver
/**
* Get our own user ID.