Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/payments/AutofillPaymentInstrument.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/payments/AutofillPaymentInstrument.java b/chrome/android/java/src/org/chromium/chrome/browser/payments/AutofillPaymentInstrument.java |
| index 62ee4cffa1fa44ede450a96efa8e00bfed5e7d77..09a028399b6c66433db578f42281d933bb45e30c 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/payments/AutofillPaymentInstrument.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/payments/AutofillPaymentInstrument.java |
| @@ -4,18 +4,20 @@ |
| package org.chromium.chrome.browser.payments; |
| +import android.os.Handler; |
| import android.text.TextUtils; |
| import android.util.JsonWriter; |
| +import org.json.JSONObject; |
| + |
| import org.chromium.chrome.browser.autofill.PersonalDataManager; |
| import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; |
| import org.chromium.chrome.browser.autofill.PersonalDataManager.CreditCard; |
| import org.chromium.chrome.browser.autofill.PersonalDataManager.FullCardRequestDelegate; |
| +import org.chromium.chrome.browser.autofill.PersonalDataManager.NormalizedAddressRequestDelegate; |
| import org.chromium.content_public.browser.WebContents; |
| import org.chromium.payments.mojom.PaymentItem; |
| -import org.json.JSONObject; |
| - |
| import java.io.IOException; |
| import java.io.StringWriter; |
| import java.util.List; |
| @@ -25,13 +27,17 @@ import javax.annotation.Nullable; |
| /** |
| * The locally stored credit card payment instrument. |
| */ |
| -public class AutofillPaymentInstrument |
| - extends PaymentInstrument implements FullCardRequestDelegate { |
| +public class AutofillPaymentInstrument extends PaymentInstrument |
| + implements FullCardRequestDelegate, NormalizedAddressRequestDelegate { |
| private final WebContents mWebContents; |
| + private final Handler mHandler = new Handler(); |
|
please use gerrit instead
2016/10/19 19:10:59
Please remove this for reasons outlined below.
sebsg
2016/10/20 17:06:52
Done.
|
| private CreditCard mCard; |
| private boolean mIsComplete; |
| + private String mSecurityCode; |
| @Nullable private AutofillProfile mBillingAddress; |
| @Nullable private DetailsCallback mCallback; |
| + private boolean mIsWaitingForBillingNormalization; |
| + private boolean mIsWaitingForFullCardDetails; |
| /** |
| * Builds a payment instrument for the given credit card. |
| @@ -58,24 +64,81 @@ public class AutofillPaymentInstrument |
| @Override |
| public void getDetails(String unusedMerchantName, String unusedOrigin, PaymentItem unusedTotal, |
| List<PaymentItem> unusedCart, JSONObject unusedDetails, DetailsCallback callback) { |
| + // The billing address should never be null for a credit card at this point. |
| + assert mBillingAddress != null; |
| assert mIsComplete; |
| assert mCallback == null; |
| mCallback = callback; |
| + |
| + mIsWaitingForBillingNormalization = true; |
| + mIsWaitingForFullCardDetails = true; |
| + |
| + // Start the billing address normalization. |
| + mIsWaitingForBillingNormalization = PersonalDataManager.getInstance().normalizeAddress( |
| + mBillingAddress.getGUID(), AutofillAddress.getCountryCode(mBillingAddress), this); |
| + if (mIsWaitingForBillingNormalization) { |
| + // If the normalization was not done synchronously, start a timer to cancel the |
| + // asynchronous normalization if it takes too long. |
| + mHandler.postDelayed(new Runnable() { |
|
please use gerrit instead
2016/10/19 19:10:59
This Handler is going to be accessed only once and
sebsg
2016/10/20 17:06:52
Done.
|
| + @Override |
| + public void run() { |
| + onAddressNormalized(null); |
| + } |
| + }, PersonalDataManager.getInstance().getNormalizationTimeoutMS()); |
|
please use gerrit instead
2016/10/19 19:10:59
We timebox normalization to 5 seconds, but unmaski
sebsg
2016/10/20 17:06:52
Done.
|
| + } |
| + |
| + // Start to get the full card details. |
| PersonalDataManager.getInstance().getFullCard(mWebContents, mCard, this); |
| } |
| @Override |
| - public void onFullCardDetails(CreditCard card, String cvc) { |
| + public void onFullCardDetails(CreditCard updatedCard, String cvc) { |
| + // Keep the cvc for after the normalization. |
| + mSecurityCode = cvc; |
| + |
| + // Update the card's expiration date. |
| + mCard.setMonth(updatedCard.getMonth()); |
| + mCard.setYear(updatedCard.getYear()); |
| + |
| + mIsWaitingForFullCardDetails = false; |
| + |
| + // Wait for the billing address normalization before sending the instrument details. |
| + if (mIsWaitingForBillingNormalization) { |
| + // Show the loading UI while the address gets normalized. |
| + mCallback.loadingInstrumentDetails(); |
| + return; |
| + } else { |
| + sendIntrumentDetails(); |
| + } |
| + } |
| + |
| + @Override |
| + public void onAddressNormalized(AutofillProfile profile) { |
| + if (!mIsWaitingForBillingNormalization) return; |
| + mIsWaitingForBillingNormalization = false; |
| + |
| + // If the normalization finished first, use the normalized address. |
| + if (profile != null) mBillingAddress = profile; |
| + |
| + // Wait for the full card details before sending the instrument details. |
| + if (!mIsWaitingForFullCardDetails) sendIntrumentDetails(); |
| + } |
| + |
| + /** |
| + * Stringify the card details and send the resulting string and the method name to the |
| + * registered callback. |
| + */ |
| + private void sendIntrumentDetails() { |
| StringWriter stringWriter = new StringWriter(); |
| JsonWriter json = new JsonWriter(stringWriter); |
| try { |
| json.beginObject(); |
| - json.name("cardholderName").value(card.getName()); |
| - json.name("cardNumber").value(card.getNumber()); |
| - json.name("expiryMonth").value(card.getMonth()); |
| - json.name("expiryYear").value(card.getYear()); |
| - json.name("cardSecurityCode").value(cvc); |
| + json.name("cardholderName").value(mCard.getName()); |
| + json.name("cardNumber").value(mCard.getNumber()); |
| + json.name("expiryMonth").value(mCard.getMonth()); |
| + json.name("expiryYear").value(mCard.getYear()); |
| + json.name("cardSecurityCode").value(mSecurityCode); |
| json.name("billingAddress").beginObject(); |
| @@ -108,9 +171,12 @@ public class AutofillPaymentInstrument |
| } catch (IOException e) { |
| onFullCardError(); |
| return; |
| + } finally { |
| + mSecurityCode = ""; |
| } |
| - mCallback.onInstrumentDetailsReady(card.getBasicCardPaymentType(), stringWriter.toString()); |
| + mCallback.onInstrumentDetailsReady( |
| + mCard.getBasicCardPaymentType(), stringWriter.toString()); |
| } |
| private static String ensureNotNull(@Nullable String value) { |
| @@ -161,4 +227,9 @@ public class AutofillPaymentInstrument |
| public CreditCard getCard() { |
| return mCard; |
| } |
| -} |
| + |
| + /** @return The billing address associated with this credit card. */ |
| + public AutofillProfile getBillingAddress() { |
| + return mBillingAddress; |
| + } |
| +} |