Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take into account well-known config to disable WorkManager network constraint #8662

Merged
merged 14 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/8662.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Take into account boolean "io.element.disable_network_constraint" from the .well-known file.
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,11 @@ data class DiscoveryInformation(
* Note: matrix.org does not send this field
*/
@Json(name = "m.identity_server")
val identityServer: WellKnownBaseConfig? = null
val identityServer: WellKnownBaseConfig? = null,

/**
* If set to true, the SDK will not use the network constraint when configuring Worker for the WorkManager.
*/
@Json(name = "io.element.disable_network_constraint")
val disableNetworkConstraint: Boolean? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,10 @@ data class WellKnown(
*/
@Json(name = "org.matrix.msc2965.authentication")
val unstableDelegatedAuthConfig: DelegatedAuthConfig? = null,

/**
* If set to true, the SDK will not use the network constraint when configuring Worker for the WorkManager.
*/
@Json(name = "io.element.disable_network_constraint")
val disableNetworkConstraint: Boolean? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ data class HomeServerCapabilities(
* Authentication issuer for use with MSC3824 delegated OIDC, provided in Wellknown.
*/
val authenticationIssuer: String? = null,

/**
* If set to true, the SDK will not use the network constraint when configuring Worker for the WorkManager, provided in Wellknown.
*/
val disableNetworkConstraint: Boolean = false,
) {

enum class RoomCapabilitySupport {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo050
import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo051
import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo052
import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo053
import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo054
import org.matrix.android.sdk.internal.util.Normalizer
import org.matrix.android.sdk.internal.util.database.MatrixRealmMigration
import javax.inject.Inject
Expand All @@ -78,7 +79,7 @@ internal class RealmSessionStoreMigration @Inject constructor(
private val normalizer: Normalizer
) : MatrixRealmMigration(
dbName = "Session",
schemaVersion = 53L,
schemaVersion = 54L,
) {
/**
* Forces all RealmSessionStoreMigration instances to be equal.
Expand Down Expand Up @@ -141,5 +142,6 @@ internal class RealmSessionStoreMigration @Inject constructor(
if (oldVersion < 51) MigrateSessionTo051(realm).perform()
if (oldVersion < 52) MigrateSessionTo052(realm).perform()
if (oldVersion < 53) MigrateSessionTo053(realm).perform()
if (oldVersion < 54) MigrateSessionTo054(realm).perform()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ internal object HomeServerCapabilitiesMapper {
canRedactRelatedEvents = entity.canRedactEventWithRelations,
externalAccountManagementUrl = entity.externalAccountManagementUrl,
authenticationIssuer = entity.authenticationIssuer,
disableNetworkConstraint = entity.disableNetworkConstraint,
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2023 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.database.migration

import io.realm.DynamicRealm
import org.matrix.android.sdk.internal.database.model.HomeServerCapabilitiesEntityFields
import org.matrix.android.sdk.internal.extensions.forceRefreshOfHomeServerCapabilities
import org.matrix.android.sdk.internal.util.database.RealmMigrator

internal class MigrateSessionTo054(realm: DynamicRealm) : RealmMigrator(realm, 54) {
override fun doMigrate(realm: DynamicRealm) {
realm.schema.get("HomeServerCapabilitiesEntity")
?.addField(HomeServerCapabilitiesEntityFields.DISABLE_NETWORK_CONSTRAINT, Boolean::class.java)
?.forceRefreshOfHomeServerCapabilities()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ internal open class HomeServerCapabilitiesEntity(
var canRedactEventWithRelations: Boolean = false,
var externalAccountManagementUrl: String? = null,
var authenticationIssuer: String? = null,
var disableNetworkConstraint: Boolean = false,
) : RealmObject() {

companion object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
import org.matrix.android.sdk.internal.session.SessionScope
import org.matrix.android.sdk.internal.session.workmanager.WorkManagerConfig
import org.matrix.android.sdk.internal.worker.MatrixWorkerFactory
import timber.log.Timber
import java.util.concurrent.TimeUnit
import javax.inject.Inject

Expand Down Expand Up @@ -102,12 +104,20 @@ internal class WorkManagerProvider @Inject constructor(
companion object {
private const val MATRIX_SDK_TAG_PREFIX = "MatrixSDK-"

/**
* Default constraints: connected network.
*/
val workConstraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
fun getWorkConstraints(
workManagerConfig: WorkManagerConfig,
): Constraints {
val widthNetworkConstraint = workManagerConfig.withNetworkConstraint()
giomfo marked this conversation as resolved.
Show resolved Hide resolved
return Constraints.Builder()
.apply {
if (widthNetworkConstraint) {
giomfo marked this conversation as resolved.
Show resolved Hide resolved
setRequiredNetworkType(NetworkType.CONNECTED)
} else {
Timber.w("Network constraint is disabled")
}
}
.build()
}

// Use min value, smaller value will be ignored
const val BACKOFF_DELAY_MILLIS = WorkRequest.MIN_BACKOFF_MILLIS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ import org.matrix.android.sdk.internal.session.room.tombstone.RoomTombstoneEvent
import org.matrix.android.sdk.internal.session.typing.DefaultTypingUsersTracker
import org.matrix.android.sdk.internal.session.user.accountdata.DefaultSessionAccountDataService
import org.matrix.android.sdk.internal.session.widgets.DefaultWidgetURLFormatter
import org.matrix.android.sdk.internal.session.workmanager.DefaultWorkManagerConfig
import org.matrix.android.sdk.internal.session.workmanager.WorkManagerConfig
import retrofit2.Retrofit
import java.io.File
import javax.inject.Provider
Expand Down Expand Up @@ -422,4 +424,7 @@ internal abstract class SessionModule {

@Binds
abstract fun bindPollAggregationProcessor(processor: DefaultPollAggregationProcessor): PollAggregationProcessor

@Binds
abstract fun bindWorkManaerConfig(config: DefaultWorkManagerConfig): WorkManagerConfig
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.zhuinden.monarchy.Monarchy
import org.matrix.android.sdk.api.MatrixPatterns.getServerName
import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
import org.matrix.android.sdk.api.auth.wellknown.WellknownResult
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.extensions.orTrue
import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilities
import org.matrix.android.sdk.internal.auth.version.Versions
Expand Down Expand Up @@ -167,6 +168,7 @@ internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor(
}
homeServerCapabilitiesEntity.authenticationIssuer = getWellknownResult.wellKnown.unstableDelegatedAuthConfig?.issuer
homeServerCapabilitiesEntity.externalAccountManagementUrl = getWellknownResult.wellKnown.unstableDelegatedAuthConfig?.accountManagementUrl
homeServerCapabilitiesEntity.disableNetworkConstraint = getWellknownResult.wellKnown.disableNetworkConstraint.orFalse()
}

homeServerCapabilitiesEntity.canLoginWithQrCode = canLoginWithQrCode(getCapabilitiesResult, getVersionResult)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.matrix.android.sdk.internal.di.SessionDatabase
import org.matrix.android.sdk.internal.di.SessionId
import org.matrix.android.sdk.internal.di.WorkManagerProvider
import org.matrix.android.sdk.internal.session.pushers.gateway.PushGatewayNotifyTask
import org.matrix.android.sdk.internal.session.workmanager.WorkManagerConfig
import org.matrix.android.sdk.internal.task.TaskExecutor
import org.matrix.android.sdk.internal.task.configureWith
import org.matrix.android.sdk.internal.worker.WorkerParamsFactory
Expand All @@ -44,7 +45,8 @@ internal class DefaultPushersService @Inject constructor(
private val addPusherTask: AddPusherTask,
private val togglePusherTask: TogglePusherTask,
private val removePusherTask: RemovePusherTask,
private val taskExecutor: TaskExecutor
private val taskExecutor: TaskExecutor,
private val workManagerConfig: WorkManagerConfig,
) : PushersService {

override suspend fun testPush(
Expand Down Expand Up @@ -130,7 +132,7 @@ internal class DefaultPushersService @Inject constructor(
private fun enqueueAddPusher(pusher: JsonPusher): UUID {
val params = AddPusherWorker.Params(sessionId, pusher)
val request = workManagerProvider.matrixOneTimeWorkRequestBuilder<AddPusherWorker>()
.setConstraints(WorkManagerProvider.workConstraints)
.setConstraints(WorkManagerProvider.getWorkConstraints(workManagerConfig))
.setInputData(WorkerParamsFactory.toData(params))
.setBackoffCriteria(BackoffPolicy.LINEAR, WorkManagerProvider.BACKOFF_DELAY_MILLIS, TimeUnit.MILLISECONDS)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ import org.matrix.android.sdk.internal.crypto.store.IMXCommonCryptoStore
import org.matrix.android.sdk.internal.di.SessionId
import org.matrix.android.sdk.internal.di.WorkManagerProvider
import org.matrix.android.sdk.internal.session.content.UploadContentWorker
import org.matrix.android.sdk.internal.session.homeserver.HomeServerCapabilitiesDataSource
import org.matrix.android.sdk.internal.session.room.send.queue.EventSenderProcessor
import org.matrix.android.sdk.internal.session.workmanager.WorkManagerConfig
import org.matrix.android.sdk.internal.task.TaskExecutor
import org.matrix.android.sdk.internal.util.CancelableWork
import org.matrix.android.sdk.internal.worker.WorkerParamsFactory
Expand All @@ -73,7 +75,8 @@ internal class DefaultSendService @AssistedInject constructor(
private val taskExecutor: TaskExecutor,
private val localEchoRepository: LocalEchoRepository,
private val eventSenderProcessor: EventSenderProcessor,
private val cancelSendTracker: CancelSendTracker
private val cancelSendTracker: CancelSendTracker,
private val workManagerConfig: WorkManagerConfig,
) : SendService {

@AssistedFactory
Expand Down Expand Up @@ -373,7 +376,7 @@ internal class DefaultSendService @AssistedInject constructor(
val uploadWorkData = WorkerParamsFactory.toData(uploadMediaWorkerParams)

return workManagerProvider.matrixOneTimeWorkRequestBuilder<UploadContentWorker>()
.setConstraints(WorkManagerProvider.workConstraints)
.setConstraints(WorkManagerProvider.getWorkConstraints(workManagerConfig))
.startChain(true)
.setInputData(uploadWorkData)
.setBackoffCriteria(BackoffPolicy.LINEAR, WorkManagerProvider.BACKOFF_DELAY_MILLIS, TimeUnit.MILLISECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import androidx.work.ListenableWorker
import androidx.work.OneTimeWorkRequest
import org.matrix.android.sdk.api.util.Cancelable
import org.matrix.android.sdk.internal.di.WorkManagerProvider
import org.matrix.android.sdk.internal.session.homeserver.HomeServerCapabilitiesDataSource
import org.matrix.android.sdk.internal.session.workmanager.WorkManagerConfig
import org.matrix.android.sdk.internal.util.CancelableWork
import org.matrix.android.sdk.internal.worker.startChain
import java.util.concurrent.TimeUnit
Expand All @@ -34,7 +36,8 @@ import javax.inject.Inject
* if not the chain will be doomed in failed state.
*/
internal class TimelineSendEventWorkCommon @Inject constructor(
private val workManagerProvider: WorkManagerProvider
private val workManagerProvider: WorkManagerProvider,
private val workManagerConfig: WorkManagerConfig,
) {

fun postWork(roomId: String, workRequest: OneTimeWorkRequest, policy: ExistingWorkPolicy = ExistingWorkPolicy.APPEND_OR_REPLACE): Cancelable {
Expand All @@ -47,7 +50,7 @@ internal class TimelineSendEventWorkCommon @Inject constructor(

inline fun <reified W : ListenableWorker> createWork(data: Data, startChain: Boolean): OneTimeWorkRequest {
return workManagerProvider.matrixOneTimeWorkRequestBuilder<W>()
.setConstraints(WorkManagerProvider.workConstraints)
.setConstraints(WorkManagerProvider.getWorkConstraints(workManagerConfig))
.startChain(startChain)
.setInputData(data)
.setBackoffCriteria(BackoffPolicy.LINEAR, WorkManagerProvider.BACKOFF_DELAY_MILLIS, TimeUnit.MILLISECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import org.matrix.android.sdk.api.session.sync.SyncService
import org.matrix.android.sdk.internal.di.SessionId
import org.matrix.android.sdk.internal.di.WorkManagerProvider
import org.matrix.android.sdk.internal.session.SessionState
import org.matrix.android.sdk.internal.session.homeserver.HomeServerCapabilitiesDataSource
import org.matrix.android.sdk.internal.session.sync.job.SyncThread
import org.matrix.android.sdk.internal.session.sync.job.SyncWorker
import org.matrix.android.sdk.internal.session.workmanager.WorkManagerConfig
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Provider
Expand All @@ -33,15 +35,26 @@ internal class DefaultSyncService @Inject constructor(
private val syncTokenStore: SyncTokenStore,
private val syncRequestStateTracker: SyncRequestStateTracker,
private val sessionState: SessionState,
private val workManagerConfig: WorkManagerConfig,
) : SyncService {
private var syncThread: SyncThread? = null

override fun requireBackgroundSync() {
SyncWorker.requireBackgroundSync(workManagerProvider, sessionId)
SyncWorker.requireBackgroundSync(
workManagerProvider = workManagerProvider,
sessionId = sessionId,
workManagerConfig = workManagerConfig,
)
}

override fun startAutomaticBackgroundSync(timeOutInSeconds: Long, repeatDelayInSeconds: Long) {
SyncWorker.automaticallyBackgroundSync(workManagerProvider, sessionId, timeOutInSeconds, repeatDelayInSeconds)
SyncWorker.automaticallyBackgroundSync(
workManagerProvider = workManagerProvider,
sessionId = sessionId,
workManagerConfig = workManagerConfig,
serverTimeoutInSeconds = timeOutInSeconds,
delayInSeconds = repeatDelayInSeconds,
)
}

override fun stopAnyBackgroundSync() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import org.matrix.android.sdk.api.failure.isTokenError
import org.matrix.android.sdk.internal.SessionManager
import org.matrix.android.sdk.internal.di.WorkManagerProvider
import org.matrix.android.sdk.internal.session.SessionComponent
import org.matrix.android.sdk.internal.session.homeserver.HomeServerCapabilitiesDataSource
import org.matrix.android.sdk.internal.session.sync.SyncPresence
import org.matrix.android.sdk.internal.session.sync.SyncTask
import org.matrix.android.sdk.internal.session.workmanager.WorkManagerConfig
import org.matrix.android.sdk.internal.worker.SessionSafeCoroutineWorker
import org.matrix.android.sdk.internal.worker.SessionWorkerParams
import org.matrix.android.sdk.internal.worker.WorkerParamsFactory
Expand Down Expand Up @@ -59,6 +61,7 @@ internal class SyncWorker(context: Context, workerParameters: WorkerParameters,

@Inject lateinit var syncTask: SyncTask
@Inject lateinit var workManagerProvider: WorkManagerProvider
@Inject lateinit var workManagerConfig: WorkManagerConfig

override fun injectWith(injector: SessionComponent) {
injector.inject(this)
Expand All @@ -77,6 +80,7 @@ internal class SyncWorker(context: Context, workerParameters: WorkerParameters,
automaticallyBackgroundSync(
workManagerProvider = workManagerProvider,
sessionId = params.sessionId,
workManagerConfig = workManagerConfig,
serverTimeoutInSeconds = params.timeout,
delayInSeconds = params.delay,
forceImmediate = hasToDeviceEvents
Expand All @@ -86,6 +90,7 @@ internal class SyncWorker(context: Context, workerParameters: WorkerParameters,
requireBackgroundSync(
workManagerProvider = workManagerProvider,
sessionId = params.sessionId,
workManagerConfig = workManagerConfig,
serverTimeoutInSeconds = 0
)
}
Expand Down Expand Up @@ -123,6 +128,7 @@ internal class SyncWorker(context: Context, workerParameters: WorkerParameters,
fun requireBackgroundSync(
workManagerProvider: WorkManagerProvider,
sessionId: String,
workManagerConfig: WorkManagerConfig,
serverTimeoutInSeconds: Long = 0
) {
val data = WorkerParamsFactory.toData(
Expand All @@ -134,7 +140,7 @@ internal class SyncWorker(context: Context, workerParameters: WorkerParameters,
)
)
val workRequest = workManagerProvider.matrixOneTimeWorkRequestBuilder<SyncWorker>()
.setConstraints(WorkManagerProvider.workConstraints)
.setConstraints(WorkManagerProvider.getWorkConstraints(workManagerConfig))
.setBackoffCriteria(BackoffPolicy.LINEAR, WorkManagerProvider.BACKOFF_DELAY_MILLIS, TimeUnit.MILLISECONDS)
.setInputData(data)
.startChain(true)
Expand All @@ -146,6 +152,7 @@ internal class SyncWorker(context: Context, workerParameters: WorkerParameters,
fun automaticallyBackgroundSync(
workManagerProvider: WorkManagerProvider,
sessionId: String,
workManagerConfig: WorkManagerConfig,
serverTimeoutInSeconds: Long = 0,
delayInSeconds: Long = 30,
forceImmediate: Boolean = false
Expand All @@ -160,7 +167,7 @@ internal class SyncWorker(context: Context, workerParameters: WorkerParameters,
)
)
val workRequest = workManagerProvider.matrixOneTimeWorkRequestBuilder<SyncWorker>()
.setConstraints(WorkManagerProvider.workConstraints)
.setConstraints(WorkManagerProvider.getWorkConstraints(workManagerConfig))
.setInputData(data)
.setBackoffCriteria(BackoffPolicy.LINEAR, WorkManagerProvider.BACKOFF_DELAY_MILLIS, TimeUnit.MILLISECONDS)
.setInitialDelay(if (forceImmediate) 0 else delayInSeconds, TimeUnit.SECONDS)
Expand Down
Loading
Loading