-
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,543 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
107 changes: 107 additions & 0 deletions
107
...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,107 @@ | ||
/* | ||
* 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.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 consumeUsernameFromBackFill(url: String): String? | ||
suspend fun wasBackFilledRecently(url: String, username: String): Boolean | ||
} | ||
|
||
@ContributesBinding(AppScope::class) | ||
@SingleInstanceIn(AppScope::class) | ||
class PartialCredentialSaveInMemoryStore @Inject constructor( | ||
private val urlMatcher: AutofillUrlMatcher, | ||
private val timeProvider: TimeProvider, | ||
private val dispatchers: DispatcherProvider, | ||
) : PartialCredentialSaveStore { | ||
|
||
private val backFillHistory = LruCache<String, PartialSave>(5) | ||
|
||
override suspend fun saveUsername( | ||
url: String, | ||
username: String, | ||
) { | ||
withContext(dispatchers.io()) { | ||
val etldPlusOne = extractEtldPlusOne(url) ?: return@withContext | ||
backFillHistory.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 consumeUsernameFromBackFill(url: String): String? { | ||
return withContext(dispatchers.io()) { | ||
val etldPlusOne = extractEtldPlusOne(url) ?: return@withContext null | ||
val activeBackFill = backFillHistory[etldPlusOne] ?: return@withContext null | ||
|
||
if (activeBackFill.isExpired()) { | ||
Timber.v("Found expired username [%s] for %s. Not using for backFill.", activeBackFill.username, etldPlusOne) | ||
return@withContext null | ||
} | ||
|
||
backFillHistory.put(etldPlusOne, activeBackFill.copy(lastConsumedTimestamp = timeProvider.currentTimeMillis())) | ||
activeBackFill.username | ||
} | ||
} | ||
|
||
override suspend fun wasBackFilledRecently(url: String, username: String): Boolean { | ||
val etldPlusOne = extractEtldPlusOne(url) ?: return false | ||
val partialSave = backFillHistory[etldPlusOne] ?: return false | ||
if (partialSave.username != username) return false | ||
|
||
return partialSave.consumedRecently(timeProvider) | ||
} | ||
|
||
private fun extractEtldPlusOne(url: String) = urlMatcher.extractUrlPartsForAutofill(url).eTldPlus1 | ||
|
||
private fun PartialSave.isExpired(): Boolean { | ||
return (timeProvider.currentTimeMillis() - creationTimestamp) > MAX_VALIDITY_MS | ||
} | ||
|
||
data class PartialSave( | ||
val username: String, | ||
val creationTimestamp: Long, | ||
val lastConsumedTimestamp: Long? = null, | ||
) { | ||
fun consumedRecently(timeProvider: TimeProvider): Boolean { | ||
return lastConsumedTimestamp?.let { timeProvider.currentTimeMillis() - it < TIME_WINDOW_FOR_BEING_RECENT_MS } ?: false | ||
} | ||
} | ||
|
||
companion object { | ||
val MAX_VALIDITY_MS = TimeUnit.MINUTES.toMillis(3) | ||
val TIME_WINDOW_FOR_BEING_RECENT_MS = TimeUnit.SECONDS.toMillis(10) | ||
} | ||
} |
Oops, something went wrong.