Skip to content

Commit

Permalink
Extract blob download in iframes logic to make it reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
CDRussell committed Oct 7, 2024
1 parent 27df391 commit 63c1596
Show file tree
Hide file tree
Showing 14 changed files with 330 additions and 267 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ import com.duckduckgo.autofill.api.passwordgeneration.AutomaticSavedLoginsMonito
import com.duckduckgo.autofill.impl.AutofillFireproofDialogSuppressor
import com.duckduckgo.browser.api.UserBrowserProperties
import com.duckduckgo.browser.api.brokensite.BrokenSiteContext
import com.duckduckgo.browser.api.download.WebViewBlobDownloader
import com.duckduckgo.common.test.CoroutineTestRule
import com.duckduckgo.common.test.InstantSchedulersRule
import com.duckduckgo.common.utils.DispatcherProvider
Expand Down Expand Up @@ -469,6 +470,8 @@ class BrowserTabViewModelTest {

private val subscriptions: Subscriptions = mock()

private val webViewBlobDownloader: WebViewBlobDownloader = mock()

private val privacyProtectionsPopupExperimentExternalPixels: PrivacyProtectionsPopupExperimentExternalPixels = mock {
runBlocking { whenever(mock.getPixelParams()).thenReturn(emptyMap()) }
}
Expand Down Expand Up @@ -639,6 +642,7 @@ class BrowserTabViewModelTest {
duckPlayer = mockDuckPlayer,
duckPlayerJSHelper = DuckPlayerJSHelper(mockDuckPlayer, mockAppBuildConfig, mockPixel, mockDuckDuckGoUrlDetector),
loadingBarExperimentManager = loadingBarExperimentManager,
webViewBlobDownloader = webViewBlobDownloader,
)

testee.loadData("abc", null, false, false)
Expand Down
55 changes: 4 additions & 51 deletions app/src/main/java/com/duckduckgo/app/browser/BrowserTabFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2369,7 +2369,7 @@ class BrowserTabFragment :

it.setDownloadListener { url, _, contentDisposition, mimeType, _ ->
lifecycleScope.launch(dispatchers.main()) {
viewModel.requestFileDownload(url, contentDisposition, mimeType, true, isBlobDownloadWebViewFeatureEnabled(it))
viewModel.requestFileDownload(url, contentDisposition, mimeType, true, isBlobDownloadWebViewFeatureEnabled())
}
}

Expand Down Expand Up @@ -2460,9 +2460,8 @@ class BrowserTabFragment :

private fun configureWebViewForBlobDownload(webView: DuckDuckGoWebView) {
lifecycleScope.launch(dispatchers.main()) {
if (isBlobDownloadWebViewFeatureEnabled(webView)) {
val script = blobDownloadScript()
WebViewCompat.addDocumentStartJavaScript(webView, script, setOf("*"))
if (isBlobDownloadWebViewFeatureEnabled()) {
viewModel.configureWebViewForBlobDownload(webView)

webView.safeAddWebMessageListener(
webViewCapabilityChecker,
Expand Down Expand Up @@ -2499,53 +2498,7 @@ class BrowserTabFragment :
}
}

private fun blobDownloadScript(): String {
val script = """
window.__url_to_blob_collection = {};
const original_createObjectURL = URL.createObjectURL;
URL.createObjectURL = function () {
const blob = arguments[0];
const url = original_createObjectURL.call(this, ...arguments);
if (blob instanceof Blob) {
__url_to_blob_collection[url] = blob;
}
return url;
}
function blobToBase64DataUrl(blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = function() {
resolve(reader.result);
}
reader.onerror = function() {
reject(new Error('Failed to read Blob object'));
}
reader.readAsDataURL(blob);
});
}
const pingMessage = 'Ping:' + window.location.href
ddgBlobDownloadObj.postMessage(pingMessage)
ddgBlobDownloadObj.onmessage = function(event) {
if (event.data.startsWith('blob:')) {
const blob = window.__url_to_blob_collection[event.data];
if (blob) {
blobToBase64DataUrl(blob).then((dataUrl) => {
ddgBlobDownloadObj.postMessage(dataUrl);
});
}
}
}
""".trimIndent()

return script
}

private suspend fun isBlobDownloadWebViewFeatureEnabled(webView: DuckDuckGoWebView): Boolean {
private suspend fun isBlobDownloadWebViewFeatureEnabled(): Boolean {
return withContext(dispatchers.io()) { webViewBlobDownloadFeature.self().isEnabled() } &&
webViewCapabilityChecker.isSupported(WebViewCapability.WebMessageListener) &&
webViewCapabilityChecker.isSupported(WebViewCapability.DocumentStartJavaScript)
Expand Down
64 changes: 11 additions & 53 deletions app/src/main/java/com/duckduckgo/app/browser/BrowserTabViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ import com.duckduckgo.autofill.impl.AutofillFireproofDialogSuppressor
import com.duckduckgo.browser.api.UserBrowserProperties
import com.duckduckgo.browser.api.brokensite.BrokenSiteData
import com.duckduckgo.browser.api.brokensite.BrokenSiteData.ReportFlow.MENU
import com.duckduckgo.browser.api.download.WebViewBlobDownloader
import com.duckduckgo.common.utils.AppUrl
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.common.utils.SingleLiveEvent
Expand Down Expand Up @@ -308,8 +309,6 @@ import kotlin.collections.List
import kotlin.collections.Map
import kotlin.collections.MutableMap
import kotlin.collections.any
import kotlin.collections.component1
import kotlin.collections.component2
import kotlin.collections.contains
import kotlin.collections.drop
import kotlin.collections.emptyList
Expand All @@ -319,7 +318,6 @@ import kotlin.collections.filterNot
import kotlin.collections.firstOrNull
import kotlin.collections.forEach
import kotlin.collections.isNotEmpty
import kotlin.collections.iterator
import kotlin.collections.map
import kotlin.collections.mapOf
import kotlin.collections.minus
Expand All @@ -330,7 +328,6 @@ import kotlin.collections.set
import kotlin.collections.setOf
import kotlin.collections.take
import kotlin.collections.toList
import kotlin.collections.toMutableMap
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
Expand Down Expand Up @@ -415,6 +412,7 @@ class BrowserTabViewModel @Inject constructor(
private val duckPlayer: DuckPlayer,
private val duckPlayerJSHelper: DuckPlayerJSHelper,
private val loadingBarExperimentManager: LoadingBarExperimentManager,
private val webViewBlobDownloader: WebViewBlobDownloader,
) : WebViewClientListener,
EditSavedSiteListener,
DeleteBookmarkListener,
Expand All @@ -426,11 +424,6 @@ class BrowserTabViewModel @Inject constructor(
private var hasUserSeenHistoryIAM = false
private var lastAutoCompleteState: AutoCompleteViewState? = null

private val replyProxyMap = mutableMapOf<String, JavaScriptReplyProxy>()

// Map<String, Map<String, JavaScriptReplyProxy>>() = Map<Origin, Map<location.href, JavaScriptReplyProxy>>()
private val fixedReplyProxyMap = mutableMapOf<String, Map<String, JavaScriptReplyProxy>>()

data class LocationPermission(
val origin: String,
val callback: GeolocationPermissions.Callback,
Expand Down Expand Up @@ -1375,7 +1368,7 @@ class BrowserTabViewModel @Inject constructor(
title: String?,
) {
Timber.v("Page changed: $url")
cleanupBlobDownloadReplyProxyMaps()
webViewBlobDownloader.clearReplyProxies()

hasCtaBeenShownForCurrentPage.set(false)
buildSiteFactory(url, title, urlUnchangedForExternalLaunchPurposes(site?.url, url))
Expand Down Expand Up @@ -1463,11 +1456,6 @@ class BrowserTabViewModel @Inject constructor(
}
}

private fun cleanupBlobDownloadReplyProxyMaps() {
fixedReplyProxyMap.clear()
replyProxyMap.clear()
}

private fun setAdClickActiveTabData(url: String?) {
val sourceTabId = tabRepository.liveSelectedTab.value?.sourceTabId
val sourceTabUrl = tabRepository.liveTabs.value?.firstOrNull { it.tabId == sourceTabId }?.url
Expand Down Expand Up @@ -2989,38 +2977,12 @@ class BrowserTabViewModel @Inject constructor(
command.postValue(RequestFileDownload(url, contentDisposition, mimeType, requestUserConfirmation))
}

@SuppressLint("RequiresFeature") // it's already checked in isBlobDownloadWebViewFeatureEnabled
private fun postMessageToConvertBlobToDataUri(url: String) {
appCoroutineScope.launch(dispatchers.main()) { // main because postMessage is not always safe in another thread
if (withContext(dispatchers.io()) { androidBrowserConfig.fixBlobDownloadWithIframes().isEnabled() }) {
for ((key, proxies) in fixedReplyProxyMap) {
if (sameOrigin(url.removePrefix("blob:"), key)) {
for (replyProxy in proxies.values) {
replyProxy.postMessage(url)
}
return@launch
}
}
} else {
for ((key, value) in replyProxyMap) {
if (sameOrigin(url.removePrefix("blob:"), key)) {
value.postMessage(url)
return@launch
}
}
}
viewModelScope.launch {
webViewBlobDownloader.convertBlobToDataUri(url)
}
}

private fun sameOrigin(firstUrl: String, secondUrl: String): Boolean {
return kotlin.runCatching {
val firstUri = Uri.parse(firstUrl)
val secondUri = Uri.parse(secondUrl)

firstUri.host == secondUri.host && firstUri.scheme == secondUri.scheme && firstUri.port == secondUri.port
}.getOrNull() ?: return false
}

fun showEmailProtectionChooseEmailPrompt(autofillWebMessageRequest: AutofillWebMessageRequest) {
emailManager.getEmailAddress()?.let {
command.postValue(ShowEmailProtectionChooseEmailPrompt(it, autofillWebMessageRequest))
Expand Down Expand Up @@ -3643,16 +3605,8 @@ class BrowserTabViewModel @Inject constructor(
}

fun saveReplyProxyForBlobDownload(originUrl: String, replyProxy: JavaScriptReplyProxy, locationHref: String? = null) {
appCoroutineScope.launch(dispatchers.io()) { // FF check has disk IO
if (androidBrowserConfig.fixBlobDownloadWithIframes().isEnabled()) {
val frameProxies = fixedReplyProxyMap[originUrl]?.toMutableMap() ?: mutableMapOf()
// if location.href is not passed, we fall back to origin
val safeLocationHref = locationHref ?: originUrl
frameProxies[safeLocationHref] = replyProxy
fixedReplyProxyMap[originUrl] = frameProxies
} else {
replyProxyMap[originUrl] = replyProxy
}
viewModelScope.launch {
webViewBlobDownloader.storeReplyProxy(originUrl, replyProxy, locationHref)
}
}

Expand Down Expand Up @@ -3725,6 +3679,10 @@ class BrowserTabViewModel @Inject constructor(
newTabPixels.get().fireNewTabDisplayed()
}

suspend fun configureWebViewForBlobDownload(webView: DuckDuckGoWebView) {
webViewBlobDownloader.addBlobDownloadSupport(webView)
}

companion object {
private const val FIXED_PROGRESS = 50

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import com.duckduckgo.app.browser.api.WebViewCapabilityChecker.WebViewCapability
import com.duckduckgo.browser.api.WebViewVersionProvider
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.common.utils.extensions.compareSemanticVersion
import com.duckduckgo.di.scopes.FragmentScope
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesBinding
import javax.inject.Inject
import kotlinx.coroutines.withContext

@ContributesBinding(FragmentScope::class)
@ContributesBinding(AppScope::class)
class RealWebViewCapabilityChecker @Inject constructor(
private val dispatchers: DispatcherProvider,
private val webViewVersionProvider: WebViewVersionProvider,
Expand Down
Loading

0 comments on commit 63c1596

Please sign in to comment.