From cfc8cea26c8c0bc375c8a80303b94d11ccc44a7f Mon Sep 17 00:00:00 2001 From: Cris Barreiro Date: Tue, 17 Dec 2024 10:10:56 +0100 Subject: [PATCH] Extract RC caching to a different class --- .../impl/MaliciousSiteProtectionRCFeature.kt | 85 +++++++++++++++++++ .../impl/RealMaliciousSiteProtection.kt | 43 +--------- 2 files changed, 87 insertions(+), 41 deletions(-) create mode 100644 malicious-site-protection/malicious-site-protection-impl/src/main/kotlin/com/duckduckgo/malicioussiteprotection/impl/MaliciousSiteProtectionRCFeature.kt diff --git a/malicious-site-protection/malicious-site-protection-impl/src/main/kotlin/com/duckduckgo/malicioussiteprotection/impl/MaliciousSiteProtectionRCFeature.kt b/malicious-site-protection/malicious-site-protection-impl/src/main/kotlin/com/duckduckgo/malicioussiteprotection/impl/MaliciousSiteProtectionRCFeature.kt new file mode 100644 index 000000000000..16db750c413d --- /dev/null +++ b/malicious-site-protection/malicious-site-protection-impl/src/main/kotlin/com/duckduckgo/malicioussiteprotection/impl/MaliciousSiteProtectionRCFeature.kt @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2024 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 com.duckduckgo.app.di.AppCoroutineScope +import com.duckduckgo.app.di.IsMainProcess +import com.duckduckgo.common.utils.DispatcherProvider +import com.duckduckgo.di.scopes.AppScope +import com.duckduckgo.privacy.config.api.PrivacyConfigCallbackPlugin +import com.squareup.anvil.annotations.ContributesBinding +import com.squareup.anvil.annotations.ContributesMultibinding +import dagger.SingleInstanceIn +import javax.inject.Inject +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.launch +import org.json.JSONObject + +interface MaliciousSiteProtectionRCFeature { + fun isFeatureEnabled(): Boolean + fun getHashPrefixUpdateFrequency(): Long + fun getFilterSetUpdateFrequency(): Long +} + +@SingleInstanceIn(AppScope::class) +@ContributesBinding(AppScope::class, MaliciousSiteProtectionRCFeature::class) +@ContributesMultibinding(AppScope::class, PrivacyConfigCallbackPlugin::class) +class RealMaliciousSiteProtectionRCFeature @Inject constructor( + private val dispatchers: DispatcherProvider, + private val maliciousSiteProtectionFeature: MaliciousSiteProtectionFeature, + @IsMainProcess private val isMainProcess: Boolean, + @AppCoroutineScope private val appCoroutineScope: CoroutineScope, +) : MaliciousSiteProtectionRCFeature, PrivacyConfigCallbackPlugin { + private var isFeatureEnabled = false + + private var hashPrefixUpdateFrequency = 20L + private var filterSetUpdateFrequency = 720L + + init { + if (isMainProcess) { + loadToMemory() + } + } + + override fun onPrivacyConfigDownloaded() { + loadToMemory() + } + + override fun getFilterSetUpdateFrequency(): Long { + return filterSetUpdateFrequency + } + + override fun getHashPrefixUpdateFrequency(): Long { + return hashPrefixUpdateFrequency + } + + override fun isFeatureEnabled(): Boolean { + return isFeatureEnabled + } + + private fun loadToMemory() { + appCoroutineScope.launch(dispatchers.io()) { + isFeatureEnabled = maliciousSiteProtectionFeature.self().isEnabled() + maliciousSiteProtectionFeature.self().getSettings()?.let { + JSONObject(it).let { settings -> + hashPrefixUpdateFrequency = settings.getLong("hashPrefixUpdateFrequency") + filterSetUpdateFrequency = settings.getLong("filterSetUpdateFrequency") + } + } + } + } +} diff --git a/malicious-site-protection/malicious-site-protection-impl/src/main/kotlin/com/duckduckgo/malicioussiteprotection/impl/RealMaliciousSiteProtection.kt b/malicious-site-protection/malicious-site-protection-impl/src/main/kotlin/com/duckduckgo/malicioussiteprotection/impl/RealMaliciousSiteProtection.kt index fb56a653a48e..7ff0dd28d5d5 100644 --- a/malicious-site-protection/malicious-site-protection-impl/src/main/kotlin/com/duckduckgo/malicioussiteprotection/impl/RealMaliciousSiteProtection.kt +++ b/malicious-site-protection/malicious-site-protection-impl/src/main/kotlin/com/duckduckgo/malicioussiteprotection/impl/RealMaliciousSiteProtection.kt @@ -17,56 +17,17 @@ package com.duckduckgo.malicioussiteprotection.impl import android.net.Uri -import com.duckduckgo.app.di.AppCoroutineScope -import com.duckduckgo.app.di.IsMainProcess -import com.duckduckgo.common.utils.DispatcherProvider import com.duckduckgo.di.scopes.AppScope import com.duckduckgo.malicioussiteprotection.api.MaliciousSiteProtection import com.duckduckgo.malicioussiteprotection.api.MaliciousSiteProtection.IsMaliciousResult -import com.duckduckgo.privacy.config.api.PrivacyConfigCallbackPlugin import com.squareup.anvil.annotations.ContributesBinding -import com.squareup.anvil.annotations.ContributesMultibinding import javax.inject.Inject -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.launch -import org.json.JSONObject import timber.log.Timber @ContributesBinding(AppScope::class, MaliciousSiteProtection::class) -@ContributesMultibinding(AppScope::class, PrivacyConfigCallbackPlugin::class) class RealMaliciousSiteProtection @Inject constructor( - private val dispatchers: DispatcherProvider, - private val maliciousSiteProtectionFeature: MaliciousSiteProtectionFeature, - @IsMainProcess private val isMainProcess: Boolean, - @AppCoroutineScope private val appCoroutineScope: CoroutineScope, -) : MaliciousSiteProtection, PrivacyConfigCallbackPlugin { - - private var isFeatureEnabled = false - - private var hashPrefixUpdateFrequency = 20L - private var filterSetUpdateFrequency = 720L - - init { - if (isMainProcess) { - loadToMemory() - } - } - - override fun onPrivacyConfigDownloaded() { - loadToMemory() - } - - private fun loadToMemory() { - appCoroutineScope.launch(dispatchers.io()) { - isFeatureEnabled = maliciousSiteProtectionFeature.self().isEnabled() - maliciousSiteProtectionFeature.self().getSettings()?.let { - JSONObject(it).let { settings -> - hashPrefixUpdateFrequency = settings.getLong("hashPrefixUpdateFrequency") - filterSetUpdateFrequency = settings.getLong("filterSetUpdateFrequency") - } - } - } - } + maliciousSiteProtectionRCFeature: MaliciousSiteProtectionRCFeature, +) : MaliciousSiteProtection { override suspend fun isMalicious(url: Uri, confirmationCallback: (isMalicious: Boolean) -> Unit): IsMaliciousResult { Timber.tag("MaliciousSiteProtection").d("isMalicious $url")