Skip to content

Commit 02ad05a

Browse files
committed
Post messages from DDGWebView
1 parent 2bb3f65 commit 02ad05a

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ import javax.inject.Inject
4747
import logcat.LogPriority.ERROR
4848
import logcat.asLog
4949
import logcat.logcat
50-
import org.json.JSONObject
5150

5251
/**
5352
* WebView subclass which allows the WebView to
@@ -482,11 +481,11 @@ class RealDuckDuckGoWebView : DuckDuckGoWebView, NestedScrollingChild3 {
482481
@SuppressLint("RequiresFeature")
483482
override suspend fun safePostMessage(
484483
replyProxy: JavaScriptReplyProxy,
485-
response: JSONObject,
484+
response: String,
486485
) {
487486
runCatching {
488487
if (webViewCapabilityChecker.isSupported(WebViewCapability.WebMessageListener)) {
489-
replyProxy.postMessage(response.toString())
488+
replyProxy.postMessage(response)
490489
}
491490
}.getOrElse { exception ->
492491
logcat(ERROR) { "Error posting message: ${exception.asLog()}" }

browser-api/src/main/java/com/duckduckgo/app/browser/api/DuckDuckGoWebView.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import android.webkit.WebView
2626
import androidx.webkit.JavaScriptReplyProxy
2727
import androidx.webkit.ScriptHandler
2828
import androidx.webkit.WebViewCompat.WebMessageListener
29-
import org.json.JSONObject
3029

3130
abstract class DuckDuckGoWebView(
3231
context: Context,
@@ -61,6 +60,6 @@ abstract class DuckDuckGoWebView(
6160

6261
abstract suspend fun safePostMessage(
6362
replyProxy: JavaScriptReplyProxy,
64-
response: JSONObject,
63+
response: String,
6564
)
6665
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,12 @@ class AdsjsContentScopeMessaging @Inject constructor(
163163
appCoroutineScope.launch(dispatcherProvider.main()) {
164164
(webView as? DuckDuckGoWebView)?.safePostMessage(
165165
replyProxy,
166-
responseWithId,
166+
responseWithId.toString(),
167167
)
168168
}
169169
}
170170
}
171171

172-
@SuppressLint("RequiresFeature")
173172
override fun postMessage(subscriptionEventData: SubscriptionEventData) {
174173
runCatching {
175174
val subscriptionEvent = SubscriptionEvent(
@@ -181,7 +180,14 @@ class AdsjsContentScopeMessaging @Inject constructor(
181180
moshi.adapter(SubscriptionEvent::class.java).toJson(it)
182181
}
183182

184-
globalReplyProxy?.postMessage(subscriptionEvent)
183+
appCoroutineScope.launch(dispatcherProvider.main()) {
184+
globalReplyProxy?.let {
185+
(webView as? DuckDuckGoWebView)?.safePostMessage(
186+
it,
187+
subscriptionEvent,
188+
)
189+
}
190+
}
185191
}
186192
}
187193
}

0 commit comments

Comments
 (0)