From 784d55c16c3af1282bca67602ab7129c15010da3 Mon Sep 17 00:00:00 2001 From: ganfra Date: Sun, 2 Jun 2019 20:34:19 +0200 Subject: [PATCH] Crypto: WIP cleaning --- matrix-sdk-android/build.gradle | 4 +- .../android/internal/crypto/CryptoManager.kt | 244 +++++++++--------- .../android/internal/crypto/CryptoModule.kt | 49 ++-- .../internal/crypto/DeviceListManager.kt | 122 ++++----- .../algorithms/megolm/MXMegolmDecryption.kt | 2 +- .../algorithms/megolm/MXMegolmEncryption.kt | 2 +- .../session/content/UploadContentWorker.kt | 6 +- .../session/room/send/EncryptEventWorker.kt | 8 +- .../session/room/send/SendEventWorker.kt | 35 ++- .../session/sync/CryptoSyncHandler.kt | 2 +- 10 files changed, 238 insertions(+), 236 deletions(-) diff --git a/matrix-sdk-android/build.gradle b/matrix-sdk-android/build.gradle index c9720fbd18..44aa2c386c 100644 --- a/matrix-sdk-android/build.gradle +++ b/matrix-sdk-android/build.gradle @@ -46,11 +46,11 @@ android { debug { // Set to true to log privacy or sensible data, such as token // TODO Set to false - buildConfigField "boolean", "LOG_PRIVATE_DATA", "true" + buildConfigField "boolean", "LOG_PRIVATE_DATA", "false" // Set to BODY instead of NONE to enable logging //TODO Revert BODY - buildConfigField "okhttp3.logging.HttpLoggingInterceptor.Level", "OKHTTP_LOGGING_LEVEL", "okhttp3.logging.HttpLoggingInterceptor.Level.BODY" + buildConfigField "okhttp3.logging.HttpLoggingInterceptor.Level", "OKHTTP_LOGGING_LEVEL", "okhttp3.logging.HttpLoggingInterceptor.Level.HEADERS" } release { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoManager.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoManager.kt index 8887afe91b..51da117567 100755 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoManager.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoManager.kt @@ -57,11 +57,8 @@ import im.vector.matrix.android.internal.crypto.model.rest.KeysUploadResponse import im.vector.matrix.android.internal.crypto.model.rest.RoomKeyRequestBody import im.vector.matrix.android.internal.crypto.repository.WarnOnUnknownDeviceRepository import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore -import im.vector.matrix.android.internal.crypto.tasks.ClaimOneTimeKeysForUsersDeviceTask import im.vector.matrix.android.internal.crypto.tasks.DeleteDeviceTask import im.vector.matrix.android.internal.crypto.tasks.GetDevicesTask -import im.vector.matrix.android.internal.crypto.tasks.GetKeyChangesTask -import im.vector.matrix.android.internal.crypto.tasks.SendToDeviceTask import im.vector.matrix.android.internal.crypto.tasks.SetDeviceNameTask import im.vector.matrix.android.internal.crypto.tasks.UploadKeysTask import im.vector.matrix.android.internal.crypto.verification.DefaultSasVerificationService @@ -80,74 +77,71 @@ import timber.log.Timber import java.util.* /** - * A `MXCrypto` class instance manages the end-to-end crypto for a MXSession instance. + * A `CryptoService` class instance manages the end-to-end crypto for a session. * * - * Messages posted by the user are automatically redirected to MXCrypto in order to be encrypted + * Messages posted by the user are automatically redirected to CryptoService in order to be encrypted * before sending. - * In the other hand, received events goes through MXCrypto for decrypting. - * MXCrypto maintains all necessary keys and their sharing with other devices required for the crypto. + * In the other hand, received events goes through CryptoService for decrypting. + * CryptoService maintains all necessary keys and their sharing with other devices required for the crypto. * Specially, it tracks all room membership changes events in order to do keys updates. */ internal class CryptoManager( // The credentials, - private val mCredentials: Credentials, - private val mMyDeviceInfoHolder: MyDeviceInfoHolder, + private val credentials: Credentials, + private val myDeviceInfoHolder: MyDeviceInfoHolder, // the crypto store - private val mCryptoStore: IMXCryptoStore, + private val cryptoStore: IMXCryptoStore, // Olm device - private val mOlmDevice: MXOlmDevice, + private val olmDevice: MXOlmDevice, // Set of parameters used to configure/customize the end-to-end crypto. - private val mCryptoConfig: MXCryptoConfig = MXCryptoConfig(), + private val cryptoConfig: MXCryptoConfig = MXCryptoConfig(), // Device list manager private val deviceListManager: DeviceListManager, // The key backup service. - private val mKeysBackup: KeysBackup, + private val keysBackup: KeysBackup, // - private val mObjectSigner: ObjectSigner, + private val objectSigner: ObjectSigner, // - private val mOneTimeKeysUploader: OneTimeKeysUploader, + private val oneTimeKeysUploader: OneTimeKeysUploader, // private val roomDecryptorProvider: RoomDecryptorProvider, // The SAS verification service. - private val mSasVerificationService: DefaultSasVerificationService, + private val sasVerificationService: DefaultSasVerificationService, // - private val mIncomingRoomKeyRequestManager: IncomingRoomKeyRequestManager, + private val incomingRoomKeyRequestManager: IncomingRoomKeyRequestManager, // - private val mOutgoingRoomKeyRequestManager: OutgoingRoomKeyRequestManager, + private val outgoingRoomKeyRequestManager: OutgoingRoomKeyRequestManager, // Olm Manager - private val mOlmManager: OlmManager, + private val olmManager: OlmManager, // Actions - private val mSetDeviceVerificationAction: SetDeviceVerificationAction, - private val mMegolmSessionDataImporter: MegolmSessionDataImporter, - private val mEnsureOlmSessionsForDevicesAction: EnsureOlmSessionsForDevicesAction, + private val setDeviceVerificationAction: SetDeviceVerificationAction, + private val megolmSessionDataImporter: MegolmSessionDataImporter, + private val ensureOlmSessionsForDevicesAction: EnsureOlmSessionsForDevicesAction, // Repository - private val mWarnOnUnknownDevicesRepository: WarnOnUnknownDeviceRepository, - private val mMXMegolmEncryptionFactory: MXMegolmEncryptionFactory, - private val mMXOlmEncryptionFactory: MXOlmEncryptionFactory, + private val warnOnUnknownDevicesRepository: WarnOnUnknownDeviceRepository, + private val megolmEncryptionFactory: MXMegolmEncryptionFactory, + private val olmEncryptionFactory: MXOlmEncryptionFactory, // Tasks - private val mClaimOneTimeKeysForUsersDeviceTask: ClaimOneTimeKeysForUsersDeviceTask, - private val mDeleteDeviceTask: DeleteDeviceTask, - private val mGetDevicesTask: GetDevicesTask, - private val mGetKeyChangesTask: GetKeyChangesTask, - private val mSendToDeviceTask: SendToDeviceTask, - private val mSetDeviceNameTask: SetDeviceNameTask, - private val mUploadKeysTask: UploadKeysTask, + private val deleteDeviceTask: DeleteDeviceTask, + private val getDevicesTask: GetDevicesTask, + private val setDeviceNameTask: SetDeviceNameTask, + private val uploadKeysTask: UploadKeysTask, private val loadRoomMembersTask: LoadRoomMembersTask, private val monarchy: Monarchy, private val coroutineDispatchers: MatrixCoroutineDispatchers, // TaskExecutor - private val mTaskExecutor: TaskExecutor + private val taskExecutor: TaskExecutor ) : CryptoService { // MXEncrypting instance for each room. - private val mRoomEncryptors: MutableMap = HashMap() + private val roomEncryptors: MutableMap = HashMap() // the encryption is starting - private var mIsStarting: Boolean = false + private var isStarting: Boolean = false // tell if the crypto is started - private var mIsStarted: Boolean = false + private var isStarted: Boolean = false // TODO //private val mNetworkListener = object : IMXNetworkEventListener { @@ -179,36 +173,36 @@ internal class CryptoManager( private val mInitializationCallbacks = ArrayList>() override fun setDeviceName(deviceId: String, deviceName: String, callback: MatrixCallback) { - mSetDeviceNameTask + setDeviceNameTask .configureWith(SetDeviceNameTask.Params(deviceId, deviceName)) .dispatchTo(callback) - .executeBy(mTaskExecutor) + .executeBy(taskExecutor) } override fun deleteDevice(deviceId: String, accountPassword: String, callback: MatrixCallback) { - mDeleteDeviceTask + deleteDeviceTask .configureWith(DeleteDeviceTask.Params(deviceId, accountPassword)) .dispatchTo(callback) - .executeBy(mTaskExecutor) + .executeBy(taskExecutor) } override fun getCryptoVersion(context: Context, longFormat: Boolean): String { - return if (longFormat) mOlmManager.getDetailedVersion(context) else mOlmManager.version + return if (longFormat) olmManager.getDetailedVersion(context) else olmManager.version } override fun getMyDevice(): MXDeviceInfo { - return mMyDeviceInfoHolder.myDevice + return myDeviceInfoHolder.myDevice } override fun getDevicesList(callback: MatrixCallback) { - mGetDevicesTask + getDevicesTask .configureWith(Unit) .dispatchTo(callback) - .executeBy(mTaskExecutor) + .executeBy(taskExecutor) } override fun inboundGroupSessionsCount(onlyBackedUp: Boolean): Int { - return mCryptoStore.inboundGroupSessionsCount(onlyBackedUp) + return cryptoStore.inboundGroupSessionsCount(onlyBackedUp) } /** @@ -218,7 +212,7 @@ internal class CryptoManager( * @return the tracking status */ override fun getDeviceTrackingStatus(userId: String): Int { - return mCryptoStore.getDeviceTrackingStatus(userId, DeviceListManager.TRACKING_STATUS_NOT_TRACKED) + return cryptoStore.getDeviceTrackingStatus(userId, DeviceListManager.TRACKING_STATUS_NOT_TRACKED) } /** @@ -227,7 +221,7 @@ internal class CryptoManager( * @return true if the crypto is started */ fun isStarted(): Boolean { - return mIsStarted + return isStarted } /** @@ -236,7 +230,7 @@ internal class CryptoManager( * @return true if the crypto is starting */ fun isStarting(): Boolean { - return mIsStarting + return isStarting } /** @@ -248,7 +242,7 @@ internal class CryptoManager( * @param isInitialSync true if it starts from an initial sync */ fun start(isInitialSync: Boolean) { - if (mIsStarting) { + if (isStarting) { return } @@ -261,16 +255,16 @@ internal class CryptoManager( // return //} - mIsStarting = true + isStarting = true // Open the store - mCryptoStore.open() + cryptoStore.open() uploadDeviceKeys(object : MatrixCallback { private fun onError() { Handler().postDelayed({ if (!isStarted()) { - mIsStarting = false + isStarting = false start(isInitialSync) } }, 1000) @@ -278,33 +272,33 @@ internal class CryptoManager( override fun onSuccess(data: KeysUploadResponse) { Timber.v("###########################################################") - Timber.v("uploadDeviceKeys done for " + mCredentials.userId) - Timber.v(" - device id : " + mCredentials.deviceId) - Timber.v(" - ed25519 : " + mOlmDevice.deviceEd25519Key) - Timber.v(" - curve25519 : " + mOlmDevice.deviceCurve25519Key) - Timber.v(" - oneTimeKeys: " + mOneTimeKeysUploader.mLastPublishedOneTimeKeys) + Timber.v("uploadDeviceKeys done for " + credentials.userId) + Timber.v(" - device id : " + credentials.deviceId) + Timber.v(" - ed25519 : " + olmDevice.deviceEd25519Key) + Timber.v(" - curve25519 : " + olmDevice.deviceCurve25519Key) + Timber.v(" - oneTimeKeys: " + oneTimeKeysUploader.mLastPublishedOneTimeKeys) Timber.v("") - mOneTimeKeysUploader.maybeUploadOneTimeKeys(object : MatrixCallback { + oneTimeKeysUploader.maybeUploadOneTimeKeys(object : MatrixCallback { override fun onSuccess(data: Unit) { // TODO //if (null != mNetworkConnectivityReceiver) { // mNetworkConnectivityReceiver!!.removeEventListener(mNetworkListener) //} - mIsStarting = false - mIsStarted = true + isStarting = false + isStarted = true - mOutgoingRoomKeyRequestManager.start() + outgoingRoomKeyRequestManager.start() - mKeysBackup.checkAndStartKeysBackup() + keysBackup.checkAndStartKeysBackup() if (isInitialSync) { // refresh the devices list for each known room members deviceListManager.invalidateAllDeviceLists() deviceListManager.refreshOutdatedDeviceLists() } else { - mIncomingRoomKeyRequestManager.processReceivedRoomKeyRequests() + incomingRoomKeyRequestManager.processReceivedRoomKeyRequests() } } @@ -326,58 +320,51 @@ internal class CryptoManager( * Close the crypto */ fun close() { - mOlmDevice.release() + olmDevice.release() - mCryptoStore.close() + cryptoStore.close() - mOutgoingRoomKeyRequestManager.stop() + outgoingRoomKeyRequestManager.stop() } override fun isCryptoEnabled(): Boolean { // TODO Check that this test is correct - return mOlmDevice != null + return olmDevice != null } /** * @return the Keys backup Service */ override fun getKeysBackupService(): KeysBackupService { - return mKeysBackup + return keysBackup } /** * @return the SasVerificationService */ override fun getSasVerificationService(): SasVerificationService { - return mSasVerificationService + return sasVerificationService } /** * A sync response has been received * * @param syncResponse the syncResponse - * @param fromToken the start sync token - * @param isCatchingUp true if there is a catch-up in progress. */ - fun onSyncCompleted(syncResponse: SyncResponse, fromToken: String?, isCatchingUp: Boolean) { - if (null != syncResponse.deviceLists) { + fun onSyncCompleted(syncResponse: SyncResponse) { + if (syncResponse.deviceLists != null) { deviceListManager.handleDeviceListsChanges(syncResponse.deviceLists.changed, syncResponse.deviceLists.left) } - - if (null != syncResponse.deviceOneTimeKeysCount) { + if (syncResponse.deviceOneTimeKeysCount != null) { val currentCount = syncResponse.deviceOneTimeKeysCount.signedCurve25519 ?: 0 - mOneTimeKeysUploader.updateOneTimeKeyCount(currentCount) + oneTimeKeysUploader.updateOneTimeKeyCount(currentCount) } if (isStarted()) { // Make sure we process to-device messages before generating new one-time-keys #2782 deviceListManager.refreshOutdatedDeviceLists() - } - - if (!isCatchingUp && isStarted()) { - mOneTimeKeysUploader.maybeUploadOneTimeKeys() - - mIncomingRoomKeyRequestManager.processReceivedRoomKeyRequests() + oneTimeKeysUploader.maybeUploadOneTimeKeys() + incomingRoomKeyRequestManager.processReceivedRoomKeyRequests() } } @@ -392,7 +379,7 @@ internal class CryptoManager( return if (!TextUtils.equals(algorithm, MXCRYPTO_ALGORITHM_MEGOLM) && !TextUtils.equals(algorithm, MXCRYPTO_ALGORITHM_OLM)) { // We only deal in olm keys null - } else mCryptoStore.deviceWithIdentityKey(senderKey) + } else cryptoStore.deviceWithIdentityKey(senderKey) } /** @@ -403,7 +390,7 @@ internal class CryptoManager( */ override fun getDeviceInfo(userId: String, deviceId: String?): MXDeviceInfo? { return if (!TextUtils.isEmpty(userId) && !TextUtils.isEmpty(deviceId)) { - mCryptoStore.getUserDevice(deviceId!!, userId) + cryptoStore.getUserDevice(deviceId!!, userId) } else { null } @@ -432,7 +419,7 @@ internal class CryptoManager( val userIds = devicesIdListByUserId.keys for (userId in userIds) { - val storedDeviceIDs = mCryptoStore.getUserDevices(userId) + val storedDeviceIDs = cryptoStore.getUserDevices(userId) // sanity checks if (null != storedDeviceIDs) { @@ -451,7 +438,7 @@ internal class CryptoManager( } if (isUpdated) { - mCryptoStore.storeUserDevices(userId, storedDeviceIDs) + cryptoStore.storeUserDevices(userId, storedDeviceIDs) } } } @@ -468,7 +455,7 @@ internal class CryptoManager( * @param userId the owner of the device */ override fun setDeviceVerification(verificationStatus: Int, deviceId: String, userId: String) { - mSetDeviceVerificationAction.handle(verificationStatus, deviceId, userId) + setDeviceVerificationAction.handle(verificationStatus, deviceId, userId) } /** @@ -484,7 +471,7 @@ internal class CryptoManager( private fun setEncryptionInRoom(roomId: String, algorithm: String?, inhibitDeviceQuery: Boolean, membersId: List): Boolean { // If we already have encryption in this room, we should ignore this event // (for now at least. Maybe we should alert the user somehow?) - val existingAlgorithm = mCryptoStore.getRoomAlgorithm(roomId) + val existingAlgorithm = cryptoStore.getRoomAlgorithm(roomId) if (!TextUtils.isEmpty(existingAlgorithm) && !TextUtils.equals(existingAlgorithm, algorithm)) { Timber.e("## setEncryptionInRoom() : Ignoring m.room.encryption event which requests a change of config in $roomId") @@ -498,15 +485,15 @@ internal class CryptoManager( return false } - mCryptoStore.storeRoomAlgorithm(roomId, algorithm!!) + cryptoStore.storeRoomAlgorithm(roomId, algorithm!!) val alg: IMXEncrypting = when (algorithm) { - MXCRYPTO_ALGORITHM_MEGOLM -> mMXMegolmEncryptionFactory.instantiate(roomId) - else -> mMXOlmEncryptionFactory.instantiate(roomId) + MXCRYPTO_ALGORITHM_MEGOLM -> megolmEncryptionFactory.instantiate(roomId) + else -> olmEncryptionFactory.instantiate(roomId) } - synchronized(mRoomEncryptors) { - mRoomEncryptors.put(roomId, alg) + synchronized(roomEncryptors) { + roomEncryptors.put(roomId, alg) } // if encryption was not previously enabled in this room, we will have been @@ -538,12 +525,12 @@ internal class CryptoManager( override fun isRoomEncrypted(roomId: String): Boolean { var res: Boolean - synchronized(mRoomEncryptors) { - res = mRoomEncryptors.containsKey(roomId) + synchronized(roomEncryptors) { + res = roomEncryptors.containsKey(roomId) } if (!res) { - res = !mCryptoStore.getRoomAlgorithm(roomId).isNullOrBlank() + res = !cryptoStore.getRoomAlgorithm(roomId).isNullOrBlank() } return res @@ -553,7 +540,7 @@ internal class CryptoManager( * @return the stored device keys for a user. */ override fun getUserDevices(userId: String): MutableList { - val map = mCryptoStore.getUserDevices(userId) + val map = cryptoStore.getUserDevices(userId) return if (null != map) ArrayList(map.values) else ArrayList() } @@ -568,15 +555,15 @@ internal class CryptoManager( */ fun ensureOlmSessionsForDevices(devicesByUser: Map>, callback: MatrixCallback>?) { - mEnsureOlmSessionsForDevicesAction.handle(devicesByUser, callback) + ensureOlmSessionsForDevicesAction.handle(devicesByUser, callback) } fun isEncryptionEnabledForInvitedUser(): Boolean { - return mCryptoConfig.mEnableEncryptionForInvitedMembers + return cryptoConfig.mEnableEncryptionForInvitedMembers } override fun getEncryptionAlgorithm(roomId: String): String? { - return mCryptoStore.getRoomAlgorithm(roomId) + return cryptoStore.getRoomAlgorithm(roomId) } /** @@ -588,7 +575,7 @@ internal class CryptoManager( * @return true if we should encrypt messages for invited users. */ override fun shouldEncryptForInvitedMembers(roomId: String): Boolean { - return mCryptoStore.shouldEncryptForInvitedMembers(roomId) + return cryptoStore.shouldEncryptForInvitedMembers(roomId) } /** @@ -612,15 +599,15 @@ internal class CryptoManager( } val userIds = getRoomUserIds(roomId) - var alg = synchronized(mRoomEncryptors) { - mRoomEncryptors[roomId] + var alg = synchronized(roomEncryptors) { + roomEncryptors[roomId] } if (null == alg) { val algorithm = getEncryptionAlgorithm(roomId) if (null != algorithm) { if (setEncryptionInRoom(roomId, algorithm, false, userIds)) { - synchronized(mRoomEncryptors) { - alg = mRoomEncryptors[roomId] + synchronized(roomEncryptors) { + alg = roomEncryptors[roomId] } } } @@ -706,7 +693,7 @@ internal class CryptoManager( * @param timelineId the timeline id */ fun resetReplayAttackCheckInTimeline(timelineId: String) { - mOlmDevice.resetReplayAttackCheckInTimeline(timelineId) + olmDevice.resetReplayAttackCheckInTimeline(timelineId) } /** @@ -718,7 +705,7 @@ internal class CryptoManager( if (event.getClearType() == EventType.ROOM_KEY || event.getClearType() == EventType.FORWARDED_ROOM_KEY) { onRoomKeyEvent(event) } else if (event.getClearType() == EventType.ROOM_KEY_REQUEST) { - mIncomingRoomKeyRequestManager.onRoomKeyRequestEvent(event) + incomingRoomKeyRequestManager.onRoomKeyRequestEvent(event) } } @@ -743,7 +730,7 @@ internal class CryptoManager( return } - alg.onRoomKeyEvent(event, mKeysBackup) + alg.onRoomKeyEvent(event, keysBackup) } /** @@ -759,7 +746,6 @@ internal class CryptoManager( .map { allLoaded -> val userIds = getRoomUserIds(roomId) setEncryptionInRoom(roomId, event.content!!["algorithm"] as String, true, userIds) - allLoaded } } } @@ -789,8 +775,8 @@ internal class CryptoManager( private fun onRoomMembershipEvent(roomId: String, event: Event) { val alg: IMXEncrypting? - synchronized(mRoomEncryptors) { - alg = mRoomEncryptors[roomId] + synchronized(roomEncryptors) { + alg = roomEncryptors[roomId] } if (null == alg) { @@ -805,7 +791,7 @@ internal class CryptoManager( deviceListManager.startTrackingDeviceList(Arrays.asList(userId)) } else if (membership == Membership.INVITE && shouldEncryptForInvitedMembers(roomId) - && mCryptoConfig.mEnableEncryptionForInvitedMembers) { + && cryptoConfig.mEnableEncryptionForInvitedMembers) { // track the deviceList for this invited user. // Caution: there's a big edge case here in that federated servers do not // know what other servers are in the room at the time they've been invited. @@ -820,7 +806,7 @@ internal class CryptoManager( val eventContent = event.content.toModel() eventContent?.historyVisibility?.let { - mCryptoStore.setShouldEncryptForInvitedMembers(roomId, it != RoomHistoryVisibility.JOINED) + cryptoStore.setShouldEncryptForInvitedMembers(roomId, it != RoomHistoryVisibility.JOINED) } } @@ -837,15 +823,15 @@ internal class CryptoManager( // Sign it val canonicalJson = MoshiProvider.getCanonicalJson(Map::class.java, getMyDevice().signalableJSONDictionary()) - getMyDevice().signatures = mObjectSigner.signObject(canonicalJson) + getMyDevice().signatures = objectSigner.signObject(canonicalJson) // For now, we set the device id explicitly, as we may not be using the // same one as used in login. - mUploadKeysTask + uploadKeysTask .configureWith(UploadKeysTask.Params(getMyDevice().toDeviceKeys(), null, getMyDevice().deviceId)) .executeOn(TaskThread.ENCRYPTION) .dispatchTo(callback) - .executeBy(mTaskExecutor) + .executeBy(taskExecutor) } /** @@ -870,7 +856,7 @@ internal class CryptoManager( val exportedSessions = ArrayList() - val inboundGroupSessions = mCryptoStore.getInboundGroupSessions() + val inboundGroupSessions = cryptoStore.getInboundGroupSessions() for (session in inboundGroupSessions) { val megolmSessionData = session.exportKeys() @@ -941,7 +927,7 @@ internal class CryptoManager( Timber.v("## importRoomKeys : JSON parsing " + (t2 - t1) + " ms") - mMegolmSessionDataImporter.handle(importedSessions, true, progressListener, callback) + megolmSessionDataImporter.handle(importedSessions, true, progressListener, callback) } /** @@ -950,7 +936,7 @@ internal class CryptoManager( * @param warn true to warn when some unknown devices are detected. */ override fun setWarnOnUnknownDevices(warn: Boolean) { - mWarnOnUnknownDevicesRepository.setWarnOnUnknownDevices(warn) + warnOnUnknownDevicesRepository.setWarnOnUnknownDevices(warn) } /** @@ -992,7 +978,7 @@ internal class CryptoManager( * @param block true to unilaterally blacklist all */ override fun setGlobalBlacklistUnverifiedDevices(block: Boolean) { - mCryptoStore.setGlobalBlacklistUnverifiedDevices(block) + cryptoStore.setGlobalBlacklistUnverifiedDevices(block) } /** @@ -1003,7 +989,7 @@ internal class CryptoManager( * @return true to unilaterally blacklist all unverified devices. */ override fun getGlobalBlacklistUnverifiedDevices(): Boolean { - return mCryptoStore.getGlobalBlacklistUnverifiedDevices() + return cryptoStore.getGlobalBlacklistUnverifiedDevices() } /** @@ -1017,7 +1003,7 @@ internal class CryptoManager( // TODO add this info in CryptoRoomEntity? override fun isRoomBlacklistUnverifiedDevices(roomId: String?): Boolean { return if (null != roomId) { - mCryptoStore.getRoomsListBlacklistUnverifiedDevices().contains(roomId) + cryptoStore.getRoomsListBlacklistUnverifiedDevices().contains(roomId) } else { false } @@ -1030,7 +1016,7 @@ internal class CryptoManager( * @param add true to add the room id to the list, false to remove it. */ private fun setRoomBlacklistUnverifiedDevices(roomId: String, add: Boolean) { - val roomIds = mCryptoStore.getRoomsListBlacklistUnverifiedDevices().toMutableList() + val roomIds = cryptoStore.getRoomsListBlacklistUnverifiedDevices().toMutableList() if (add) { if (!roomIds.contains(roomId)) { @@ -1040,7 +1026,7 @@ internal class CryptoManager( roomIds.remove(roomId) } - mCryptoStore.setRoomsListBlacklistUnverifiedDevices(roomIds) + cryptoStore.setRoomsListBlacklistUnverifiedDevices(roomIds) } @@ -1069,7 +1055,7 @@ internal class CryptoManager( * @param requestBody requestBody */ override fun cancelRoomKeyRequest(requestBody: RoomKeyRequestBody) { - mOutgoingRoomKeyRequestManager.cancelRoomKeyRequest(requestBody) + outgoingRoomKeyRequestManager.cancelRoomKeyRequest(requestBody) } /** @@ -1091,7 +1077,7 @@ internal class CryptoManager( requestBody.senderKey = senderKey requestBody.sessionId = sessionId - mOutgoingRoomKeyRequestManager.resendRoomKeyRequest(requestBody) + outgoingRoomKeyRequestManager.resendRoomKeyRequest(requestBody) } /** @@ -1100,7 +1086,7 @@ internal class CryptoManager( * @param listener listener */ override fun addRoomKeysRequestListener(listener: RoomKeysRequestListener) { - mIncomingRoomKeyRequestManager.addRoomKeysRequestListener(listener) + incomingRoomKeyRequestManager.addRoomKeysRequestListener(listener) } /** @@ -1109,7 +1095,7 @@ internal class CryptoManager( * @param listener listener */ fun removeRoomKeysRequestListener(listener: RoomKeysRequestListener) { - mIncomingRoomKeyRequestManager.removeRoomKeysRequestListener(listener) + incomingRoomKeyRequestManager.removeRoomKeysRequestListener(listener) } /** @@ -1141,7 +1127,7 @@ internal class CryptoManager( * ========================================================================================== */ override fun toString(): String { - return "CryptoManager of " + mCredentials.userId + " (" + mCredentials.deviceId + ")" + return "CryptoManager of " + credentials.userId + " (" + credentials.deviceId + ")" } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoModule.kt index 3b85ca5221..b90c96892a 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoModule.kt @@ -171,38 +171,35 @@ internal class CryptoModule { // CryptoManager scope(DefaultSession.SCOPE) { CryptoManager( - mCredentials = get(), - mMyDeviceInfoHolder = get(), - mCryptoStore = get(), - mOlmDevice = get(), - mCryptoConfig = get(), + credentials = get(), + myDeviceInfoHolder = get(), + cryptoStore = get(), + olmDevice = get(), + cryptoConfig = get(), deviceListManager = get(), - mKeysBackup = get(), - mObjectSigner = get(), - mOneTimeKeysUploader = get(), + keysBackup = get(), + objectSigner = get(), + oneTimeKeysUploader = get(), roomDecryptorProvider = get(), - mSasVerificationService = get(), - mIncomingRoomKeyRequestManager = get(), - mOutgoingRoomKeyRequestManager = get(), - mOlmManager = get(), - mSetDeviceVerificationAction = get(), - mMegolmSessionDataImporter = get(), - mEnsureOlmSessionsForDevicesAction = get(), - mWarnOnUnknownDevicesRepository = get(), - mMXMegolmEncryptionFactory = get(), - mMXOlmEncryptionFactory = get(), - mClaimOneTimeKeysForUsersDeviceTask = get(), + sasVerificationService = get(), + incomingRoomKeyRequestManager = get(), + outgoingRoomKeyRequestManager = get(), + olmManager = get(), + setDeviceVerificationAction = get(), + megolmSessionDataImporter = get(), + ensureOlmSessionsForDevicesAction = get(), + warnOnUnknownDevicesRepository = get(), + megolmEncryptionFactory = get(), + olmEncryptionFactory = get(), // Tasks - mDeleteDeviceTask = get(), - mGetDevicesTask = get(), - mGetKeyChangesTask = get(), - mSendToDeviceTask = get(), - mSetDeviceNameTask = get(), - mUploadKeysTask = get(), + deleteDeviceTask = get(), + getDevicesTask = get(), + setDeviceNameTask = get(), + uploadKeysTask = get(), loadRoomMembersTask = get(), monarchy = get(), coroutineDispatchers = get(), - mTaskExecutor = get() + taskExecutor = get() ) } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/DeviceListManager.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/DeviceListManager.kt index 0e44683653..f584c97a44 100755 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/DeviceListManager.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/DeviceListManager.kt @@ -33,27 +33,27 @@ import timber.log.Timber import java.util.* // Legacy name: MXDeviceList -internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, - private val mOlmDevice: MXOlmDevice, - private val mSyncTokenStore: SyncTokenStore, - private val mCredentials: Credentials, - private val mDownloadKeysForUsersTask: DownloadKeysForUsersTask, - private val mTaskExecutor: TaskExecutor) { +internal class DeviceListManager(private val cryptoStore: IMXCryptoStore, + private val olmDevice: MXOlmDevice, + private val syncTokenStore: SyncTokenStore, + private val credentials: Credentials, + private val downloadKeysForUsersTask: DownloadKeysForUsersTask, + private val taskExecutor: TaskExecutor) { // keys in progress - private val mUserKeyDownloadsInProgress = HashSet() + private val userKeyDownloadsInProgress = HashSet() // HS not ready for retry - private val mNotReadyToRetryHS = HashSet() + private val notReadyToRetryHS = HashSet() // indexed by UserId - private val mPendingDownloadKeysRequestToken = HashMap() + private val pendingDownloadKeysRequestToken = HashMap() // pending queues list - private val mDownloadKeysQueues = ArrayList() + private val downloadKeysQueues = ArrayList() // tells if there is a download keys request in progress - private var mIsDownloadingKeys = false + private var isDownloadingKeys = false /** * Creator @@ -78,7 +78,7 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, init { var isUpdated = false - val deviceTrackingStatuses = mCryptoStore.getDeviceTrackingStatuses().toMutableMap() + val deviceTrackingStatuses = cryptoStore.getDeviceTrackingStatuses().toMutableMap() for (userId in deviceTrackingStatuses.keys) { val status = deviceTrackingStatuses[userId]!! @@ -90,7 +90,7 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, } if (isUpdated) { - mCryptoStore.saveDeviceTrackingStatuses(deviceTrackingStatuses) + cryptoStore.saveDeviceTrackingStatuses(deviceTrackingStatuses) } } @@ -105,8 +105,8 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, if (!TextUtils.isEmpty(userId) && userId.contains(":")) { try { - synchronized(mNotReadyToRetryHS) { - res = !mNotReadyToRetryHS.contains(userId.substring(userId.lastIndexOf(":") + 1)) + synchronized(notReadyToRetryHS) { + res = !notReadyToRetryHS.contains(userId.substring(userId.lastIndexOf(":") + 1)) } } catch (e: Exception) { Timber.e(e, "## canRetryKeysDownload() failed") @@ -138,15 +138,15 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, } } - synchronized(mUserKeyDownloadsInProgress) { - filteredUserIds.removeAll(mUserKeyDownloadsInProgress) - mUserKeyDownloadsInProgress.addAll(userIds) + synchronized(userKeyDownloadsInProgress) { + filteredUserIds.removeAll(userKeyDownloadsInProgress) + userKeyDownloadsInProgress.addAll(userIds) // got some email addresses instead of matrix ids - mUserKeyDownloadsInProgress.removeAll(invalidUserIds) + userKeyDownloadsInProgress.removeAll(invalidUserIds) userIds.removeAll(invalidUserIds) } - mDownloadKeysQueues.add(DownloadKeysPromise(userIds, callback)) + downloadKeysQueues.add(DownloadKeysPromise(userIds, callback)) return filteredUserIds } else { @@ -158,8 +158,8 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, * Clear the unavailable server lists */ private fun clearUnavailableServersList() { - synchronized(mNotReadyToRetryHS) { - mNotReadyToRetryHS.clear() + synchronized(notReadyToRetryHS) { + notReadyToRetryHS.clear() } } @@ -172,7 +172,7 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, fun startTrackingDeviceList(userIds: List?) { if (null != userIds) { var isUpdated = false - val deviceTrackingStatuses = mCryptoStore.getDeviceTrackingStatuses().toMutableMap() + val deviceTrackingStatuses = cryptoStore.getDeviceTrackingStatuses().toMutableMap() for (userId in userIds) { if (!deviceTrackingStatuses.containsKey(userId) || TRACKING_STATUS_NOT_TRACKED == deviceTrackingStatuses[userId]) { @@ -183,7 +183,7 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, } if (isUpdated) { - mCryptoStore.saveDeviceTrackingStatuses(deviceTrackingStatuses) + cryptoStore.saveDeviceTrackingStatuses(deviceTrackingStatuses) } } } @@ -196,7 +196,7 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, */ fun handleDeviceListsChanges(changed: List?, left: List?) { var isUpdated = false - val deviceTrackingStatuses = mCryptoStore.getDeviceTrackingStatuses().toMutableMap() + val deviceTrackingStatuses = cryptoStore.getDeviceTrackingStatuses().toMutableMap() if (changed?.isNotEmpty() == true) { clearUnavailableServersList() @@ -223,7 +223,7 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, } if (isUpdated) { - mCryptoStore.saveDeviceTrackingStatuses(deviceTrackingStatuses) + cryptoStore.saveDeviceTrackingStatuses(deviceTrackingStatuses) } } @@ -232,7 +232,7 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, * + update */ fun invalidateAllDeviceLists() { - handleDeviceListsChanges(ArrayList(mCryptoStore.getDeviceTrackingStatuses().keys), null) + handleDeviceListsChanges(ArrayList(cryptoStore.getDeviceTrackingStatuses().keys), null) } /** @@ -242,19 +242,19 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, */ private fun onKeysDownloadFailed(userIds: List?) { if (null != userIds) { - synchronized(mUserKeyDownloadsInProgress) { - val deviceTrackingStatuses = mCryptoStore.getDeviceTrackingStatuses().toMutableMap() + synchronized(userKeyDownloadsInProgress) { + val deviceTrackingStatuses = cryptoStore.getDeviceTrackingStatuses().toMutableMap() for (userId in userIds) { - mUserKeyDownloadsInProgress.remove(userId) + userKeyDownloadsInProgress.remove(userId) deviceTrackingStatuses.put(userId, TRACKING_STATUS_PENDING_DOWNLOAD) } - mCryptoStore.saveDeviceTrackingStatuses(deviceTrackingStatuses) + cryptoStore.saveDeviceTrackingStatuses(deviceTrackingStatuses) } } - mIsDownloadingKeys = false + isDownloadingKeys = false } /** @@ -281,21 +281,21 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, } if (statusCode == 503) { - synchronized(mNotReadyToRetryHS) { - mNotReadyToRetryHS.add(k) + synchronized(notReadyToRetryHS) { + notReadyToRetryHS.add(k) } } } } } - val deviceTrackingStatuses = mCryptoStore.getDeviceTrackingStatuses().toMutableMap() + val deviceTrackingStatuses = cryptoStore.getDeviceTrackingStatuses().toMutableMap() if (null != userIds) { - if (mDownloadKeysQueues.size > 0) { + if (downloadKeysQueues.size > 0) { val promisesToRemove = ArrayList() - for (promise in mDownloadKeysQueues) { + for (promise in downloadKeysQueues) { promise.mPendingUserIdsList.removeAll(userIds) if (promise.mPendingUserIdsList.size == 0) { @@ -303,7 +303,7 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, val usersDevicesInfoMap = MXUsersDevicesMap() for (userId in promise.mUserIdsList) { - val devices = mCryptoStore.getUserDevices(userId) + val devices = cryptoStore.getUserDevices(userId) if (null == devices) { if (canRetryKeysDownload(userId)) { deviceTrackingStatuses.put(userId, TRACKING_STATUS_PENDING_DOWNLOAD) @@ -336,17 +336,17 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, promisesToRemove.add(promise) } } - mDownloadKeysQueues.removeAll(promisesToRemove) + downloadKeysQueues.removeAll(promisesToRemove) } for (userId in userIds) { - mUserKeyDownloadsInProgress.remove(userId) + userKeyDownloadsInProgress.remove(userId) } - mCryptoStore.saveDeviceTrackingStatuses(deviceTrackingStatuses) + cryptoStore.saveDeviceTrackingStatuses(deviceTrackingStatuses) } - mIsDownloadingKeys = false + isDownloadingKeys = false } /** @@ -372,14 +372,14 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, downloadUsers.addAll(userIds) } else { for (userId in userIds) { - val status = mCryptoStore.getDeviceTrackingStatus(userId, TRACKING_STATUS_NOT_TRACKED) + val status = cryptoStore.getDeviceTrackingStatus(userId, TRACKING_STATUS_NOT_TRACKED) // downloading keys ->the keys download won't be triggered twice but the callback requires the dedicated keys // not yet retrieved - if (mUserKeyDownloadsInProgress.contains(userId) || TRACKING_STATUS_UP_TO_DATE != status && TRACKING_STATUS_UNREACHABLE_SERVER != status) { + if (userKeyDownloadsInProgress.contains(userId) || TRACKING_STATUS_UP_TO_DATE != status && TRACKING_STATUS_UNREACHABLE_SERVER != status) { downloadUsers.add(userId) } else { - val devices = mCryptoStore.getUserDevices(userId) + val devices = cryptoStore.getUserDevices(userId) // should always be true if (null != devices) { @@ -444,7 +444,7 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, // return //} - mIsDownloadingKeys = true + isDownloadingKeys = true // track the race condition while sending requests // we defines a tag for each request @@ -452,11 +452,11 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, val downloadToken = filteredUsers.hashCode().toString() + " " + System.currentTimeMillis() for (userId in filteredUsers) { - mPendingDownloadKeysRequestToken[userId] = downloadToken + pendingDownloadKeysRequestToken[userId] = downloadToken } - mDownloadKeysForUsersTask - .configureWith(DownloadKeysForUsersTask.Params(filteredUsers, mSyncTokenStore.getLastToken())) + downloadKeysForUsersTask + .configureWith(DownloadKeysForUsersTask.Params(filteredUsers, syncTokenStore.getLastToken())) .dispatchTo(object : MatrixCallback { override fun onSuccess(data: KeysQueryResponse) { CryptoAsyncHelper.getEncryptBackgroundHandler().post { @@ -465,7 +465,7 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, for (userId in userIdsList) { // test if the response is the latest request one - if (!TextUtils.equals(mPendingDownloadKeysRequestToken[userId], downloadToken)) { + if (!TextUtils.equals(pendingDownloadKeysRequestToken[userId], downloadToken)) { Timber.e("## doKeyDownloadForUsers() : Another update in the queue for " + userId + " not marking up-to-date") filteredUsers.remove(userId) @@ -486,12 +486,12 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, //} // Get the potential previously store device keys for this device - val previouslyStoredDeviceKeys = mCryptoStore.getUserDevice(deviceId, userId) + val previouslyStoredDeviceKeys = cryptoStore.getUserDevice(deviceId, userId) val deviceInfo = mutableDevices[deviceId] // in some race conditions (like unit tests) // the self device must be seen as verified - if (TextUtils.equals(deviceInfo!!.deviceId, mCredentials.deviceId) && TextUtils.equals(userId, mCredentials.userId)) { + if (TextUtils.equals(deviceInfo!!.deviceId, credentials.deviceId) && TextUtils.equals(userId, credentials.userId)) { deviceInfo.mVerified = MXDeviceInfo.DEVICE_VERIFICATION_VERIFIED } @@ -514,11 +514,11 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, // Update the store // Note that devices which aren't in the response will be removed from the stores - mCryptoStore.storeUserDevices(userId, mutableDevices) + cryptoStore.storeUserDevices(userId, mutableDevices) } // the response is the latest request one - mPendingDownloadKeysRequestToken.remove(userId) + pendingDownloadKeysRequestToken.remove(userId) } } @@ -532,12 +532,12 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, // test if the response is the latest request one for (userId in userIdsList) { - if (!TextUtils.equals(mPendingDownloadKeysRequestToken[userId], downloadToken)) { + if (!TextUtils.equals(pendingDownloadKeysRequestToken[userId], downloadToken)) { Timber.e("## doKeyDownloadForUsers() : Another update in the queue for $userId not marking up-to-date") filteredUsers.remove(userId) } else { // the response is the latest request one - mPendingDownloadKeysRequestToken.remove(userId) + pendingDownloadKeysRequestToken.remove(userId) } } @@ -553,7 +553,7 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, callback?.onFailure(failure) } }) - .executeBy(mTaskExecutor) + .executeBy(taskExecutor) } /** @@ -619,7 +619,7 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, var errorMessage: String? = null try { - mOlmDevice.verifySignature(signKey, deviceKeys.signalableJSONDictionary(), signature) + olmDevice.verifySignature(signKey, deviceKeys.signalableJSONDictionary(), signature) isVerified = true } catch (e: Exception) { errorMessage = e.message @@ -658,7 +658,7 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, fun refreshOutdatedDeviceLists() { val users = ArrayList() - val deviceTrackingStatuses = mCryptoStore.getDeviceTrackingStatuses().toMutableMap() + val deviceTrackingStatuses = cryptoStore.getDeviceTrackingStatuses().toMutableMap() for (userId in deviceTrackingStatuses.keys) { if (TRACKING_STATUS_PENDING_DOWNLOAD == deviceTrackingStatuses[userId]) { @@ -670,7 +670,7 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, return } - if (mIsDownloadingKeys) { + if (isDownloadingKeys) { // request already in progress - do nothing. (We will automatically // make another request if there are more users with outdated // device lists when the current request completes). @@ -686,7 +686,7 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore, } } - mCryptoStore.saveDeviceTrackingStatuses(deviceTrackingStatuses) + cryptoStore.saveDeviceTrackingStatuses(deviceTrackingStatuses) doKeyDownloadForUsers(users, object : MatrixCallback> { override fun onSuccess(data: MXUsersDevicesMap) { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmDecryption.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmDecryption.kt index 92776d8b61..c2b8af1737 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmDecryption.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmDecryption.kt @@ -67,9 +67,9 @@ internal class MXMegolmDecryption(private val mCredentials: Credentials, } @Throws(MXDecryptionException::class) + private fun decryptEvent(event: Event, timeline: String, requestKeysOnFail: Boolean): MXEventDecryptionResult? { val encryptedEventContent = event.content.toModel()!! - if (TextUtils.isEmpty(encryptedEventContent.senderKey) || TextUtils.isEmpty(encryptedEventContent.sessionId) || TextUtils.isEmpty(encryptedEventContent.ciphertext)) { throw MXDecryptionException(MXCryptoError(MXCryptoError.MISSING_FIELDS_ERROR_CODE, MXCryptoError.UNABLE_TO_DECRYPT, MXCryptoError.MISSING_FIELDS_REASON)) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmEncryption.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmEncryption.kt index 5d76ed73a1..ee5133ba56 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmEncryption.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmEncryption.kt @@ -499,7 +499,7 @@ internal class MXMegolmEncryption( CryptoAsyncHelper.getUiHandler().post { // Check if any of these devices are not yet known to the user. // if so, warn the user so they can verify or ignore. - if (0 != unknownDevices.map.size) { + if (unknownDevices.map.isNotEmpty()) { callback.onFailure(Failure.CryptoError(MXCryptoError(MXCryptoError.UNKNOWN_DEVICES_CODE, MXCryptoError.UNABLE_TO_ENCRYPT, MXCryptoError.UNKNOWN_DEVICES_REASON, unknownDevices))) } else { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/UploadContentWorker.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/UploadContentWorker.kt index 4eeb124de9..d3d1ea9ee7 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/UploadContentWorker.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/UploadContentWorker.kt @@ -54,9 +54,9 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) override suspend fun doWork(): Result { val params = WorkerParamsFactory.fromData(inputData) - ?: return Result.failure() + ?: return Result.success() - val eventId = params.event.eventId ?: return Result.failure() + val eventId = params.event.eventId ?: return Result.success() val attachment = params.attachment val thumbnailData = ThumbnailExtractor.extractThumbnail(params.attachment) @@ -96,7 +96,7 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) private fun handleFailure(params: Params): Result { contentUploadProgressTracker.setFailure(params.event.eventId!!) - return Result.failure() + return Result.success() } private fun handleSuccess(params: Params, diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/EncryptEventWorker.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/EncryptEventWorker.kt index e059abf726..12185a071e 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/EncryptEventWorker.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/EncryptEventWorker.kt @@ -44,11 +44,11 @@ internal class EncryptEventWorker(context: Context, params: WorkerParameters) override fun doWork(): Result { val params = WorkerParamsFactory.fromData(inputData) - ?: return Result.failure() + ?: return Result.success() val localEvent = params.event if (localEvent.eventId == null) { - return Result.failure() + return Result.success() } // TODO Better async handling @@ -78,7 +78,7 @@ internal class EncryptEventWorker(context: Context, params: WorkerParameters) val safeResult = result // TODO Update local echo return if (error != null) { - Result.failure() // TODO Pass error!!) + Result.success() // TODO Pass error!!) } else if (safeResult != null) { val encryptedEvent = localEvent.copy( type = safeResult.mEventType, @@ -88,7 +88,7 @@ internal class EncryptEventWorker(context: Context, params: WorkerParameters) Result.success(WorkerParamsFactory.toData(nextWorkerParams)) } else { - Result.failure() + Result.success() } } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/SendEventWorker.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/SendEventWorker.kt index 1f49bb666a..f595b8dbbb 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/SendEventWorker.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/SendEventWorker.kt @@ -20,11 +20,20 @@ import android.content.Context import androidx.work.Worker import androidx.work.WorkerParameters import com.squareup.moshi.JsonClass +import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.api.session.events.model.Event +import im.vector.matrix.android.api.session.room.send.SendState +import im.vector.matrix.android.internal.database.helper.addSendingEvent +import im.vector.matrix.android.internal.database.model.ChunkEntity +import im.vector.matrix.android.internal.database.model.RoomEntity +import im.vector.matrix.android.internal.database.query.find +import im.vector.matrix.android.internal.database.query.findLastLiveChunkFromRoom +import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.di.MatrixKoinComponent import im.vector.matrix.android.internal.network.executeRequest import im.vector.matrix.android.internal.session.room.RoomAPI import im.vector.matrix.android.internal.util.WorkerParamsFactory +import im.vector.matrix.android.internal.util.tryTransactionAsync import org.koin.standalone.inject internal class SendEventWorker(context: Context, params: WorkerParameters) @@ -38,25 +47,35 @@ internal class SendEventWorker(context: Context, params: WorkerParameters) ) private val roomAPI by inject() + private val monarchy by inject() override fun doWork(): Result { val params = WorkerParamsFactory.fromData(inputData) - ?: return Result.failure() + ?: return Result.success() - val localEvent = params.event - if (localEvent.eventId == null) { - return Result.failure() + val event = params.event + if (event.eventId == null) { + return Result.success() } val result = executeRequest { apiCall = roomAPI.send( - localEvent.eventId, + event.eventId, params.roomId, - localEvent.type, - localEvent.content + event.type, + event.content ) } - return result.fold({ Result.failure() }, { Result.success() }) + return result.fold( + { + //TODO export that in a localEchoRepository + monarchy.tryTransactionAsync { realm -> + val roomEntity = RoomEntity.where(realm, roomId = params.roomId).findFirst() + val eventEntity = roomEntity?.sendingTimelineEvents?.find(event.eventId) + eventEntity?.sendState = SendState.UNSENT + } + Result.success() } + , { Result.success() }) } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/CryptoSyncHandler.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/CryptoSyncHandler.kt index c9f9235b8f..e24f0e0bb6 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/CryptoSyncHandler.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/CryptoSyncHandler.kt @@ -49,7 +49,7 @@ internal class CryptoSyncHandler(private val cryptoManager: CryptoManager, } fun onSyncCompleted(syncResponse: SyncResponse, fromToken: String?, catchingUp: Boolean) { - cryptoManager.onSyncCompleted(syncResponse, fromToken, catchingUp) + cryptoManager.onSyncCompleted(syncResponse) }