addressinput_util.cc 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "third_party/libaddressinput/chromium/addressinput_util.h"
  5. #include <stddef.h>
  6. #include <algorithm>
  7. #include "base/check.h"
  8. #include "base/stl_util.h"
  9. #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
  10. #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_metadata.h"
  11. namespace autofill {
  12. namespace addressinput {
  13. namespace {
  14. using ::i18n::addressinput::AddressData;
  15. using ::i18n::addressinput::AddressField;
  16. using ::i18n::addressinput::AddressProblem;
  17. using ::i18n::addressinput::IsFieldRequired;
  18. using ::i18n::addressinput::MISSING_REQUIRED_FIELD;
  19. // Returns true if the |problem| should be reported for the |field| because
  20. // the |filter| is either null, empty or contains it.
  21. bool FilterContains(const std::multimap<AddressField, AddressProblem>* filter,
  22. AddressField field,
  23. AddressProblem problem) {
  24. return filter == nullptr || filter->empty() ||
  25. std::find(filter->begin(), filter->end(),
  26. std::multimap<AddressField, AddressProblem>::value_type(
  27. field, problem)) != filter->end();
  28. }
  29. // Returns true if the |problem| should not be reported for the |field| because
  30. // the |filter| is not null or empty and contains it.
  31. bool FilterExcludes(const std::multimap<AddressField, AddressProblem>* filter,
  32. AddressField field,
  33. AddressProblem problem) {
  34. return filter != nullptr && !filter->empty() &&
  35. std::find(filter->begin(), filter->end(),
  36. std::multimap<AddressField, AddressProblem>::value_type(
  37. field, problem)) != filter->end();
  38. }
  39. static const AddressField kFields[] = {
  40. ::i18n::addressinput::COUNTRY, ::i18n::addressinput::ADMIN_AREA,
  41. ::i18n::addressinput::LOCALITY, ::i18n::addressinput::DEPENDENT_LOCALITY,
  42. ::i18n::addressinput::SORTING_CODE, ::i18n::addressinput::POSTAL_CODE,
  43. ::i18n::addressinput::STREET_ADDRESS,
  44. // ORGANIZATION is never required.
  45. ::i18n::addressinput::RECIPIENT};
  46. } // namespace
  47. bool HasAllRequiredFields(const AddressData& address_to_check) {
  48. std::multimap<AddressField, AddressProblem> problems;
  49. ValidateRequiredFields(address_to_check, nullptr, &problems);
  50. return problems.empty();
  51. }
  52. void ValidateRequiredFields(
  53. const AddressData& address_to_check,
  54. const std::multimap<AddressField, AddressProblem>* inclusion_filter,
  55. std::multimap<AddressField, AddressProblem>* problems) {
  56. DCHECK(problems);
  57. for (auto field : kFields) {
  58. if (address_to_check.IsFieldEmpty(field) &&
  59. IsFieldRequired(field, address_to_check.region_code) &&
  60. FilterContains(inclusion_filter, field, MISSING_REQUIRED_FIELD)) {
  61. problems->insert(std::make_pair(field, MISSING_REQUIRED_FIELD));
  62. }
  63. }
  64. }
  65. void ValidateRequiredFieldsExceptFilteredOut(
  66. const AddressData& address_to_check,
  67. const std::multimap<AddressField, AddressProblem>* exclusion_filter,
  68. std::multimap<AddressField, AddressProblem>* problems) {
  69. DCHECK(problems);
  70. for (auto field : kFields) {
  71. if (address_to_check.IsFieldEmpty(field) &&
  72. IsFieldRequired(field, address_to_check.region_code) &&
  73. !FilterExcludes(exclusion_filter, field, MISSING_REQUIRED_FIELD)) {
  74. problems->insert(std::make_pair(field, MISSING_REQUIRED_FIELD));
  75. }
  76. }
  77. }
  78. } // namespace addressinput
  79. } // namespace autofill