|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 DuckDuckGo |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.duckduckgo.app.browser |
| 18 | + |
| 19 | +import com.duckduckgo.app.browser.webview.WebViewCompatFeature |
| 20 | +import com.duckduckgo.app.browser.webview.WebViewCompatFeatureSettings |
| 21 | +import com.duckduckgo.browser.api.webviewcompat.WebViewCompatWrapper |
| 22 | +import com.duckduckgo.common.utils.DispatcherProvider |
| 23 | +import com.duckduckgo.di.scopes.FragmentScope |
| 24 | +import com.squareup.anvil.annotations.ContributesBinding |
| 25 | +import com.squareup.moshi.Moshi |
| 26 | +import dagger.SingleInstanceIn |
| 27 | +import kotlinx.coroutines.withContext |
| 28 | +import javax.inject.Inject |
| 29 | + |
| 30 | +private const val delay = "\$DELAY$" |
| 31 | +private const val postInitialPing = "\$POST_INITIAL_PING$" |
| 32 | +private const val replyToNativeMessages = "\$REPLY_TO_NATIVE_MESSAGES$" |
| 33 | + |
| 34 | +interface WebViewCompatTestHelper { |
| 35 | + suspend fun configureWebViewForWebViewCompatTest(webView: DuckDuckGoWebView) |
| 36 | +} |
| 37 | + |
| 38 | +@ContributesBinding(FragmentScope::class) |
| 39 | +@SingleInstanceIn(FragmentScope::class) |
| 40 | +class RealWebViewCompatTestHelper @Inject constructor( |
| 41 | + private val dispatchers: DispatcherProvider, |
| 42 | + private val webViewCompatFeature: WebViewCompatFeature, |
| 43 | + private val webViewCompatWrapper: WebViewCompatWrapper, |
| 44 | + moshi: Moshi, |
| 45 | +) : WebViewCompatTestHelper { |
| 46 | + |
| 47 | + private val adapter = moshi.adapter(WebViewCompatFeatureSettings::class.java) |
| 48 | + |
| 49 | + override suspend fun configureWebViewForWebViewCompatTest(webView: DuckDuckGoWebView) { |
| 50 | + val script = withContext(dispatchers.io()) { |
| 51 | + if (!webViewCompatFeature.self().isEnabled()) return@withContext null |
| 52 | + |
| 53 | + val webViewCompatSettings = webViewCompatFeature.self().getSettings()?.let { |
| 54 | + adapter.fromJson(it) |
| 55 | + } |
| 56 | + webView.resources?.openRawResource(R.raw.webviewcompat_test_script)?.bufferedReader().use { it?.readText() }.orEmpty() |
| 57 | + .replace(delay, webViewCompatSettings?.jsInitialPingDelay?.toString() ?: "0") |
| 58 | + .replace(postInitialPing, webViewCompatFeature.jsSendsInitialPing().isEnabled().toString()) |
| 59 | + .replace(replyToNativeMessages, webViewCompatFeature.jsRepliesToNativeMessages().isEnabled().toString()) |
| 60 | + } ?: return |
| 61 | + |
| 62 | + withContext(dispatchers.main()) { |
| 63 | + webViewCompatWrapper.addDocumentStartJavaScript(webView, script, setOf("*")) |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments