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/password_autofill_manager.h" | 5 #include "chrome/renderer/autofill/password_autofill_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "chrome/common/autofill_messages.h" | 10 #include "chrome/common/autofill_messages.h" |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 188 } | 188 } |
| 189 | 189 |
| 190 bool DoUsernamesMatch(const string16& username1, | 190 bool DoUsernamesMatch(const string16& username1, |
| 191 const string16& username2, | 191 const string16& username2, |
| 192 bool exact_match) { | 192 bool exact_match) { |
| 193 if (exact_match) | 193 if (exact_match) |
| 194 return username1 == username2; | 194 return username1 == username2; |
| 195 return StartsWith(username1, username2, true); | 195 return StartsWith(username1, username2, true); |
| 196 } | 196 } |
| 197 | 197 |
| 198 // Get whether fill_data has a username or not. | |
|
Ilya Sherman
2012/04/19 00:27:37
nit: Wording suggestion: """Returns true iff the |
| |
| 199 bool HasUsernameField(const webkit::forms::PasswordFormFillData& fill_data) { | |
| 200 return fill_data.basic_data.fields.size() == 2; | |
| 201 } | |
| 202 | |
| 203 // Get the username field from fill_data. | |
|
Ilya Sherman
2012/04/19 00:27:37
nit: Wording suggestion: "Returns the username fie
| |
| 204 // If it hasn't any username, calls NOTREACHED(). | |
|
Ilya Sherman
2012/04/19 00:27:37
nit: Please omit this line -- it's redundant with
| |
| 205 const webkit::forms::FormField& GetUsernameField( | |
| 206 const webkit::forms::PasswordFormFillData& fill_data) { | |
| 207 DCHECK(HasUsernameField(fill_data)); | |
| 208 return fill_data.basic_data.fields[1]; | |
| 209 } | |
| 210 | |
| 211 // Get the password field from fill_data. | |
|
Ilya Sherman
2012/04/19 00:27:37
nit: Wording suggestion: "Returns the password fie
| |
| 212 const webkit::forms::FormField& GetPasswordField( | |
| 213 const webkit::forms::PasswordFormFillData& fill_data) { | |
| 214 DCHECK(!fill_data.basic_data.fields.empty()); | |
| 215 return fill_data.basic_data.fields[0]; | |
| 216 } | |
| 198 } // namespace | 217 } // namespace |
| 199 | 218 |
| 200 namespace autofill { | 219 namespace autofill { |
| 201 | 220 |
| 202 //////////////////////////////////////////////////////////////////////////////// | 221 //////////////////////////////////////////////////////////////////////////////// |
| 203 // PasswordAutofillManager, public: | 222 // PasswordAutofillManager, public: |
| 204 | 223 |
| 205 PasswordAutofillManager::PasswordAutofillManager( | 224 PasswordAutofillManager::PasswordAutofillManager( |
| 206 content::RenderView* render_view) | 225 content::RenderView* render_view) |
| 207 : content::RenderViewObserver(render_view), | 226 : content::RenderViewObserver(render_view), |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 440 FindFormElements(render_view()->GetWebView(), form_data.basic_data, &forms); | 459 FindFormElements(render_view()->GetWebView(), form_data.basic_data, &forms); |
| 441 FormElementsList::iterator iter; | 460 FormElementsList::iterator iter; |
| 442 for (iter = forms.begin(); iter != forms.end(); ++iter) { | 461 for (iter = forms.begin(); iter != forms.end(); ++iter) { |
| 443 scoped_ptr<FormElements> form_elements(*iter); | 462 scoped_ptr<FormElements> form_elements(*iter); |
| 444 | 463 |
| 445 // If wait_for_username is true, we don't want to initially fill the form | 464 // If wait_for_username is true, we don't want to initially fill the form |
| 446 // until the user types in a valid username. | 465 // until the user types in a valid username. |
| 447 if (!form_data.wait_for_username) | 466 if (!form_data.wait_for_username) |
| 448 FillForm(form_elements.get(), form_data.basic_data); | 467 FillForm(form_elements.get(), form_data.basic_data); |
| 449 | 468 |
| 469 // If there isn't any username, go to next iteration. | |
|
Ilya Sherman
2012/04/19 00:27:37
nit: This comment is redundant with the code. Ins
| |
| 470 if (!HasUsernameField(form_data)) | |
| 471 continue; | |
| 472 | |
| 473 // If there is a username, set the username and login_to_password_info_. | |
| 450 // Attach autocomplete listener to enable selecting alternate logins. | 474 // Attach autocomplete listener to enable selecting alternate logins. |
| 451 // First, get pointers to username element. | 475 // First, get pointers to username element. |
| 452 WebKit::WebInputElement username_element = | 476 WebKit::WebInputElement username_element = |
| 453 form_elements->input_elements[form_data.basic_data.fields[0].name]; | 477 form_elements->input_elements[GetUsernameField(form_data).name]; |
| 454 | 478 |
| 455 // Get pointer to password element. (We currently only support single | 479 // Get pointer to password element. (We currently only support single |
| 456 // password forms). | 480 // password forms). |
| 457 WebKit::WebInputElement password_element = | 481 WebKit::WebInputElement password_element = |
| 458 form_elements->input_elements[form_data.basic_data.fields[1].name]; | 482 form_elements->input_elements[GetPasswordField(form_data).name]; |
| 459 | 483 |
| 460 // We might have already filled this form if there are two <form> elements | 484 // We might have already filled this form if there are two <form> elements |
| 461 // with identical markup. | 485 // with identical markup. |
| 462 if (login_to_password_info_.find(username_element) != | 486 if (login_to_password_info_.find(username_element) != |
| 463 login_to_password_info_.end()) | 487 login_to_password_info_.end()) |
| 464 continue; | 488 continue; |
| 465 | 489 |
| 466 PasswordInfo password_info; | 490 PasswordInfo password_info; |
| 467 password_info.fill_data = form_data; | 491 password_info.fill_data = form_data; |
| 468 password_info.password_field = password_element; | 492 password_info.password_field = password_element; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 479 } | 503 } |
| 480 } | 504 } |
| 481 | 505 |
| 482 //////////////////////////////////////////////////////////////////////////////// | 506 //////////////////////////////////////////////////////////////////////////////// |
| 483 // PasswordAutofillManager, private: | 507 // PasswordAutofillManager, private: |
| 484 | 508 |
| 485 void PasswordAutofillManager::GetSuggestions( | 509 void PasswordAutofillManager::GetSuggestions( |
| 486 const webkit::forms::PasswordFormFillData& fill_data, | 510 const webkit::forms::PasswordFormFillData& fill_data, |
| 487 const string16& input, | 511 const string16& input, |
| 488 std::vector<string16>* suggestions) { | 512 std::vector<string16>* suggestions) { |
| 489 if (StartsWith(fill_data.basic_data.fields[0].value, input, false)) | 513 // There are both a password and a username. |
|
Ilya Sherman
2012/04/19 00:27:37
nit: Please move this comment into the if-stmt, ri
| |
| 490 suggestions->push_back(fill_data.basic_data.fields[0].value); | 514 if (HasUsernameField(fill_data) && |
| 515 StartsWith(GetUsernameField(fill_data).value, input, false)) { | |
| 516 suggestions->push_back(GetUsernameField(fill_data).value); | |
| 517 } | |
| 491 | 518 |
| 492 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter; | 519 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter; |
| 493 for (iter = fill_data.additional_logins.begin(); | 520 for (iter = fill_data.additional_logins.begin(); |
| 494 iter != fill_data.additional_logins.end(); ++iter) { | 521 iter != fill_data.additional_logins.end(); ++iter) { |
| 495 if (StartsWith(iter->first, input, false)) | 522 if (StartsWith(iter->first, input, false)) |
| 496 suggestions->push_back(iter->first); | 523 suggestions->push_back(iter->first); |
| 497 } | 524 } |
| 498 } | 525 } |
| 499 | 526 |
| 500 bool PasswordAutofillManager::ShowSuggestionPopup( | 527 bool PasswordAutofillManager::ShowSuggestionPopup( |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 544 WebKit::WebInputElement* username_element, | 571 WebKit::WebInputElement* username_element, |
| 545 WebKit::WebInputElement* password_element, | 572 WebKit::WebInputElement* password_element, |
| 546 const webkit::forms::PasswordFormFillData& fill_data, | 573 const webkit::forms::PasswordFormFillData& fill_data, |
| 547 bool exact_username_match, | 574 bool exact_username_match, |
| 548 bool set_selection) { | 575 bool set_selection) { |
| 549 string16 current_username = username_element->value(); | 576 string16 current_username = username_element->value(); |
| 550 // username and password will contain the match found if any. | 577 // username and password will contain the match found if any. |
| 551 string16 username; | 578 string16 username; |
| 552 string16 password; | 579 string16 password; |
| 553 | 580 |
| 581 // If there isn't any username form, just exit. | |
| 582 // Because this function is used for the case | |
| 583 // that the username form and the possword form exist. | |
|
Ilya Sherman
2012/04/19 00:27:37
nit: Wording suggestion: """If there is no usernam
| |
| 584 if (!HasUsernameField(fill_data)) | |
| 585 return false; | |
| 586 | |
| 554 // Look for any suitable matches to current field text. | 587 // Look for any suitable matches to current field text. |
| 555 if (DoUsernamesMatch(fill_data.basic_data.fields[0].value, current_username, | 588 if (DoUsernamesMatch(GetUsernameField(fill_data).value, current_username, |
| 556 exact_username_match)) { | 589 exact_username_match)) { |
| 557 username = fill_data.basic_data.fields[0].value; | 590 username = GetUsernameField(fill_data).value; |
| 558 password = fill_data.basic_data.fields[1].value; | 591 password = GetPasswordField(fill_data).value; |
| 559 } else { | 592 } else { |
| 560 // Scan additional logins for a match. | 593 // Scan additional logins for a match. |
| 561 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter; | 594 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter; |
| 562 for (iter = fill_data.additional_logins.begin(); | 595 for (iter = fill_data.additional_logins.begin(); |
| 563 iter != fill_data.additional_logins.end(); ++iter) { | 596 iter != fill_data.additional_logins.end(); ++iter) { |
| 564 if (DoUsernamesMatch(iter->first, current_username, | 597 if (DoUsernamesMatch(iter->first, current_username, |
| 565 exact_username_match)) { | 598 exact_username_match)) { |
| 566 username = iter->first; | 599 username = iter->first; |
| 567 password = iter->second; | 600 password = iter->second; |
| 568 break; | 601 break; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 636 LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(input); | 669 LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(input); |
| 637 if (iter == login_to_password_info_.end()) | 670 if (iter == login_to_password_info_.end()) |
| 638 return false; | 671 return false; |
| 639 | 672 |
| 640 *found_input = input; | 673 *found_input = input; |
| 641 *found_password = iter->second; | 674 *found_password = iter->second; |
| 642 return true; | 675 return true; |
| 643 } | 676 } |
| 644 | 677 |
| 645 } // namespace autofill | 678 } // namespace autofill |
| OLD | NEW |