Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(385)

Side by Side Diff: chrome/renderer/autofill/password_autofill_manager.cc

Issue 9625026: Save password without an associated username. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Refine the unit_test codes Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 } 189 }
190 190
191 bool DoUsernamesMatch(const string16& username1, 191 bool DoUsernamesMatch(const string16& username1,
192 const string16& username2, 192 const string16& username2,
193 bool exact_match) { 193 bool exact_match) {
194 if (exact_match) 194 if (exact_match)
195 return username1 == username2; 195 return username1 == username2;
196 return StartsWith(username1, username2, true); 196 return StartsWith(username1, username2, true);
197 } 197 }
198 198
199 // Get whether fill_data has a username or not.
200 bool HasUsernameField(const webkit::forms::PasswordFormFillData& fill_data) {
201 return fill_data.basic_data.fields.size() == 2;
202 }
203
204 // Get the username field from fill_data.
205 // If it hasn't any username, calls NOTREACHED().
206 const webkit::forms::FormField& GetUsernameField(
207 const webkit::forms::PasswordFormFillData& fill_data) {
208 DCHECK(HasUsernameField(fill_data));
209 return fill_data.basic_data.fields[1];
210 }
211
212 // Get the password field from fill_data.
213 const webkit::forms::FormField& GetPasswordField(
214 const webkit::forms::PasswordFormFillData& fill_data) {
215 DCHECK(!fill_data.basic_data.fields.empty());
216 return fill_data.basic_data.fields[0];
217 }
199 } // namespace 218 } // namespace
200 219
201 namespace autofill { 220 namespace autofill {
202 221
203 //////////////////////////////////////////////////////////////////////////////// 222 ////////////////////////////////////////////////////////////////////////////////
204 // PasswordAutofillManager, public: 223 // PasswordAutofillManager, public:
205 224
206 PasswordAutofillManager::PasswordAutofillManager( 225 PasswordAutofillManager::PasswordAutofillManager(
207 content::RenderView* render_view) 226 content::RenderView* render_view)
208 : content::RenderViewObserver(render_view), 227 : content::RenderViewObserver(render_view),
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 FindFormElements(render_view()->GetWebView(), form_data.basic_data, &forms); 460 FindFormElements(render_view()->GetWebView(), form_data.basic_data, &forms);
442 FormElementsList::iterator iter; 461 FormElementsList::iterator iter;
443 for (iter = forms.begin(); iter != forms.end(); ++iter) { 462 for (iter = forms.begin(); iter != forms.end(); ++iter) {
444 scoped_ptr<FormElements> form_elements(*iter); 463 scoped_ptr<FormElements> form_elements(*iter);
445 464
446 // If wait_for_username is true, we don't want to initially fill the form 465 // If wait_for_username is true, we don't want to initially fill the form
447 // until the user types in a valid username. 466 // until the user types in a valid username.
448 if (!form_data.wait_for_username) 467 if (!form_data.wait_for_username)
449 FillForm(form_elements.get(), form_data.basic_data); 468 FillForm(form_elements.get(), form_data.basic_data);
450 469
470 // If there isn't any username, go to next iteration.
471 if (!HasUsernameField(form_data))
472 continue;
473
474 // If there is a username, set the username and login_to_password_info_.
451 // Attach autocomplete listener to enable selecting alternate logins. 475 // Attach autocomplete listener to enable selecting alternate logins.
452 // First, get pointers to username element. 476 // First, get pointers to username element.
453 WebKit::WebInputElement username_element = 477 WebKit::WebInputElement username_element =
454 form_elements->input_elements[form_data.basic_data.fields[0].name]; 478 form_elements->input_elements[GetUsernameField(form_data).name];
455 479
456 // Get pointer to password element. (We currently only support single 480 // Get pointer to password element. (We currently only support single
457 // password forms). 481 // password forms).
458 WebKit::WebInputElement password_element = 482 WebKit::WebInputElement password_element =
459 form_elements->input_elements[form_data.basic_data.fields[1].name]; 483 form_elements->input_elements[GetPasswordField(form_data).name];
460 484
461 // We might have already filled this form if there are two <form> elements 485 // We might have already filled this form if there are two <form> elements
462 // with identical markup. 486 // with identical markup.
463 if (login_to_password_info_.find(username_element) != 487 if (login_to_password_info_.find(username_element) !=
464 login_to_password_info_.end()) 488 login_to_password_info_.end())
465 continue; 489 continue;
466 490
467 PasswordInfo password_info; 491 PasswordInfo password_info;
468 password_info.fill_data = form_data; 492 password_info.fill_data = form_data;
469 password_info.password_field = password_element; 493 password_info.password_field = password_element;
(...skipping 10 matching lines...) Expand all
480 } 504 }
481 } 505 }
482 506
483 //////////////////////////////////////////////////////////////////////////////// 507 ////////////////////////////////////////////////////////////////////////////////
484 // PasswordAutofillManager, private: 508 // PasswordAutofillManager, private:
485 509
486 void PasswordAutofillManager::GetSuggestions( 510 void PasswordAutofillManager::GetSuggestions(
487 const webkit::forms::PasswordFormFillData& fill_data, 511 const webkit::forms::PasswordFormFillData& fill_data,
488 const string16& input, 512 const string16& input,
489 std::vector<string16>* suggestions) { 513 std::vector<string16>* suggestions) {
490 if (StartsWith(fill_data.basic_data.fields[0].value, input, false)) 514 // There are both a password and a username.
491 suggestions->push_back(fill_data.basic_data.fields[0].value); 515 if (HasUsernameField(fill_data) &&
516 StartsWith(GetUsernameField(fill_data).value, input, false)) {
517 suggestions->push_back(GetUsernameField(fill_data).value);
518 }
492 519
493 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter; 520 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter;
494 for (iter = fill_data.additional_logins.begin(); 521 for (iter = fill_data.additional_logins.begin();
495 iter != fill_data.additional_logins.end(); ++iter) { 522 iter != fill_data.additional_logins.end(); ++iter) {
496 if (StartsWith(iter->first, input, false)) 523 if (StartsWith(iter->first, input, false))
497 suggestions->push_back(iter->first); 524 suggestions->push_back(iter->first);
498 } 525 }
499 } 526 }
500 527
501 bool PasswordAutofillManager::ShowSuggestionPopup( 528 bool PasswordAutofillManager::ShowSuggestionPopup(
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 WebKit::WebInputElement* username_element, 573 WebKit::WebInputElement* username_element,
547 WebKit::WebInputElement* password_element, 574 WebKit::WebInputElement* password_element,
548 const webkit::forms::PasswordFormFillData& fill_data, 575 const webkit::forms::PasswordFormFillData& fill_data,
549 bool exact_username_match, 576 bool exact_username_match,
550 bool set_selection) { 577 bool set_selection) {
551 string16 current_username = username_element->value(); 578 string16 current_username = username_element->value();
552 // username and password will contain the match found if any. 579 // username and password will contain the match found if any.
553 string16 username; 580 string16 username;
554 string16 password; 581 string16 password;
555 582
583 // If there isn't any username form, just exit.
584 // Because this function is used for the case
585 // that the username form and the possword form exist.
586 if (!HasUsernameField(fill_data))
587 return false;
588
556 // Look for any suitable matches to current field text. 589 // Look for any suitable matches to current field text.
557 if (DoUsernamesMatch(fill_data.basic_data.fields[0].value, current_username, 590 if (DoUsernamesMatch(GetUsernameField(fill_data).value, current_username,
558 exact_username_match)) { 591 exact_username_match)) {
559 username = fill_data.basic_data.fields[0].value; 592 username = GetUsernameField(fill_data).value;
560 password = fill_data.basic_data.fields[1].value; 593 password = GetPasswordField(fill_data).value;
561 } else { 594 } else {
562 // Scan additional logins for a match. 595 // Scan additional logins for a match.
563 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter; 596 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter;
564 for (iter = fill_data.additional_logins.begin(); 597 for (iter = fill_data.additional_logins.begin();
565 iter != fill_data.additional_logins.end(); ++iter) { 598 iter != fill_data.additional_logins.end(); ++iter) {
566 if (DoUsernamesMatch(iter->first, current_username, 599 if (DoUsernamesMatch(iter->first, current_username,
567 exact_username_match)) { 600 exact_username_match)) {
568 username = iter->first; 601 username = iter->first;
569 password = iter->second; 602 password = iter->second;
570 break; 603 break;
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
638 LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(input); 671 LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(input);
639 if (iter == login_to_password_info_.end()) 672 if (iter == login_to_password_info_.end())
640 return false; 673 return false;
641 674
642 *found_input = input; 675 *found_input = input;
643 *found_password = iter->second; 676 *found_password = iter->second;
644 return true; 677 return true;
645 } 678 }
646 679
647 } // namespace autofill 680 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698