Skip to content

Commit 1964f88

Browse files
committed
Improve thread safety
1 parent 4a18322 commit 1964f88

File tree

4 files changed

+20
-25
lines changed

4 files changed

+20
-25
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class RealWebViewCompatWrapper @Inject constructor(
3737
private val dispatcherProvider: DispatcherProvider,
3838
) : WebViewCompatWrapper {
3939
override suspend fun addDocumentStartJavaScript(
40-
webView: android.webkit.WebView,
40+
webView: WebView,
4141
script: String,
4242
allowedOriginRules: Set<String>,
4343
): ScriptHandler {
@@ -46,19 +46,23 @@ class RealWebViewCompatWrapper @Inject constructor(
4646
}
4747
}
4848

49-
override fun removeWebMessageListener(webView: WebView, jsObjectName: String) {
50-
WebViewCompat.removeWebMessageListener(
51-
webView,
52-
jsObjectName,
53-
)
49+
override suspend fun removeWebMessageListener(webView: WebView, jsObjectName: String) {
50+
withContext(dispatcherProvider.main()) {
51+
WebViewCompat.removeWebMessageListener(
52+
webView,
53+
jsObjectName,
54+
)
55+
}
5456
}
5557

56-
override fun addWebMessageListener(
58+
override suspend fun addWebMessageListener(
5759
webView: WebView,
5860
jsObjectName: String,
5961
allowedOriginRules: Set<String>,
6062
listener: WebViewCompat.WebMessageListener,
6163
) {
62-
return WebViewCompat.addWebMessageListener(webView, jsObjectName, allowedOriginRules, listener)
64+
return withContext(dispatcherProvider.main()) {
65+
WebViewCompat.addWebMessageListener(webView, jsObjectName, allowedOriginRules, listener)
66+
}
6367
}
6468
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ interface WebViewCompatWrapper {
2828
allowedOriginRules: Set<String>,
2929
): ScriptHandler
3030

31-
fun removeWebMessageListener(
31+
suspend fun removeWebMessageListener(
3232
webView: WebView,
3333
jsObjectName: String,
3434
)
3535

36-
fun addWebMessageListener(
36+
suspend fun addWebMessageListener(
3737
webView: WebView,
3838
jsObjectName: String,
3939
allowedOriginRules: Set<String>,

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package com.duckduckgo.contentscopescripts.impl.messaging
1818

1919
import androidx.annotation.VisibleForTesting
2020
import androidx.webkit.WebViewCompat.WebMessageListener
21-
import com.duckduckgo.common.utils.DispatcherProvider
2221
import com.duckduckgo.common.utils.plugins.PluginPoint
2322
import com.duckduckgo.contentscopescripts.api.WebViewCompatContentScopeJsMessageHandlersPlugin
2423
import com.duckduckgo.contentscopescripts.impl.WebViewCompatContentScopeScripts
@@ -29,7 +28,6 @@ import com.duckduckgo.js.messaging.api.WebMessagingPlugin
2928
import com.squareup.anvil.annotations.ContributesMultibinding
3029
import com.squareup.moshi.Moshi
3130
import javax.inject.Inject
32-
import kotlinx.coroutines.withContext
3331
import logcat.LogPriority.ERROR
3432
import logcat.asLog
3533
import logcat.logcat
@@ -41,7 +39,6 @@ class WebViewCompatWebCompatMessagingPlugin @Inject constructor(
4139
private val handlers: PluginPoint<WebViewCompatContentScopeJsMessageHandlersPlugin>,
4240
private val globalHandlers: PluginPoint<GlobalContentScopeJsMessageHandlersPlugin>,
4341
private val webViewCompatContentScopeScripts: WebViewCompatContentScopeScripts,
44-
private val dispatcherProvider: DispatcherProvider,
4542
) : WebMessagingPlugin {
4643

4744
private val moshi = Moshi.Builder().add(JSONObjectAdapter()).build()
@@ -83,7 +80,7 @@ class WebViewCompatWebCompatMessagingPlugin @Inject constructor(
8380
jsMessageCallback: JsMessageCallback?,
8481
registerer: suspend (objectName: String, allowedOriginRules: Set<String>, webMessageListener: WebMessageListener) -> Boolean,
8582
) {
86-
if (withContext(dispatcherProvider.io()) { !webViewCompatContentScopeScripts.isEnabled() }) return
83+
if (!webViewCompatContentScopeScripts.isEnabled()) return
8784
if (jsMessageCallback == null) throw Exception("Callback cannot be null")
8885

8986
runCatching {
@@ -107,13 +104,11 @@ class WebViewCompatWebCompatMessagingPlugin @Inject constructor(
107104
unregisterer: suspend (objectName: String) -> Boolean,
108105
) {
109106
if (!webViewCompatContentScopeScripts.isEnabled()) return
110-
withContext(dispatcherProvider.main()) {
111-
runCatching {
112-
return@runCatching unregisterer(JS_OBJECT_NAME)
113-
}.getOrElse { exception ->
114-
logcat(ERROR) {
115-
"Error removing WebMessageListener for contentScopeAdsjs: ${exception.asLog()}"
116-
}
107+
runCatching {
108+
return@runCatching unregisterer(JS_OBJECT_NAME)
109+
}.getOrElse { exception ->
110+
logcat(ERROR) {
111+
"Error removing WebMessageListener for contentScopeAdsjs: ${exception.asLog()}"
117112
}
118113
}
119114
}

content-scope-scripts/content-scope-scripts-impl/src/test/java/com/duckduckgo/contentscopescripts/impl/messaging/WebViewCompatWebCompatMessagingPluginTest.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package com.duckduckgo.contentscopescripts.impl.messaging
1818

1919
import androidx.test.ext.junit.runners.AndroidJUnit4
2020
import androidx.webkit.WebViewCompat.WebMessageListener
21-
import com.duckduckgo.common.test.CoroutineTestRule
2221
import com.duckduckgo.common.utils.plugins.PluginPoint
2322
import com.duckduckgo.contentscopescripts.api.WebViewCompatContentScopeJsMessageHandlersPlugin
2423
import com.duckduckgo.contentscopescripts.impl.WebViewCompatContentScopeScripts
@@ -30,15 +29,13 @@ import junit.framework.TestCase.assertNull
3029
import kotlinx.coroutines.test.runTest
3130
import org.json.JSONObject
3231
import org.junit.Before
33-
import org.junit.Rule
3432
import org.junit.Test
3533
import org.junit.runner.RunWith
3634
import org.mockito.kotlin.mock
3735
import org.mockito.kotlin.whenever
3836

3937
@RunWith(AndroidJUnit4::class)
4038
class WebViewCompatWebCompatMessagingPluginTest {
41-
@get:Rule var coroutineRule = CoroutineTestRule()
4239

4340
private val webViewCompatContentScopeScripts: WebViewCompatContentScopeScripts = mock()
4441
private val handlers: PluginPoint<WebViewCompatContentScopeJsMessageHandlersPlugin> = FakePluginPoint()
@@ -97,7 +94,6 @@ class WebViewCompatWebCompatMessagingPluginTest {
9794
handlers = handlers,
9895
globalHandlers = globalHandlers,
9996
webViewCompatContentScopeScripts = webViewCompatContentScopeScripts,
100-
coroutineRule.testDispatcherProvider,
10197
)
10298
}
10399

0 commit comments

Comments
 (0)