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

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

Issue 2885443003: enable modifiers for native apps (Closed)
Patch Set: more beautify Created 3 years, 7 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 unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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.app.Activity; 7 import android.app.Activity;
8 import android.content.ComponentName; 8 import android.content.ComponentName;
9 import android.content.Context; 9 import android.content.Context;
10 import android.content.DialogInterface; 10 import android.content.DialogInterface;
(...skipping 19 matching lines...) Expand all
30 import org.chromium.content_public.browser.WebContents; 30 import org.chromium.content_public.browser.WebContents;
31 import org.chromium.payments.mojom.PaymentCurrencyAmount; 31 import org.chromium.payments.mojom.PaymentCurrencyAmount;
32 import org.chromium.payments.mojom.PaymentDetailsModifier; 32 import org.chromium.payments.mojom.PaymentDetailsModifier;
33 import org.chromium.payments.mojom.PaymentItem; 33 import org.chromium.payments.mojom.PaymentItem;
34 import org.chromium.payments.mojom.PaymentMethodData; 34 import org.chromium.payments.mojom.PaymentMethodData;
35 import org.chromium.ui.base.WindowAndroid; 35 import org.chromium.ui.base.WindowAndroid;
36 36
37 import java.io.IOException; 37 import java.io.IOException;
38 import java.io.StringWriter; 38 import java.io.StringWriter;
39 import java.util.ArrayList; 39 import java.util.ArrayList;
40 import java.util.Collection;
40 import java.util.Collections; 41 import java.util.Collections;
41 import java.util.HashSet; 42 import java.util.HashSet;
42 import java.util.List; 43 import java.util.List;
43 import java.util.Map; 44 import java.util.Map;
44 import java.util.Set; 45 import java.util.Set;
45 46
46 import javax.annotation.Nullable; 47 import javax.annotation.Nullable;
47 48
48 /** 49 /**
49 * The point of interaction with a locally installed 3rd party native Android pa yment app. 50 * The point of interaction with a locally installed 3rd party native Android pa yment app.
(...skipping 18 matching lines...) Expand all
68 private static final String EXTRA_DEPRECATED_ID = "id"; 69 private static final String EXTRA_DEPRECATED_ID = "id";
69 private static final String EXTRA_DEPRECATED_IFRAME_ORIGIN = "iframeOrigin"; 70 private static final String EXTRA_DEPRECATED_IFRAME_ORIGIN = "iframeOrigin";
70 private static final String EXTRA_DEPRECATED_METHOD_NAME = "methodName"; 71 private static final String EXTRA_DEPRECATED_METHOD_NAME = "methodName";
71 private static final String EXTRA_DEPRECATED_ORIGIN = "origin"; 72 private static final String EXTRA_DEPRECATED_ORIGIN = "origin";
72 73
73 // Freshest parameters sent to the payment app. 74 // Freshest parameters sent to the payment app.
74 private static final String EXTRA_CERTIFICATE = "certificate"; 75 private static final String EXTRA_CERTIFICATE = "certificate";
75 private static final String EXTRA_MERCHANT_NAME = "merchantName"; 76 private static final String EXTRA_MERCHANT_NAME = "merchantName";
76 private static final String EXTRA_METHOD_DATA = "methodData"; 77 private static final String EXTRA_METHOD_DATA = "methodData";
77 private static final String EXTRA_METHOD_NAMES = "methodNames"; 78 private static final String EXTRA_METHOD_NAMES = "methodNames";
79 private static final String EXTRA_MODIFIERS = "modifiers";
78 private static final String EXTRA_PAYMENT_REQUEST_ID = "paymentRequestId"; 80 private static final String EXTRA_PAYMENT_REQUEST_ID = "paymentRequestId";
79 private static final String EXTRA_PAYMENT_REQUEST_ORIGIN = "paymentRequestOr igin"; 81 private static final String EXTRA_PAYMENT_REQUEST_ORIGIN = "paymentRequestOr igin";
80 private static final String EXTRA_TOP_LEVEL_CERTIFICATE_CHAIN = "topLevelCer tificateChain"; 82 private static final String EXTRA_TOP_LEVEL_CERTIFICATE_CHAIN = "topLevelCer tificateChain";
81 private static final String EXTRA_TOP_LEVEL_ORIGIN = "topLevelOrigin"; 83 private static final String EXTRA_TOP_LEVEL_ORIGIN = "topLevelOrigin";
82 private static final String EXTRA_TOTAL = "total"; 84 private static final String EXTRA_TOTAL = "total";
83 85
84 // Response from the payment app. 86 // Response from the payment app.
85 private static final String EXTRA_DEPRECATED_RESPONSE_INSTRUMENT_DETAILS = " instrumentDetails"; 87 private static final String EXTRA_DEPRECATED_RESPONSE_INSTRUMENT_DETAILS = " instrumentDetails";
86 private static final String EXTRA_RESPONSE_DETAILS = "details"; 88 private static final String EXTRA_RESPONSE_DETAILS = "details";
87 private static final String EXTRA_RESPONSE_METHOD_NAME = "methodName"; 89 private static final String EXTRA_RESPONSE_METHOD_NAME = "methodName";
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 return; 332 return;
331 } 333 }
332 334
333 mPayIntent.putExtras(buildExtras(id, merchantName, origin, iframeOrigin, certificateChain, 335 mPayIntent.putExtras(buildExtras(id, merchantName, origin, iframeOrigin, certificateChain,
334 methodDataMap, total, displayItems, modifiers)); 336 methodDataMap, total, displayItems, modifiers));
335 if (!window.showIntent(mPayIntent, this, R.string.payments_android_app_e rror)) { 337 if (!window.showIntent(mPayIntent, this, R.string.payments_android_app_e rror)) {
336 notifyErrorInvokingPaymentApp(); 338 notifyErrorInvokingPaymentApp();
337 } 339 }
338 } 340 }
339 341
340 private static Bundle buildExtras(@Nullable String id, @Nullable String merc hantName, 342 private Bundle buildExtras(@Nullable String id, @Nullable String merchantNam e, String origin,
gogerald1 2017/05/18 21:57:33 you may want put static back here
wuandy1 2017/05/19 01:59:57 Done.
341 String origin, String iframeOrigin, @Nullable byte[][] certificateCh ain, 343 String iframeOrigin, @Nullable byte[][] certificateChain,
342 Map<String, PaymentMethodData> methodDataMap, PaymentItem total, 344 Map<String, PaymentMethodData> methodDataMap, PaymentItem total,
343 @Nullable List<PaymentItem> displayItems, 345 @Nullable List<PaymentItem> displayItems,
344 @Nullable Map<String, PaymentDetailsModifier> modifiers) { 346 @Nullable Map<String, PaymentDetailsModifier> modifiers) {
345 Bundle extras = new Bundle(); 347 Bundle extras = new Bundle();
346 348
347 if (id != null) extras.putString(EXTRA_PAYMENT_REQUEST_ID, id); 349 if (id != null) extras.putString(EXTRA_PAYMENT_REQUEST_ID, id);
348 350
349 if (merchantName != null) extras.putString(EXTRA_MERCHANT_NAME, merchant Name); 351 if (merchantName != null) extras.putString(EXTRA_MERCHANT_NAME, merchant Name);
350 352
351 extras.putString(EXTRA_TOP_LEVEL_ORIGIN, origin); 353 extras.putString(EXTRA_TOP_LEVEL_ORIGIN, origin);
(...skipping 10 matching lines...) Expand all
362 extras.putStringArrayList(EXTRA_METHOD_NAMES, new ArrayList<>(methodData Map.keySet())); 364 extras.putStringArrayList(EXTRA_METHOD_NAMES, new ArrayList<>(methodData Map.keySet()));
363 365
364 Bundle methodDataBundle = new Bundle(); 366 Bundle methodDataBundle = new Bundle();
365 for (Map.Entry<String, PaymentMethodData> methodData : methodDataMap.ent rySet()) { 367 for (Map.Entry<String, PaymentMethodData> methodData : methodDataMap.ent rySet()) {
366 methodDataBundle.putString(methodData.getKey(), 368 methodDataBundle.putString(methodData.getKey(),
367 methodData.getValue() == null ? EMPTY_JSON_DATA 369 methodData.getValue() == null ? EMPTY_JSON_DATA
368 : methodData.getValue().string ifiedData); 370 : methodData.getValue().string ifiedData);
369 } 371 }
370 extras.putParcelable(EXTRA_METHOD_DATA, methodDataBundle); 372 extras.putParcelable(EXTRA_METHOD_DATA, methodDataBundle);
371 373
374 if (modifiers != null) {
375 extras.putString(EXTRA_MODIFIERS, serializeModifiers(modifiers.value s()));
376 }
377
372 String serializedTotalAmount = serializeTotalAmount(total.amount); 378 String serializedTotalAmount = serializeTotalAmount(total.amount);
373 extras.putString(EXTRA_TOTAL, 379 extras.putString(EXTRA_TOTAL,
374 serializedTotalAmount == null ? EMPTY_JSON_DATA : serializedTota lAmount); 380 serializedTotalAmount == null ? EMPTY_JSON_DATA : serializedTota lAmount);
375 381
376 return addDeprecatedExtras(id, origin, iframeOrigin, serializedCertifica teChain, 382 return addDeprecatedExtras(id, origin, iframeOrigin, serializedCertifica teChain,
377 methodDataMap, methodDataBundle, total, displayItems, extras); 383 methodDataMap, methodDataBundle, total, displayItems, extras);
378 } 384 }
379 385
380 private static Bundle addDeprecatedExtras(@Nullable String id, String origin , 386 private static Bundle addDeprecatedExtras(@Nullable String id, String origin ,
381 String iframeOrigin, @Nullable Parcelable[] serializedCertificateCha in, 387 String iframeOrigin, @Nullable Parcelable[] serializedCertificateCha in,
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 json.name("amount").beginObject(); 489 json.name("amount").beginObject();
484 json.name("currency").value(item.amount.currency); 490 json.name("currency").value(item.amount.currency);
485 json.name("value").value(item.amount.value); 491 json.name("value").value(item.amount.value);
486 json.endObject(); 492 json.endObject();
487 // }}} amount 493 // }}} amount
488 494
489 json.endObject(); 495 json.endObject();
490 // }}} item 496 // }}} item
491 } 497 }
492 498
499 private static String serializeModifiers(Collection<PaymentDetailsModifier> modifiers) {
500 StringWriter stringWriter = new StringWriter();
501 JsonWriter json = new JsonWriter(stringWriter);
502 try {
503 json.beginArray();
504 for (PaymentDetailsModifier modifier : modifiers) {
505 serializeModifier(modifier, json);
506 }
507 json.endArray();
508 } catch (IOException e) {
509 return EMPTY_JSON_DATA;
510 }
511 return stringWriter.toString();
512 }
513
514 private static void serializeModifier(PaymentDetailsModifier modifier, JsonW riter json)
515 throws IOException {
516 // {{{
517 json.beginObject();
518
519 if (modifier.total != null) {
gogerald1 2017/05/18 21:57:33 I guess you would do "json.name("total").nullValue
wuandy1 2017/05/19 01:59:57 Done.
520 // total {{{
521 json.name("total");
522 serializePaymentItem(modifier.total, json);
523 // }}} total
524 }
525
526 // supportedMethods {{{
527 json.name("supportedMethods").beginArray();
528 for (String method : modifier.methodData.supportedMethods) {
529 json.value(method);
530 }
531 json.endArray();
532 // }}} supportedMethods
533
534 json.endObject();
535 // }}}
536 }
537
493 @Override 538 @Override
494 public void onIntentCompleted(WindowAndroid window, int resultCode, Intent d ata) { 539 public void onIntentCompleted(WindowAndroid window, int resultCode, Intent d ata) {
495 ThreadUtils.assertOnUiThread(); 540 ThreadUtils.assertOnUiThread();
496 window.removeIntentCallback(this); 541 window.removeIntentCallback(this);
497 if (data == null || data.getExtras() == null || resultCode != Activity.R ESULT_OK) { 542 if (data == null || data.getExtras() == null || resultCode != Activity.R ESULT_OK) {
498 mInstrumentDetailsCallback.onInstrumentDetailsError(); 543 mInstrumentDetailsCallback.onInstrumentDetailsError();
499 } else { 544 } else {
500 String details = data.getExtras().getString(EXTRA_RESPONSE_DETAILS); 545 String details = data.getExtras().getString(EXTRA_RESPONSE_DETAILS);
501 if (details == null) { 546 if (details == null) {
502 details = data.getExtras().getString(EXTRA_DEPRECATED_RESPONSE_I NSTRUMENT_DETAILS); 547 details = data.getExtras().getString(EXTRA_DEPRECATED_RESPONSE_I NSTRUMENT_DETAILS);
503 } 548 }
504 if (details == null) { 549 if (details == null) {
505 details = EMPTY_JSON_DATA; 550 details = EMPTY_JSON_DATA;
506 } 551 }
507 mInstrumentDetailsCallback.onInstrumentDetailsReady( 552 mInstrumentDetailsCallback.onInstrumentDetailsReady(
508 data.getExtras().getString(EXTRA_RESPONSE_METHOD_NAME), deta ils); 553 data.getExtras().getString(EXTRA_RESPONSE_METHOD_NAME), deta ils);
509 } 554 }
510 mInstrumentDetailsCallback = null; 555 mInstrumentDetailsCallback = null;
511 } 556 }
512 557
513 @Override 558 @Override
514 public void dismissInstrument() {} 559 public void dismissInstrument() {}
515 } 560 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698