Skip to content

Commit 551fa17

Browse files
committed
Fix threading
1 parent 659ca09 commit 551fa17

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
@@ -20,6 +20,7 @@ import android.webkit.WebView
2020
import androidx.annotation.VisibleForTesting
2121
import androidx.webkit.JavaScriptReplyProxy
2222
import com.duckduckgo.app.browser.api.DuckDuckGoWebView
23+
import com.duckduckgo.app.di.AppCoroutineScope
2324
import com.duckduckgo.common.utils.DispatcherProvider
2425
import com.duckduckgo.common.utils.plugins.PluginPoint
2526
import com.duckduckgo.contentscopescripts.api.AdsjsContentScopeJsMessageHandlersPlugin
@@ -34,6 +35,8 @@ import com.squareup.anvil.annotations.ContributesBinding
3435
import com.squareup.moshi.Moshi
3536
import javax.inject.Inject
3637
import javax.inject.Named
38+
import kotlinx.coroutines.CoroutineScope
39+
import kotlinx.coroutines.launch
3740
import kotlinx.coroutines.withContext
3841
import logcat.LogPriority.ERROR
3942
import logcat.asLog
@@ -49,6 +52,7 @@ class AdsjsContentScopeMessaging @Inject constructor(
4952
private val globalHandlers: PluginPoint<GlobalContentScopeJsMessageHandlersPlugin>,
5053
private val adsJsContentScopeScripts: AdsJsContentScopeScripts,
5154
private val dispatcherProvider: DispatcherProvider,
55+
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
5256
) : AdsjsMessaging {
5357

5458
private val moshi = Moshi.Builder().add(JSONObjectAdapter()).build()
@@ -115,7 +119,7 @@ class AdsjsContentScopeMessaging @Inject constructor(
115119
process(
116120
message.data ?: "",
117121
jsMessageCallback,
118-
replyProxy
122+
replyProxy,
119123
)
120124
}
121125
} ?: false
@@ -139,18 +143,20 @@ class AdsjsContentScopeMessaging @Inject constructor(
139143
}
140144
}
141145

142-
private suspend fun onResponse(response: JsCallbackData, replyProxy: JavaScriptReplyProxy) {
146+
private fun onResponse(response: JsCallbackData, replyProxy: JavaScriptReplyProxy) {
143147
runCatching {
144148
val responseWithId = JSONObject().apply {
145149
put("id", response.id)
146150
put("result", response.params)
147151
put("featureName", response.featureName)
148152
put("context", context)
149153
}
150-
(webView as? DuckDuckGoWebView)?.safePostMessage(
151-
replyProxy,
152-
responseWithId,
153-
)
154+
appCoroutineScope.launch(dispatcherProvider.main()) {
155+
(webView as? DuckDuckGoWebView)?.safePostMessage(
156+
replyProxy,
157+
responseWithId,
158+
)
159+
}
154160
}
155161
}
156162
}

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)