Skip to content

Commit 8d30eb3

Browse files
committed
Refine postMessage fallback
1 parent a96c50a commit 8d30eb3

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -487,17 +487,13 @@ class RealDuckDuckGoWebView : DuckDuckGoWebView, NestedScrollingChild3 {
487487
override suspend fun safePostMessage(
488488
replyProxy: JavaScriptReplyProxy,
489489
response: String,
490-
): Boolean {
491-
return runCatching {
492-
return if (webViewCapabilityChecker.isSupported(WebViewCapability.WebMessageListener)) {
490+
) {
491+
runCatching {
492+
if (webViewCapabilityChecker.isSupported(WebViewCapability.WebMessageListener)) {
493493
replyProxy.postMessage(response)
494-
true
495-
} else {
496-
false
497494
}
498495
}.getOrElse { exception ->
499496
logcat(ERROR) { "Error posting message: ${exception.asLog()}" }
500-
false
501497
}
502498
}
503499

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
@@ -90,10 +90,9 @@ abstract class DuckDuckGoWebView(
9090
*
9191
* @param replyProxy The proxy to send the message to.
9292
* @param response The message content to send.
93-
* @return `true` if the message was sent successfully, `false` otherwise.
9493
*/
9594
abstract suspend fun safePostMessage(
9695
replyProxy: JavaScriptReplyProxy,
9796
response: String,
98-
): Boolean
97+
)
9998
}

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

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import com.duckduckgo.common.utils.plugins.PluginPoint
2626
import com.duckduckgo.contentscopescripts.api.AdsjsContentScopeJsMessageHandlersPlugin
2727
import com.duckduckgo.contentscopescripts.api.GlobalContentScopeJsMessageHandlersPlugin
2828
import com.duckduckgo.contentscopescripts.impl.AdsJsContentScopeScripts
29+
import com.duckduckgo.contentscopescripts.impl.ContentScopeScriptsFeature
2930
import com.duckduckgo.di.scopes.ActivityScope
3031
import com.duckduckgo.js.messaging.api.AdsjsJsMessageCallback
3132
import com.duckduckgo.js.messaging.api.AdsjsMessaging
@@ -55,6 +56,7 @@ class AdsjsContentScopeMessaging @Inject constructor(
5556
private val adsJsContentScopeScripts: AdsJsContentScopeScripts,
5657
private val dispatcherProvider: DispatcherProvider,
5758
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
59+
private val contentScopeScriptsFeature: ContentScopeScriptsFeature,
5860
) : AdsjsMessaging {
5961

6062
private val moshi = Moshi.Builder().add(JSONObjectAdapter()).build()
@@ -80,6 +82,7 @@ class AdsjsContentScopeMessaging @Inject constructor(
8082
if (context == jsMessage.context) {
8183
// Setup reply proxy so we can send subscription events
8284
if (jsMessage.featureName == "messaging" || jsMessage.method == "initialPing") {
85+
logcat("Cris") { "initialPing" }
8386
globalReplyProxy = replyProxy
8487
}
8588

@@ -159,6 +162,9 @@ class AdsjsContentScopeMessaging @Inject constructor(
159162
put("featureName", response.featureName)
160163
put("context", context)
161164
}
165+
166+
logcat("Cris") { "Sending response: $responseWithId" }
167+
162168
appCoroutineScope.launch(dispatcherProvider.main()) {
163169
(webView as? DuckDuckGoWebView)?.safePostMessage(
164170
replyProxy,
@@ -169,26 +175,31 @@ class AdsjsContentScopeMessaging @Inject constructor(
169175
}
170176

171177
override suspend fun postMessage(subscriptionEventData: SubscriptionEventData): Boolean {
172-
return runCatching {
173-
val subscriptionEvent = SubscriptionEvent(
174-
context = context,
175-
featureName = subscriptionEventData.featureName,
176-
subscriptionName = subscriptionEventData.subscriptionName,
177-
params = subscriptionEventData.params,
178-
).let {
179-
moshi.adapter(SubscriptionEvent::class.java).toJson(it)
180-
}
178+
val newWebCompatApisEnabled = withContext(dispatcherProvider.io()) {
179+
contentScopeScriptsFeature.useNewWebCompatApis().isEnabled()
180+
}
181181

182-
withContext(dispatcherProvider.main()) {
183-
globalReplyProxy?.let {
184-
(webView as? DuckDuckGoWebView)?.safePostMessage(
185-
it,
186-
subscriptionEvent,
187-
)
188-
} ?: false
182+
if (!newWebCompatApisEnabled) {
183+
return false
184+
}
185+
186+
val subscriptionEvent = SubscriptionEvent(
187+
context = context,
188+
featureName = subscriptionEventData.featureName,
189+
subscriptionName = subscriptionEventData.subscriptionName,
190+
params = subscriptionEventData.params,
191+
).let {
192+
moshi.adapter(SubscriptionEvent::class.java).toJson(it)
193+
}
194+
195+
return withContext(dispatcherProvider.main()) {
196+
globalReplyProxy?.let {
197+
(webView as? DuckDuckGoWebView)?.safePostMessage(
198+
it,
199+
subscriptionEvent,
200+
)
189201
}
190-
}.getOrElse {
191-
false
202+
return@withContext true
192203
}
193204
}
194205
}

js-messaging/js-messaging-api/src/main/java/com/duckduckgo/js/messaging/api/AdsjsMessaging.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ interface AdsjsMessaging {
4242
/**
4343
* Sends a message to the JS layer.
4444
* @param subscriptionEventData Data to be sent.
45-
* @return true if the message was sent successfully, false otherwise.
4645
*/
4746
suspend fun postMessage(subscriptionEventData: SubscriptionEventData): Boolean
4847

0 commit comments

Comments
 (0)