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

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

Issue 11428071: support CC expiration dates in imperative autocomplete dialog. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sync Created 8 years 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 (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 requeted.
Ilya Sherman 2012/11/30 01:01:32 nit: "requeted" -> "requested"
Evan Stade 2012/11/30 22:55:25 Done.
22 bool InputMatchesField(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 InputMatchesField(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
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
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 if (!cc_exp_month_combobox_model_.get())
297 cc_exp_month_combobox_model_.reset(new MonthComboboxModel());
Ilya Sherman 2012/11/30 01:01:32 nit: Why lazy-init and heap-allocate rather than j
Evan Stade 2012/11/30 22:55:25 good point
298 return cc_exp_month_combobox_model_.get();
299
300 case CREDIT_CARD_EXP_4_DIGIT_YEAR:
301 if (!cc_exp_year_combobox_model_.get())
302 cc_exp_year_combobox_model_.reset(new YearComboboxModel());
303 return cc_exp_year_combobox_model_.get();
304
305 default:
306 return NULL;
307 }
308 }
309
274 ui::ComboboxModel* AutofillDialogController::ComboboxModelForSection( 310 ui::ComboboxModel* AutofillDialogController::ComboboxModelForSection(
275 DialogSection section) { 311 DialogSection section) {
276 return SuggestionsModelForSection(section); 312 return SuggestionsModelForSection(section);
277 } 313 }
278 314
279 void AutofillDialogController::ViewClosed(DialogAction action) { 315 void AutofillDialogController::ViewClosed(DialogAction action) {
280 if (action == ACTION_SUBMIT) { 316 if (action == ACTION_SUBMIT) {
281 FillOutputForSection(SECTION_EMAIL); 317 FillOutputForSection(SECTION_EMAIL);
282 FillOutputForSection(SECTION_CC); 318 FillOutputForSection(SECTION_CC);
283 FillOutputForSection(SECTION_BILLING); 319 FillOutputForSection(SECTION_BILLING);
284 if (view_->UseBillingForShipping()) { 320 if (view_->UseBillingForShipping()) {
285 FillOutputForSectionWithComparator( 321 FillOutputForSectionWithComparator(
286 SECTION_BILLING, 322 SECTION_BILLING,
287 base::Bind(DetailInputMatchesFieldWithSection, "shipping")); 323 base::Bind(DetailInputMatchesFieldWithSection, "shipping"));
288 } else { 324 } else {
289 FillOutputForSection(SECTION_SHIPPING); 325 FillOutputForSection(SECTION_SHIPPING);
290 } 326 }
291 } 327 }
292 328
293 callback_.Run(&form_structure_); 329 callback_.Run(&form_structure_);
294 delete this; 330 delete this;
295 } 331 }
296 332
297 void AutofillDialogController::GenerateComboboxModels() { 333 void AutofillDialogController::GenerateComboboxModels() {
298 PersonalDataManager* manager = 334 PersonalDataManager* manager =
299 PersonalDataManagerFactory::GetForProfile(profile_); 335 PersonalDataManagerFactory::GetForProfile(profile_);
300 const std::vector<CreditCard*>& cards = manager->credit_cards(); 336 const std::vector<CreditCard*>& cards = manager->credit_cards();
301 for (size_t i = 0; i < cards.size(); ++i) { 337 for (size_t i = 0; i < cards.size(); ++i) {
302 suggested_cc_.AddItem(cards[i]->Label(), cards[i]->guid()); 338 suggested_cc_.AddItem(cards[i]->guid(), cards[i]->Label());
303 } 339 }
304 suggested_cc_.AddItem(ASCIIToUTF16("Enter new card"), ""); 340 suggested_cc_.AddItem("", ASCIIToUTF16("Enter new card"));
305 341
306 const std::vector<AutofillProfile*>& profiles = manager->GetProfiles(); 342 const std::vector<AutofillProfile*>& profiles = manager->GetProfiles();
307 for (size_t i = 0; i < profiles.size(); ++i) { 343 for (size_t i = 0; i < profiles.size(); ++i) {
308 string16 email = profiles[i]->GetCanonicalizedInfo(EMAIL_ADDRESS); 344 string16 email = profiles[i]->GetCanonicalizedInfo(EMAIL_ADDRESS);
309 if (!email.empty()) 345 if (!email.empty())
310 suggested_email_.AddItem(email, profiles[i]->guid()); 346 suggested_email_.AddItem(profiles[i]->guid(), email);
311 suggested_billing_.AddItem(profiles[i]->Label(), profiles[i]->guid()); 347 suggested_billing_.AddItem(profiles[i]->guid(), profiles[i]->Label());
312 suggested_shipping_.AddItem(profiles[i]->Label(), profiles[i]->guid()); 348 suggested_shipping_.AddItem(profiles[i]->guid(), profiles[i]->Label());
313 } 349 }
314 suggested_billing_.AddItem(ASCIIToUTF16("Enter new billing"), ""); 350 suggested_billing_.AddItem("", ASCIIToUTF16("Enter new billing"));
315 suggested_email_.AddItem(ASCIIToUTF16("Enter new email"), ""); 351 suggested_email_.AddItem("", ASCIIToUTF16("Enter new email"));
316 suggested_shipping_.AddItem(ASCIIToUTF16("Enter new shipping"), ""); 352 suggested_shipping_.AddItem("", ASCIIToUTF16("Enter new shipping"));
317 } 353 }
318 354
319 void AutofillDialogController::PopulateInputsWithGuesses() { 355 void AutofillDialogController::PopulateInputsWithGuesses() {
320 PersonalDataManager* manager = 356 PersonalDataManager* manager =
321 PersonalDataManagerFactory::GetForProfile(profile_); 357 PersonalDataManagerFactory::GetForProfile(profile_);
322 358
323 DetailInputs* profile_inputs[] = { &requested_email_fields_, 359 DetailInputs* profile_inputs[] = { &requested_email_fields_,
324 &requested_billing_fields_ }; 360 &requested_billing_fields_ };
325 const std::vector<AutofillProfile*>& profiles = manager->GetProfiles(); 361 const std::vector<AutofillProfile*>& profiles = manager->GetProfiles();
326 FormGroup* best_profile = 362 FormGroup* best_profile =
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 manager->SaveImportedProfile(profile); 436 manager->SaveImportedProfile(profile);
401 } 437 }
402 } 438 }
403 } 439 }
404 440
405 void AutofillDialogController::FillOutputForSection(DialogSection section) { 441 void AutofillDialogController::FillOutputForSection(DialogSection section) {
406 FillOutputForSectionWithComparator(section, 442 FillOutputForSectionWithComparator(section,
407 base::Bind(DetailInputMatchesField)); 443 base::Bind(DetailInputMatchesField));
408 } 444 }
409 445
410 AutofillDialogController::SuggestionsComboboxModel* AutofillDialogController:: 446 SuggestionsComboboxModel* AutofillDialogController::SuggestionsModelForSection(
411 SuggestionsModelForSection(DialogSection section) { 447 DialogSection section) {
412 switch (section) { 448 switch (section) {
413 case SECTION_EMAIL: 449 case SECTION_EMAIL:
414 return &suggested_email_; 450 return &suggested_email_;
415 case SECTION_CC: 451 case SECTION_CC:
416 return &suggested_cc_; 452 return &suggested_cc_;
417 case SECTION_BILLING: 453 case SECTION_BILLING:
418 return &suggested_billing_; 454 return &suggested_billing_;
419 case SECTION_SHIPPING: 455 case SECTION_SHIPPING:
420 return &suggested_shipping_; 456 return &suggested_shipping_;
421 } 457 }
422 458
423 NOTREACHED(); 459 NOTREACHED();
424 return NULL; 460 return NULL;
425 } 461 }
426 462
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 463 } // namespace autofill
455
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698