Skip to content

Commit 8b6def6

Browse files
committed
Post messages from DDGWebView
1 parent e08c09e commit 8b6def6

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
@@ -478,11 +477,11 @@ class RealDuckDuckGoWebView : DuckDuckGoWebView, NestedScrollingChild3 {
478477
@SuppressLint("RequiresFeature")
479478
override suspend fun safePostMessage(
480479
replyProxy: JavaScriptReplyProxy,
481-
response: JSONObject,
480+
response: String,
482481
) {
483482
runCatching {
484483
if (webViewCapabilityChecker.isSupported(WebViewCapability.WebMessageListener)) {
485-
replyProxy.postMessage(response.toString())
484+
replyProxy.postMessage(response)
486485
}
487486
}.getOrElse { exception ->
488487
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,
@@ -59,6 +58,6 @@ abstract class DuckDuckGoWebView(
5958

6059
abstract suspend fun safePostMessage(
6160
replyProxy: JavaScriptReplyProxy,
62-
response: JSONObject,
61+
response: String,
6362
)
6463
}

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)