Skip to content

Commit 34ba275

Browse files
Fix ANR/Crash on autoconsent init handler (#6354)
Task/Issue URL: https://app.asana.com/1/137249556945/project/488551667048375/task/1210704553451145?focus=true ### Description This PR tries to fix and ANR and crash in the autoconsent init handler. It does remove the call to trimIndent() which is not very performant for long strings (and doesn't seem to be needed) and it moves all the logic to IO leaving only the webview call to evaluate JS in main. ### Steps to test this PR - [ ] Test autoconsent
1 parent 9c2eda6 commit 34ba275

File tree

6 files changed

+12
-21
lines changed

6 files changed

+12
-21
lines changed

autoconsent/autoconsent-impl/src/main/java/com/duckduckgo/autoconsent/impl/handlers/InitMessageHandlerPlugin.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import com.squareup.moshi.Moshi
3535
import javax.inject.Inject
3636
import kotlinx.coroutines.CoroutineScope
3737
import kotlinx.coroutines.launch
38+
import kotlinx.coroutines.withContext
3839
import logcat.logcat
3940

4041
@ContributesMultibinding(AppScope::class)
@@ -49,7 +50,7 @@ class InitMessageHandlerPlugin @Inject constructor(
4950

5051
override fun process(messageType: String, jsonString: String, webView: WebView, autoconsentCallback: AutoconsentCallback) {
5152
if (supportedTypes.contains(messageType)) {
52-
appCoroutineScope.launch(dispatcherProvider.main()) {
53+
appCoroutineScope.launch(dispatcherProvider.io()) {
5354
try {
5455
val message: InitMessage = parseMessage(jsonString) ?: return@launch
5556
val url = message.url
@@ -82,7 +83,9 @@ class InitMessageHandlerPlugin @Inject constructor(
8283

8384
val response = ReplyHandler.constructReply(getMessage(initResp))
8485

85-
webView.evaluateJavascript("javascript:$response", null)
86+
withContext(dispatcherProvider.main()) {
87+
webView.evaluateJavascript("javascript:$response", null)
88+
}
8689
} catch (e: Exception) {
8790
logcat { e.localizedMessage }
8891
}

autoconsent/autoconsent-impl/src/main/java/com/duckduckgo/autoconsent/impl/handlers/ReplyHandler.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ package com.duckduckgo.autoconsent.impl.handlers
1818

1919
object ReplyHandler {
2020
fun constructReply(message: String): String {
21-
return """
22-
(function() {
23-
window.autoconsentMessageCallback($message, window.origin);
24-
})();
25-
""".trimIndent()
21+
return "(function() {window.autoconsentMessageCallback($message, window.origin);})();"
2622
}
2723
}

autoconsent/autoconsent-impl/src/test/java/com/duckduckgo/autoconsent/impl/RealAutoconsentTest.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,7 @@ class RealAutoconsentTest {
117117

118118
@Test
119119
fun whenSetAutoconsentOptOutThenEvaluateJavascriptCalled() {
120-
val expected = """
121-
javascript:(function() {
122-
window.autoconsentMessageCallback({ "type": "optOut" }, window.origin);
123-
})();
124-
""".trimIndent()
120+
val expected = """javascript:(function() {window.autoconsentMessageCallback({ "type": "optOut" }, window.origin);})();"""
125121

126122
autoconsent.setAutoconsentOptOut(webView)
127123
assertEquals(expected, shadowOf(webView).lastEvaluatedJavascript)

autoconsent/autoconsent-impl/src/test/java/com/duckduckgo/autoconsent/impl/handlers/EvalMessageHandlerPluginTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ class EvalMessageHandlerPluginTest {
118118

119119
private fun jsonToEvalResp(json: String): EvalResp? {
120120
val trimmedJson = json
121-
.removePrefix("javascript:(function() {\n window.autoconsentMessageCallback(")
122-
.removeSuffix(", window.origin);\n})();")
121+
.removePrefix("javascript:(function() {window.autoconsentMessageCallback(")
122+
.removeSuffix(", window.origin);})();")
123123
val moshi = Moshi.Builder().add(JSONObjectAdapter()).build()
124124
val jsonAdapter: JsonAdapter<EvalResp> = moshi.adapter(EvalResp::class.java)
125125
return jsonAdapter.fromJson(trimmedJson)

autoconsent/autoconsent-impl/src/test/java/com/duckduckgo/autoconsent/impl/handlers/InitMessageHandlerPluginTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ class InitMessageHandlerPluginTest {
242242

243243
private fun jsonToInitResp(json: String): InitResp? {
244244
val trimmedJson = json
245-
.removePrefix("javascript:(function() {\n window.autoconsentMessageCallback(")
246-
.removeSuffix(", window.origin);\n})();")
245+
.removePrefix("javascript:(function() {window.autoconsentMessageCallback(")
246+
.removeSuffix(", window.origin);})();")
247247
val moshi = Moshi.Builder().add(JSONObjectAdapter()).build()
248248
val jsonAdapter: JsonAdapter<InitResp> = moshi.adapter(InitResp::class.java)
249249
return jsonAdapter.fromJson(trimmedJson)

autoconsent/autoconsent-impl/src/test/java/com/duckduckgo/autoconsent/impl/handlers/OptOutAndAutoconsentDoneMessageHandlerPluginTest.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,7 @@ class OptOutAndAutoconsentDoneMessageHandlerPluginTest {
9292

9393
@Test
9494
fun whenProcessOptOutWithSelfTestThenAutoconsentCallsEvaluateJavascript() {
95-
val expected = """
96-
javascript:(function() {
97-
window.autoconsentMessageCallback({ "type": "selfTest" }, window.origin);
98-
})();
99-
""".trimIndent()
95+
val expected = """javascript:(function() {window.autoconsentMessageCallback({ "type": "selfTest" }, window.origin);})();"""
10096

10197
handler.process(getOptOut(), optOutMessage(result = true, selfTest = true), webView, mockCallback)
10298
handler.process(getAutoconsentType(), autoconsentDoneMessage(), webView, mockCallback)

0 commit comments

Comments
 (0)