| 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 requeted. |
| 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 } | 142 } |
| 126 | 143 |
| 127 int row_id = 0; | 144 int row_id = 0; |
| 128 | 145 |
| 129 const DetailInput kEmailInputs[] = { | 146 const DetailInput kEmailInputs[] = { |
| 130 { ++row_id, EMAIL_ADDRESS, "Email address" }, | 147 { ++row_id, EMAIL_ADDRESS, "Email address" }, |
| 131 }; | 148 }; |
| 132 | 149 |
| 133 const DetailInput kCCInputs[] = { | 150 const DetailInput kCCInputs[] = { |
| 134 { ++row_id, CREDIT_CARD_NUMBER, "Card number" }, | 151 { ++row_id, CREDIT_CARD_NUMBER, "Card number" }, |
| 135 { ++row_id, CREDIT_CARD_EXP_2_DIGIT_YEAR, "Expiration MM/YY" }, | 152 { ++row_id, CREDIT_CARD_EXP_MONTH }, |
| 153 { row_id, CREDIT_CARD_EXP_4_DIGIT_YEAR }, |
| 136 { row_id, CREDIT_CARD_VERIFICATION_CODE, "CVC" }, | 154 { row_id, CREDIT_CARD_VERIFICATION_CODE, "CVC" }, |
| 137 { ++row_id, CREDIT_CARD_NAME, "Cardholder name" }, | 155 { ++row_id, CREDIT_CARD_NAME, "Cardholder name" }, |
| 138 }; | 156 }; |
| 139 | 157 |
| 140 const DetailInput kBillingInputs[] = { | 158 const DetailInput kBillingInputs[] = { |
| 141 { ++row_id, ADDRESS_HOME_LINE1, "Street address", "billing" }, | 159 { ++row_id, ADDRESS_HOME_LINE1, "Street address", "billing" }, |
| 142 { ++row_id, ADDRESS_HOME_LINE2, "Street address (optional)", "billing" }, | 160 { ++row_id, ADDRESS_HOME_LINE2, "Street address (optional)", "billing" }, |
| 143 { ++row_id, ADDRESS_HOME_CITY, "City", "billing" }, | 161 { ++row_id, ADDRESS_HOME_CITY, "City", "billing" }, |
| 144 { ++row_id, ADDRESS_HOME_STATE, "State", "billing" }, | 162 { ++row_id, ADDRESS_HOME_STATE, "State", "billing" }, |
| 145 { row_id, ADDRESS_HOME_ZIP, "ZIP code", "billing", 0.5 }, | 163 { row_id, ADDRESS_HOME_ZIP, "ZIP code", "billing", 0.5 }, |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 case SECTION_BILLING: | 272 case SECTION_BILLING: |
| 255 return requested_billing_fields_; | 273 return requested_billing_fields_; |
| 256 case SECTION_SHIPPING: | 274 case SECTION_SHIPPING: |
| 257 return requested_shipping_fields_; | 275 return requested_shipping_fields_; |
| 258 } | 276 } |
| 259 | 277 |
| 260 NOTREACHED(); | 278 NOTREACHED(); |
| 261 return requested_shipping_fields_; | 279 return requested_shipping_fields_; |
| 262 } | 280 } |
| 263 | 281 |
| 282 ui::ComboboxModel* AutofillDialogController::ComboboxModelForAutofillType( |
| 283 AutofillFieldType type) { |
| 284 switch (type) { |
| 285 case CREDIT_CARD_EXP_MONTH: |
| 286 if (!cc_exp_month_combobox_model_.get()) |
| 287 cc_exp_month_combobox_model_.reset(new MonthComboboxModel()); |
| 288 return cc_exp_month_combobox_model_.get(); |
| 289 |
| 290 case CREDIT_CARD_EXP_4_DIGIT_YEAR: |
| 291 if (!cc_exp_year_combobox_model_.get()) |
| 292 cc_exp_year_combobox_model_.reset(new YearComboboxModel()); |
| 293 return cc_exp_year_combobox_model_.get(); |
| 294 |
| 295 default: |
| 296 return NULL; |
| 297 } |
| 298 } |
| 299 |
| 264 ui::ComboboxModel* AutofillDialogController::ComboboxModelForSection( | 300 ui::ComboboxModel* AutofillDialogController::ComboboxModelForSection( |
| 265 DialogSection section) { | 301 DialogSection section) { |
| 266 return SuggestionsModelForSection(section); | 302 return SuggestionsModelForSection(section); |
| 267 } | 303 } |
| 268 | 304 |
| 269 void AutofillDialogController::ViewClosed(DialogAction action) { | 305 void AutofillDialogController::ViewClosed(DialogAction action) { |
| 270 if (action == ACTION_SUBMIT) { | 306 if (action == ACTION_SUBMIT) { |
| 271 FillOutputForSection(SECTION_EMAIL); | 307 FillOutputForSection(SECTION_EMAIL); |
| 272 FillOutputForSection(SECTION_CC); | 308 FillOutputForSection(SECTION_CC); |
| 273 FillOutputForSection(SECTION_BILLING); | 309 FillOutputForSection(SECTION_BILLING); |
| 274 if (view_->UseBillingForShipping()) { | 310 if (view_->UseBillingForShipping()) { |
| 275 FillOutputForSectionWithComparator( | 311 FillOutputForSectionWithComparator( |
| 276 SECTION_BILLING, | 312 SECTION_BILLING, |
| 277 base::Bind(DetailInputMatchesFieldWithSection, "shipping")); | 313 base::Bind(DetailInputMatchesFieldWithSection, "shipping")); |
| 278 } else { | 314 } else { |
| 279 FillOutputForSection(SECTION_SHIPPING); | 315 FillOutputForSection(SECTION_SHIPPING); |
| 280 } | 316 } |
| 281 // TODO(estade): pass the result along to the page. | 317 // TODO(estade): pass the result along to the page. |
| 282 } | 318 } |
| 283 | 319 |
| 284 callback_.Run(&form_structure_); | 320 callback_.Run(&form_structure_); |
| 285 delete this; | 321 delete this; |
| 286 } | 322 } |
| 287 | 323 |
| 288 void AutofillDialogController::GenerateComboboxModels() { | 324 void AutofillDialogController::GenerateComboboxModels() { |
| 289 PersonalDataManager* manager = | 325 PersonalDataManager* manager = |
| 290 PersonalDataManagerFactory::GetForProfile(profile_); | 326 PersonalDataManagerFactory::GetForProfile(profile_); |
| 291 const std::vector<CreditCard*>& cards = manager->credit_cards(); | 327 const std::vector<CreditCard*>& cards = manager->credit_cards(); |
| 292 for (size_t i = 0; i < cards.size(); ++i) { | 328 for (size_t i = 0; i < cards.size(); ++i) { |
| 293 suggested_cc_.AddItem(cards[i]->Label(), cards[i]->guid()); | 329 suggested_cc_.AddItem(cards[i]->guid(), cards[i]->Label()); |
| 294 } | 330 } |
| 295 suggested_cc_.AddItem(ASCIIToUTF16("Enter new card"), ""); | 331 suggested_cc_.AddItem("", ASCIIToUTF16("Enter new card")); |
| 296 | 332 |
| 297 const std::vector<AutofillProfile*>& profiles = manager->profiles(); | 333 const std::vector<AutofillProfile*>& profiles = manager->profiles(); |
| 298 for (size_t i = 0; i < profiles.size(); ++i) { | 334 for (size_t i = 0; i < profiles.size(); ++i) { |
| 299 string16 email = profiles[i]->GetCanonicalizedInfo(EMAIL_ADDRESS); | 335 string16 email = profiles[i]->GetCanonicalizedInfo(EMAIL_ADDRESS); |
| 300 if (!email.empty()) | 336 if (!email.empty()) |
| 301 suggested_email_.AddItem(email, profiles[i]->guid()); | 337 suggested_email_.AddItem(profiles[i]->guid(), email); |
| 302 suggested_billing_.AddItem(profiles[i]->Label(), profiles[i]->guid()); | 338 suggested_billing_.AddItem(profiles[i]->guid(), profiles[i]->Label()); |
| 303 suggested_shipping_.AddItem(profiles[i]->Label(), profiles[i]->guid()); | 339 suggested_shipping_.AddItem(profiles[i]->guid(), profiles[i]->Label()); |
| 304 } | 340 } |
| 305 suggested_billing_.AddItem(ASCIIToUTF16("Enter new billing"), ""); | 341 suggested_billing_.AddItem("", ASCIIToUTF16("Enter new billing")); |
| 306 suggested_email_.AddItem(ASCIIToUTF16("Enter new email"), ""); | 342 suggested_email_.AddItem("", ASCIIToUTF16("Enter new email")); |
| 307 suggested_shipping_.AddItem(ASCIIToUTF16("Enter new shipping"), ""); | 343 suggested_shipping_.AddItem("", ASCIIToUTF16("Enter new shipping")); |
| 308 } | 344 } |
| 309 | 345 |
| 310 void AutofillDialogController::PopulateInputsWithGuesses() { | 346 void AutofillDialogController::PopulateInputsWithGuesses() { |
| 311 PersonalDataManager* manager = | 347 PersonalDataManager* manager = |
| 312 PersonalDataManagerFactory::GetForProfile(profile_); | 348 PersonalDataManagerFactory::GetForProfile(profile_); |
| 313 | 349 |
| 314 DetailInputs* profile_inputs[] = { &requested_email_fields_, | 350 DetailInputs* profile_inputs[] = { &requested_email_fields_, |
| 315 &requested_billing_fields_ }; | 351 &requested_billing_fields_ }; |
| 316 const std::vector<AutofillProfile*>& profiles = manager->profiles(); | 352 const std::vector<AutofillProfile*>& profiles = manager->profiles(); |
| 317 FormGroup* best_profile = | 353 FormGroup* best_profile = |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 } | 410 } |
| 375 } | 411 } |
| 376 } | 412 } |
| 377 } | 413 } |
| 378 | 414 |
| 379 void AutofillDialogController::FillOutputForSection(DialogSection section) { | 415 void AutofillDialogController::FillOutputForSection(DialogSection section) { |
| 380 FillOutputForSectionWithComparator(section, | 416 FillOutputForSectionWithComparator(section, |
| 381 base::Bind(DetailInputMatchesField)); | 417 base::Bind(DetailInputMatchesField)); |
| 382 } | 418 } |
| 383 | 419 |
| 384 AutofillDialogController::SuggestionsComboboxModel* AutofillDialogController:: | 420 SuggestionsComboboxModel* AutofillDialogController::SuggestionsModelForSection( |
| 385 SuggestionsModelForSection(DialogSection section) { | 421 DialogSection section) { |
| 386 switch (section) { | 422 switch (section) { |
| 387 case SECTION_EMAIL: | 423 case SECTION_EMAIL: |
| 388 return &suggested_email_; | 424 return &suggested_email_; |
| 389 case SECTION_CC: | 425 case SECTION_CC: |
| 390 return &suggested_cc_; | 426 return &suggested_cc_; |
| 391 case SECTION_BILLING: | 427 case SECTION_BILLING: |
| 392 return &suggested_billing_; | 428 return &suggested_billing_; |
| 393 case SECTION_SHIPPING: | 429 case SECTION_SHIPPING: |
| 394 return &suggested_shipping_; | 430 return &suggested_shipping_; |
| 395 } | 431 } |
| 396 | 432 |
| 397 NOTREACHED(); | 433 NOTREACHED(); |
| 398 return NULL; | 434 return NULL; |
| 399 } | 435 } |
| 400 | 436 |
| 401 // SuggestionsComboboxModel ---------------------------------------------------- | |
| 402 | |
| 403 AutofillDialogController::SuggestionsComboboxModel::SuggestionsComboboxModel() { | |
| 404 } | |
| 405 | |
| 406 AutofillDialogController::SuggestionsComboboxModel:: | |
| 407 ~SuggestionsComboboxModel() {} | |
| 408 | |
| 409 void AutofillDialogController::SuggestionsComboboxModel::AddItem( | |
| 410 const string16& item, const std::string& key) { | |
| 411 items_.push_back(std::make_pair(key, item)); | |
| 412 } | |
| 413 | |
| 414 std::string AutofillDialogController::SuggestionsComboboxModel::GetItemKeyAt( | |
| 415 int index) { | |
| 416 return items_[index].first; | |
| 417 } | |
| 418 | |
| 419 int AutofillDialogController::SuggestionsComboboxModel::GetItemCount() const { | |
| 420 return items_.size(); | |
| 421 } | |
| 422 | |
| 423 string16 AutofillDialogController::SuggestionsComboboxModel::GetItemAt( | |
| 424 int index) { | |
| 425 return items_[index].second; | |
| 426 } | |
| 427 | |
| 428 } // namespace autofill | 437 } // namespace autofill |
| 429 | |
| OLD | NEW |