OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "third_party/libaddressinput/chromium/has_all_required_fields.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/macros.h" |
| 11 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da
ta.h" |
| 12 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_me
tadata.h" |
| 13 |
| 14 namespace autofill { |
| 15 namespace addressinput { |
| 16 |
| 17 namespace { |
| 18 |
| 19 using ::i18n::addressinput::AddressData; |
| 20 using ::i18n::addressinput::AddressField; |
| 21 using ::i18n::addressinput::AddressProblem; |
| 22 using ::i18n::addressinput::IsFieldRequired; |
| 23 |
| 24 using ::i18n::addressinput::MISSING_REQUIRED_FIELD; |
| 25 |
| 26 // Based on ::i18n::addressinput::ValidationTask::ShouldReport(). |
| 27 bool ShouldReport(const std::multimap<AddressField, AddressProblem>* filter, |
| 28 AddressField field, |
| 29 AddressProblem problem) { |
| 30 return filter == NULL || filter->empty() || |
| 31 std::find(filter->begin(), |
| 32 filter->end(), |
| 33 std::multimap<AddressField, AddressProblem>::value_type( |
| 34 field, problem)) != filter->end(); |
| 35 } |
| 36 |
| 37 } // namespace |
| 38 |
| 39 bool HasAllRequiredFields(const AddressData& address_to_check) { |
| 40 std::multimap<AddressField, AddressProblem> problems; |
| 41 ValidateRequiredFields(address_to_check, NULL, &problems); |
| 42 return problems.empty(); |
| 43 } |
| 44 |
| 45 void ValidateRequiredFields( |
| 46 const AddressData& address_to_check, |
| 47 const std::multimap<AddressField, AddressProblem>* filter, |
| 48 std::multimap<AddressField, AddressProblem>* problems) { |
| 49 DCHECK(problems); |
| 50 |
| 51 static const AddressField kFields[] = { |
| 52 ::i18n::addressinput::COUNTRY, |
| 53 ::i18n::addressinput::ADMIN_AREA, |
| 54 ::i18n::addressinput::LOCALITY, |
| 55 ::i18n::addressinput::DEPENDENT_LOCALITY, |
| 56 ::i18n::addressinput::SORTING_CODE, |
| 57 ::i18n::addressinput::POSTAL_CODE, |
| 58 ::i18n::addressinput::STREET_ADDRESS, |
| 59 ::i18n::addressinput::RECIPIENT}; |
| 60 |
| 61 for (size_t i = 0; i < arraysize(kFields); ++i) { |
| 62 AddressField field = kFields[i]; |
| 63 if (address_to_check.IsFieldEmpty(field) && |
| 64 IsFieldRequired(field, address_to_check.region_code) && |
| 65 ShouldReport(filter, field, MISSING_REQUIRED_FIELD)) { |
| 66 problems->insert(std::make_pair(field, MISSING_REQUIRED_FIELD)); |
| 67 } |
| 68 } |
| 69 } |
| 70 |
| 71 } // namespace addressinput |
| 72 } // namespace autofill |
OLD | NEW |