@@ -26,6 +26,7 @@ import com.duckduckgo.common.utils.plugins.PluginPoint
26
26
import com.duckduckgo.contentscopescripts.api.AdsjsContentScopeJsMessageHandlersPlugin
27
27
import com.duckduckgo.contentscopescripts.api.GlobalContentScopeJsMessageHandlersPlugin
28
28
import com.duckduckgo.contentscopescripts.impl.AdsJsContentScopeScripts
29
+ import com.duckduckgo.contentscopescripts.impl.ContentScopeScriptsFeature
29
30
import com.duckduckgo.di.scopes.ActivityScope
30
31
import com.duckduckgo.js.messaging.api.AdsjsJsMessageCallback
31
32
import com.duckduckgo.js.messaging.api.AdsjsMessaging
@@ -55,6 +56,7 @@ class AdsjsContentScopeMessaging @Inject constructor(
55
56
private val adsJsContentScopeScripts : AdsJsContentScopeScripts ,
56
57
private val dispatcherProvider : DispatcherProvider ,
57
58
@AppCoroutineScope private val appCoroutineScope : CoroutineScope ,
59
+ private val contentScopeScriptsFeature : ContentScopeScriptsFeature ,
58
60
) : AdsjsMessaging {
59
61
60
62
private val moshi = Moshi .Builder ().add(JSONObjectAdapter ()).build()
@@ -80,6 +82,7 @@ class AdsjsContentScopeMessaging @Inject constructor(
80
82
if (context == jsMessage.context) {
81
83
// Setup reply proxy so we can send subscription events
82
84
if (jsMessage.featureName == " messaging" || jsMessage.method == " initialPing" ) {
85
+ logcat(" Cris" ) { " initialPing" }
83
86
globalReplyProxy = replyProxy
84
87
}
85
88
@@ -159,6 +162,9 @@ class AdsjsContentScopeMessaging @Inject constructor(
159
162
put(" featureName" , response.featureName)
160
163
put(" context" , context)
161
164
}
165
+
166
+ logcat(" Cris" ) { " Sending response: $responseWithId " }
167
+
162
168
appCoroutineScope.launch(dispatcherProvider.main()) {
163
169
(webView as ? DuckDuckGoWebView )?.safePostMessage(
164
170
replyProxy,
@@ -169,26 +175,31 @@ class AdsjsContentScopeMessaging @Inject constructor(
169
175
}
170
176
171
177
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
+ }
181
181
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
+ )
189
201
}
190
- }.getOrElse {
191
- false
202
+ return @withContext true
192
203
}
193
204
}
194
205
}
0 commit comments