| 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 <set> |
| 9 | 9 |
| 10 #include "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/string16.h" | 12 #include "base/string16.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" | 14 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" |
| 15 #include "chrome/browser/autofill/autofill_dialog.h" | 15 #include "chrome/browser/autofill/autofill_dialog.h" |
| 16 #include "chrome/browser/autofill/form_structure.h" | 16 #include "chrome/browser/autofill/form_structure.h" |
| 17 #include "chrome/browser/autofill/phone_number.h" | 17 #include "chrome/browser/autofill/phone_number.h" |
| 18 #include "chrome/browser/autofill/select_control_handler.h" | 18 #include "chrome/browser/autofill/select_control_handler.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 34 namespace { | 34 namespace { |
| 35 | 35 |
| 36 // We only send a fraction of the forms to upload server. | 36 // We only send a fraction of the forms to upload server. |
| 37 // The rate for positive/negative matches potentially could be different. | 37 // The rate for positive/negative matches potentially could be different. |
| 38 const double kAutoFillPositiveUploadRateDefaultValue = 0.01; | 38 const double kAutoFillPositiveUploadRateDefaultValue = 0.01; |
| 39 const double kAutoFillNegativeUploadRateDefaultValue = 0.01; | 39 const double kAutoFillNegativeUploadRateDefaultValue = 0.01; |
| 40 | 40 |
| 41 const string16::value_type kCreditCardPrefix[] = {'*',0}; | 41 const string16::value_type kCreditCardPrefix[] = {'*',0}; |
| 42 const string16::value_type kLabelSeparator[] = {';',' ','*',0}; | 42 const string16::value_type kLabelSeparator[] = {';',' ','*',0}; |
| 43 | 43 |
| 44 // Removes duplicate elements whilst preserving original order of |elements| and | 44 // Removes duplicate suggestions whilst preserving their original order. |
| 45 // |unique_ids|. | 45 void RemoveDuplicateSuggestions(std::vector<string16>* values, |
| 46 void RemoveDuplicateElements( | 46 std::vector<string16>* labels, |
| 47 std::vector<string16>* elements, std::vector<int>* unique_ids) { | 47 std::vector<string16>* icons, |
| 48 DCHECK_EQ(elements->size(), unique_ids->size()); | 48 std::vector<int>* unique_ids) { |
| 49 DCHECK_EQ(values->size(), labels->size()); |
| 50 DCHECK_EQ(values->size(), icons->size()); |
| 51 DCHECK_EQ(values->size(), unique_ids->size()); |
| 49 | 52 |
| 50 std::vector<string16> elements_copy; | 53 std::set<std::pair<string16, string16> > seen_suggestions; |
| 54 std::vector<string16> values_copy; |
| 55 std::vector<string16> labels_copy; |
| 56 std::vector<string16> icons_copy; |
| 51 std::vector<int> unique_ids_copy; | 57 std::vector<int> unique_ids_copy; |
| 52 for (size_t i = 0; i < elements->size(); ++i) { | |
| 53 const string16& element = (*elements)[i]; | |
| 54 | 58 |
| 55 bool unique = true; | 59 for (size_t i = 0; i < values->size(); ++i) { |
| 56 for (std::vector<string16>::const_iterator copy_iter | 60 const std::pair<string16, string16> suggestion((*values)[i], (*labels)[i]); |
| 57 = elements_copy.begin(); | 61 if (seen_suggestions.insert(suggestion).second) { |
| 58 copy_iter != elements_copy.end(); ++copy_iter) { | 62 values_copy.push_back((*values)[i]); |
| 59 if (element == *copy_iter) { | 63 labels_copy.push_back((*labels)[i]); |
| 60 unique = false; | 64 icons_copy.push_back((*icons)[i]); |
| 61 break; | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 if (unique) { | |
| 66 elements_copy.push_back(element); | |
| 67 unique_ids_copy.push_back((*unique_ids)[i]); | 65 unique_ids_copy.push_back((*unique_ids)[i]); |
| 68 } | 66 } |
| 69 } | 67 } |
| 70 | 68 |
| 71 elements->assign(elements_copy.begin(), elements_copy.end()); | 69 values->swap(values_copy); |
| 72 unique_ids->assign(unique_ids_copy.begin(), unique_ids_copy.end()); | 70 labels->swap(labels_copy); |
| 71 icons->swap(icons_copy); |
| 72 unique_ids->swap(unique_ids_copy); |
| 73 } | 73 } |
| 74 | 74 |
| 75 // Precondition: |form_structure| and |form| should correspond to the same | 75 // Precondition: |form_structure| and |form| should correspond to the same |
| 76 // logical form. Returns true if the relevant portion of |form| is auto-filled. | 76 // logical form. Returns true if the relevant portion of |form| is auto-filled. |
| 77 // If |is_filling_credit_card|, the relevant portion is the credit card portion; | 77 // If |is_filling_credit_card|, the relevant portion is the credit card portion; |
| 78 // otherwise it is the address and contact info portion. | 78 // otherwise it is the address and contact info portion. |
| 79 bool FormIsAutoFilled(const FormStructure* form_structure, | 79 bool FormIsAutoFilled(const FormStructure* form_structure, |
| 80 const webkit_glue::FormData& form, | 80 const webkit_glue::FormData& form, |
| 81 bool is_filling_credit_card) { | 81 bool is_filling_credit_card) { |
| 82 // TODO(isherman): It would be nice to share most of this code with the loop | 82 // TODO(isherman): It would be nice to share most of this code with the loop |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 values.assign(1, l10n_util::GetStringUTF16(warning)); | 244 values.assign(1, l10n_util::GetStringUTF16(warning)); |
| 245 labels.assign(1, string16()); | 245 labels.assign(1, string16()); |
| 246 icons.assign(1, string16()); | 246 icons.assign(1, string16()); |
| 247 unique_ids.assign(1, -1); | 247 unique_ids.assign(1, -1); |
| 248 host->AutoFillSuggestionsReturned(values, labels, icons, unique_ids); | 248 host->AutoFillSuggestionsReturned(values, labels, icons, unique_ids); |
| 249 return true; | 249 return true; |
| 250 } | 250 } |
| 251 | 251 |
| 252 // If the form is auto-filled and the renderer is querying for suggestions, | 252 // If the form is auto-filled and the renderer is querying for suggestions, |
| 253 // then the user is editing the value of a field. In this case, mimick | 253 // then the user is editing the value of a field. In this case, mimick |
| 254 // autocomplete. In particular, don't display labels, as that information is | 254 // autocomplete: don't display or icons, as that information is redundant. |
| 255 // redundant. In addition, remove duplicate values. | |
| 256 if (FormIsAutoFilled(form_structure, form, is_filling_credit_card)) { | 255 if (FormIsAutoFilled(form_structure, form, is_filling_credit_card)) { |
| 257 RemoveDuplicateElements(&values, &unique_ids); | 256 labels.assign(labels.size(), string16()); |
| 258 labels.assign(values.size(), string16()); | 257 icons.assign(icons.size(), string16()); |
| 259 icons.assign(values.size(), string16()); | |
| 260 } | 258 } |
| 261 | 259 |
| 260 RemoveDuplicateSuggestions(&values, &labels, &icons, &unique_ids); |
| 262 host->AutoFillSuggestionsReturned(values, labels, icons, unique_ids); | 261 host->AutoFillSuggestionsReturned(values, labels, icons, unique_ids); |
| 263 return true; | 262 return true; |
| 264 } | 263 } |
| 265 | 264 |
| 266 bool AutoFillManager::FillAutoFillFormData(int query_id, | 265 bool AutoFillManager::FillAutoFillFormData(int query_id, |
| 267 const FormData& form, | 266 const FormData& form, |
| 268 const FormField& field, | 267 const FormField& field, |
| 269 int unique_id) { | 268 int unique_id) { |
| 270 const std::vector<AutoFillProfile*>& profiles = personal_data_->profiles(); | 269 const std::vector<AutoFillProfile*>& profiles = personal_data_->profiles(); |
| 271 const std::vector<CreditCard*>& credit_cards = personal_data_->credit_cards(); | 270 const std::vector<CreditCard*>& credit_cards = personal_data_->credit_cards(); |
| (...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 770 return std::string(); | 769 return std::string(); |
| 771 | 770 |
| 772 std::map<int, std::string>::const_iterator iter = id_guid_map_.find(id); | 771 std::map<int, std::string>::const_iterator iter = id_guid_map_.find(id); |
| 773 if (iter == id_guid_map_.end()) { | 772 if (iter == id_guid_map_.end()) { |
| 774 NOTREACHED(); | 773 NOTREACHED(); |
| 775 return std::string(); | 774 return std::string(); |
| 776 } | 775 } |
| 777 | 776 |
| 778 return iter->second; | 777 return iter->second; |
| 779 } | 778 } |
| OLD | NEW |