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

Side by Side Diff: chrome/renderer/autofill/autofill_agent.cc

Issue 10024059: DataList UI (Chromium part) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed nits and tests Created 8 years, 8 months 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) 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 "chrome/renderer/autofill/autofill_agent.h" 5 #include "chrome/renderer/autofill/autofill_agent.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/time.h" 9 #include "base/time.h"
10 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
11 #include "chrome/common/autofill_messages.h" 11 #include "chrome/common/autofill_messages.h"
12 #include "chrome/common/chrome_constants.h" 12 #include "chrome/common/chrome_constants.h"
13 #include "chrome/renderer/autofill/form_autofill_util.h" 13 #include "chrome/renderer/autofill/form_autofill_util.h"
14 #include "chrome/renderer/autofill/password_autofill_manager.h" 14 #include "chrome/renderer/autofill/password_autofill_manager.h"
15 #include "content/public/renderer/render_view.h" 15 #include "content/public/renderer/render_view.h"
16 #include "grit/chromium_strings.h" 16 #include "grit/chromium_strings.h"
17 #include "grit/generated_resources.h" 17 #include "grit/generated_resources.h"
18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h"
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormControlElement .h" 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormControlElement .h"
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h"
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeCollection.h"
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebOptionElement.h"
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
24 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h"
25 #include "ui/base/keycodes/keyboard_codes.h" 27 #include "ui/base/keycodes/keyboard_codes.h"
26 #include "ui/base/l10n/l10n_util.h" 28 #include "ui/base/l10n/l10n_util.h"
27 #include "webkit/forms/form_data.h" 29 #include "webkit/forms/form_data.h"
28 #include "webkit/forms/form_data_predictions.h" 30 #include "webkit/forms/form_data_predictions.h"
29 #include "webkit/forms/form_field.h" 31 #include "webkit/forms/form_field.h"
30 #include "webkit/forms/password_form.h" 32 #include "webkit/forms/password_form.h"
31 33
34 using WebKit::WebAutofillClient;
32 using WebKit::WebFormControlElement; 35 using WebKit::WebFormControlElement;
33 using WebKit::WebFormElement; 36 using WebKit::WebFormElement;
34 using WebKit::WebFrame; 37 using WebKit::WebFrame;
35 using WebKit::WebInputElement; 38 using WebKit::WebInputElement;
36 using WebKit::WebKeyboardEvent; 39 using WebKit::WebKeyboardEvent;
37 using WebKit::WebNode; 40 using WebKit::WebNode;
41 using WebKit::WebNodeCollection;
42 using WebKit::WebOptionElement;
38 using WebKit::WebString; 43 using WebKit::WebString;
39 using webkit::forms::FormData; 44 using webkit::forms::FormData;
40 using webkit::forms::FormDataPredictions; 45 using webkit::forms::FormDataPredictions;
41 46
42 namespace { 47 namespace {
43 48
44 // The size above which we stop triggering autofill for an input text field 49 // The size above which we stop triggering autofill for an input text field
45 // (so to avoid sending long strings through IPC). 50 // (so to avoid sending long strings through IPC).
46 const size_t kMaximumTextSizeForAutofill = 1000; 51 const size_t kMaximumTextSizeForAutofill = 1000;
47 52
53 void AppendDataListSuggestions(const WebKit::WebInputElement& element,
54 std::vector<string16>* values,
55 std::vector<string16>* labels,
56 std::vector<string16>* icons,
57 std::vector<int>* item_ids) {
58 WebNodeCollection options = element.dataListOptions();
59 if (options.isNull())
60 return;
61
62 for (WebOptionElement option = options.firstItem().to<WebOptionElement>();
63 !option.isNull(); option = options.nextItem().to<WebOptionElement>()) {
64 if (!StartsWith(option.value(), element.value(), false))
65 continue;
66
67 values->push_back(option.value());
68 if (option.value() != option.label())
69 labels->push_back(option.label());
70 else
71 labels->push_back(string16());
72 icons->push_back(string16());
73 item_ids->push_back(WebAutofillClient::MenuItemIDDataListEntry);
74 }
75 }
76
48 } // namespace 77 } // namespace
49 78
50 namespace autofill { 79 namespace autofill {
51 80
52 AutofillAgent::AutofillAgent( 81 AutofillAgent::AutofillAgent(
53 content::RenderView* render_view, 82 content::RenderView* render_view,
54 PasswordAutofillManager* password_autofill_manager) 83 PasswordAutofillManager* password_autofill_manager)
55 : content::RenderViewObserver(render_view), 84 : content::RenderViewObserver(render_view),
56 password_autofill_manager_(password_autofill_manager), 85 password_autofill_manager_(password_autofill_manager),
57 autofill_query_id_(0), 86 autofill_query_id_(0),
58 autofill_action_(AUTOFILL_NONE), 87 autofill_action_(AUTOFILL_NONE),
59 display_warning_if_disabled_(false), 88 display_warning_if_disabled_(false),
60 was_query_node_autofilled_(false), 89 was_query_node_autofilled_(false),
61 suggestions_clear_index_(-1),
62 suggestions_options_index_(-1),
63 has_shown_autofill_popup_for_current_edit_(false), 90 has_shown_autofill_popup_for_current_edit_(false),
64 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { 91 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
65 render_view->GetWebView()->setAutofillClient(this); 92 render_view->GetWebView()->setAutofillClient(this);
66 } 93 }
67 94
68 AutofillAgent::~AutofillAgent() {} 95 AutofillAgent::~AutofillAgent() {}
69 96
70 bool AutofillAgent::OnMessageReceived(const IPC::Message& message) { 97 bool AutofillAgent::OnMessageReceived(const IPC::Message& message) {
71 bool handled = true; 98 bool handled = true;
72 IPC_BEGIN_MESSAGE_MAP(AutofillAgent, message) 99 IPC_BEGIN_MESSAGE_MAP(AutofillAgent, message)
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 166
140 bool AutofillAgent::InputElementLostFocus() { 167 bool AutofillAgent::InputElementLostFocus() {
141 Send(new AutofillHostMsg_HideAutofillPopup(routing_id())); 168 Send(new AutofillHostMsg_HideAutofillPopup(routing_id()));
142 169
143 return false; 170 return false;
144 } 171 }
145 172
146 void AutofillAgent::didAcceptAutofillSuggestion(const WebNode& node, 173 void AutofillAgent::didAcceptAutofillSuggestion(const WebNode& node,
147 const WebString& value, 174 const WebString& value,
148 const WebString& label, 175 const WebString& label,
149 int unique_id, 176 int item_id,
150 unsigned index) { 177 unsigned index) {
151 if (password_autofill_manager_->DidAcceptAutofillSuggestion(node, value)) 178 if (password_autofill_manager_->DidAcceptAutofillSuggestion(node, value))
152 return; 179 return;
153 180
154 DCHECK(node == autofill_query_element_); 181 DCHECK(node == element_);
155 182
156 if (suggestions_options_index_ != -1 && 183 switch (item_id) {
157 index == static_cast<unsigned>(suggestions_options_index_)) { 184 case WebAutofillClient::MenuItemIDWarningMessage:
158 // User selected 'Autofill Options'. 185 case WebAutofillClient::MenuItemIDSeparator:
159 Send(new AutofillHostMsg_ShowAutofillDialog(routing_id())); 186 NOTREACHED();
160 } else if (suggestions_clear_index_ != -1 && 187 break;
161 index == static_cast<unsigned>(suggestions_clear_index_)) { 188 case WebAutofillClient::MenuItemIDAutofillOptions:
162 // User selected 'Clear form'. 189 // User selected 'Autofill Options'.
163 form_cache_.ClearFormWithElement(autofill_query_element_); 190 Send(new AutofillHostMsg_ShowAutofillDialog(routing_id()));
164 } else if (!unique_id) { 191 break;
165 // User selected an Autocomplete entry, so we fill directly. 192 case WebAutofillClient::MenuItemIDClearForm:
166 WebInputElement element = node.toConst<WebInputElement>(); 193 // User selected 'Clear form'.
167 SetNodeText(value, &element); 194 form_cache_.ClearFormWithElement(element_);
168 } else { 195 break;
169 // Fill the values for the whole form. 196 case WebAutofillClient::MenuItemIDAutocompleteEntry:
170 FillAutofillFormData(node, unique_id, AUTOFILL_FILL); 197 case WebAutofillClient::MenuItemIDPasswordEntry:
198 case WebAutofillClient::MenuItemIDDataListEntry:
199 // User selected an Autocomplete or password or datalist entry, so we
200 // fill directly.
201 SetNodeText(value, &element_);
202 break;
203 default:
204 // A positive item_id is a unique id for an autofill (vs. autocomplete)
205 // suggestion.
206 DCHECK_GT(item_id, 0);
207 // Fill the values for the whole form.
208 FillAutofillFormData(node, item_id, AUTOFILL_FILL);
171 } 209 }
172
173 suggestions_clear_index_ = -1;
174 suggestions_options_index_ = -1;
175 } 210 }
176 211
177 void AutofillAgent::didSelectAutofillSuggestion(const WebNode& node, 212 void AutofillAgent::didSelectAutofillSuggestion(const WebNode& node,
178 const WebString& value, 213 const WebString& value,
179 const WebString& label, 214 const WebString& label,
180 int unique_id) { 215 int item_id) {
181 DCHECK_GE(unique_id, 0);
182 if (password_autofill_manager_->DidSelectAutofillSuggestion(node)) 216 if (password_autofill_manager_->DidSelectAutofillSuggestion(node))
183 return; 217 return;
184 218
185 didClearAutofillSelection(node); 219 didClearAutofillSelection(node);
186 FillAutofillFormData(node, unique_id, AUTOFILL_PREVIEW); 220
221 if (item_id > 0)
222 FillAutofillFormData(node, item_id, AUTOFILL_PREVIEW);
187 } 223 }
188 224
189 void AutofillAgent::didClearAutofillSelection(const WebNode& node) { 225 void AutofillAgent::didClearAutofillSelection(const WebNode& node) {
190 if (password_autofill_manager_->DidClearAutofillSelection(node)) 226 if (password_autofill_manager_->DidClearAutofillSelection(node))
191 return; 227 return;
192 228
193 if (!autofill_query_element_.isNull() && node == autofill_query_element_) { 229 if (!element_.isNull() && node == element_) {
194 ClearPreviewedFormWithElement(autofill_query_element_, 230 ClearPreviewedFormWithElement(element_, was_query_node_autofilled_);
195 was_query_node_autofilled_);
196 } else { 231 } else {
197 // TODO(isherman): There seem to be rare cases where this code *is* 232 // TODO(isherman): There seem to be rare cases where this code *is*
198 // reachable: see [ http://crbug.com/96321#c6 ]. Ideally we would 233 // reachable: see [ http://crbug.com/96321#c6 ]. Ideally we would
199 // understand those cases and fix the code to avoid them. However, so far I 234 // understand those cases and fix the code to avoid them. However, so far I
200 // have been unable to reproduce such a case locally. If you hit this 235 // have been unable to reproduce such a case locally. If you hit this
201 // NOTREACHED(), please file a bug against me. 236 // NOTREACHED(), please file a bug against me.
202 NOTREACHED(); 237 NOTREACHED();
203 } 238 }
204 } 239 }
205 240
206 void AutofillAgent::removeAutocompleteSuggestion(const WebString& name, 241 void AutofillAgent::removeAutocompleteSuggestion(const WebString& name,
207 const WebString& value) { 242 const WebString& value) {
208 // The index of clear & options will have shifted down.
209 if (suggestions_clear_index_ != -1)
210 suggestions_clear_index_--;
211 if (suggestions_options_index_ != -1)
212 suggestions_options_index_--;
213
214 Send(new AutofillHostMsg_RemoveAutocompleteEntry(routing_id(), name, value)); 243 Send(new AutofillHostMsg_RemoveAutocompleteEntry(routing_id(), name, value));
215 } 244 }
216 245
217 void AutofillAgent::textFieldDidEndEditing(const WebInputElement& element) { 246 void AutofillAgent::textFieldDidEndEditing(const WebInputElement& element) {
218 password_autofill_manager_->TextFieldDidEndEditing(element); 247 password_autofill_manager_->TextFieldDidEndEditing(element);
219 has_shown_autofill_popup_for_current_edit_ = false; 248 has_shown_autofill_popup_for_current_edit_ = false;
220 Send(new AutofillHostMsg_DidEndTextFieldEditing(routing_id())); 249 Send(new AutofillHostMsg_DidEndTextFieldEditing(routing_id()));
221 } 250 }
222 251
223 void AutofillAgent::textFieldDidChange(const WebInputElement& element) { 252 void AutofillAgent::textFieldDidChange(const WebInputElement& element) {
224 // We post a task for doing the Autofill as the caret position is not set 253 // We post a task for doing the Autofill as the caret position is not set
225 // properly at this point (http://bugs.webkit.org/show_bug.cgi?id=16976) and 254 // properly at this point (http://bugs.webkit.org/show_bug.cgi?id=16976) and
226 // it is needed to trigger autofill. 255 // it is needed to trigger autofill.
227 weak_ptr_factory_.InvalidateWeakPtrs(); 256 weak_ptr_factory_.InvalidateWeakPtrs();
228 MessageLoop::current()->PostTask( 257 MessageLoop::current()->PostTask(
229 FROM_HERE, 258 FROM_HERE,
230 base::Bind(&AutofillAgent::TextFieldDidChangeImpl, 259 base::Bind(&AutofillAgent::TextFieldDidChangeImpl,
231 weak_ptr_factory_.GetWeakPtr(), element)); 260 weak_ptr_factory_.GetWeakPtr(), element));
232 } 261 }
233 262
234 void AutofillAgent::TextFieldDidChangeImpl(const WebInputElement& element) { 263 void AutofillAgent::TextFieldDidChangeImpl(const WebInputElement& element) {
235 if (password_autofill_manager_->TextDidChangeInTextField(element)) { 264 if (password_autofill_manager_->TextDidChangeInTextField(element)) {
236 autofill_query_element_ = element; 265 element_ = element;
237 return; 266 return;
238 } 267 }
239 268
240 ShowSuggestions(element, false, true, false); 269 ShowSuggestions(element, false, true, false);
241 270
242 webkit::forms::FormData form; 271 webkit::forms::FormData form;
243 webkit::forms::FormField field; 272 webkit::forms::FormField field;
244 if (FindFormAndFieldForInputElement(element, &form, &field, REQUIRE_NONE)) { 273 if (FindFormAndFieldForInputElement(element, &form, &field, REQUIRE_NONE)) {
245 Send(new AutofillHostMsg_TextFieldDidChange(routing_id(), form, field, 274 Send(new AutofillHostMsg_TextFieldDidChange(routing_id(), form, field,
246 base::TimeTicks::Now())); 275 base::TimeTicks::Now()));
247 } 276 }
248 } 277 }
249 278
250 void AutofillAgent::textFieldDidReceiveKeyDown(const WebInputElement& element, 279 void AutofillAgent::textFieldDidReceiveKeyDown(const WebInputElement& element,
251 const WebKeyboardEvent& event) { 280 const WebKeyboardEvent& event) {
252 if (password_autofill_manager_->TextFieldHandlingKeyDown(element, event)) { 281 if (password_autofill_manager_->TextFieldHandlingKeyDown(element, event)) {
253 autofill_query_element_ = element; 282 element_ = element;
254 return; 283 return;
255 } 284 }
256 285
257 if (event.windowsKeyCode == ui::VKEY_DOWN || 286 if (event.windowsKeyCode == ui::VKEY_DOWN ||
258 event.windowsKeyCode == ui::VKEY_UP) 287 event.windowsKeyCode == ui::VKEY_UP)
259 ShowSuggestions(element, true, true, true); 288 ShowSuggestions(element, true, true, true);
260 } 289 }
261 290
262 void AutofillAgent::OnSuggestionsReturned(int query_id, 291 void AutofillAgent::OnSuggestionsReturned(int query_id,
263 const std::vector<string16>& values, 292 const std::vector<string16>& values,
264 const std::vector<string16>& labels, 293 const std::vector<string16>& labels,
265 const std::vector<string16>& icons, 294 const std::vector<string16>& icons,
266 const std::vector<int>& unique_ids) { 295 const std::vector<int>& unique_ids) {
267 WebKit::WebView* web_view = render_view()->GetWebView(); 296 if (query_id != autofill_query_id_)
268 if (!web_view || query_id != autofill_query_id_)
269 return; 297 return;
270 298
271 if (values.empty()) { 299 if (element_.isNull() || !element_.isFocusable())
272 // No suggestions, any popup currently showing is obsolete.
273 web_view->hidePopups();
274 return; 300 return;
275 }
276 301
277 std::vector<string16> v(values); 302 std::vector<string16> v(values);
278 std::vector<string16> l(labels); 303 std::vector<string16> l(labels);
279 std::vector<string16> i(icons); 304 std::vector<string16> i(icons);
280 std::vector<int> ids(unique_ids); 305 std::vector<int> ids(unique_ids);
281 int separator_index = -1;
282 306
283 DCHECK_GT(ids.size(), 0U); 307 if (!element_.autoComplete() && !v.empty()) {
284 if (!autofill_query_element_.isNull() &&
285 !autofill_query_element_.autoComplete()) {
286 // If autofill is disabled and we had suggestions, show a warning instead. 308 // If autofill is disabled and we had suggestions, show a warning instead.
287 v.assign(1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)); 309 v.assign(1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED));
288 l.assign(1, string16()); 310 l.assign(1, string16());
289 i.assign(1, string16()); 311 i.assign(1, string16());
290 ids.assign(1, -1); 312 ids.assign(1, WebAutofillClient::MenuItemIDWarningMessage);
291 } else if (ids[0] < 0 && ids.size() > 1) { 313 } else if (ids.size() > 1 &&
292 // If we received a warning instead of suggestions from autofill but regular 314 ids[0] == WebAutofillClient::MenuItemIDWarningMessage) {
293 // suggestions from autocomplete, don't show the autofill warning. 315 // If we received an autofill warning plus some autocomplete suggestions,
316 // remove the autofill warning.
294 v.erase(v.begin()); 317 v.erase(v.begin());
295 l.erase(l.begin()); 318 l.erase(l.begin());
296 i.erase(i.begin()); 319 i.erase(i.begin());
297 ids.erase(ids.begin()); 320 ids.erase(ids.begin());
298 } 321 }
299 322
300 // If we were about to show a warning and we shouldn't, don't. 323 // If we were about to show a warning and we shouldn't, don't.
301 if (ids[0] < 0 && !display_warning_if_disabled_) 324 if (!display_warning_if_disabled_ && !v.empty() &&
302 return; 325 ids[0] == WebAutofillClient::MenuItemIDWarningMessage) {
326 v.clear();
327 l.clear();
328 i.clear();
329 ids.clear();
330 }
303 331
304 // Only include "Autofill Options" special menu item if we have Autofill 332 // Only include "Autofill Options" special menu item if we have Autofill
305 // items, identified by |unique_ids| having at least one valid value. 333 // items, identified by |unique_ids| having at least one valid value.
306 bool has_autofill_item = false; 334 bool has_autofill_item = false;
307 for (size_t i = 0; i < ids.size(); ++i) { 335 for (size_t i = 0; i < ids.size(); ++i) {
308 if (ids[i] > 0) { 336 if (ids[i] > 0) {
309 has_autofill_item = true; 337 has_autofill_item = true;
310 break; 338 break;
311 } 339 }
312 } 340 }
313 341
314 // The form has been auto-filled, so give the user the chance to clear the 342 if (has_autofill_item) {
315 // form. Append the 'Clear form' menu item. 343 v.push_back(string16());
316 if (has_autofill_item &&
317 FormWithElementIsAutofilled(autofill_query_element_)) {
318 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_CLEAR_FORM_MENU_ITEM));
319 l.push_back(string16()); 344 l.push_back(string16());
320 i.push_back(string16()); 345 i.push_back(string16());
321 ids.push_back(0); 346 ids.push_back(WebAutofillClient::MenuItemIDSeparator);
322 suggestions_clear_index_ = v.size() - 1;
323 separator_index = v.size() - 1;
324 }
325 347
326 if (has_autofill_item) { 348 if (FormWithElementIsAutofilled(element_)) {
349 // The form has been auto-filled, so give the user the chance to clear the
350 // form. Append the 'Clear form' menu item.
351 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_CLEAR_FORM_MENU_ITEM));
352 l.push_back(string16());
353 i.push_back(string16());
354 ids.push_back(WebAutofillClient::MenuItemIDClearForm);
355 }
356
327 // Append the 'Chrome Autofill options' menu item; 357 // Append the 'Chrome Autofill options' menu item;
328 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_OPTIONS_POPUP)); 358 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_OPTIONS_POPUP));
329 l.push_back(string16()); 359 l.push_back(string16());
330 i.push_back(string16()); 360 i.push_back(string16());
331 ids.push_back(0); 361 ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions);
332 suggestions_options_index_ = v.size() - 1; 362 }
333 separator_index = values.size(); 363
364 CombineDataListEntriesAndShow(element_, v, l, i, ids, has_autofill_item);
365 }
366
367 void AutofillAgent::CombineDataListEntriesAndShow(
368 const WebKit::WebInputElement& element,
369 const std::vector<string16>& values,
370 const std::vector<string16>& labels,
371 const std::vector<string16>& icons,
372 const std::vector<int>& item_ids,
373 bool has_autofill_item) {
374 std::vector<string16> v;
375 std::vector<string16> l;
376 std::vector<string16> i;
377 std::vector<int> ids;
378
379 AppendDataListSuggestions(element, &v, &l, &i, &ids);
380
381 // If there are both <datalist> items and Autofill suggestions, add a
382 // separator between them.
383 if (!v.empty() && !values.empty()) {
384 v.push_back(string16());
385 l.push_back(string16());
386 i.push_back(string16());
387 ids.push_back(WebAutofillClient::MenuItemIDSeparator);
388 }
389
390 // Append the Autofill suggestions.
391 v.insert(v.end(), values.begin(), values.end());
392 l.insert(l.end(), labels.begin(), labels.end());
393 i.insert(i.end(), icons.begin(), icons.end());
394 ids.insert(ids.end(), item_ids.begin(), item_ids.end());
395
396 WebKit::WebView* web_view = render_view()->GetWebView();
397 if (!web_view)
398 return;
399
400 if (v.empty()) {
401 // No suggestions, any popup currently showing is obsolete.
402 web_view->hidePopups();
403 return;
334 } 404 }
335 405
336 // Send to WebKit for display. 406 // Send to WebKit for display.
337 if (!v.empty() && !autofill_query_element_.isNull() && 407 web_view->applyAutofillSuggestions(element, v, l, i, ids);
338 autofill_query_element_.isFocusable()) {
339 web_view->applyAutofillSuggestions(
340 autofill_query_element_, v, l, i, ids, separator_index);
341 }
342 408
343 Send(new AutofillHostMsg_DidShowAutofillSuggestions( 409 Send(new AutofillHostMsg_DidShowAutofillSuggestions(
344 routing_id(), 410 routing_id(),
345 has_autofill_item && !has_shown_autofill_popup_for_current_edit_)); 411 has_autofill_item && !has_shown_autofill_popup_for_current_edit_));
346 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item; 412 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item;
347 } 413 }
348 414
349 void AutofillAgent::OnFormDataFilled(int query_id, 415 void AutofillAgent::OnFormDataFilled(int query_id,
350 const webkit::forms::FormData& form) { 416 const webkit::forms::FormData& form) {
351 if (!render_view()->GetWebView() || query_id != autofill_query_id_) 417 if (!render_view()->GetWebView() || query_id != autofill_query_id_)
352 return; 418 return;
353 419
354 was_query_node_autofilled_ = autofill_query_element_.isAutofilled(); 420 was_query_node_autofilled_ = element_.isAutofilled();
355 421
356 switch (autofill_action_) { 422 switch (autofill_action_) {
357 case AUTOFILL_FILL: 423 case AUTOFILL_FILL:
358 FillForm(form, autofill_query_element_); 424 FillForm(form, element_);
359 Send(new AutofillHostMsg_DidFillAutofillFormData(routing_id(), 425 Send(new AutofillHostMsg_DidFillAutofillFormData(routing_id(),
360 base::TimeTicks::Now())); 426 base::TimeTicks::Now()));
361 break; 427 break;
362 case AUTOFILL_PREVIEW: 428 case AUTOFILL_PREVIEW:
363 didClearAutofillSelection(autofill_query_element_); 429 didClearAutofillSelection(element_);
364 430
365 PreviewForm(form, autofill_query_element_); 431 PreviewForm(form, element_);
366 Send(new AutofillHostMsg_DidPreviewAutofillFormData(routing_id())); 432 Send(new AutofillHostMsg_DidPreviewAutofillFormData(routing_id()));
367 break; 433 break;
368 default: 434 default:
369 NOTREACHED(); 435 NOTREACHED();
370 } 436 }
371 autofill_action_ = AUTOFILL_NONE; 437 autofill_action_ = AUTOFILL_NONE;
372 } 438 }
373 439
374 void AutofillAgent::OnFieldTypePredictionsAvailable( 440 void AutofillAgent::OnFieldTypePredictionsAvailable(
375 const std::vector<FormDataPredictions>& forms) { 441 const std::vector<FormDataPredictions>& forms) {
376 for (size_t i = 0; i < forms.size(); ++i) { 442 for (size_t i = 0; i < forms.size(); ++i) {
377 form_cache_.ShowPredictions(forms[i]); 443 form_cache_.ShowPredictions(forms[i]);
378 } 444 }
379 } 445 }
380 446
381 void AutofillAgent::OnSelectAutofillSuggestionAtIndex(int listIndex) { 447 void AutofillAgent::OnSelectAutofillSuggestionAtIndex(int listIndex) {
382 NOTIMPLEMENTED(); 448 NOTIMPLEMENTED();
383 // TODO(jrg): enable once changes land in WebKit 449 // TODO(jrg): enable once changes land in WebKit
384 // render_view()->webview()->selectAutofillSuggestionAtIndex(listIndex); 450 // render_view()->webview()->selectAutofillSuggestionAtIndex(listIndex);
385 } 451 }
386 452
387 void AutofillAgent::OnSetAutofillActionFill() { 453 void AutofillAgent::OnSetAutofillActionFill() {
388 autofill_action_ = AUTOFILL_FILL; 454 autofill_action_ = AUTOFILL_FILL;
389 } 455 }
390 456
391 void AutofillAgent::OnClearForm() { 457 void AutofillAgent::OnClearForm() {
392 form_cache_.ClearFormWithElement(autofill_query_element_); 458 form_cache_.ClearFormWithElement(element_);
393 } 459 }
394 460
395 void AutofillAgent::OnSetAutofillActionPreview() { 461 void AutofillAgent::OnSetAutofillActionPreview() {
396 autofill_action_ = AUTOFILL_PREVIEW; 462 autofill_action_ = AUTOFILL_PREVIEW;
397 } 463 }
398 464
399 void AutofillAgent::OnClearPreviewedForm() { 465 void AutofillAgent::OnClearPreviewedForm() {
400 didClearAutofillSelection(autofill_query_element_); 466 didClearAutofillSelection(element_);
401 } 467 }
402 468
403 void AutofillAgent::OnSetNodeText(const string16& value) { 469 void AutofillAgent::OnSetNodeText(const string16& value) {
404 SetNodeText(value, &autofill_query_element_); 470 SetNodeText(value, &element_);
405 } 471 }
406 472
407 void AutofillAgent::OnAcceptPasswordAutofillSuggestion(const string16& value) { 473 void AutofillAgent::OnAcceptPasswordAutofillSuggestion(const string16& value) {
408 // We need to make sure this is handled here because the browser process 474 // We need to make sure this is handled here because the browser process
409 // skipped it handling because it believed it would be handled here. If it 475 // skipped it handling because it believed it would be handled here. If it
410 // isn't handled here then the browser logic needs to be updated. 476 // isn't handled here then the browser logic needs to be updated.
411 bool handled = password_autofill_manager_->DidAcceptAutofillSuggestion( 477 bool handled = password_autofill_manager_->DidAcceptAutofillSuggestion(
412 autofill_query_element_, 478 element_,
413 value); 479 value);
414 DCHECK(handled); 480 DCHECK(handled);
415 } 481 }
416 482
417 void AutofillAgent::ShowSuggestions(const WebInputElement& element, 483 void AutofillAgent::ShowSuggestions(const WebInputElement& element,
418 bool autofill_on_empty_values, 484 bool autofill_on_empty_values,
419 bool requires_caret_at_end, 485 bool requires_caret_at_end,
420 bool display_warning_if_disabled) { 486 bool display_warning_if_disabled) {
421 // If autocomplete is disabled at the form level, then we might want to show 487 if (!element.isEnabled() || element.isReadOnly() || !element.isTextField() ||
422 // a warning in place of suggestions. However, if autocomplete is disabled 488 element.isPasswordField() || !element.suggestedValue().isEmpty())
423 // specifically for this field, we never want to show a warning. Otherwise,
424 // we might interfere with custom popups (e.g. search suggestions) used by
425 // the website.
426 const WebFormElement form = element.form();
427 if (!element.isEnabled() || element.isReadOnly() ||
428 (!element.autoComplete() && (form.isNull() || form.autoComplete())) ||
429 !element.isTextField() || element.isPasswordField() ||
430 !element.suggestedValue().isEmpty())
431 return;
432
433 // If the field has no name, then we won't have values.
434 if (element.nameForAutofill().isEmpty())
435 return; 489 return;
436 490
437 // Don't attempt to autofill with values that are too large or if filling 491 // Don't attempt to autofill with values that are too large or if filling
438 // criteria are not met. 492 // criteria are not met.
439 WebString value = element.value(); 493 WebString value = element.value();
440 if (value.length() > kMaximumTextSizeForAutofill || 494 if (value.length() > kMaximumTextSizeForAutofill ||
441 (!autofill_on_empty_values && value.isEmpty()) || 495 (!autofill_on_empty_values && value.isEmpty()) ||
442 (requires_caret_at_end && 496 (requires_caret_at_end &&
443 (element.selectionStart() != element.selectionEnd() || 497 (element.selectionStart() != element.selectionEnd() ||
444 element.selectionEnd() != static_cast<int>(value.length())))) { 498 element.selectionEnd() != static_cast<int>(value.length())))) {
445 // Any popup currently showing is obsolete. 499 // Any popup currently showing is obsolete.
446 WebKit::WebView* web_view = render_view()->GetWebView(); 500 WebKit::WebView* web_view = render_view()->GetWebView();
447 if (web_view) 501 if (web_view)
448 web_view->hidePopups(); 502 web_view->hidePopups();
449 503
450 return; 504 return;
451 } 505 }
452 506
507 element_ = element;
508
509 // If autocomplete is disabled at the form level, then we might want to show
510 // a warning in place of suggestions. However, if autocomplete is disabled
511 // specifically for this field, we never want to show a warning. Otherwise,
512 // we might interfere with custom popups (e.g. search suggestions) used by
513 // the website. Also, if the field has no name, then we won't have values.
514 const WebFormElement form = element.form();
515 if ((!element.autoComplete() && (form.isNull() || form.autoComplete())) ||
516 element.nameForAutofill().isEmpty()) {
517 CombineDataListEntriesAndShow(element, std::vector<string16>(),
518 std::vector<string16>(),
519 std::vector<string16>(),
520 std::vector<int>(), false);
521 return;
522 }
523
453 QueryAutofillSuggestions(element, display_warning_if_disabled); 524 QueryAutofillSuggestions(element, display_warning_if_disabled);
454 } 525 }
455 526
456 void AutofillAgent::QueryAutofillSuggestions(const WebInputElement& element, 527 void AutofillAgent::QueryAutofillSuggestions(const WebInputElement& element,
457 bool display_warning_if_disabled) { 528 bool display_warning_if_disabled) {
458 static int query_counter = 0; 529 static int query_counter = 0;
459 autofill_query_id_ = query_counter++; 530 autofill_query_id_ = query_counter++;
460 autofill_query_element_ = element;
461 display_warning_if_disabled_ = display_warning_if_disabled; 531 display_warning_if_disabled_ = display_warning_if_disabled;
462 532
463 webkit::forms::FormData form; 533 webkit::forms::FormData form;
464 webkit::forms::FormField field; 534 webkit::forms::FormField field;
465 if (!FindFormAndFieldForInputElement(element, &form, &field, 535 if (!FindFormAndFieldForInputElement(element, &form, &field,
466 REQUIRE_AUTOCOMPLETE)) { 536 REQUIRE_AUTOCOMPLETE)) {
467 // If we didn't find the cached form, at least let autocomplete have a shot 537 // If we didn't find the cached form, at least let autocomplete have a shot
468 // at providing suggestions. 538 // at providing suggestions.
469 WebFormControlElementToFormField(element, EXTRACT_VALUE, &field); 539 WebFormControlElementToFormField(element, EXTRACT_VALUE, &field);
470 } 540 }
471 541
472 gfx::Rect bounding_box(autofill_query_element_.boundsInViewportSpace()); 542 gfx::Rect bounding_box(element_.boundsInViewportSpace());
473 543
474 Send(new AutofillHostMsg_QueryFormFieldAutofill(routing_id(), 544 Send(new AutofillHostMsg_QueryFormFieldAutofill(routing_id(),
475 autofill_query_id_, 545 autofill_query_id_,
476 form, 546 form,
477 field, 547 field,
478 bounding_box, 548 bounding_box,
479 display_warning_if_disabled)); 549 display_warning_if_disabled));
480 } 550 }
481 551
482 void AutofillAgent::FillAutofillFormData(const WebNode& node, 552 void AutofillAgent::FillAutofillFormData(const WebNode& node,
483 int unique_id, 553 int unique_id,
484 AutofillAction action) { 554 AutofillAction action) {
555 DCHECK_GT(unique_id, 0);
556
485 static int query_counter = 0; 557 static int query_counter = 0;
486 autofill_query_id_ = query_counter++; 558 autofill_query_id_ = query_counter++;
487 559
488 webkit::forms::FormData form; 560 webkit::forms::FormData form;
489 webkit::forms::FormField field; 561 webkit::forms::FormField field;
490 if (!FindFormAndFieldForInputElement(node.toConst<WebInputElement>(), &form, 562 if (!FindFormAndFieldForInputElement(node.toConst<WebInputElement>(), &form,
491 &field, REQUIRE_AUTOCOMPLETE)) { 563 &field, REQUIRE_AUTOCOMPLETE)) {
492 return; 564 return;
493 } 565 }
494 566
495 autofill_action_ = action; 567 autofill_action_ = action;
496 Send(new AutofillHostMsg_FillAutofillFormData( 568 Send(new AutofillHostMsg_FillAutofillFormData(
497 routing_id(), autofill_query_id_, form, field, unique_id)); 569 routing_id(), autofill_query_id_, form, field, unique_id));
498 } 570 }
499 571
500 void AutofillAgent::SetNodeText(const string16& value, 572 void AutofillAgent::SetNodeText(const string16& value,
501 WebKit::WebInputElement* node) { 573 WebKit::WebInputElement* node) {
502 string16 substring = value; 574 string16 substring = value;
503 substring = substring.substr(0, node->maxLength()); 575 substring = substring.substr(0, node->maxLength());
504 576
505 node->setValue(substring, true); 577 node->setValue(substring, true);
506 } 578 }
507 579
508 } // namespace autofill 580 } // namespace autofill
OLDNEW
« no previous file with comments | « chrome/renderer/autofill/autofill_agent.h ('k') | chrome/renderer/autofill/autofill_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698