Skip to content

Commit 40387e5

Browse files
committed
Improve thread safety
1 parent 16ef071 commit 40387e5

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
@@ -1353,7 +1353,7 @@ class BrowserWebViewClientTest {
13531353
}
13541354

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ class BrowserWebViewClient @Inject constructor(
783783
}
784784
}
785785

786-
fun postMessage(
786+
suspend fun postMessage(
787787
eventData: SubscriptionEventData,
788788
fallback: () -> Unit,
789789
) {

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
@@ -39,7 +39,6 @@ import com.squareup.moshi.Moshi
3939
import javax.inject.Inject
4040
import kotlinx.coroutines.CoroutineScope
4141
import kotlinx.coroutines.launch
42-
import kotlinx.coroutines.runBlocking
4342
import kotlinx.coroutines.withContext
4443
import logcat.LogPriority.ERROR
4544
import logcat.asLog
@@ -208,14 +207,9 @@ class WebViewCompatWebCompatMessagingPlugin @Inject constructor(
208207
}
209208

210209
@SuppressLint("RequiresFeature")
211-
override fun postMessage(subscriptionEventData: SubscriptionEventData): Boolean {
210+
override suspend fun postMessage(subscriptionEventData: SubscriptionEventData): Boolean {
212211
return runCatching {
213-
// TODO (cbarreiro) temporary, remove
214-
val newWebCompatApisEnabled = runBlocking {
215-
webViewCompatContentScopeScripts.isEnabled()
216-
}
217-
218-
if (!newWebCompatApisEnabled) {
212+
if (!webViewCompatContentScopeScripts.isEnabled()) {
219213
return false
220214
}
221215

@@ -228,8 +222,10 @@ class WebViewCompatWebCompatMessagingPlugin @Inject constructor(
228222
moshi.adapter(SubscriptionEvent::class.java).toJson(it)
229223
}
230224

231-
globalReplyProxy?.postMessage(subscriptionEvent)
232-
true
225+
withContext(dispatcherProvider.main()) {
226+
globalReplyProxy?.postMessage(subscriptionEvent)
227+
true
228+
}
233229
}.getOrElse { false }
234230
}
235231
}

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
unregisterer: suspend (objectName: String) -> Boolean,
2929
)
3030

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

0 commit comments

Comments
 (0)