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

Side by Side Diff: chrome/browser/ui/autofill/autofill_dialog_i18n_input.cc

Issue 145553009: rAc: use libaddressinput to validate international addresses. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 6 years, 10 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 | Annotate | Revision Log
OLDNEW
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 "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h" 5 #include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/strings/string_split.h"
8 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/common/chrome_switches.h" 10 #include "chrome/common/chrome_switches.h"
10 #include "components/autofill/core/browser/autofill_profile.h" 11 #include "components/autofill/core/browser/autofill_profile.h"
11 #include "components/autofill/core/browser/credit_card.h" 12 #include "components/autofill/core/browser/credit_card.h"
12 #include "components/autofill/core/browser/field_types.h" 13 #include "components/autofill/core/browser/field_types.h"
13 #include "grit/component_strings.h" 14 #include "grit/component_strings.h"
15 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_data.h"
14 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_field.h" 16 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_field.h"
15 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_ui.h" 17 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_ui.h"
16 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_ui_component.h" 18 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_ui_component.h"
17 #include "ui/base/l10n/l10n_util.h" 19 #include "ui/base/l10n/l10n_util.h"
18 20
19 namespace autofill { 21 namespace autofill {
20 namespace i18ninput { 22 namespace i18ninput {
21 23
22 namespace { 24 namespace {
23 25
26 using base::UTF16ToUTF8;
27 using ::i18n::addressinput::AddressData;
24 using ::i18n::addressinput::AddressField; 28 using ::i18n::addressinput::AddressField;
25 using ::i18n::addressinput::AddressUiComponent; 29 using ::i18n::addressinput::AddressUiComponent;
26 30
27 ServerFieldType AddressFieldToServerFieldType(AddressField address_field,
28 bool billing) {
29 switch (address_field) {
30 case ::i18n::addressinput::COUNTRY:
31 return billing ? ADDRESS_BILLING_COUNTRY : ADDRESS_HOME_COUNTRY;
32 case ::i18n::addressinput::ADMIN_AREA:
33 return billing ? ADDRESS_BILLING_STATE : ADDRESS_HOME_STATE;
34 case ::i18n::addressinput::LOCALITY:
35 return billing ? ADDRESS_BILLING_CITY : ADDRESS_HOME_CITY;
36 case ::i18n::addressinput::DEPENDENT_LOCALITY:
37 return billing ? ADDRESS_BILLING_DEPENDENT_LOCALITY :
38 ADDRESS_HOME_DEPENDENT_LOCALITY;
39 case ::i18n::addressinput::POSTAL_CODE:
40 return billing ? ADDRESS_BILLING_ZIP : ADDRESS_HOME_ZIP;
41 case ::i18n::addressinput::SORTING_CODE:
42 return billing ? ADDRESS_BILLING_SORTING_CODE : ADDRESS_HOME_SORTING_CODE;
43 case ::i18n::addressinput::STREET_ADDRESS:
44 return billing ? ADDRESS_BILLING_LINE1 : ADDRESS_HOME_LINE1;
45 case ::i18n::addressinput::RECIPIENT:
46 return billing ? NAME_BILLING_FULL : NAME_FULL;
47 case ::i18n::addressinput::ORGANIZATION:
48 return COMPANY_NAME;
49 }
50 NOTREACHED();
51 return UNKNOWN_TYPE;
52 }
53
54 DetailInput::Length LengthFromHint(AddressUiComponent::LengthHint hint) { 31 DetailInput::Length LengthFromHint(AddressUiComponent::LengthHint hint) {
55 if (hint == AddressUiComponent::HINT_SHORT) 32 if (hint == AddressUiComponent::HINT_SHORT)
56 return DetailInput::SHORT; 33 return DetailInput::SHORT;
57 DCHECK_EQ(hint, AddressUiComponent::HINT_LONG); 34 DCHECK_EQ(hint, AddressUiComponent::HINT_LONG);
58 return DetailInput::LONG; 35 return DetailInput::LONG;
59 } 36 }
60 37
61 } // namespace 38 } // namespace
62 39
63 bool Enabled() { 40 bool Enabled() {
64 CommandLine* command_line = CommandLine::ForCurrentProcess(); 41 CommandLine* command_line = CommandLine::ForCurrentProcess();
65 return command_line->HasSwitch(::switches::kEnableAutofillAddressI18n); 42 return command_line->HasSwitch(::switches::kEnableAutofillAddressI18n);
66 } 43 }
67 44
68 void BuildAddressInputs(common::AddressType address_type, 45 void BuildAddressInputs(common::AddressType address_type,
69 const std::string& country_code, 46 const std::string& country_code,
70 DetailInputs* inputs) { 47 DetailInputs* inputs) {
71 std::vector<AddressUiComponent> components( 48 std::vector<AddressUiComponent> components(
72 ::i18n::addressinput::BuildComponents(country_code)); 49 ::i18n::addressinput::BuildComponents(country_code));
73 50
74 const bool billing = address_type == common::ADDRESS_TYPE_BILLING; 51 const bool billing = address_type == common::ADDRESS_TYPE_BILLING;
75 52
76 for (size_t i = 0; i < components.size(); ++i) { 53 for (size_t i = 0; i < components.size(); ++i) {
77 const AddressUiComponent& component = components[i]; 54 const AddressUiComponent& component = components[i];
78 if (component.field == ::i18n::addressinput::ORGANIZATION) { 55 if (component.field == ::i18n::addressinput::ORGANIZATION) {
79 // TODO(dbeam): figure out when we actually need this. 56 // TODO(dbeam): figure out when we actually need this.
80 continue; 57 continue;
81 } 58 }
82 59
83 ServerFieldType server_type = AddressFieldToServerFieldType(component.field, 60 ServerFieldType server_type = TypeForField(component.field, address_type);
84 billing);
85 DetailInput::Length length = LengthFromHint(component.length_hint); 61 DetailInput::Length length = LengthFromHint(component.length_hint);
86 base::string16 placeholder = l10n_util::GetStringUTF16(component.name_id); 62 base::string16 placeholder = l10n_util::GetStringUTF16(component.name_id);
87 DetailInput input = { length, server_type, placeholder }; 63 DetailInput input = { length, server_type, placeholder };
88 inputs->push_back(input); 64 inputs->push_back(input);
89 65
90 if (component.field == ::i18n::addressinput::STREET_ADDRESS && 66 if (component.field == ::i18n::addressinput::STREET_ADDRESS &&
91 component.length_hint == AddressUiComponent::HINT_LONG) { 67 component.length_hint == AddressUiComponent::HINT_LONG) {
92 // TODO(dbeam): support more than 2 address lines. http://crbug.com/324889 68 // TODO(dbeam): support more than 2 address lines. http://crbug.com/324889
93 ServerFieldType server_type = 69 ServerFieldType server_type =
94 billing ? ADDRESS_BILLING_LINE2 : ADDRESS_HOME_LINE2; 70 billing ? ADDRESS_BILLING_LINE2 : ADDRESS_HOME_LINE2;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 106
131 base::string16 country_code = profile.GetRawInfo(ADDRESS_HOME_COUNTRY); 107 base::string16 country_code = profile.GetRawInfo(ADDRESS_HOME_COUNTRY);
132 if (country_code.empty()) 108 if (country_code.empty())
133 return false; 109 return false;
134 110
135 std::vector<AddressField> required_fields = 111 std::vector<AddressField> required_fields =
136 ::i18n::addressinput::GetRequiredFields(base::UTF16ToUTF8(country_code)); 112 ::i18n::addressinput::GetRequiredFields(base::UTF16ToUTF8(country_code));
137 113
138 for (size_t i = 0; i < required_fields.size(); ++i) { 114 for (size_t i = 0; i < required_fields.size(); ++i) {
139 ServerFieldType type = 115 ServerFieldType type =
140 AddressFieldToServerFieldType(required_fields[i], false); 116 TypeForField(required_fields[i], common::ADDRESS_TYPE_SHIPPING);
141 if (profile.GetRawInfo(type).empty()) 117 if (profile.GetRawInfo(type).empty())
142 return false; 118 return false;
143 } 119 }
144 120
145 const ServerFieldType more_required_fields[] = { 121 const ServerFieldType more_required_fields[] = {
146 NAME_FULL, 122 NAME_FULL,
147 EMAIL_ADDRESS, 123 EMAIL_ADDRESS,
148 PHONE_HOME_WHOLE_NUMBER 124 PHONE_HOME_WHOLE_NUMBER
149 }; 125 };
150 126
151 for (size_t i = 0; i < arraysize(more_required_fields); ++i) { 127 for (size_t i = 0; i < arraysize(more_required_fields); ++i) {
152 if (profile.GetRawInfo(more_required_fields[i]).empty()) 128 if (profile.GetRawInfo(more_required_fields[i]).empty())
153 return false; 129 return false;
154 } 130 }
155 131
156 return true; 132 return true;
157 } 133 }
158 134
135 ServerFieldType TypeForField(AddressField address_field,
136 common::AddressType address_type) {
137 bool billing = address_type == common::ADDRESS_TYPE_BILLING;
138 switch (address_field) {
139 case ::i18n::addressinput::COUNTRY:
140 return billing ? ADDRESS_BILLING_COUNTRY : ADDRESS_HOME_COUNTRY;
141 case ::i18n::addressinput::ADMIN_AREA:
142 return billing ? ADDRESS_BILLING_STATE : ADDRESS_HOME_STATE;
143 case ::i18n::addressinput::LOCALITY:
144 return billing ? ADDRESS_BILLING_CITY : ADDRESS_HOME_CITY;
145 case ::i18n::addressinput::DEPENDENT_LOCALITY:
146 return billing ? ADDRESS_BILLING_DEPENDENT_LOCALITY :
147 ADDRESS_HOME_DEPENDENT_LOCALITY;
148 case ::i18n::addressinput::POSTAL_CODE:
149 return billing ? ADDRESS_BILLING_ZIP : ADDRESS_HOME_ZIP;
150 case ::i18n::addressinput::SORTING_CODE:
151 return billing ? ADDRESS_BILLING_SORTING_CODE : ADDRESS_HOME_SORTING_CODE;
152 case ::i18n::addressinput::STREET_ADDRESS:
153 return billing ? ADDRESS_BILLING_LINE1 : ADDRESS_HOME_LINE1;
154 case ::i18n::addressinput::RECIPIENT:
155 return billing ? NAME_BILLING_FULL : NAME_FULL;
156 case ::i18n::addressinput::ORGANIZATION:
157 return COMPANY_NAME;
158 }
159 NOTREACHED();
160 return UNKNOWN_TYPE;
161 }
162
163 void CreateAddressData(
164 const base::Callback<base::string16(const AutofillType&)>& get_info,
165 AddressData* address_data) {
166 address_data->recipient = UTF16ToUTF8(get_info.Run(AutofillType(NAME_FULL)));
167 address_data->country_code = UTF16ToUTF8(
168 get_info.Run(AutofillType(HTML_TYPE_COUNTRY_CODE, HTML_MODE_SHIPPING)));
169 DCHECK_EQ(2U, address_data->country_code.size());
170 address_data->administrative_area = UTF16ToUTF8(
171 get_info.Run(AutofillType(ADDRESS_HOME_STATE)));
172 address_data->locality = UTF16ToUTF8(
173 get_info.Run(AutofillType(ADDRESS_HOME_CITY)));
174 address_data->dependent_locality = UTF16ToUTF8(
175 get_info.Run(AutofillType(ADDRESS_HOME_DEPENDENT_LOCALITY)));
176 address_data->sorting_code = UTF16ToUTF8(
177 get_info.Run(AutofillType(ADDRESS_HOME_SORTING_CODE)));
178 address_data->postal_code = UTF16ToUTF8(
179 get_info.Run(AutofillType(ADDRESS_HOME_ZIP)));
180 base::SplitString(
181 UTF16ToUTF8(get_info.Run(AutofillType(ADDRESS_HOME_STREET_ADDRESS))),
182 '\n',
183 &address_data->address_lines);
184 }
185
159 } // namespace i18ninput 186 } // namespace i18ninput
160 } // namespace autofill 187 } // namespace autofill
OLDNEW
« no previous file with comments | « chrome/browser/ui/autofill/autofill_dialog_i18n_input.h ('k') | chrome/browser/ui/autofill/data_model_wrapper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698