-
Notifications
You must be signed in to change notification settings - Fork 922
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
17 changed files
with
1,493 additions
and
794 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
81 changes: 81 additions & 0 deletions
81
...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,81 @@ | ||
/* | ||
* 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 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 | ||
|
||
interface PartialCredentialSaveStore { | ||
suspend fun saveUsername(url: String, username: String) | ||
suspend fun consumeUsername(url: String): String? | ||
|
||
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 { | ||
|
||
private val map = mutableMapOf<String, PartialSave>() | ||
|
||
override suspend fun saveUsername( | ||
url: String, | ||
username: String, | ||
) { | ||
withContext(dispatchers.io()) { | ||
val etldPlusOne = urlMatcher.extractUrlPartsForAutofill(url).eTldPlus1 ?: return@withContext | ||
map[etldPlusOne] = PartialSave(username = username, creationTimestamp = timeProvider.currentTimeMillis()) | ||
} | ||
} | ||
|
||
override suspend fun consumeUsername(url: String): String? { | ||
return withContext(dispatchers.io()) { | ||
removeExpiredEntries() | ||
val etldPlusOne = urlMatcher.extractUrlPartsForAutofill(url).eTldPlus1 | ||
map[etldPlusOne]?.username?.also { | ||
map.remove(etldPlusOne) | ||
} | ||
} | ||
} | ||
|
||
private fun removeExpiredEntries() { | ||
map.entries.removeIf { it.value.isExpired() } | ||
} | ||
|
||
private fun PartialSave.isExpired(): Boolean { | ||
return (timeProvider.currentTimeMillis() - creationTimestamp) > MAX_VALIDITY_MS | ||
} | ||
|
||
companion object { | ||
val MAX_VALIDITY_MS = TimeUnit.MINUTES.toMillis(3) | ||
} | ||
} |
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
Oops, something went wrong.