Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/payments/PaymentRequestImpl.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/payments/PaymentRequestImpl.java b/chrome/android/java/src/org/chromium/chrome/browser/payments/PaymentRequestImpl.java |
| index 356215070d205c2880a3b1f2228759117f5912ca..72c8bbad8e7db9401088ba1b5267181461cbbcea 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/payments/PaymentRequestImpl.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/payments/PaymentRequestImpl.java |
| @@ -159,6 +159,12 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| private ContactEditor mContactEditor; |
| private boolean mHasRecordedAbortReason; |
| + /** True if any of the requested payment methods are supported. */ |
| + private boolean mArePaymentMethodsSupported; |
| + |
| + /** True if show() was called. */ |
| + private boolean mIsShowing; |
| + |
| /** |
| * Builds the PaymentRequest service implementation. |
| * |
| @@ -214,10 +220,10 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| } |
| /** |
| - * Called by the merchant website to show the payment request to the user. |
| + * Called by the merchant website to initialize the payment request data. |
| */ |
| @Override |
| - public void show(PaymentMethodData[] methodData, PaymentDetails details, |
| + public void init(PaymentMethodData[] methodData, PaymentDetails details, |
| PaymentOptions options) { |
| if (mClient == null) return; |
| @@ -238,14 +244,7 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| if (!parseAndValidateDetailsOrDisconnectFromClient(details)) return; |
| - if (!getMatchingPaymentInstruments()) { |
| - disconnectFromClientWithDebugMessage("Requested payment methods are not supported", |
| - PaymentErrorReason.NOT_SUPPORTED); |
| - if (sObserverForTest != null) sObserverForTest.onPaymentRequestServiceShowFailed(); |
| - recordAbortReasonHistogram( |
| - PaymentRequestMetrics.ABORT_REASON_NO_SUPPORTED_PAYMENT_METHOD); |
| - return; |
| - } |
| + getMatchingPaymentInstruments(); |
| boolean requestShipping = options != null && options.requestShipping; |
| boolean requestPayerPhone = options != null && options.requestPayerPhone; |
| @@ -341,11 +340,21 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| mCardEditor.setEditorView(mUI.getCardEditorView()); |
| if (mContactEditor != null) mContactEditor.setEditorView(mUI.getEditorView()); |
| - recordSuccessFunnelHistograms("Shown"); |
| PaymentRequestMetrics.recordRequestedInformationHistogram(requestPayerEmail, |
| requestPayerPhone, requestShipping); |
| } |
| + /** |
| + * Called by the merchant website to show the payment request to the user. |
| + */ |
| + @Override |
| + public void show() { |
|
gone
2016/08/22 17:25:24
Should we be worried about show being called twice
please use gerrit instead
2016/08/22 17:52:36
Good point. A corrupted renderer can call show() t
|
| + mIsShowing = true; |
| + if (disconnectIfNoPaymentMethodsSupported()) return; |
| + mUI.show(); |
| + recordSuccessFunnelHistograms("Shown"); |
| + } |
| + |
| private static Map<String, JSONObject> getValidatedMethodData( |
| PaymentMethodData[] methodData, CardEditor paymentMethodsCollector) { |
| // Payment methodData are required. |
| @@ -383,16 +392,11 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| return result; |
| } |
| - /** |
| - * Queries the installed payment apps for their instruments that merchant supports. |
| - * |
| - * @return True if any of the requested payment methods are supported. |
| - */ |
| - private boolean getMatchingPaymentInstruments() { |
| + /** Queries the installed payment apps for their instruments that merchant supports. */ |
| + private void getMatchingPaymentInstruments() { |
| mPendingApps = new ArrayList<>(mApps); |
| mPendingInstruments = new ArrayList<>(); |
| mPendingAutofillInstruments = new ArrayList<>(); |
| - boolean arePaymentMethodsSupported = false; |
| Map<PaymentApp, JSONObject> queryApps = new HashMap<>(); |
| for (int i = 0; i < mApps.size(); i++) { |
| @@ -402,7 +406,7 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| if (appMethods.isEmpty()) { |
| mPendingApps.remove(app); |
| } else { |
| - arePaymentMethodsSupported = true; |
| + mArePaymentMethodsSupported = true; |
| mMerchantSupportsAutofillPaymentInstruments |= app instanceof AutofillPaymentApp; |
| queryApps.put(app, mMethodData.get(appMethods.iterator().next())); |
| } |
| @@ -414,8 +418,6 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| for (Map.Entry<PaymentApp, JSONObject> q : queryApps.entrySet()) { |
| q.getKey().getInstruments(q.getValue(), this); |
| } |
| - |
| - return arePaymentMethodsSupported; |
| } |
| /** |
| @@ -882,17 +884,7 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| // Some payment apps still have not responded. Continue waiting for them. |
| if (!mPendingApps.isEmpty()) return; |
| - if (mPendingInstruments.isEmpty() && !mMerchantSupportsAutofillPaymentInstruments) { |
| - // All payment apps have responded, but none of them have instruments. It's possible to |
| - // add credit cards, but the merchant does not support them either. The payment request |
| - // must be rejected. |
| - disconnectFromClientWithDebugMessage("Requested payment methods have no instruments", |
| - PaymentErrorReason.NOT_SUPPORTED); |
| - if (sObserverForTest != null) sObserverForTest.onPaymentRequestServiceShowFailed(); |
| - recordAbortReasonHistogram( |
| - PaymentRequestMetrics.ABORT_REASON_NO_MATCHING_PAYMENT_METHOD); |
| - return; |
| - } |
| + if (disconnectIfNoPaymentMethodsSupported()) return; |
| // List order: |
| // > Non-autofill instruments. |
| @@ -919,13 +911,40 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| selection, mPendingInstruments); |
| mPendingInstruments.clear(); |
| - mPendingInstruments = null; |
| // UI has requested the full list of payment instruments. Provide it now. |
| if (mPaymentInformationCallback != null) providePaymentInformation(); |
| } |
| /** |
| + * If no payment methods are supported, disconnect from the client and return true. |
| + * |
| + * @return True if no payment methods are supported |
| + */ |
| + private boolean disconnectIfNoPaymentMethodsSupported() { |
| + boolean waitingForPaymentApps = !mPendingApps.isEmpty() || !mPendingInstruments.isEmpty(); |
| + boolean foundPaymentMethods = |
| + mPaymentMethodsSection != null && !mPaymentMethodsSection.isEmpty(); |
| + |
| + if (!mArePaymentMethodsSupported |
| + || (mIsShowing && !waitingForPaymentApps && !foundPaymentMethods |
| + && !mMerchantSupportsAutofillPaymentInstruments)) { |
| + // All payment apps have responded, but none of them have instruments. It's possible to |
| + // add credit cards, but the merchant does not support them either. The payment request |
| + // must be rejected. |
| + disconnectFromClientWithDebugMessage("Requested payment methods have no instruments", |
| + PaymentErrorReason.NOT_SUPPORTED); |
| + recordAbortReasonHistogram(mArePaymentMethodsSupported |
| + ? PaymentRequestMetrics.ABORT_REASON_NO_MATCHING_PAYMENT_METHOD |
| + : PaymentRequestMetrics.ABORT_REASON_NO_SUPPORTED_PAYMENT_METHOD); |
| + if (sObserverForTest != null) sObserverForTest.onPaymentRequestServiceShowFailed(); |
| + return true; |
| + } |
| + |
| + return false; |
| + } |
| + |
| + /** |
| * Saves the given instrument in either "autofill" or "non-autofill" list. The separation |
| * enables placing autofill instruments on the bottom of the list. |
| * |