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