use clock time instead of system

This commit is contained in:
Valere 2022-05-11 10:58:13 +02:00
parent 546d6fe56d
commit 992f477ab1
6 changed files with 30 additions and 21 deletions

View File

@ -16,6 +16,8 @@
package org.matrix.android.sdk.api.session.crypto.model
import org.matrix.android.sdk.internal.util.time.Clock
/**
* IncomingRoomKeyRequest class defines the incoming room keys request.
*/
@ -71,13 +73,13 @@ data class IncomingRoomKeyRequest(
}
}
fun fromRestRequest(senderId: String, request: RoomKeyShareRequest): IncomingRoomKeyRequest? {
fun fromRestRequest(senderId: String, request: RoomKeyShareRequest, clock: Clock): IncomingRoomKeyRequest? {
return IncomingRoomKeyRequest(
userId = senderId,
deviceId = request.requestingDeviceId,
requestId = request.requestId,
requestBody = request.body,
localCreationTimestamp = System.currentTimeMillis()
localCreationTimestamp = clock.epochMillis()
)
}
}

View File

@ -317,12 +317,12 @@ internal class DeviceListManager @Inject constructor(
val t0 = clock.epochMillis()
try {
val result = doKeyDownloadForUsers(downloadUsers)
Timber.v("## CRYPTO | downloadKeys() : doKeyDownloadForUsers succeeds after ${System.currentTimeMillis() - t0} ms")
Timber.v("## CRYPTO | downloadKeys() : doKeyDownloadForUsers succeeds after ${clock.epochMillis() - t0} ms")
result.also {
it.addEntriesFromMap(stored)
}
} catch (failure: Throwable) {
Timber.w(failure, "## CRYPTO | downloadKeys() : doKeyDownloadForUsers failed after ${System.currentTimeMillis() - t0} ms")
Timber.w(failure, "## CRYPTO | downloadKeys() : doKeyDownloadForUsers failed after ${clock.epochMillis() - t0} ms")
if (forceDownload) {
throw failure
} else {

View File

@ -44,6 +44,7 @@ import org.matrix.android.sdk.internal.crypto.store.IMXCryptoStore
import org.matrix.android.sdk.internal.crypto.tasks.SendToDeviceTask
import org.matrix.android.sdk.internal.session.SessionScope
import org.matrix.android.sdk.internal.task.SemaphoreCoroutineSequencer
import org.matrix.android.sdk.internal.util.time.Clock
import timber.log.Timber
import java.util.concurrent.Executors
import javax.inject.Inject
@ -60,7 +61,9 @@ internal class IncomingKeyRequestManager @Inject constructor(
private val cryptoConfig: MXCryptoConfig,
private val messageEncrypter: MessageEncrypter,
private val coroutineDispatchers: MatrixCoroutineDispatchers,
private val sendToDeviceTask: SendToDeviceTask) {
private val sendToDeviceTask: SendToDeviceTask,
private val clock: Clock,
) {
private val dispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
private val outgoingRequestScope = CoroutineScope(SupervisorJob() + dispatcher)
@ -135,7 +138,7 @@ internal class IncomingKeyRequestManager @Inject constructor(
MegolmRequestAction.Cancel -> {
// ignore, we can't cancel as it's not known (probably already processed)
// still notify app layer if it was passed up previously
IncomingRoomKeyRequest.fromRestRequest(senderId, request)?.let { iReq ->
IncomingRoomKeyRequest.fromRestRequest(senderId, request, clock)?.let { iReq ->
outgoingRequestScope.launch(coroutineDispatchers.computation) {
val listenersCopy = synchronized(gossipingRequestListeners) {
gossipingRequestListeners.toList()
@ -164,7 +167,7 @@ internal class IncomingKeyRequestManager @Inject constructor(
gossipingRequestListeners.toList()
}
listenersCopy.onEach {
IncomingRoomKeyRequest.fromRestRequest(senderId, request)?.let { iReq ->
IncomingRoomKeyRequest.fromRestRequest(senderId, request, clock)?.let { iReq ->
withContext(coroutineDispatchers.main) {
tryOrNull { it.onRequestCancelled(iReq) }
}
@ -287,7 +290,7 @@ internal class IncomingKeyRequestManager @Inject constructor(
sessionId = request.sessionId,
roomId = request.roomId
),
localCreationTimestamp = System.currentTimeMillis()
localCreationTimestamp = clock.epochMillis()
)
listenersCopy.onEach {
withContext(coroutineDispatchers.main) {

View File

@ -26,6 +26,7 @@ import org.matrix.android.sdk.api.session.crypto.model.ImportRoomKeysResult
import org.matrix.android.sdk.api.util.awaitCallback
import org.matrix.android.sdk.internal.crypto.keysbackup.DefaultKeysBackupService
import org.matrix.android.sdk.internal.crypto.store.IMXCryptoStore
import org.matrix.android.sdk.internal.util.time.Clock
import timber.log.Timber
import javax.inject.Inject
@ -40,7 +41,8 @@ private val loggerTag = LoggerTag("OutgoingGossipingRequestManager", LoggerTag.C
internal class PerSessionBackupQueryRateLimiter @Inject constructor(
private val coroutineDispatchers: MatrixCoroutineDispatchers,
private val keysBackupService: Lazy<DefaultKeysBackupService>,
private val cryptoStore: IMXCryptoStore
private val cryptoStore: IMXCryptoStore,
private val clock: Clock,
) {
companion object {
@ -54,7 +56,7 @@ internal class PerSessionBackupQueryRateLimiter @Inject constructor(
data class LastTry(
val backupVersion: String,
val timestamp: Long = System.currentTimeMillis()
val timestamp: Long
)
/**
@ -66,7 +68,7 @@ internal class PerSessionBackupQueryRateLimiter @Inject constructor(
private var backupVersion: KeysVersionResult? = null
private var savedKeyBackupKeyInfo: SavedKeyBackupKeyInfo? = null
var backupWasCheckedFromServer: Boolean = false
val now = System.currentTimeMillis()
val now = clock.epochMillis()
fun refreshBackupInfoIfNeeded(force: Boolean = false) {
if (backupWasCheckedFromServer && !force) return
@ -124,7 +126,7 @@ internal class PerSessionBackupQueryRateLimiter @Inject constructor(
return true
} else {
Timber.tag(loggerTag.value).v("Failed to find key in backup session:$sessionId in $roomId")
lastFailureMap[cacheKey] = LastTry(currentVersion.version)
lastFailureMap[cacheKey] = LastTry(currentVersion.version, clock.epochMillis())
return false
}
}

View File

@ -43,6 +43,7 @@ import org.matrix.android.sdk.internal.crypto.store.IMXCryptoStore
import org.matrix.android.sdk.internal.crypto.tasks.SendToDeviceTask
import org.matrix.android.sdk.internal.crypto.tasks.createUniqueTxnId
import org.matrix.android.sdk.internal.session.SessionScope
import org.matrix.android.sdk.internal.util.time.Clock
import timber.log.Timber
import javax.inject.Inject
@ -56,7 +57,8 @@ internal class SecretShareManager @Inject constructor(
private val messageEncrypter: MessageEncrypter,
private val ensureOlmSessionsForDevicesAction: EnsureOlmSessionsForDevicesAction,
private val sendToDeviceTask: SendToDeviceTask,
private val coroutineDispatchers: MatrixCoroutineDispatchers
private val coroutineDispatchers: MatrixCoroutineDispatchers,
private val clock: Clock,
) {
companion object {
@ -100,7 +102,7 @@ internal class SecretShareManager @Inject constructor(
// For now we just keep an in memory cache
cryptoCoroutineScope.launch {
verifMutex.withLock {
recentlyVerifiedDevices[deviceId] = System.currentTimeMillis()
recentlyVerifiedDevices[deviceId] = clock.epochMillis()
}
}
}
@ -217,7 +219,7 @@ internal class SecretShareManager @Inject constructor(
recentlyVerifiedDevices[deviceId]
} ?: return false
val age = System.currentTimeMillis() - verifTimestamp
val age = clock.epochMillis() - verifTimestamp
return age < SECRET_SHARE_WINDOW_DURATION
}

View File

@ -1108,7 +1108,7 @@ internal class RealmCryptoStore @Inject constructor(
}
private fun createUnknownTrail() = AuditTrail(
System.currentTimeMillis(),
clock.epochMillis(),
TrailType.Unknown,
IncomingKeyRequestInfo(
"",
@ -1187,7 +1187,7 @@ internal class RealmCryptoStore @Inject constructor(
this.requestedIndex = fromIndex
this.requestState = OutgoingRoomKeyRequestState.UNSENT
this.setRequestBody(requestBody)
this.creationTimeStamp = System.currentTimeMillis()
this.creationTimeStamp = clock.epochMillis()
}.toOutgoingKeyRequest()
} else {
request = existing
@ -1268,7 +1268,7 @@ internal class RealmCryptoStore @Inject constructor(
fromUser: String,
fromDevice: String) {
monarchy.writeAsync { realm ->
val now = System.currentTimeMillis()
val now = clock.epochMillis()
realm.createObject<AuditTrailEntity>().apply {
this.ageLocalTs = now
this.type = TrailType.IncomingKeyRequest.name
@ -1296,7 +1296,7 @@ internal class RealmCryptoStore @Inject constructor(
userId: String,
deviceId: String) {
monarchy.writeAsync { realm ->
val now = System.currentTimeMillis()
val now = clock.epochMillis()
realm.createObject<AuditTrailEntity>().apply {
this.ageLocalTs = now
this.type = TrailType.OutgoingKeyWithheld.name
@ -1346,7 +1346,7 @@ internal class RealmCryptoStore @Inject constructor(
incoming: Boolean
) {
monarchy.writeAsync { realm ->
val now = System.currentTimeMillis()
val now = clock.epochMillis()
realm.createObject<AuditTrailEntity>().apply {
this.ageLocalTs = now
this.type = if (incoming) TrailType.IncomingKeyForward.name else TrailType.OutgoingKeyForward.name
@ -1683,7 +1683,7 @@ internal class RealmCryptoStore @Inject constructor(
// Only keep one month history
val prevMonthTs = System.currentTimeMillis() - 4 * 7 * 24 * 60 * 60 * 1_000L
val prevMonthTs = clock.epochMillis() - 4 * 7 * 24 * 60 * 60 * 1_000L
realm.where<AuditTrailEntity>()
.lessThan(AuditTrailEntityFields.AGE_LOCAL_TS, prevMonthTs)
.findAll()