| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_impl.h" | 5 #include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 CreditCard* card, | 159 CreditCard* card, |
| 160 string16* cvc, | 160 string16* cvc, |
| 161 AutofillProfile* profile) { | 161 AutofillProfile* profile) { |
| 162 for (DetailOutputMap::const_iterator it = output.begin(); | 162 for (DetailOutputMap::const_iterator it = output.begin(); |
| 163 it != output.end(); ++it) { | 163 it != output.end(); ++it) { |
| 164 string16 trimmed; | 164 string16 trimmed; |
| 165 TrimWhitespace(it->second, TRIM_ALL, &trimmed); | 165 TrimWhitespace(it->second, TRIM_ALL, &trimmed); |
| 166 | 166 |
| 167 // Special case CVC as CreditCard just swallows it. | 167 // Special case CVC as CreditCard just swallows it. |
| 168 if (it->first->type == CREDIT_CARD_VERIFICATION_CODE) { | 168 if (it->first->type == CREDIT_CARD_VERIFICATION_CODE) { |
| 169 *cvc = trimmed; | 169 if (cvc) |
| 170 cvc->assign(trimmed); |
| 170 } else { | 171 } else { |
| 171 // Copy the credit card name to |profile| in addition to |card| as | 172 // Copy the credit card name to |profile| in addition to |card| as |
| 172 // wallet::Instrument requires a recipient name for its billing address. | 173 // wallet::Instrument requires a recipient name for its billing address. |
| 173 if (it->first->type == CREDIT_CARD_NAME) | 174 if (profile && it->first->type == CREDIT_CARD_NAME) |
| 174 profile->SetRawInfo(NAME_FULL, trimmed); | 175 profile->SetRawInfo(NAME_FULL, trimmed); |
| 175 | 176 |
| 176 if (AutofillType(it->first->type).group() == AutofillType::CREDIT_CARD) | 177 if (AutofillType(it->first->type).group() == AutofillType::CREDIT_CARD) { |
| 177 card->SetRawInfo(it->first->type, trimmed); | 178 if (card) |
| 178 else | 179 card->SetRawInfo(it->first->type, trimmed); |
| 180 } else if (profile) { |
| 179 profile->SetRawInfo(it->first->type, trimmed); | 181 profile->SetRawInfo(it->first->type, trimmed); |
| 182 } |
| 180 } | 183 } |
| 181 } | 184 } |
| 182 } | 185 } |
| 183 | 186 |
| 184 // Returns the containing window for the given |web_contents|. The containing | 187 // Returns the containing window for the given |web_contents|. The containing |
| 185 // window might be a browser window for a Chrome tab, or it might be a shell | 188 // window might be a browser window for a Chrome tab, or it might be a shell |
| 186 // window for a platform app. | 189 // window for a platform app. |
| 187 BaseWindow* GetBaseWindowForWebContents( | 190 BaseWindow* GetBaseWindowForWebContents( |
| 188 const content::WebContents* web_contents) { | 191 const content::WebContents* web_contents) { |
| 189 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | 192 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
| (...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 588 | 591 |
| 589 scoped_ptr<DataModelWrapper> AutofillDialogControllerImpl::CreateWrapper( | 592 scoped_ptr<DataModelWrapper> AutofillDialogControllerImpl::CreateWrapper( |
| 590 DialogSection section) { | 593 DialogSection section) { |
| 591 SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section); | 594 SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section); |
| 592 std::string item_key = model->GetItemKeyForCheckedItem(); | 595 std::string item_key = model->GetItemKeyForCheckedItem(); |
| 593 scoped_ptr<DataModelWrapper> wrapper; | 596 scoped_ptr<DataModelWrapper> wrapper; |
| 594 if (item_key.empty()) | 597 if (item_key.empty()) |
| 595 return wrapper.Pass(); | 598 return wrapper.Pass(); |
| 596 | 599 |
| 597 if (IsPayingWithWallet()) { | 600 if (IsPayingWithWallet()) { |
| 598 int index; | 601 if (did_submit_ && full_wallet_.get()) { |
| 599 bool success = base::StringToInt(item_key, &index); | 602 if (section == SECTION_CC_BILLING) |
| 600 DCHECK(success); | 603 wrapper.reset(new FullWalletBillingWrapper(full_wallet_.get())); |
| 604 else |
| 605 wrapper.reset(new FullWalletShippingWrapper(full_wallet_.get())); |
| 606 } else { |
| 607 int index; |
| 608 bool success = base::StringToInt(item_key, &index); |
| 609 DCHECK(success); |
| 601 | 610 |
| 602 if (section == SECTION_CC_BILLING) { | 611 if (section == SECTION_CC_BILLING) { |
| 603 wrapper.reset( | 612 wrapper.reset( |
| 604 new WalletInstrumentWrapper(wallet_items_->instruments()[index])); | 613 new WalletInstrumentWrapper(wallet_items_->instruments()[index])); |
| 605 } else { | 614 } else { |
| 606 wrapper.reset( | 615 wrapper.reset( |
| 607 new WalletAddressWrapper(wallet_items_->addresses()[index])); | 616 new WalletAddressWrapper(wallet_items_->addresses()[index])); |
| 617 } |
| 608 } | 618 } |
| 609 } else if (section == SECTION_CC) { | 619 } else if (section == SECTION_CC) { |
| 610 CreditCard* card = GetManager()->GetCreditCardByGUID(item_key); | 620 CreditCard* card = GetManager()->GetCreditCardByGUID(item_key); |
| 611 DCHECK(card); | 621 DCHECK(card); |
| 612 wrapper.reset(new AutofillCreditCardWrapper(card)); | 622 wrapper.reset(new AutofillCreditCardWrapper(card)); |
| 613 } else { | 623 } else { |
| 614 // Calculate the variant by looking at how many items come from the same | 624 // Calculate the variant by looking at how many items come from the same |
| 615 // FormGroup. | 625 // FormGroup. |
| 616 size_t variant = 0; | 626 size_t variant = 0; |
| 617 for (int i = model->checked_item() - 1; i >= 0; --i) { | 627 for (int i = model->checked_item() - 1; i >= 0; --i) { |
| (...skipping 705 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1323 return true; | 1333 return true; |
| 1324 } | 1334 } |
| 1325 | 1335 |
| 1326 void AutofillDialogControllerImpl::FillOutputForSectionWithComparator( | 1336 void AutofillDialogControllerImpl::FillOutputForSectionWithComparator( |
| 1327 DialogSection section, | 1337 DialogSection section, |
| 1328 const InputFieldComparator& compare) { | 1338 const InputFieldComparator& compare) { |
| 1329 if (!SectionIsActive(section)) | 1339 if (!SectionIsActive(section)) |
| 1330 return; | 1340 return; |
| 1331 | 1341 |
| 1332 if (!IsManuallyEditingSection(section)) { | 1342 if (!IsManuallyEditingSection(section)) { |
| 1333 if (section == SECTION_CC_BILLING) { | 1343 scoped_ptr<DataModelWrapper> model = CreateWrapper(section); |
| 1334 // TODO(dbeam): implement. | 1344 // Only fill in data that is associated with this section. |
| 1335 } else { | 1345 const DetailInputs& inputs = RequestedFieldsForSection(section); |
| 1336 scoped_ptr<DataModelWrapper> model = CreateWrapper(section); | 1346 model->FillFormStructure(inputs, compare, &form_structure_); |
| 1337 // Only fill in data that is associated with this section. | |
| 1338 const DetailInputs& inputs = RequestedFieldsForSection(section); | |
| 1339 model->FillFormStructure(inputs, compare, &form_structure_); | |
| 1340 | 1347 |
| 1341 // CVC needs special-casing because the CreditCard class doesn't store | 1348 // CVC needs special-casing because the CreditCard class doesn't store |
| 1342 // or handle them. | 1349 // or handle them. |
| 1343 if (section == SECTION_CC) | 1350 if (section == SECTION_CC) |
| 1344 SetCvcResult(view_->GetCvc()); | 1351 SetCvcResult(view_->GetCvc()); |
| 1345 } | |
| 1346 } else { | 1352 } else { |
| 1347 // The user manually input data. | 1353 // The user manually input data. |
| 1348 DetailOutputMap output; | 1354 DetailOutputMap output; |
| 1349 view_->GetUserInput(section, &output); | 1355 view_->GetUserInput(section, &output); |
| 1350 | 1356 |
| 1351 if (IsPayingWithWallet()) { | 1357 scoped_ptr<CreditCard> card; |
| 1352 // TODO(dbeam): implement. | 1358 scoped_ptr<string16> cvc; |
| 1353 } else { | 1359 if (section == SECTION_CC || section == SECTION_CC_BILLING) { |
| 1354 // Save the info as new or edited data and fill it into |form_structure_|. | 1360 card.reset(new CreditCard()); |
| 1355 if (section == SECTION_CC) { | 1361 cvc.reset(new string16()); |
| 1356 CreditCard card; | 1362 } |
| 1357 FillFormGroupFromOutputs(output, &card); | |
| 1358 | 1363 |
| 1359 if (view_->SaveDetailsLocally()) | 1364 AutofillProfile profile; |
| 1360 GetManager()->SaveImportedCreditCard(card); | 1365 GetBillingInfoFromOutputs(output, card.get(), cvc.get(), &profile); |
| 1361 | 1366 |
| 1362 FillFormStructureForSection(card, 0, section, compare); | 1367 // TODO(estade): we should probably edit the existing profile in the cases |
| 1368 // where the input data is based on an existing profile (user clicked "Edit" |
| 1369 // or autofill popup filled in the form). |
| 1370 if (card.get()) { |
| 1371 if (ShouldSaveDetailsLocally()) |
| 1372 GetManager()->SaveImportedCreditCard(*card); |
| 1363 | 1373 |
| 1364 // Again, CVC needs special-casing. Fill it in directly from |output|. | 1374 FillFormStructureForSection(*card, 0, section, compare); |
| 1365 SetCvcResult(GetValueForType(output, CREDIT_CARD_VERIFICATION_CODE)); | 1375 } |
| 1366 } else { | |
| 1367 AutofillProfile profile; | |
| 1368 FillFormGroupFromOutputs(output, &profile); | |
| 1369 | 1376 |
| 1370 // TODO(estade): we should probably edit the existing profile in the | 1377 if (cvc.get() && !cvc->empty()) |
| 1371 // cases where the input data is based on an existing profile (user | 1378 SetCvcResult(*cvc); |
| 1372 // clicked "Edit" or autofill popup filled in the form). | |
| 1373 if (view_->SaveDetailsLocally()) | |
| 1374 GetManager()->SaveImportedProfile(profile); | |
| 1375 | 1379 |
| 1376 FillFormStructureForSection(profile, 0, section, compare); | 1380 if (ShouldSaveDetailsLocally()) |
| 1377 } | 1381 GetManager()->SaveImportedProfile(profile); |
| 1378 } | 1382 |
| 1383 FillFormStructureForSection(profile, 0, section, compare); |
| 1379 } | 1384 } |
| 1380 } | 1385 } |
| 1381 | 1386 |
| 1382 void AutofillDialogControllerImpl::FillOutputForSection(DialogSection section) { | 1387 void AutofillDialogControllerImpl::FillOutputForSection(DialogSection section) { |
| 1383 FillOutputForSectionWithComparator(section, | 1388 FillOutputForSectionWithComparator(section, |
| 1384 base::Bind(DetailInputMatchesField)); | 1389 base::Bind(DetailInputMatchesField)); |
| 1385 } | 1390 } |
| 1386 | 1391 |
| 1387 void AutofillDialogControllerImpl::FillFormStructureForSection( | 1392 void AutofillDialogControllerImpl::FillFormStructureForSection( |
| 1388 const FormGroup& form_group, | 1393 const FormGroup& form_group, |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1491 NOTIMPLEMENTED(); | 1496 NOTIMPLEMENTED(); |
| 1492 } | 1497 } |
| 1493 | 1498 |
| 1494 bool AutofillDialogControllerImpl::IsManuallyEditingSection( | 1499 bool AutofillDialogControllerImpl::IsManuallyEditingSection( |
| 1495 DialogSection section) { | 1500 DialogSection section) { |
| 1496 return section_editing_state_[section] || | 1501 return section_editing_state_[section] || |
| 1497 SuggestionsMenuModelForSection(section)-> | 1502 SuggestionsMenuModelForSection(section)-> |
| 1498 GetItemKeyForCheckedItem().empty(); | 1503 GetItemKeyForCheckedItem().empty(); |
| 1499 } | 1504 } |
| 1500 | 1505 |
| 1501 bool AutofillDialogControllerImpl::UseBillingForShipping() { | 1506 bool AutofillDialogControllerImpl::ShouldUseBillingForShipping() { |
| 1502 // If the user is editing or inputting data, ask the view. | 1507 // If the user is editing or inputting data, ask the view. |
| 1503 if (IsManuallyEditingSection(SECTION_SHIPPING)) | 1508 if (IsManuallyEditingSection(SECTION_SHIPPING)) |
| 1504 return view_->UseBillingForShipping(); | 1509 return view_->UseBillingForShipping(); |
| 1505 | 1510 |
| 1506 // Otherwise, the checkbox should be hidden so its state is irrelevant. | 1511 // Otherwise, the checkbox should be hidden so its state is irrelevant. |
| 1507 // Always use the shipping suggestion model. | 1512 // Always use the shipping suggestion model. |
| 1508 return false; | 1513 return false; |
| 1509 } | 1514 } |
| 1510 | 1515 |
| 1516 bool AutofillDialogControllerImpl::ShouldSaveDetailsLocally() { |
| 1517 return !IsPayingWithWallet() && view_->SaveDetailsLocally(); |
| 1518 } |
| 1519 |
| 1511 void AutofillDialogControllerImpl::SubmitWithWallet() { | 1520 void AutofillDialogControllerImpl::SubmitWithWallet() { |
| 1512 // TODO(dbeam): disallow interacting with the dialog while submitting. | 1521 // TODO(dbeam): disallow interacting with the dialog while submitting. |
| 1513 | 1522 |
| 1514 active_instrument_id_.clear(); | 1523 active_instrument_id_.clear(); |
| 1515 active_address_id_.clear(); | 1524 active_address_id_.clear(); |
| 1516 | 1525 |
| 1517 if (!section_editing_state_[SECTION_CC_BILLING]) { | 1526 if (!section_editing_state_[SECTION_CC_BILLING]) { |
| 1518 SuggestionsMenuModel* billing = | 1527 SuggestionsMenuModel* billing = |
| 1519 SuggestionsMenuModelForSection(SECTION_CC_BILLING); | 1528 SuggestionsMenuModelForSection(SECTION_CC_BILLING); |
| 1520 if (!billing->GetItemKeyForCheckedItem().empty() && | 1529 if (!billing->GetItemKeyForCheckedItem().empty() && |
| 1521 billing->checked_item() < | 1530 billing->checked_item() < |
| 1522 static_cast<int>(wallet_items_->instruments().size())) { | 1531 static_cast<int>(wallet_items_->instruments().size())) { |
| 1523 const wallet::WalletItems::MaskedInstrument* active_instrument = | 1532 const wallet::WalletItems::MaskedInstrument* active_instrument = |
| 1524 wallet_items_->instruments()[billing->checked_item()]; | 1533 wallet_items_->instruments()[billing->checked_item()]; |
| 1525 active_instrument_id_ = active_instrument->object_id(); | 1534 active_instrument_id_ = active_instrument->object_id(); |
| 1526 | 1535 |
| 1527 // TODO(dbeam): does re-using instrument address IDs work? | 1536 // TODO(dbeam): does re-using instrument address IDs work? |
| 1528 if (UseBillingForShipping()) | 1537 if (ShouldUseBillingForShipping()) |
| 1529 active_address_id_ = active_instrument->address().object_id(); | 1538 active_address_id_ = active_instrument->address().object_id(); |
| 1530 } | 1539 } |
| 1531 } | 1540 } |
| 1532 | 1541 |
| 1533 if (!section_editing_state_[SECTION_SHIPPING] && active_address_id_.empty()) { | 1542 if (!section_editing_state_[SECTION_SHIPPING] && active_address_id_.empty()) { |
| 1534 SuggestionsMenuModel* shipping = | 1543 SuggestionsMenuModel* shipping = |
| 1535 SuggestionsMenuModelForSection(SECTION_SHIPPING); | 1544 SuggestionsMenuModelForSection(SECTION_SHIPPING); |
| 1536 if (!shipping->GetItemKeyForCheckedItem().empty() && | 1545 if (!shipping->GetItemKeyForCheckedItem().empty() && |
| 1537 shipping->checked_item() < | 1546 shipping->checked_item() < |
| 1538 static_cast<int>(wallet_items_->addresses().size())) { | 1547 static_cast<int>(wallet_items_->addresses().size())) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1559 CreditCard card; | 1568 CreditCard card; |
| 1560 AutofillProfile profile; | 1569 AutofillProfile profile; |
| 1561 string16 cvc; | 1570 string16 cvc; |
| 1562 GetBillingInfoFromOutputs(output, &card, &cvc, &profile); | 1571 GetBillingInfoFromOutputs(output, &card, &cvc, &profile); |
| 1563 | 1572 |
| 1564 new_instrument.reset(new wallet::Instrument(card, cvc, profile)); | 1573 new_instrument.reset(new wallet::Instrument(card, cvc, profile)); |
| 1565 } | 1574 } |
| 1566 | 1575 |
| 1567 scoped_ptr<wallet::Address> new_address; | 1576 scoped_ptr<wallet::Address> new_address; |
| 1568 if (active_address_id_.empty()) { | 1577 if (active_address_id_.empty()) { |
| 1569 if (UseBillingForShipping() && new_instrument.get()) { | 1578 if (ShouldUseBillingForShipping() && new_instrument.get()) { |
| 1570 new_address.reset(new wallet::Address(new_instrument->address())); | 1579 new_address.reset(new wallet::Address(new_instrument->address())); |
| 1571 } else { | 1580 } else { |
| 1572 DetailOutputMap output; | 1581 DetailOutputMap output; |
| 1573 view_->GetUserInput(SECTION_SHIPPING, &output); | 1582 view_->GetUserInput(SECTION_SHIPPING, &output); |
| 1574 | 1583 |
| 1575 AutofillProfile profile; | 1584 AutofillProfile profile; |
| 1576 FillFormGroupFromOutputs(output, &profile); | 1585 FillFormGroupFromOutputs(output, &profile); |
| 1577 | 1586 |
| 1578 new_address.reset(new wallet::Address(profile)); | 1587 new_address.reset(new wallet::Address(profile)); |
| 1579 } | 1588 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 1610 wallet_items_->google_transaction_id(), | 1619 wallet_items_->google_transaction_id(), |
| 1611 dialog_type_, | 1620 dialog_type_, |
| 1612 std::vector<wallet::WalletClient::RiskCapability>())); | 1621 std::vector<wallet::WalletClient::RiskCapability>())); |
| 1613 } | 1622 } |
| 1614 | 1623 |
| 1615 void AutofillDialogControllerImpl::FinishSubmit() { | 1624 void AutofillDialogControllerImpl::FinishSubmit() { |
| 1616 FillOutputForSection(SECTION_EMAIL); | 1625 FillOutputForSection(SECTION_EMAIL); |
| 1617 FillOutputForSection(SECTION_CC); | 1626 FillOutputForSection(SECTION_CC); |
| 1618 FillOutputForSection(SECTION_BILLING); | 1627 FillOutputForSection(SECTION_BILLING); |
| 1619 FillOutputForSection(SECTION_CC_BILLING); | 1628 FillOutputForSection(SECTION_CC_BILLING); |
| 1620 if (UseBillingForShipping()) { | 1629 if (ShouldUseBillingForShipping()) { |
| 1621 FillOutputForSectionWithComparator( | 1630 FillOutputForSectionWithComparator( |
| 1622 SECTION_BILLING, | 1631 SECTION_BILLING, |
| 1623 base::Bind(DetailInputMatchesShippingField)); | 1632 base::Bind(DetailInputMatchesShippingField)); |
| 1624 FillOutputForSectionWithComparator( | 1633 FillOutputForSectionWithComparator( |
| 1625 SECTION_CC, | 1634 SECTION_CC, |
| 1626 base::Bind(DetailInputMatchesShippingField)); | 1635 base::Bind(DetailInputMatchesShippingField)); |
| 1627 } else { | 1636 } else { |
| 1628 FillOutputForSection(SECTION_SHIPPING); | 1637 FillOutputForSection(SECTION_SHIPPING); |
| 1629 } | 1638 } |
| 1630 callback_.Run(&form_structure_); | 1639 callback_.Run(&form_structure_); |
| 1631 callback_ = base::Callback<void(const FormStructure*)>(); | 1640 callback_ = base::Callback<void(const FormStructure*)>(); |
| 1632 | 1641 |
| 1633 if (dialog_type_ == DIALOG_TYPE_REQUEST_AUTOCOMPLETE) { | 1642 if (dialog_type_ == DIALOG_TYPE_REQUEST_AUTOCOMPLETE) { |
| 1634 // This may delete us. | 1643 // This may delete us. |
| 1635 Hide(); | 1644 Hide(); |
| 1636 } | 1645 } |
| 1637 } | 1646 } |
| 1638 | 1647 |
| 1639 } // namespace autofill | 1648 } // namespace autofill |
| OLD | NEW |