Chromium Code Reviews| Index: components/autofill/content/renderer/password_autofill_agent.cc |
| diff --git a/components/autofill/content/renderer/password_autofill_agent.cc b/components/autofill/content/renderer/password_autofill_agent.cc |
| index 9e1e1db7025acd8685af9239015a95f397cd8bc2..c17d876c25771349d4310c5f8379176061bd2f5a 100644 |
| --- a/components/autofill/content/renderer/password_autofill_agent.cc |
| +++ b/components/autofill/content/renderer/password_autofill_agent.cc |
| @@ -229,6 +229,8 @@ PasswordAutofillAgent::PasswordAutofillAgent(content::RenderView* render_view) |
| usernames_usage_(NOTHING_TO_AUTOFILL), |
| web_view_(render_view->GetWebView()), |
| logging_state_active_(false), |
| + was_username_autofilled_(false), |
| + was_password_autofilled_(false), |
| weak_ptr_factory_(this) { |
| } |
| @@ -367,7 +369,7 @@ bool PasswordAutofillAgent::TextFieldHandlingKeyDown( |
| return true; |
| } |
| -bool PasswordAutofillAgent::AcceptSuggestion( |
| +bool PasswordAutofillAgent::FillSuggestion( |
| const blink::WebNode& node, |
| const blink::WebString& username, |
| const blink::WebString& password) { |
| @@ -391,11 +393,59 @@ bool PasswordAutofillAgent::AcceptSuggestion( |
| return true; |
| } |
| +bool PasswordAutofillAgent::PreviewSuggestion( |
| + const blink::WebNode& node, |
| + const blink::WebString& username, |
| + const blink::WebString& password) { |
| + blink::WebInputElement username_element; |
| + PasswordInfo password_info; |
| + |
| + if (!FindLoginInfo(node, &username_element, &password_info) || |
| + !IsElementAutocompletable(username_element) || |
| + !IsElementAutocompletable(password_info.password_field)) { |
| + return false; |
| + } |
| + |
| + was_username_autofilled_ = username_element.isAutofilled(); |
| + username_element.setSuggestedValue(username); |
| + username_element.setAutofilled(true); |
| + // Selection range starts from the end of matching characters between the |
| + // value and sugggestedValue of the username field. |
| + std::string value_string = username_element.value().utf8(); |
| + std::string suggested_string = username_element.suggestedValue().utf8(); |
| + size_t selection_start = 0; |
| + bool done_compare = false; |
| + while (done_compare == false) { |
| + if (selection_start == value_string.length() || |
| + selection_start == suggested_string.length()) |
| + done_compare = true; |
| + |
| + if (value_string[selection_start] == suggested_string[selection_start]) |
| + selection_start++; |
| + else |
| + done_compare = true; |
| + } |
| + username_element.setSelectionRange( |
| + selection_start, |
| + username_element.suggestedValue().length()); |
| + |
| + was_password_autofilled_ = password_info.password_field.isAutofilled(); |
| + password_info.password_field.setSuggestedValue(password); |
| + password_info.password_field.setAutofilled(true); |
| + |
| + return true; |
| +} |
| + |
| bool PasswordAutofillAgent::DidClearAutofillSelection( |
| const blink::WebNode& node) { |
| - blink::WebInputElement input; |
| - PasswordInfo password; |
| - return FindLoginInfo(node, &input, &password); |
| + blink::WebInputElement username_element; |
| + PasswordInfo password_info; |
| + if (!FindLoginInfo(node, &username_element, &password_info)) |
| + return false; |
| + |
| + ClearPreview(&username_element, &password_info.password_field); |
| + |
|
Ilya Sherman
2014/05/15 21:36:09
nit: I'd omit this blank line.
ziran.sun
2014/05/16 09:18:13
Done.
|
| + return true; |
| } |
| bool PasswordAutofillAgent::ShowSuggestions( |
| @@ -1026,4 +1076,19 @@ bool PasswordAutofillAgent::FindLoginInfo(const blink::WebNode& node, |
| return true; |
| } |
| +void PasswordAutofillAgent::ClearPreview( |
| + blink::WebInputElement* username, |
| + blink::WebInputElement* password) { |
| + if (!username->suggestedValue().isEmpty()) { |
| + username->setSuggestedValue(blink::WebString()); |
| + username->setAutofilled(was_username_autofilled_); |
| + username->setSelectionRange(username->value().length(), |
| + username->value().length()); |
| + } |
| + if (!password->suggestedValue().isEmpty()) { |
| + password->setSuggestedValue(blink::WebString()); |
| + password->setAutofilled(was_password_autofilled_); |
| + } |
| +} |
| + |
| } // namespace autofill |