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 21, 2024
1 parent 1b11c38 commit a9c3baf
Show file tree
Hide file tree
Showing 19 changed files with 645 additions and 300 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,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 @@ -322,7 +320,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 @@ -333,7 +330,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
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright (c) 2024 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.app.browser.downloader

import android.annotation.SuppressLint
import android.net.Uri
import android.webkit.WebView
import androidx.webkit.JavaScriptReplyProxy
import androidx.webkit.WebViewCompat
import com.duckduckgo.app.browser.api.WebViewCapabilityChecker
import com.duckduckgo.app.browser.api.WebViewCapabilityChecker.WebViewCapability
import com.duckduckgo.app.browser.webview.WebViewBlobDownloadFeature
import com.duckduckgo.app.pixels.remoteconfig.AndroidBrowserConfigFeature
import com.duckduckgo.browser.api.download.WebViewBlobDownloader
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesBinding
import javax.inject.Inject
import kotlinx.coroutines.withContext

@ContributesBinding(AppScope::class)
class WebViewBlobDownloaderModernImpl @Inject constructor(
private val webViewBlobDownloadFeature: WebViewBlobDownloadFeature,
private val dispatchers: DispatcherProvider,
private val webViewCapabilityChecker: WebViewCapabilityChecker,
private val androidBrowserConfig: AndroidBrowserConfigFeature,

) : WebViewBlobDownloader {

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>>()

@SuppressLint("RequiresFeature")
override suspend fun addBlobDownloadSupport(webView: WebView) {
if (isBlobDownloadWebViewFeatureEnabled()) {
WebViewCompat.addDocumentStartJavaScript(webView, script, setOf("*"))
}
}

@SuppressLint("RequiresFeature")
override suspend fun convertBlobToDataUri(blobUrl: String) {
if (withContext(dispatchers.io()) { androidBrowserConfig.fixBlobDownloadWithIframes().isEnabled() }) {
for ((key, proxies) in fixedReplyProxyMap) {
if (sameOrigin(blobUrl.removePrefix("blob:"), key)) {
for (replyProxy in proxies.values) {
replyProxy.postMessage(blobUrl)
}
return
}
}
} else {
for ((key, value) in replyProxyMap) {
if (sameOrigin(blobUrl.removePrefix("blob:"), key)) {
value.postMessage(blobUrl)
return
}
}
}
}

override suspend fun storeReplyProxy(
originUrl: String,
replyProxy: JavaScriptReplyProxy,
locationHref: String?,
) {
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
}
}

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
}

override fun clearReplyProxies() {
fixedReplyProxyMap.clear()
replyProxyMap.clear()
}

private suspend fun isBlobDownloadWebViewFeatureEnabled(): Boolean {
return withContext(dispatchers.io()) { webViewBlobDownloadFeature.self().isEnabled() } &&
webViewCapabilityChecker.isSupported(WebViewCapability.WebMessageListener) &&
webViewCapabilityChecker.isSupported(WebViewCapability.DocumentStartJavaScript)
}

companion object {
private 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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

package com.duckduckgo.autofill.api

import android.os.Parcelable
import com.duckduckgo.autofill.api.domain.app.LoginCredentials
import com.duckduckgo.navigation.api.GlobalActivityStarter.ActivityParams
import kotlinx.parcelize.Parcelize

sealed interface AutofillScreens {

Expand Down Expand Up @@ -55,23 +53,6 @@ sealed interface AutofillScreens {
data object AutofillImportViaGooglePasswordManagerScreen : ActivityParams {
private fun readResolve(): Any = AutofillImportViaGooglePasswordManagerScreen
}

sealed interface Result : Parcelable {

companion object {
const val RESULT_KEY = "importResult"
const val RESULT_KEY_DETAILS = "importResultDetails"
}

@Parcelize
data class Success(val importedCount: Int) : Result

@Parcelize
data class UserCancelled(val stage: String) : Result

@Parcelize
data object Error : Result
}
}
}

Expand Down
1 change: 1 addition & 0 deletions autofill/autofill-impl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ plugins {
id 'com.android.library'
id 'kotlin-android'
id 'com.squareup.anvil'
id 'kotlin-parcelize'
}

apply from: "$rootProject.projectDir/gradle/android-library.gradle"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,28 @@
package com.duckduckgo.autofill.impl.importing

import android.net.Uri
import android.os.Parcelable
import com.duckduckgo.autofill.api.domain.app.LoginCredentials
import com.duckduckgo.autofill.impl.importing.CsvPasswordImporter.ParseResult
import com.duckduckgo.autofill.impl.importing.CsvPasswordImporter.ParseResult.Success
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesBinding
import javax.inject.Inject
import kotlinx.coroutines.withContext
import kotlinx.parcelize.Parcelize

interface CsvPasswordImporter {
suspend fun readCsv(blob: String): List<LoginCredentials>
suspend fun readCsv(fileUri: Uri): List<LoginCredentials>
suspend fun readCsv(blob: String): ParseResult
suspend fun readCsv(fileUri: Uri): ParseResult

sealed interface ParseResult : Parcelable {
@Parcelize
data class Success(val numberPasswordsInSource: Int, val loginCredentialsToImport: List<LoginCredentials>) : ParseResult

@Parcelize
data object Error : ParseResult
}
}

@ContributesBinding(AppScope::class)
Expand All @@ -39,30 +51,30 @@ class GooglePasswordManagerCsvPasswordImporter @Inject constructor(
private val blobDecoder: GooglePasswordBlobDecoder,
) : CsvPasswordImporter {

override suspend fun readCsv(blob: String): List<LoginCredentials> {
override suspend fun readCsv(blob: String): ParseResult {
return kotlin.runCatching {
withContext(dispatchers.io()) {
val csv = blobDecoder.decode(blob)
importPasswords(csv)
convertToLoginCredentials(csv)
}
}.getOrElse { emptyList() }
}.getOrElse { ParseResult.Error }
}

override suspend fun readCsv(fileUri: Uri): List<LoginCredentials> {
override suspend fun readCsv(fileUri: Uri): ParseResult {
return kotlin.runCatching {
withContext(dispatchers.io()) {
val csv = fileReader.readCsvFile(fileUri)
importPasswords(csv)
convertToLoginCredentials(csv)
}
}.getOrElse { emptyList() }
}.getOrElse { ParseResult.Error }
}

private suspend fun importPasswords(csv: String): List<LoginCredentials> {
private suspend fun convertToLoginCredentials(csv: String): Success {
val allPasswords = parser.parseCsv(csv)
val dedupedPasswords = allPasswords.distinct()
val validPasswords = filterValidPasswords(dedupedPasswords)
val normalizedDomains = domainNameNormalizer.normalizeDomains(validPasswords)
return normalizedDomains
return Success(allPasswords.size, normalizedDomains)
}

private fun filterValidPasswords(passwords: List<LoginCredentials>): List<LoginCredentials> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,55 @@

package com.duckduckgo.autofill.impl.importing

import android.os.Parcelable
import com.duckduckgo.autofill.api.domain.app.LoginCredentials
import com.duckduckgo.autofill.impl.importing.PasswordImporter.ImportResult
import com.duckduckgo.autofill.impl.importing.PasswordImporter.ImportResult.Finished
import com.duckduckgo.autofill.impl.importing.PasswordImporter.ImportResult.InProgress
import com.duckduckgo.autofill.impl.store.InternalAutofillStore
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesBinding
import dagger.SingleInstanceIn
import javax.inject.Inject
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.withContext
import kotlinx.parcelize.Parcelize

interface PasswordImporter {
suspend fun importPasswords(importList: List<LoginCredentials>): ImportResult
suspend fun importPasswords(importList: List<LoginCredentials>)
fun getImportStatus(): Flow<ImportResult>

data class ImportResult(val savedCredentialIds: List<Long>, val duplicatedPasswords: List<LoginCredentials>)
sealed interface ImportResult : Parcelable {

@Parcelize
data class InProgress(
val savedCredentialIds: List<Long>,
val duplicatedPasswords: List<LoginCredentials>,
val importListSize: Int,
) : ImportResult

@Parcelize
data class Finished(
val savedCredentialIds: List<Long>,
val duplicatedPasswords: List<LoginCredentials>,
val importListSize: Int,
) : ImportResult
}
}

@SingleInstanceIn(AppScope::class)
@ContributesBinding(AppScope::class)
class PasswordImporterImpl @Inject constructor(
private val existingPasswordMatchDetector: ExistingPasswordMatchDetector,
private val autofillStore: InternalAutofillStore,
private val dispatchers: DispatcherProvider,
) : PasswordImporter {

override suspend fun importPasswords(importList: List<LoginCredentials>): ImportResult {
private val _importStatus = MutableSharedFlow<ImportResult>(replay = 1)

override suspend fun importPasswords(importList: List<LoginCredentials>) {
return withContext(dispatchers.io()) {
val savedCredentialIds = mutableListOf<Long>()
val duplicatedPasswords = mutableListOf<LoginCredentials>()
Expand All @@ -53,9 +79,15 @@ class PasswordImporterImpl @Inject constructor(
} else {
duplicatedPasswords.add(it)
}

_importStatus.emit(InProgress(savedCredentialIds, duplicatedPasswords, importList.size))
}

ImportResult(savedCredentialIds, duplicatedPasswords)
_importStatus.emit(Finished(savedCredentialIds, duplicatedPasswords, importList.size))
}
}

override fun getImportStatus(): Flow<ImportResult> {
return _importStatus
}
}
Loading

0 comments on commit a9c3baf

Please sign in to comment.