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 Dec 28, 2021
1 parent 12e2f62 commit f5d761f
Show file tree
Hide file tree
Showing 27 changed files with 448 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ class GeckoAutocompleteStorageDelegate(

@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 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 @@ -812,7 +812,7 @@ class GeckoPromptDelegateTest {
}
})

val creditCard1 = CreditCard(
val creditCard1 = CreditCardEntry(
guid = "1",
name = "Banana Apple",
number = "4111111111111110",
Expand All @@ -823,7 +823,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 @@ -78,14 +79,14 @@ 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 onDismiss callback to let the page know the user dismissed the dialog.
* @property onConfirm callback that is called when the user confirms the credit card selection.
*/
data class SelectCreditCard(
val creditCards: List<CreditCard>,
val creditCards: List<CreditCardEntry>,
override val onDismiss: () -> Unit,
val onConfirm: (CreditCard) -> Unit
val onConfirm: (CreditCardEntry) -> 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 @@ -266,7 +267,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 @@ -276,7 +277,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 f5d761f

Please sign in to comment.