|
16 | 16 |
|
17 | 17 | package com.duckduckgo.contentscopescripts.impl.messaging
|
18 | 18 |
|
19 |
| -import android.annotation.SuppressLint |
20 |
| -import android.webkit.WebView |
21 |
| -import androidx.annotation.VisibleForTesting |
22 |
| -import androidx.webkit.JavaScriptReplyProxy |
23 |
| -import com.duckduckgo.app.di.AppCoroutineScope |
24 |
| -import com.duckduckgo.browser.api.webviewcompat.WebViewCompatWrapper |
25 |
| -import com.duckduckgo.common.utils.DispatcherProvider |
26 | 19 | import com.duckduckgo.common.utils.plugins.PluginPoint
|
27 | 20 | import com.duckduckgo.contentscopescripts.api.WebViewCompatContentScopeJsMessageHandlersPlugin
|
28 | 21 | import com.duckduckgo.contentscopescripts.impl.WebViewCompatContentScopeScripts
|
29 | 22 | import com.duckduckgo.di.scopes.FragmentScope
|
30 |
| -import com.duckduckgo.js.messaging.api.JsCallbackData |
31 |
| -import com.duckduckgo.js.messaging.api.JsMessage |
32 |
| -import com.duckduckgo.js.messaging.api.ProcessResult.SendResponse |
33 |
| -import com.duckduckgo.js.messaging.api.ProcessResult.SendToConsumer |
34 |
| -import com.duckduckgo.js.messaging.api.SubscriptionEvent |
35 |
| -import com.duckduckgo.js.messaging.api.SubscriptionEventData |
| 23 | +import com.duckduckgo.js.messaging.api.GlobalJsMessageHandler |
36 | 24 | import com.duckduckgo.js.messaging.api.WebMessagingPlugin
|
37 |
| -import com.duckduckgo.js.messaging.api.WebViewCompatMessageCallback |
| 25 | +import com.duckduckgo.js.messaging.api.WebMessagingPluginDelegate |
| 26 | +import com.duckduckgo.js.messaging.api.WebMessagingPluginStrategy |
| 27 | +import com.duckduckgo.js.messaging.api.WebViewCompatMessageHandler |
38 | 28 | import com.squareup.anvil.annotations.ContributesBinding
|
39 | 29 | import com.squareup.anvil.annotations.ContributesMultibinding
|
40 |
| -import com.squareup.moshi.Moshi |
41 | 30 | import dagger.SingleInstanceIn
|
42 | 31 | import javax.inject.Inject
|
43 | 32 | import javax.inject.Named
|
44 |
| -import kotlinx.coroutines.CoroutineScope |
45 |
| -import kotlinx.coroutines.launch |
46 |
| -import kotlinx.coroutines.withContext |
47 |
| -import logcat.LogPriority.ERROR |
48 |
| -import logcat.asLog |
49 |
| -import logcat.logcat |
50 |
| -import org.json.JSONObject |
51 |
| - |
52 |
| -private const val JS_OBJECT_NAME = "contentScopeAdsjs" |
53 | 33 |
|
54 | 34 | @Named("contentScopeScripts")
|
55 | 35 | @SingleInstanceIn(FragmentScope::class)
|
56 | 36 | @ContributesBinding(FragmentScope::class)
|
57 | 37 | @ContributesMultibinding(scope = FragmentScope::class, ignoreQualifier = true)
|
58 | 38 | class ContentScopeScriptsWebMessagingPlugin @Inject constructor(
|
59 |
| - private val handlers: PluginPoint<WebViewCompatContentScopeJsMessageHandlersPlugin>, |
60 |
| - private val globalHandlers: PluginPoint<GlobalContentScopeJsMessageHandlersPlugin>, |
61 |
| - private val webViewCompatContentScopeScripts: WebViewCompatContentScopeScripts, |
62 |
| - private val webViewCompatWrapper: WebViewCompatWrapper, |
63 |
| - private val dispatcherProvider: DispatcherProvider, |
64 |
| - @AppCoroutineScope private val appCoroutineScope: CoroutineScope, |
65 |
| -) : WebMessagingPlugin { |
66 |
| - |
67 |
| - private val moshi = Moshi.Builder().add(JSONObjectAdapter()).build() |
68 |
| - |
69 |
| - override val context: String = "contentScopeScripts" |
70 |
| - private val allowedDomains: Set<String> = setOf("*") |
71 |
| - |
72 |
| - private var globalReplyProxy: JavaScriptReplyProxy? = null |
73 |
| - |
74 |
| - @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) |
75 |
| - internal fun process( |
76 |
| - message: String, |
77 |
| - jsMessageCallback: WebViewCompatMessageCallback, |
78 |
| - replyProxy: JavaScriptReplyProxy, |
79 |
| - ) { |
80 |
| - try { |
81 |
| - val adapter = moshi.adapter(JsMessage::class.java) |
82 |
| - val jsMessage = adapter.fromJson(message) |
83 |
| - |
84 |
| - jsMessage?.let { |
85 |
| - if (context == jsMessage.context) { |
86 |
| - // Setup reply proxy so we can send subscription events |
87 |
| - if (jsMessage.featureName == "messaging" && jsMessage.method == "initialPing") { |
88 |
| - globalReplyProxy = replyProxy |
89 |
| - } |
90 |
| - |
91 |
| - // Process global handlers first (always processed regardless of feature handlers) |
92 |
| - globalHandlers.getPlugins() |
93 |
| - .map { it.getGlobalJsMessageHandler() } |
94 |
| - .filter { it.method == jsMessage.method } |
95 |
| - .forEach { handler -> |
96 |
| - handler.process(jsMessage)?.let { processResult -> |
97 |
| - when (processResult) { |
98 |
| - is SendToConsumer -> { |
99 |
| - sendToConsumer(jsMessageCallback, jsMessage, replyProxy) |
100 |
| - } |
101 |
| - is SendResponse -> { |
102 |
| - onResponse(jsMessage, replyProxy) |
103 |
| - } |
104 |
| - } |
105 |
| - } |
106 |
| - } |
107 |
| - |
108 |
| - // Process with feature handlers |
109 |
| - handlers.getPlugins().map { it.getJsMessageHandler() }.firstOrNull { |
110 |
| - it.methods.contains(jsMessage.method) && it.featureName == jsMessage.featureName |
111 |
| - }?.process(jsMessage)?.let { processResult -> |
112 |
| - when (processResult) { |
113 |
| - is SendToConsumer -> { |
114 |
| - sendToConsumer(jsMessageCallback, jsMessage, replyProxy) |
115 |
| - } |
116 |
| - is SendResponse -> { |
117 |
| - onResponse(jsMessage, replyProxy) |
118 |
| - } |
119 |
| - } |
120 |
| - } |
121 |
| - } |
122 |
| - } |
123 |
| - } catch (e: Exception) { |
124 |
| - logcat(ERROR) { "Exception is ${e.asLog()}" } |
| 39 | + handlers: PluginPoint<WebViewCompatContentScopeJsMessageHandlersPlugin>, |
| 40 | + globalHandlers: PluginPoint<GlobalContentScopeJsMessageHandlersPlugin>, |
| 41 | + webViewCompatContentScopeScripts: WebViewCompatContentScopeScripts, |
| 42 | + webMessagingPluginDelegate: WebMessagingPluginDelegate, |
| 43 | +) : WebMessagingPlugin by webMessagingPluginDelegate.createPlugin( |
| 44 | + object : WebMessagingPluginStrategy { |
| 45 | + override val context: String = "contentScopeScripts" |
| 46 | + override val allowedDomains: Set<String> = setOf("*") |
| 47 | + override val objectName: String |
| 48 | + get() = "contentScopeAdsjs" |
| 49 | + |
| 50 | + override suspend fun isEnabled(): Boolean { |
| 51 | + return webViewCompatContentScopeScripts.isEnabled() |
125 | 52 | }
|
126 |
| - } |
127 |
| - |
128 |
| - private fun onResponse( |
129 |
| - jsMessage: JsMessage, |
130 |
| - replyProxy: JavaScriptReplyProxy, |
131 |
| - ) { |
132 |
| - val callbackData = JsCallbackData( |
133 |
| - id = jsMessage.id ?: "", |
134 |
| - params = jsMessage.params, |
135 |
| - featureName = jsMessage.featureName, |
136 |
| - method = jsMessage.method, |
137 |
| - ) |
138 |
| - onResponse(callbackData, replyProxy) |
139 |
| - } |
140 | 53 |
|
141 |
| - private fun sendToConsumer( |
142 |
| - jsMessageCallback: WebViewCompatMessageCallback, |
143 |
| - jsMessage: JsMessage, |
144 |
| - replyProxy: JavaScriptReplyProxy, |
145 |
| - ) { |
146 |
| - jsMessageCallback.process( |
147 |
| - context = context, |
148 |
| - featureName = jsMessage.featureName, |
149 |
| - method = jsMessage.method, |
150 |
| - id = jsMessage.id ?: "", |
151 |
| - data = jsMessage.params, |
152 |
| - onResponse = { response: JSONObject -> |
153 |
| - val callbackData = JsCallbackData( |
154 |
| - id = jsMessage.id ?: "", |
155 |
| - params = response, |
156 |
| - featureName = jsMessage.featureName, |
157 |
| - method = jsMessage.method, |
158 |
| - ) |
159 |
| - onResponse(callbackData, replyProxy) |
160 |
| - }, |
161 |
| - ) |
162 |
| - } |
163 |
| - |
164 |
| - override fun register( |
165 |
| - jsMessageCallback: WebViewCompatMessageCallback, |
166 |
| - webView: WebView, |
167 |
| - ) { |
168 |
| - appCoroutineScope.launch { |
169 |
| - if (!webViewCompatContentScopeScripts.isEnabled()) { |
170 |
| - return@launch |
171 |
| - } |
172 |
| - |
173 |
| - runCatching { |
174 |
| - return@runCatching webViewCompatWrapper.addWebMessageListener( |
175 |
| - webView, |
176 |
| - JS_OBJECT_NAME, |
177 |
| - allowedDomains, |
178 |
| - ) { _, message, _, _, replyProxy -> |
179 |
| - process( |
180 |
| - message.data ?: "", |
181 |
| - jsMessageCallback, |
182 |
| - replyProxy, |
183 |
| - ) |
184 |
| - } |
185 |
| - }.getOrElse { exception -> |
186 |
| - logcat(ERROR) { "Error adding WebMessageListener for contentScopeAdsjs: ${exception.asLog()}" } |
187 |
| - } |
188 |
| - } |
189 |
| - } |
190 |
| - |
191 |
| - override fun unregister( |
192 |
| - webView: WebView, |
193 |
| - ) { |
194 |
| - appCoroutineScope.launch { |
195 |
| - if (!webViewCompatContentScopeScripts.isEnabled()) return@launch |
196 |
| - runCatching { |
197 |
| - return@runCatching webViewCompatWrapper.removeWebMessageListener(webView, JS_OBJECT_NAME) |
198 |
| - }.getOrElse { exception -> |
199 |
| - logcat(ERROR) { |
200 |
| - "Error removing WebMessageListener for contentScopeAdsjs: ${exception.asLog()}" |
201 |
| - } |
202 |
| - } |
| 54 | + override fun getMessageHandlers(): List<WebViewCompatMessageHandler> { |
| 55 | + return handlers.getPlugins().map { it.getJsMessageHandler() } |
203 | 56 | }
|
204 |
| - } |
205 |
| - |
206 |
| - @SuppressLint("RequiresFeature") |
207 |
| - private fun onResponse(response: JsCallbackData, replyProxy: JavaScriptReplyProxy) { |
208 |
| - runCatching { |
209 |
| - val responseWithId = JSONObject().apply { |
210 |
| - put("id", response.id) |
211 |
| - put("result", response.params) |
212 |
| - put("featureName", response.featureName) |
213 |
| - put("context", context) |
214 |
| - } |
215 |
| - appCoroutineScope.launch(dispatcherProvider.main()) { |
216 |
| - replyProxy.postMessage(responseWithId.toString()) |
217 |
| - } |
218 |
| - } |
219 |
| - } |
220 |
| - |
221 |
| - @SuppressLint("RequiresFeature") |
222 |
| - override fun postMessage(subscriptionEventData: SubscriptionEventData) { |
223 |
| - runCatching { |
224 |
| - appCoroutineScope.launch { |
225 |
| - if (!webViewCompatContentScopeScripts.isEnabled()) { |
226 |
| - return@launch |
227 |
| - } |
228 |
| - |
229 |
| - val subscriptionEvent = SubscriptionEvent( |
230 |
| - context = context, |
231 |
| - featureName = subscriptionEventData.featureName, |
232 |
| - subscriptionName = subscriptionEventData.subscriptionName, |
233 |
| - params = subscriptionEventData.params, |
234 |
| - ).let { |
235 |
| - moshi.adapter(SubscriptionEvent::class.java).toJson(it) |
236 |
| - } |
237 | 57 |
|
238 |
| - withContext(dispatcherProvider.main()) { |
239 |
| - globalReplyProxy?.postMessage(subscriptionEvent) |
240 |
| - } |
241 |
| - } |
| 58 | + override fun getGlobalMessageHandler(): List<GlobalJsMessageHandler> { |
| 59 | + return globalHandlers.getPlugins().map { it.getGlobalJsMessageHandler() } |
242 | 60 | }
|
243 |
| - } |
244 |
| -} |
| 61 | + }, |
| 62 | +) |
0 commit comments