crypto: Forward sync crypto chagnes to the rust side

This commit is contained in:
Damir Jelić 2021-02-17 15:50:49 +01:00
parent 01149c8d45
commit e2692ec604
6 changed files with 61 additions and 18 deletions

View file

@ -98,6 +98,9 @@ import org.matrix.android.sdk.internal.extensions.foldToCallback
import org.matrix.android.sdk.internal.session.SessionScope
import org.matrix.android.sdk.internal.session.room.membership.LoadRoomMembersTask
import org.matrix.android.sdk.internal.session.sync.model.SyncResponse
import org.matrix.android.sdk.internal.session.sync.model.DeviceListResponse
import org.matrix.android.sdk.internal.session.sync.model.DeviceOneTimeKeysCountSyncResponse
import org.matrix.android.sdk.internal.session.sync.model.ToDeviceSyncResponse
import org.matrix.android.sdk.internal.task.TaskExecutor
import org.matrix.android.sdk.internal.task.TaskThread
import org.matrix.android.sdk.internal.task.configureWith
@ -321,9 +324,7 @@ internal class DefaultCryptoService @Inject constructor(
*
*/
suspend fun start() {
cryptoCoroutineScope.launch(coroutineDispatchers.crypto) {
internalStart()
}
internalStart()
// Just update
fetchDevicesList(NoOpMatrixCallback())
@ -939,6 +940,13 @@ internal class DefaultCryptoService @Inject constructor(
}
}
suspend fun receiveSyncChanges(
toDevice: ToDeviceSyncResponse?,
deviceChanges: DeviceListResponse?,
keyCounts: DeviceOneTimeKeysCountSyncResponse?) {
olmMachine!!.receiveSyncChanges(toDevice, deviceChanges, keyCounts)
}
private suspend fun sendOutgoingRequests() {
// TODO these requests should be sent out in parallel
for (outgoingRequest in olmMachine!!.outgoingRequests()) {

View file

@ -19,17 +19,21 @@ package org.matrix.android.sdk.internal
import java.io.File
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.matrix.android.sdk.internal.di.MoshiProvider
import org.matrix.android.sdk.internal.session.sync.model.DeviceListResponse
import org.matrix.android.sdk.internal.session.sync.model.DeviceOneTimeKeysCountSyncResponse
import org.matrix.android.sdk.internal.session.sync.model.ToDeviceSyncResponse
import timber.log.Timber
import uniffi.olm.Device as InnerDevice
import uniffi.olm.DeviceLists
import uniffi.olm.Logger
import uniffi.olm.OlmMachine as InnerMachine
import uniffi.olm.Request
import uniffi.olm.RequestType
import uniffi.olm.Sas as InnerSas
import uniffi.olm.Logger
import uniffi.olm.setLogger
import timber.log.Timber
class CryptoLogger(): Logger {
class CryptoLogger() : Logger {
override fun log(logLine: String) {
Timber.d(logLine)
}
@ -60,7 +64,7 @@ class Device(inner: InnerDevice, machine: InnerMachine) {
}
}
class OlmMachine(user_id: String, device_id: String, path: File) {
internal class OlmMachine(user_id: String, device_id: String, path: File) {
private val inner: InnerMachine = InnerMachine(user_id, device_id, path.toString())
fun userId(): String {
@ -79,6 +83,24 @@ class OlmMachine(user_id: String, device_id: String, path: File) {
inner.outgoingRequests()
}
suspend fun receiveSyncChanges(
toDevice: ToDeviceSyncResponse?,
deviceChanges: DeviceListResponse?,
keyCounts: DeviceOneTimeKeysCountSyncResponse?
) = withContext(Dispatchers.IO) {
var counts: MutableMap<String, Int> = mutableMapOf()
if (keyCounts?.signedCurve25519 != null) {
counts.put("signed_curve25519", keyCounts.signedCurve25519)
}
val devices = DeviceLists(deviceChanges?.changed ?: listOf(), deviceChanges?.left ?: listOf())
val adapter = MoshiProvider.providesMoshi().adapter<ToDeviceSyncResponse>(ToDeviceSyncResponse::class.java)
val events = adapter.toJson(toDevice ?: ToDeviceSyncResponse())!!
inner.receiveSyncChanges(events, devices, counts)
}
suspend fun markRequestAsSent(
request_id: String,
request_type: RequestType,

View file

@ -62,9 +62,10 @@ internal class SyncResponseHandler @Inject constructor(@SessionDatabase private
measureTimeMillis {
if (!cryptoService.isStarted()) {
Timber.v("Should start cryptoService")
// TODO shirley there's a better place for this than the sync
// loop?
cryptoService.start()
}
cryptoService.onSyncWillProcess(isInitialSync)
}.also {
Timber.v("Finish handling start cryptoService in $it ms")
}
@ -73,11 +74,12 @@ internal class SyncResponseHandler @Inject constructor(@SessionDatabase private
// to ensure to decrypt them properly
measureTimeMillis {
Timber.v("Handle toDevice")
reportSubtask(reporter, R.string.initial_sync_start_importing_account_crypto, 100, 0.1f) {
if (syncResponse.toDevice != null) {
cryptoSyncHandler.handleToDevice(syncResponse.toDevice, reporter)
}
}
cryptoService.receiveSyncChanges(
syncResponse.toDevice,
syncResponse.deviceLists,
syncResponse.deviceOneTimeKeysCount
)
}.also {
Timber.v("Finish handling toDevice in $it ms")
}

View file

@ -18,6 +18,10 @@ thiserror = "1.0.23"
tracing = "0.1.23"
tracing-subscriber = "0.2.15"
[dependencies.js_int]
version = "0.2.0"
features = ["lax_deserialize"]
[dependencies.matrix-sdk-crypto]
git = "https://github.com/matrix-org/matrix-rust-sdk/"
features = ["sled_cryptostore"]

View file

@ -1,6 +1,6 @@
use std::{
collections::{BTreeMap, HashMap},
convert::TryFrom,
convert::{TryFrom, TryInto},
};
use http::Response;
@ -301,13 +301,20 @@ impl OlmMachine {
&self,
events: &str,
device_changes: DeviceLists,
key_counts: HashMap<String, u32>,
key_counts: HashMap<String, i32>,
) {
let events: ToDevice = serde_json::from_str(events).unwrap();
let device_changes: RumaDeviceLists = device_changes.into();
let key_counts: BTreeMap<DeviceKeyAlgorithm, UInt> = key_counts
.into_iter()
.map(|(k, v)| (DeviceKeyAlgorithm::from(k), v.into()))
.map(|(k, v)| {
(
DeviceKeyAlgorithm::from(k),
v.clamp(0, i32::MAX)
.try_into()
.expect("Couldn't convert key counts into an UInt"),
)
})
.collect();
self.runtime

View file

@ -56,7 +56,7 @@ interface OlmMachine {
void receive_sync_changes([ByRef] string events,
DeviceLists device_changes,
record<DOMString, u32> key_counts);
record<DOMString, i32> key_counts);
record<DOMString, string> identity_keys();