OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/data_model_wrapper.h" | 5 #include "chrome/browser/ui/autofill/data_model_wrapper.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/ui/autofill/autofill_dialog_models.h" | 10 #include "chrome/browser/ui/autofill/autofill_dialog_models.h" |
11 #include "components/autofill/content/browser/wallet/full_wallet.h" | 11 #include "components/autofill/content/browser/wallet/full_wallet.h" |
12 #include "components/autofill/content/browser/wallet/wallet_address.h" | 12 #include "components/autofill/content/browser/wallet/wallet_address.h" |
13 #include "components/autofill/content/browser/wallet/wallet_items.h" | 13 #include "components/autofill/content/browser/wallet/wallet_items.h" |
14 #include "components/autofill/core/browser/autofill_data_model.h" | 14 #include "components/autofill/core/browser/autofill_data_model.h" |
15 #include "components/autofill/core/browser/autofill_profile.h" | 15 #include "components/autofill/core/browser/autofill_profile.h" |
16 #include "components/autofill/core/browser/autofill_type.h" | 16 #include "components/autofill/core/browser/autofill_type.h" |
17 #include "components/autofill/core/browser/credit_card.h" | 17 #include "components/autofill/core/browser/credit_card.h" |
18 #include "components/autofill/core/browser/form_structure.h" | 18 #include "components/autofill/core/browser/form_structure.h" |
19 #include "components/autofill/core/browser/validation.h" | 19 #include "components/autofill/core/browser/validation.h" |
20 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
21 #include "ui/gfx/image/image.h" | 21 #include "ui/gfx/image/image.h" |
22 | 22 |
23 namespace autofill { | 23 namespace autofill { |
24 | 24 |
25 DataModelWrapper::~DataModelWrapper() {} | 25 DataModelWrapper::~DataModelWrapper() {} |
26 | 26 |
27 string16 DataModelWrapper::GetDisplayText() { | 27 string16 DataModelWrapper::GetDisplayText() { |
28 string16 comma = ASCIIToUTF16(", "); | 28 string16 comma = ASCIIToUTF16(", "); |
29 string16 label = GetInfo(NAME_FULL) + comma + GetInfo(ADDRESS_HOME_LINE1); | 29 string16 label = |
30 string16 address2 = GetInfo(ADDRESS_HOME_LINE2); | 30 GetInfo(AutofillType(NAME_FULL)) + comma + |
| 31 GetInfo(AutofillType(ADDRESS_HOME_LINE1)); |
| 32 string16 address2 = GetInfo(AutofillType(ADDRESS_HOME_LINE2)); |
31 if (!address2.empty()) | 33 if (!address2.empty()) |
32 label += comma + address2; | 34 label += comma + address2; |
33 label += ASCIIToUTF16("\n") + | 35 label += ASCIIToUTF16("\n") + |
34 GetInfo(ADDRESS_HOME_CITY) + comma + | 36 GetInfo(AutofillType(ADDRESS_HOME_CITY)) + comma + |
35 GetInfo(ADDRESS_HOME_STATE) + ASCIIToUTF16(" ") + | 37 GetInfo(AutofillType(ADDRESS_HOME_STATE)) + ASCIIToUTF16(" ") + |
36 GetInfo(ADDRESS_HOME_ZIP); | 38 GetInfo(AutofillType(ADDRESS_HOME_ZIP)); |
37 return label; | 39 return label; |
38 } | 40 } |
39 | 41 |
40 bool DataModelWrapper::FillFormStructure( | 42 bool DataModelWrapper::FillFormStructure( |
41 const DetailInputs& inputs, | 43 const DetailInputs& inputs, |
42 const InputFieldComparator& compare, | 44 const InputFieldComparator& compare, |
43 FormStructure* form_structure) const { | 45 FormStructure* form_structure) const { |
44 bool filled_something = false; | 46 bool filled_something = false; |
45 for (size_t i = 0; i < form_structure->field_count(); ++i) { | 47 for (size_t i = 0; i < form_structure->field_count(); ++i) { |
46 AutofillField* field = form_structure->field(i); | 48 AutofillField* field = form_structure->field(i); |
47 for (size_t j = 0; j < inputs.size(); ++j) { | 49 for (size_t j = 0; j < inputs.size(); ++j) { |
48 if (compare.Run(inputs[j], *field)) { | 50 if (compare.Run(inputs[j], *field)) { |
49 FillFormField(field); | 51 FillFormField(field); |
50 filled_something = true; | 52 filled_something = true; |
51 break; | 53 break; |
52 } | 54 } |
53 } | 55 } |
54 } | 56 } |
55 return filled_something; | 57 return filled_something; |
56 } | 58 } |
57 | 59 |
58 void DataModelWrapper::FillInputs(DetailInputs* inputs) { | 60 void DataModelWrapper::FillInputs(DetailInputs* inputs) { |
59 for (size_t i = 0; i < inputs->size(); ++i) { | 61 for (size_t i = 0; i < inputs->size(); ++i) { |
60 (*inputs)[i].initial_value = GetInfo((*inputs)[i].type); | 62 (*inputs)[i].initial_value = GetInfo(AutofillType((*inputs)[i].type)); |
61 } | 63 } |
62 } | 64 } |
63 | 65 |
64 void DataModelWrapper::FillFormField(AutofillField* field) const { | 66 void DataModelWrapper::FillFormField(AutofillField* field) const { |
65 field->value = GetInfo(field->type()); | 67 field->value = GetInfo(field->Type()); |
66 } | 68 } |
67 | 69 |
68 DataModelWrapper::DataModelWrapper() {} | 70 DataModelWrapper::DataModelWrapper() {} |
69 | 71 |
70 gfx::Image DataModelWrapper::GetIcon() { | 72 gfx::Image DataModelWrapper::GetIcon() { |
71 return gfx::Image(); | 73 return gfx::Image(); |
72 } | 74 } |
73 | 75 |
74 // EmptyDataModelWrapper | 76 // EmptyDataModelWrapper |
75 | 77 |
76 EmptyDataModelWrapper::EmptyDataModelWrapper() {} | 78 EmptyDataModelWrapper::EmptyDataModelWrapper() {} |
77 EmptyDataModelWrapper::~EmptyDataModelWrapper() {} | 79 EmptyDataModelWrapper::~EmptyDataModelWrapper() {} |
78 | 80 |
79 string16 EmptyDataModelWrapper::GetInfo(AutofillFieldType type) const { | 81 string16 EmptyDataModelWrapper::GetInfo(const AutofillType& type) const { |
80 return string16(); | 82 return string16(); |
81 } | 83 } |
82 | 84 |
83 void EmptyDataModelWrapper::FillFormField(AutofillField* field) const {} | 85 void EmptyDataModelWrapper::FillFormField(AutofillField* field) const {} |
84 | 86 |
85 // AutofillDataModelWrapper | 87 // AutofillDataModelWrapper |
86 | 88 |
87 AutofillDataModelWrapper::AutofillDataModelWrapper( | 89 AutofillDataModelWrapper::AutofillDataModelWrapper( |
88 const AutofillDataModel* data_model, | 90 const AutofillDataModel* data_model, |
89 size_t variant) | 91 size_t variant) |
90 : data_model_(data_model), | 92 : data_model_(data_model), |
91 variant_(variant) {} | 93 variant_(variant) {} |
92 | 94 |
93 AutofillDataModelWrapper::~AutofillDataModelWrapper() {} | 95 AutofillDataModelWrapper::~AutofillDataModelWrapper() {} |
94 | 96 |
95 string16 AutofillDataModelWrapper::GetInfo(AutofillFieldType type) const { | 97 string16 AutofillDataModelWrapper::GetInfo(const AutofillType& type) const { |
96 return data_model_->GetInfo(type, g_browser_process->GetApplicationLocale()); | 98 return data_model_->GetInfo(type, g_browser_process->GetApplicationLocale()); |
97 } | 99 } |
98 | 100 |
99 void AutofillDataModelWrapper::FillFormField(AutofillField* field) const { | 101 void AutofillDataModelWrapper::FillFormField(AutofillField* field) const { |
100 data_model_->FillFormField( | 102 data_model_->FillFormField( |
101 *field, variant_, g_browser_process->GetApplicationLocale(), field); | 103 *field, variant_, g_browser_process->GetApplicationLocale(), field); |
102 } | 104 } |
103 | 105 |
104 // AutofillProfileWrapper | 106 // AutofillProfileWrapper |
105 | 107 |
106 AutofillProfileWrapper::AutofillProfileWrapper( | 108 AutofillProfileWrapper::AutofillProfileWrapper( |
107 const AutofillProfile* profile, size_t variant) | 109 const AutofillProfile* profile, size_t variant) |
108 : AutofillDataModelWrapper(profile, variant), | 110 : AutofillDataModelWrapper(profile, variant), |
109 profile_(profile) {} | 111 profile_(profile) {} |
110 | 112 |
111 AutofillProfileWrapper::~AutofillProfileWrapper() {} | 113 AutofillProfileWrapper::~AutofillProfileWrapper() {} |
112 | 114 |
113 void AutofillProfileWrapper::FillInputs(DetailInputs* inputs) { | 115 void AutofillProfileWrapper::FillInputs(DetailInputs* inputs) { |
114 const std::string app_locale = g_browser_process->GetApplicationLocale(); | 116 const std::string app_locale = g_browser_process->GetApplicationLocale(); |
115 for (size_t j = 0; j < inputs->size(); ++j) { | 117 for (size_t j = 0; j < inputs->size(); ++j) { |
116 std::vector<string16> values; | 118 std::vector<string16> values; |
117 profile_->GetMultiInfo((*inputs)[j].type, app_locale, &values); | 119 profile_->GetMultiInfo( |
| 120 AutofillType((*inputs)[j].type), app_locale, &values); |
118 (*inputs)[j].initial_value = values[variant()]; | 121 (*inputs)[j].initial_value = values[variant()]; |
119 } | 122 } |
120 } | 123 } |
121 | 124 |
122 void AutofillProfileWrapper::FillFormField(AutofillField* field) const { | 125 void AutofillProfileWrapper::FillFormField(AutofillField* field) const { |
123 AutofillFieldType field_type = field->type(); | 126 AutofillType field_type = field->Type(); |
124 | 127 |
125 if (field_type == CREDIT_CARD_NAME) { | 128 if (field_type.server_type() == CREDIT_CARD_NAME) { |
126 // Requests for the user's credit card are filled from the billing address, | 129 // Requests for the user's credit card are filled from the billing address, |
127 // but the AutofillProfile class doesn't know how to fill credit card | 130 // but the AutofillProfile class doesn't know how to fill credit card |
128 // fields. So, temporarily set the type to the corresponding profile type. | 131 // fields. So, temporarily set the type to the corresponding profile type. |
129 field->set_heuristic_type(NAME_FULL); | 132 field->set_heuristic_type(NAME_FULL); |
130 } | 133 } |
131 | 134 |
132 AutofillDataModelWrapper::FillFormField(field); | 135 AutofillDataModelWrapper::FillFormField(field); |
133 | 136 |
134 field->set_heuristic_type(field_type); | 137 field->set_heuristic_type(field_type.server_type()); |
135 } | 138 } |
136 | 139 |
137 // AutofillCreditCardWrapper | 140 // AutofillCreditCardWrapper |
138 | 141 |
139 AutofillCreditCardWrapper::AutofillCreditCardWrapper(const CreditCard* card) | 142 AutofillCreditCardWrapper::AutofillCreditCardWrapper(const CreditCard* card) |
140 : AutofillDataModelWrapper(card, 0), | 143 : AutofillDataModelWrapper(card, 0), |
141 card_(card) {} | 144 card_(card) {} |
142 | 145 |
143 AutofillCreditCardWrapper::~AutofillCreditCardWrapper() {} | 146 AutofillCreditCardWrapper::~AutofillCreditCardWrapper() {} |
144 | 147 |
145 string16 AutofillCreditCardWrapper::GetInfo(AutofillFieldType type) const { | 148 string16 AutofillCreditCardWrapper::GetInfo(const AutofillType& type) const { |
146 if (type == CREDIT_CARD_EXP_MONTH) | 149 if (type.server_type() == CREDIT_CARD_EXP_MONTH) |
147 return MonthComboboxModel::FormatMonth(card_->expiration_month()); | 150 return MonthComboboxModel::FormatMonth(card_->expiration_month()); |
148 | 151 |
149 return AutofillDataModelWrapper::GetInfo(type); | 152 return AutofillDataModelWrapper::GetInfo(type); |
150 } | 153 } |
151 | 154 |
152 gfx::Image AutofillCreditCardWrapper::GetIcon() { | 155 gfx::Image AutofillCreditCardWrapper::GetIcon() { |
153 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 156 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
154 return rb.GetImageNamed(CreditCard::IconResourceId(card_->type())); | 157 return rb.GetImageNamed(CreditCard::IconResourceId(card_->type())); |
155 } | 158 } |
156 | 159 |
157 string16 AutofillCreditCardWrapper::GetDisplayText() { | 160 string16 AutofillCreditCardWrapper::GetDisplayText() { |
158 if (!card_->IsValid()) | 161 if (!card_->IsValid()) |
159 return string16(); | 162 return string16(); |
160 | 163 |
161 return card_->TypeAndLastFourDigits(); | 164 return card_->TypeAndLastFourDigits(); |
162 } | 165 } |
163 | 166 |
164 // WalletAddressWrapper | 167 // WalletAddressWrapper |
165 | 168 |
166 WalletAddressWrapper::WalletAddressWrapper( | 169 WalletAddressWrapper::WalletAddressWrapper( |
167 const wallet::Address* address) : address_(address) {} | 170 const wallet::Address* address) : address_(address) {} |
168 | 171 |
169 WalletAddressWrapper::~WalletAddressWrapper() {} | 172 WalletAddressWrapper::~WalletAddressWrapper() {} |
170 | 173 |
171 string16 WalletAddressWrapper::GetInfo(AutofillFieldType type) const { | 174 string16 WalletAddressWrapper::GetInfo(const AutofillType& type) const { |
172 return address_->GetInfo(type, g_browser_process->GetApplicationLocale()); | 175 return address_->GetInfo(type, g_browser_process->GetApplicationLocale()); |
173 } | 176 } |
174 | 177 |
175 string16 WalletAddressWrapper::GetDisplayText() { | 178 string16 WalletAddressWrapper::GetDisplayText() { |
176 if (!address_->is_complete_address() || | 179 if (!address_->is_complete_address() || |
177 GetInfo(PHONE_HOME_WHOLE_NUMBER).empty()) { | 180 GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER)).empty()) { |
178 return string16(); | 181 return string16(); |
179 } | 182 } |
180 | 183 |
181 return DataModelWrapper::GetDisplayText(); | 184 return DataModelWrapper::GetDisplayText(); |
182 } | 185 } |
183 | 186 |
184 // WalletInstrumentWrapper | 187 // WalletInstrumentWrapper |
185 | 188 |
186 WalletInstrumentWrapper::WalletInstrumentWrapper( | 189 WalletInstrumentWrapper::WalletInstrumentWrapper( |
187 const wallet::WalletItems::MaskedInstrument* instrument) | 190 const wallet::WalletItems::MaskedInstrument* instrument) |
188 : instrument_(instrument) {} | 191 : instrument_(instrument) {} |
189 | 192 |
190 WalletInstrumentWrapper::~WalletInstrumentWrapper() {} | 193 WalletInstrumentWrapper::~WalletInstrumentWrapper() {} |
191 | 194 |
192 string16 WalletInstrumentWrapper::GetInfo(AutofillFieldType type) const { | 195 string16 WalletInstrumentWrapper::GetInfo(const AutofillType& type) const { |
193 if (type == CREDIT_CARD_EXP_MONTH) | 196 if (type.server_type() == CREDIT_CARD_EXP_MONTH) |
194 return MonthComboboxModel::FormatMonth(instrument_->expiration_month()); | 197 return MonthComboboxModel::FormatMonth(instrument_->expiration_month()); |
195 | 198 |
196 return instrument_->GetInfo(type, g_browser_process->GetApplicationLocale()); | 199 return instrument_->GetInfo(type, g_browser_process->GetApplicationLocale()); |
197 } | 200 } |
198 | 201 |
199 gfx::Image WalletInstrumentWrapper::GetIcon() { | 202 gfx::Image WalletInstrumentWrapper::GetIcon() { |
200 return instrument_->CardIcon(); | 203 return instrument_->CardIcon(); |
201 } | 204 } |
202 | 205 |
203 string16 WalletInstrumentWrapper::GetDisplayText() { | 206 string16 WalletInstrumentWrapper::GetDisplayText() { |
204 // TODO(dbeam): handle other instrument statuses? http://crbug.com/233048 | 207 // TODO(dbeam): handle other instrument statuses? http://crbug.com/233048 |
205 if (instrument_->status() == wallet::WalletItems::MaskedInstrument::EXPIRED || | 208 if (instrument_->status() == wallet::WalletItems::MaskedInstrument::EXPIRED || |
206 !instrument_->address().is_complete_address() || | 209 !instrument_->address().is_complete_address() || |
207 GetInfo(PHONE_HOME_WHOLE_NUMBER).empty()) { | 210 GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER)).empty()) { |
208 return string16(); | 211 return string16(); |
209 } | 212 } |
210 | 213 |
211 // TODO(estade): descriptive_name() is user-provided. Should we use it or | 214 // TODO(estade): descriptive_name() is user-provided. Should we use it or |
212 // just type + last 4 digits? | 215 // just type + last 4 digits? |
213 string16 line1 = instrument_->descriptive_name(); | 216 string16 line1 = instrument_->descriptive_name(); |
214 return line1 + ASCIIToUTF16("\n") + DataModelWrapper::GetDisplayText(); | 217 return line1 + ASCIIToUTF16("\n") + DataModelWrapper::GetDisplayText(); |
215 } | 218 } |
216 | 219 |
217 // FullWalletBillingWrapper | 220 // FullWalletBillingWrapper |
218 | 221 |
219 FullWalletBillingWrapper::FullWalletBillingWrapper( | 222 FullWalletBillingWrapper::FullWalletBillingWrapper( |
220 wallet::FullWallet* full_wallet) | 223 wallet::FullWallet* full_wallet) |
221 : full_wallet_(full_wallet) { | 224 : full_wallet_(full_wallet) { |
222 DCHECK(full_wallet_); | 225 DCHECK(full_wallet_); |
223 } | 226 } |
224 | 227 |
225 FullWalletBillingWrapper::~FullWalletBillingWrapper() {} | 228 FullWalletBillingWrapper::~FullWalletBillingWrapper() {} |
226 | 229 |
227 string16 FullWalletBillingWrapper::GetInfo(AutofillFieldType type) const { | 230 string16 FullWalletBillingWrapper::GetInfo(const AutofillType& type) const { |
228 if (type == CREDIT_CARD_EXP_MONTH) | 231 if (type.server_type() == CREDIT_CARD_EXP_MONTH) |
229 return MonthComboboxModel::FormatMonth(full_wallet_->expiration_month()); | 232 return MonthComboboxModel::FormatMonth(full_wallet_->expiration_month()); |
230 | 233 |
231 if (AutofillType(type).group() == CREDIT_CARD) | 234 if (type.group() == CREDIT_CARD) |
232 return full_wallet_->GetInfo(type); | 235 return full_wallet_->GetInfo(type); |
233 | 236 |
234 return full_wallet_->billing_address()->GetInfo( | 237 return full_wallet_->billing_address()->GetInfo( |
235 type, g_browser_process->GetApplicationLocale()); | 238 type, g_browser_process->GetApplicationLocale()); |
236 } | 239 } |
237 | 240 |
238 string16 FullWalletBillingWrapper::GetDisplayText() { | 241 string16 FullWalletBillingWrapper::GetDisplayText() { |
239 // TODO(dbeam): handle other required actions? http://crbug.com/163508 | 242 // TODO(dbeam): handle other required actions? http://crbug.com/163508 |
240 if (full_wallet_->HasRequiredAction(wallet::UPDATE_EXPIRATION_DATE)) | 243 if (full_wallet_->HasRequiredAction(wallet::UPDATE_EXPIRATION_DATE)) |
241 return string16(); | 244 return string16(); |
242 | 245 |
243 return DataModelWrapper::GetDisplayText(); | 246 return DataModelWrapper::GetDisplayText(); |
244 } | 247 } |
245 | 248 |
246 // FullWalletShippingWrapper | 249 // FullWalletShippingWrapper |
247 | 250 |
248 FullWalletShippingWrapper::FullWalletShippingWrapper( | 251 FullWalletShippingWrapper::FullWalletShippingWrapper( |
249 wallet::FullWallet* full_wallet) | 252 wallet::FullWallet* full_wallet) |
250 : full_wallet_(full_wallet) { | 253 : full_wallet_(full_wallet) { |
251 DCHECK(full_wallet_); | 254 DCHECK(full_wallet_); |
252 } | 255 } |
253 | 256 |
254 FullWalletShippingWrapper::~FullWalletShippingWrapper() {} | 257 FullWalletShippingWrapper::~FullWalletShippingWrapper() {} |
255 | 258 |
256 string16 FullWalletShippingWrapper::GetInfo(AutofillFieldType type) const { | 259 string16 FullWalletShippingWrapper::GetInfo(const AutofillType& type) const { |
257 return full_wallet_->shipping_address()->GetInfo( | 260 return full_wallet_->shipping_address()->GetInfo( |
258 type, g_browser_process->GetApplicationLocale()); | 261 type, g_browser_process->GetApplicationLocale()); |
259 } | 262 } |
260 | 263 |
261 DetailOutputWrapper::DetailOutputWrapper(const DetailOutputMap& outputs) | 264 DetailOutputWrapper::DetailOutputWrapper(const DetailOutputMap& outputs) |
262 : outputs_(outputs) {} | 265 : outputs_(outputs) {} |
263 | 266 |
264 DetailOutputWrapper::~DetailOutputWrapper() {} | 267 DetailOutputWrapper::~DetailOutputWrapper() {} |
265 | 268 |
266 base::string16 DetailOutputWrapper::GetInfo(AutofillFieldType type) const { | 269 base::string16 DetailOutputWrapper::GetInfo(const AutofillType& type) const { |
267 for (DetailOutputMap::const_iterator it = outputs_.begin(); | 270 for (DetailOutputMap::const_iterator it = outputs_.begin(); |
268 it != outputs_.end(); ++it) { | 271 it != outputs_.end(); ++it) { |
269 if (type == it->first->type) | 272 if (type.server_type() == it->first->type) |
270 return it->second; | 273 return it->second; |
271 } | 274 } |
272 return base::string16(); | 275 return base::string16(); |
273 } | 276 } |
274 | 277 |
275 } // namespace autofill | 278 } // namespace autofill |
OLD | NEW |