Chromium Code Reviews| 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 WebInputElement element = node.toConst<WebInputElement>(); |
| 154 | 165 |
| 155 string16 substring = value; | 166 string16 substring = value; |
| 156 substring = substring.substr(0, element.maxLength()); | 167 substring = substring.substr(0, element.maxLength()); |
| 157 element.setValue(substring, true); | 168 element.setValue(substring, true); |
|
Ilya Sherman
2012/02/07 23:35:55
nit: This code is essentially repeated in OnSetNod
csharp
2012/02/08 16:11:16
Done.
| |
| 158 } else { | 169 } else { |
| 159 // Fill the values for the whole form. | 170 // Fill the values for the whole form. |
| 160 FillAutofillFormData(node, unique_id, AUTOFILL_FILL); | 171 FillAutofillFormData(node, unique_id, AUTOFILL_FILL); |
| 161 } | 172 } |
| 162 | 173 |
| 163 suggestions_clear_index_ = -1; | 174 suggestions_clear_index_ = -1; |
| 164 suggestions_options_index_ = -1; | 175 suggestions_options_index_ = -1; |
| 165 } | 176 } |
| 166 | 177 |
| 167 void AutofillAgent::didSelectAutofillSuggestion(const WebNode& node, | 178 void AutofillAgent::didSelectAutofillSuggestion(const WebNode& node, |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 330 routing_id(), | 341 routing_id(), |
| 331 has_autofill_item && !has_shown_autofill_popup_for_current_edit_)); | 342 has_autofill_item && !has_shown_autofill_popup_for_current_edit_)); |
| 332 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item; | 343 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item; |
| 333 } | 344 } |
| 334 | 345 |
| 335 void AutofillAgent::OnFormDataFilled(int query_id, | 346 void AutofillAgent::OnFormDataFilled(int query_id, |
| 336 const webkit::forms::FormData& form) { | 347 const webkit::forms::FormData& form) { |
| 337 if (!render_view()->GetWebView() || query_id != autofill_query_id_) | 348 if (!render_view()->GetWebView() || query_id != autofill_query_id_) |
| 338 return; | 349 return; |
| 339 | 350 |
| 351 was_query_node_autofilled_ = autofill_query_element_.isAutofilled(); | |
| 352 | |
| 340 switch (autofill_action_) { | 353 switch (autofill_action_) { |
| 341 case AUTOFILL_FILL: | 354 case AUTOFILL_FILL: |
| 342 FillForm(form, autofill_query_element_); | 355 FillForm(form, autofill_query_element_); |
| 343 Send(new AutofillHostMsg_DidFillAutofillFormData(routing_id(), | 356 Send(new AutofillHostMsg_DidFillAutofillFormData(routing_id(), |
| 344 base::TimeTicks::Now())); | 357 base::TimeTicks::Now())); |
| 345 break; | 358 break; |
| 346 case AUTOFILL_PREVIEW: | 359 case AUTOFILL_PREVIEW: |
| 360 didClearAutofillSelection(autofill_query_element_); | |
| 361 | |
| 347 PreviewForm(form, autofill_query_element_); | 362 PreviewForm(form, autofill_query_element_); |
| 348 Send(new AutofillHostMsg_DidPreviewAutofillFormData(routing_id())); | 363 Send(new AutofillHostMsg_DidPreviewAutofillFormData(routing_id())); |
| 349 break; | 364 break; |
| 350 default: | 365 default: |
| 351 NOTREACHED(); | 366 NOTREACHED(); |
| 352 } | 367 } |
| 353 autofill_action_ = AUTOFILL_NONE; | 368 autofill_action_ = AUTOFILL_NONE; |
| 354 } | 369 } |
| 355 | 370 |
| 356 void AutofillAgent::OnFieldTypePredictionsAvailable( | 371 void AutofillAgent::OnFieldTypePredictionsAvailable( |
| 357 const std::vector<FormDataPredictions>& forms) { | 372 const std::vector<FormDataPredictions>& forms) { |
| 358 for (size_t i = 0; i < forms.size(); ++i) { | 373 for (size_t i = 0; i < forms.size(); ++i) { |
| 359 form_cache_.ShowPredictions(forms[i]); | 374 form_cache_.ShowPredictions(forms[i]); |
| 360 } | 375 } |
| 361 } | 376 } |
| 362 | 377 |
| 363 void AutofillAgent::OnSelectAutofillSuggestionAtIndex(int listIndex) { | 378 void AutofillAgent::OnSelectAutofillSuggestionAtIndex(int listIndex) { |
| 364 NOTIMPLEMENTED(); | 379 NOTIMPLEMENTED(); |
| 365 // TODO(jrg): enable once changes land in WebKit | 380 // TODO(jrg): enable once changes land in WebKit |
| 366 // render_view()->webview()->selectAutofillSuggestionAtIndex(listIndex); | 381 // render_view()->webview()->selectAutofillSuggestionAtIndex(listIndex); |
| 367 } | 382 } |
| 368 | 383 |
| 384 void AutofillAgent::OnSetAutofillActionFill() { | |
| 385 autofill_action_ = AUTOFILL_FILL; | |
| 386 } | |
| 387 | |
| 388 void AutofillAgent::OnClearForm() { | |
| 389 form_cache_.ClearFormWithElement(autofill_query_element_); | |
| 390 } | |
| 391 | |
| 392 void AutofillAgent::OnSetAutofillActionPreview() { | |
| 393 autofill_action_ = AUTOFILL_PREVIEW; | |
| 394 } | |
| 395 | |
| 396 void AutofillAgent::OnClearPreviewedForm() { | |
| 397 didClearAutofillSelection(autofill_query_element_); | |
| 398 } | |
| 399 | |
| 400 void AutofillAgent::OnSetNodeText(string16 value) { | |
| 401 string16 substring = value; | |
| 402 substring = substring.substr(0, autofill_query_element_.maxLength()); | |
| 403 | |
| 404 autofill_query_element_.setValue(value, true); | |
| 405 } | |
| 406 | |
| 369 void AutofillAgent::ShowSuggestions(const WebInputElement& element, | 407 void AutofillAgent::ShowSuggestions(const WebInputElement& element, |
| 370 bool autofill_on_empty_values, | 408 bool autofill_on_empty_values, |
| 371 bool requires_caret_at_end, | 409 bool requires_caret_at_end, |
| 372 bool display_warning_if_disabled) { | 410 bool display_warning_if_disabled) { |
| 373 // If autocomplete is disabled at the form level, then we might want to show | 411 // 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 | 412 // a warning in place of suggestions. However, if autocomplete is disabled |
| 375 // specifically for this field, we never want to show a warning. Otherwise, | 413 // 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 | 414 // we might interfere with custom popups (e.g. search suggestions) used by |
| 377 // the website. | 415 // the website. |
| 378 const WebFormElement form = element.form(); | 416 const WebFormElement form = element.form(); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 435 autofill_query_id_ = query_counter++; | 473 autofill_query_id_ = query_counter++; |
| 436 | 474 |
| 437 webkit::forms::FormData form; | 475 webkit::forms::FormData form; |
| 438 webkit::forms::FormField field; | 476 webkit::forms::FormField field; |
| 439 if (!FindFormAndFieldForInputElement(node.toConst<WebInputElement>(), &form, | 477 if (!FindFormAndFieldForInputElement(node.toConst<WebInputElement>(), &form, |
| 440 &field, REQUIRE_AUTOCOMPLETE)) { | 478 &field, REQUIRE_AUTOCOMPLETE)) { |
| 441 return; | 479 return; |
| 442 } | 480 } |
| 443 | 481 |
| 444 autofill_action_ = action; | 482 autofill_action_ = action; |
| 445 was_query_node_autofilled_ = field.is_autofilled; | |
| 446 Send(new AutofillHostMsg_FillAutofillFormData( | 483 Send(new AutofillHostMsg_FillAutofillFormData( |
| 447 routing_id(), autofill_query_id_, form, field, unique_id)); | 484 routing_id(), autofill_query_id_, form, field, unique_id)); |
| 448 } | 485 } |
| 449 | 486 |
| 450 } // namespace autofill | 487 } // namespace autofill |
| OLD | NEW |