Skip to content

Commit 6c6f9a3

Browse files
committed
Keep fallback for subscription events
1 parent 0b1eeea commit 6c6f9a3

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,27 +1208,33 @@ class BrowserTabFragment :
12081208
private fun onOmnibarCustomTabPrivacyDashboardPressed() {
12091209
val params = PrivacyDashboardPrimaryScreen(tabId)
12101210
val intent = globalActivityStarter.startIntent(requireContext(), params)
1211-
webViewClient.postMessage(createBreakageReportingEventData())
1212-
// contentScopeScripts.sendSubscriptionEvent(createBreakageReportingEventData())
1211+
postBreakageReportingEvent()
12131212
intent?.let { activityResultPrivacyDashboard.launch(intent) }
12141213
pixel.fire(CustomTabPixelNames.CUSTOM_TABS_PRIVACY_DASHBOARD_OPENED)
12151214
}
12161215

1216+
private fun postBreakageReportingEvent() {
1217+
appCoroutineScope.launch {
1218+
val eventData = createBreakageReportingEventData()
1219+
webViewClient.postMessage(eventData) {
1220+
contentScopeScripts.sendSubscriptionEvent(eventData)
1221+
}
1222+
}
1223+
}
1224+
12171225
private fun onFireButtonPressed() {
12181226
val isFocusedNtp = omnibar.viewMode == ViewMode.NewTab && omnibar.getText().isEmpty() && omnibar.omnibarTextInput.hasFocus()
12191227
browserActivity?.launchFire(launchedFromFocusedNtp = isFocusedNtp)
12201228
viewModel.onFireMenuSelected()
12211229
}
12221230

12231231
private fun onBrowserMenuButtonPressed() {
1224-
webViewClient.postMessage(createBreakageReportingEventData())
1225-
// contentScopeScripts.sendSubscriptionEvent(createBreakageReportingEventData())
1232+
postBreakageReportingEvent()
12261233
viewModel.onBrowserMenuClicked(isCustomTab = isActiveCustomTab())
12271234
}
12281235

12291236
private fun onOmnibarPrivacyShieldButtonPressed() {
1230-
webViewClient.postMessage(createBreakageReportingEventData())
1231-
// contentScopeScripts.sendSubscriptionEvent(createBreakageReportingEventData())
1237+
postBreakageReportingEvent()
12321238
viewModel.onOmnibarPrivacyShieldButtonPressed()
12331239
launchPrivacyDashboard(toggle = false)
12341240
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,9 +783,14 @@ class BrowserWebViewClient @Inject constructor(
783783
}
784784
}
785785

786-
fun postMessage(eventData: SubscriptionEventData) {
786+
fun postMessage(
787+
eventData: SubscriptionEventData,
788+
fallback: () -> Unit
789+
) {
787790
webMessagingPlugins.getPlugins().forEach {
788-
it.postMessage(eventData)
791+
if (!it.postMessage(eventData)) {
792+
fallback()
793+
}
789794
}
790795
}
791796
}

content-scope-scripts/content-scope-scripts-api/src/main/java/com/duckduckgo/contentscopescripts/api/WebMessagingPlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ interface WebMessagingPlugin {
3030
unregisterer: suspend (objectName: String) -> Boolean,
3131
)
3232

33-
fun postMessage(subscriptionEventData: SubscriptionEventData)
33+
fun postMessage(subscriptionEventData: SubscriptionEventData): Boolean
3434
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ class WebCompatMessagingPlugin @Inject constructor(
151151
}
152152

153153
@SuppressLint("RequiresFeature")
154-
override fun postMessage(subscriptionEventData: SubscriptionEventData) {
155-
runCatching {
154+
override fun postMessage(subscriptionEventData: SubscriptionEventData): Boolean {
155+
return runCatching {
156156
val subscriptionEvent = SubscriptionEvent(
157157
context = context,
158158
featureName = subscriptionEventData.featureName,
@@ -162,7 +162,9 @@ class WebCompatMessagingPlugin @Inject constructor(
162162
moshi.adapter(SubscriptionEvent::class.java).toJson(it)
163163
}
164164

165-
globalReplyProxy?.postMessage(subscriptionEvent)
166-
}
165+
globalReplyProxy?.postMessage(subscriptionEvent)?.let {
166+
true
167+
} ?: false
168+
}.getOrElse { false }
167169
}
168170
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ package com.duckduckgo.js.messaging.api
1919
import org.json.JSONObject
2020

2121
interface WebViewCompatMessageCallback {
22+
/**
23+
* Processes a JavaScript message received by the WebView.
24+
*
25+
* This method is responsible for handling a message with the given parameters
26+
* and invoking a callback to send a response back to the JavaScript code.
27+
*
28+
* @param featureName The name of the feature associated with the message.
29+
* @param method The method name of the message.
30+
* @param id An optional identifier for the message.
31+
* @param data An optional JSON object containing additional data for the message.
32+
* @param onResponse A callback function to send a response back to the JavaScript code.
33+
*/
2234
fun process(
2335
featureName: String,
2436
method: String,

0 commit comments

Comments
 (0)