OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 the username field from fill_data. | |
198 // If it hasn't any username, calls NOTREACHED(). | |
199 const webkit::forms::FormField& GetUsernameField( | |
200 const webkit::forms::PasswordFormFillData& fill_data) { | |
201 if (fill_data.basic_data.fields.size() == 2) { | |
202 return fill_data.basic_data.fields[1]; | |
203 } | |
204 NOTREACHED(); | |
205 // This return expression is to make this function returns a value | |
206 // in all paths. | |
207 // If there is a path which doesn't return any values, | |
208 // most of compiler output a warning or error. | |
209 // (ex. Visual C++ compiler outputs C4715 warning.) | |
210 return fill_data.basic_data.fields[1]; | |
211 } | |
Ilya Sherman
2012/03/27 00:15:28
nit: I would write this method simply as
DCHECK_G
Yumikiyo Osanai
2012/03/27 22:59:28
Thanks! I didn't know the DCHECK_** macro. It seem
| |
212 | |
213 // Get the password field from fill_data. | |
214 const webkit::forms::FormField& GetPasswordField( | |
215 const webkit::forms::PasswordFormFillData& fill_data) { | |
216 if (fill_data.basic_data.fields.size() == 2 || | |
217 fill_data.basic_data.fields.size() == 1) { | |
218 return fill_data.basic_data.fields[0]; | |
219 } | |
220 NOTREACHED(); | |
221 // This return expression is to make this function returns a value | |
222 // in all paths. | |
223 // If there is a path which doesn't return any values, | |
224 // most of compiler output a warning or error. | |
225 // (ex. Visual C++ compiler outputs C4715 warning.) | |
226 return fill_data.basic_data.fields[0]; | |
Ilya Sherman
2012/03/27 00:15:28
nit: I would write this method simply as
DCHECK(!
Yumikiyo Osanai
2012/03/27 22:59:28
Done.
| |
227 } | |
228 | |
197 } // namespace | 229 } // namespace |
198 | 230 |
199 namespace autofill { | 231 namespace autofill { |
200 | 232 |
201 //////////////////////////////////////////////////////////////////////////////// | 233 //////////////////////////////////////////////////////////////////////////////// |
202 // PasswordAutofillManager, public: | 234 // PasswordAutofillManager, public: |
203 | 235 |
204 PasswordAutofillManager::PasswordAutofillManager( | 236 PasswordAutofillManager::PasswordAutofillManager( |
205 content::RenderView* render_view) | 237 content::RenderView* render_view) |
206 : content::RenderViewObserver(render_view), | 238 : content::RenderViewObserver(render_view), |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
415 // We own the FormElements* in forms. | 447 // We own the FormElements* in forms. |
416 FindFormElements(render_view()->GetWebView(), form_data.basic_data, &forms); | 448 FindFormElements(render_view()->GetWebView(), form_data.basic_data, &forms); |
417 FormElementsList::iterator iter; | 449 FormElementsList::iterator iter; |
418 for (iter = forms.begin(); iter != forms.end(); ++iter) { | 450 for (iter = forms.begin(); iter != forms.end(); ++iter) { |
419 scoped_ptr<FormElements> form_elements(*iter); | 451 scoped_ptr<FormElements> form_elements(*iter); |
420 | 452 |
421 // If wait_for_username is true, we don't want to initially fill the form | 453 // If wait_for_username is true, we don't want to initially fill the form |
422 // until the user types in a valid username. | 454 // until the user types in a valid username. |
423 if (!form_data.wait_for_username) | 455 if (!form_data.wait_for_username) |
424 FillForm(form_elements.get(), form_data.basic_data); | 456 FillForm(form_elements.get(), form_data.basic_data); |
425 | 457 |
Ilya Sherman
2012/03/27 00:15:28
nit: There's no need to do anything else on this i
Yumikiyo Osanai
2012/03/27 22:59:28
I've defined the HasUsernameField() and changed to
Ilya Sherman
2012/03/27 23:28:57
Sorry, I think my comment was a little unclear. W
Yumikiyo Osanai
2012/03/28 00:11:00
Thank you for writing the detail:)
I've moved the
| |
426 // Attach autocomplete listener to enable selecting alternate logins. | |
427 // First, get pointers to username element. | |
428 WebKit::WebInputElement username_element = | |
429 form_elements->input_elements[form_data.basic_data.fields[0].name]; | |
430 | |
431 // Get pointer to password element. (We currently only support single | 458 // Get pointer to password element. (We currently only support single |
432 // password forms). | 459 // password forms). |
433 WebKit::WebInputElement password_element = | 460 WebKit::WebInputElement password_element = |
434 form_elements->input_elements[form_data.basic_data.fields[1].name]; | 461 form_elements->input_elements[GetPasswordField(form_data).name]; |
435 | 462 |
436 // We might have already filled this form if there are two <form> elements | 463 // If there is a username, set the username and login_to_password_info_. |
437 // with identical markup. | 464 if (form_data.basic_data.fields.size() == 2) { |
438 if (login_to_password_info_.find(username_element) != | 465 // Attach autocomplete listener to enable selecting alternate logins. |
439 login_to_password_info_.end()) | 466 // First, get pointers to username element. |
440 continue; | 467 WebKit::WebInputElement username_element = |
441 | 468 form_elements->input_elements[GetUsernameField(form_data).name]; |
442 PasswordInfo password_info; | 469 // We might have already filled this form if there are two <form> elements |
443 password_info.fill_data = form_data; | 470 // with identical markup. |
444 password_info.password_field = password_element; | 471 if (login_to_password_info_.find(username_element) != |
445 login_to_password_info_[username_element] = password_info; | 472 login_to_password_info_.end()) |
473 continue; | |
474 PasswordInfo password_info; | |
475 password_info.fill_data = form_data; | |
476 password_info.password_field = password_element; | |
477 login_to_password_info_[username_element] = password_info; | |
478 } | |
446 } | 479 } |
447 } | 480 } |
448 | 481 |
449 //////////////////////////////////////////////////////////////////////////////// | 482 //////////////////////////////////////////////////////////////////////////////// |
450 // PasswordAutofillManager, private: | 483 // PasswordAutofillManager, private: |
451 | 484 |
452 void PasswordAutofillManager::GetSuggestions( | 485 void PasswordAutofillManager::GetSuggestions( |
453 const webkit::forms::PasswordFormFillData& fill_data, | 486 const webkit::forms::PasswordFormFillData& fill_data, |
454 const string16& input, | 487 const string16& input, |
455 std::vector<string16>* suggestions) { | 488 std::vector<string16>* suggestions) { |
456 if (StartsWith(fill_data.basic_data.fields[0].value, input, false)) | 489 // There are both a password and a username. |
457 suggestions->push_back(fill_data.basic_data.fields[0].value); | 490 if (fill_data.basic_data.fields.size() == 2) { |
458 | 491 if (StartsWith(GetUsernameField(fill_data).value, input, false)) |
Ilya Sherman
2012/03/27 00:15:28
nit: Please use "&&" rather than nested if-stmts
Yumikiyo Osanai
2012/03/27 22:59:28
Done.
| |
492 suggestions->push_back(GetUsernameField(fill_data).value); | |
493 } | |
459 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter; | 494 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter; |
460 for (iter = fill_data.additional_logins.begin(); | 495 for (iter = fill_data.additional_logins.begin(); |
461 iter != fill_data.additional_logins.end(); ++iter) { | 496 iter != fill_data.additional_logins.end(); ++iter) { |
462 if (StartsWith(iter->first, input, false)) | 497 if (StartsWith(iter->first, input, false)) |
463 suggestions->push_back(iter->first); | 498 suggestions->push_back(iter->first); |
464 } | 499 } |
465 } | 500 } |
466 | 501 |
467 bool PasswordAutofillManager::ShowSuggestionPopup( | 502 bool PasswordAutofillManager::ShowSuggestionPopup( |
468 const webkit::forms::PasswordFormFillData& fill_data, | 503 const webkit::forms::PasswordFormFillData& fill_data, |
(...skipping 25 matching lines...) Expand all Loading... | |
494 WebKit::WebInputElement* username_element, | 529 WebKit::WebInputElement* username_element, |
495 WebKit::WebInputElement* password_element, | 530 WebKit::WebInputElement* password_element, |
496 const webkit::forms::PasswordFormFillData& fill_data, | 531 const webkit::forms::PasswordFormFillData& fill_data, |
497 bool exact_username_match, | 532 bool exact_username_match, |
498 bool set_selection) { | 533 bool set_selection) { |
499 string16 current_username = username_element->value(); | 534 string16 current_username = username_element->value(); |
500 // username and password will contain the match found if any. | 535 // username and password will contain the match found if any. |
501 string16 username; | 536 string16 username; |
502 string16 password; | 537 string16 password; |
503 | 538 |
539 // If there isn't any username form, just exit. | |
540 // Because this function is used for the case | |
541 // that the username form and the possword form exist. | |
542 if (fill_data.basic_data.fields.size() == 1) { | |
543 return false; | |
Ilya Sherman
2012/03/27 00:15:28
nit: No need for curly braces, since this if-stmt'
Yumikiyo Osanai
2012/03/27 22:59:28
Done. And I've changed to use the HasUsernameField
| |
544 } | |
545 | |
504 // Look for any suitable matches to current field text. | 546 // Look for any suitable matches to current field text. |
505 if (DoUsernamesMatch(fill_data.basic_data.fields[0].value, current_username, | 547 if (DoUsernamesMatch(GetUsernameField(fill_data).value, current_username, |
506 exact_username_match)) { | 548 exact_username_match)) { |
507 username = fill_data.basic_data.fields[0].value; | 549 username = GetUsernameField(fill_data).value; |
508 password = fill_data.basic_data.fields[1].value; | 550 password = GetPasswordField(fill_data).value; |
509 } else { | 551 } else { |
510 // Scan additional logins for a match. | 552 // Scan additional logins for a match. |
511 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter; | 553 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter; |
512 for (iter = fill_data.additional_logins.begin(); | 554 for (iter = fill_data.additional_logins.begin(); |
513 iter != fill_data.additional_logins.end(); ++iter) { | 555 iter != fill_data.additional_logins.end(); ++iter) { |
514 if (DoUsernamesMatch(iter->first, current_username, | 556 if (DoUsernamesMatch(iter->first, current_username, |
515 exact_username_match)) { | 557 exact_username_match)) { |
516 username = iter->first; | 558 username = iter->first; |
517 password = iter->second; | 559 password = iter->second; |
518 break; | 560 break; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
586 LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(input); | 628 LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(input); |
587 if (iter == login_to_password_info_.end()) | 629 if (iter == login_to_password_info_.end()) |
588 return false; | 630 return false; |
589 | 631 |
590 *found_input = input; | 632 *found_input = input; |
591 *found_password = iter->second; | 633 *found_password = iter->second; |
592 return true; | 634 return true; |
593 } | 635 } |
594 | 636 |
595 } // namespace autofill | 637 } // namespace autofill |
OLD | NEW |