Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: chrome/browser/autofill/autofill_manager.cc

Issue 5877001: Don't show duplicate Autofill suggestions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/autofill_metrics.h" 16 #include "chrome/browser/autofill/autofill_metrics.h"
17 #include "chrome/browser/autofill/form_structure.h" 17 #include "chrome/browser/autofill/form_structure.h"
18 #include "chrome/browser/autofill/phone_number.h" 18 #include "chrome/browser/autofill/phone_number.h"
(...skipping 16 matching lines...) Expand all
35 namespace { 35 namespace {
36 36
37 // We only send a fraction of the forms to upload server. 37 // We only send a fraction of the forms to upload server.
38 // The rate for positive/negative matches potentially could be different. 38 // The rate for positive/negative matches potentially could be different.
39 const double kAutoFillPositiveUploadRateDefaultValue = 0.01; 39 const double kAutoFillPositiveUploadRateDefaultValue = 0.01;
40 const double kAutoFillNegativeUploadRateDefaultValue = 0.01; 40 const double kAutoFillNegativeUploadRateDefaultValue = 0.01;
41 41
42 const string16::value_type kCreditCardPrefix[] = {'*',0}; 42 const string16::value_type kCreditCardPrefix[] = {'*',0};
43 const string16::value_type kLabelSeparator[] = {';',' ','*',0}; 43 const string16::value_type kLabelSeparator[] = {';',' ','*',0};
44 44
45 // Removes duplicate elements whilst preserving original order of |elements| and 45 // Removes duplicate suggestions whilst preserving their original order.
46 // |unique_ids|. 46 void RemoveDuplicateSuggestions(std::vector<string16>* values,
47 void RemoveDuplicateElements( 47 std::vector<string16>* labels,
48 std::vector<string16>* elements, std::vector<int>* unique_ids) { 48 std::vector<string16>* icons,
49 DCHECK_EQ(elements->size(), unique_ids->size()); 49 std::vector<int>* unique_ids) {
50 DCHECK_EQ(values->size(), labels->size());
51 DCHECK_EQ(values->size(), icons->size());
52 DCHECK_EQ(values->size(), unique_ids->size());
50 53
51 std::vector<string16> elements_copy; 54 std::set<std::pair<string16, string16> > seen_suggestions;
55 std::vector<string16> values_copy;
56 std::vector<string16> labels_copy;
57 std::vector<string16> icons_copy;
52 std::vector<int> unique_ids_copy; 58 std::vector<int> unique_ids_copy;
53 for (size_t i = 0; i < elements->size(); ++i) {
54 const string16& element = (*elements)[i];
55 59
56 bool unique = true; 60 for (size_t i = 0; i < values->size(); ++i) {
57 for (std::vector<string16>::const_iterator copy_iter 61 const std::pair<string16, string16> suggestion((*values)[i], (*labels)[i]);
58 = elements_copy.begin(); 62 if (seen_suggestions.insert(suggestion).second) {
59 copy_iter != elements_copy.end(); ++copy_iter) { 63 values_copy.push_back((*values)[i]);
60 if (element == *copy_iter) { 64 labels_copy.push_back((*labels)[i]);
61 unique = false; 65 icons_copy.push_back((*icons)[i]);
62 break;
63 }
64 }
65
66 if (unique) {
67 elements_copy.push_back(element);
68 unique_ids_copy.push_back((*unique_ids)[i]); 66 unique_ids_copy.push_back((*unique_ids)[i]);
69 } 67 }
70 } 68 }
71 69
72 elements->assign(elements_copy.begin(), elements_copy.end()); 70 values->swap(values_copy);
73 unique_ids->assign(unique_ids_copy.begin(), unique_ids_copy.end()); 71 labels->swap(labels_copy);
72 icons->swap(icons_copy);
73 unique_ids->swap(unique_ids_copy);
74 } 74 }
75 75
76 // Precondition: |form_structure| and |form| should correspond to the same 76 // Precondition: |form_structure| and |form| should correspond to the same
77 // logical form. Returns true if the relevant portion of |form| is auto-filled. 77 // logical form. Returns true if the relevant portion of |form| is auto-filled.
78 // If |is_filling_credit_card|, the relevant portion is the credit card portion; 78 // If |is_filling_credit_card|, the relevant portion is the credit card portion;
79 // otherwise it is the address and contact info portion. 79 // otherwise it is the address and contact info portion.
80 bool FormIsAutoFilled(const FormStructure* form_structure, 80 bool FormIsAutoFilled(const FormStructure* form_structure,
81 const webkit_glue::FormData& form, 81 const webkit_glue::FormData& form,
82 bool is_filling_credit_card) { 82 bool is_filling_credit_card) {
83 // TODO(isherman): It would be nice to share most of this code with the loop 83 // TODO(isherman): It would be nice to share most of this code with the loop
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 values.assign(1, l10n_util::GetStringUTF16(warning)); 251 values.assign(1, l10n_util::GetStringUTF16(warning));
252 labels.assign(1, string16()); 252 labels.assign(1, string16());
253 icons.assign(1, string16()); 253 icons.assign(1, string16());
254 unique_ids.assign(1, -1); 254 unique_ids.assign(1, -1);
255 host->AutoFillSuggestionsReturned(values, labels, icons, unique_ids); 255 host->AutoFillSuggestionsReturned(values, labels, icons, unique_ids);
256 return true; 256 return true;
257 } 257 }
258 258
259 // If the form is auto-filled and the renderer is querying for suggestions, 259 // If the form is auto-filled and the renderer is querying for suggestions,
260 // then the user is editing the value of a field. In this case, mimick 260 // then the user is editing the value of a field. In this case, mimick
261 // autocomplete. In particular, don't display labels, as that information is 261 // autocomplete: Don't display or icons, as that information is redundant.
dhollowa 2010/12/16 03:57:12 "don't display labels or icons"
dhollowa 2010/12/16 15:58:27 Missed "labels".
262 // redundant. In addition, remove duplicate values.
263 if (FormIsAutoFilled(form_structure, form, is_filling_credit_card)) { 262 if (FormIsAutoFilled(form_structure, form, is_filling_credit_card)) {
264 RemoveDuplicateElements(&values, &unique_ids); 263 labels.assign(labels.size(), string16());
265 labels.assign(values.size(), string16()); 264 icons.assign(icons.size(), string16());
266 icons.assign(values.size(), string16());
267 } 265 }
268 266
267 RemoveDuplicateSuggestions(&values, &labels, &icons, &unique_ids);
269 host->AutoFillSuggestionsReturned(values, labels, icons, unique_ids); 268 host->AutoFillSuggestionsReturned(values, labels, icons, unique_ids);
270 return true; 269 return true;
271 } 270 }
272 271
273 bool AutoFillManager::FillAutoFillFormData(int query_id, 272 bool AutoFillManager::FillAutoFillFormData(int query_id,
274 const FormData& form, 273 const FormData& form,
275 const FormField& field, 274 const FormField& field,
276 int unique_id) { 275 int unique_id) {
277 const std::vector<AutoFillProfile*>& profiles = personal_data_->profiles(); 276 const std::vector<AutoFillProfile*>& profiles = personal_data_->profiles();
278 const std::vector<CreditCard*>& credit_cards = personal_data_->credit_cards(); 277 const std::vector<CreditCard*>& credit_cards = personal_data_->credit_cards();
(...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after
810 return std::string(); 809 return std::string();
811 810
812 std::map<int, std::string>::const_iterator iter = id_guid_map_.find(id); 811 std::map<int, std::string>::const_iterator iter = id_guid_map_.find(id);
813 if (iter == id_guid_map_.end()) { 812 if (iter == id_guid_map_.end()) {
814 NOTREACHED(); 813 NOTREACHED();
815 return std::string(); 814 return std::string();
816 } 815 }
817 816
818 return iter->second; 817 return iter->second;
819 } 818 }
OLDNEW
« no previous file with comments | « chrome/browser/autofill/autofill_manager.h ('k') | chrome/browser/autofill/autofill_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698