OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/autofill/autofill_manager.h" | 5 #include "chrome/browser/autofill/autofill_manager.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 // The rate for positive/negative matches potentially could be different. | 32 // The rate for positive/negative matches potentially could be different. |
33 const double kAutoFillPositiveUploadRateDefaultValue = 0.01; | 33 const double kAutoFillPositiveUploadRateDefaultValue = 0.01; |
34 const double kAutoFillNegativeUploadRateDefaultValue = 0.01; | 34 const double kAutoFillNegativeUploadRateDefaultValue = 0.01; |
35 | 35 |
36 // Size and offset of the prefix and suffix portions of phone numbers. | 36 // Size and offset of the prefix and suffix portions of phone numbers. |
37 const int kAutoFillPhoneNumberPrefixOffset = 0; | 37 const int kAutoFillPhoneNumberPrefixOffset = 0; |
38 const int kAutoFillPhoneNumberPrefixCount = 3; | 38 const int kAutoFillPhoneNumberPrefixCount = 3; |
39 const int kAutoFillPhoneNumberSuffixOffset = 3; | 39 const int kAutoFillPhoneNumberSuffixOffset = 3; |
40 const int kAutoFillPhoneNumberSuffixCount = 4; | 40 const int kAutoFillPhoneNumberSuffixCount = 4; |
41 | 41 |
| 42 const string16::value_type kCreditCardPrefix[] = {'*', 0}; |
| 43 const string16::value_type kLabelSeparator[] = {';',' ', '*', 0}; |
42 | 44 |
43 const string16::value_type kCreditCardLabelPrefix[] = {'*', 0}; | 45 // Combines the |label| string with the last four digits of the credit card |
44 const string16::value_type kLabelSeparator[] = {';',' ', '*', 0}; | 46 // |cc|. If one, the other, or both are empty strings we omit the separator. |
| 47 string16 CombineLabelAndCreditCard(const string16& label, |
| 48 const CreditCard* cc) { |
| 49 if (label.empty()) |
| 50 return kCreditCardPrefix + cc->LastFourDigits(); |
| 51 else if (cc->LastFourDigits().empty()) |
| 52 return label; |
| 53 else |
| 54 return label + kLabelSeparator + cc->LastFourDigits(); |
| 55 } |
45 | 56 |
46 // The name of the generic credit card icon, which maps to the image resource ID | 57 // The name of the generic credit card icon, which maps to the image resource ID |
47 // in webkit/glue:WebKitClientImpl. | 58 // in webkit/glue:WebKitClientImpl. |
48 // TODO(jhawkins): Move the images to chrome/common and implement the resource | 59 // TODO(jhawkins): Move the images to chrome/common and implement the resource |
49 // handling in RendererWebKitClientImpl. | 60 // handling in RendererWebKitClientImpl. |
50 const char kGenericCC[] = "genericCC"; | 61 const char kGenericCC[] = "genericCC"; |
51 | 62 |
52 // Removes duplicate elements whilst preserving original order of |elements| and | 63 // Removes duplicate elements whilst preserving original order of |elements| and |
53 // |unique_ids|. | 64 // |unique_ids|. |
54 void RemoveDuplicateElements( | 65 void RemoveDuplicateElements( |
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 std::vector<string16> expanded_labels; | 519 std::vector<string16> expanded_labels; |
509 std::vector<int> expanded_ids; | 520 std::vector<int> expanded_ids; |
510 for (std::vector<AutoFillProfile*>::const_iterator iter = | 521 for (std::vector<AutoFillProfile*>::const_iterator iter = |
511 matched_profiles.begin(); iter != matched_profiles.end(); | 522 matched_profiles.begin(); iter != matched_profiles.end(); |
512 ++iter, ++i) { | 523 ++iter, ++i) { |
513 AutoFillProfile* profile = *iter; | 524 AutoFillProfile* profile = *iter; |
514 for (std::vector<CreditCard*>::const_iterator cc = | 525 for (std::vector<CreditCard*>::const_iterator cc = |
515 personal_data_->credit_cards().begin(); | 526 personal_data_->credit_cards().begin(); |
516 cc != personal_data_->credit_cards().end(); ++cc) { | 527 cc != personal_data_->credit_cards().end(); ++cc) { |
517 expanded_values.push_back((*values)[i]); | 528 expanded_values.push_back((*values)[i]); |
518 string16 label = (*labels)[i] + kLabelSeparator + (*cc)->LastFourDigits(); | 529 string16 label = CombineLabelAndCreditCard((*labels)[i], *cc); |
519 expanded_labels.push_back(label); | 530 expanded_labels.push_back(label); |
520 expanded_ids.push_back(PackIDs((*cc)->unique_id(), profile->unique_id())); | 531 expanded_ids.push_back(PackIDs((*cc)->unique_id(), profile->unique_id())); |
521 } | 532 } |
522 } | 533 } |
523 expanded_labels.swap(*labels); | 534 expanded_labels.swap(*labels); |
524 expanded_values.swap(*values); | 535 expanded_values.swap(*values); |
| 536 // No CC, so no icons. |
| 537 icons->resize(values->size()); |
525 expanded_ids.swap(*unique_ids); | 538 expanded_ids.swap(*unique_ids); |
| 539 DCHECK_EQ(values->size(), icons->size()); |
526 } | 540 } |
527 | 541 |
528 void AutoFillManager::GetBillingProfileSuggestions( | 542 void AutoFillManager::GetBillingProfileSuggestions( |
529 FormStructure* form, | 543 FormStructure* form, |
530 const FormField& field, | 544 const FormField& field, |
531 AutoFillType type, | 545 AutoFillType type, |
532 std::vector<string16>* values, | 546 std::vector<string16>* values, |
533 std::vector<string16>* labels, | 547 std::vector<string16>* labels, |
534 std::vector<string16>* icons, | 548 std::vector<string16>* icons, |
535 std::vector<int>* unique_ids) { | 549 std::vector<int>* unique_ids) { |
536 std::vector<CreditCard*> matching_creditcards; | |
537 std::vector<AutoFillProfile*> matching_profiles; | |
538 std::vector<string16> cc_values; | |
539 std::vector<string16> cc_labels; | |
540 | 550 |
541 // If the form is non-HTTPS, no CC suggestions are provided; however, give the | 551 // If the form is non-HTTPS, no CC suggestions are provided; however, give the |
542 // user the option of filling the billing address fields with regular address | 552 // user the option of filling the billing address fields with regular address |
543 // data. | 553 // data. |
544 if (!FormIsHTTPS(form)) { | 554 if (!FormIsHTTPS(form)) { |
545 GetProfileSuggestions( | 555 GetProfileSuggestions( |
546 form, field, type, false, values, icons, labels, unique_ids); | 556 form, field, type, false, values, icons, labels, unique_ids); |
547 return; | 557 return; |
548 } | 558 } |
549 | 559 |
| 560 std::vector<CreditCard*> matching_creditcards; |
| 561 std::vector<AutoFillProfile*> matching_profiles; |
| 562 |
| 563 // Collect matching pairs of credit cards and related profiles, where profile |
| 564 // field value matches form field value. |
550 for (std::vector<CreditCard*>::const_iterator cc = | 565 for (std::vector<CreditCard*>::const_iterator cc = |
551 personal_data_->credit_cards().begin(); | 566 personal_data_->credit_cards().begin(); |
552 cc != personal_data_->credit_cards().end(); ++cc) { | 567 cc != personal_data_->credit_cards().end(); ++cc) { |
553 string16 label = (*cc)->billing_address(); | 568 string16 label = (*cc)->billing_address(); |
554 AutoFillProfile* billing_profile = NULL; | 569 AutoFillProfile* billing_profile = NULL; |
555 | 570 |
556 // The value of the stored data for this field type in the |profile|. | 571 // The value of the stored data for this field type in the |profile|. |
557 string16 profile_field_value; | 572 string16 profile_field_value; |
558 | 573 |
559 for (std::vector<AutoFillProfile*>::const_iterator iter = | 574 for (std::vector<AutoFillProfile*>::const_iterator iter = |
560 personal_data_->profiles().begin(); | 575 personal_data_->profiles().begin(); |
561 iter != personal_data_->profiles().end(); ++iter) { | 576 iter != personal_data_->profiles().end(); ++iter) { |
562 AutoFillProfile* profile = *iter; | 577 AutoFillProfile* profile = *iter; |
563 | 578 |
564 // This assumes that labels are unique. | 579 // This assumes that labels are unique. |
565 if (profile->Label() == label && | 580 if (profile->Label() == label && |
566 !profile->GetFieldText(type).empty() && | 581 !profile->GetFieldText(type).empty() && |
567 StartsWith(profile->GetFieldText(type), field.value(), false)) { | 582 StartsWith(profile->GetFieldText(type), field.value(), false)) { |
568 billing_profile = profile; | 583 billing_profile = profile; |
569 break; | 584 break; |
570 } | 585 } |
571 } | 586 } |
572 | 587 |
573 if (!billing_profile) | 588 if (!billing_profile) |
574 continue; | 589 continue; |
575 | 590 |
576 for (std::vector<AutoFillProfile*>::const_iterator iter = | 591 matching_creditcards.push_back(*cc); |
577 personal_data_->profiles().begin(); | 592 matching_profiles.push_back(billing_profile); |
578 iter != personal_data_->profiles().end(); ++iter) { | 593 } |
579 values->push_back(billing_profile->GetFieldText(type)); | |
580 | 594 |
581 string16 label = (*iter)->Label() + kLabelSeparator + | 595 std::vector<string16> inferred_labels; |
582 (*cc)->LastFourDigits(); | 596 AutoFillProfile::CreateInferredLabels(&matching_profiles, &inferred_labels, 0, |
583 labels->push_back(label); | 597 type.field_type()); |
584 icons->push_back(ASCIIToUTF16(kGenericCC)); | 598 |
585 unique_ids->push_back( | 599 DCHECK_EQ(matching_profiles.size(), matching_creditcards.size()); |
586 PackIDs((*cc)->unique_id(), (*iter)->unique_id())); | 600 DCHECK_EQ(matching_profiles.size(), inferred_labels.size()); |
587 } | 601 |
| 602 // Process the matching pairs into suggested |values|, |labels|, and |
| 603 // |unique_ids|. |
| 604 for (size_t i = 0; i < matching_profiles.size(); ++i) { |
| 605 values->push_back(matching_profiles[i]->GetFieldText(type)); |
| 606 string16 label = CombineLabelAndCreditCard(inferred_labels[i], |
| 607 matching_creditcards[i]); |
| 608 labels->push_back(label); |
| 609 icons->push_back(ASCIIToUTF16(kGenericCC)); |
| 610 unique_ids->push_back(PackIDs(matching_creditcards[i]->unique_id(), |
| 611 matching_profiles[i]->unique_id())); |
588 } | 612 } |
589 } | 613 } |
590 | 614 |
591 void AutoFillManager::GetCreditCardSuggestions(FormStructure* form, | 615 void AutoFillManager::GetCreditCardSuggestions(FormStructure* form, |
592 const FormField& field, | 616 const FormField& field, |
593 AutoFillType type, | 617 AutoFillType type, |
594 std::vector<string16>* values, | 618 std::vector<string16>* values, |
595 std::vector<string16>* labels, | 619 std::vector<string16>* labels, |
596 std::vector<string16>* icons, | 620 std::vector<string16>* icons, |
597 std::vector<int>* unique_ids) { | 621 std::vector<int>* unique_ids) { |
598 // Don't return CC suggestions for non-HTTPS pages. | 622 // Don't return CC suggestions for non-HTTPS pages. |
599 if (!FormIsHTTPS(form)) | 623 if (!FormIsHTTPS(form)) |
600 return; | 624 return; |
601 | 625 |
602 for (std::vector<CreditCard*>::const_iterator iter = | 626 for (std::vector<CreditCard*>::const_iterator iter = |
603 personal_data_->credit_cards().begin(); | 627 personal_data_->credit_cards().begin(); |
604 iter != personal_data_->credit_cards().end(); ++iter) { | 628 iter != personal_data_->credit_cards().end(); ++iter) { |
605 CreditCard* credit_card = *iter; | 629 CreditCard* credit_card = *iter; |
606 | 630 |
607 // The value of the stored data for this field type in the |credit_card|. | 631 // The value of the stored data for this field type in the |credit_card|. |
608 string16 creditcard_field_value = credit_card->GetFieldText(type); | 632 string16 creditcard_field_value = credit_card->GetFieldText(type); |
609 if (!creditcard_field_value.empty() && | 633 if (!creditcard_field_value.empty() && |
610 StartsWith(creditcard_field_value, field.value(), false)) { | 634 StartsWith(creditcard_field_value, field.value(), false)) { |
611 if (type.field_type() == CREDIT_CARD_NUMBER) | 635 if (type.field_type() == CREDIT_CARD_NUMBER) |
612 creditcard_field_value = credit_card->ObfuscatedNumber(); | 636 creditcard_field_value = credit_card->ObfuscatedNumber(); |
613 | 637 |
614 if (!form->HasNonBillingFields()) { | 638 if (!form->HasNonBillingFields()) { |
615 values->push_back(creditcard_field_value); | 639 values->push_back(creditcard_field_value); |
616 labels->push_back( | 640 labels->push_back(CombineLabelAndCreditCard(string16(), credit_card)); |
617 kCreditCardLabelPrefix + credit_card->LastFourDigits()); | 641 icons->push_back(ASCIIToUTF16(kGenericCC)); |
618 unique_ids->push_back(PackIDs(credit_card->unique_id(), 0)); | 642 unique_ids->push_back(PackIDs(credit_card->unique_id(), 0)); |
619 } else { | 643 } else { |
620 for (std::vector<AutoFillProfile*>::const_iterator iter = | 644 const std::vector<AutoFillProfile*>& profiles |
621 personal_data_->profiles().begin(); | 645 = personal_data_->profiles(); |
622 iter != personal_data_->profiles().end(); ++iter) { | 646 std::vector<string16> inferred_labels; |
| 647 AutoFillProfile::CreateInferredLabels(&profiles, |
| 648 &inferred_labels, |
| 649 0, |
| 650 type.field_type()); |
| 651 DCHECK_EQ(profiles.size(), inferred_labels.size()); |
| 652 |
| 653 for (size_t i = 0; i < profiles.size(); ++i) { |
623 values->push_back(creditcard_field_value); | 654 values->push_back(creditcard_field_value); |
624 | 655 |
625 string16 label = (*iter)->Label() + kLabelSeparator + | 656 string16 label = CombineLabelAndCreditCard(inferred_labels[i], |
626 credit_card->LastFourDigits(); | 657 credit_card); |
627 labels->push_back(label); | 658 labels->push_back(label); |
628 icons->push_back(ASCIIToUTF16(kGenericCC)); | 659 icons->push_back(ASCIIToUTF16(kGenericCC)); |
629 unique_ids->push_back( | 660 unique_ids->push_back( |
630 PackIDs(credit_card->unique_id(), (*iter)->unique_id())); | 661 PackIDs(credit_card->unique_id(), profiles[i]->unique_id())); |
631 } | 662 } |
632 } | 663 } |
633 } | 664 } |
634 } | 665 } |
635 } | 666 } |
636 | 667 |
637 void AutoFillManager::FillBillingFormField(const CreditCard* credit_card, | 668 void AutoFillManager::FillBillingFormField(const CreditCard* credit_card, |
638 AutoFillType type, | 669 AutoFillType type, |
639 webkit_glue::FormField* field) { | 670 webkit_glue::FormField* field) { |
640 DCHECK(credit_card); | 671 DCHECK(credit_card); |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
765 | 796 |
766 // When receiving IDs (across processes) from the renderer we unpack credit card | 797 // When receiving IDs (across processes) from the renderer we unpack credit card |
767 // and profile IDs from a single integer. Credit card IDs are stored in the | 798 // and profile IDs from a single integer. Credit card IDs are stored in the |
768 // high word and profile IDs are stored in the low word. | 799 // high word and profile IDs are stored in the low word. |
769 // static | 800 // static |
770 void AutoFillManager::UnpackIDs(int id, int* cc_id, int* profile_id) { | 801 void AutoFillManager::UnpackIDs(int id, int* cc_id, int* profile_id) { |
771 *cc_id = id >> std::numeric_limits<unsigned short>::digits & | 802 *cc_id = id >> std::numeric_limits<unsigned short>::digits & |
772 std::numeric_limits<unsigned short>::max(); | 803 std::numeric_limits<unsigned short>::max(); |
773 *profile_id = id & std::numeric_limits<unsigned short>::max(); | 804 *profile_id = id & std::numeric_limits<unsigned short>::max(); |
774 } | 805 } |
OLD | NEW |