OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/autofill/core/browser/autocomplete_history_manager.h" | 5 #include "components/autofill/core/browser/autocomplete_history_manager.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/profiler/scoped_tracker.h" | 10 #include "base/profiler/scoped_tracker.h" |
11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
| 12 #include "base/strings/string_util.h" |
12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
13 #include "components/autofill/core/browser/autofill_client.h" | 14 #include "components/autofill/core/browser/autofill_client.h" |
14 #include "components/autofill/core/browser/autofill_driver.h" | 15 #include "components/autofill/core/browser/autofill_driver.h" |
15 #include "components/autofill/core/browser/autofill_external_delegate.h" | 16 #include "components/autofill/core/browser/autofill_external_delegate.h" |
16 #include "components/autofill/core/browser/validation.h" | 17 #include "components/autofill/core/browser/validation.h" |
17 #include "components/autofill/core/common/autofill_pref_names.h" | 18 #include "components/autofill/core/common/autofill_pref_names.h" |
| 19 #include "components/autofill/core/common/autofill_util.h" |
18 #include "components/autofill/core/common/form_data.h" | 20 #include "components/autofill/core/common/form_data.h" |
19 | 21 |
20 namespace autofill { | 22 namespace autofill { |
21 namespace { | 23 namespace { |
22 | 24 |
23 // Limit on the number of suggestions to appear in the pop-up menu under an | 25 // Limit on the number of suggestions to appear in the pop-up menu under an |
24 // text input element in a form. | 26 // text input element in a form. |
25 const int kMaxAutocompleteMenuItems = 6; | 27 const int kMaxAutocompleteMenuItems = 6; |
26 | 28 |
27 bool IsTextField(const FormFieldData& field) { | 29 bool IsTextField(const FormFieldData& field) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 // See http://crbug.com/68783. | 76 // See http://crbug.com/68783. |
75 if (!result) { | 77 if (!result) { |
76 SendSuggestions(NULL); | 78 SendSuggestions(NULL); |
77 return; | 79 return; |
78 } | 80 } |
79 | 81 |
80 DCHECK_EQ(AUTOFILL_VALUE_RESULT, result->GetType()); | 82 DCHECK_EQ(AUTOFILL_VALUE_RESULT, result->GetType()); |
81 const WDResult<std::vector<base::string16> >* autofill_result = | 83 const WDResult<std::vector<base::string16> >* autofill_result = |
82 static_cast<const WDResult<std::vector<base::string16> >*>(result); | 84 static_cast<const WDResult<std::vector<base::string16> >*>(result); |
83 std::vector<base::string16> suggestions = autofill_result->GetValue(); | 85 std::vector<base::string16> suggestions = autofill_result->GetValue(); |
84 SendSuggestions(&suggestions); | 86 |
| 87 if (!IsFeatureSubstringMatchEnabled() || user_input_.empty()) { |
| 88 SendSuggestions(&suggestions); |
| 89 } else { |
| 90 std::vector<base::string16> preferred_suggestions; |
| 91 for (base::string16 suggestion : suggestions) { |
| 92 if (StartsWith(suggestion, user_input_, false) || |
| 93 IsContentsPrefixOfSuggestionToken(suggestion, user_input_, false)) { |
| 94 preferred_suggestions.push_back(suggestion); |
| 95 } |
| 96 } |
| 97 |
| 98 SendSuggestions(&preferred_suggestions); |
| 99 } |
85 } | 100 } |
86 | 101 |
87 void AutocompleteHistoryManager::OnGetAutocompleteSuggestions( | 102 void AutocompleteHistoryManager::OnGetAutocompleteSuggestions( |
88 int query_id, | 103 int query_id, |
89 const base::string16& name, | 104 const base::string16& name, |
90 const base::string16& prefix, | 105 const base::string16& prefix, |
91 const std::string& form_control_type, | 106 const std::string& form_control_type, |
92 const std::vector<Suggestion>& suggestions) { | 107 const std::vector<Suggestion>& suggestions) { |
93 CancelPendingQuery(); | 108 CancelPendingQuery(); |
94 | 109 |
95 query_id_ = query_id; | 110 query_id_ = query_id; |
96 autofill_suggestions_ = suggestions; | 111 autofill_suggestions_ = suggestions; |
97 if (!autofill_client_->IsAutocompleteEnabled() || | 112 if (!autofill_client_->IsAutocompleteEnabled() || |
98 form_control_type == "textarea") { | 113 form_control_type == "textarea") { |
99 SendSuggestions(NULL); | 114 SendSuggestions(NULL); |
100 return; | 115 return; |
101 } | 116 } |
102 | 117 |
103 if (database_.get()) { | 118 if (database_.get()) { |
| 119 user_input_ = prefix; |
104 pending_query_handle_ = database_->GetFormValuesForElementName( | 120 pending_query_handle_ = database_->GetFormValuesForElementName( |
105 name, prefix, kMaxAutocompleteMenuItems, this); | 121 name, IsFeatureSubstringMatchEnabled() ? base::string16() : prefix, |
| 122 kMaxAutocompleteMenuItems, this); |
106 } | 123 } |
107 } | 124 } |
108 | 125 |
109 void AutocompleteHistoryManager::OnFormSubmitted(const FormData& form) { | 126 void AutocompleteHistoryManager::OnFormSubmitted(const FormData& form) { |
110 if (!autofill_client_->IsAutocompleteEnabled()) | 127 if (!autofill_client_->IsAutocompleteEnabled()) |
111 return; | 128 return; |
112 | 129 |
113 if (driver_->IsOffTheRecord()) | 130 if (driver_->IsOffTheRecord()) |
114 return; | 131 return; |
115 | 132 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 } | 197 } |
181 } | 198 } |
182 | 199 |
183 external_delegate_->OnSuggestionsReturned(query_id_, autofill_suggestions_); | 200 external_delegate_->OnSuggestionsReturned(query_id_, autofill_suggestions_); |
184 | 201 |
185 query_id_ = 0; | 202 query_id_ = 0; |
186 autofill_suggestions_.clear(); | 203 autofill_suggestions_.clear(); |
187 } | 204 } |
188 | 205 |
189 } // namespace autofill | 206 } // namespace autofill |
OLD | NEW |