-
Notifications
You must be signed in to change notification settings - Fork 921
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Autofill: Increase ratio of complete credential saves
- Loading branch information
Showing
18 changed files
with
1,517 additions
and
764 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
...impl/src/main/java/com/duckduckgo/autofill/impl/partialsave/PartialCredentialSaveStore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* 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.autofill.impl.partialsave | ||
|
||
import androidx.collection.LruCache | ||
import com.duckduckgo.autofill.impl.partialsave.PartialCredentialSaveStore.PartialSave | ||
import com.duckduckgo.autofill.impl.time.TimeProvider | ||
import com.duckduckgo.autofill.impl.urlmatcher.AutofillUrlMatcher | ||
import com.duckduckgo.common.utils.DispatcherProvider | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import dagger.SingleInstanceIn | ||
import java.util.concurrent.TimeUnit | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.withContext | ||
import timber.log.Timber | ||
|
||
interface PartialCredentialSaveStore { | ||
suspend fun saveUsername( | ||
url: String, | ||
username: String, | ||
) | ||
|
||
suspend fun consumeActiveBackFill(url: String): String? | ||
fun consumeBackFillHistory( | ||
url: String, | ||
username: String?, | ||
): Boolean | ||
|
||
data class PartialSave( | ||
val username: String, | ||
val creationTimestamp: Long, | ||
) | ||
} | ||
|
||
@ContributesBinding(AppScope::class) | ||
@SingleInstanceIn(AppScope::class) | ||
class PartialCredentialSaveInMemoryStore @Inject constructor( | ||
private val urlMatcher: AutofillUrlMatcher, | ||
private val timeProvider: TimeProvider, | ||
private val dispatchers: DispatcherProvider, | ||
) : PartialCredentialSaveStore { | ||
|
||
// these are potentially useful partial saves; ones which might be used later for backFilling | ||
private val potentialBackFills = LruCache<String, PartialSave>(5) | ||
|
||
// these are ones which were actually backFilled | ||
private val backFillHistory = LruCache<String, PartialSave>(5) | ||
|
||
override suspend fun saveUsername( | ||
url: String, | ||
username: String, | ||
) { | ||
withContext(dispatchers.io()) { | ||
val etldPlusOne = extractEtldPlusOne(url) ?: return@withContext | ||
potentialBackFills.put(etldPlusOne, PartialSave(username = username, creationTimestamp = timeProvider.currentTimeMillis())) | ||
} | ||
} | ||
|
||
/** | ||
* If a potential backFill username can be used for the given URL it will be returned | ||
*/ | ||
override suspend fun consumeActiveBackFill(url: String): String? { | ||
return withContext(dispatchers.io()) { | ||
removeExpiredEntries() | ||
val etldPlusOne = extractEtldPlusOne(url) ?: return@withContext null | ||
val activeBackFill = potentialBackFills[etldPlusOne] | ||
activeBackFill?.username?.also { | ||
potentialBackFills.remove(etldPlusOne) | ||
backFillHistory.put(etldPlusOne, activeBackFill) | ||
} | ||
} | ||
} | ||
|
||
override fun consumeBackFillHistory( | ||
url: String, | ||
username: String?, | ||
): Boolean { | ||
if (username == null) return false | ||
val etldPlusOne = extractEtldPlusOne(url) ?: return false | ||
return (backFillHistory[etldPlusOne]?.username == username).also { | ||
if (it) { | ||
backFillHistory.remove(etldPlusOne) | ||
} | ||
Timber.v("Username [%s] was %sbackfilled for %s", username, if (it) "" else "not ", etldPlusOne) | ||
} | ||
} | ||
|
||
private fun removeExpiredEntries() { | ||
potentialBackFills.snapshot().entries.forEach { | ||
if (it.value.isExpired()) { | ||
potentialBackFills.remove(it.key) | ||
} | ||
} | ||
} | ||
|
||
private fun extractEtldPlusOne(url: String) = urlMatcher.extractUrlPartsForAutofill(url).eTldPlus1 | ||
|
||
private fun PartialSave.isExpired(): Boolean { | ||
return (timeProvider.currentTimeMillis() - creationTimestamp) > MAX_VALIDITY_MS | ||
} | ||
|
||
companion object { | ||
val MAX_VALIDITY_MS = TimeUnit.MINUTES.toMillis(3) | ||
} | ||
} |
Oops, something went wrong.