Skip to content

Commit f688897

Browse files
committed
Fix threading
1 parent 8d460f9 commit f688897

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ class RealDuckDuckGoWebView : DuckDuckGoWebView, NestedScrollingChild3 {
482482
@SuppressLint("RequiresFeature")
483483
override suspend fun safePostMessage(
484484
replyProxy: JavaScriptReplyProxy,
485-
response: JSONObject
485+
response: JSONObject,
486486
) {
487487
runCatching {
488488
if (webViewCapabilityChecker.isSupported(WebViewCapability.WebMessageListener)) {

browser-api/src/main/java/com/duckduckgo/app/browser/api/DuckDuckGoWebView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ abstract class DuckDuckGoWebView(
6161

6262
abstract suspend fun safePostMessage(
6363
replyProxy: JavaScriptReplyProxy,
64-
response: JSONObject
64+
response: JSONObject,
6565
)
6666
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.duckduckgo.contentscopescripts.impl.messaging
1919
import android.webkit.WebView
2020
import androidx.webkit.JavaScriptReplyProxy
2121
import com.duckduckgo.app.browser.api.DuckDuckGoWebView
22+
import com.duckduckgo.app.di.AppCoroutineScope
2223
import com.duckduckgo.common.utils.DispatcherProvider
2324
import com.duckduckgo.common.utils.plugins.PluginPoint
2425
import com.duckduckgo.contentscopescripts.api.AdsjsContentScopeJsMessageHandlersPlugin
@@ -33,6 +34,8 @@ import com.squareup.anvil.annotations.ContributesBinding
3334
import com.squareup.moshi.Moshi
3435
import javax.inject.Inject
3536
import javax.inject.Named
37+
import kotlinx.coroutines.CoroutineScope
38+
import kotlinx.coroutines.launch
3639
import kotlinx.coroutines.withContext
3740
import logcat.LogPriority.ERROR
3841
import logcat.asLog
@@ -48,6 +51,7 @@ class AdsjsContentScopeMessaging @Inject constructor(
4851
private val globalHandlers: PluginPoint<GlobalContentScopeJsMessageHandlersPlugin>,
4952
private val adsJsContentScopeScripts: AdsJsContentScopeScripts,
5053
private val dispatcherProvider: DispatcherProvider,
54+
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
5155
) : AdsjsMessaging {
5256

5357
private val moshi = Moshi.Builder().add(JSONObjectAdapter()).build()
@@ -113,7 +117,7 @@ class AdsjsContentScopeMessaging @Inject constructor(
113117
process(
114118
message.data ?: "",
115119
jsMessageCallback,
116-
replyProxy
120+
replyProxy,
117121
)
118122
}
119123
} ?: false
@@ -137,18 +141,20 @@ class AdsjsContentScopeMessaging @Inject constructor(
137141
}
138142
}
139143

140-
private suspend fun onResponse(response: JsCallbackData, replyProxy: JavaScriptReplyProxy) {
144+
private fun onResponse(response: JsCallbackData, replyProxy: JavaScriptReplyProxy) {
141145
runCatching {
142146
val responseWithId = JSONObject().apply {
143147
put("id", response.id)
144148
put("result", response.params)
145149
put("featureName", response.featureName)
146150
put("context", context)
147151
}
148-
(webView as? DuckDuckGoWebView)?.safePostMessage(
149-
replyProxy,
150-
responseWithId,
151-
)
152+
appCoroutineScope.launch(dispatcherProvider.main()) {
153+
(webView as? DuckDuckGoWebView)?.safePostMessage(
154+
replyProxy,
155+
responseWithId,
156+
)
157+
}
152158
}
153159
}
154160
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ interface AdsjsMessageHandler {
5858
fun process(
5959
jsMessage: JsMessage,
6060
jsMessageCallback: AdsjsJsMessageCallback?,
61-
onResponse: suspend (JSONObject) -> Unit,
61+
onResponse: (JSONObject) -> Unit,
6262
)
6363

6464
/**

0 commit comments

Comments
 (0)