Skip to content

Commit 5e8147a

Browse files
committed
Improve thread safety
1 parent 33d7500 commit 5e8147a

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
@@ -1350,7 +1350,7 @@ class BrowserWebViewClientTest {
13501350
}
13511351

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ class BrowserWebViewClient @Inject constructor(
763763
}
764764
}
765765

766-
fun postMessage(
766+
suspend fun postMessage(
767767
eventData: SubscriptionEventData,
768768
fallback: () -> Unit,
769769
) {

content-scope-scripts/content-scope-scripts-impl/src/main/java/com/duckduckgo/contentscopescripts/impl/messaging/ContentScopeScriptsWebMessagingPlugin.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
@@ -212,14 +211,9 @@ class ContentScopeScriptsWebMessagingPlugin @Inject constructor(
212211
}
213212

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

@@ -232,8 +226,10 @@ class ContentScopeScriptsWebMessagingPlugin @Inject constructor(
232226
moshi.adapter(SubscriptionEvent::class.java).toJson(it)
233227
}
234228

235-
globalReplyProxy?.postMessage(subscriptionEvent)
236-
true
229+
withContext(dispatcherProvider.main()) {
230+
globalReplyProxy?.postMessage(subscriptionEvent)
231+
true
232+
}
237233
}.getOrElse { false }
238234
}
239235
}

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)