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 5069a737c5cfe6cd6329fad4052970fb026eb6e7..56f89f1d03e8d3783408b613ef4a2e2273bd5ff9 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 |
| @@ -41,6 +41,7 @@ import org.chromium.components.safejson.JsonSanitizer; |
| import org.chromium.components.url_formatter.UrlFormatter; |
| import org.chromium.content_public.browser.WebContents; |
| import org.chromium.mojo.system.MojoException; |
| +import org.chromium.payments.mojom.ActivePaymentQueryResult; |
| import org.chromium.payments.mojom.PaymentComplete; |
| import org.chromium.payments.mojom.PaymentDetails; |
| import org.chromium.payments.mojom.PaymentErrorReason; |
| @@ -73,9 +74,14 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| PaymentApp.InstrumentsCallback, PaymentInstrument.InstrumentDetailsCallback, |
| PaymentResponseHelper.PaymentResponseRequesterDelegate { |
| /** |
| - * Observer to be notified when PaymentRequest UI has been dismissed. |
| + * Observer to be notified when PaymentRequest UI has been displayed or dismissed. |
| */ |
| - public interface PaymentRequestDismissObserver { |
| + public interface PaymentRequestDisplayObserver { |
| + /** |
| + * Called when PaymentRequest UI has been displayed. |
| + */ |
| + void onPaymentRequestDisplayed(); |
| + |
| /** |
| * Called when PaymentRequest UI has been dismissed. |
| */ |
| @@ -111,6 +117,11 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| * </ul> |
| */ |
| void onPaymentRequestServiceShowFailed(); |
| + |
| + /** |
| + * Called when the canMakeActivePayment() request has been responded. |
| + */ |
| + void onPaymentRequestServiceActivePaymentQueryResponded(); |
| } |
| private static final String TAG = "cr_PaymentRequest"; |
| @@ -124,8 +135,18 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| } |
| }; |
| + /** Every origin can call canMakeActivePayment() every 30 minutes. */ |
| + private static final int CAN_MAKE_ACTIVE_PAYMENT_QUERY_PERIOD_MS = 30 * 60 * 1000; |
| + |
| private static PaymentRequestServiceObserverForTest sObserverForTest; |
| + /** |
| + * In-memory mapping of the origins of websites that have recently called canMakeActivePayment() |
| + * to the list of the payment methods that were been queried. Used for throttling the usage of |
| + * this call. The user can clear the list by restarting the browser. |
| + */ |
| + private static Map<String, Set<String>> sCanMakeActivePaymentQueries; |
| + |
| /** Monitors changes in the TabModelSelector. */ |
| private final TabModelSelectorObserver mSelectorObserver = new EmptyTabModelSelectorObserver() { |
| @Override |
| @@ -144,7 +165,7 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| private final Handler mHandler = new Handler(); |
| private final ChromeActivity mContext; |
| - private final PaymentRequestDismissObserver mDismissObserver; |
| + private final PaymentRequestDisplayObserver mDisplayObserver; |
| private final String mMerchantName; |
| private final String mOrigin; |
| private final List<PaymentApp> mApps; |
| @@ -192,6 +213,7 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| private boolean mMerchantSupportsAutofillPaymentInstruments; |
| private ContactEditor mContactEditor; |
| private boolean mHasRecordedAbortReason; |
| + private boolean mQueriedCanMakeActivePayment; |
| /** True if any of the requested payment methods are supported. */ |
| private boolean mArePaymentMethodsSupported; |
| @@ -207,18 +229,19 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| * |
| * @param context The context where PaymentRequest has been invoked. |
| * @param webContents The web contents that have invoked the PaymentRequest API. |
| - * @param dismissObserver The observer to notify when PaymentRequest UI has been dismissed. |
| + * @param displayObserver The observer to notify when PaymentRequest UI has been displayed or |
| + * dismissed. |
| */ |
| public PaymentRequestImpl(Activity context, WebContents webContents, |
| - PaymentRequestDismissObserver dismissObserver) { |
| + PaymentRequestDisplayObserver displayObserver) { |
| assert context != null; |
| assert webContents != null; |
| - assert dismissObserver != null; |
| + assert displayObserver != null; |
| assert context instanceof ChromeActivity; |
| mContext = (ChromeActivity) context; |
| - mDismissObserver = dismissObserver; |
| + mDisplayObserver = displayObserver; |
| mMerchantName = webContents.getTitle(); |
| // The feature is available only in secure context, so it's OK to not show HTTPS. |
| mOrigin = UrlFormatter.formatUrlForSecurityDisplay(webContents.getVisibleUrl(), false); |
| @@ -419,6 +442,8 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| mUI.show(); |
| recordSuccessFunnelHistograms("Shown"); |
| + |
| + mDisplayObserver.onPaymentRequestDisplayed(); |
| } |
| private static Map<String, JSONObject> getValidatedMethodData( |
| @@ -907,6 +932,47 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| } |
| /** |
| + * Called by the merchant website to check if the user has complete payment instruments. |
| + */ |
| + @Override |
| + public void canMakeActivePayment() { |
| + if (mClient == null) return; |
| + |
| + if (sCanMakeActivePaymentQueries == null) sCanMakeActivePaymentQueries = new HashMap<>(); |
|
Ian Wen
2016/11/10 20:21:50
Optional comment:
You might want to consider usin
please use gerrit instead
2016/11/10 21:56:38
Done.
|
| + |
| + if (sCanMakeActivePaymentQueries.containsKey(mOrigin)) { |
| + if (!mMethodData.keySet().equals(sCanMakeActivePaymentQueries.get(mOrigin))) { |
| + mClient.onCanMakeActivePayment(ActivePaymentQueryResult.QUERY_QUOTA_EXCEEDED); |
| + if (sObserverForTest != null) { |
| + sObserverForTest.onPaymentRequestServiceActivePaymentQueryResponded(); |
| + } |
| + return; |
| + } |
| + } else { |
| + sCanMakeActivePaymentQueries.put(mOrigin, mMethodData.keySet()); |
| + } |
| + |
| + mHandler.postDelayed(new Runnable() { |
| + @Override |
| + public void run() { |
| + sCanMakeActivePaymentQueries.remove(mOrigin); |
| + } |
| + }, CAN_MAKE_ACTIVE_PAYMENT_QUERY_PERIOD_MS); |
| + |
| + if (!mPendingApps.isEmpty() || !mPendingInstruments.isEmpty()) { |
| + mQueriedCanMakeActivePayment = true; |
| + } else { |
| + mClient.onCanMakeActivePayment(mPaymentMethodsSection == null |
| + || mPaymentMethodsSection.getSelectedItem() == null |
| + ? ActivePaymentQueryResult.CANNOT_MAKE_ACTIVE_PAYMENT |
| + : ActivePaymentQueryResult.CAN_MAKE_ACTIVE_PAYMENT); |
| + if (sObserverForTest != null) { |
| + sObserverForTest.onPaymentRequestServiceActivePaymentQueryResponded(); |
| + } |
| + } |
| + } |
| + |
| + /** |
| * Called when the renderer closes the Mojo connection. |
| */ |
| @Override |
| @@ -993,6 +1059,16 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| } |
| } |
| + if (mQueriedCanMakeActivePayment) { |
| + mQueriedCanMakeActivePayment = false; |
| + mClient.onCanMakeActivePayment(selection == 0 |
| + ? ActivePaymentQueryResult.CAN_MAKE_ACTIVE_PAYMENT |
| + : ActivePaymentQueryResult.CANNOT_MAKE_ACTIVE_PAYMENT); |
| + if (sObserverForTest != null) { |
| + sObserverForTest.onPaymentRequestServiceActivePaymentQueryResponded(); |
| + } |
| + } |
| + |
| // The list of payment instruments is ready to display. |
| mPaymentMethodsSection = new SectionInformation(PaymentRequestUI.TYPE_PAYMENT_METHODS, |
| selection, mPendingInstruments); |
| @@ -1133,7 +1209,7 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| private void closeClient() { |
| if (mClient != null) mClient.close(); |
| mClient = null; |
| - mDismissObserver.onPaymentRequestDismissed(); |
| + mDisplayObserver.onPaymentRequestDismissed(); |
| } |
| @VisibleForTesting |