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

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

Issue 2373103002: [Web Payments] Common Payments validation (Closed)
Patch Set: Deps and new field Created 4 years, 1 month 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 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.app.Activity; 7 import android.app.Activity;
8 import android.graphics.Bitmap; 8 import android.graphics.Bitmap;
9 import android.os.Handler; 9 import android.os.Handler;
10 import android.text.TextUtils; 10 import android.text.TextUtils;
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 /** 534 /**
535 * Sets the total, display line items, and shipping options based on input a nd returns the 535 * Sets the total, display line items, and shipping options based on input a nd returns the
536 * status boolean. That status is true for valid data, false for invalid dat a. If the input is 536 * status boolean. That status is true for valid data, false for invalid dat a. If the input is
537 * invalid, disconnects from the client. Both raw and UI versions of data ar e updated. 537 * invalid, disconnects from the client. Both raw and UI versions of data ar e updated.
538 * 538 *
539 * @param details The total, line items, and shipping options to parse, vali date, and save in 539 * @param details The total, line items, and shipping options to parse, vali date, and save in
540 * member variables. 540 * member variables.
541 * @return True if the data is valid. False if the data is invalid. 541 * @return True if the data is valid. False if the data is invalid.
542 */ 542 */
543 private boolean parseAndValidateDetailsOrDisconnectFromClient(PaymentDetails details) { 543 private boolean parseAndValidateDetailsOrDisconnectFromClient(PaymentDetails details) {
544 if (details == null) { 544 if (!PaymentValidator.validatePaymentDetails(details)) {
545 disconnectFromClientWithDebugMessage("Payment details required"); 545 disconnectFromClientWithDebugMessage("Invalid payment details");
546 recordAbortReasonHistogram( 546 recordAbortReasonHistogram(
547 PaymentRequestMetrics.ABORT_REASON_INVALID_DATA_FROM_RENDERE R); 547 PaymentRequestMetrics.ABORT_REASON_INVALID_DATA_FROM_RENDERE R);
548 return false; 548 return false;
549 }
550
551 if (!hasAllPaymentItemFields(details.total)) {
552 disconnectFromClientWithDebugMessage("Invalid total");
553 recordAbortReasonHistogram(
554 PaymentRequestMetrics.ABORT_REASON_INVALID_DATA_FROM_RENDERE R);
555 return false;
556 } 549 }
557 550
558 String totalCurrency = details.total.amount.currency; 551 String totalCurrency = details.total.amount.currency;
559 CurrencyStringFormatter formatter = 552 CurrencyStringFormatter formatter =
560 new CurrencyStringFormatter(totalCurrency, Locale.getDefault()); 553 new CurrencyStringFormatter(totalCurrency, Locale.getDefault());
561 554
562 if (!formatter.isValidAmountCurrencyCode(details.total.amount.currency)) {
563 disconnectFromClientWithDebugMessage("Invalid total amount currency" );
564 recordAbortReasonHistogram(
565 PaymentRequestMetrics.ABORT_REASON_INVALID_DATA_FROM_RENDERE R);
566 return false;
567 }
568
569 if (!formatter.isValidAmountValue(details.total.amount.value)
570 || details.total.amount.value.startsWith("-")) {
571 disconnectFromClientWithDebugMessage("Invalid total amount value");
572 recordAbortReasonHistogram(
573 PaymentRequestMetrics.ABORT_REASON_INVALID_DATA_FROM_RENDERE R);
574 return false;
575 }
576
577 // Total is never pending. 555 // Total is never pending.
578 LineItem uiTotal = new LineItem( 556 LineItem uiTotal = new LineItem(
579 details.total.label, formatter.getFormattedCurrencyCode(), 557 details.total.label, formatter.getFormattedCurrencyCode(),
580 formatter.format(details.total.amount.value), /* isPending */ fa lse); 558 formatter.format(details.total.amount.value), /* isPending */ fa lse);
581 559
582 List<LineItem> uiLineItems = getValidatedLineItems(details.displayItems, totalCurrency, 560 List<LineItem> uiLineItems = getValidatedLineItems(details.displayItems, totalCurrency,
583 formatter); 561 formatter);
584 if (uiLineItems == null) {
585 disconnectFromClientWithDebugMessage("Invalid line items");
586 recordAbortReasonHistogram(
587 PaymentRequestMetrics.ABORT_REASON_INVALID_DATA_FROM_RENDERE R);
588 return false;
589 }
590 562
591 mUiShoppingCart = new ShoppingCart(uiTotal, uiLineItems); 563 mUiShoppingCart = new ShoppingCart(uiTotal, uiLineItems);
592 mRawTotal = details.total; 564 mRawTotal = details.total;
593 mRawLineItems = Arrays.asList(details.displayItems); 565 mRawLineItems = Arrays.asList(details.displayItems);
594 566
595 mUiShippingOptions = getValidatedShippingOptions(details.shippingOptions , totalCurrency, 567 mUiShippingOptions = getValidatedShippingOptions(details.shippingOptions , totalCurrency,
596 formatter); 568 formatter);
597 if (mUiShippingOptions == null) {
598 disconnectFromClientWithDebugMessage("Invalid shipping options");
599 recordAbortReasonHistogram(
600 PaymentRequestMetrics.ABORT_REASON_INVALID_DATA_FROM_RENDERE R);
601 return false;
602 }
603 569
604 return true; 570 return true;
605 } 571 }
606 572
607 /** 573 /**
608 * Returns true if all fields in the payment item are non-null and non-empty . 574 * Returns true if all fields in the payment item are non-null and non-empty .
609 * 575 *
610 * @param item The payment item to examine. 576 * @param item The payment item to examine.
611 * @return True if all fields are present and non-empty. 577 * @return True if all fields are present and non-empty.
612 */ 578 */
613 private static boolean hasAllPaymentItemFields(PaymentItem item) { 579 private static boolean hasAllPaymentItemFields(PaymentItem item) {
614 // "label", "currency", and "value" should be non-empty. 580 // "label", "currency", and "value" should be non-empty.
615 return item != null && !TextUtils.isEmpty(item.label) && item.amount != null 581 return item != null && !TextUtils.isEmpty(item.label) && item.amount != null
616 && !TextUtils.isEmpty(item.amount.currency) 582 && !TextUtils.isEmpty(item.amount.currency)
617 && !TextUtils.isEmpty(item.amount.value); 583 && !TextUtils.isEmpty(item.amount.value);
618 } 584 }
619 585
620 /** 586 /**
621 * Validates a list of payment items and returns their parsed representation or null if invalid. 587 * Validates a list of payment items and returns their parsed representation or null if invalid.
please use gerrit instead 2016/10/31 14:31:15 s/Validates/Converts/ Please remove "or null if i
Kevin Bailey 2016/10/31 20:32:00 Done.
622 * 588 *
623 * @param items The payment items to parse and validate. 589 * @param items The payment items to parse and validate.
624 * @param totalCurrency The currency code for the total amount of payment. 590 * @param totalCurrency The currency code for the total amount of payment.
625 * @param formatter A formatter and validator for the currency amount value. 591 * @param formatter A formatter and validator for the currency amount value.
626 * @return A list of valid line items or null if invalid. 592 * @return A list of valid line items or null if invalid.
please use gerrit instead 2016/10/31 14:31:15 Please remove "or null if invalid" from this comme
Kevin Bailey 2016/10/31 20:32:00 Done.
627 */ 593 */
628 private static List<LineItem> getValidatedLineItems( 594 private static List<LineItem> getValidatedLineItems(
please use gerrit instead 2016/10/31 14:31:15 Please remove the "validated" keyword from the fun
Kevin Bailey 2016/10/31 20:32:00 Done.
629 PaymentItem[] items, String totalCurrency, CurrencyStringFormatter f ormatter) { 595 PaymentItem[] items, String totalCurrency, CurrencyStringFormatter f ormatter) {
630 // Line items are optional. 596 // Line items are optional.
631 if (items == null) return new ArrayList<>(); 597 if (items == null) return new ArrayList<>();
632 598
633 List<LineItem> result = new ArrayList<>(items.length); 599 List<LineItem> result = new ArrayList<>(items.length);
634 for (int i = 0; i < items.length; i++) { 600 for (int i = 0; i < items.length; i++) {
635 PaymentItem item = items[i]; 601 PaymentItem item = items[i];
636 602
637 if (!hasAllPaymentItemFields(item)) return null; 603 if (!hasAllPaymentItemFields(item)) return null;
please use gerrit instead 2016/10/31 14:31:14 You can either assert that this is true or remove
Kevin Bailey 2016/10/31 20:32:00 Done.
638 604
639 // All currencies must match. 605 // All currencies must match.
640 if (!item.amount.currency.equals(totalCurrency)) return null; 606 if (!item.amount.currency.equals(totalCurrency)) return null;
please use gerrit instead 2016/10/31 14:31:14 Ditto
Kevin Bailey 2016/10/31 20:32:00 Done.
641 607
642 // Value should be in correct format. 608 // Value should be in correct format.
643 if (!formatter.isValidAmountValue(item.amount.value)) return null; 609 if (!formatter.isValidAmountValue(item.amount.value)) return null;
please use gerrit instead 2016/10/31 14:31:15 Ditto
Kevin Bailey 2016/10/31 20:32:00 Done.
644 610
645 result.add(new LineItem( 611 result.add(new LineItem(
646 item.label, "", formatter.format(item.amount.value), item.pe nding)); 612 item.label, "", formatter.format(item.amount.value), item.pe nding));
647 } 613 }
648 614
649 return result; 615 return result;
650 } 616 }
651 617
652 /** 618 /**
please use gerrit instead 2016/10/31 14:31:15 Same validation comments for this function.
Kevin Bailey 2016/10/31 20:32:00 Done.
653 * Validates a list of shipping options and returns their parsed representat ion or null if 619 * Validates a list of shipping options and returns their parsed representat ion or null if
654 * invalid. 620 * invalid.
655 * 621 *
656 * @param options The raw shipping options to parse and validate. 622 * @param options The raw shipping options to parse and validate.
657 * @param totalCurrency The currency code for the total amount of payment. 623 * @param totalCurrency The currency code for the total amount of payment.
658 * @param formatter A formatter and validator for the currency amount value. 624 * @param formatter A formatter and validator for the currency amount value.
659 * @return The UI representation of the shipping options or null if invalid. 625 * @return The UI representation of the shipping options or null if invalid.
660 */ 626 */
661 private static SectionInformation getValidatedShippingOptions(PaymentShippin gOption[] options, 627 private static SectionInformation getValidatedShippingOptions(PaymentShippin gOption[] options,
662 String totalCurrency, CurrencyStringFormatter formatter) { 628 String totalCurrency, CurrencyStringFormatter formatter) {
(...skipping 570 matching lines...) Expand 10 before | Expand all | Expand 10 after
1233 "PaymentRequest.CheckoutFunnel.Aborted", abortReason, 1199 "PaymentRequest.CheckoutFunnel.Aborted", abortReason,
1234 PaymentRequestMetrics.ABORT_REASON_MAX); 1200 PaymentRequestMetrics.ABORT_REASON_MAX);
1235 1201
1236 if (abortReason == PaymentRequestMetrics.ABORT_REASON_ABORTED_BY_USER) { 1202 if (abortReason == PaymentRequestMetrics.ABORT_REASON_ABORTED_BY_USER) {
1237 mJourneyLogger.recordJourneyStatsHistograms("UserAborted"); 1203 mJourneyLogger.recordJourneyStatsHistograms("UserAborted");
1238 } else { 1204 } else {
1239 mJourneyLogger.recordJourneyStatsHistograms("OtherAborted"); 1205 mJourneyLogger.recordJourneyStatsHistograms("OtherAborted");
1240 } 1206 }
1241 } 1207 }
1242 } 1208 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698