Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1073)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/payments/AndroidPaymentApp.java

Issue 2746163002: Disconnect from the READY_TO_PAY service after query. (Closed)
Patch Set: Rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/android/java/src/org/chromium/chrome/browser/payments/AndroidPaymentApp.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/payments/AndroidPaymentApp.java b/chrome/android/java/src/org/chromium/chrome/browser/payments/AndroidPaymentApp.java
index 0c7fcc0218d906d4605c259979e0a35a99fe2794..9443c4bb3fb098b690982d036ef0b88c6723127f 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/payments/AndroidPaymentApp.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/payments/AndroidPaymentApp.java
@@ -23,6 +23,7 @@ import android.util.JsonWriter;
import org.chromium.IsReadyToPayService;
import org.chromium.IsReadyToPayServiceCallback;
+import org.chromium.base.ContextUtils;
import org.chromium.base.ThreadUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ChromeActivity;
@@ -42,14 +43,17 @@ import java.util.Map;
import java.util.Set;
/** The point of interaction with a locally installed 3rd party native Android payment app. */
-public class AndroidPaymentApp extends PaymentInstrument implements PaymentApp,
- WindowAndroid.IntentCallback {
+public class AndroidPaymentApp
+ extends PaymentInstrument implements PaymentApp, WindowAndroid.IntentCallback {
/** The action name for the Pay Intent. */
public static final String ACTION_PAY = "org.chromium.intent.action.PAY";
/** The maximum number of milliseconds to wait for a response from a READY_TO_PAY service. */
private static final long READY_TO_PAY_TIMEOUT_MS = 400;
+ /** The maximum number of milliseconds to wait for a connection to READY_TO_PAY service. */
+ private static final long SERVICE_CONNECTION_TIMEOUT_MS = 1000;
+
private static final String EXTRA_METHOD_NAME = "methodName";
private static final String EXTRA_DATA = "data";
private static final String EXTRA_ORIGIN = "origin";
@@ -64,25 +68,10 @@ public class AndroidPaymentApp extends PaymentInstrument implements PaymentApp,
private final Intent mPayIntent;
private final Set<String> mMethodNames;
private final boolean mIsIncognito;
- private IsReadyToPayService mIsReadyToPayService;
private InstrumentsCallback mInstrumentsCallback;
private InstrumentDetailsCallback mInstrumentDetailsCallback;
- private final ServiceConnection mServiceConnection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- mIsReadyToPayService = IsReadyToPayService.Stub.asInterface(service);
- if (mIsReadyToPayService == null) {
- respondToGetInstrumentsQuery(null);
- } else {
- sendIsReadyToPayIntentToPaymentApp();
- }
- }
-
- @Override
- public void onServiceDisconnected(ComponentName name) {
- respondToGetInstrumentsQuery(null);
- }
- };
+ private ServiceConnection mServiceConnection;
+ private boolean mIsReadyToPayQueried;
/**
* Builds the point of interaction with a locally installed 3rd party native Android payment
@@ -135,8 +124,8 @@ public class AndroidPaymentApp extends PaymentInstrument implements PaymentApp,
@Override
public void getInstruments(Map<String, PaymentMethodData> methodData, String origin,
byte[][] certificateChain, InstrumentsCallback callback) {
- assert mInstrumentsCallback == null
- : "Have not responded to previous request for instruments yet";
+ assert mInstrumentsCallback
+ == null : "Have not responded to previous request for instruments yet";
mInstrumentsCallback = callback;
if (mIsReadyToPayIntent.getComponent() == null) {
respondToGetInstrumentsQuery(AndroidPaymentApp.this);
@@ -152,25 +141,47 @@ public class AndroidPaymentApp extends PaymentInstrument implements PaymentApp,
addCertificateChain(extras, certificateChain);
mIsReadyToPayIntent.putExtras(extras);
- if (mIsReadyToPayService != null) {
- sendIsReadyToPayIntentToPaymentApp();
- } else {
- WindowAndroid window = mWebContents.getTopLevelNativeWindow();
- if (window == null) {
- respondToGetInstrumentsQuery(null);
- return;
+ mServiceConnection = new ServiceConnection() {
+ @Override
+ public void onServiceConnected(ComponentName name, IBinder service) {
+ IsReadyToPayService isReadyToPayService =
+ IsReadyToPayService.Stub.asInterface(service);
+ if (isReadyToPayService == null) {
+ respondToGetInstrumentsQuery(null);
+ } else {
+ sendIsReadyToPayIntentToPaymentApp(isReadyToPayService);
+ }
}
- try {
- window.getApplicationContext().bindService(
- mIsReadyToPayIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
- } catch (SecurityException e) {
+ @Override
+ public void onServiceDisconnected(ComponentName name) {}
+ };
+
+ try {
+ if (!ContextUtils.getApplicationContext().bindService(
+ mIsReadyToPayIntent, mServiceConnection, Context.BIND_AUTO_CREATE)) {
respondToGetInstrumentsQuery(null);
+ return;
}
+ } catch (SecurityException e) {
+ respondToGetInstrumentsQuery(null);
+ return;
}
+
+ mHandler.postDelayed(new Runnable() {
+ @Override
+ public void run() {
+ if (!mIsReadyToPayQueried) respondToGetInstrumentsQuery(null);
+ }
+ }, SERVICE_CONNECTION_TIMEOUT_MS);
}
private void respondToGetInstrumentsQuery(final PaymentInstrument instrument) {
+ if (mServiceConnection != null) {
+ ContextUtils.getApplicationContext().unbindService(mServiceConnection);
+ mServiceConnection = null;
+ }
+
if (mInstrumentsCallback == null) return;
mHandler.post(new Runnable() {
@Override
@@ -188,8 +199,9 @@ public class AndroidPaymentApp extends PaymentInstrument implements PaymentApp,
});
}
- private void sendIsReadyToPayIntentToPaymentApp() {
- assert mIsReadyToPayService != null;
+ private void sendIsReadyToPayIntentToPaymentApp(IsReadyToPayService isReadyToPayService) {
+ if (mInstrumentsCallback == null) return;
+ mIsReadyToPayQueried = true;
IsReadyToPayServiceCallback.Stub callback = new IsReadyToPayServiceCallback.Stub() {
@Override
public void handleIsReadyToPay(boolean isReadyToPay) throws RemoteException {
@@ -201,7 +213,7 @@ public class AndroidPaymentApp extends PaymentInstrument implements PaymentApp,
}
};
try {
- mIsReadyToPayService.isReadyToPay(callback);
+ isReadyToPayService.isReadyToPay(callback);
} catch (Throwable e) {
// Many undocumented exceptions are not caught in the remote Service but passed on to
// the Service caller, see writeException in Parcel.java.
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698