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