Skip to content

Commit 97c6987

Browse files
committed
Improve thread safety
1 parent 983e95e commit 97c6987

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import androidx.annotation.VisibleForTesting
2222
import androidx.webkit.JavaScriptReplyProxy
2323
import com.duckduckgo.app.di.AppCoroutineScope
2424
import com.duckduckgo.browser.api.webviewcompat.WebViewCompatWrapper
25+
import com.duckduckgo.common.utils.DispatcherProvider
2526
import com.duckduckgo.common.utils.plugins.PluginPoint
2627
import com.duckduckgo.contentscopescripts.api.WebViewCompatContentScopeJsMessageHandlersPlugin
2728
import com.duckduckgo.contentscopescripts.impl.WebViewCompatContentScopeScripts
@@ -50,7 +51,8 @@ class WebViewCompatWebCompatMessagingPlugin @Inject constructor(
5051
private val globalHandlers: PluginPoint<GlobalContentScopeJsMessageHandlersPlugin>,
5152
private val webViewCompatContentScopeScripts: WebViewCompatContentScopeScripts,
5253
private val webViewCompatWrapper: WebViewCompatWrapper,
53-
@AppCoroutineScope private val coroutineScope: CoroutineScope,
54+
private val dispatcherProvider: DispatcherProvider,
55+
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
5456
) : WebMessagingPlugin {
5557

5658
private val moshi = Moshi.Builder().add(JSONObjectAdapter()).build()
@@ -146,7 +148,7 @@ class WebViewCompatWebCompatMessagingPlugin @Inject constructor(
146148
jsMessageCallback: WebViewCompatMessageCallback?,
147149
webView: WebView,
148150
) {
149-
coroutineScope.launch {
151+
appCoroutineScope.launch {
150152
if (!webViewCompatContentScopeScripts.isEnabled()) return@launch
151153
if (jsMessageCallback == null) throw Exception("Callback cannot be null")
152154

@@ -171,7 +173,7 @@ class WebViewCompatWebCompatMessagingPlugin @Inject constructor(
171173
override fun unregister(
172174
webView: WebView,
173175
) {
174-
coroutineScope.launch {
176+
appCoroutineScope.launch {
175177
if (!webViewCompatContentScopeScripts.isEnabled()) return@launch
176178
runCatching {
177179
return@runCatching webViewCompatWrapper.removeWebMessageListener(webView, JS_OBJECT_NAME)
@@ -192,7 +194,9 @@ class WebViewCompatWebCompatMessagingPlugin @Inject constructor(
192194
put("featureName", response.featureName)
193195
put("context", context)
194196
}
195-
replyProxy.postMessage(responseWithId.toString())
197+
appCoroutineScope.launch(dispatcherProvider.main()) {
198+
replyProxy.postMessage(responseWithId.toString())
199+
}
196200
}
197201
}
198202
}

content-scope-scripts/content-scope-scripts-impl/src/test/java/com/duckduckgo/contentscopescripts/impl/messaging/WebViewCompatWebCompatMessagingPluginTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ class WebViewCompatWebCompatMessagingPluginTest {
108108
globalHandlers = globalHandlers,
109109
webViewCompatContentScopeScripts = webViewCompatContentScopeScripts,
110110
webViewCompatWrapper = mockWebViewCompatWrapper,
111-
coroutineScope = coroutineRule.testScope,
111+
dispatcherProvider = coroutineRule.testDispatcherProvider,
112+
appCoroutineScope = coroutineRule.testScope,
112113
)
113114
}
114115

0 commit comments

Comments
 (0)