| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/autofill/content/renderer/password_autofill_agent.h" | 5 #include "components/autofill/content/renderer/password_autofill_agent.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/i18n/case_conversion.h" | 9 #include "base/i18n/case_conversion.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/metrics/field_trial.h" | 12 #include "base/metrics/field_trial.h" |
| 13 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "components/autofill/content/common/autofill_messages.h" | 15 #include "components/autofill/content/common/autofill_messages.h" |
| 16 #include "components/autofill/content/renderer/form_autofill_util.h" | 16 #include "components/autofill/content/renderer/form_autofill_util.h" |
| 17 #include "components/autofill/content/renderer/password_form_conversion_utils.h" | 17 #include "components/autofill/content/renderer/password_form_conversion_utils.h" |
| 18 #include "components/autofill/content/renderer/renderer_save_password_progress_l
ogger.h" | 18 #include "components/autofill/content/renderer/renderer_save_password_progress_l
ogger.h" |
| 19 #include "components/autofill/core/common/autofill_constants.h" | 19 #include "components/autofill/core/common/autofill_constants.h" |
| 20 #include "components/autofill/core/common/autofill_switches.h" | 20 #include "components/autofill/core/common/autofill_switches.h" |
| 21 #include "components/autofill/core/common/autofill_util.h" |
| 21 #include "components/autofill/core/common/form_field_data.h" | 22 #include "components/autofill/core/common/form_field_data.h" |
| 22 #include "components/autofill/core/common/password_form.h" | 23 #include "components/autofill/core/common/password_form.h" |
| 23 #include "components/autofill/core/common/password_form_fill_data.h" | 24 #include "components/autofill/core/common/password_form_fill_data.h" |
| 24 #include "content/public/renderer/document_state.h" | 25 #include "content/public/renderer/document_state.h" |
| 25 #include "content/public/renderer/navigation_state.h" | 26 #include "content/public/renderer/navigation_state.h" |
| 26 #include "content/public/renderer/render_frame.h" | 27 #include "content/public/renderer/render_frame.h" |
| 27 #include "content/public/renderer/render_view.h" | 28 #include "content/public/renderer/render_view.h" |
| 28 #include "third_party/WebKit/public/platform/WebVector.h" | 29 #include "third_party/WebKit/public/platform/WebVector.h" |
| 29 #include "third_party/WebKit/public/web/WebAutofillClient.h" | 30 #include "third_party/WebKit/public/web/WebAutofillClient.h" |
| 30 #include "third_party/WebKit/public/web/WebDocument.h" | 31 #include "third_party/WebKit/public/web/WebDocument.h" |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 | 220 |
| 220 bool IsElementEditable(const blink::WebInputElement& element) { | 221 bool IsElementEditable(const blink::WebInputElement& element) { |
| 221 return element.isEnabled() && !element.isReadOnly(); | 222 return element.isEnabled() && !element.isReadOnly(); |
| 222 } | 223 } |
| 223 | 224 |
| 224 bool DoUsernamesMatch(const base::string16& username1, | 225 bool DoUsernamesMatch(const base::string16& username1, |
| 225 const base::string16& username2, | 226 const base::string16& username2, |
| 226 bool exact_match) { | 227 bool exact_match) { |
| 227 if (exact_match) | 228 if (exact_match) |
| 228 return username1 == username2; | 229 return username1 == username2; |
| 229 return base::StartsWith(username1, username2, base::CompareCase::SENSITIVE); | 230 return base::StartsWith(username1, username2, base::CompareCase::SENSITIVE) || |
| 231 FieldIsSuggestionSubstringStartingOnTokenBoundary(username1, username2, |
| 232 true); |
| 230 } | 233 } |
| 231 | 234 |
| 232 // Returns |true| if the given element is editable. Otherwise, returns |false|. | 235 // Returns |true| if the given element is editable. Otherwise, returns |false|. |
| 233 bool IsElementAutocompletable(const blink::WebInputElement& element) { | 236 bool IsElementAutocompletable(const blink::WebInputElement& element) { |
| 234 return IsElementEditable(element); | 237 return IsElementEditable(element); |
| 235 } | 238 } |
| 236 | 239 |
| 237 // Returns true if the password specified in |form| is a default value. | 240 // Returns true if the password specified in |form| is a default value. |
| 238 bool PasswordValueIsDefault(const base::string16& password_element, | 241 bool PasswordValueIsDefault(const base::string16& password_element, |
| 239 const base::string16& password_value, | 242 const base::string16& password_value, |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 } | 404 } |
| 402 if (password.empty()) | 405 if (password.empty()) |
| 403 return false; | 406 return false; |
| 404 | 407 |
| 405 // TODO(tkent): Check maxlength and pattern for both username and password | 408 // TODO(tkent): Check maxlength and pattern for both username and password |
| 406 // fields. | 409 // fields. |
| 407 | 410 |
| 408 // Input matches the username, fill in required values. | 411 // Input matches the username, fill in required values. |
| 409 if (!username_element->isNull() && | 412 if (!username_element->isNull() && |
| 410 IsElementAutocompletable(*username_element)) { | 413 IsElementAutocompletable(*username_element)) { |
| 414 // TODO(vabr): Why not setSuggestedValue? http://crbug.com/507714 |
| 411 username_element->setValue(username, true); | 415 username_element->setValue(username, true); |
| 412 (*nonscript_modified_values)[*username_element] = username; | 416 (*nonscript_modified_values)[*username_element] = username; |
| 413 username_element->setAutofilled(true); | 417 username_element->setAutofilled(true); |
| 414 | 418 if (set_selection) |
| 415 if (set_selection) { | 419 PreviewSuggestion(username, current_username, username_element); |
| 416 username_element->setSelectionRange(current_username.length(), | |
| 417 username.length()); | |
| 418 } | |
| 419 } else if (current_username != username) { | 420 } else if (current_username != username) { |
| 420 // If the username can't be filled and it doesn't match a saved password | 421 // If the username can't be filled and it doesn't match a saved password |
| 421 // as is, don't autofill a password. | 422 // as is, don't autofill a password. |
| 422 return false; | 423 return false; |
| 423 } | 424 } |
| 424 | 425 |
| 425 // Wait to fill in the password until a user gesture occurs. This is to make | 426 // Wait to fill in the password until a user gesture occurs. This is to make |
| 426 // sure that we do not fill in the DOM with a password until we believe the | 427 // sure that we do not fill in the DOM with a password until we believe the |
| 427 // user is intentionally interacting with the page. | 428 // user is intentionally interacting with the page. |
| 428 password_element->setSuggestedValue(password); | 429 password_element->setSuggestedValue(password); |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 | 555 |
| 555 //////////////////////////////////////////////////////////////////////////////// | 556 //////////////////////////////////////////////////////////////////////////////// |
| 556 // PasswordAutofillAgent, public: | 557 // PasswordAutofillAgent, public: |
| 557 | 558 |
| 558 PasswordAutofillAgent::PasswordAutofillAgent(content::RenderFrame* render_frame) | 559 PasswordAutofillAgent::PasswordAutofillAgent(content::RenderFrame* render_frame) |
| 559 : content::RenderFrameObserver(render_frame), | 560 : content::RenderFrameObserver(render_frame), |
| 560 legacy_(render_frame->GetRenderView(), this), | 561 legacy_(render_frame->GetRenderView(), this), |
| 561 logging_state_active_(false), | 562 logging_state_active_(false), |
| 562 was_username_autofilled_(false), | 563 was_username_autofilled_(false), |
| 563 was_password_autofilled_(false), | 564 was_password_autofilled_(false), |
| 564 username_selection_start_(0), | |
| 565 did_stop_loading_(false), | 565 did_stop_loading_(false), |
| 566 weak_ptr_factory_(this) { | 566 weak_ptr_factory_(this) { |
| 567 Send(new AutofillHostMsg_PasswordAutofillAgentConstructed(routing_id())); | 567 Send(new AutofillHostMsg_PasswordAutofillAgentConstructed(routing_id())); |
| 568 } | 568 } |
| 569 | 569 |
| 570 PasswordAutofillAgent::~PasswordAutofillAgent() { | 570 PasswordAutofillAgent::~PasswordAutofillAgent() { |
| 571 } | 571 } |
| 572 | 572 |
| 573 PasswordAutofillAgent::PasswordValueGatekeeper::PasswordValueGatekeeper() | 573 PasswordAutofillAgent::PasswordValueGatekeeper::PasswordValueGatekeeper() |
| 574 : was_user_gesture_seen_(false) { | 574 : was_user_gesture_seen_(false) { |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 745 const blink::WebString& password) { | 745 const blink::WebString& password) { |
| 746 blink::WebInputElement username_element; | 746 blink::WebInputElement username_element; |
| 747 PasswordInfo* password_info; | 747 PasswordInfo* password_info; |
| 748 | 748 |
| 749 if (!FindLoginInfo(node, &username_element, &password_info) || | 749 if (!FindLoginInfo(node, &username_element, &password_info) || |
| 750 !IsElementAutocompletable(username_element) || | 750 !IsElementAutocompletable(username_element) || |
| 751 !IsElementAutocompletable(password_info->password_field)) { | 751 !IsElementAutocompletable(password_info->password_field)) { |
| 752 return false; | 752 return false; |
| 753 } | 753 } |
| 754 | 754 |
| 755 if (username_query_prefix_.empty()) |
| 756 username_query_prefix_ = username_element.value(); |
| 757 |
| 755 was_username_autofilled_ = username_element.isAutofilled(); | 758 was_username_autofilled_ = username_element.isAutofilled(); |
| 756 username_selection_start_ = username_element.selectionStart(); | |
| 757 username_element.setSuggestedValue(username); | 759 username_element.setSuggestedValue(username); |
| 758 username_element.setAutofilled(true); | 760 username_element.setAutofilled(true); |
| 759 username_element.setSelectionRange( | 761 ::autofill::PreviewSuggestion(username_element.suggestedValue(), |
| 760 username_selection_start_, | 762 username_query_prefix_, &username_element); |
| 761 username_element.suggestedValue().length()); | |
| 762 | |
| 763 was_password_autofilled_ = password_info->password_field.isAutofilled(); | 763 was_password_autofilled_ = password_info->password_field.isAutofilled(); |
| 764 password_info->password_field.setSuggestedValue(password); | 764 password_info->password_field.setSuggestedValue(password); |
| 765 password_info->password_field.setAutofilled(true); | 765 password_info->password_field.setAutofilled(true); |
| 766 | 766 |
| 767 return true; | 767 return true; |
| 768 } | 768 } |
| 769 | 769 |
| 770 bool PasswordAutofillAgent::DidClearAutofillSelection( | 770 bool PasswordAutofillAgent::DidClearAutofillSelection( |
| 771 const blink::WebNode& node) { | 771 const blink::WebNode& node) { |
| 772 blink::WebInputElement username_element; | 772 blink::WebInputElement username_element; |
| (...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1297 if (show_all) | 1297 if (show_all) |
| 1298 options |= SHOW_ALL; | 1298 options |= SHOW_ALL; |
| 1299 if (show_on_password_field) | 1299 if (show_on_password_field) |
| 1300 options |= IS_PASSWORD_FIELD; | 1300 options |= IS_PASSWORD_FIELD; |
| 1301 base::string16 username_string( | 1301 base::string16 username_string( |
| 1302 username.isNull() ? base::string16() | 1302 username.isNull() ? base::string16() |
| 1303 : static_cast<base::string16>(user_input.value())); | 1303 : static_cast<base::string16>(user_input.value())); |
| 1304 Send(new AutofillHostMsg_ShowPasswordSuggestions( | 1304 Send(new AutofillHostMsg_ShowPasswordSuggestions( |
| 1305 routing_id(), key_it->second, field.text_direction, username_string, | 1305 routing_id(), key_it->second, field.text_direction, username_string, |
| 1306 options, bounding_box_scaled)); | 1306 options, bounding_box_scaled)); |
| 1307 username_query_prefix_ = username_string; |
| 1307 return CanShowSuggestion(fill_data, username_string, show_all); | 1308 return CanShowSuggestion(fill_data, username_string, show_all); |
| 1308 } | 1309 } |
| 1309 | 1310 |
| 1310 void PasswordAutofillAgent::FrameClosing() { | 1311 void PasswordAutofillAgent::FrameClosing() { |
| 1311 for (auto const& iter : login_to_password_info_) { | 1312 for (auto const& iter : login_to_password_info_) { |
| 1312 login_to_password_info_key_.erase(iter.first); | 1313 login_to_password_info_key_.erase(iter.first); |
| 1313 password_to_username_.erase(iter.second.password_field); | 1314 password_to_username_.erase(iter.second.password_field); |
| 1314 } | 1315 } |
| 1315 login_to_password_info_.clear(); | 1316 login_to_password_info_.clear(); |
| 1316 provisionally_saved_form_.reset(); | 1317 provisionally_saved_form_.reset(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1332 return FindPasswordInfoForElement(*found_input, &username_element, | 1333 return FindPasswordInfoForElement(*found_input, &username_element, |
| 1333 found_password); | 1334 found_password); |
| 1334 } | 1335 } |
| 1335 | 1336 |
| 1336 void PasswordAutofillAgent::ClearPreview( | 1337 void PasswordAutofillAgent::ClearPreview( |
| 1337 blink::WebInputElement* username, | 1338 blink::WebInputElement* username, |
| 1338 blink::WebInputElement* password) { | 1339 blink::WebInputElement* password) { |
| 1339 if (!username->suggestedValue().isEmpty()) { | 1340 if (!username->suggestedValue().isEmpty()) { |
| 1340 username->setSuggestedValue(blink::WebString()); | 1341 username->setSuggestedValue(blink::WebString()); |
| 1341 username->setAutofilled(was_username_autofilled_); | 1342 username->setAutofilled(was_username_autofilled_); |
| 1342 username->setSelectionRange(username_selection_start_, | 1343 username->setSelectionRange(username_query_prefix_.length(), |
| 1343 username->value().length()); | 1344 username->value().length()); |
| 1344 } | 1345 } |
| 1345 if (!password->suggestedValue().isEmpty()) { | 1346 if (!password->suggestedValue().isEmpty()) { |
| 1346 password->setSuggestedValue(blink::WebString()); | 1347 password->setSuggestedValue(blink::WebString()); |
| 1347 password->setAutofilled(was_password_autofilled_); | 1348 password->setAutofilled(was_password_autofilled_); |
| 1348 } | 1349 } |
| 1349 } | 1350 } |
| 1350 | 1351 |
| 1351 void PasswordAutofillAgent::ProvisionallySavePassword( | 1352 void PasswordAutofillAgent::ProvisionallySavePassword( |
| 1352 const blink::WebFormElement& form, | 1353 const blink::WebFormElement& form, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1391 void PasswordAutofillAgent::LegacyPasswordAutofillAgent::DidStopLoading() { | 1392 void PasswordAutofillAgent::LegacyPasswordAutofillAgent::DidStopLoading() { |
| 1392 agent_->DidStopLoading(); | 1393 agent_->DidStopLoading(); |
| 1393 } | 1394 } |
| 1394 | 1395 |
| 1395 void PasswordAutofillAgent::LegacyPasswordAutofillAgent:: | 1396 void PasswordAutofillAgent::LegacyPasswordAutofillAgent:: |
| 1396 DidStartProvisionalLoad(blink::WebLocalFrame* navigated_frame) { | 1397 DidStartProvisionalLoad(blink::WebLocalFrame* navigated_frame) { |
| 1397 agent_->LegacyDidStartProvisionalLoad(navigated_frame); | 1398 agent_->LegacyDidStartProvisionalLoad(navigated_frame); |
| 1398 } | 1399 } |
| 1399 | 1400 |
| 1400 } // namespace autofill | 1401 } // namespace autofill |
| OLD | NEW |