Skip to content

Commit ebbd73d

Browse files
committed
Improve thread safety
1 parent b9ed3dd commit ebbd73d

File tree

4 files changed

+9
-13
lines changed

4 files changed

+9
-13
lines changed

app/src/androidTest/java/com/duckduckgo/app/browser/BrowserWebViewClientTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,7 @@ class BrowserWebViewClientTest {
13511351
}
13521352

13531353
// TODO (cbarreiro) Test message posting
1354-
override fun postMessage(subscriptionEventData: SubscriptionEventData): Boolean {
1354+
override suspend fun postMessage(subscriptionEventData: SubscriptionEventData): Boolean {
13551355
TODO("Not yet implemented")
13561356
}
13571357
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ class BrowserWebViewClient @Inject constructor(
772772
}
773773
}
774774

775-
fun postMessage(
775+
suspend fun postMessage(
776776
eventData: SubscriptionEventData,
777777
fallback: () -> Unit,
778778
) {

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import com.squareup.moshi.Moshi
4040
import javax.inject.Inject
4141
import kotlinx.coroutines.CoroutineScope
4242
import kotlinx.coroutines.launch
43-
import kotlinx.coroutines.runBlocking
4443
import kotlinx.coroutines.withContext
4544
import logcat.LogPriority.ERROR
4645
import logcat.asLog
@@ -213,14 +212,9 @@ class WebViewCompatWebCompatMessagingPlugin @Inject constructor(
213212
}
214213

215214
@SuppressLint("RequiresFeature")
216-
override fun postMessage(subscriptionEventData: SubscriptionEventData): Boolean {
215+
override suspend fun postMessage(subscriptionEventData: SubscriptionEventData): Boolean {
217216
return runCatching {
218-
// TODO (cbarreiro) temporary, remove
219-
val newWebCompatApisEnabled = runBlocking {
220-
webViewCompatContentScopeScripts.isEnabled()
221-
}
222-
223-
if (!newWebCompatApisEnabled) {
217+
if (!webViewCompatContentScopeScripts.isEnabled()) {
224218
return false
225219
}
226220

@@ -233,8 +227,10 @@ class WebViewCompatWebCompatMessagingPlugin @Inject constructor(
233227
moshi.adapter(SubscriptionEvent::class.java).toJson(it)
234228
}
235229

236-
globalReplyProxy?.postMessage(subscriptionEvent)
237-
true
230+
withContext(dispatcherProvider.main()) {
231+
globalReplyProxy?.postMessage(subscriptionEvent)
232+
true
233+
}
238234
}.getOrElse { false }
239235
}
240236
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ interface WebMessagingPlugin {
2828
webView: WebView,
2929
)
3030

31-
fun postMessage(subscriptionEventData: SubscriptionEventData): Boolean
31+
suspend fun postMessage(subscriptionEventData: SubscriptionEventData): Boolean
3232
}

0 commit comments

Comments
 (0)