Skip to content
This repository has been archived by the owner on Jul 18, 2018. It is now read-only.

Commit

Permalink
[Payments] Delegate PaymentRequest.abort to web payment handler
Browse files Browse the repository at this point in the history
Spec change:
w3c/payment-handler#207

Bug: 736745
Change-Id: Ifb78aa332080044180e2e836ac0709ba3f727567
Reviewed-on: https://chromium-review.googlesource.com/636504
Reviewed-by: Rouslan Solomakhin <[email protected]>
Commit-Queue: Ganggui Tang <[email protected]>
Cr-Commit-Position: refs/heads/master@{#498114}
  • Loading branch information
gogerald authored and Commit Bot committed Aug 29, 2017
1 parent b989f8a commit 059f9dc
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import android.graphics.drawable.Drawable;

import org.chromium.base.ThreadUtils;
import org.chromium.chrome.browser.payments.ui.PaymentOption;
import org.chromium.payments.mojom.PaymentDetailsModifier;
import org.chromium.payments.mojom.PaymentItem;
Expand Down Expand Up @@ -46,6 +47,16 @@ public interface InstrumentDetailsCallback {
void onInstrumentDetailsError();
}

/** The interface for the requester to abort payment. */
public interface AbortCallback {
/**
* Called after aborting payment is finished.
*
* @param abortSucceeded Indicates whether abort is succeed.
*/
void onInstrumentAbortResult(boolean abortSucceeded);
}

protected PaymentInstrument(String id, String label, String sublabel, Drawable icon) {
super(id, label, sublabel, icon);
}
Expand Down Expand Up @@ -143,6 +154,20 @@ public abstract void invokePaymentApp(String id, String merchantName, String ori
List<PaymentItem> displayItems, Map<String, PaymentDetailsModifier> modifiers,
InstrumentDetailsCallback callback);

/**
* Abort invocation of the payment app.
*
* @param callback The callback to return abort result.
*/
public void abortPaymentApp(AbortCallback callback) {
ThreadUtils.postOnUiThread(new Runnable() {
@Override
public void run() {
callback.onInstrumentAbortResult(false);
}
});
}

/**
* Cleans up any resources held by the payment instrument. For example, closes server
* connections.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@
* Android implementation of the PaymentRequest service defined in
* components/payments/content/payment_request.mojom.
*/
public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Client,
PaymentApp.InstrumentsCallback,
PaymentInstrument.InstrumentDetailsCallback,
PaymentAppFactory.PaymentAppCreatedCallback,
PaymentResponseHelper.PaymentResponseRequesterDelegate,
FocusChangedObserver, NormalizedAddressRequestDelegate {
public class PaymentRequestImpl
implements PaymentRequest, PaymentRequestUI.Client, PaymentApp.InstrumentsCallback,
PaymentInstrument.AbortCallback, PaymentInstrument.InstrumentDetailsCallback,
PaymentAppFactory.PaymentAppCreatedCallback,
PaymentResponseHelper.PaymentResponseRequesterDelegate, FocusChangedObserver,
NormalizedAddressRequestDelegate {
/**
* A test-only observer for the PaymentRequest service implementation.
*/
Expand Down Expand Up @@ -1426,13 +1426,24 @@ private void disconnectFromClientWithDebugMessage(String debugMessage, int reaso
@Override
public void abort() {
if (mClient == null) return;
mClient.onAbort(!mPaymentAppRunning);

if (mPaymentAppRunning) {
if (sObserverForTest != null) sObserverForTest.onPaymentRequestServiceUnableToAbort();
} else {
((PaymentInstrument) mPaymentMethodsSection.getSelectedItem()).abortPaymentApp(this);
return;
}
onInstrumentAbortResult(true);
}

/** Called by the payment app in response to an abort request. */
@Override
public void onInstrumentAbortResult(boolean abortSucceeded) {
mClient.onAbort(abortSucceeded);
if (abortSucceeded) {
closeClient();
closeUI(true);
mJourneyLogger.setAborted(AbortReason.ABORTED_BY_MERCHANT);
} else {
if (sObserverForTest != null) sObserverForTest.onPaymentRequestServiceUnableToAbort();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ public void invokePaymentApp(String id, String merchantName, String origin, Stri
new HashSet<>(modifiers.values()), callback);
}

@Override
public void abortPaymentApp(AbortCallback callback) {
ServiceWorkerPaymentAppBridge.abortPaymentApp(mWebContents, mRegistrationId, callback);
}

@Override
public void dismissInstrument() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ public static void invokePaymentApp(WebContents webContents, long registrationId
modifiers.toArray(new PaymentDetailsModifier[0]), callback);
}

/**
* Abort invocation of the payment app.
*
* @param webContents The web contents that invoked PaymentRequest.
* @param registrationId The service worker registration ID of the Payment App.
* @param callback Called after abort invoke payment app is finished running.
*/
public static void abortPaymentApp(WebContents webContents, long registrationId,
PaymentInstrument.AbortCallback callback) {
nativeAbortPaymentApp(webContents, registrationId, callback);
}

@CalledByNative
private static String[] getSupportedMethodsFromMethodData(PaymentMethodData data) {
return data.supportedMethods;
Expand Down Expand Up @@ -140,6 +152,12 @@ private static void onPaymentAppInvoked(
}
}

@CalledByNative
private static void onPaymentAppAborted(Object callback, boolean result) {
assert callback instanceof PaymentInstrument.AbortCallback;
((PaymentInstrument.AbortCallback) callback).onInstrumentAbortResult(result);
}

/*
* TODO(tommyt): crbug.com/505554. Change the |callback| parameter below to
* be of type PaymentInstrument.InstrumentDetailsCallback, once this JNI bug
Expand All @@ -156,4 +174,12 @@ private static native void nativeInvokePaymentApp(WebContents webContents, long
String topLevelOrigin, String paymentRequestOrigin, String paymentRequestId,
PaymentMethodData[] methodData, PaymentItem total, PaymentDetailsModifier[] modifiers,
Object callback);

/*
* TODO(tommyt): crbug.com/505554. Change the |callback| parameter below to
* be of type PaymentInstrument.InstrumentDetailsCallback, once this JNI bug
* has been resolved.
*/
private static native void nativeAbortPaymentApp(
WebContents webContents, long registrationId, Object callback);
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ void OnPaymentAppInvoked(
ConvertUTF8ToJavaString(env, handler_response->stringified_details));
}

void OnPaymentAppAborted(const JavaRef<jobject>& jweb_contents,
const JavaRef<jobject>& jcallback,
bool result) {
JNIEnv* env = AttachCurrentThread();

Java_ServiceWorkerPaymentAppBridge_onPaymentAppAborted(env, jcallback,
result);
}

} // namespace

static void GetAllPaymentApps(JNIEnv* env,
Expand Down Expand Up @@ -202,3 +211,18 @@ static void InvokePaymentApp(
ScopedJavaGlobalRef<jobject>(env, jweb_contents),
ScopedJavaGlobalRef<jobject>(env, jcallback)));
}

static void AbortPaymentApp(JNIEnv* env,
const JavaParamRef<jclass>& jcaller,
const JavaParamRef<jobject>& jweb_contents,
jlong registration_id,
const JavaParamRef<jobject>& jcallback) {
content::WebContents* web_contents =
content::WebContents::FromJavaWebContents(jweb_contents);

content::PaymentAppProvider::GetInstance()->AbortPayment(
web_contents->GetBrowserContext(), registration_id,
base::BindOnce(&OnPaymentAppAborted,
ScopedJavaGlobalRef<jobject>(env, jweb_contents),
ScopedJavaGlobalRef<jobject>(env, jcallback)));
}

0 comments on commit 059f9dc

Please sign in to comment.