Skip to content

Commit

Permalink
Replace matrix callbacks by coroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
yostyle committed Nov 10, 2021
1 parent b6c25cf commit 69b56ed
Show file tree
Hide file tree
Showing 22 changed files with 324 additions and 149 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
/*
* Copyright 2020 New Vector Ltd - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Copyright 2021 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.matrix.android.sdk.api.session.contentscanner
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
/*
* Copyright 2020 New Vector Ltd - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Copyright 2021 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.matrix.android.sdk.api.session.contentscanner

import androidx.lifecycle.LiveData
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.util.Optional
import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt

Expand All @@ -20,16 +29,14 @@ interface ContentScannerService {
* Get the current public curve25519 key that the AV server is advertising.
* @param callback on success callback containing the server public key
*/
fun getServerPublicKey(forceDownload: Boolean = false, callback: MatrixCallback<String?>)
suspend fun getServerPublicKey(forceDownload: Boolean = false): String?

fun getScanResultForAttachment(mxcUrl: String, callback: MatrixCallback<ScanStatusInfo>)
fun getScanResultForAttachment(mxcUrl: String, fileInfo: ElementToDecrypt, callback: MatrixCallback<ScanStatusInfo>)
suspend fun getScanResultForAttachment(mxcUrl: String, fileInfo: ElementToDecrypt? = null): ScanStatusInfo

fun setScannerUrl(url: String?)

fun enableScanner(enabled: Boolean)
fun isScannerEnabled(): Boolean
fun getLiveStatusForFile(mxcUrl: String, fetchIfNeeded: Boolean = true): LiveData<Optional<ScanStatusInfo>>
fun getLiveStatusForEncryptedFile(mxcUrl: String, fileInfo: ElementToDecrypt, fetchIfNeeded: Boolean = true): LiveData<Optional<ScanStatusInfo>>
fun getLiveStatusForFile(mxcUrl: String, fetchIfNeeded: Boolean = true, fileInfo: ElementToDecrypt? = null): LiveData<Optional<ScanStatusInfo>>
fun getCachedScanResultForFile(mxcUrl: String): ScanStatusInfo?
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
/*
* Copyright 2020 New Vector Ltd - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Copyright 2021 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.matrix.android.sdk.api.session.contentscanner
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
/*
* Copyright 2020 New Vector Ltd - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Copyright 2021 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.matrix.android.sdk.internal.session.contentscanner
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
/*
* Copyright 2020 New Vector Ltd - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Copyright 2021 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.matrix.android.sdk.internal.session.contentscanner
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
/*
* Copyright 2020 New Vector Ltd - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Copyright 2021 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.matrix.android.sdk.internal.session.contentscanner
Expand Down Expand Up @@ -55,7 +65,7 @@ internal abstract class ContentScannerModule {
}

@Binds
abstract fun bindContentScannerService(service: DefaultContentScannerService): ContentScannerService
abstract fun bindContentScannerService(service: DisabledContentScannerService): ContentScannerService

@Binds
abstract fun bindContentScannerStore(store: RealmContentScannerStore): ContentScannerStore
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
/*
* Copyright 2020 New Vector Ltd - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Copyright 2021 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.matrix.android.sdk.internal.session.contentscanner
Expand All @@ -10,9 +20,6 @@ import androidx.lifecycle.LiveData
import dagger.Lazy
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
import org.matrix.android.sdk.api.NoOpMatrixCallback
import org.matrix.android.sdk.api.session.contentscanner.ContentScannerService
import org.matrix.android.sdk.api.session.contentscanner.ScanState
import org.matrix.android.sdk.api.session.contentscanner.ScanStatusInfo
Expand All @@ -26,8 +33,6 @@ import org.matrix.android.sdk.internal.session.contentscanner.tasks.GetServerPub
import org.matrix.android.sdk.internal.session.contentscanner.tasks.ScanEncryptedTask
import org.matrix.android.sdk.internal.session.contentscanner.tasks.ScanMediaTask
import org.matrix.android.sdk.internal.task.TaskExecutor
import org.matrix.android.sdk.internal.task.launchToCallback
import org.matrix.android.sdk.internal.util.awaitCallback
import timber.log.Timber
import javax.inject.Inject

Expand All @@ -38,12 +43,10 @@ internal class DefaultContentScannerService @Inject constructor(
private val okHttpClient: Lazy<OkHttpClient>,
private val contentScannerApiProvider: ContentScannerApiProvider,
private val contentScannerStore: ContentScannerStore,
// private val sessionParams: SessionParams,
private val getServerPublicKeyTask: GetServerPublicKeyTask,
private val scanEncryptedTask: ScanEncryptedTask,
private val scanMediaTask: ScanMediaTask,
private val taskExecutor: TaskExecutor,
private val coroutineDispatchers: MatrixCoroutineDispatchers
private val taskExecutor: TaskExecutor
) : ContentScannerService {

// Cache public key in memory
Expand All @@ -54,52 +57,34 @@ internal class DefaultContentScannerService @Inject constructor(
return contentScannerStore.getScannerUrl()
}

override fun getServerPublicKey(forceDownload: Boolean, callback: MatrixCallback<String?>) {
val api = contentScannerApiProvider.contentScannerApi ?: return Unit.also {
callback.onFailure(IllegalArgumentException("No content scanner defined"))
}
override suspend fun getServerPublicKey(forceDownload: Boolean): String? {
val api = contentScannerApiProvider.contentScannerApi ?: throw IllegalArgumentException("No content scanner define")

if (!forceDownload && serverPublicKey != null) {
callback.onSuccess(serverPublicKey)
return
return serverPublicKey
}
taskExecutor.executorScope.launchToCallback(coroutineDispatchers.io, callback) {
getServerPublicKeyTask.execute(GetServerPublicKeyTask.Params(api)).also {
serverPublicKey = it
}

return getServerPublicKeyTask.execute(GetServerPublicKeyTask.Params(api)).also {
serverPublicKey = it
}
}

override fun getScanResultForAttachment(mxcUrl: String, fileInfo: ElementToDecrypt, callback: MatrixCallback<ScanStatusInfo>) {
taskExecutor.executorScope.launchToCallback(coroutineDispatchers.io, callback) {
val serverPublicKey = serverPublicKey ?: awaitCallback<String?> {
getServerPublicKey(false, it)
}

val result = scanEncryptedTask.execute(ScanEncryptedTask.Params(
override suspend fun getScanResultForAttachment(mxcUrl: String, fileInfo: ElementToDecrypt?): ScanStatusInfo {
val result = if (fileInfo != null) {
scanEncryptedTask.execute(ScanEncryptedTask.Params(
mxcUrl = mxcUrl,
publicServerKey = serverPublicKey,
publicServerKey = getServerPublicKey(false),
encryptedInfo = fileInfo
))

ScanStatusInfo(
state = if (result.clean) ScanState.TRUSTED else ScanState.INFECTED,
humanReadableMessage = result.info,
scanDateTimestamp = System.currentTimeMillis()
)
} else {
scanMediaTask.execute(ScanMediaTask.Params(mxcUrl))
}
}

override fun getScanResultForAttachment(mxcUrl: String, callback: MatrixCallback<ScanStatusInfo>) {
taskExecutor.executorScope.launchToCallback(coroutineDispatchers.io, callback) {
val result = scanMediaTask.execute(ScanMediaTask.Params(mxcUrl))

ScanStatusInfo(
state = if (result.clean) ScanState.TRUSTED else ScanState.INFECTED,
humanReadableMessage = result.info,
scanDateTimestamp = System.currentTimeMillis()
)
}
return ScanStatusInfo(
state = if (result.clean) ScanState.TRUSTED else ScanState.INFECTED,
humanReadableMessage = result.info,
scanDateTimestamp = System.currentTimeMillis()
)
}

override fun setScannerUrl(url: String?) = contentScannerStore.setScannerUrl(url).also {
Expand All @@ -112,38 +97,34 @@ internal class DefaultContentScannerService @Inject constructor(
.create(ContentScannerApi::class.java)
contentScannerApiProvider.contentScannerApi = api

taskExecutor.executorScope.launch(coroutineDispatchers.io) {
taskExecutor.executorScope.launch {
try {
awaitCallback<String?> {
getServerPublicKey(true, it)
}
getServerPublicKey(true)
} catch (failure: Throwable) {
Timber.e("Failed to get public server api")
}
}
}
}

override fun enableScanner(enabled: Boolean) = contentScannerStore.enableScanning(enabled)
override fun enableScanner(enabled: Boolean) = contentScannerStore.enableScanner(enabled)

override fun isScannerEnabled(): Boolean = contentScannerStore.isScanEnabled()

override fun getCachedScanResultForFile(mxcUrl: String): ScanStatusInfo? {
return contentScannerStore.getScanResult(mxcUrl)
}

override fun getLiveStatusForFile(mxcUrl: String, fetchIfNeeded: Boolean): LiveData<Optional<ScanStatusInfo>> {
override fun getLiveStatusForFile(mxcUrl: String, fetchIfNeeded: Boolean, fileInfo: ElementToDecrypt?): LiveData<Optional<ScanStatusInfo>> {
val data = contentScannerStore.getLiveScanResult(mxcUrl)
if (fetchIfNeeded && !contentScannerStore.isScanResultKnownOrInProgress(mxcUrl, getContentScannerServer())) {
getScanResultForAttachment(mxcUrl, NoOpMatrixCallback())
}
return data
}

override fun getLiveStatusForEncryptedFile(mxcUrl: String, fileInfo: ElementToDecrypt, fetchIfNeeded: Boolean): LiveData<Optional<ScanStatusInfo>> {
val data = contentScannerStore.getLiveScanResult(mxcUrl)
if (fetchIfNeeded && !contentScannerStore.isScanResultKnownOrInProgress(mxcUrl, getContentScannerServer())) {
getScanResultForAttachment(mxcUrl, fileInfo, NoOpMatrixCallback())
taskExecutor.executorScope.launch {
try {
getScanResultForAttachment(mxcUrl, fileInfo)
} catch (failure: Throwable) {
Timber.e("Failed to get file status : ${failure.localizedMessage}")
}
}
}
return data
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ package org.matrix.android.sdk.internal.session.contentscanner

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.session.contentscanner.ContentScannerService
import org.matrix.android.sdk.api.session.contentscanner.ScanStatusInfo
import org.matrix.android.sdk.api.util.Optional
import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt
import org.matrix.android.sdk.internal.session.SessionScope
import javax.inject.Inject

/**
* Created to by-pass ProfileTask execution in LoginWizard.
*/
class DisabledContentScannerService : ContentScannerService {
@SessionScope
internal class DisabledContentScannerService @Inject constructor() : ContentScannerService {

override val serverPublicKey: String?
get() = null
Expand All @@ -36,13 +38,12 @@ class DisabledContentScannerService : ContentScannerService {
return null
}

override fun getServerPublicKey(forceDownload: Boolean, callback: MatrixCallback<String?>) {
}

override fun getScanResultForAttachment(mxcUrl: String, callback: MatrixCallback<ScanStatusInfo>) {
override suspend fun getServerPublicKey(forceDownload: Boolean): String? {
return null
}

override fun getScanResultForAttachment(mxcUrl: String, fileInfo: ElementToDecrypt, callback: MatrixCallback<ScanStatusInfo>) {
override suspend fun getScanResultForAttachment(mxcUrl: String, fileInfo: ElementToDecrypt?): ScanStatusInfo {
TODO("Not yet implemented")
}

override fun setScannerUrl(url: String?) {
Expand All @@ -55,11 +56,7 @@ class DisabledContentScannerService : ContentScannerService {
return false
}

override fun getLiveStatusForFile(mxcUrl: String, fetchIfNeeded: Boolean): LiveData<Optional<ScanStatusInfo>> {
return MutableLiveData()
}

override fun getLiveStatusForEncryptedFile(mxcUrl: String, fileInfo: ElementToDecrypt, fetchIfNeeded: Boolean): LiveData<Optional<ScanStatusInfo>> {
override fun getLiveStatusForFile(mxcUrl: String, fetchIfNeeded: Boolean, fileInfo: ElementToDecrypt?): LiveData<Optional<ScanStatusInfo>> {
return MutableLiveData()
}

Expand Down
Loading

0 comments on commit 69b56ed

Please sign in to comment.