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 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 } | 187 } |
| 188 | 188 |
| 189 bool DoUsernamesMatch(const string16& username1, | 189 bool DoUsernamesMatch(const string16& username1, |
| 190 const string16& username2, | 190 const string16& username2, |
| 191 bool exact_match) { | 191 bool exact_match) { |
| 192 if (exact_match) | 192 if (exact_match) |
| 193 return username1 == username2; | 193 return username1 == username2; |
| 194 return StartsWith(username1, username2, true); | 194 return StartsWith(username1, username2, true); |
| 195 } | 195 } |
| 196 | 196 |
| 197 // Get whether fill_data has a username or not. | |
| 198 bool HasUsernameField(const webkit::forms::PasswordFormFillData& fill_data) { | |
| 199 return fill_data.basic_data.fields.size() == 2; | |
| 200 } | |
| 201 | |
| 202 // Get the username field from fill_data. | |
| 203 // If it hasn't any username, calls NOTREACHED(). | |
| 204 const webkit::forms::FormField& GetUsernameField( | |
| 205 const webkit::forms::PasswordFormFillData& fill_data) { | |
| 206 DCHECK(HasUsernameField(fill_data)); | |
| 207 return fill_data.basic_data.fields[1]; | |
| 208 } | |
| 209 | |
| 210 // Get the password field from fill_data. | |
| 211 const webkit::forms::FormField& GetPasswordField( | |
| 212 const webkit::forms::PasswordFormFillData& fill_data) { | |
| 213 DCHECK(!fill_data.basic_data.fields.empty()); | |
| 214 return fill_data.basic_data.fields[0]; | |
| 215 } | |
| 197 } // namespace | 216 } // namespace |
| 198 | 217 |
| 199 namespace autofill { | 218 namespace autofill { |
| 200 | 219 |
| 201 //////////////////////////////////////////////////////////////////////////////// | 220 //////////////////////////////////////////////////////////////////////////////// |
| 202 // PasswordAutofillManager, public: | 221 // PasswordAutofillManager, public: |
| 203 | 222 |
| 204 PasswordAutofillManager::PasswordAutofillManager( | 223 PasswordAutofillManager::PasswordAutofillManager( |
| 205 content::RenderView* render_view) | 224 content::RenderView* render_view) |
| 206 : content::RenderViewObserver(render_view), | 225 : content::RenderViewObserver(render_view), |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 431 FindFormElements(render_view()->GetWebView(), form_data.basic_data, &forms); | 450 FindFormElements(render_view()->GetWebView(), form_data.basic_data, &forms); |
| 432 FormElementsList::iterator iter; | 451 FormElementsList::iterator iter; |
| 433 for (iter = forms.begin(); iter != forms.end(); ++iter) { | 452 for (iter = forms.begin(); iter != forms.end(); ++iter) { |
| 434 scoped_ptr<FormElements> form_elements(*iter); | 453 scoped_ptr<FormElements> form_elements(*iter); |
| 435 | 454 |
| 436 // If wait_for_username is true, we don't want to initially fill the form | 455 // If wait_for_username is true, we don't want to initially fill the form |
| 437 // until the user types in a valid username. | 456 // until the user types in a valid username. |
| 438 if (!form_data.wait_for_username) | 457 if (!form_data.wait_for_username) |
| 439 FillForm(form_elements.get(), form_data.basic_data); | 458 FillForm(form_elements.get(), form_data.basic_data); |
| 440 | 459 |
| 460 // If there isn't any username, go to next iteration. | |
| 461 if (!HasUsernameField(form_data)) | |
| 462 continue; | |
| 463 | |
| 464 // If there is a username, set the username and login_to_password_info_. | |
| 441 // Attach autocomplete listener to enable selecting alternate logins. | 465 // Attach autocomplete listener to enable selecting alternate logins. |
| 442 // First, get pointers to username element. | 466 // First, get pointers to username element. |
| 443 WebKit::WebInputElement username_element = | 467 WebKit::WebInputElement username_element = |
| 444 form_elements->input_elements[form_data.basic_data.fields[0].name]; | 468 form_elements->input_elements[GetUsernameField(form_data).name]; |
|
Ilya Sherman
2012/04/05 22:20:36
nit: The indentation here was previously correct -
Yumikiyo Osanai
2012/04/06 00:17:34
Done.
| |
| 445 | 469 |
| 446 // Get pointer to password element. (We currently only support single | 470 // Get pointer to password element. (We currently only support single |
| 447 // password forms). | 471 // password forms). |
| 448 WebKit::WebInputElement password_element = | 472 WebKit::WebInputElement password_element = |
| 449 form_elements->input_elements[form_data.basic_data.fields[1].name]; | 473 form_elements->input_elements[GetPasswordField(form_data).name]; |
|
Ilya Sherman
2012/04/05 22:20:36
nit: Ditto.
Yumikiyo Osanai
2012/04/06 00:17:34
Done.
| |
| 450 | 474 |
| 451 // We might have already filled this form if there are two <form> elements | 475 // We might have already filled this form if there are two <form> elements |
| 452 // with identical markup. | 476 // with identical markup. |
| 453 if (login_to_password_info_.find(username_element) != | 477 if (login_to_password_info_.find(username_element) != |
| 454 login_to_password_info_.end()) | 478 login_to_password_info_.end()) |
| 455 continue; | 479 continue; |
| 456 | 480 |
| 457 PasswordInfo password_info; | 481 PasswordInfo password_info; |
| 458 password_info.fill_data = form_data; | 482 password_info.fill_data = form_data; |
| 459 password_info.password_field = password_element; | 483 password_info.password_field = password_element; |
| 460 login_to_password_info_[username_element] = password_info; | 484 login_to_password_info_[username_element] = password_info; |
| 461 } | 485 } |
| 462 } | 486 } |
| 463 | 487 |
| 464 //////////////////////////////////////////////////////////////////////////////// | 488 //////////////////////////////////////////////////////////////////////////////// |
| 465 // PasswordAutofillManager, private: | 489 // PasswordAutofillManager, private: |
| 466 | 490 |
| 467 void PasswordAutofillManager::GetSuggestions( | 491 void PasswordAutofillManager::GetSuggestions( |
| 468 const webkit::forms::PasswordFormFillData& fill_data, | 492 const webkit::forms::PasswordFormFillData& fill_data, |
| 469 const string16& input, | 493 const string16& input, |
| 470 std::vector<string16>* suggestions) { | 494 std::vector<string16>* suggestions) { |
| 471 if (StartsWith(fill_data.basic_data.fields[0].value, input, false)) | 495 // There are both a password and a username. |
| 472 suggestions->push_back(fill_data.basic_data.fields[0].value); | 496 if (HasUsernameField(fill_data) && |
| 497 StartsWith(GetUsernameField(fill_data).value, input, false)) { | |
| 498 suggestions->push_back(GetUsernameField(fill_data).value); | |
| 499 } | |
| 473 | 500 |
| 474 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter; | 501 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter; |
| 475 for (iter = fill_data.additional_logins.begin(); | 502 for (iter = fill_data.additional_logins.begin(); |
| 476 iter != fill_data.additional_logins.end(); ++iter) { | 503 iter != fill_data.additional_logins.end(); ++iter) { |
| 477 if (StartsWith(iter->first, input, false)) | 504 if (StartsWith(iter->first, input, false)) |
| 478 suggestions->push_back(iter->first); | 505 suggestions->push_back(iter->first); |
| 479 } | 506 } |
| 480 } | 507 } |
| 481 | 508 |
| 482 bool PasswordAutofillManager::ShowSuggestionPopup( | 509 bool PasswordAutofillManager::ShowSuggestionPopup( |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 509 WebKit::WebInputElement* username_element, | 536 WebKit::WebInputElement* username_element, |
| 510 WebKit::WebInputElement* password_element, | 537 WebKit::WebInputElement* password_element, |
| 511 const webkit::forms::PasswordFormFillData& fill_data, | 538 const webkit::forms::PasswordFormFillData& fill_data, |
| 512 bool exact_username_match, | 539 bool exact_username_match, |
| 513 bool set_selection) { | 540 bool set_selection) { |
| 514 string16 current_username = username_element->value(); | 541 string16 current_username = username_element->value(); |
| 515 // username and password will contain the match found if any. | 542 // username and password will contain the match found if any. |
| 516 string16 username; | 543 string16 username; |
| 517 string16 password; | 544 string16 password; |
| 518 | 545 |
| 546 // If there isn't any username form, just exit. | |
| 547 // Because this function is used for the case | |
| 548 // that the username form and the possword form exist. | |
| 549 if (!HasUsernameField(fill_data)) | |
| 550 return false; | |
| 551 | |
| 519 // Look for any suitable matches to current field text. | 552 // Look for any suitable matches to current field text. |
| 520 if (DoUsernamesMatch(fill_data.basic_data.fields[0].value, current_username, | 553 if (DoUsernamesMatch(GetUsernameField(fill_data).value, current_username, |
| 521 exact_username_match)) { | 554 exact_username_match)) { |
| 522 username = fill_data.basic_data.fields[0].value; | 555 username = GetUsernameField(fill_data).value; |
| 523 password = fill_data.basic_data.fields[1].value; | 556 password = GetPasswordField(fill_data).value; |
| 524 } else { | 557 } else { |
| 525 // Scan additional logins for a match. | 558 // Scan additional logins for a match. |
| 526 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter; | 559 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter; |
| 527 for (iter = fill_data.additional_logins.begin(); | 560 for (iter = fill_data.additional_logins.begin(); |
| 528 iter != fill_data.additional_logins.end(); ++iter) { | 561 iter != fill_data.additional_logins.end(); ++iter) { |
| 529 if (DoUsernamesMatch(iter->first, current_username, | 562 if (DoUsernamesMatch(iter->first, current_username, |
| 530 exact_username_match)) { | 563 exact_username_match)) { |
| 531 username = iter->first; | 564 username = iter->first; |
| 532 password = iter->second; | 565 password = iter->second; |
| 533 break; | 566 break; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 601 LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(input); | 634 LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(input); |
| 602 if (iter == login_to_password_info_.end()) | 635 if (iter == login_to_password_info_.end()) |
| 603 return false; | 636 return false; |
| 604 | 637 |
| 605 *found_input = input; | 638 *found_input = input; |
| 606 *found_password = iter->second; | 639 *found_password = iter->second; |
| 607 return true; | 640 return true; |
| 608 } | 641 } |
| 609 | 642 |
| 610 } // namespace autofill | 643 } // namespace autofill |
| OLD | NEW |