OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/utf_string_conversions.h" | 5 #include "base/utf_string_conversions.h" |
6 #include "chrome/browser/autocomplete_history_manager.h" | 6 #include "chrome/browser/autocomplete_history_manager.h" |
7 #include "chrome/browser/autofill/autofill_external_delegate.h" | 7 #include "chrome/browser/autofill/autofill_external_delegate.h" |
8 #include "chrome/browser/autofill/autofill_manager.h" | 8 #include "chrome/browser/autofill/autofill_manager.h" |
9 #include "chrome/common/autofill_messages.h" | 9 #include "chrome/common/autofill_messages.h" |
10 #include "chrome/common/chrome_constants.h" | 10 #include "chrome/common/chrome_constants.h" |
(...skipping 12 matching lines...) Expand all Loading... | |
23 | 23 |
24 AutofillExternalDelegate::AutofillExternalDelegate( | 24 AutofillExternalDelegate::AutofillExternalDelegate( |
25 TabContentsWrapper* tab_contents_wrapper, | 25 TabContentsWrapper* tab_contents_wrapper, |
26 AutofillManager* autofill_manager) | 26 AutofillManager* autofill_manager) |
27 : tab_contents_wrapper_(tab_contents_wrapper), | 27 : tab_contents_wrapper_(tab_contents_wrapper), |
28 autofill_manager_(autofill_manager), | 28 autofill_manager_(autofill_manager), |
29 password_autofill_manager_( | 29 password_autofill_manager_( |
30 tab_contents_wrapper ? tab_contents_wrapper->web_contents() : NULL), | 30 tab_contents_wrapper ? tab_contents_wrapper->web_contents() : NULL), |
31 autofill_query_id_(0), | 31 autofill_query_id_(0), |
32 display_warning_if_disabled_(false), | 32 display_warning_if_disabled_(false), |
33 has_shown_autofill_popup_for_current_edit_(false) { | 33 has_shown_autofill_popup_for_current_edit_(false), |
34 popup_visible_(false) { | |
34 } | 35 } |
35 | 36 |
36 void AutofillExternalDelegate::SelectAutofillSuggestionAtIndex(int unique_id) { | 37 void AutofillExternalDelegate::SelectAutofillSuggestionAtIndex(int unique_id) { |
37 if (unique_id == WebAutofillClient::MenuItemIDAutofillOptions || | 38 if (unique_id == WebAutofillClient::MenuItemIDAutofillOptions || |
38 unique_id == WebAutofillClient::MenuItemIDClearForm || | 39 unique_id == WebAutofillClient::MenuItemIDClearForm || |
39 unique_id == WebAutofillClient::MenuItemIDSeparator || | 40 unique_id == WebAutofillClient::MenuItemIDSeparator || |
40 unique_id == WebAutofillClient::MenuItemIDWarningMessage) | 41 unique_id == WebAutofillClient::MenuItemIDWarningMessage) |
41 return; | 42 return; |
42 | 43 |
43 ClearPreviewedForm(); | 44 ClearPreviewedForm(); |
44 | 45 |
45 if (unique_id == WebAutofillClient::MenuItemIDPasswordEntry) | 46 if (unique_id == WebAutofillClient::MenuItemIDPasswordEntry) |
46 return; | 47 return; |
47 | 48 |
48 FillAutofillFormData(unique_id, true); | 49 // Only preview the data if it is a profile. |
50 if (unique_id > 0) | |
51 FillAutofillFormData(unique_id, true); | |
49 } | 52 } |
50 | 53 |
51 void AutofillExternalDelegate::OnQuery(int query_id, | 54 void AutofillExternalDelegate::OnQuery(int query_id, |
52 const webkit::forms::FormData& form, | 55 const webkit::forms::FormData& form, |
53 const webkit::forms::FormField& field, | 56 const webkit::forms::FormField& field, |
54 const gfx::Rect& bounds, | 57 const gfx::Rect& bounds, |
55 bool display_warning_if_disabled) { | 58 bool display_warning_if_disabled) { |
56 autofill_query_form_ = form; | 59 autofill_query_form_ = form; |
57 autofill_query_field_ = field; | 60 autofill_query_field_ = field; |
58 display_warning_if_disabled_ = display_warning_if_disabled; | 61 display_warning_if_disabled_ = display_warning_if_disabled; |
59 autofill_query_id_ = query_id; | 62 autofill_query_id_ = query_id; |
60 | 63 |
61 OnQueryPlatformSpecific(query_id, form, field, bounds); | 64 OnQueryPlatformSpecific(query_id, form, field, bounds); |
62 } | 65 } |
63 | 66 |
64 void AutofillExternalDelegate::OnSuggestionsReturned( | 67 void AutofillExternalDelegate::OnSuggestionsReturned( |
65 int query_id, | 68 int query_id, |
66 const std::vector<string16>& values, | 69 const std::vector<string16>& values, |
67 const std::vector<string16>& labels, | 70 const std::vector<string16>& labels, |
68 const std::vector<string16>& icons, | 71 const std::vector<string16>& icons, |
69 const std::vector<int>& unique_ids) { | 72 const std::vector<int>& unique_ids) { |
70 if (query_id != autofill_query_id_) | 73 if (query_id != autofill_query_id_) |
71 return; | 74 return; |
72 | 75 |
73 if (values.empty()) { | 76 // List any data list values at the start. |
Ilya Sherman
2012/06/01 06:28:16
nit: Remove this comment?
csharp
2012/06/04 15:02:03
Done.
| |
74 // No suggestions, any popup currently showing is obsolete. | |
75 HideAutofillPopup(); | |
76 return; | |
77 } | |
78 | |
79 std::vector<string16> v(values); | 77 std::vector<string16> v(values); |
80 std::vector<string16> l(labels); | 78 std::vector<string16> l(labels); |
81 std::vector<string16> i(icons); | 79 std::vector<string16> i(icons); |
82 std::vector<int> ids(unique_ids); | 80 std::vector<int> ids(unique_ids); |
83 | 81 |
84 // Add a separator to go between the values and menu items. | 82 // Add a separator to go between the values and menu items. |
85 v.push_back(string16()); | 83 v.push_back(string16()); |
86 l.push_back(string16()); | 84 l.push_back(string16()); |
87 i.push_back(string16()); | 85 i.push_back(string16()); |
88 ids.push_back(WebAutofillClient::MenuItemIDSeparator); | 86 ids.push_back(WebAutofillClient::MenuItemIDSeparator); |
89 | 87 |
90 DCHECK_GT(ids.size(), 0U); | 88 DCHECK_GT(ids.size(), 0U); |
91 if (!autofill_query_field_.should_autocomplete) { | 89 if (!autofill_query_field_.should_autocomplete) { |
92 // If autofill is disabled and we had suggestions, show a warning instead. | 90 // If autofill is disabled and we had suggestions, show a warning instead. |
93 v.assign(1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)); | 91 v.assign(1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)); |
94 l.assign(1, string16()); | 92 l.assign(1, string16()); |
95 i.assign(1, string16()); | 93 i.assign(1, string16()); |
96 ids.assign(1, WebAutofillClient::MenuItemIDWarningMessage); | 94 ids.assign(1, WebAutofillClient::MenuItemIDWarningMessage); |
97 } else if (ids[0] < 0 && ids.size() > 1) { | 95 } else if (ids[0] == WebAutofillClient::MenuItemIDWarningMessage && |
96 ids.size() > 1) { | |
98 // If we received a warning instead of suggestions from autofill but regular | 97 // If we received a warning instead of suggestions from autofill but regular |
99 // suggestions from autocomplete, don't show the autofill warning. | 98 // suggestions from autocomplete, don't show the autofill warning. |
100 v.erase(v.begin()); | 99 v.erase(v.begin()); |
101 l.erase(l.begin()); | 100 l.erase(l.begin()); |
102 i.erase(i.begin()); | 101 i.erase(i.begin()); |
103 ids.erase(ids.begin()); | 102 ids.erase(ids.begin()); |
104 } | 103 } |
105 | 104 |
106 // If we were about to show a warning and we shouldn't, don't. | 105 // If we were about to show a warning and we shouldn't, don't. |
107 if (ids[0] < 0 && !display_warning_if_disabled_) | 106 if (ids[0] == WebAutofillClient::MenuItemIDWarningMessage && |
108 return; | 107 !display_warning_if_disabled_) { |
108 v.clear(); | |
109 l.clear(); | |
110 i.clear(); | |
111 ids.clear(); | |
112 } | |
109 | 113 |
110 // Only include "Autofill Options" special menu item if we have Autofill | 114 // Only include "Autofill Options" special menu item if we have Autofill |
111 // items, identified by |unique_ids| having at least one valid value. | 115 // items, identified by |unique_ids| having at least one valid value. |
112 bool has_autofill_item = false; | 116 bool has_autofill_item = false; |
113 for (size_t i = 0; i < ids.size(); ++i) { | 117 for (size_t i = 0; i < ids.size(); ++i) { |
114 if (ids[i] > 0) { | 118 if (ids[i] > 0) { |
115 has_autofill_item = true; | 119 has_autofill_item = true; |
116 break; | 120 break; |
117 } | 121 } |
118 } | 122 } |
(...skipping 16 matching lines...) Expand all Loading... | |
135 } | 139 } |
136 | 140 |
137 // Remove the separator if it is the last element. | 141 // Remove the separator if it is the last element. |
138 if (*(ids.rbegin()) == WebAutofillClient::MenuItemIDSeparator) { | 142 if (*(ids.rbegin()) == WebAutofillClient::MenuItemIDSeparator) { |
139 v.pop_back(); | 143 v.pop_back(); |
140 l.pop_back(); | 144 l.pop_back(); |
141 i.pop_back(); | 145 i.pop_back(); |
142 ids.pop_back(); | 146 ids.pop_back(); |
143 } | 147 } |
144 | 148 |
149 // Insert the datalist elements, if any, at the start of the list. | |
150 if (!data_list_values_.empty()) { | |
151 // Insert the separator between the datalist and Autofill values. | |
Ilya Sherman
2012/06/01 06:28:16
Only if there are non-datalist items?
csharp
2012/06/04 15:02:03
Done.
| |
152 v.insert(v.begin(), string16()); | |
153 l.insert(l.begin(), string16()); | |
154 i.insert(i.begin(), string16()); | |
155 ids.insert(ids.begin(), WebAutofillClient::MenuItemIDSeparator); | |
156 | |
157 // Insert the datalist elements. | |
158 v.insert(v.begin(), data_list_values_.begin(), data_list_values_.end()); | |
159 l.insert(l.begin(), data_list_labels_.begin(), data_list_labels_.end()); | |
160 i.insert(i.begin(), data_list_icons_.begin(), data_list_icons_.end()); | |
161 ids.insert(ids.begin(), | |
162 data_list_unique_ids_.begin(), | |
163 data_list_unique_ids_.end()); | |
164 } | |
165 | |
166 if (v.empty()) { | |
167 // No suggestions, any popup currently showing is obsolete. | |
168 HideAutofillPopup(); | |
169 return; | |
170 } | |
171 | |
145 // Send to display. | 172 // Send to display. |
146 if (!v.empty() && autofill_query_field_.is_focusable) | 173 if (!v.empty() && autofill_query_field_.is_focusable) { |
174 popup_visible_ = true; | |
147 ApplyAutofillSuggestions(v, l, i, ids); | 175 ApplyAutofillSuggestions(v, l, i, ids); |
176 } | |
148 | 177 |
149 tab_contents_wrapper_->autofill_manager()->OnDidShowAutofillSuggestions( | 178 tab_contents_wrapper_->autofill_manager()->OnDidShowAutofillSuggestions( |
150 has_autofill_item && !has_shown_autofill_popup_for_current_edit_); | 179 has_autofill_item && !has_shown_autofill_popup_for_current_edit_); |
151 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item; | 180 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item; |
152 } | 181 } |
Ilya Sherman
2012/06/01 06:28:16
This method is getting pretty long... would be nic
csharp
2012/06/04 15:02:03
Done.
| |
153 | 182 |
154 void AutofillExternalDelegate::OnShowPasswordSuggestions( | 183 void AutofillExternalDelegate::OnShowPasswordSuggestions( |
155 const std::vector<string16>& suggestions, | 184 const std::vector<string16>& suggestions, |
156 const webkit::forms::FormField& field, | 185 const webkit::forms::FormField& field, |
157 const gfx::Rect& bounds) { | 186 const gfx::Rect& bounds) { |
158 autofill_query_field_ = field; | 187 autofill_query_field_ = field; |
159 | 188 |
160 if (suggestions.empty()) { | 189 if (suggestions.empty()) { |
161 HideAutofillPopup(); | 190 HideAutofillPopup(); |
162 return; | 191 return; |
163 } | 192 } |
164 | 193 |
165 SetBounds(bounds); | 194 SetBounds(bounds); |
166 | 195 |
167 std::vector<string16> empty(suggestions.size()); | 196 std::vector<string16> empty(suggestions.size()); |
168 std::vector<int> password_ids(suggestions.size(), | 197 std::vector<int> password_ids(suggestions.size(), |
169 WebAutofillClient::MenuItemIDPasswordEntry); | 198 WebAutofillClient::MenuItemIDPasswordEntry); |
170 ApplyAutofillSuggestions(suggestions, empty, empty, password_ids); | 199 ApplyAutofillSuggestions(suggestions, empty, empty, password_ids); |
171 } | 200 } |
172 | 201 |
202 void AutofillExternalDelegate::SetCurrentDataListValues( | |
203 const std::vector<string16>& data_list_values, | |
204 const std::vector<string16>& data_list_labels, | |
205 const std::vector<string16>& data_list_icons, | |
206 const std::vector<int>& data_list_unique_ids) { | |
207 DCHECK(!popup_visible_); | |
208 | |
209 data_list_values_ = data_list_values; | |
210 data_list_labels_ = data_list_labels; | |
211 data_list_icons_ = data_list_icons; | |
212 data_list_unique_ids_ = data_list_unique_ids; | |
213 } | |
214 | |
173 void AutofillExternalDelegate::RemoveAutocompleteEntry(const string16& value) { | 215 void AutofillExternalDelegate::RemoveAutocompleteEntry(const string16& value) { |
174 if (tab_contents_wrapper_) { | 216 if (tab_contents_wrapper_) { |
175 tab_contents_wrapper_->autocomplete_history_manager()-> | 217 tab_contents_wrapper_->autocomplete_history_manager()-> |
176 OnRemoveAutocompleteEntry(autofill_query_field_.name, value); | 218 OnRemoveAutocompleteEntry(autofill_query_field_.name, value); |
177 } | 219 } |
178 } | 220 } |
179 | 221 |
180 void AutofillExternalDelegate::RemoveAutofillProfileOrCreditCard( | 222 void AutofillExternalDelegate::RemoveAutofillProfileOrCreditCard( |
181 int unique_id) { | 223 int unique_id) { |
182 autofill_manager_->RemoveAutofillProfileOrCreditCard(unique_id); | 224 autofill_manager_->RemoveAutofillProfileOrCreditCard(unique_id); |
183 } | 225 } |
184 | 226 |
185 | 227 |
186 void AutofillExternalDelegate::DidEndTextFieldEditing() { | 228 void AutofillExternalDelegate::DidEndTextFieldEditing() { |
187 HideAutofillPopup(); | 229 HideAutofillPopup(); |
188 | 230 |
189 has_shown_autofill_popup_for_current_edit_ = false; | 231 has_shown_autofill_popup_for_current_edit_ = false; |
190 } | 232 } |
191 | 233 |
192 bool AutofillExternalDelegate::DidAcceptAutofillSuggestions( | 234 bool AutofillExternalDelegate::DidAcceptAutofillSuggestions( |
193 const string16& value, | 235 const string16& value, |
194 int unique_id, | 236 int unique_id, |
195 unsigned index) { | 237 unsigned index) { |
196 // If the selected element is a warning we don't want to do anything. | 238 // If the selected element is a warning we don't want to do anything. |
197 if (unique_id == WebAutofillClient::MenuItemIDWarningMessage) | 239 if (unique_id == WebAutofillClient::MenuItemIDWarningMessage) |
198 return false; | 240 return false; |
199 | 241 |
242 RenderViewHost* host = | |
243 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); | |
244 | |
200 if (unique_id == WebAutofillClient::MenuItemIDAutofillOptions) { | 245 if (unique_id == WebAutofillClient::MenuItemIDAutofillOptions) { |
201 // User selected 'Autofill Options'. | 246 // User selected 'Autofill Options'. |
202 autofill_manager_->OnShowAutofillDialog(); | 247 autofill_manager_->OnShowAutofillDialog(); |
203 } else if (unique_id == WebAutofillClient::MenuItemIDClearForm) { | 248 } else if (unique_id == WebAutofillClient::MenuItemIDClearForm) { |
204 // User selected 'Clear form'. | 249 // User selected 'Clear form'. |
205 RenderViewHost* host = | |
206 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); | |
207 host->Send(new AutofillMsg_ClearForm(host->GetRoutingID())); | 250 host->Send(new AutofillMsg_ClearForm(host->GetRoutingID())); |
208 } else if (unique_id == WebAutofillClient::MenuItemIDPasswordEntry && | 251 } else if (unique_id == WebAutofillClient::MenuItemIDPasswordEntry && |
209 password_autofill_manager_.DidAcceptAutofillSuggestion( | 252 password_autofill_manager_.DidAcceptAutofillSuggestion( |
210 autofill_query_field_, value)) { | 253 autofill_query_field_, value)) { |
211 // DidAcceptAutofillSuggestion has already handled the work to fill in | 254 // DidAcceptAutofillSuggestion has already handled the work to fill in |
212 // the page as required. | 255 // the page as required. |
256 } else if (unique_id == WebAutofillClient::MenuItemIDDataListEntry) { | |
257 host->Send(new AutofillMsg_AcceptDataListSuggestion(host->GetRoutingID(), | |
258 value)); | |
213 } else if (unique_id == WebAutofillClient::MenuItemIDAutocompleteEntry) { | 259 } else if (unique_id == WebAutofillClient::MenuItemIDAutocompleteEntry) { |
214 // User selected an Autocomplete, so we fill directly. | 260 // User selected an Autocomplete, so we fill directly. |
215 RenderViewHost* host = | |
216 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); | |
217 host->Send(new AutofillMsg_SetNodeText( | 261 host->Send(new AutofillMsg_SetNodeText( |
218 host->GetRoutingID(), | 262 host->GetRoutingID(), |
219 value)); | 263 value)); |
220 } else { | 264 } else { |
221 FillAutofillFormData(unique_id, false); | 265 FillAutofillFormData(unique_id, false); |
222 } | 266 } |
223 | 267 |
224 HideAutofillPopup(); | 268 HideAutofillPopup(); |
225 | 269 |
226 return true; | 270 return true; |
227 } | 271 } |
228 | 272 |
229 void AutofillExternalDelegate::ClearPreviewedForm() { | 273 void AutofillExternalDelegate::ClearPreviewedForm() { |
230 RenderViewHost* host = | 274 RenderViewHost* host = |
231 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); | 275 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); |
232 host->Send(new AutofillMsg_ClearPreviewedForm(host->GetRoutingID())); | 276 host->Send(new AutofillMsg_ClearPreviewedForm(host->GetRoutingID())); |
233 } | 277 } |
234 | 278 |
235 void AutofillExternalDelegate::HideAutofillPopup() { | 279 void AutofillExternalDelegate::HideAutofillPopup() { |
280 popup_visible_ = false; | |
281 | |
236 HideAutofillPopupInternal(); | 282 HideAutofillPopupInternal(); |
237 } | 283 } |
238 | 284 |
239 void AutofillExternalDelegate::Reset() { | 285 void AutofillExternalDelegate::Reset() { |
240 HideAutofillPopup(); | 286 HideAutofillPopup(); |
241 | 287 |
242 password_autofill_manager_.Reset(); | 288 password_autofill_manager_.Reset(); |
243 } | 289 } |
244 | 290 |
245 void AutofillExternalDelegate::AddPasswordFormMapping( | 291 void AutofillExternalDelegate::AddPasswordFormMapping( |
(...skipping 27 matching lines...) Expand all Loading... | |
273 // none, so all platforms use the default. | 319 // none, so all platforms use the default. |
274 | 320 |
275 #if !defined(OS_ANDROID) && !defined(TOOLKIT_GTK) | 321 #if !defined(OS_ANDROID) && !defined(TOOLKIT_GTK) |
276 | 322 |
277 AutofillExternalDelegate* AutofillExternalDelegate::Create( | 323 AutofillExternalDelegate* AutofillExternalDelegate::Create( |
278 TabContentsWrapper*, AutofillManager*) { | 324 TabContentsWrapper*, AutofillManager*) { |
279 return NULL; | 325 return NULL; |
280 } | 326 } |
281 | 327 |
282 #endif | 328 #endif |
OLD | NEW |