Skip to content

Commit 10b111a

Browse files
committed
Add support to unregister message listener
1 parent 8fc83e6 commit 10b111a

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

app/src/main/java/com/duckduckgo/app/browser/BrowserTabFragment.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3855,8 +3855,13 @@ class BrowserTabFragment :
38553855

38563856
private fun destroyWebView() {
38573857
if (::webViewContainer.isInitialized) webViewContainer.removeAllViews()
3858-
webView?.destroy()
3859-
webView = null
3858+
appCoroutineScope.launch(dispatchers.main()) {
3859+
webView?.let {
3860+
adsJsContentScopeScripts.unregister(it)
3861+
it.destroy()
3862+
}
3863+
webView = null
3864+
}
38603865
}
38613866

38623867
private fun convertBlobToDataUri(blob: Command.ConvertBlobToDataUri) {

content-scope-scripts/content-scope-scripts-impl/src/main/java/com/duckduckgo/contentscopescripts/impl/RealAdsJsContentScopeScripts.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.duckduckgo.contentscopescripts.impl
1818

1919
import com.duckduckgo.app.privacy.db.UserAllowListRepository
2020
import com.duckduckgo.appbuildconfig.api.AppBuildConfig
21+
import com.duckduckgo.common.utils.DispatcherProvider
2122
import com.duckduckgo.common.utils.plugins.PluginPoint
2223
import com.duckduckgo.contentscopescripts.api.ContentScopeConfigPlugin
2324
import com.duckduckgo.di.scopes.AppScope
@@ -35,6 +36,7 @@ import java.util.concurrent.CopyOnWriteArrayList
3536
import javax.inject.Inject
3637
import javax.inject.Named
3738
import kotlinx.coroutines.runBlocking
39+
import kotlinx.coroutines.withContext
3840

3941
interface AdsJsContentScopeScripts {
4042
fun getScript(
@@ -58,6 +60,7 @@ class RealAdsJsContentScopeScripts @Inject constructor(
5860
private val unprotectedTemporary: UnprotectedTemporary,
5961
private val fingerprintProtectionManager: FingerprintProtectionManager,
6062
private val contentScopeScriptsFeature: ContentScopeScriptsFeature,
63+
private val dispatcherProvider: DispatcherProvider,
6164
) : AdsJsContentScopeScripts {
6265

6366
private var cachedContentScopeJson: String = getContentScopeJson("", emptyList())
@@ -112,7 +115,9 @@ class RealAdsJsContentScopeScripts @Inject constructor(
112115
}
113116

114117
override suspend fun isEnabled(): Boolean {
115-
return contentScopeScriptsFeature.self().isEnabled() && contentScopeScriptsFeature.useNewWebCompatApis().isEnabled()
118+
return withContext(dispatcherProvider.io()) {
119+
contentScopeScriptsFeature.self().isEnabled() && contentScopeScriptsFeature.useNewWebCompatApis().isEnabled()
120+
}
116121
}
117122

118123
private fun getSecretKeyValuePair() = "\"messageSecret\":\"$secret\""

content-scope-scripts/content-scope-scripts-impl/src/main/java/com/duckduckgo/contentscopescripts/impl/messaging/AdsjsContentScopeMessaging.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import logcat.LogPriority.ERROR
3838
import logcat.asLog
3939
import logcat.logcat
4040

41+
private const val JS_OBJECT_NAME = "contentScopeAdsjs"
42+
4143
@ContributesBinding(ActivityScope::class)
4244
@Named("AdsjsContentScopeScripts")
4345
class AdsjsContentScopeMessaging @Inject constructor(
@@ -93,7 +95,7 @@ class AdsjsContentScopeMessaging @Inject constructor(
9395
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) {
9496
WebViewCompat.addWebMessageListener(
9597
webView,
96-
"contentScopeAdsjs",
98+
JS_OBJECT_NAME,
9799
allowedDomains,
98100
) { _, message, _, _, replyProxy ->
99101
process(
@@ -110,4 +112,17 @@ class AdsjsContentScopeMessaging @Inject constructor(
110112
false
111113
}
112114
}
115+
116+
override suspend fun unregister(webView: WebView) {
117+
if (!adsJsContentScopeScripts.isEnabled()) return
118+
withContext(dispatcherProvider.main()) {
119+
runCatching {
120+
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) {
121+
WebViewCompat.removeWebMessageListener(webView, JS_OBJECT_NAME)
122+
} else {
123+
logcat(ERROR) { "WebMessageListener is not supported on this WebView" }
124+
}
125+
}
126+
}
127+
}
113128
}

js-messaging/js-messaging-api/src/main/java/com/duckduckgo/js/messaging/api/AdsjsMessaging.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ interface AdsjsMessaging {
2424
* Method to register the JS interface to the webView instance
2525
*/
2626
suspend fun register(webView: WebView, jsMessageCallback: JsMessageCallback?)
27+
suspend fun unregister(webView: WebView)
2728

2829
/**
2930
* Context name

0 commit comments

Comments
 (0)