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