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 <string> | 8 #include <string> |
8 | 9 |
9 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
10 #include "base/string16.h" | 11 #include "base/string16.h" |
11 #include "chrome/browser/autofill/autofill_dialog.h" | 12 #include "chrome/browser/autofill/autofill_dialog.h" |
12 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" | 13 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" |
13 #include "chrome/browser/autofill/form_structure.h" | 14 #include "chrome/browser/autofill/form_structure.h" |
14 #include "chrome/browser/autofill/select_control_handler.h" | 15 #include "chrome/browser/autofill/select_control_handler.h" |
15 #include "chrome/browser/pref_service.h" | 16 #include "chrome/browser/pref_service.h" |
16 #include "chrome/browser/profile.h" | 17 #include "chrome/browser/profile.h" |
(...skipping 14 matching lines...) Expand all Loading... |
31 // The rate for positive/negative matches potentially could be different. | 32 // The rate for positive/negative matches potentially could be different. |
32 const double kAutoFillPositiveUploadRateDefaultValue = 0.01; | 33 const double kAutoFillPositiveUploadRateDefaultValue = 0.01; |
33 const double kAutoFillNegativeUploadRateDefaultValue = 0.01; | 34 const double kAutoFillNegativeUploadRateDefaultValue = 0.01; |
34 | 35 |
35 // 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. |
36 const int kAutoFillPhoneNumberPrefixOffset = 0; | 37 const int kAutoFillPhoneNumberPrefixOffset = 0; |
37 const int kAutoFillPhoneNumberPrefixCount = 3; | 38 const int kAutoFillPhoneNumberPrefixCount = 3; |
38 const int kAutoFillPhoneNumberSuffixOffset = 3; | 39 const int kAutoFillPhoneNumberSuffixOffset = 3; |
39 const int kAutoFillPhoneNumberSuffixCount = 4; | 40 const int kAutoFillPhoneNumberSuffixCount = 4; |
40 | 41 |
| 42 const string16::value_type kCreditCardLabelPrefix[] = {'*', 0}; |
41 const string16::value_type kLabelSeparator[] = {';',' ', '*', 0}; | 43 const string16::value_type kLabelSeparator[] = {';',' ', '*', 0}; |
42 | 44 |
43 // Removes duplicate elements whilst preserving original order of |elements| and | 45 // Removes duplicate elements whilst preserving original order of |elements| and |
44 // |unique_ids|. | 46 // |unique_ids|. |
45 void RemoveDuplicateElements( | 47 void RemoveDuplicateElements( |
46 std::vector<string16>* elements, std::vector<int>* unique_ids) { | 48 std::vector<string16>* elements, std::vector<int>* unique_ids) { |
47 DCHECK_EQ(elements->size(), unique_ids->size()); | 49 DCHECK_EQ(elements->size(), unique_ids->size()); |
48 | 50 |
49 std::vector<string16> elements_copy; | 51 std::vector<string16> elements_copy; |
50 std::vector<int> unique_ids_copy; | 52 std::vector<int> unique_ids_copy; |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 } | 270 } |
269 } | 271 } |
270 | 272 |
271 if (!form_structure) | 273 if (!form_structure) |
272 return false; | 274 return false; |
273 | 275 |
274 // No data to return if there are no auto-fillable fields. | 276 // No data to return if there are no auto-fillable fields. |
275 if (!form_structure->autofill_count()) | 277 if (!form_structure->autofill_count()) |
276 return false; | 278 return false; |
277 | 279 |
278 // |cc_digits| will contain the last four digits of a credit card number only | 280 // Unpack the |unique_id| into component parts. |
279 // if the form has billing fields. | 281 int cc_id = 0; |
280 string16 cc_digits; | 282 int profile_id = 0; |
| 283 UnpackIDs(unique_id, &cc_id, &profile_id); |
281 | 284 |
282 // If the form has billing fields, |label| will contain at least one "; " | 285 // Find the profile that matches the |profile_id|, if one is specified. |
283 // followed by the last four digits of a credit card number. | 286 const AutoFillProfile* profile = NULL; |
284 if (form_structure->HasBillingFields()) { | 287 if (profile_id != 0) { |
285 // We must search for the last "; " as it's possible the profile label | 288 for (std::vector<AutoFillProfile*>::const_iterator iter = profiles.begin(); |
286 // proper can contain this sequence of characters. | 289 iter != profiles.end(); ++iter) { |
287 size_t index = label.find_last_of(kLabelSeparator); | 290 if ((*iter)->unique_id() == profile_id) { |
288 if (index != string16::npos) { | 291 profile = *iter; |
289 size_t cc_index = index + 1; | 292 break; |
290 cc_digits = label.substr(cc_index); | 293 } |
291 } | 294 } |
292 } | 295 } |
293 | 296 |
294 // Find the profile that matches the |unique_id|. | 297 // Find the credit card that matches the |cc_id|, if one is specified. |
295 const AutoFillProfile* profile = NULL; | |
296 for (std::vector<AutoFillProfile*>::const_iterator iter = profiles.begin(); | |
297 iter != profiles.end(); ++iter) { | |
298 if ((*iter)->unique_id() == unique_id) { | |
299 profile = *iter; | |
300 } | |
301 } | |
302 | |
303 // Don't look for a matching credit card if we fully-matched the profile using | |
304 // the entire label. | |
305 const CreditCard* credit_card = NULL; | 298 const CreditCard* credit_card = NULL; |
306 if (!cc_digits.empty()) { | 299 if (cc_id != 0) { |
307 // Find the credit card that matches the |cc_label|. | |
308 for (std::vector<CreditCard*>::const_iterator iter = credit_cards.begin(); | 300 for (std::vector<CreditCard*>::const_iterator iter = credit_cards.begin(); |
309 iter != credit_cards.end(); ++iter) { | 301 iter != credit_cards.end(); ++iter) { |
310 if ((*iter)->LastFourDigits() == cc_digits) { | 302 if ((*iter)->unique_id() == cc_id) { |
311 credit_card = *iter; | 303 credit_card = *iter; |
312 break; | 304 break; |
313 } | 305 } |
314 } | 306 } |
315 } | 307 } |
316 | 308 |
317 if (!profile && !credit_card) | 309 if (!profile && !credit_card) |
318 return false; | 310 return false; |
319 | 311 |
320 // The list of fields in |form_structure| and |result.fields| often match | 312 // The list of fields in |form_structure| and |result.fields| often match |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 iter != profiles.end(); ++iter) { | 468 iter != profiles.end(); ++iter) { |
477 AutoFillProfile* profile = *iter; | 469 AutoFillProfile* profile = *iter; |
478 | 470 |
479 // The value of the stored data for this field type in the |profile|. | 471 // The value of the stored data for this field type in the |profile|. |
480 string16 profile_field_value = profile->GetFieldText(type); | 472 string16 profile_field_value = profile->GetFieldText(type); |
481 | 473 |
482 if (!profile_field_value.empty() && | 474 if (!profile_field_value.empty() && |
483 StartsWith(profile_field_value, field.value(), false)) { | 475 StartsWith(profile_field_value, field.value(), false)) { |
484 matched_profiles.push_back(profile); | 476 matched_profiles.push_back(profile); |
485 values->push_back(profile_field_value); | 477 values->push_back(profile_field_value); |
486 unique_ids->push_back(profile->unique_id()); | 478 unique_ids->push_back(PackIDs(0, profile->unique_id())); |
487 } | 479 } |
488 } | 480 } |
489 | 481 |
490 AutoFillProfile::CreateInferredLabels(&matched_profiles, labels, 0, | 482 AutoFillProfile::CreateInferredLabels(&matched_profiles, labels, 0, |
491 type.field_type()); | 483 type.field_type()); |
492 | 484 |
493 if (!include_cc_labels || !form->HasBillingFields() || !FormIsHTTPS(form)) | 485 if (!include_cc_labels || !form->HasBillingFields() || !FormIsHTTPS(form)) |
494 return; | 486 return; |
495 | 487 |
496 size_t i = 0; | 488 size_t i = 0; |
497 std::vector<string16> expanded_values; | 489 std::vector<string16> expanded_values; |
498 std::vector<string16> expanded_labels; | 490 std::vector<string16> expanded_labels; |
| 491 std::vector<int> expanded_ids; |
499 for (std::vector<AutoFillProfile*>::const_iterator iter = | 492 for (std::vector<AutoFillProfile*>::const_iterator iter = |
500 matched_profiles.begin(); iter != matched_profiles.end(); | 493 matched_profiles.begin(); iter != matched_profiles.end(); |
501 ++iter, ++i) { | 494 ++iter, ++i) { |
502 AutoFillProfile* profile = *iter; | 495 AutoFillProfile* profile = *iter; |
503 for (std::vector<CreditCard*>::const_iterator cc = | 496 for (std::vector<CreditCard*>::const_iterator cc = |
504 personal_data_->credit_cards().begin(); | 497 personal_data_->credit_cards().begin(); |
505 cc != personal_data_->credit_cards().end(); ++cc) { | 498 cc != personal_data_->credit_cards().end(); ++cc) { |
506 expanded_values.push_back((*values)[i]); | 499 expanded_values.push_back((*values)[i]); |
507 string16 label = (*labels)[i] + kLabelSeparator + (*cc)->LastFourDigits(); | 500 string16 label = (*labels)[i] + kLabelSeparator + (*cc)->LastFourDigits(); |
508 expanded_labels.push_back(label); | 501 expanded_labels.push_back(label); |
509 unique_ids->push_back(profile->unique_id()); | 502 expanded_ids.push_back(PackIDs((*cc)->unique_id(), profile->unique_id())); |
510 } | 503 } |
511 } | 504 } |
512 expanded_labels.swap(*labels); | 505 expanded_labels.swap(*labels); |
513 expanded_values.swap(*values); | 506 expanded_values.swap(*values); |
| 507 expanded_ids.swap(*unique_ids); |
514 } | 508 } |
515 | 509 |
516 void AutoFillManager::GetBillingProfileSuggestions( | 510 void AutoFillManager::GetBillingProfileSuggestions( |
517 FormStructure* form, | 511 FormStructure* form, |
518 const FormField& field, | 512 const FormField& field, |
519 AutoFillType type, | 513 AutoFillType type, |
520 std::vector<string16>* values, | 514 std::vector<string16>* values, |
521 std::vector<string16>* labels, | 515 std::vector<string16>* labels, |
522 std::vector<int>* unique_ids) { | 516 std::vector<int>* unique_ids) { |
523 std::vector<CreditCard*> matching_creditcards; | 517 std::vector<CreditCard*> matching_creditcards; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 continue; | 554 continue; |
561 | 555 |
562 for (std::vector<AutoFillProfile*>::const_iterator iter = | 556 for (std::vector<AutoFillProfile*>::const_iterator iter = |
563 personal_data_->profiles().begin(); | 557 personal_data_->profiles().begin(); |
564 iter != personal_data_->profiles().end(); ++iter) { | 558 iter != personal_data_->profiles().end(); ++iter) { |
565 values->push_back(billing_profile->GetFieldText(type)); | 559 values->push_back(billing_profile->GetFieldText(type)); |
566 | 560 |
567 string16 label = (*iter)->Label() + kLabelSeparator + | 561 string16 label = (*iter)->Label() + kLabelSeparator + |
568 (*cc)->LastFourDigits(); | 562 (*cc)->LastFourDigits(); |
569 labels->push_back(label); | 563 labels->push_back(label); |
570 unique_ids->push_back((*iter)->unique_id()); | 564 unique_ids->push_back( |
| 565 PackIDs((*cc)->unique_id(), (*iter)->unique_id())); |
571 } | 566 } |
572 } | 567 } |
573 } | 568 } |
574 | 569 |
575 void AutoFillManager::GetCreditCardSuggestions(FormStructure* form, | 570 void AutoFillManager::GetCreditCardSuggestions(FormStructure* form, |
576 const FormField& field, | 571 const FormField& field, |
577 AutoFillType type, | 572 AutoFillType type, |
578 std::vector<string16>* values, | 573 std::vector<string16>* values, |
579 std::vector<string16>* labels, | 574 std::vector<string16>* labels, |
580 std::vector<int>* unique_ids) { | 575 std::vector<int>* unique_ids) { |
581 // Don't return CC suggestions for non-HTTPS pages. | 576 // Don't return CC suggestions for non-HTTPS pages. |
582 if (!FormIsHTTPS(form)) | 577 if (!FormIsHTTPS(form)) |
583 return; | 578 return; |
584 | 579 |
585 for (std::vector<CreditCard*>::const_iterator iter = | 580 for (std::vector<CreditCard*>::const_iterator iter = |
586 personal_data_->credit_cards().begin(); | 581 personal_data_->credit_cards().begin(); |
587 iter != personal_data_->credit_cards().end(); ++iter) { | 582 iter != personal_data_->credit_cards().end(); ++iter) { |
588 CreditCard* credit_card = *iter; | 583 CreditCard* credit_card = *iter; |
589 | 584 |
590 // The value of the stored data for this field type in the |credit_card|. | 585 // The value of the stored data for this field type in the |credit_card|. |
591 string16 creditcard_field_value = | 586 string16 creditcard_field_value = credit_card->GetFieldText(type); |
592 credit_card->GetFieldText(type); | |
593 if (!creditcard_field_value.empty() && | 587 if (!creditcard_field_value.empty() && |
594 StartsWith(creditcard_field_value, field.value(), false)) { | 588 StartsWith(creditcard_field_value, field.value(), false)) { |
595 if (type.field_type() == CREDIT_CARD_NUMBER) | 589 if (type.field_type() == CREDIT_CARD_NUMBER) |
596 creditcard_field_value = credit_card->ObfuscatedNumber(); | 590 creditcard_field_value = credit_card->ObfuscatedNumber(); |
597 | 591 |
598 if (!form->HasNonBillingFields()) { | 592 if (!form->HasNonBillingFields()) { |
599 values->push_back(creditcard_field_value); | 593 values->push_back(creditcard_field_value); |
600 labels->push_back(credit_card->Label()); | 594 labels->push_back( |
601 unique_ids->push_back(credit_card->unique_id()); | 595 kCreditCardLabelPrefix + credit_card->LastFourDigits()); |
| 596 unique_ids->push_back(PackIDs(credit_card->unique_id(), 0)); |
602 } else { | 597 } else { |
603 for (std::vector<AutoFillProfile*>::const_iterator iter = | 598 for (std::vector<AutoFillProfile*>::const_iterator iter = |
604 personal_data_->profiles().begin(); | 599 personal_data_->profiles().begin(); |
605 iter != personal_data_->profiles().end(); ++iter) { | 600 iter != personal_data_->profiles().end(); ++iter) { |
606 values->push_back(creditcard_field_value); | 601 values->push_back(creditcard_field_value); |
607 | 602 |
608 string16 label = (*iter)->Label() + kLabelSeparator + | 603 string16 label = (*iter)->Label() + kLabelSeparator + |
609 credit_card->LastFourDigits(); | 604 credit_card->LastFourDigits(); |
610 labels->push_back(label); | 605 labels->push_back(label); |
611 unique_ids->push_back((*iter)->unique_id()); | 606 unique_ids->push_back( |
| 607 PackIDs(credit_card->unique_id(), (*iter)->unique_id())); |
612 } | 608 } |
613 } | 609 } |
614 } | 610 } |
615 } | 611 } |
616 } | 612 } |
617 | 613 |
618 void AutoFillManager::FillBillingFormField(const CreditCard* credit_card, | 614 void AutoFillManager::FillBillingFormField(const CreditCard* credit_card, |
619 AutoFillType type, | 615 AutoFillType type, |
620 webkit_glue::FormField* field) { | 616 webkit_glue::FormField* field) { |
621 DCHECK(credit_card); | 617 DCHECK(credit_card); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
725 continue; | 721 continue; |
726 | 722 |
727 DeterminePossibleFieldTypes(form_structure); | 723 DeterminePossibleFieldTypes(form_structure); |
728 form_structures_.push_back(form_structure); | 724 form_structures_.push_back(form_structure); |
729 } | 725 } |
730 | 726 |
731 // If none of the forms were parsed, no use querying the server. | 727 // If none of the forms were parsed, no use querying the server. |
732 if (!form_structures_.empty()) | 728 if (!form_structures_.empty()) |
733 download_manager_.StartQueryRequest(form_structures_); | 729 download_manager_.StartQueryRequest(form_structures_); |
734 } | 730 } |
| 731 |
| 732 // When sending IDs (across processes) to the renderer we pack credit card and |
| 733 // profile IDs into a single integer. Credit card IDs are sent in the high |
| 734 // word and profile IDs are sent in the low word. |
| 735 // static |
| 736 int AutoFillManager::PackIDs(int cc_id, int profile_id) { |
| 737 DCHECK(cc_id <= std::numeric_limits<unsigned short>::max()); |
| 738 DCHECK(profile_id <= std::numeric_limits<unsigned short>::max()); |
| 739 |
| 740 return cc_id << std::numeric_limits<unsigned short>::digits | profile_id; |
| 741 } |
| 742 |
| 743 // 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 |
| 745 // high word and profile IDs are stored in the low word. |
| 746 // static |
| 747 void AutoFillManager::UnpackIDs(int id, int* cc_id, int* profile_id) { |
| 748 *cc_id = id >> std::numeric_limits<unsigned short>::digits & |
| 749 std::numeric_limits<unsigned short>::max(); |
| 750 *profile_id = id & std::numeric_limits<unsigned short>::max(); |
| 751 } |
OLD | NEW |