Skip to content

Commit 9ced48c

Browse files
committed
Improve thread safety
1 parent f8e1cc5 commit 9ced48c

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

152154
runCatching {
@@ -170,7 +172,7 @@ class WebViewCompatWebCompatMessagingPlugin @Inject constructor(
170172
override fun unregister(
171173
webView: WebView,
172174
) {
173-
coroutineScope.launch {
175+
appCoroutineScope.launch {
174176
if (!webViewCompatContentScopeScripts.isEnabled()) return@launch
175177
runCatching {
176178
return@runCatching webViewCompatWrapper.removeWebMessageListener(webView, JS_OBJECT_NAME)
@@ -191,7 +193,9 @@ class WebViewCompatWebCompatMessagingPlugin @Inject constructor(
191193
put("featureName", response.featureName)
192194
put("context", context)
193195
}
194-
replyProxy.postMessage(responseWithId.toString())
196+
appCoroutineScope.launch(dispatcherProvider.main()) {
197+
replyProxy.postMessage(responseWithId.toString())
198+
}
195199
}
196200
}
197201
}

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)