Skip to content

Commit a5c2c09

Browse files
committed
Post messages from DDGWebView
1 parent 7d60bcd commit a5c2c09

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
@@ -48,7 +48,6 @@ import javax.inject.Inject
4848
import logcat.LogPriority.ERROR
4949
import logcat.asLog
5050
import logcat.logcat
51-
import org.json.JSONObject
5251

5352
/**
5453
* WebView subclass which allows the WebView to
@@ -487,11 +486,11 @@ class RealDuckDuckGoWebView : DuckDuckGoWebView, NestedScrollingChild3 {
487486
@SuppressLint("RequiresFeature")
488487
override suspend fun safePostMessage(
489488
replyProxy: JavaScriptReplyProxy,
490-
response: JSONObject,
489+
response: String,
491490
) {
492491
runCatching {
493492
if (webViewCapabilityChecker.isSupported(WebViewCapability.WebMessageListener)) {
494-
replyProxy.postMessage(response.toString())
493+
replyProxy.postMessage(response)
495494
}
496495
}.getOrElse { exception ->
497496
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)