| 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 |
| 42 const string16::value_type kCreditCardLabelPrefix[] = {'*', 0}; | 43 const string16::value_type kCreditCardLabelPrefix[] = {'*', 0}; |
| 43 const string16::value_type kLabelSeparator[] = {';',' ', '*', 0}; | 44 const string16::value_type kLabelSeparator[] = {';',' ', '*', 0}; |
| 44 | 45 |
| 46 // The name of the generic credit card icon, which maps to the image resource ID |
| 47 // in webkit/glue:WebKitClientImpl. |
| 48 // TODO(jhawkins): Move the images to chrome/common and implement the resource |
| 49 // handling in RendererWebKitClientImpl. |
| 50 const char kGenericCC[] = "genericCC"; |
| 51 |
| 45 // Removes duplicate elements whilst preserving original order of |elements| and | 52 // Removes duplicate elements whilst preserving original order of |elements| and |
| 46 // |unique_ids|. | 53 // |unique_ids|. |
| 47 void RemoveDuplicateElements( | 54 void RemoveDuplicateElements( |
| 48 std::vector<string16>* elements, std::vector<int>* unique_ids) { | 55 std::vector<string16>* elements, std::vector<int>* unique_ids) { |
| 49 DCHECK_EQ(elements->size(), unique_ids->size()); | 56 DCHECK_EQ(elements->size(), unique_ids->size()); |
| 50 | 57 |
| 51 std::vector<string16> elements_copy; | 58 std::vector<string16> elements_copy; |
| 52 std::vector<int> unique_ids_copy; | 59 std::vector<int> unique_ids_copy; |
| 53 for (size_t i = 0; i < elements->size(); ++i) { | 60 for (size_t i = 0; i < elements->size(); ++i) { |
| 54 const string16& element = (*elements)[i]; | 61 const string16& element = (*elements)[i]; |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 break; | 199 break; |
| 193 } | 200 } |
| 194 } | 201 } |
| 195 } | 202 } |
| 196 | 203 |
| 197 if (autofill_field == NULL) | 204 if (autofill_field == NULL) |
| 198 return false; | 205 return false; |
| 199 | 206 |
| 200 std::vector<string16> values; | 207 std::vector<string16> values; |
| 201 std::vector<string16> labels; | 208 std::vector<string16> labels; |
| 209 std::vector<string16> icons; |
| 202 std::vector<int> unique_ids; | 210 std::vector<int> unique_ids; |
| 203 AutoFillType type(autofill_field->type()); | 211 AutoFillType type(autofill_field->type()); |
| 204 | 212 |
| 205 // If this form is non-HTTPS, treat billing address fields as regular address | 213 // If this form is non-HTTPS, treat billing address fields as regular address |
| 206 // fields. | 214 // fields. |
| 207 bool handle_billing = FormIsHTTPS(form); | 215 bool handle_billing = FormIsHTTPS(form); |
| 208 | 216 |
| 209 if (type.group() == AutoFillType::CREDIT_CARD) | 217 if (type.group() == AutoFillType::CREDIT_CARD) |
| 210 GetCreditCardSuggestions(form, field, type, &values, &labels, &unique_ids); | 218 GetCreditCardSuggestions( |
| 219 form, field, type, &values, &labels, &icons, &unique_ids); |
| 211 else if (type.group() == AutoFillType::ADDRESS_BILLING) | 220 else if (type.group() == AutoFillType::ADDRESS_BILLING) |
| 212 GetBillingProfileSuggestions( | 221 GetBillingProfileSuggestions( |
| 213 form, field, type, &values, &labels, &unique_ids); | 222 form, field, type, &values, &labels, &icons, &unique_ids); |
| 214 else | 223 else |
| 215 GetProfileSuggestions( | 224 GetProfileSuggestions(form, field, type, handle_billing, |
| 216 form, field, type, handle_billing, &values, &labels, &unique_ids); | 225 &values, &labels, &icons, &unique_ids); |
| 217 | 226 |
| 218 DCHECK_EQ(values.size(), labels.size()); | 227 DCHECK_EQ(values.size(), labels.size()); |
| 228 DCHECK_EQ(values.size(), icons.size()); |
| 219 DCHECK_EQ(values.size(), unique_ids.size()); | 229 DCHECK_EQ(values.size(), unique_ids.size()); |
| 220 | 230 |
| 221 // No suggestions. | 231 // No suggestions. |
| 222 if (values.empty()) | 232 if (values.empty()) |
| 223 return false; | 233 return false; |
| 224 | 234 |
| 225 // If the form is auto-filled and the renderer is querying for suggestions, | 235 // If the form is auto-filled and the renderer is querying for suggestions, |
| 226 // then the user is editing the value of a field. In this case, don't display | 236 // then the user is editing the value of a field. In this case, don't display |
| 227 // labels, as that information is redundant. In addition, remove duplicate | 237 // labels, as that information is redundant. In addition, remove duplicate |
| 228 // values. | 238 // values. |
| 229 if (form_autofilled) { | 239 if (form_autofilled) { |
| 230 RemoveDuplicateElements(&values, &unique_ids); | 240 RemoveDuplicateElements(&values, &unique_ids); |
| 231 labels.resize(values.size()); | 241 labels.resize(values.size()); |
| 242 icons.resize(values.size()); |
| 232 | 243 |
| 233 for (size_t i = 0; i < labels.size(); ++i) | 244 for (size_t i = 0; i < labels.size(); ++i) { |
| 234 labels[i] = string16(); | 245 labels[i] = string16(); |
| 246 icons[i] = string16(); |
| 247 } |
| 235 } | 248 } |
| 236 | 249 |
| 237 host->AutoFillSuggestionsReturned(query_id, values, labels, unique_ids); | 250 host->AutoFillSuggestionsReturned( |
| 251 query_id, values, labels, icons, unique_ids); |
| 238 return true; | 252 return true; |
| 239 } | 253 } |
| 240 | 254 |
| 241 // TODO(jhawkins): Remove the |value| parameter. | 255 // TODO(jhawkins): Remove the |value| parameter. |
| 242 bool AutoFillManager::FillAutoFillFormData(int query_id, | 256 bool AutoFillManager::FillAutoFillFormData(int query_id, |
| 243 const FormData& form, | 257 const FormData& form, |
| 244 const string16& value, | 258 const string16& value, |
| 245 const string16& label, | 259 const string16& label, |
| 246 int unique_id) { | 260 int unique_id) { |
| 247 if (!IsAutoFillEnabled()) | 261 if (!IsAutoFillEnabled()) |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 download_manager_(NULL) { | 468 download_manager_(NULL) { |
| 455 DCHECK(tab_contents); | 469 DCHECK(tab_contents); |
| 456 } | 470 } |
| 457 | 471 |
| 458 void AutoFillManager::GetProfileSuggestions(FormStructure* form, | 472 void AutoFillManager::GetProfileSuggestions(FormStructure* form, |
| 459 const FormField& field, | 473 const FormField& field, |
| 460 AutoFillType type, | 474 AutoFillType type, |
| 461 bool include_cc_labels, | 475 bool include_cc_labels, |
| 462 std::vector<string16>* values, | 476 std::vector<string16>* values, |
| 463 std::vector<string16>* labels, | 477 std::vector<string16>* labels, |
| 478 std::vector<string16>* icons, |
| 464 std::vector<int>* unique_ids) { | 479 std::vector<int>* unique_ids) { |
| 465 const std::vector<AutoFillProfile*>& profiles = personal_data_->profiles(); | 480 const std::vector<AutoFillProfile*>& profiles = personal_data_->profiles(); |
| 466 std::vector<AutoFillProfile*> matched_profiles; | 481 std::vector<AutoFillProfile*> matched_profiles; |
| 467 for (std::vector<AutoFillProfile*>::const_iterator iter = profiles.begin(); | 482 for (std::vector<AutoFillProfile*>::const_iterator iter = profiles.begin(); |
| 468 iter != profiles.end(); ++iter) { | 483 iter != profiles.end(); ++iter) { |
| 469 AutoFillProfile* profile = *iter; | 484 AutoFillProfile* profile = *iter; |
| 470 | 485 |
| 471 // The value of the stored data for this field type in the |profile|. | 486 // The value of the stored data for this field type in the |profile|. |
| 472 string16 profile_field_value = profile->GetFieldText(type); | 487 string16 profile_field_value = profile->GetFieldText(type); |
| 473 | 488 |
| 474 if (!profile_field_value.empty() && | 489 if (!profile_field_value.empty() && |
| 475 StartsWith(profile_field_value, field.value(), false)) { | 490 StartsWith(profile_field_value, field.value(), false)) { |
| 476 matched_profiles.push_back(profile); | 491 matched_profiles.push_back(profile); |
| 477 values->push_back(profile_field_value); | 492 values->push_back(profile_field_value); |
| 478 unique_ids->push_back(PackIDs(0, profile->unique_id())); | 493 unique_ids->push_back(PackIDs(0, profile->unique_id())); |
| 479 } | 494 } |
| 480 } | 495 } |
| 481 | 496 |
| 497 // No CC, so no icons. |
| 498 icons->resize(values->size()); |
| 499 |
| 482 AutoFillProfile::CreateInferredLabels(&matched_profiles, labels, 0, | 500 AutoFillProfile::CreateInferredLabels(&matched_profiles, labels, 0, |
| 483 type.field_type()); | 501 type.field_type()); |
| 484 | 502 |
| 485 if (!include_cc_labels || !form->HasBillingFields() || !FormIsHTTPS(form)) | 503 if (!include_cc_labels || !form->HasBillingFields() || !FormIsHTTPS(form)) |
| 486 return; | 504 return; |
| 487 | 505 |
| 488 size_t i = 0; | 506 size_t i = 0; |
| 489 std::vector<string16> expanded_values; | 507 std::vector<string16> expanded_values; |
| 490 std::vector<string16> expanded_labels; | 508 std::vector<string16> expanded_labels; |
| 491 std::vector<int> expanded_ids; | 509 std::vector<int> expanded_ids; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 506 expanded_values.swap(*values); | 524 expanded_values.swap(*values); |
| 507 expanded_ids.swap(*unique_ids); | 525 expanded_ids.swap(*unique_ids); |
| 508 } | 526 } |
| 509 | 527 |
| 510 void AutoFillManager::GetBillingProfileSuggestions( | 528 void AutoFillManager::GetBillingProfileSuggestions( |
| 511 FormStructure* form, | 529 FormStructure* form, |
| 512 const FormField& field, | 530 const FormField& field, |
| 513 AutoFillType type, | 531 AutoFillType type, |
| 514 std::vector<string16>* values, | 532 std::vector<string16>* values, |
| 515 std::vector<string16>* labels, | 533 std::vector<string16>* labels, |
| 534 std::vector<string16>* icons, |
| 516 std::vector<int>* unique_ids) { | 535 std::vector<int>* unique_ids) { |
| 517 std::vector<CreditCard*> matching_creditcards; | 536 std::vector<CreditCard*> matching_creditcards; |
| 518 std::vector<AutoFillProfile*> matching_profiles; | 537 std::vector<AutoFillProfile*> matching_profiles; |
| 519 std::vector<string16> cc_values; | 538 std::vector<string16> cc_values; |
| 520 std::vector<string16> cc_labels; | 539 std::vector<string16> cc_labels; |
| 521 | 540 |
| 522 // If the form is non-HTTPS, no CC suggestions are provided; however, give the | 541 // If the form is non-HTTPS, no CC suggestions are provided; however, give the |
| 523 // user the option of filling the billing address fields with regular address | 542 // user the option of filling the billing address fields with regular address |
| 524 // data. | 543 // data. |
| 525 if (!FormIsHTTPS(form)) { | 544 if (!FormIsHTTPS(form)) { |
| 526 GetProfileSuggestions(form, field, type, false, values, labels, unique_ids); | 545 GetProfileSuggestions( |
| 546 form, field, type, false, values, icons, labels, unique_ids); |
| 527 return; | 547 return; |
| 528 } | 548 } |
| 529 | 549 |
| 530 for (std::vector<CreditCard*>::const_iterator cc = | 550 for (std::vector<CreditCard*>::const_iterator cc = |
| 531 personal_data_->credit_cards().begin(); | 551 personal_data_->credit_cards().begin(); |
| 532 cc != personal_data_->credit_cards().end(); ++cc) { | 552 cc != personal_data_->credit_cards().end(); ++cc) { |
| 533 string16 label = (*cc)->billing_address(); | 553 string16 label = (*cc)->billing_address(); |
| 534 AutoFillProfile* billing_profile = NULL; | 554 AutoFillProfile* billing_profile = NULL; |
| 535 | 555 |
| 536 // The value of the stored data for this field type in the |profile|. | 556 // The value of the stored data for this field type in the |profile|. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 554 continue; | 574 continue; |
| 555 | 575 |
| 556 for (std::vector<AutoFillProfile*>::const_iterator iter = | 576 for (std::vector<AutoFillProfile*>::const_iterator iter = |
| 557 personal_data_->profiles().begin(); | 577 personal_data_->profiles().begin(); |
| 558 iter != personal_data_->profiles().end(); ++iter) { | 578 iter != personal_data_->profiles().end(); ++iter) { |
| 559 values->push_back(billing_profile->GetFieldText(type)); | 579 values->push_back(billing_profile->GetFieldText(type)); |
| 560 | 580 |
| 561 string16 label = (*iter)->Label() + kLabelSeparator + | 581 string16 label = (*iter)->Label() + kLabelSeparator + |
| 562 (*cc)->LastFourDigits(); | 582 (*cc)->LastFourDigits(); |
| 563 labels->push_back(label); | 583 labels->push_back(label); |
| 584 icons->push_back(ASCIIToUTF16(kGenericCC)); |
| 564 unique_ids->push_back( | 585 unique_ids->push_back( |
| 565 PackIDs((*cc)->unique_id(), (*iter)->unique_id())); | 586 PackIDs((*cc)->unique_id(), (*iter)->unique_id())); |
| 566 } | 587 } |
| 567 } | 588 } |
| 568 } | 589 } |
| 569 | 590 |
| 570 void AutoFillManager::GetCreditCardSuggestions(FormStructure* form, | 591 void AutoFillManager::GetCreditCardSuggestions(FormStructure* form, |
| 571 const FormField& field, | 592 const FormField& field, |
| 572 AutoFillType type, | 593 AutoFillType type, |
| 573 std::vector<string16>* values, | 594 std::vector<string16>* values, |
| 574 std::vector<string16>* labels, | 595 std::vector<string16>* labels, |
| 596 std::vector<string16>* icons, |
| 575 std::vector<int>* unique_ids) { | 597 std::vector<int>* unique_ids) { |
| 576 // Don't return CC suggestions for non-HTTPS pages. | 598 // Don't return CC suggestions for non-HTTPS pages. |
| 577 if (!FormIsHTTPS(form)) | 599 if (!FormIsHTTPS(form)) |
| 578 return; | 600 return; |
| 579 | 601 |
| 580 for (std::vector<CreditCard*>::const_iterator iter = | 602 for (std::vector<CreditCard*>::const_iterator iter = |
| 581 personal_data_->credit_cards().begin(); | 603 personal_data_->credit_cards().begin(); |
| 582 iter != personal_data_->credit_cards().end(); ++iter) { | 604 iter != personal_data_->credit_cards().end(); ++iter) { |
| 583 CreditCard* credit_card = *iter; | 605 CreditCard* credit_card = *iter; |
| 584 | 606 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 596 unique_ids->push_back(PackIDs(credit_card->unique_id(), 0)); | 618 unique_ids->push_back(PackIDs(credit_card->unique_id(), 0)); |
| 597 } else { | 619 } else { |
| 598 for (std::vector<AutoFillProfile*>::const_iterator iter = | 620 for (std::vector<AutoFillProfile*>::const_iterator iter = |
| 599 personal_data_->profiles().begin(); | 621 personal_data_->profiles().begin(); |
| 600 iter != personal_data_->profiles().end(); ++iter) { | 622 iter != personal_data_->profiles().end(); ++iter) { |
| 601 values->push_back(creditcard_field_value); | 623 values->push_back(creditcard_field_value); |
| 602 | 624 |
| 603 string16 label = (*iter)->Label() + kLabelSeparator + | 625 string16 label = (*iter)->Label() + kLabelSeparator + |
| 604 credit_card->LastFourDigits(); | 626 credit_card->LastFourDigits(); |
| 605 labels->push_back(label); | 627 labels->push_back(label); |
| 628 icons->push_back(ASCIIToUTF16(kGenericCC)); |
| 606 unique_ids->push_back( | 629 unique_ids->push_back( |
| 607 PackIDs(credit_card->unique_id(), (*iter)->unique_id())); | 630 PackIDs(credit_card->unique_id(), (*iter)->unique_id())); |
| 608 } | 631 } |
| 609 } | 632 } |
| 610 } | 633 } |
| 611 } | 634 } |
| 612 } | 635 } |
| 613 | 636 |
| 614 void AutoFillManager::FillBillingFormField(const CreditCard* credit_card, | 637 void AutoFillManager::FillBillingFormField(const CreditCard* credit_card, |
| 615 AutoFillType type, | 638 AutoFillType type, |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 742 | 765 |
| 743 // When receiving IDs (across processes) from the renderer we unpack credit card | 766 // When receiving IDs (across processes) from the renderer we unpack credit card |
| 744 // and profile IDs from a single integer. Credit card IDs are stored in the | 767 // and profile IDs from a single integer. Credit card IDs are stored in the |
| 745 // high word and profile IDs are stored in the low word. | 768 // high word and profile IDs are stored in the low word. |
| 746 // static | 769 // static |
| 747 void AutoFillManager::UnpackIDs(int id, int* cc_id, int* profile_id) { | 770 void AutoFillManager::UnpackIDs(int id, int* cc_id, int* profile_id) { |
| 748 *cc_id = id >> std::numeric_limits<unsigned short>::digits & | 771 *cc_id = id >> std::numeric_limits<unsigned short>::digits & |
| 749 std::numeric_limits<unsigned short>::max(); | 772 std::numeric_limits<unsigned short>::max(); |
| 750 *profile_id = id & std::numeric_limits<unsigned short>::max(); | 773 *profile_id = id & std::numeric_limits<unsigned short>::max(); |
| 751 } | 774 } |
| OLD | NEW |