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