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 "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" |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
69 | 69 |
70 bool AutofillAgent::OnMessageReceived(const IPC::Message& message) { | 70 bool AutofillAgent::OnMessageReceived(const IPC::Message& message) { |
71 bool handled = true; | 71 bool handled = true; |
72 IPC_BEGIN_MESSAGE_MAP(AutofillAgent, message) | 72 IPC_BEGIN_MESSAGE_MAP(AutofillAgent, message) |
73 IPC_MESSAGE_HANDLER(AutofillMsg_SuggestionsReturned, OnSuggestionsReturned) | 73 IPC_MESSAGE_HANDLER(AutofillMsg_SuggestionsReturned, OnSuggestionsReturned) |
74 IPC_MESSAGE_HANDLER(AutofillMsg_FormDataFilled, OnFormDataFilled) | 74 IPC_MESSAGE_HANDLER(AutofillMsg_FormDataFilled, OnFormDataFilled) |
75 IPC_MESSAGE_HANDLER(AutofillMsg_FieldTypePredictionsAvailable, | 75 IPC_MESSAGE_HANDLER(AutofillMsg_FieldTypePredictionsAvailable, |
76 OnFieldTypePredictionsAvailable) | 76 OnFieldTypePredictionsAvailable) |
77 IPC_MESSAGE_HANDLER(AutofillMsg_SelectAutofillSuggestionAtIndex, | 77 IPC_MESSAGE_HANDLER(AutofillMsg_SelectAutofillSuggestionAtIndex, |
78 OnSelectAutofillSuggestionAtIndex) | 78 OnSelectAutofillSuggestionAtIndex) |
79 IPC_MESSAGE_HANDLER(AutofillMsg_SetAutofillActionFill, | |
80 OnSetAutofillActionFill) | |
81 IPC_MESSAGE_HANDLER(AutofillMsg_ClearForm, | |
82 OnClearForm) | |
83 IPC_MESSAGE_HANDLER(AutofillMsg_SetAutofillActionPreview, | |
84 OnSetAutofillActionPreview) | |
85 IPC_MESSAGE_HANDLER(AutofillMsg_ClearPreviewedForm, | |
86 OnClearPreviewedForm) | |
87 IPC_MESSAGE_HANDLER(AutofillMsg_SetNodeText, | |
88 OnSetNodeText) | |
79 IPC_MESSAGE_UNHANDLED(handled = false) | 89 IPC_MESSAGE_UNHANDLED(handled = false) |
80 IPC_END_MESSAGE_MAP() | 90 IPC_END_MESSAGE_MAP() |
81 return handled; | 91 return handled; |
82 } | 92 } |
83 | 93 |
84 void AutofillAgent::DidFinishDocumentLoad(WebFrame* frame) { | 94 void AutofillAgent::DidFinishDocumentLoad(WebFrame* frame) { |
85 // The document has now been fully loaded. Scan for forms to be sent up to | 95 // The document has now been fully loaded. Scan for forms to be sent up to |
86 // the browser. | 96 // the browser. |
87 std::vector<webkit::forms::FormData> forms; | 97 std::vector<webkit::forms::FormData> forms; |
88 form_cache_.ExtractForms(*frame, &forms); | 98 form_cache_.ExtractForms(*frame, &forms); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
132 } | 142 } |
133 | 143 |
134 void AutofillAgent::didAcceptAutofillSuggestion(const WebNode& node, | 144 void AutofillAgent::didAcceptAutofillSuggestion(const WebNode& node, |
135 const WebString& value, | 145 const WebString& value, |
136 const WebString& label, | 146 const WebString& label, |
137 int unique_id, | 147 int unique_id, |
138 unsigned index) { | 148 unsigned index) { |
139 if (password_autofill_manager_->DidAcceptAutofillSuggestion(node, value)) | 149 if (password_autofill_manager_->DidAcceptAutofillSuggestion(node, value)) |
140 return; | 150 return; |
141 | 151 |
152 DCHECK(node == autofill_query_element_); | |
153 | |
142 if (suggestions_options_index_ != -1 && | 154 if (suggestions_options_index_ != -1 && |
143 index == static_cast<unsigned>(suggestions_options_index_)) { | 155 index == static_cast<unsigned>(suggestions_options_index_)) { |
144 // User selected 'Autofill Options'. | 156 // User selected 'Autofill Options'. |
145 Send(new AutofillHostMsg_ShowAutofillDialog(routing_id())); | 157 Send(new AutofillHostMsg_ShowAutofillDialog(routing_id())); |
146 } else if (suggestions_clear_index_ != -1 && | 158 } else if (suggestions_clear_index_ != -1 && |
147 index == static_cast<unsigned>(suggestions_clear_index_)) { | 159 index == static_cast<unsigned>(suggestions_clear_index_)) { |
148 // User selected 'Clear form'. | 160 // User selected 'Clear form'. |
149 DCHECK(node == autofill_query_element_); | |
150 form_cache_.ClearFormWithElement(autofill_query_element_); | 161 form_cache_.ClearFormWithElement(autofill_query_element_); |
151 } else if (!unique_id) { | 162 } else if (!unique_id) { |
152 // User selected an Autocomplete entry, so we fill directly. | 163 // User selected an Autocomplete entry, so we fill directly. |
153 WebInputElement element = node.toConst<WebInputElement>(); | 164 SetNodeText(node.toConst<WebInputElement>(), value); |
154 | |
155 string16 substring = value; | |
156 substring = substring.substr(0, element.maxLength()); | |
157 element.setValue(substring, true); | |
158 } else { | 165 } else { |
159 // Fill the values for the whole form. | 166 // Fill the values for the whole form. |
160 FillAutofillFormData(node, unique_id, AUTOFILL_FILL); | 167 FillAutofillFormData(node, unique_id, AUTOFILL_FILL); |
161 } | 168 } |
162 | 169 |
163 suggestions_clear_index_ = -1; | 170 suggestions_clear_index_ = -1; |
164 suggestions_options_index_ = -1; | 171 suggestions_options_index_ = -1; |
165 } | 172 } |
166 | 173 |
167 void AutofillAgent::didSelectAutofillSuggestion(const WebNode& node, | 174 void AutofillAgent::didSelectAutofillSuggestion(const WebNode& node, |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
330 routing_id(), | 337 routing_id(), |
331 has_autofill_item && !has_shown_autofill_popup_for_current_edit_)); | 338 has_autofill_item && !has_shown_autofill_popup_for_current_edit_)); |
332 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item; | 339 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item; |
333 } | 340 } |
334 | 341 |
335 void AutofillAgent::OnFormDataFilled(int query_id, | 342 void AutofillAgent::OnFormDataFilled(int query_id, |
336 const webkit::forms::FormData& form) { | 343 const webkit::forms::FormData& form) { |
337 if (!render_view()->GetWebView() || query_id != autofill_query_id_) | 344 if (!render_view()->GetWebView() || query_id != autofill_query_id_) |
338 return; | 345 return; |
339 | 346 |
347 was_query_node_autofilled_ = autofill_query_element_.isAutofilled(); | |
348 | |
340 switch (autofill_action_) { | 349 switch (autofill_action_) { |
341 case AUTOFILL_FILL: | 350 case AUTOFILL_FILL: |
342 FillForm(form, autofill_query_element_); | 351 FillForm(form, autofill_query_element_); |
343 Send(new AutofillHostMsg_DidFillAutofillFormData(routing_id(), | 352 Send(new AutofillHostMsg_DidFillAutofillFormData(routing_id(), |
344 base::TimeTicks::Now())); | 353 base::TimeTicks::Now())); |
345 break; | 354 break; |
346 case AUTOFILL_PREVIEW: | 355 case AUTOFILL_PREVIEW: |
356 didClearAutofillSelection(autofill_query_element_); | |
357 | |
347 PreviewForm(form, autofill_query_element_); | 358 PreviewForm(form, autofill_query_element_); |
348 Send(new AutofillHostMsg_DidPreviewAutofillFormData(routing_id())); | 359 Send(new AutofillHostMsg_DidPreviewAutofillFormData(routing_id())); |
349 break; | 360 break; |
350 default: | 361 default: |
351 NOTREACHED(); | 362 NOTREACHED(); |
352 } | 363 } |
353 autofill_action_ = AUTOFILL_NONE; | 364 autofill_action_ = AUTOFILL_NONE; |
354 } | 365 } |
355 | 366 |
356 void AutofillAgent::OnFieldTypePredictionsAvailable( | 367 void AutofillAgent::OnFieldTypePredictionsAvailable( |
357 const std::vector<FormDataPredictions>& forms) { | 368 const std::vector<FormDataPredictions>& forms) { |
358 for (size_t i = 0; i < forms.size(); ++i) { | 369 for (size_t i = 0; i < forms.size(); ++i) { |
359 form_cache_.ShowPredictions(forms[i]); | 370 form_cache_.ShowPredictions(forms[i]); |
360 } | 371 } |
361 } | 372 } |
362 | 373 |
363 void AutofillAgent::OnSelectAutofillSuggestionAtIndex(int listIndex) { | 374 void AutofillAgent::OnSelectAutofillSuggestionAtIndex(int listIndex) { |
364 NOTIMPLEMENTED(); | 375 NOTIMPLEMENTED(); |
365 // TODO(jrg): enable once changes land in WebKit | 376 // TODO(jrg): enable once changes land in WebKit |
366 // render_view()->webview()->selectAutofillSuggestionAtIndex(listIndex); | 377 // render_view()->webview()->selectAutofillSuggestionAtIndex(listIndex); |
367 } | 378 } |
368 | 379 |
380 void AutofillAgent::OnSetAutofillActionFill() { | |
381 autofill_action_ = AUTOFILL_FILL; | |
382 } | |
383 | |
384 void AutofillAgent::OnClearForm() { | |
385 form_cache_.ClearFormWithElement(autofill_query_element_); | |
386 } | |
387 | |
388 void AutofillAgent::OnSetAutofillActionPreview() { | |
389 autofill_action_ = AUTOFILL_PREVIEW; | |
390 } | |
391 | |
392 void AutofillAgent::OnClearPreviewedForm() { | |
393 didClearAutofillSelection(autofill_query_element_); | |
394 } | |
395 | |
396 void AutofillAgent::OnSetNodeText(string16 value) { | |
Ilya Sherman
2012/02/08 17:01:57
nit: Might as well pass the |value| by const-refer
csharp
2012/02/08 19:05:43
Done.
| |
397 SetNodeText(autofill_query_element_, value); | |
398 } | |
399 | |
369 void AutofillAgent::ShowSuggestions(const WebInputElement& element, | 400 void AutofillAgent::ShowSuggestions(const WebInputElement& element, |
370 bool autofill_on_empty_values, | 401 bool autofill_on_empty_values, |
371 bool requires_caret_at_end, | 402 bool requires_caret_at_end, |
372 bool display_warning_if_disabled) { | 403 bool display_warning_if_disabled) { |
373 // If autocomplete is disabled at the form level, then we might want to show | 404 // If autocomplete is disabled at the form level, then we might want to show |
374 // a warning in place of suggestions. However, if autocomplete is disabled | 405 // a warning in place of suggestions. However, if autocomplete is disabled |
375 // specifically for this field, we never want to show a warning. Otherwise, | 406 // specifically for this field, we never want to show a warning. Otherwise, |
376 // we might interfere with custom popups (e.g. search suggestions) used by | 407 // we might interfere with custom popups (e.g. search suggestions) used by |
377 // the website. | 408 // the website. |
378 const WebFormElement form = element.form(); | 409 const WebFormElement form = element.form(); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
435 autofill_query_id_ = query_counter++; | 466 autofill_query_id_ = query_counter++; |
436 | 467 |
437 webkit::forms::FormData form; | 468 webkit::forms::FormData form; |
438 webkit::forms::FormField field; | 469 webkit::forms::FormField field; |
439 if (!FindFormAndFieldForInputElement(node.toConst<WebInputElement>(), &form, | 470 if (!FindFormAndFieldForInputElement(node.toConst<WebInputElement>(), &form, |
440 &field, REQUIRE_AUTOCOMPLETE)) { | 471 &field, REQUIRE_AUTOCOMPLETE)) { |
441 return; | 472 return; |
442 } | 473 } |
443 | 474 |
444 autofill_action_ = action; | 475 autofill_action_ = action; |
445 was_query_node_autofilled_ = field.is_autofilled; | |
446 Send(new AutofillHostMsg_FillAutofillFormData( | 476 Send(new AutofillHostMsg_FillAutofillFormData( |
447 routing_id(), autofill_query_id_, form, field, unique_id)); | 477 routing_id(), autofill_query_id_, form, field, unique_id)); |
448 } | 478 } |
449 | 479 |
480 void AutofillAgent::SetNodeText(WebKit::WebInputElement node, string16 value) { | |
481 string16 substring = value; | |
482 substring = substring.substr(0, node.maxLength()); | |
483 | |
484 node.setValue(substring, true); | |
485 } | |
486 | |
450 } // namespace autofill | 487 } // namespace autofill |
OLD | NEW |