| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.payments; | 5 package org.chromium.chrome.browser.payments; |
| 6 | 6 |
| 7 import android.graphics.drawable.Drawable; | |
| 8 | |
| 9 import org.chromium.base.annotations.CalledByNative; | 7 import org.chromium.base.annotations.CalledByNative; |
| 10 import org.chromium.base.annotations.SuppressFBWarnings; | 8 import org.chromium.base.annotations.SuppressFBWarnings; |
| 11 import org.chromium.content_public.browser.WebContents; | 9 import org.chromium.content_public.browser.WebContents; |
| 10 import org.chromium.chrome.browser.payments.PaymentAppFactory; |
| 12 import org.chromium.payments.mojom.PaymentDetailsModifier; | 11 import org.chromium.payments.mojom.PaymentDetailsModifier; |
| 13 import org.chromium.payments.mojom.PaymentItem; | 12 import org.chromium.payments.mojom.PaymentItem; |
| 14 import org.chromium.payments.mojom.PaymentMethodData; | 13 import org.chromium.payments.mojom.PaymentMethodData; |
| 15 | 14 |
| 16 import java.util.ArrayList; | 15 import java.util.ArrayList; |
| 16 import java.util.Arrays; |
| 17 import java.util.HashSet; |
| 17 import java.util.List; | 18 import java.util.List; |
| 18 import java.util.Set; | 19 import java.util.Set; |
| 19 | 20 |
| 20 /** | 21 /** |
| 21 * Native bridge for interacting with service worker based payment apps. | 22 * Native bridge for interacting with service worker based payment apps. |
| 22 */ | 23 */ |
| 23 // TODO(tommyt): crbug.com/669876. Remove these suppressions when we actually | 24 // TODO(tommyt): crbug.com/669876. Remove these suppressions when we actually |
| 24 // start using all of the functionality in this class. | 25 // start using all of the functionality in this class. |
| 25 @SuppressFBWarnings({"UWF_NULL_FIELD", "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD", | 26 @SuppressFBWarnings({"UWF_NULL_FIELD", "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD", |
| 26 "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UUF_UNUSED_PUBLIC_OR_PROTECT
ED_FIELD"}) | 27 "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UUF_UNUSED_PUBLIC_OR_PROTECT
ED_FIELD"}) |
| 27 public class ServiceWorkerPaymentAppBridge implements PaymentAppFactory.PaymentA
ppFactoryAddition { | 28 public class ServiceWorkerPaymentAppBridge implements PaymentAppFactory.PaymentA
ppFactoryAddition { |
| 28 /** | 29 @Override |
| 29 * This class represents a payment app manifest as defined in the Payment | 30 public void create(WebContents webContents, Set<String> methodNames, |
| 30 * App API specification. | 31 PaymentAppFactory.PaymentAppCreatedCallback callback) { |
| 31 * | 32 nativeGetAllPaymentApps(webContents, callback); |
| 32 * @see https://w3c.github.io/webpayments-payment-apps-api/#payment-app-mani
fest | |
| 33 */ | |
| 34 public static class Manifest { | |
| 35 /** | |
| 36 * The registration ID of the service worker. | |
| 37 * | |
| 38 * This can be used to identify a service worker based payment app. | |
| 39 */ | |
| 40 public long registrationId; | |
| 41 public String label; | |
| 42 public Drawable icon; | |
| 43 public List<Option> options = new ArrayList<>(); | |
| 44 } | 33 } |
| 45 | 34 |
| 46 /** | 35 /** |
| 47 * This class represents a payment option as defined in the Payment App API | |
| 48 * specification. | |
| 49 * | |
| 50 * @see https://w3c.github.io/webpayments-payment-apps-api/#payment-app-opti
ons | |
| 51 */ | |
| 52 public static class Option { | |
| 53 public String id; | |
| 54 public String label; | |
| 55 public Drawable icon; | |
| 56 public List<String> enabledMethods = new ArrayList<>(); | |
| 57 } | |
| 58 | |
| 59 @Override | |
| 60 public void create(WebContents webContents, Set<String> methodNames, | |
| 61 PaymentAppFactory.PaymentAppCreatedCallback callback) { | |
| 62 nativeGetAllAppManifests(webContents, callback); | |
| 63 } | |
| 64 | |
| 65 /** | |
| 66 * Invoke a payment app with a given option and matching method data. | 36 * Invoke a payment app with a given option and matching method data. |
| 67 * | 37 * |
| 68 * @param webContents The web contents that invoked PaymentRequest. | 38 * @param webContents The web contents that invoked PaymentRequest. |
| 69 * @param registrationId The service worker registration ID of the Payment A
pp. | 39 * @param registrationId The service worker registration ID of the Payment A
pp. |
| 70 * @param optionId The ID of the PaymentOption that was selected by th
e user. | 40 * @param optionId The ID of the PaymentOption that was selected by th
e user. |
| 71 * @param methodData The PaymentMethodData objects that are relevant for
this payment | 41 * @param methodData The PaymentMethodData objects that are relevant for
this payment |
| 72 * app. | 42 * app. |
| 73 * @param total The PaymentItem that represents the total cost of t
he payment. | 43 * @param total The PaymentItem that represents the total cost of t
he payment. |
| 74 * @param modifiers Payment method specific modifiers to the payment it
ems and the total. | 44 * @param modifiers Payment method specific modifiers to the payment it
ems and the total. |
| 75 * @param callback Called after the payment app is finished running. | 45 * @param callback Called after the payment app is finished running. |
| 76 */ | 46 */ |
| 77 public static void invokePaymentApp(WebContents webContents, long registrati
onId, | 47 public static void invokePaymentApp(WebContents webContents, long registrati
onId, |
| 78 String optionId, String origin, String unusedIframeOrigin, | 48 String optionId, String origin, String unusedIframeOrigin, |
| 79 Set<PaymentMethodData> methodData, PaymentItem total, List<PaymentIt
em> displayItems, | 49 Set<PaymentMethodData> methodData, PaymentItem total, List<PaymentIt
em> displayItems, |
| 80 Set<PaymentDetailsModifier> modifiers, | 50 Set<PaymentDetailsModifier> modifiers, |
| 81 PaymentInstrument.InstrumentDetailsCallback callback) { | 51 PaymentInstrument.InstrumentDetailsCallback callback) { |
| 82 nativeInvokePaymentApp(webContents, registrationId, optionId, origin, | 52 nativeInvokePaymentApp(webContents, registrationId, optionId, origin, |
| 83 methodData.toArray(new PaymentMethodData[0]), total, | 53 methodData.toArray(new PaymentMethodData[0]), total, |
| 84 modifiers.toArray(new PaymentDetailsModifier[0]), callback); | 54 modifiers.toArray(new PaymentDetailsModifier[0]), callback); |
| 85 } | 55 } |
| 86 | 56 |
| 87 @CalledByNative | 57 @CalledByNative |
| 88 private static Manifest createManifest(long registrationId, String label, St
ring icon) { | 58 private static List<PaymentInstrument> createInstrumentList() { |
| 89 Manifest manifest = new Manifest(); | 59 return new ArrayList<PaymentInstrument>(); |
| 90 manifest.registrationId = registrationId; | |
| 91 manifest.label = label; | |
| 92 // TODO(tommyt): crbug.com/669876. Handle icons. | |
| 93 manifest.icon = null; | |
| 94 return manifest; | |
| 95 } | 60 } |
| 96 | 61 |
| 97 @CalledByNative | 62 @CalledByNative |
| 98 private static Option createAndAddOption( | 63 private static void addInstrument(List<PaymentInstrument> instruments, WebCo
ntents webContents, |
| 99 Manifest manifest, String id, String label, String icon) { | 64 long swRegistrationId, String instrumentId, String label, String[] m
ethodNames) { |
| 100 Option option = new Option(); | 65 instruments.add(new ServiceWorkerPaymentInstrument(webContents, swRegist
rationId, |
| 101 option.id = id; | 66 instrumentId, label, new HashSet<String>(Arrays.asList(methodNam
es)))); |
| 102 option.label = label; | |
| 103 // TODO(tommyt): crbug.com/669876. Handle icons. | |
| 104 option.icon = null; | |
| 105 manifest.options.add(option); | |
| 106 return option; | |
| 107 } | 67 } |
| 108 | 68 |
| 109 @CalledByNative | 69 @CalledByNative |
| 110 private static void addEnabledMethod(Option option, String enabledMethod) { | |
| 111 option.enabledMethods.add(enabledMethod); | |
| 112 } | |
| 113 | |
| 114 @CalledByNative | |
| 115 private static String[] getSupportedMethodsFromMethodData(PaymentMethodData
data) { | 70 private static String[] getSupportedMethodsFromMethodData(PaymentMethodData
data) { |
| 116 return data.supportedMethods; | 71 return data.supportedMethods; |
| 117 } | 72 } |
| 118 | 73 |
| 119 @CalledByNative | 74 @CalledByNative |
| 120 private static String getStringifiedDataFromMethodData(PaymentMethodData dat
a) { | 75 private static String getStringifiedDataFromMethodData(PaymentMethodData dat
a) { |
| 121 return data.stringifiedData; | 76 return data.stringifiedData; |
| 122 } | 77 } |
| 123 | 78 |
| 124 @CalledByNative | 79 @CalledByNative |
| (...skipping 15 matching lines...) Expand all Loading... |
| 140 private static String getCurrencyFromPaymentItem(PaymentItem item) { | 95 private static String getCurrencyFromPaymentItem(PaymentItem item) { |
| 141 return item.amount.currency; | 96 return item.amount.currency; |
| 142 } | 97 } |
| 143 | 98 |
| 144 @CalledByNative | 99 @CalledByNative |
| 145 private static String getValueFromPaymentItem(PaymentItem item) { | 100 private static String getValueFromPaymentItem(PaymentItem item) { |
| 146 return item.amount.value; | 101 return item.amount.value; |
| 147 } | 102 } |
| 148 | 103 |
| 149 @CalledByNative | 104 @CalledByNative |
| 150 private static void onGotManifest(Manifest manifest, WebContents webContents
, Object callback) { | 105 private static void onPaymentAppCreated(List<PaymentInstrument> instruments, |
| 151 assert callback instanceof PaymentAppFactory.PaymentAppCreatedCallback; | 106 WebContents webContents, PaymentAppFactory.PaymentAppCreatedCallback
callback) { |
| 152 ((PaymentAppFactory.PaymentAppCreatedCallback) callback) | 107 callback.onPaymentAppCreated(new ServiceWorkerPaymentApp(webContents, in
struments)); |
| 153 .onPaymentAppCreated(new ServiceWorkerPaymentApp(webContents, ma
nifest)); | |
| 154 } | 108 } |
| 155 | 109 |
| 156 @CalledByNative | 110 @CalledByNative |
| 157 private static void onGotAllManifests(Object callback) { | 111 private static void onAllPaymentAppsCreated( |
| 158 assert callback instanceof PaymentAppFactory.PaymentAppCreatedCallback; | 112 PaymentAppFactory.PaymentAppCreatedCallback callback) { |
| 159 ((PaymentAppFactory.PaymentAppCreatedCallback) callback).onAllPaymentApp
sCreated(); | 113 callback.onAllPaymentAppsCreated(); |
| 160 } | 114 } |
| 161 | 115 |
| 162 @CalledByNative | 116 @CalledByNative |
| 163 private static void onPaymentAppInvoked( | 117 private static void onPaymentAppInvoked( |
| 164 Object callback, String methodName, String stringifiedDetails) { | 118 Object callback, String methodName, String stringifiedDetails) { |
| 165 assert callback instanceof PaymentInstrument.InstrumentDetailsCallback; | 119 assert callback instanceof PaymentInstrument.InstrumentDetailsCallback; |
| 166 if (methodName == null) { | 120 if (methodName == null) { |
| 167 ((PaymentInstrument.InstrumentDetailsCallback) callback).onInstrumen
tDetailsError(); | 121 ((PaymentInstrument.InstrumentDetailsCallback) callback).onInstrumen
tDetailsError(); |
| 168 } else { | 122 } else { |
| 169 ((PaymentInstrument.InstrumentDetailsCallback) callback) | 123 ((PaymentInstrument.InstrumentDetailsCallback) callback) |
| 170 .onInstrumentDetailsReady(methodName, stringifiedDetails); | 124 .onInstrumentDetailsReady(methodName, stringifiedDetails); |
| 171 } | 125 } |
| 172 } | 126 } |
| 173 | 127 |
| 174 /* | 128 private static native void nativeGetAllPaymentApps( |
| 175 * TODO(tommyt): crbug.com/505554. Change the |callback| parameter below to | 129 WebContents webContents, PaymentAppFactory.PaymentAppCreatedCallback
callback); |
| 176 * be of type PaymentAppFactory.PaymentAppCreatedCallback, once this JNI bug | |
| 177 * has been resolved. | |
| 178 */ | |
| 179 private static native void nativeGetAllAppManifests(WebContents webContents,
Object callback); | |
| 180 | 130 |
| 181 /* | 131 /* |
| 182 * TODO(tommyt): crbug.com/505554. Change the |callback| parameter below to | 132 * TODO(tommyt): crbug.com/505554. Change the |callback| parameter below to |
| 183 * be of type PaymentInstrument.InstrumentDetailsCallback, once this JNI bug | 133 * be of type PaymentInstrument.InstrumentDetailsCallback, once this JNI bug |
| 184 * has been resolved. | 134 * has been resolved. |
| 185 */ | 135 */ |
| 186 private static native void nativeInvokePaymentApp(WebContents webContents, l
ong registrationId, | 136 private static native void nativeInvokePaymentApp(WebContents webContents, l
ong registrationId, |
| 187 String optionId, String origin, PaymentMethodData[] methodData, Paym
entItem total, | 137 String optionId, String origin, PaymentMethodData[] methodData, Paym
entItem total, |
| 188 PaymentDetailsModifier[] modifiers, Object callback); | 138 PaymentDetailsModifier[] modifiers, Object callback); |
| 189 } | 139 } |
| OLD | NEW |