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/autofill_dialog_controller.h" | 5 #include "chrome/browser/ui/autofill/autofill_dialog_controller.h" |
6 | 6 |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/autofill/autofill_manager.h" | 9 #include "chrome/browser/autofill/autofill_manager.h" |
10 #include "chrome/browser/autofill/personal_data_manager.h" | 10 #include "chrome/browser/autofill/personal_data_manager.h" |
11 #include "chrome/browser/autofill/personal_data_manager_factory.h" | 11 #include "chrome/browser/autofill/personal_data_manager_factory.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/ui/autofill/autofill_dialog_view.h" | 13 #include "chrome/browser/ui/autofill/autofill_dialog_view.h" |
14 #include "chrome/common/form_data.h" | 14 #include "chrome/common/form_data.h" |
15 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
16 | 16 |
17 namespace autofill { | 17 namespace autofill { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
| 21 // Returns true if |input| should be shown when |field| has been requested. |
| 22 bool InputTypeMatchesFieldType(const DetailInput& input, |
| 23 const AutofillField& field) { |
| 24 // If any credit card expiration info is asked for, show both month and year |
| 25 // inputs. |
| 26 if (field.type() == CREDIT_CARD_EXP_4_DIGIT_YEAR || |
| 27 field.type() == CREDIT_CARD_EXP_2_DIGIT_YEAR || |
| 28 field.type() == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR || |
| 29 field.type() == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR || |
| 30 field.type() == CREDIT_CARD_EXP_MONTH) { |
| 31 return input.type == CREDIT_CARD_EXP_4_DIGIT_YEAR || |
| 32 input.type == CREDIT_CARD_EXP_MONTH; |
| 33 } |
| 34 |
| 35 return input.type == field.type(); |
| 36 } |
| 37 |
21 // Returns true if |input| should be used for a site-requested |field|. If | 38 // Returns true if |input| should be used for a site-requested |field|. If |
22 // non-empty, |section_suffix| overrides the section specified by |input|. | 39 // non-empty, |section_suffix| overrides the section specified by |input|. |
23 bool DetailInputMatchesFieldWithSection(const std::string& section_suffix, | 40 bool DetailInputMatchesFieldWithSection(const std::string& section_suffix, |
24 const DetailInput& input, | 41 const DetailInput& input, |
25 const AutofillField& field) { | 42 const AutofillField& field) { |
26 bool right_section = section_suffix.empty() || | 43 bool right_section = section_suffix.empty() || |
27 EndsWith(field.section(), section_suffix, false); | 44 EndsWith(field.section(), section_suffix, false); |
28 return input.type == field.type() && right_section; | 45 return InputTypeMatchesFieldType(input, field) && right_section; |
29 } | 46 } |
30 | 47 |
31 // Returns true if |input| should be used for a site-requested |field|. | 48 // Returns true if |input| should be used for a site-requested |field|. |
32 bool DetailInputMatchesField(const DetailInput& input, | 49 bool DetailInputMatchesField(const DetailInput& input, |
33 const AutofillField& field) { | 50 const AutofillField& field) { |
34 std::string section_suffix = input.section_suffix ? input.section_suffix : ""; | 51 std::string section_suffix = input.section_suffix ? input.section_suffix : ""; |
35 return DetailInputMatchesFieldWithSection(section_suffix, input, field); | 52 return DetailInputMatchesFieldWithSection(section_suffix, input, field); |
36 } | 53 } |
37 | 54 |
38 // Looks through |input_template| for the types in |requested_data|. Appends | 55 // Looks through |input_template| for the types in |requested_data|. Appends |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 } | 152 } |
136 | 153 |
137 int row_id = 0; | 154 int row_id = 0; |
138 | 155 |
139 const DetailInput kEmailInputs[] = { | 156 const DetailInput kEmailInputs[] = { |
140 { ++row_id, EMAIL_ADDRESS, "Email address" }, | 157 { ++row_id, EMAIL_ADDRESS, "Email address" }, |
141 }; | 158 }; |
142 | 159 |
143 const DetailInput kCCInputs[] = { | 160 const DetailInput kCCInputs[] = { |
144 { ++row_id, CREDIT_CARD_NUMBER, "Card number" }, | 161 { ++row_id, CREDIT_CARD_NUMBER, "Card number" }, |
145 { ++row_id, CREDIT_CARD_EXP_2_DIGIT_YEAR, "Expiration MM/YY" }, | 162 { ++row_id, CREDIT_CARD_EXP_MONTH }, |
| 163 { row_id, CREDIT_CARD_EXP_4_DIGIT_YEAR }, |
146 { row_id, CREDIT_CARD_VERIFICATION_CODE, "CVC" }, | 164 { row_id, CREDIT_CARD_VERIFICATION_CODE, "CVC" }, |
147 { ++row_id, CREDIT_CARD_NAME, "Cardholder name" }, | 165 { ++row_id, CREDIT_CARD_NAME, "Cardholder name" }, |
148 }; | 166 }; |
149 | 167 |
150 const DetailInput kBillingInputs[] = { | 168 const DetailInput kBillingInputs[] = { |
151 { ++row_id, ADDRESS_HOME_LINE1, "Street address", "billing" }, | 169 { ++row_id, ADDRESS_HOME_LINE1, "Street address", "billing" }, |
152 { ++row_id, ADDRESS_HOME_LINE2, "Street address (optional)", "billing" }, | 170 { ++row_id, ADDRESS_HOME_LINE2, "Street address (optional)", "billing" }, |
153 { ++row_id, ADDRESS_HOME_CITY, "City", "billing" }, | 171 { ++row_id, ADDRESS_HOME_CITY, "City", "billing" }, |
154 { ++row_id, ADDRESS_HOME_STATE, "State", "billing" }, | 172 { ++row_id, ADDRESS_HOME_STATE, "State", "billing" }, |
155 { row_id, ADDRESS_HOME_ZIP, "ZIP code", "billing", 0.5 }, | 173 { row_id, ADDRESS_HOME_ZIP, "ZIP code", "billing", 0.5 }, |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 case SECTION_BILLING: | 282 case SECTION_BILLING: |
265 return requested_billing_fields_; | 283 return requested_billing_fields_; |
266 case SECTION_SHIPPING: | 284 case SECTION_SHIPPING: |
267 return requested_shipping_fields_; | 285 return requested_shipping_fields_; |
268 } | 286 } |
269 | 287 |
270 NOTREACHED(); | 288 NOTREACHED(); |
271 return requested_shipping_fields_; | 289 return requested_shipping_fields_; |
272 } | 290 } |
273 | 291 |
| 292 ui::ComboboxModel* AutofillDialogController::ComboboxModelForAutofillType( |
| 293 AutofillFieldType type) { |
| 294 switch (type) { |
| 295 case CREDIT_CARD_EXP_MONTH: |
| 296 return &cc_exp_month_combobox_model_; |
| 297 |
| 298 case CREDIT_CARD_EXP_4_DIGIT_YEAR: |
| 299 return &cc_exp_year_combobox_model_; |
| 300 |
| 301 default: |
| 302 return NULL; |
| 303 } |
| 304 } |
| 305 |
274 ui::ComboboxModel* AutofillDialogController::ComboboxModelForSection( | 306 ui::ComboboxModel* AutofillDialogController::ComboboxModelForSection( |
275 DialogSection section) { | 307 DialogSection section) { |
276 return SuggestionsModelForSection(section); | 308 return SuggestionsModelForSection(section); |
277 } | 309 } |
278 | 310 |
279 void AutofillDialogController::ViewClosed(DialogAction action) { | 311 void AutofillDialogController::ViewClosed(DialogAction action) { |
280 if (action == ACTION_SUBMIT) { | 312 if (action == ACTION_SUBMIT) { |
281 FillOutputForSection(SECTION_EMAIL); | 313 FillOutputForSection(SECTION_EMAIL); |
282 FillOutputForSection(SECTION_CC); | 314 FillOutputForSection(SECTION_CC); |
283 FillOutputForSection(SECTION_BILLING); | 315 FillOutputForSection(SECTION_BILLING); |
284 if (view_->UseBillingForShipping()) { | 316 if (view_->UseBillingForShipping()) { |
285 FillOutputForSectionWithComparator( | 317 FillOutputForSectionWithComparator( |
286 SECTION_BILLING, | 318 SECTION_BILLING, |
287 base::Bind(DetailInputMatchesFieldWithSection, "shipping")); | 319 base::Bind(DetailInputMatchesFieldWithSection, "shipping")); |
288 } else { | 320 } else { |
289 FillOutputForSection(SECTION_SHIPPING); | 321 FillOutputForSection(SECTION_SHIPPING); |
290 } | 322 } |
291 } | 323 } |
292 | 324 |
293 callback_.Run(&form_structure_); | 325 callback_.Run(&form_structure_); |
294 delete this; | 326 delete this; |
295 } | 327 } |
296 | 328 |
297 void AutofillDialogController::GenerateComboboxModels() { | 329 void AutofillDialogController::GenerateComboboxModels() { |
298 PersonalDataManager* manager = | 330 PersonalDataManager* manager = |
299 PersonalDataManagerFactory::GetForProfile(profile_); | 331 PersonalDataManagerFactory::GetForProfile(profile_); |
300 const std::vector<CreditCard*>& cards = manager->credit_cards(); | 332 const std::vector<CreditCard*>& cards = manager->credit_cards(); |
301 for (size_t i = 0; i < cards.size(); ++i) { | 333 for (size_t i = 0; i < cards.size(); ++i) { |
302 suggested_cc_.AddItem(cards[i]->Label(), cards[i]->guid()); | 334 suggested_cc_.AddItem(cards[i]->guid(), cards[i]->Label()); |
303 } | 335 } |
304 suggested_cc_.AddItem(ASCIIToUTF16("Enter new card"), ""); | 336 suggested_cc_.AddItem("", ASCIIToUTF16("Enter new card")); |
305 | 337 |
306 const std::vector<AutofillProfile*>& profiles = manager->GetProfiles(); | 338 const std::vector<AutofillProfile*>& profiles = manager->GetProfiles(); |
307 for (size_t i = 0; i < profiles.size(); ++i) { | 339 for (size_t i = 0; i < profiles.size(); ++i) { |
308 string16 email = profiles[i]->GetCanonicalizedInfo(EMAIL_ADDRESS); | 340 string16 email = profiles[i]->GetCanonicalizedInfo(EMAIL_ADDRESS); |
309 if (!email.empty()) | 341 if (!email.empty()) |
310 suggested_email_.AddItem(email, profiles[i]->guid()); | 342 suggested_email_.AddItem(profiles[i]->guid(), email); |
311 suggested_billing_.AddItem(profiles[i]->Label(), profiles[i]->guid()); | 343 suggested_billing_.AddItem(profiles[i]->guid(), profiles[i]->Label()); |
312 suggested_shipping_.AddItem(profiles[i]->Label(), profiles[i]->guid()); | 344 suggested_shipping_.AddItem(profiles[i]->guid(), profiles[i]->Label()); |
313 } | 345 } |
314 suggested_billing_.AddItem(ASCIIToUTF16("Enter new billing"), ""); | 346 suggested_billing_.AddItem("", ASCIIToUTF16("Enter new billing")); |
315 suggested_email_.AddItem(ASCIIToUTF16("Enter new email"), ""); | 347 suggested_email_.AddItem("", ASCIIToUTF16("Enter new email")); |
316 suggested_shipping_.AddItem(ASCIIToUTF16("Enter new shipping"), ""); | 348 suggested_shipping_.AddItem("", ASCIIToUTF16("Enter new shipping")); |
317 } | 349 } |
318 | 350 |
319 void AutofillDialogController::PopulateInputsWithGuesses() { | 351 void AutofillDialogController::PopulateInputsWithGuesses() { |
320 PersonalDataManager* manager = | 352 PersonalDataManager* manager = |
321 PersonalDataManagerFactory::GetForProfile(profile_); | 353 PersonalDataManagerFactory::GetForProfile(profile_); |
322 | 354 |
323 DetailInputs* profile_inputs[] = { &requested_email_fields_, | 355 DetailInputs* profile_inputs[] = { &requested_email_fields_, |
324 &requested_billing_fields_ }; | 356 &requested_billing_fields_ }; |
325 const std::vector<AutofillProfile*>& profiles = manager->GetProfiles(); | 357 const std::vector<AutofillProfile*>& profiles = manager->GetProfiles(); |
326 FormGroup* best_profile = | 358 FormGroup* best_profile = |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 manager->SaveImportedProfile(profile); | 432 manager->SaveImportedProfile(profile); |
401 } | 433 } |
402 } | 434 } |
403 } | 435 } |
404 | 436 |
405 void AutofillDialogController::FillOutputForSection(DialogSection section) { | 437 void AutofillDialogController::FillOutputForSection(DialogSection section) { |
406 FillOutputForSectionWithComparator(section, | 438 FillOutputForSectionWithComparator(section, |
407 base::Bind(DetailInputMatchesField)); | 439 base::Bind(DetailInputMatchesField)); |
408 } | 440 } |
409 | 441 |
410 AutofillDialogController::SuggestionsComboboxModel* AutofillDialogController:: | 442 SuggestionsComboboxModel* AutofillDialogController::SuggestionsModelForSection( |
411 SuggestionsModelForSection(DialogSection section) { | 443 DialogSection section) { |
412 switch (section) { | 444 switch (section) { |
413 case SECTION_EMAIL: | 445 case SECTION_EMAIL: |
414 return &suggested_email_; | 446 return &suggested_email_; |
415 case SECTION_CC: | 447 case SECTION_CC: |
416 return &suggested_cc_; | 448 return &suggested_cc_; |
417 case SECTION_BILLING: | 449 case SECTION_BILLING: |
418 return &suggested_billing_; | 450 return &suggested_billing_; |
419 case SECTION_SHIPPING: | 451 case SECTION_SHIPPING: |
420 return &suggested_shipping_; | 452 return &suggested_shipping_; |
421 } | 453 } |
422 | 454 |
423 NOTREACHED(); | 455 NOTREACHED(); |
424 return NULL; | 456 return NULL; |
425 } | 457 } |
426 | 458 |
427 // SuggestionsComboboxModel ---------------------------------------------------- | |
428 | |
429 AutofillDialogController::SuggestionsComboboxModel::SuggestionsComboboxModel() { | |
430 } | |
431 | |
432 AutofillDialogController::SuggestionsComboboxModel:: | |
433 ~SuggestionsComboboxModel() {} | |
434 | |
435 void AutofillDialogController::SuggestionsComboboxModel::AddItem( | |
436 const string16& item, const std::string& key) { | |
437 items_.push_back(std::make_pair(key, item)); | |
438 } | |
439 | |
440 std::string AutofillDialogController::SuggestionsComboboxModel::GetItemKeyAt( | |
441 int index) { | |
442 return items_[index].first; | |
443 } | |
444 | |
445 int AutofillDialogController::SuggestionsComboboxModel::GetItemCount() const { | |
446 return items_.size(); | |
447 } | |
448 | |
449 string16 AutofillDialogController::SuggestionsComboboxModel::GetItemAt( | |
450 int index) { | |
451 return items_[index].second; | |
452 } | |
453 | |
454 } // namespace autofill | 459 } // namespace autofill |
455 | |
OLD | NEW |