Skip to content

Commit 168009b

Browse files
committed
Post messages from DDGWebView
1 parent 65094db commit 168009b

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
@@ -160,13 +160,12 @@ class AdsjsContentScopeMessaging @Inject constructor(
160160
appCoroutineScope.launch(dispatcherProvider.main()) {
161161
(webView as? DuckDuckGoWebView)?.safePostMessage(
162162
replyProxy,
163-
responseWithId,
163+
responseWithId.toString(),
164164
)
165165
}
166166
}
167167
}
168168

169-
@SuppressLint("RequiresFeature")
170169
override fun postMessage(subscriptionEventData: SubscriptionEventData) {
171170
runCatching {
172171
val subscriptionEvent = SubscriptionEvent(
@@ -178,7 +177,14 @@ class AdsjsContentScopeMessaging @Inject constructor(
178177
moshi.adapter(SubscriptionEvent::class.java).toJson(it)
179178
}
180179

181-
globalReplyProxy?.postMessage(subscriptionEvent)
180+
appCoroutineScope.launch(dispatcherProvider.main()) {
181+
globalReplyProxy?.let {
182+
(webView as? DuckDuckGoWebView)?.safePostMessage(
183+
it,
184+
subscriptionEvent,
185+
)
186+
}
187+
}
182188
}
183189
}
184190
}

0 commit comments

Comments
 (0)