Skip to content

Commit

Permalink
Remove embedded dataset and add worker to download data
Browse files Browse the repository at this point in the history
  • Loading branch information
CrisBarreiro committed Jan 15, 2025
1 parent 39d1235 commit 39c4f03
Show file tree
Hide file tree
Showing 12 changed files with 390 additions and 470,353 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies {

implementation KotlinX.coroutines.android
implementation AndroidX.core.ktx
implementation AndroidX.work.runtimeKtx
implementation Google.dagger

implementation project(path: ':common-utils')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2025 DuckDuckGo
*
* 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 com.duckduckgo.malicioussiteprotection.impl

import android.content.Context
import androidx.lifecycle.LifecycleOwner
import androidx.work.BackoffPolicy
import androidx.work.CoroutineWorker
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import com.duckduckgo.anvil.annotations.ContributesWorker
import com.duckduckgo.app.lifecycle.MainProcessLifecycleObserver
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.malicioussiteprotection.impl.data.MaliciousSiteRepository
import com.squareup.anvil.annotations.ContributesMultibinding
import dagger.SingleInstanceIn
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import kotlinx.coroutines.withContext
import timber.log.Timber

@ContributesWorker(AppScope::class)
class MaliciousSiteProtectionFiltersUpdateWorker(
context: Context,
workerParameters: WorkerParameters,
) : CoroutineWorker(context, workerParameters) {
@Inject
lateinit var maliciousSiteRepository: MaliciousSiteRepository

@Inject
lateinit var dispatcherProvider: DispatcherProvider

@Inject
lateinit var maliciousSiteProtectionFeature: MaliciousSiteProtectionRCFeature

override suspend fun doWork(): Result {
return withContext(dispatcherProvider.io()) {
Timber.v("Cris. Executing MSP worker")
if (maliciousSiteProtectionFeature.isFeatureEnabled().not()) {
return@withContext Result.success()
}
try {
maliciousSiteRepository.loadFilters()
return@withContext Result.success()
} catch (e: Exception) {
return@withContext Result.retry()
}
}
}
}

@ContributesMultibinding(
scope = AppScope::class,
boundType = MainProcessLifecycleObserver::class,
)
@SingleInstanceIn(AppScope::class)
class MaliciousSiteProtectionFiltersUpdateWorkerScheduler @Inject constructor(
private val workManager: WorkManager,
private val maliciousSiteProtectionFeature: MaliciousSiteProtectionRCFeature,

) : MainProcessLifecycleObserver {

override fun onCreate(owner: LifecycleOwner) {
Timber.v("Cris. SchedulingMSP worker")
val workerRequest = PeriodicWorkRequestBuilder<MaliciousSiteProtectionFiltersUpdateWorker>(
maliciousSiteProtectionFeature.getFilterSetUpdateFrequency(),
TimeUnit.MINUTES,
)
.addTag(MALICIOUS_SITE_PROTECTION_FILTERS_UPDATE_WORKER_TAG)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 1, TimeUnit.MINUTES)
.build()
workManager.enqueueUniquePeriodicWork(MALICIOUS_SITE_PROTECTION_FILTERS_UPDATE_WORKER_TAG, ExistingPeriodicWorkPolicy.UPDATE, workerRequest)
}

companion object {
private const val MALICIOUS_SITE_PROTECTION_FILTERS_UPDATE_WORKER_TAG = "MALICIOUS_SITE_PROTECTION_FILTERS_UPDATE_WORKER_TAG"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2025 DuckDuckGo
*
* 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 com.duckduckgo.malicioussiteprotection.impl

import android.content.Context
import androidx.lifecycle.LifecycleOwner
import androidx.work.BackoffPolicy
import androidx.work.CoroutineWorker
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import com.duckduckgo.anvil.annotations.ContributesWorker
import com.duckduckgo.app.lifecycle.MainProcessLifecycleObserver
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.malicioussiteprotection.impl.data.MaliciousSiteRepository
import com.squareup.anvil.annotations.ContributesMultibinding
import dagger.SingleInstanceIn
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import kotlinx.coroutines.withContext
import timber.log.Timber

@ContributesWorker(AppScope::class)
class MaliciousSiteProtectionHashPrefixesUpdateWorker(
context: Context,
workerParameters: WorkerParameters,
) : CoroutineWorker(context, workerParameters) {
@Inject
lateinit var maliciousSiteRepository: MaliciousSiteRepository

@Inject
lateinit var dispatcherProvider: DispatcherProvider

@Inject
lateinit var maliciousSiteProtectionFeature: MaliciousSiteProtectionRCFeature

override suspend fun doWork(): Result {
return withContext(dispatcherProvider.io()) {
Timber.v("Cris. Executing MSP worker")
if (maliciousSiteProtectionFeature.isFeatureEnabled().not()) {
return@withContext Result.success()
}
try {
maliciousSiteRepository.loadHashPrefixes()
return@withContext Result.success()
} catch (e: Exception) {
return@withContext Result.retry()
}
}
}
}

@ContributesMultibinding(
scope = AppScope::class,
boundType = MainProcessLifecycleObserver::class,
)
@SingleInstanceIn(AppScope::class)
class MaliciousSiteProtectionHashPrefixesUpdateWorkerScheduler @Inject constructor(
private val workManager: WorkManager,
private val maliciousSiteProtectionFeature: MaliciousSiteProtectionRCFeature,

) : MainProcessLifecycleObserver {

override fun onCreate(owner: LifecycleOwner) {
Timber.v("Cris. SchedulingMSP worker")
val workerRequest = PeriodicWorkRequestBuilder<MaliciousSiteProtectionHashPrefixesUpdateWorker>(
maliciousSiteProtectionFeature.getHashPrefixUpdateFrequency(),
TimeUnit.MINUTES,
)
.addTag(MALICIOUS_SITE_PROTECTION_HASH_PREFIXES_UPDATE_WORKER_TAG)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 1, TimeUnit.MINUTES)
.build()
workManager.enqueueUniquePeriodicWork(
MALICIOUS_SITE_PROTECTION_HASH_PREFIXES_UPDATE_WORKER_TAG,
ExistingPeriodicWorkPolicy.UPDATE,
workerRequest,
)
}

companion object {
private const val MALICIOUS_SITE_PROTECTION_HASH_PREFIXES_UPDATE_WORKER_TAG = "MALICIOUS_SITE_PROTECTION_HASH_PREFIXES_UPDATE_WORKER_TAG"
}
}
Loading

0 comments on commit 39c4f03

Please sign in to comment.