Skip to content

Commit 808a0ea

Browse files
committed
Fix reload loop detector: move call after guard checks and synchronize WeakHashMap
- Move reloadLoopDetector.detectReloadLoop() after the userSetting and IGNORE_CMP_SUFFIX early-return guards in PopUpFoundMessageHandlerPlugin to prevent accumulating false state when autoconsent is disabled or for -top frame rules. - Wrap WeakHashMap with Collections.synchronizedMap() in AutoconsentReloadLoopDetector to prevent ConcurrentModificationException from concurrent access across IO dispatcher and WebView JS bridge threads.
1 parent f2fae5f commit 808a0ea

2 files changed

Lines changed: 3 additions & 2 deletions

File tree

autoconsent/autoconsent-impl/src/main/java/com/duckduckgo/autoconsent/impl/AutoconsentReloadLoopDetector.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import com.duckduckgo.autoconsent.impl.pixels.AutoconsentPixelManager
2424
import com.duckduckgo.di.scopes.AppScope
2525
import dagger.SingleInstanceIn
2626
import logcat.logcat
27+
import java.util.Collections
2728
import java.util.WeakHashMap
2829
import javax.inject.Inject
2930

@@ -38,7 +39,7 @@ class AutoconsentReloadLoopDetector @Inject constructor(
3839
var reloadLoopDetected: Boolean = false,
3940
)
4041

41-
private val tabStates: MutableMap<WebView, TabState> = WeakHashMap()
42+
private val tabStates: MutableMap<WebView, TabState> = Collections.synchronizedMap(WeakHashMap())
4243

4344
fun updateUrl(webView: WebView, url: String) {
4445
val newUri = url.toUri()

autoconsent/autoconsent-impl/src/main/java/com/duckduckgo/autoconsent/impl/handlers/PopUpFoundMessageHandlerPlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ class PopUpFoundMessageHandlerPlugin @Inject constructor(
4646
autoconsentPixelManager.fireDailyPixel(AutoConsentPixel.AUTOCONSENT_POPUP_FOUND_DAILY)
4747

4848
val message: PopUpFoundMessage = parseMessage(jsonString) ?: return
49-
reloadLoopDetector.detectReloadLoop(webView, message.cmp)
5049

5150
// Opt-in flow below, if user setting is true, do nothing
5251
if (repository.userSetting) return
5352
// for complex multi-frame CMPs, trigger opt-in flow only on -frame rules
5453
if (message.cmp.endsWith(IGNORE_CMP_SUFFIX, ignoreCase = true)) return
5554

55+
reloadLoopDetector.detectReloadLoop(webView, message.cmp)
5656
autoconsentCallback.onFirstPopUpHandled()
5757
}
5858
} catch (e: Exception) {

0 commit comments

Comments
 (0)