Skip to content

Commit

Permalink
Issue mozilla-mobile#9838: Introduce CreditCardValidationDelegate and…
Browse files Browse the repository at this point in the history
… implement onCreditCardSave in GeckoCreditCardsAddressesStorageDelegate

- Introduces `CreditCardValidationDelegate` and a default implementation in `DefaultCreditCardValidationDelegate`
- Implements `onCreditCardSave` in `GeckoCreditCardsAddressesStorageDelegate`
- Refactors `CreditCard` from concept-engine to `CreditCardEntry` in concept-storage so that it can validated with the `CreditCardValidationDelegate`
  • Loading branch information
gabrielluong committed Mar 14, 2022
1 parent bed6675 commit 0bfa520
Show file tree
Hide file tree
Showing 28 changed files with 550 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@ class GeckoAutocompleteStorageDelegate(
private val loginStorageDelegate: LoginStorageDelegate
) : Autocomplete.StorageDelegate {

override fun onCreditCardFetch(): GeckoResult<Array<Autocomplete.CreditCard>>? {
override fun onCreditCardFetch(): GeckoResult<Array<Autocomplete.CreditCard>> {
val result = GeckoResult<Array<Autocomplete.CreditCard>>()

@OptIn(DelicateCoroutinesApi::class)
GlobalScope.launch(IO) {
val creditCards = creditCardsAddressesStorageDelegate.onCreditCardsFetch().await()
val key = creditCardsAddressesStorageDelegate.getOrGenerateKey()

val creditCards = creditCardsAddressesStorageDelegate.onCreditCardsFetch()
.mapNotNull {
val plaintextCardNumber =
creditCardsAddressesStorageDelegate.decrypt(it.encryptedCardNumber)?.number
creditCardsAddressesStorageDelegate.decrypt(key, it.encryptedCardNumber)?.number

if (plaintextCardNumber == null) {
null
Expand All @@ -54,6 +56,7 @@ class GeckoAutocompleteStorageDelegate(
}
}
.toTypedArray()

result.complete(creditCards)
}

Expand All @@ -64,7 +67,7 @@ class GeckoAutocompleteStorageDelegate(
loginStorageDelegate.onLoginSave(login.toLoginEntry())
}

override fun onLoginFetch(domain: String): GeckoResult<Array<Autocomplete.LoginEntry>>? {
override fun onLoginFetch(domain: String): GeckoResult<Array<Autocomplete.LoginEntry>> {
val result = GeckoResult<Array<Autocomplete.LoginEntry>>()

@OptIn(DelicateCoroutinesApi::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

package mozilla.components.browser.engine.gecko.ext

import mozilla.components.concept.engine.prompt.CreditCard
import mozilla.components.concept.storage.CreditCardEntry
import mozilla.components.support.utils.creditCardIIN
import org.mozilla.geckoview.Autocomplete

/**
* Converts a GeckoView [Autocomplete.CreditCard] to an Android Components [CreditCard].
* Converts a GeckoView [Autocomplete.CreditCard] to an Android Components [CreditCardEntry].
*/
fun Autocomplete.CreditCard.toCreditCard() = CreditCard(
fun Autocomplete.CreditCard.toCreditCardEntry() = CreditCardEntry(
guid = guid,
name = name,
number = number,
Expand All @@ -21,9 +21,9 @@ fun Autocomplete.CreditCard.toCreditCard() = CreditCard(
)

/**
* Converts an Android Components [CreditCard] to a GeckoView [Autocomplete.CreditCard].
* Converts an Android Components [CreditCardEntry] to a GeckoView [Autocomplete.CreditCard].
*/
fun CreditCard.toAutocompleteCreditCard() = Autocomplete.CreditCard.Builder()
fun CreditCardEntry.toAutocompleteCreditCard() = Autocomplete.CreditCard.Builder()
.guid(guid)
.name(name)
.number(number)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import android.net.Uri
import androidx.annotation.VisibleForTesting
import mozilla.components.browser.engine.gecko.GeckoEngineSession
import mozilla.components.browser.engine.gecko.ext.toAutocompleteCreditCard
import mozilla.components.browser.engine.gecko.ext.toCreditCard
import mozilla.components.browser.engine.gecko.ext.toCreditCardEntry
import mozilla.components.browser.engine.gecko.ext.toLoginEntry
import mozilla.components.concept.engine.prompt.Choice
import mozilla.components.concept.engine.prompt.CreditCard
import mozilla.components.concept.engine.prompt.PromptRequest
import mozilla.components.concept.engine.prompt.PromptRequest.MenuChoice
import mozilla.components.concept.engine.prompt.PromptRequest.MultipleChoice
import mozilla.components.concept.engine.prompt.PromptRequest.SingleChoice
import mozilla.components.concept.engine.prompt.ShareData
import mozilla.components.concept.storage.CreditCardEntry
import mozilla.components.concept.storage.Login
import mozilla.components.concept.storage.LoginEntry
import mozilla.components.support.base.log.logger.Logger
Expand Down Expand Up @@ -77,7 +77,7 @@ internal class GeckoPromptDelegate(private val geckoEngineSession: GeckoEngineSe
): GeckoResult<PromptResponse>? {
val geckoResult = GeckoResult<PromptResponse>()

val onConfirm: (CreditCard) -> Unit = { creditCard ->
val onConfirm: (CreditCardEntry) -> Unit = { creditCard ->
if (!request.isComplete) {
geckoResult.complete(
request.confirm(
Expand All @@ -94,7 +94,7 @@ internal class GeckoPromptDelegate(private val geckoEngineSession: GeckoEngineSe
geckoEngineSession.notifyObservers {
onPromptRequest(
PromptRequest.SelectCreditCard(
creditCards = request.options.map { it.value.toCreditCard() },
creditCards = request.options.map { it.value.toCreditCardEntry() },
onDismiss = onDismiss,
onConfirm = onConfirm
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import mozilla.components.browser.engine.gecko.ext.toAutocompleteCreditCard
import mozilla.components.browser.engine.gecko.ext.toLoginEntry
import mozilla.components.concept.engine.EngineSession
import mozilla.components.concept.engine.prompt.Choice
import mozilla.components.concept.engine.prompt.CreditCard
import mozilla.components.concept.engine.prompt.PromptRequest
import mozilla.components.concept.engine.prompt.PromptRequest.MultipleChoice
import mozilla.components.concept.engine.prompt.PromptRequest.SingleChoice
import mozilla.components.concept.storage.CreditCardEntry
import mozilla.components.concept.storage.Login
import mozilla.components.concept.storage.LoginEntry
import mozilla.components.support.ktx.kotlin.toDate
Expand Down Expand Up @@ -813,7 +813,7 @@ class GeckoPromptDelegateTest {
}
})

val creditCard1 = CreditCard(
val creditCard1 = CreditCardEntry(
guid = "1",
name = "Banana Apple",
number = "4111111111111110",
Expand All @@ -824,7 +824,7 @@ class GeckoPromptDelegateTest {
val creditCardSelectOption1 =
Autocomplete.CreditCardSelectOption(creditCard1.toAutocompleteCreditCard())

val creditCard2 = CreditCard(
val creditCard2 = CreditCardEntry(
guid = "2",
name = "Orange Pineapple",
number = "4111111111115555",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.net.Uri
import mozilla.components.concept.engine.prompt.PromptRequest.Authentication.Level
import mozilla.components.concept.engine.prompt.PromptRequest.Authentication.Method
import mozilla.components.concept.engine.prompt.PromptRequest.TimeSelection.Type
import mozilla.components.concept.storage.CreditCardEntry
import mozilla.components.concept.storage.Login
import mozilla.components.concept.storage.LoginEntry
import java.util.UUID
Expand Down Expand Up @@ -93,13 +94,13 @@ sealed class PromptRequest(

/**
* Value type that represents a request for a select credit card prompt.
* @property creditCards a list of [CreditCard]s to select from.
* @property creditCards a list of [CreditCardEntry]s to select from.
* @property onConfirm callback that is called when the user confirms the credit card selection.
* @property onDismiss callback to let the page know the user dismissed the dialog.
*/
data class SelectCreditCard(
val creditCards: List<CreditCard>,
val onConfirm: (CreditCard) -> Unit,
val creditCards: List<CreditCardEntry>,
val onConfirm: (CreditCardEntry) -> Unit,
override val onDismiss: () -> Unit
) : PromptRequest(), Dismissible

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import mozilla.components.concept.engine.prompt.PromptRequest.SingleChoice
import mozilla.components.concept.engine.prompt.PromptRequest.TextPrompt
import mozilla.components.concept.engine.prompt.PromptRequest.TimeSelection
import mozilla.components.concept.engine.prompt.PromptRequest.TimeSelection.Type
import mozilla.components.concept.storage.CreditCardEntry
import mozilla.components.concept.storage.Login
import mozilla.components.concept.storage.LoginEntry
import mozilla.components.support.test.mock
Expand Down Expand Up @@ -271,7 +272,7 @@ class PromptRequestTest {

@Test
fun `GIVEN a list of credit cards WHEN SelectCreditCard is confirmed or dismissed THEN their respective callback is invoked`() {
val creditCard = CreditCard(
val creditCard = CreditCardEntry(
guid = "id",
name = "Banana Apple",
number = "4111111111111110",
Expand All @@ -281,7 +282,7 @@ class PromptRequestTest {
)
var onDismissCalled = false
var onConfirmCalled = false
var confirmedCreditCard: CreditCard? = null
var confirmedCreditCard: CreditCardEntry? = null

val selectCreditCardRequest = SelectCreditCard(
creditCards = listOf(creditCard),
Expand Down
2 changes: 2 additions & 0 deletions components/concept/storage/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ dependencies {
// dependency, but it will crash at runtime.
// Included via 'api' because this module is unusable without coroutines.
api Dependencies.kotlin_coroutines

implementation project(':support-ktx')
}

apply from: '../../../publish.gradle'
Expand Down
Loading

0 comments on commit 0bfa520

Please sign in to comment.